char.c 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. // original code from athena
  4. // SQL conversion by Jioh L. Jung
  5. // TXT 1.105
  6. #include <sys/types.h>
  7. #ifdef _WIN32
  8. #include <winsock.h>
  9. typedef long in_addr_t;
  10. //#pragma lib <libmysql.lib> // Not required [Lance]
  11. #else
  12. #include <sys/socket.h>
  13. #include <netinet/in.h>
  14. #include <sys/time.h>
  15. #include <sys/ioctl.h>
  16. #include <arpa/inet.h>
  17. #include <netdb.h>
  18. #include <unistd.h>
  19. #endif
  20. #include <time.h>
  21. #include <signal.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <limits.h>
  28. #include "char.h"
  29. #include "../common/utils.h"
  30. #include "../common/strlib.h"
  31. #include "../common/showmsg.h"
  32. #include "itemdb.h"
  33. #include "inter.h"
  34. #include "db.h"
  35. #include "malloc.h"
  36. #include "int_guild.h"
  37. static struct dbt *char_db_;
  38. char char_db[256] = "char";
  39. char scdata_db[256] = "sc_data";
  40. char cart_db[256] = "cart_inventory";
  41. char inventory_db[256] = "inventory";
  42. char charlog_db[256] = "charlog";
  43. char storage_db[256] = "storage";
  44. char interlog_db[256] = "interlog";
  45. char reg_db[256] = "global_reg_value";
  46. char skill_db[256] = "skill";
  47. char memo_db[256] = "memo";
  48. char guild_db[256] = "guild";
  49. char guild_alliance_db[256] = "guild_alliance";
  50. char guild_castle_db[256] = "guild_castle";
  51. char guild_expulsion_db[256] = "guild_expulsion";
  52. char guild_member_db[256] = "guild_member";
  53. char guild_position_db[256] = "guild_position";
  54. char guild_skill_db[256] = "guild_skill";
  55. char guild_storage_db[256] = "guild_storage";
  56. char party_db[256] = "party";
  57. char pet_db[256] = "pet";
  58. char gm_db[256] = "gm_accounts";
  59. char friend_db[256] = "friends";
  60. int db_use_sqldbs;
  61. char login_db_account_id[32] = "account_id";
  62. char login_db_level[32] = "level";
  63. int lowest_gm_level = 1;
  64. char *SQL_CONF_NAME = "conf/inter_athena.conf";
  65. struct mmo_map_server server[MAX_MAP_SERVERS];
  66. int server_fd[MAX_MAP_SERVERS];
  67. int login_fd, char_fd;
  68. char userid[24];
  69. char passwd[24];
  70. char server_name[20];
  71. char wisp_server_name[NAME_LENGTH] = "Server";
  72. int login_ip_set_ = 0;
  73. char login_ip_str[128];
  74. in_addr_t login_ip;
  75. int login_port = 6900;
  76. int char_ip_set_ = 0;
  77. char char_ip_str[128];
  78. int bind_ip_set_ = 0;
  79. char bind_ip_str[128];
  80. in_addr_t char_ip;
  81. int char_port = 6121;
  82. int char_maintenance;
  83. int char_new;
  84. int char_new_display;
  85. int name_ignoring_case = 0; // Allow or not identical name for characters but with a different case by [Yor]
  86. int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  87. char char_name_letters[1024] = ""; // list of letters/symbols used to authorise or not a name of a character. by [Yor]
  88. //The following are characters that are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  89. #define TRIM_CHARS "\032\t\n "
  90. int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius]
  91. int log_char = 1; // loggin char or not [devil]
  92. int log_inter = 1; // loggin inter or not [devil]
  93. // Advanced subnet check [LuzZza]
  94. struct _subnet {
  95. long subnet;
  96. long mask;
  97. long char_ip;
  98. long map_ip;
  99. } subnet[16];
  100. int subnet_count = 0;
  101. char unknown_char_name[NAME_LENGTH] = "Unknown";
  102. char db_path[1024]="db";
  103. //These are used to aid the map server in identifying valid clients. [Skotlex]
  104. static int max_account_id = DEFAULT_MAX_ACCOUNT_ID, max_char_id = DEFAULT_MAX_CHAR_ID;
  105. static int online_check = 1; //If one, it won't let players connect when their account is already registered online and will send the relevant map server a kick user request. [Skotlex]
  106. struct char_session_data{
  107. int account_id, login_id1, login_id2,sex;
  108. int found_char[9];
  109. char email[40]; // e-mail (default: a@a.com) by [Yor]
  110. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  111. };
  112. #define AUTH_FIFO_SIZE 256
  113. struct {
  114. int account_id, char_id, login_id1, login_id2, ip, char_pos, delflag,sex;
  115. time_t connect_until_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  116. } auth_fifo[AUTH_FIFO_SIZE];
  117. int auth_fifo_pos = 0;
  118. int check_ip_flag = 1; // It's to check IP of a player between char-server and other servers (part of anti-hacking system)
  119. struct mmo_charstatus *char_dat;
  120. int char_num,char_max;
  121. int max_connect_user = 0;
  122. int gm_allow_level = 99;
  123. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  124. int save_log = 1;
  125. int start_zeny = 500;
  126. int start_weapon = 1201;
  127. int start_armor = 2301;
  128. //Custom limits for the fame lists. [Skotlex]
  129. int fame_list_size_chemist = MAX_FAME_LIST;
  130. int fame_list_size_smith = MAX_FAME_LIST;
  131. int fame_list_size_taekwon = MAX_FAME_LIST;
  132. // check for exit signal
  133. // 0 is saving complete
  134. // other is char_id
  135. unsigned int save_flag = 0;
  136. // start point (you can reset point on conf file)
  137. struct point start_point = { 0, 53, 111};
  138. bool char_gm_read = false;
  139. struct gm_account *gm_account = NULL;
  140. int GM_num = 0;
  141. int console = 0;
  142. //Structure for holding in memory which characters are online on the map servers connected.
  143. struct online_char_data {
  144. int account_id;
  145. int char_id;
  146. short server;
  147. unsigned waiting_disconnect :1;
  148. };
  149. struct dbt *online_char_db; //Holds all online characters.
  150. #ifndef SQL_DEBUG
  151. #define mysql_query(_x, _y) mysql_real_query(_x, _y, strlen(_y)) //supports ' in names and runs faster [Kevin]
  152. #else
  153. #define mysql_query(_x, _y) debug_mysql_query(__FILE__, __LINE__, _x, _y)
  154. #endif
  155. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data);
  156. static void * create_online_char_data(DBKey key, va_list args) {
  157. struct online_char_data* character;
  158. character = aCalloc(1, sizeof(struct online_char_data));
  159. character->account_id = key.i;
  160. character->char_id = -1;
  161. character->server = -1;
  162. return character;
  163. }
  164. //-------------------------------------------------
  165. // Set Character online/offline [Wizputer]
  166. //-------------------------------------------------
  167. void set_char_online(int map_id, int char_id, int account_id) {
  168. struct online_char_data* character;
  169. if ( char_id != 99 ) {
  170. sprintf(tmp_sql, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d'",char_db,char_id);
  171. if (mysql_query(&mysql_handle, tmp_sql)) {
  172. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  173. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  174. }
  175. if (max_account_id < account_id || max_char_id < char_id)
  176. { //Notify map-server of the new max IDs [Skotlex]
  177. if (account_id > max_account_id)
  178. max_account_id = account_id;
  179. if (char_id > max_char_id)
  180. max_char_id = char_id;
  181. mapif_send_maxid(max_account_id, max_char_id);
  182. }
  183. }
  184. character = idb_ensure(online_char_db, account_id, create_online_char_data);
  185. if (online_check && character->char_id != -1 && character->server > -1 && character->server != map_id)
  186. {
  187. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  188. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  189. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  190. }
  191. character->char_id = (char_id==99)?-1:char_id;
  192. character->server = (char_id==99)?-1:map_id;
  193. character->waiting_disconnect = 0;
  194. if (char_id != 99)
  195. { //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  196. struct mmo_charstatus *cp;
  197. cp = idb_get(char_db_,char_id);
  198. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  199. }
  200. if (login_fd <= 0 || session[login_fd]->eof)
  201. return;
  202. WFIFOW(login_fd,0) = 0x272b;
  203. WFIFOL(login_fd,2) = account_id;
  204. WFIFOSET(login_fd,6);
  205. }
  206. void set_char_offline(int char_id, int account_id) {
  207. struct mmo_charstatus *cp;
  208. struct online_char_data* character;
  209. if ( char_id == 99 )
  210. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id);
  211. else {
  212. cp = idb_get(char_db_,char_id);
  213. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  214. if (cp)
  215. idb_remove(char_db_,char_id);
  216. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, char_id);
  217. if (mysql_query(&mysql_handle, tmp_sql))
  218. {
  219. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  220. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  221. }
  222. }
  223. if ((character = idb_get(online_char_db, account_id)) != NULL)
  224. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  225. character->char_id = -1;
  226. character->server = -1;
  227. character->waiting_disconnect = 0;
  228. }
  229. if (login_fd <= 0 || session[login_fd]->eof)
  230. return;
  231. WFIFOW(login_fd,0) = 0x272c;
  232. WFIFOL(login_fd,2) = account_id;
  233. WFIFOSET(login_fd,6);
  234. }
  235. static int char_db_setoffline(DBKey key, void* data, va_list ap) {
  236. struct online_char_data* character = (struct online_char_data*)data;
  237. int server = va_arg(ap, int);
  238. if (server == -1) {
  239. character->char_id = -1;
  240. character->server = -1;
  241. character->waiting_disconnect = 0;
  242. } else if (character->server == server)
  243. character->server = -2; //In some map server that we aren't connected to.
  244. return 0;
  245. }
  246. void set_all_offline(void) {
  247. MYSQL_RES* sql_res2; //Needed because it is used inside inter_guild_CharOffline; [Skotlex]
  248. int char_id;
  249. sprintf(tmp_sql, "SELECT `account_id`, `char_id`, `guild_id` FROM `%s` WHERE `online`='1'",char_db);
  250. if (mysql_query(&mysql_handle, tmp_sql)) {
  251. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  252. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  253. }
  254. ShowNotice("Sending all users offline.\n");
  255. sql_res2 = mysql_store_result(&mysql_handle);
  256. if (sql_res2) {
  257. while((sql_row = mysql_fetch_row(sql_res2))) {
  258. char_id = atoi(sql_row[1]);
  259. inter_guild_CharOffline(char_id, atoi(sql_row[2]));
  260. if ( login_fd > 0 ) {
  261. ShowInfo("send user offline: %d\n",char_id);
  262. WFIFOW(login_fd,0) = 0x272c;
  263. WFIFOL(login_fd,2) = char_id;
  264. WFIFOSET(login_fd,6);
  265. }
  266. }
  267. mysql_free_result(sql_res2);
  268. }
  269. sprintf(tmp_sql,"UPDATE `%s` SET `online`='0' WHERE `online`='1'", char_db);
  270. if (mysql_query(&mysql_handle, tmp_sql)) {
  271. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  272. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  273. }
  274. online_char_db->foreach(online_char_db,char_db_setoffline,-1);
  275. }
  276. //----------------------------------------------------------------------
  277. // Determine if an account (id) is a GM account
  278. // and returns its level (or 0 if it isn't a GM account or if not found)
  279. //----------------------------------------------------------------------
  280. // Removed since nothing GM related goes on in the char server [CLOWNISIUS]
  281. int isGM(int account_id) {
  282. int i;
  283. for(i = 0; i < GM_num; i++)
  284. if (gm_account[i].account_id == account_id)
  285. return gm_account[i].level;
  286. return 0;
  287. }
  288. void read_gm_account(void) {
  289. if(char_gm_read)
  290. {
  291. if (gm_account != NULL)
  292. aFree(gm_account);
  293. GM_num = 0;
  294. sprintf(tmp_sql, "SELECT `%s`,`%s` FROM `%s` WHERE `%s`>='%d'",login_db_account_id,login_db_level,gm_db,login_db_level,lowest_gm_level);
  295. if (mysql_query(&lmysql_handle, tmp_sql)) {
  296. ShowSQL("DB error - %s\n",mysql_error(&lmysql_handle));
  297. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  298. }
  299. lsql_res = mysql_store_result(&lmysql_handle);
  300. if (lsql_res) {
  301. gm_account = (struct gm_account*)aCalloc(sizeof(struct gm_account) * (size_t)mysql_num_rows(lsql_res), 1);
  302. while ((lsql_row = mysql_fetch_row(lsql_res))) {
  303. gm_account[GM_num].account_id = atoi(lsql_row[0]);
  304. gm_account[GM_num].level = atoi(lsql_row[1]);
  305. GM_num++;
  306. }
  307. }
  308. mysql_free_result(lsql_res);
  309. mapif_send_gmaccounts();
  310. }
  311. }
  312. int compare_item(struct item *a, struct item *b) {
  313. if(a->id == b->id &&
  314. a->nameid == b->nameid &&
  315. a->amount == b->amount &&
  316. a->equip == b->equip &&
  317. a->identify == b->identify &&
  318. a->refine == b->refine &&
  319. a->attribute == b->attribute)
  320. {
  321. int i;
  322. for (i=0; i<MAX_SLOTS && a->card[i]==b->card[i]; i++);
  323. return (i == MAX_SLOTS);
  324. }
  325. return 0;
  326. }
  327. static void* create_charstatus(DBKey key, va_list args) {
  328. struct mmo_charstatus *cp;
  329. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  330. cp->char_id = key.i;
  331. return cp;
  332. }
  333. int mmo_char_tosql(int char_id, struct mmo_charstatus *p){
  334. int i=0,j;
  335. int count = 0;
  336. int diff = 0;
  337. char *tmp_ptr; //Building a single query should be more efficient than running
  338. //multiple queries for each thing about to be saved, right? [Skotlex]
  339. char save_status[128]; //For displaying save information. [Skotlex]
  340. struct mmo_charstatus *cp;
  341. struct itemtmp mapitem[MAX_GUILD_STORAGE];
  342. if (char_id!=p->char_id) return 0;
  343. cp = idb_ensure(char_db_, char_id, create_charstatus);
  344. // ShowInfo("Saving char "CL_WHITE"%d"CL_RESET" (%s)...\n",char_id,char_dat[0].name);
  345. memset(save_status, 0, sizeof(save_status));
  346. diff = 0;
  347. //map inventory data
  348. for(i=0;i<MAX_INVENTORY;i++){
  349. if (!compare_item(&p->inventory[i], &cp->inventory[i]))
  350. diff = 1;
  351. if(p->inventory[i].nameid>0){
  352. mapitem[count].flag=0;
  353. mapitem[count].id = p->inventory[i].id;
  354. mapitem[count].nameid=p->inventory[i].nameid;
  355. mapitem[count].amount = p->inventory[i].amount;
  356. mapitem[count].equip = p->inventory[i].equip;
  357. mapitem[count].identify = p->inventory[i].identify;
  358. mapitem[count].refine = p->inventory[i].refine;
  359. mapitem[count].attribute = p->inventory[i].attribute;
  360. for (j=0; j<MAX_SLOTS; j++)
  361. mapitem[count].card[j] = p->inventory[i].card[j];
  362. count++;
  363. }
  364. }
  365. //printf("- Save item data to MySQL!\n");
  366. if (diff)
  367. if (!memitemdata_to_sql(mapitem, count, p->char_id,TABLE_INVENTORY))
  368. strcat(save_status, " inventory");
  369. count = 0;
  370. diff = 0;
  371. //map cart data
  372. for(i=0;i<MAX_CART;i++){
  373. if (!compare_item(&p->cart[i], &cp->cart[i]))
  374. diff = 1;
  375. if(p->cart[i].nameid>0){
  376. mapitem[count].flag=0;
  377. mapitem[count].id = p->cart[i].id;
  378. mapitem[count].nameid=p->cart[i].nameid;
  379. mapitem[count].amount = p->cart[i].amount;
  380. mapitem[count].equip = p->cart[i].equip;
  381. mapitem[count].identify = p->cart[i].identify;
  382. mapitem[count].refine = p->cart[i].refine;
  383. mapitem[count].attribute = p->cart[i].attribute;
  384. for (j=0; j<MAX_SLOTS; j++)
  385. mapitem[count].card[j] = p->cart[i].card[j];
  386. count++;
  387. }
  388. }
  389. if (diff)
  390. if (!memitemdata_to_sql(mapitem, count, p->char_id,TABLE_CART))
  391. strcat(save_status, " cart");
  392. if (
  393. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  394. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  395. (p->zeny != cp->zeny) ||
  396. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  397. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  398. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  399. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  400. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  401. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  402. (p->option != cp->option) ||
  403. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  404. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) ||
  405. (p->shield != cp->shield) || (p->head_top != cp->head_top) ||
  406. (p->head_mid != cp->head_mid) || (p->head_bottom != cp->head_bottom)
  407. )
  408. { //Save status
  409. sprintf(tmp_sql ,"UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  410. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  411. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  412. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  413. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',"
  414. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  415. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d'"
  416. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  417. char_db, p->base_level, p->job_level,
  418. p->base_exp, p->job_exp, p->zeny,
  419. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  420. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  421. p->option, p->party_id, p->guild_id, p->pet_id,
  422. p->weapon, p->shield, p->head_top, p->head_mid, p->head_bottom,
  423. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  424. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y,
  425. p->account_id, p->char_id
  426. );
  427. if(mysql_query(&mysql_handle, tmp_sql))
  428. {
  429. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  430. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  431. } else
  432. strcat(save_status, " status");
  433. }
  434. //Values that will seldom change (to speed up saving)
  435. if (
  436. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  437. (p->class_ != cp->class_) ||
  438. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  439. (p->mother != cp->mother) || (p->child != cp->child) ||
  440. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  441. (p->fame != cp->fame)
  442. )
  443. {
  444. sprintf(tmp_sql ,"UPDATE `%s` SET `class`='%d',"
  445. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  446. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  447. "`karma`='%d',`manner`='%d', `fame`='%d'"
  448. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  449. char_db, p->class_,
  450. p->hair, p->hair_color, p->clothes_color,
  451. p->partner_id, p->father, p->mother, p->child,
  452. p->karma, p->manner, p->fame,
  453. p->account_id, p->char_id
  454. );
  455. if(mysql_query(&mysql_handle, tmp_sql))
  456. {
  457. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  458. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  459. } else
  460. strcat(save_status, " status2");
  461. }
  462. diff = 0;
  463. for(i=0;i<MAX_MEMOPOINTS;i++){
  464. if(p->memo_point[i].map == cp->memo_point[i].map && p->memo_point[i].x == cp->memo_point[i].x && p->memo_point[i].y == cp->memo_point[i].y)
  465. continue;
  466. diff = 1;
  467. break;
  468. }
  469. if (diff)
  470. { //Save memo
  471. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  472. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, p->char_id);
  473. if(mysql_query(&mysql_handle, tmp_sql))
  474. {
  475. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  476. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  477. }
  478. //insert here.
  479. tmp_ptr = tmp_sql;
  480. tmp_ptr += sprintf(tmp_ptr, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  481. count = 0;
  482. for(i=0;i<MAX_MEMOPOINTS;i++){
  483. if(p->memo_point[i].map){
  484. tmp_ptr += sprintf(tmp_ptr,"('%d', '%s', '%d', '%d'),",
  485. char_id, mapindex_id2name(p->memo_point[i].map), p->memo_point[i].x, p->memo_point[i].y);
  486. count++;
  487. }
  488. }
  489. if (count)
  490. { //Dangerous? Only if none of the above sprintf worked. [Skotlex]
  491. tmp_ptr[-1] = '\0'; //Remove the trailing comma.
  492. if(mysql_query(&mysql_handle, tmp_sql))
  493. {
  494. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  495. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  496. } else
  497. strcat(save_status, " memo");
  498. } else //Memo Points cleared (how is this possible?).
  499. strcat(save_status, " memo");
  500. }
  501. diff = 0;
  502. for(i=0;i<MAX_SKILL;i++) {
  503. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  504. p->skill[i].id = i; // Fix skill tree
  505. if((p->skill[i].id != cp->skill[i].id) || (p->skill[i].lv != cp->skill[i].lv) ||
  506. (p->skill[i].flag != cp->skill[i].flag))
  507. {
  508. diff = 1;
  509. break;
  510. }
  511. }
  512. if (diff)
  513. { //Save skills
  514. //`skill` (`char_id`, `id`, `lv`)
  515. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, p->char_id);
  516. if(mysql_query(&mysql_handle, tmp_sql))
  517. {
  518. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  519. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  520. }
  521. tmp_ptr = tmp_sql;
  522. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db);
  523. count = 0;
  524. //insert here.
  525. for(i=0;i<MAX_SKILL;i++){
  526. if(p->skill[i].id && p->skill[i].flag!=1)
  527. {
  528. tmp_ptr += sprintf(tmp_ptr,"('%d','%d','%d'),",
  529. char_id, p->skill[i].id, (p->skill[i].flag==0)?p->skill[i].lv:p->skill[i].flag-2);
  530. count++;
  531. }
  532. }
  533. if (count)
  534. {
  535. tmp_ptr[-1] = '\0'; //Remove trailing comma.
  536. if(mysql_query(&mysql_handle, tmp_sql))
  537. {
  538. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  539. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  540. } else
  541. strcat(save_status, " skills");
  542. } else //Skills removed (reset?)
  543. strcat(save_status, " skills");
  544. }
  545. /* Saving of global registry values is now handled by the inter-server. [Skotlex]
  546. diff = 0;
  547. for(i=0;i<p->global_reg_num;i++) {
  548. if ((p->global_reg[i].str == NULL) && (cp->global_reg[i].str == NULL))
  549. continue;
  550. if (((p->global_reg[i].str == NULL) != (cp->global_reg[i].str == NULL)) ||
  551. strcmp(p->global_reg[i].value, cp->global_reg[i].value) != 0 ||
  552. strcmp(p->global_reg[i].str, cp->global_reg[i].str) != 0
  553. ) {
  554. diff = 1;
  555. break;
  556. }
  557. }
  558. if (diff)
  559. { //Save global registry.
  560. //`global_reg_value` (`char_id`, `str`, `value`)
  561. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, p->char_id);
  562. if (mysql_query(&mysql_handle, tmp_sql))
  563. {
  564. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  565. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  566. }
  567. //insert here.
  568. tmp_ptr = tmp_sql;
  569. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s` (`type`, `char_id`, `str`, `value`) VALUES", reg_db);
  570. count = 0;
  571. for(i=0;i<p->global_reg_num;i++)
  572. {
  573. if (p->global_reg[i].str && p->global_reg[i].value)
  574. {
  575. tmp_ptr += sprintf(tmp_ptr,"('3','%d','%s','%s'),",
  576. char_id, jstrescapecpy(temp_str,p->global_reg[i].str), jstrescapecpy(temp_str2,p->global_reg[i].value));
  577. if (++count%100 == 0)
  578. { //Save every X registers to avoid overflowing tmp_sql [Skotlex]
  579. tmp_ptr[-1] = '\0';
  580. if(mysql_query(&mysql_handle, tmp_sql))
  581. {
  582. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  583. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  584. } else
  585. strcat(save_status, " global_reg");
  586. tmp_ptr = tmp_sql;
  587. tmp_ptr += sprintf(tmp_ptr,"INSERT INTO `%s` (`type`, `char_id`, `str`, `value`) VALUES", reg_db);
  588. count = 0;
  589. }
  590. }
  591. }
  592. if (count)
  593. {
  594. tmp_ptr[-1] = '\0';
  595. if(mysql_query(&mysql_handle, tmp_sql))
  596. {
  597. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  598. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  599. } else
  600. strcat(save_status, " global_reg");
  601. } else //Values cleared.
  602. strcat(save_status, " global_reg");
  603. }
  604. */
  605. diff = 0;
  606. for(i = 0; i < MAX_FRIENDS; i++){
  607. if(p->friends[i].char_id != cp->friends[i].char_id ||
  608. p->friends[i].account_id != cp->friends[i].account_id){
  609. diff = 1;
  610. break;
  611. }
  612. }
  613. if(diff == 1)
  614. { //Save friends
  615. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id);
  616. if(mysql_query(&mysql_handle, tmp_sql)){
  617. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  618. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  619. }
  620. tmp_ptr = tmp_sql;
  621. tmp_ptr += sprintf(tmp_ptr, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  622. count = 0;
  623. for(i = 0; i < MAX_FRIENDS; i++){
  624. if(p->friends[i].char_id > 0)
  625. {
  626. tmp_ptr += sprintf(tmp_ptr, "('%d','%d','%d'),", char_id, p->friends[i].account_id, p->friends[i].char_id);
  627. count++;
  628. }
  629. }
  630. if (count)
  631. {
  632. tmp_ptr[-1] = '\0'; //Remove the last comma. [Skotlex]
  633. if(mysql_query(&mysql_handle, tmp_sql))
  634. {
  635. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  636. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  637. } else
  638. strcat(save_status, " friends");
  639. } else //Friend list cleared.
  640. strcat(save_status, " friends");
  641. }
  642. if (save_status[0]!='\0' && save_log)
  643. ShowInfo("Saved char %d - %s:%s.\n", char_id, char_dat[0].name, save_status);
  644. memcpy(cp, p, sizeof(struct mmo_charstatus));
  645. return 0;
  646. }
  647. // [Ilpalazzo-sama]
  648. int memitemdata_to_sql(struct itemtmp mapitem[], int count, int char_id, int tableswitch)
  649. {
  650. int i,j, flag, id;
  651. char *tablename;
  652. char selectoption[16];
  653. char * str_p = tmp_sql;
  654. switch (tableswitch) {
  655. case TABLE_INVENTORY:
  656. tablename = inventory_db; // no need for sprintf here as *_db are char*.
  657. sprintf(selectoption,"char_id");
  658. break;
  659. case TABLE_CART:
  660. tablename = cart_db;
  661. sprintf(selectoption,"char_id");
  662. break;
  663. case TABLE_STORAGE:
  664. tablename = storage_db;
  665. sprintf(selectoption,"account_id");
  666. break;
  667. case TABLE_GUILD_STORAGE:
  668. tablename = guild_storage_db;
  669. sprintf(selectoption,"guild_id");
  670. break;
  671. default:
  672. ShowError("Invalid table name!\n");
  673. return 1;
  674. }
  675. //=======================================mysql database data > memory===============================================
  676. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  677. for (j=0; j<MAX_SLOTS; j++)
  678. str_p += sprintf(str_p, ", `card%d`", j);
  679. str_p += sprintf(str_p, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, char_id);
  680. if (mysql_query(&mysql_handle, tmp_sql)) {
  681. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  682. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  683. return 1;
  684. }
  685. sql_res = mysql_store_result(&mysql_handle);
  686. if (sql_res) {
  687. while ((sql_row = mysql_fetch_row(sql_res))) {
  688. flag = 0;
  689. id = atoi(sql_row[0]);
  690. for(i = 0; i < count; i++) {
  691. if(mapitem[i].flag == 1)
  692. continue;
  693. if(mapitem[i].nameid == atoi(sql_row[1])
  694. && mapitem[i].card[0] == atoi(sql_row[7])
  695. && mapitem[i].card[2] == atoi(sql_row[9])
  696. && mapitem[i].card[3] == atoi(sql_row[10])
  697. ) { //They are the same item.
  698. for (j = 0; j<MAX_SLOTS && mapitem[i].card[j] == atoi(sql_row[7+j]); j++);
  699. if (j == MAX_SLOTS &&
  700. mapitem[i].amount == atoi(sql_row[2]) &&
  701. mapitem[i].equip == atoi(sql_row[3]) &&
  702. mapitem[i].identify == atoi(sql_row[4]) &&
  703. mapitem[i].refine == atoi(sql_row[5]) &&
  704. mapitem[i].attribute == atoi(sql_row[6]))
  705. { //Do nothing.
  706. } else
  707. //==============================================Memory data > SQL ===============================
  708. if(!itemdb_isequip(mapitem[i].nameid))
  709. { //Quick update of stackable items. Update Qty and Equip should be enough, but in case we are also updating identify
  710. sprintf(tmp_sql,"UPDATE `%s` SET `equip`='%d', `identify`='%d', `amount`='%d' WHERE `id`='%d' LIMIT 1",
  711. tablename, mapitem[i].equip, mapitem[i].identify,mapitem[i].amount, id);
  712. if(mysql_query(&mysql_handle, tmp_sql))
  713. {
  714. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  715. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  716. }
  717. } else
  718. { //Equipment or Misc item, just update all fields.
  719. str_p = tmp_sql;
  720. str_p += sprintf(str_p,"UPDATE `%s` SET `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d'",
  721. tablename, mapitem[i].equip, mapitem[i].identify, mapitem[i].refine, mapitem[i].attribute);
  722. for(j=0; j<MAX_SLOTS; j++)
  723. str_p += sprintf(str_p, ", `card%d`=%d", j, mapitem[i].card[j]);
  724. str_p += sprintf(str_p,", `amount`='%d' WHERE `id`='%d' LIMIT 1",
  725. mapitem[i].amount, id);
  726. if(mysql_query(&mysql_handle, tmp_sql))
  727. {
  728. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  729. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  730. }
  731. }
  732. flag = mapitem[i].flag = 1; //Item dealt with,
  733. break; //skip to next item in the db.
  734. }
  735. }
  736. if(!flag) { //Item not updated, remove it.
  737. sprintf(tmp_sql,"DELETE from `%s` where `id`='%d'", tablename, id);
  738. if(mysql_query(&mysql_handle, tmp_sql))
  739. {
  740. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  741. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  742. }
  743. }
  744. }
  745. mysql_free_result(sql_res);
  746. }
  747. for(i = 0; i < count; i++) {
  748. if(!mapitem[i].flag) {
  749. str_p = tmp_sql;
  750. str_p += sprintf(str_p,"INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`",
  751. tablename, selectoption);
  752. for(j=0; j<MAX_SLOTS; j++)
  753. str_p += sprintf(str_p,", `card%d`", j);
  754. str_p += sprintf(str_p,") VALUES ( '%d','%d', '%d', '%d', '%d', '%d', '%d'",
  755. char_id, mapitem[i].nameid, mapitem[i].amount, mapitem[i].equip, mapitem[i].identify, mapitem[i].refine,
  756. mapitem[i].attribute);
  757. for(j=0; j<MAX_SLOTS; j++)
  758. str_p +=sprintf(str_p,", '%d'",mapitem[i].card[j]);
  759. strcat(tmp_sql, ")");
  760. if(mysql_query(&mysql_handle, tmp_sql))
  761. {
  762. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  763. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  764. }
  765. }
  766. }
  767. return 0;
  768. }
  769. //=====================================================================================================
  770. int mmo_char_fromsql(int char_id, struct mmo_charstatus *p){
  771. int i,j, n;
  772. double exp;
  773. char t_msg[128];
  774. char *str_p = tmp_sql;
  775. struct mmo_charstatus *cp;
  776. memset(p, 0, sizeof(struct mmo_charstatus));
  777. t_msg[0]= '\0';
  778. p->char_id = char_id;
  779. if (save_log)
  780. ShowInfo("Char load request (%d)\n", char_id);
  781. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  782. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  783. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  784. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  785. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  786. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  787. //splite 2 parts. cause veeeery long SQL syntax
  788. sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  789. "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  790. if (mysql_query(&mysql_handle, tmp_sql)) {
  791. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  792. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  793. }
  794. sql_res = mysql_store_result(&mysql_handle);
  795. if (sql_res) {
  796. sql_row = mysql_fetch_row(sql_res);
  797. if (!sql_row)
  798. { //Just how does this happens? [Skotlex]
  799. ShowError("Requested non-existant character id: %d!\n", char_id);
  800. return 0;
  801. }
  802. p->char_id = char_id;
  803. p->account_id = atoi(sql_row[1]);
  804. p->char_num = atoi(sql_row[2]);
  805. strcpy(p->name, sql_row[3]);
  806. p->class_ = atoi(sql_row[4]);
  807. p->base_level = atoi(sql_row[5]);
  808. p->job_level = atoi(sql_row[6]);
  809. exp = atof(sql_row[7]);
  810. p->base_exp = exp<0?0:(exp>UINT_MAX?UINT_MAX:(unsigned int)exp);
  811. exp = atof(sql_row[8]);
  812. p->job_exp = exp<0?0:(exp>UINT_MAX?UINT_MAX:(unsigned int)exp);
  813. p->zeny = atoi(sql_row[9]);
  814. p->str = atoi(sql_row[10]);
  815. p->agi = atoi(sql_row[11]);
  816. p->vit = atoi(sql_row[12]);
  817. p->int_ = atoi(sql_row[13]);
  818. p->dex = atoi(sql_row[14]);
  819. p->luk = atoi(sql_row[15]);
  820. p->max_hp = atoi(sql_row[16]);
  821. p->hp = atoi(sql_row[17]);
  822. p->max_sp = atoi(sql_row[18]);
  823. p->sp = atoi(sql_row[19]);
  824. p->status_point = atoi(sql_row[20]) > USHRT_MAX ? USHRT_MAX : atoi(sql_row[20]);
  825. p->skill_point = atoi(sql_row[21]) > USHRT_MAX ? USHRT_MAX : atoi(sql_row[21]);
  826. //free mysql result.
  827. mysql_free_result(sql_res);
  828. strcat (t_msg, " status");
  829. } else
  830. ShowError("Load char failed (%d - table %s).\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  831. sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`hair`,`hair_color`,"
  832. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,"
  833. "`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`, `partner_id`, `father`, `mother`, `child`, `fame`"
  834. "FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  835. if (mysql_query(&mysql_handle, tmp_sql)) {
  836. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  837. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  838. }
  839. sql_res = mysql_store_result(&mysql_handle);
  840. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  841. if (sql_row) {
  842. p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]);
  843. p->party_id = atoi(sql_row[3]); p->guild_id = atoi(sql_row[4]); p->pet_id = atoi(sql_row[5]);
  844. p->hair = atoi(sql_row[6]); p->hair_color = atoi(sql_row[7]); p->clothes_color = atoi(sql_row[8]);
  845. p->weapon = atoi(sql_row[9]); p->shield = atoi(sql_row[10]);
  846. p->head_top = atoi(sql_row[11]); p->head_mid = atoi(sql_row[12]); p->head_bottom = atoi(sql_row[13]);
  847. p->last_point.map = mapindex_name2id(sql_row[14]); p->last_point.x = atoi(sql_row[15]); p->last_point.y = atoi(sql_row[16]);
  848. p->save_point.map = mapindex_name2id(sql_row[17]); p->save_point.x = atoi(sql_row[18]); p->save_point.y = atoi(sql_row[19]);
  849. p->partner_id = atoi(sql_row[20]); p->father = atoi(sql_row[21]); p->mother = atoi(sql_row[22]); p->child = atoi(sql_row[23]);
  850. p->fame = atoi(sql_row[24]);
  851. strcat (t_msg, " status2");
  852. } else
  853. ShowError("Char load failed (%d - table %s)\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  854. //free mysql result.
  855. if (sql_res)
  856. mysql_free_result(sql_res);
  857. //read memo data
  858. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  859. sprintf(tmp_sql, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`='%d' ORDER by `memo_id`",memo_db, char_id); // TBR
  860. if (mysql_query(&mysql_handle, tmp_sql)) {
  861. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  862. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  863. }
  864. sql_res = mysql_store_result(&mysql_handle);
  865. if (sql_res) {
  866. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  867. p->memo_point[i].map = mapindex_name2id(sql_row[0]);
  868. p->memo_point[i].x=atoi(sql_row[1]);
  869. p->memo_point[i].y=atoi(sql_row[2]);
  870. //i ++;
  871. }
  872. mysql_free_result(sql_res);
  873. strcat (t_msg, " memo");
  874. }
  875. //read inventory
  876. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  877. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  878. for (j=0; j<MAX_SLOTS; j++)
  879. str_p += sprintf(str_p, ", `card%d`", j);
  880. str_p += sprintf(str_p, " FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id);
  881. if (mysql_query(&mysql_handle, tmp_sql)) {
  882. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  883. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  884. }
  885. sql_res = mysql_store_result(&mysql_handle);
  886. if (sql_res) {
  887. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  888. p->inventory[i].id = atoi(sql_row[0]);
  889. p->inventory[i].nameid = atoi(sql_row[1]);
  890. p->inventory[i].amount = atoi(sql_row[2]);
  891. p->inventory[i].equip = atoi(sql_row[3]);
  892. p->inventory[i].identify = atoi(sql_row[4]);
  893. p->inventory[i].refine = atoi(sql_row[5]);
  894. p->inventory[i].attribute = atoi(sql_row[6]);
  895. for (j=0; j<MAX_SLOTS; j++)
  896. p->inventory[i].card[j] = atoi(sql_row[7+j]);
  897. }
  898. mysql_free_result(sql_res);
  899. strcat (t_msg, " inventory");
  900. }
  901. //read cart.
  902. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  903. str_p = tmp_sql;
  904. str_p += sprintf(str_p, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`");
  905. for (j=0; j<MAX_SLOTS; j++)
  906. str_p += sprintf(str_p, ", `card%d`", j);
  907. str_p += sprintf(str_p, " FROM `%s` WHERE `char_id`='%d'", cart_db, char_id);
  908. if (mysql_query(&mysql_handle, tmp_sql)) {
  909. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  910. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  911. }
  912. sql_res = mysql_store_result(&mysql_handle);
  913. if (sql_res) {
  914. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  915. p->cart[i].id = atoi(sql_row[0]);
  916. p->cart[i].nameid = atoi(sql_row[1]);
  917. p->cart[i].amount = atoi(sql_row[2]);
  918. p->cart[i].equip = atoi(sql_row[3]);
  919. p->cart[i].identify = atoi(sql_row[4]);
  920. p->cart[i].refine = atoi(sql_row[5]);
  921. p->cart[i].attribute = atoi(sql_row[6]);
  922. for(j=0; j<MAX_SLOTS; j++)
  923. p->cart[i].card[j] = atoi(sql_row[7+j]);
  924. }
  925. mysql_free_result(sql_res);
  926. strcat (t_msg, " cart");
  927. }
  928. //read skill
  929. //`skill` (`char_id`, `id`, `lv`)
  930. sprintf(tmp_sql, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`='%d'",skill_db, char_id); // TBR
  931. if (mysql_query(&mysql_handle, tmp_sql)) {
  932. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  933. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  934. }
  935. sql_res = mysql_store_result(&mysql_handle);
  936. if (sql_res) {
  937. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  938. n = atoi(sql_row[0]);
  939. p->skill[n].id = n; //memory!? shit!.
  940. p->skill[n].lv = atoi(sql_row[1]);
  941. }
  942. mysql_free_result(sql_res);
  943. strcat (t_msg, " skills");
  944. }
  945. /* Global-reg loading is now handled by the inter-server.
  946. //global_reg
  947. //`global_reg_value` (`char_id`, `str`, `value`)
  948. sprintf(tmp_sql, "SELECT `str`, `value` FROM `%s` WHERE `type`=3 AND `char_id`='%d'",reg_db, char_id); // TBR
  949. if (mysql_query(&mysql_handle, tmp_sql)) {
  950. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  951. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  952. }
  953. i = 0;
  954. sql_res = mysql_store_result(&mysql_handle);
  955. if (sql_res) {
  956. for(i=0;(sql_row = mysql_fetch_row(sql_res));i++){
  957. strcpy (p->global_reg[i].str, sql_row[0]);
  958. strcpy (p->global_reg[i].value, sql_row[1]);
  959. }
  960. mysql_free_result(sql_res);
  961. strcat (t_msg, " reg_values");
  962. }
  963. p->global_reg_num=i;
  964. */
  965. //Shamelessly stolen from its_sparky (ie: thanks) and then assimilated by [Skotlex]
  966. //Friend list
  967. sprintf(tmp_sql, "SELECT f.friend_account, f.friend_id, c.name FROM `%s` f LEFT JOIN `%s` c ON f.friend_account=c.account_id AND f.friend_id=c.char_id WHERE f.char_id='%d'", friend_db, char_db, char_id);
  968. if(mysql_query(&mysql_handle, tmp_sql)){
  969. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  970. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  971. }
  972. sql_res = mysql_store_result(&mysql_handle);
  973. if(sql_res)
  974. {
  975. for(i = 0; (sql_row = mysql_fetch_row(sql_res)) && i<MAX_FRIENDS; i++)
  976. {
  977. if(sql_row) { //need to check if we have sql_row before we check if we have sql_row[2] because we don't want a segfault
  978. if(sql_row[2]) {
  979. p->friends[i].account_id = atoi(sql_row[0]);
  980. p->friends[i].char_id = atoi(sql_row[1]);
  981. strncpy(p->friends[i].name, sql_row[2], NAME_LENGTH-1); //The -1 is to avoid losing the ending \0 [Skotlex]
  982. }
  983. }
  984. }
  985. mysql_free_result(sql_res);
  986. strcat (t_msg, " friends");
  987. }
  988. if (save_log)
  989. ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  990. cp = idb_ensure(char_db_, char_id, create_charstatus);
  991. memcpy(cp, p, sizeof(struct mmo_charstatus));
  992. return 1;
  993. }
  994. // For quick selection of data when displaying the char menu. [Skotlex]
  995. //
  996. int mmo_char_fromsql_short(int char_id, struct mmo_charstatus *p){
  997. char t_msg[128];
  998. double exp;
  999. memset(p, 0, sizeof(struct mmo_charstatus));
  1000. t_msg[0]= '\0';
  1001. p->char_id = char_id;
  1002. // ShowInfo("Quick Char load request (%d)\n", char_id);
  1003. //`char`( `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`, //9
  1004. //`str`,`agi`,`vit`,`int`,`dex`,`luk`, //15
  1005. //`max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point`, //21
  1006. //`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`, //27
  1007. //`hair`,`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`, //35
  1008. //`last_map`,`last_x`,`last_y`,`save_map`,`save_x`,`save_y`)
  1009. //splite 2 parts. cause veeeery long SQL syntax
  1010. sprintf(tmp_sql, "SELECT `char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  1011. "`str`,`agi`,`vit`,`int`,`dex`,`luk`, `max_hp`,`hp`,`max_sp`,`sp`,`status_point`,`skill_point` FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  1012. if (mysql_query(&mysql_handle, tmp_sql)) {
  1013. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1014. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1015. }
  1016. sql_res = mysql_store_result(&mysql_handle);
  1017. if (sql_res) {
  1018. sql_row = mysql_fetch_row(sql_res);
  1019. if (!sql_row)
  1020. { //Just how does this happens? [Skotlex]
  1021. ShowError("Requested non-existant character id: %d!\n", char_id);
  1022. mysql_free_result(sql_res);
  1023. return 0;
  1024. }
  1025. p->char_id = char_id;
  1026. p->account_id = atoi(sql_row[1]);
  1027. p->char_num = atoi(sql_row[2]);
  1028. strcpy(p->name, sql_row[3]);
  1029. p->class_ = atoi(sql_row[4]);
  1030. p->base_level = atoi(sql_row[5]);
  1031. p->job_level = atoi(sql_row[6]);
  1032. exp = atof(sql_row[7]);
  1033. p->base_exp = exp<0?0:(exp>UINT_MAX?UINT_MAX:(unsigned int)exp);
  1034. exp = atof(sql_row[8]);
  1035. p->job_exp = exp<0?0:(exp>UINT_MAX?UINT_MAX:(unsigned int)exp);
  1036. p->zeny = atoi(sql_row[9]);
  1037. p->str = atoi(sql_row[10]);
  1038. p->agi = atoi(sql_row[11]);
  1039. p->vit = atoi(sql_row[12]);
  1040. p->int_ = atoi(sql_row[13]);
  1041. p->dex = atoi(sql_row[14]);
  1042. p->luk = atoi(sql_row[15]);
  1043. p->max_hp = atoi(sql_row[16]);
  1044. p->hp = atoi(sql_row[17]);
  1045. p->max_sp = atoi(sql_row[18]);
  1046. p->sp = atoi(sql_row[19]);
  1047. p->status_point = atoi(sql_row[20]) > USHRT_MAX ? USHRT_MAX : atoi(sql_row[20]);
  1048. p->skill_point = atoi(sql_row[21]) > USHRT_MAX ? USHRT_MAX : atoi(sql_row[21]);
  1049. //free mysql result.
  1050. mysql_free_result(sql_res);
  1051. strcat (t_msg, " status");
  1052. } else
  1053. ShowError("Load char failed (%d - table %s).\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  1054. sprintf(tmp_sql, "SELECT `option`,`karma`,`manner`,`hair`,`hair_color`,"
  1055. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`"
  1056. "FROM `%s` WHERE `char_id` = '%d'",char_db, char_id); // TBR
  1057. if (mysql_query(&mysql_handle, tmp_sql)) {
  1058. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1059. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1060. }
  1061. sql_res = mysql_store_result(&mysql_handle);
  1062. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  1063. if (sql_row) {
  1064. p->option = atoi(sql_row[0]); p->karma = atoi(sql_row[1]); p->manner = atoi(sql_row[2]);
  1065. p->hair = atoi(sql_row[3]); p->hair_color = atoi(sql_row[4]); p->clothes_color = atoi(sql_row[5]);
  1066. p->weapon = atoi(sql_row[6]); p->shield = atoi(sql_row[7]);
  1067. p->head_top = atoi(sql_row[8]); p->head_mid = atoi(sql_row[9]); p->head_bottom = atoi(sql_row[10]);
  1068. strcat (t_msg, " status2");
  1069. } else
  1070. ShowError("Char load failed (%d - table %s)\n", char_id, char_db); //Error?! ERRRRRR WHAT THAT SAY!?
  1071. //free mysql result.
  1072. if (sql_res)
  1073. mysql_free_result(sql_res);
  1074. // if (save_log) //Too much spam :/
  1075. // ShowInfo("Quick Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  1076. return 1;
  1077. }
  1078. //==========================================================================================================
  1079. int mmo_char_sql_init(void) {
  1080. ShowInfo("Begin Initializing.......\n");
  1081. char_db_= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA, sizeof(int));
  1082. // memory initialize
  1083. // no need to set twice size in this routine. but some cause segmentation error. :P
  1084. // The hell? Why segmentation faults? Sounds like a bug that needs addressing... [Skotlex]
  1085. ShowDebug("initializing char memory...(%d byte)\n",sizeof(struct mmo_charstatus)*2);
  1086. CREATE(char_dat, struct mmo_charstatus, 2);
  1087. memset(char_dat, 0, sizeof(struct mmo_charstatus)*2);
  1088. if(char_per_account == 0){
  1089. ShowStatus("Chars per Account: 'Unlimited'.......\n");
  1090. }else{
  1091. ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
  1092. }
  1093. //the 'set offline' part is now in check_login_conn ...
  1094. //if the server connects to loginserver
  1095. //it will dc all off players
  1096. //and send the loginserver the new state....
  1097. ShowInfo("Finished initilizing.......\n");
  1098. return 0;
  1099. }
  1100. //==========================================================================================================
  1101. int make_new_char_sql(int fd, unsigned char *dat) {
  1102. struct char_session_data *sd;
  1103. char name[NAME_LENGTH];
  1104. char t_name[NAME_LENGTH*2];
  1105. unsigned int i; // Used in for loop and comparing with strlen, safe to be unsigned. [Lance]
  1106. int char_id, temp;
  1107. strncpy(name, dat, NAME_LENGTH);
  1108. name[NAME_LENGTH-1] = '\0'; //Always terminate string.
  1109. trim(name,TRIM_CHARS); //Trim character name. [Skotlex]
  1110. jstrescapecpy(t_name, name);
  1111. // disabled until fixed >.>
  1112. // Note: escape characters should be added to jstrescape()!
  1113. //mysql_real_escape_string(&mysql_handle, t_name, t_name_temp, sizeof(t_name_temp));
  1114. if (!session_isValid(fd) || !(sd = (struct char_session_data*)session[fd]->session_data))
  1115. return -2;
  1116. ShowInfo("New character request (%d)\n", sd->account_id);
  1117. //check name != main chat nick [LuzZza]
  1118. if(strcmpi(name, main_chat_nick) == 0) {
  1119. ShowInfo("Create char failed (%d): this nick (%s) reserved for mainchat messages.\n", sd->account_id, name);
  1120. return -2;
  1121. }
  1122. //check for charcount (maxchars) :)
  1123. if(char_per_account != 0){
  1124. sprintf(tmp_sql, "SELECT `account_id` FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id);
  1125. if(mysql_query(&mysql_handle, tmp_sql)){
  1126. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1127. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1128. }
  1129. sql_res = mysql_store_result(&mysql_handle);
  1130. if(sql_res){
  1131. //ok
  1132. temp = (int)mysql_num_rows(sql_res);
  1133. if(temp >= char_per_account){
  1134. //hehe .. limit exceeded :P
  1135. ShowInfo("Create char failed (%d): charlimit exceeded.\n", sd->account_id);
  1136. mysql_free_result(sql_res);
  1137. return -2;
  1138. }
  1139. mysql_free_result(sql_res);
  1140. }
  1141. }
  1142. // Check Authorised letters/symbols in the name of the character
  1143. if (char_name_option == 1) { // only letters/symbols in char_name_letters are authorised
  1144. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1145. if (strchr(char_name_letters, name[i]) == NULL)
  1146. return -2;
  1147. } else if (char_name_option == 2) { // letters/symbols in char_name_letters are forbidden
  1148. for (i = 0; i < NAME_LENGTH && name[i]; i++)
  1149. if (strchr(char_name_letters, name[i]) != NULL)
  1150. return -2;
  1151. } // else, all letters/symbols are authorised (except control char removed before)
  1152. //check stat error
  1153. if ((dat[24]+dat[25]+dat[26]+dat[27]+dat[28]+dat[29]!=6*5 ) || // stats
  1154. (dat[30] >= 9) || // slots (dat[30] can not be negativ)
  1155. (dat[33] <= 0) || (dat[33] >= 24) || // hair style
  1156. (dat[31] >= 9)) { // hair color (dat[31] can not be negativ)
  1157. if (log_char) {
  1158. // char.log to charlog
  1159. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1160. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1161. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1162. //query
  1163. if(mysql_query(&mysql_handle, tmp_sql)){
  1164. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1165. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1166. }
  1167. }
  1168. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1169. return -2;
  1170. } // for now we have checked: stat points used <31, char slot is less then 9, hair style/color values are acceptable
  1171. // check individual stat value
  1172. for(i = 24; i <= 29; i++) {
  1173. if (dat[i] < 1 || dat[i] > 9) {
  1174. if (log_char) {
  1175. // char.log to charlog
  1176. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1177. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1178. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1179. //query
  1180. if(mysql_query(&mysql_handle, tmp_sql)){
  1181. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1182. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1183. }
  1184. }
  1185. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1186. return -2;
  1187. }
  1188. } // now we know that every stat has proper value but we have to check if str/int agi/luk vit/dex pairs are correct
  1189. if( ((dat[24]+dat[27]) > 10) || ((dat[25]+dat[29]) > 10) || ((dat[26]+dat[28]) > 10) ) {
  1190. if (log_char) {
  1191. // char.log to charlog
  1192. sprintf(tmp_sql,"INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1193. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1194. charlog_db,"make new char error", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1195. //query
  1196. if(mysql_query(&mysql_handle, tmp_sql)){
  1197. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1198. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1199. }
  1200. }
  1201. ShowWarning("Create char failed (%d): stats error (bot cheat?!)\n", sd->account_id);
  1202. return -2;
  1203. } // now when we have passed all stat checks
  1204. if (log_char) {
  1205. // char.log to charlog
  1206. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1207. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1208. charlog_db,"make new char", sd->account_id, dat[30], t_name, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1209. //query
  1210. if (mysql_query(&mysql_handle, tmp_sql)) {
  1211. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1212. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1213. }
  1214. }
  1215. //printf("make new char %d-%d %s %d, %d, %d, %d, %d, %d - %d, %d" RETCODE,
  1216. // fd, dat[30], dat, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29], dat[33], dat[31]);
  1217. //Check Name (already in use?)
  1218. sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `name` = '%s'",char_db, t_name);
  1219. if (mysql_query(&mysql_handle, tmp_sql)) {
  1220. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1221. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1222. return -2;
  1223. }
  1224. sql_res = mysql_store_result(&mysql_handle);
  1225. if(sql_res){
  1226. temp = (int)mysql_num_rows(sql_res);
  1227. mysql_free_result(sql_res);
  1228. if (temp > 0) {
  1229. ShowInfo("Create char failed: charname already in use\n");
  1230. return -1;
  1231. }
  1232. }
  1233. // check char slot.
  1234. sprintf(tmp_sql, "SELECT `account_id`, `char_num` FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'",char_db, sd->account_id, dat[30]);
  1235. if (mysql_query(&mysql_handle, tmp_sql)) {
  1236. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1237. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1238. }
  1239. sql_res = mysql_store_result(&mysql_handle);
  1240. if(sql_res){
  1241. temp = (int)mysql_num_rows(sql_res);
  1242. mysql_free_result(sql_res);
  1243. if (temp > 0) {
  1244. ShowWarning("Create char failed (%d, slot: %d), slot already in use\n", sd->account_id, dat[30]);
  1245. return -2;
  1246. }
  1247. }
  1248. //New Querys [Sirius]
  1249. //Insert the char to the 'chardb' ^^
  1250. sprintf(tmp_sql, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1251. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1252. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1253. char_db, sd->account_id , dat[30] , t_name, start_zeny, dat[24], dat[25], dat[26], dat[27], dat[28], dat[29],
  1254. (40 * (100 + dat[26])/100) , (40 * (100 + dat[26])/100 ), (11 * (100 + dat[27])/100), (11 * (100 + dat[27])/100), dat[33], dat[31],
  1255. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y);
  1256. if(mysql_query(&mysql_handle, tmp_sql)){
  1257. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1258. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1259. return -2; //No, stop the procedure!
  1260. }
  1261. //Now we need the charid from sql!
  1262. if(mysql_field_count(&mysql_handle) == 0 &&
  1263. mysql_insert_id(&mysql_handle) > 0)
  1264. char_id = (int)mysql_insert_id(&mysql_handle);
  1265. else {
  1266. //delete the char ..(no trash in DB!) but how is this possible?
  1267. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d' AND `name` = '%s'", char_db, sd->account_id, dat[30], t_name);
  1268. if(mysql_query(&mysql_handle, tmp_sql)){
  1269. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1270. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1271. }
  1272. return -2; //XD end of the (World? :P) .. charcreate (denied)
  1273. }
  1274. //Give the char the default items
  1275. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  1276. if (start_weapon > 0) { //add Start Weapon (Knife?)
  1277. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon,1,0x02,1);
  1278. if (mysql_query(&mysql_handle, tmp_sql)){
  1279. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1280. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1281. }
  1282. }
  1283. if (start_armor > 0) { //Add default armor (cotton shirt?)
  1284. sprintf(tmp_sql,"INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `equip`, `identify`) VALUES ('%d', '%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor,1,0x10,1);
  1285. if (mysql_query(&mysql_handle, tmp_sql)){
  1286. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1287. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1288. }
  1289. }
  1290. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, dat[30], name);
  1291. return char_id;
  1292. }
  1293. /*----------------------------------------------------------------------------------------------------------*/
  1294. /* Delete char - davidsiaw */
  1295. /*----------------------------------------------------------------------------------------------------------*/
  1296. /* Returns 0 if successful
  1297. * Returns < 0 for error
  1298. */
  1299. int delete_char_sql(int char_id, int partner_id)
  1300. {
  1301. char char_name[NAME_LENGTH], t_name[NAME_LENGTH*2]; //Name needs be escaped.
  1302. int account_id=0, party_id=0, guild_id=0;
  1303. sprintf(tmp_sql, "SELECT `name`,`account_id`,`party_id`,`guild_id` FROM `%s` WHERE `char_id`='%d'",char_db, char_id);
  1304. if (mysql_query(&mysql_handle, tmp_sql)) {
  1305. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1306. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1307. }
  1308. sql_res = mysql_store_result(&mysql_handle);
  1309. if(sql_res)
  1310. sql_row = mysql_fetch_row(sql_res);
  1311. if (sql_res == NULL || sql_row == NULL)
  1312. {
  1313. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1314. if (sql_res)
  1315. mysql_free_result(sql_res);
  1316. return -1;
  1317. }
  1318. strncpy(char_name, sql_row[0], NAME_LENGTH);
  1319. char_name[NAME_LENGTH-1] = '\0';
  1320. jstrescapecpy(t_name, char_name); //Escape string for sql use... [Skotlex]
  1321. account_id = atoi(sql_row[1]);
  1322. party_id = atoi(sql_row[2]);
  1323. guild_id = atoi(sql_row[3]);
  1324. mysql_free_result(sql_res); //Let's free this as soon as possible to avoid problems later on.
  1325. /* Divorce [Wizputer] */
  1326. if (partner_id) {
  1327. sprintf(tmp_sql,"UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d'",char_db,partner_id);
  1328. if(mysql_query(&mysql_handle, tmp_sql)) {
  1329. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1330. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1331. }
  1332. sprintf(tmp_sql,"DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND `char_id`='%d'",inventory_db,WEDDING_RING_M,WEDDING_RING_F,partner_id);
  1333. if(mysql_query(&mysql_handle, tmp_sql)) {
  1334. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1335. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1336. }
  1337. }
  1338. //Make the character leave the party [Skotlex]
  1339. if (party_id)
  1340. inter_party_leave(party_id, account_id, char_id);
  1341. /* delete char's pet */
  1342. //Delete the hatched pet if you have one...
  1343. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'",pet_db, char_id);
  1344. if(mysql_query(&mysql_handle, tmp_sql)) {
  1345. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1346. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1347. }
  1348. // Komurka's suggested way to clear pets, modified by [Skotlex] (because I always personalize what I do :X)
  1349. //Removing pets that are in the char's inventory....
  1350. { //NOTE: The syntax for multi-table deletes is a bit changed between 4.0 and 4.1 regarding aliases, so we have to consider the version... [Skotlex]
  1351. //Since we only care about the major and minor version, a double conversion is good enough. (4.1.20 -> 4.10000)
  1352. double mysql_version = atof(mysql_get_server_info(&mysql_handle));
  1353. sprintf(tmp_sql,
  1354. "delete FROM `%s` USING `%s` as c LEFT JOIN `%s` as i ON c.char_id = i.char_id, `%s` as p WHERE c.char_id = '%d' AND i.card0 = -256 AND p.pet_id = (i.card1|(i.card2<<2))",
  1355. (mysql_version<4.1?pet_db:"p"), char_db, inventory_db, pet_db, char_id);
  1356. if(mysql_query(&mysql_handle, tmp_sql)) {
  1357. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1358. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1359. }
  1360. //Removing pets that are in the char's cart....
  1361. sprintf(tmp_sql,
  1362. "delete FROM `%s` USING `%s` as c LEFT JOIN `%s` as i ON c.char_id = i.char_id, `%s` as p WHERE c.char_id = '%d' AND i.card0 = -256 AND p.pet_id = (i.card1|(i.card2<<2))",
  1363. (mysql_version<4.1?pet_db:"p"), char_db, cart_db, pet_db, char_id);
  1364. if(mysql_query(&mysql_handle, tmp_sql)) {
  1365. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1366. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1367. }
  1368. }
  1369. /* delete char's friends list */
  1370. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d'",friend_db, char_id);
  1371. if(mysql_query(&mysql_handle, tmp_sql)) {
  1372. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1373. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1374. }
  1375. /* delete char from other's friend list */
  1376. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1377. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `friend_id` = '%d'",friend_db, char_id);
  1378. if(mysql_query(&mysql_handle, tmp_sql)) {
  1379. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1380. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1381. }
  1382. /* delete inventory */
  1383. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",inventory_db, char_id);
  1384. if(mysql_query(&mysql_handle, tmp_sql)) {
  1385. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1386. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1387. }
  1388. /* delete cart inventory */
  1389. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",cart_db, char_id);
  1390. if(mysql_query(&mysql_handle, tmp_sql)) {
  1391. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1392. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1393. }
  1394. /* delete memo areas */
  1395. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",memo_db, char_id);
  1396. if(mysql_query(&mysql_handle, tmp_sql)) {
  1397. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1398. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1399. }
  1400. /* delete skills */
  1401. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",skill_db, char_id);
  1402. if(mysql_query(&mysql_handle, tmp_sql)) {
  1403. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1404. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1405. }
  1406. /* delete character */
  1407. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",char_db, char_id);
  1408. if(mysql_query(&mysql_handle, tmp_sql)) {
  1409. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1410. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1411. }
  1412. /* No need as we used inter_guild_leave [Skotlex]
  1413. // Also delete info from guildtables.
  1414. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `char_id`='%d'",guild_member_db, char_id);
  1415. if (mysql_query(&mysql_handle, tmp_sql)) {
  1416. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1417. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1418. }
  1419. */
  1420. sprintf(tmp_sql, "SELECT `guild_id` FROM `%s` WHERE `master` = '%s'", guild_db, t_name);
  1421. if (mysql_query(&mysql_handle, tmp_sql))
  1422. {
  1423. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1424. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1425. } else {
  1426. sql_res = mysql_store_result(&mysql_handle);
  1427. if (sql_res == NULL) {
  1428. if (mysql_errno(&mysql_handle) != 0)
  1429. {
  1430. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1431. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1432. }
  1433. return -1;
  1434. } else {
  1435. int rows = (int)mysql_num_rows(sql_res);
  1436. mysql_free_result(sql_res);
  1437. if (rows > 0) {
  1438. mapif_parse_BreakGuild(0,guild_id);
  1439. }
  1440. else if (guild_id) //Leave your guild.
  1441. inter_guild_leave(guild_id, account_id, char_id);
  1442. }
  1443. }
  1444. return 0;
  1445. }
  1446. //==========================================================================================================
  1447. void mmo_char_sync(void)
  1448. {
  1449. ShowWarning("mmo_char_sync() - nothing to do\n");
  1450. }
  1451. // to do
  1452. ///////////////////////////
  1453. int mmo_char_sync_timer(int tid, unsigned int tick, int id, int data) {
  1454. ShowWarning("mmo_char_sync_timer() tic - no works to do\n");
  1455. return 0;
  1456. }
  1457. int count_users(void) {
  1458. int i, users;
  1459. if (login_fd > 0 && session[login_fd]){
  1460. users = 0;
  1461. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  1462. if (server_fd[i] > 0) {
  1463. users += server[i].users;
  1464. }
  1465. }
  1466. return users;
  1467. }
  1468. return 0;
  1469. }
  1470. int mmo_char_send006b(int fd, struct char_session_data *sd) {
  1471. int i, j, found_num = 0;
  1472. struct mmo_charstatus *p = NULL;
  1473. // hehe. commented other. anyway there's no need to use older version.
  1474. // if use older packet version just uncomment that!
  1475. //#ifdef NEW_006b
  1476. const int offset = 24;
  1477. //#else
  1478. // int offset = 4;
  1479. //#endif
  1480. // ShowDebug("mmo_char_send006b start.. (account:%d)\n",sd->account_id);
  1481. // printf("offset -> %d...\n",offset);
  1482. set_char_online(-1, 99,sd->account_id);
  1483. //search char.
  1484. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id` = '%d'",char_db, sd->account_id);
  1485. if (mysql_query(&mysql_handle, tmp_sql)) {
  1486. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1487. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1488. }
  1489. sql_res = mysql_store_result(&mysql_handle);
  1490. if (sql_res) {
  1491. found_num = (int)mysql_num_rows(sql_res);
  1492. // ShowInfo("number of chars: %d\n", found_num);
  1493. i = 0;
  1494. while((sql_row = mysql_fetch_row(sql_res))) {
  1495. sd->found_char[i] = atoi(sql_row[0]);
  1496. i++;
  1497. }
  1498. mysql_free_result(sql_res);
  1499. }
  1500. // printf("char fetching end (total: %d)....\n", found_num);
  1501. for(i = found_num; i < 9; i++)
  1502. sd->found_char[i] = -1;
  1503. memset(WFIFOP(fd, 0), 0, offset + found_num * 106);
  1504. WFIFOW(fd, 0) = 0x6b;
  1505. WFIFOW(fd, 2) = offset + found_num * 106;
  1506. if (save_log)
  1507. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1508. for(i = 0; i < found_num; i++) {
  1509. mmo_char_fromsql_short(sd->found_char[i], char_dat);
  1510. p = &char_dat[0];
  1511. j = offset + (i * 106); // increase speed of code
  1512. WFIFOL(fd,j) = p->char_id;
  1513. WFIFOL(fd,j+4) = p->base_exp>LONG_MAX?LONG_MAX:p->base_exp;
  1514. WFIFOL(fd,j+8) = p->zeny;
  1515. WFIFOL(fd,j+12) = p->job_exp>LONG_MAX?LONG_MAX:p->job_exp;
  1516. WFIFOL(fd,j+16) = p->job_level;
  1517. WFIFOL(fd,j+20) = 0;
  1518. WFIFOL(fd,j+24) = 0;
  1519. WFIFOL(fd,j+28) = p->option;
  1520. WFIFOL(fd,j+32) = p->karma;
  1521. WFIFOL(fd,j+36) = p->manner;
  1522. WFIFOW(fd,j+40) = (p->status_point > SHRT_MAX) ? SHRT_MAX : p->status_point;
  1523. WFIFOW(fd,j+42) = (p->hp > SHRT_MAX) ? SHRT_MAX : p->hp;
  1524. WFIFOW(fd,j+44) = (p->max_hp > SHRT_MAX) ? SHRT_MAX : p->max_hp;
  1525. WFIFOW(fd,j+46) = (p->sp > SHRT_MAX) ? SHRT_MAX : p->sp;
  1526. WFIFOW(fd,j+48) = (p->max_sp > SHRT_MAX) ? SHRT_MAX : p->max_sp;
  1527. WFIFOW(fd,j+50) = DEFAULT_WALK_SPEED; // p->speed;
  1528. WFIFOW(fd,j+52) = p->class_;
  1529. WFIFOW(fd,j+54) = p->hair;
  1530. // pecopeco knights/crusaders crash fix
  1531. if (p->class_ == 13 || p->class_ == 21 ||
  1532. p->class_ == 4014 || p->class_ == 4022 ||
  1533. p->class_ == 4036 || p->class_ == 4044)
  1534. WFIFOW(fd,j+56) = 0;
  1535. else WFIFOW(fd,j+56) = p->weapon;
  1536. WFIFOW(fd,j+58) = p->base_level;
  1537. WFIFOW(fd,j+60) = (p->skill_point > SHRT_MAX) ? SHRT_MAX : p->skill_point;
  1538. WFIFOW(fd,j+62) = p->head_bottom;
  1539. WFIFOW(fd,j+64) = p->shield;
  1540. WFIFOW(fd,j+66) = p->head_top;
  1541. WFIFOW(fd,j+68) = p->head_mid;
  1542. WFIFOW(fd,j+70) = p->hair_color;
  1543. WFIFOW(fd,j+72) = p->clothes_color;
  1544. memcpy(WFIFOP(fd,j+74), p->name, NAME_LENGTH);
  1545. WFIFOB(fd,j+98) = (p->str > UCHAR_MAX) ? UCHAR_MAX : p->str;
  1546. WFIFOB(fd,j+99) = (p->agi > UCHAR_MAX) ? UCHAR_MAX : p->agi;
  1547. WFIFOB(fd,j+100) = (p->vit > UCHAR_MAX) ? UCHAR_MAX : p->vit;
  1548. WFIFOB(fd,j+101) = (p->int_ > UCHAR_MAX) ? UCHAR_MAX : p->int_;
  1549. WFIFOB(fd,j+102) = (p->dex > UCHAR_MAX) ? UCHAR_MAX : p->dex;
  1550. WFIFOB(fd,j+103) = (p->luk > UCHAR_MAX) ? UCHAR_MAX : p->luk;
  1551. WFIFOB(fd,j+104) = p->char_num;
  1552. }
  1553. WFIFOSET(fd,WFIFOW(fd,2));
  1554. // printf("mmo_char_send006b end..\n");
  1555. return 0;
  1556. }
  1557. int parse_tologin(int fd) {
  1558. int i;
  1559. struct char_session_data *sd;
  1560. // only login-server can have an access to here.
  1561. // so, if it isn't the login-server, we disconnect the session.
  1562. //session eof check!
  1563. if(fd != login_fd)
  1564. session[fd]->eof = 1;
  1565. if(session[fd]->eof) {
  1566. if (fd == login_fd) {
  1567. ShowWarning("Connection to login-server lost (connection #%d).\n", fd);
  1568. login_fd = -1;
  1569. }
  1570. do_close(fd);
  1571. return 0;
  1572. }
  1573. sd = (struct char_session_data*)session[fd]->session_data;
  1574. // hehe. no need to set user limit on SQL version. :P
  1575. // but char limitation is good way to maintain server. :D
  1576. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  1577. // printf("parse_tologin : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd, 0));
  1578. switch(RFIFOW(fd, 0)){
  1579. case 0x2711:
  1580. if (RFIFOREST(fd) < 3)
  1581. return 0;
  1582. if (RFIFOB(fd, 2)) {
  1583. //printf("connect login server error : %d\n", RFIFOB(fd, 2));
  1584. ShowError("Can not connect to login-server.\n");
  1585. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1586. ShowError("Also, please make sure your login db has the correct coounication username/passwords and the gender of the account is S.\n");
  1587. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1588. return 0;
  1589. //exit(1); //fixed for server shutdown.
  1590. }else {
  1591. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1592. if (kick_on_disconnect)
  1593. set_all_offline();
  1594. // if no map-server already connected, display a message...
  1595. for(i = 0; i < MAX_MAP_SERVERS; i++)
  1596. if (server_fd[i] > 0 && server[i].map[0]) // if map-server online and at least 1 map
  1597. break;
  1598. if (i == MAX_MAP_SERVERS)
  1599. ShowStatus("Awaiting maps from map-server.\n");
  1600. }
  1601. RFIFOSKIP(fd, 3);
  1602. break;
  1603. case 0x2713:
  1604. if(RFIFOREST(fd)<51)
  1605. return 0;
  1606. for(i = 0; i < fd_max; i++) {
  1607. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == RFIFOL(fd,2)) {
  1608. if (RFIFOB(fd,6) != 0) {
  1609. WFIFOW(i,0) = 0x6c;
  1610. WFIFOB(i,2) = 0x42;
  1611. WFIFOSET(i,3);
  1612. } else if (max_connect_user == 0 || count_users() < max_connect_user) {
  1613. // if (max_connect_user == 0)
  1614. // printf("max_connect_user (unlimited) -> accepted.\n");
  1615. // else
  1616. // printf("count_users(): %d < max_connect_user (%d) -> accepted.\n", count_users(), max_connect_user);
  1617. sd->connect_until_time = (time_t)RFIFOL(fd,47);
  1618. memcpy(sd->email, RFIFOP(fd, 7), 40);
  1619. // send characters to player
  1620. mmo_char_send006b(i, sd);
  1621. } else if(isGM(sd->account_id) >= gm_allow_level) {
  1622. sd->connect_until_time = (time_t)RFIFOL(fd,47);
  1623. memcpy(sd->email, RFIFOP(fd, 7), 40);
  1624. // send characters to player
  1625. mmo_char_send006b(i, sd);
  1626. } else {
  1627. // refuse connection: too much online players
  1628. // printf("count_users(): %d < max_connect_use (%d) -> fail...\n", count_users(), max_connect_user);
  1629. WFIFOW(i,0) = 0x6c;
  1630. WFIFOW(i,2) = 0;
  1631. WFIFOSET(i,3);
  1632. }
  1633. }
  1634. }
  1635. RFIFOSKIP(fd,51);
  1636. break;
  1637. case 0x2717:
  1638. if (RFIFOREST(fd) < 50)
  1639. return 0;
  1640. for(i = 0; i < fd_max; i++) {
  1641. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1642. if (sd->account_id == RFIFOL(fd,2)) {
  1643. memcpy(sd->email, RFIFOP(fd, 6), 40);
  1644. sd->connect_until_time = (time_t)RFIFOL(fd,46);
  1645. break;
  1646. }
  1647. }
  1648. }
  1649. RFIFOSKIP(fd,50);
  1650. break;
  1651. // login-server alive packet
  1652. case 0x2718:
  1653. if (RFIFOREST(fd) < 2)
  1654. return 0;
  1655. RFIFOSKIP(fd,2);
  1656. break;
  1657. // Receiving authentification from Freya-type login server (to avoid char->login->char)
  1658. case 0x2719:
  1659. if (RFIFOREST(fd) < 18)
  1660. return 0;
  1661. // to conserv a maximum of authentification, search if account is already authentified and replace it
  1662. // that will reduce multiple connection too
  1663. for(i = 0; i < AUTH_FIFO_SIZE; i++)
  1664. if (auth_fifo[i].account_id == RFIFOL(fd,2))
  1665. break;
  1666. // if not found, use next value
  1667. if (i == AUTH_FIFO_SIZE) {
  1668. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  1669. auth_fifo_pos = 0;
  1670. i = auth_fifo_pos;
  1671. auth_fifo_pos++;
  1672. }
  1673. //printf("auth_fifo set (auth #%d) - account: %d, secure: %08x-%08x\n", i, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10));
  1674. auth_fifo[i].account_id = RFIFOL(fd,2);
  1675. auth_fifo[i].char_id = 0;
  1676. auth_fifo[i].login_id1 = RFIFOL(fd,6);
  1677. auth_fifo[i].login_id2 = RFIFOL(fd,10);
  1678. auth_fifo[i].delflag = 2; // 0: auth_fifo canceled/void, 2: auth_fifo received from login/map server in memory, 1: connection authentified
  1679. auth_fifo[i].char_pos = 0;
  1680. auth_fifo[i].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server)
  1681. auth_fifo[i].ip = RFIFOL(fd,14);
  1682. //auth_fifo[i].map_auth = 0;
  1683. RFIFOSKIP(fd,18);
  1684. break;
  1685. /* case 0x2721: // gm reply. I don't want to support this function.
  1686. printf("0x2721:GM reply\n");
  1687. {
  1688. int oldacc, newacc;
  1689. unsigned char buf[64];
  1690. if (RFIFOREST(fd) < 10)
  1691. return 0;
  1692. oldacc = RFIFOL(fd, 2);
  1693. newacc = RFIFOL(fd, 6);
  1694. RFIFOSKIP(fd, 10);
  1695. if (newacc > 0) {
  1696. for(i=0;i<char_num;i++){
  1697. if(char_dat[i].account_id==oldacc)
  1698. char_dat[i].account_id=newacc;
  1699. }
  1700. }
  1701. WBUFW(buf,0)=0x2b0b;
  1702. WBUFL(buf,2)=oldacc;
  1703. WBUFL(buf,6)=newacc;
  1704. mapif_sendall(buf,10);
  1705. // printf("char -> map\n");
  1706. }
  1707. break;
  1708. */
  1709. case 0x2723: // changesex reply (modified by [Yor])
  1710. if (RFIFOREST(fd) < 7)
  1711. return 0;
  1712. {
  1713. int acc, sex;
  1714. unsigned char buf[16];
  1715. MYSQL_RES* sql_res2;
  1716. acc = RFIFOL(fd,2);
  1717. sex = RFIFOB(fd,6);
  1718. RFIFOSKIP(fd, 7);
  1719. if (acc > 0) {
  1720. sprintf(tmp_sql, "SELECT `char_id`,`class`,`skill_point`,`guild_id` FROM `%s` WHERE `account_id` = '%d'",char_db, acc);
  1721. if (mysql_query(&mysql_handle, tmp_sql)) {
  1722. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1723. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1724. }
  1725. sql_res2 = mysql_store_result(&mysql_handle);
  1726. while(sql_res2 && (sql_row = mysql_fetch_row(sql_res2))) {
  1727. int char_id, guild_id, jobclass, skill_point, class_;
  1728. char_id = atoi(sql_row[0]);
  1729. jobclass = atoi(sql_row[1]);
  1730. skill_point = atoi(sql_row[2]);
  1731. guild_id = atoi(sql_row[3]);
  1732. class_ = jobclass;
  1733. if (jobclass == 19 || jobclass == 20 ||
  1734. jobclass == 4020 || jobclass == 4021 ||
  1735. jobclass == 4042 || jobclass == 4043) {
  1736. // job modification
  1737. if (jobclass == 19 || jobclass == 20) {
  1738. class_ = (sex) ? 19 : 20;
  1739. } else if (jobclass == 4020 || jobclass == 4021) {
  1740. class_ = (sex) ? 4020 : 4021;
  1741. } else if (jobclass == 4042 || jobclass == 4043) {
  1742. class_ = (sex) ? 4042 : 4043;
  1743. }
  1744. // remove specifical skills of classes 19,20 4020,4021 and 4042,4043
  1745. sprintf(tmp_sql, "SELECT `lv` FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1746. if (mysql_query(&mysql_handle, tmp_sql)) {
  1747. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1748. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1749. }
  1750. sql_res = mysql_store_result(&mysql_handle);
  1751. if (sql_res) {
  1752. while(( sql_row = mysql_fetch_row(sql_res))) {
  1753. skill_point += atoi(sql_row[0]);
  1754. }
  1755. mysql_free_result(sql_res);
  1756. }
  1757. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'",skill_db, char_id);
  1758. if (mysql_query(&mysql_handle, tmp_sql)) {
  1759. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1760. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1761. }
  1762. }
  1763. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  1764. sprintf(tmp_sql, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'",inventory_db, char_id);
  1765. if (mysql_query(&mysql_handle, tmp_sql)) {
  1766. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1767. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1768. }
  1769. sprintf(tmp_sql, "UPDATE `%s` SET `class`='%d' , `skill_point`='%d' , `weapon`='0' , `shield='0' , `head_top`='0' , `head_mid`='0' , `head_bottom`='0' WHERE `char_id` = '%d'",char_db, class_, skill_point, char_id);
  1770. if (mysql_query(&mysql_handle, tmp_sql)) {
  1771. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  1772. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  1773. }
  1774. if (guild_id) //If there is a guild, update the guild_member data [Skotlex]
  1775. inter_guild_sex_changed(guild_id, acc, char_id, sex);
  1776. }
  1777. if (sql_res2)
  1778. mysql_free_result(sql_res2);
  1779. }
  1780. // disconnect player if online on char-server
  1781. for(i = 0; i < fd_max; i++) {
  1782. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1783. if (sd->account_id == acc) {
  1784. session[i]->eof = 1;
  1785. break;
  1786. }
  1787. }
  1788. }
  1789. WBUFW(buf,0) = 0x2b0d;
  1790. WBUFL(buf,2) = acc;
  1791. WBUFB(buf,6) = sex;
  1792. mapif_sendall(buf, 7);
  1793. }
  1794. break;
  1795. // account_reg2変更通知
  1796. case 0x2729:
  1797. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1798. return 0;
  1799. { //Receive account_reg2 registry, forward to map servers.
  1800. unsigned char buf[ACCOUNT_REG2_NUM*(256+32+2)+16];
  1801. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  1802. // WBUFW(buf,0) = 0x2b11;
  1803. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  1804. mapif_sendall(buf, WBUFW(buf,2));
  1805. RFIFOSKIP(fd, RFIFOW(fd,2));
  1806. }
  1807. break;
  1808. // State change of account/ban notification (from login-server) by [Yor]
  1809. case 0x2731:
  1810. if (RFIFOREST(fd) < 11)
  1811. return 0;
  1812. // send to all map-servers to disconnect the player
  1813. {
  1814. unsigned char buf[16];
  1815. WBUFW(buf,0) = 0x2b14;
  1816. WBUFL(buf,2) = RFIFOL(fd,2);
  1817. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  1818. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  1819. mapif_sendall(buf, 11);
  1820. }
  1821. // disconnect player if online on char-server
  1822. for(i = 0; i < fd_max; i++) {
  1823. if (session[i] && (sd = (struct char_session_data*)session[i]->session_data)) {
  1824. if (sd->account_id == RFIFOL(fd,2)) {
  1825. session[i]->eof = 1;
  1826. break;
  1827. }
  1828. }
  1829. }
  1830. RFIFOSKIP(fd,11);
  1831. break;
  1832. case 0x2732:
  1833. if(!char_gm_read)
  1834. {
  1835. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1836. return 0;
  1837. {
  1838. unsigned char buf[32000];
  1839. if (gm_account != NULL)
  1840. aFree(gm_account);
  1841. gm_account = (struct gm_account*)aCalloc(sizeof(struct gm_account) * ((RFIFOW(fd,2) - 4) / 5), 1);
  1842. GM_num = 0;
  1843. for (i = 4; i < RFIFOW(fd,2); i = i + 5) {
  1844. gm_account[GM_num].account_id = RFIFOL(fd,i);
  1845. gm_account[GM_num].level = (int)RFIFOB(fd,i+4);
  1846. //printf("GM account: %d -> level %d\n", gm_account[GM_num].account_id, gm_account[GM_num].level);
  1847. GM_num++;
  1848. }
  1849. ShowStatus("From login-server: receiving information of %d GM accounts.\n", GM_num);
  1850. // send new gm acccounts level to map-servers
  1851. memcpy(buf, RFIFOP(fd,0), RFIFOW(fd,2));
  1852. WBUFW(buf,0) = 0x2b15;
  1853. mapif_sendall(buf, RFIFOW(fd,2));
  1854. }
  1855. RFIFOSKIP(fd,RFIFOW(fd,2));
  1856. }
  1857. break;
  1858. // Receive GM accounts [Freya login server packet by Yor]
  1859. case 0x2733:
  1860. // add test here to remember that the login-server is Freya-type
  1861. // sprintf (login_server_type, "Freya");
  1862. if (RFIFOREST(fd) < 7)
  1863. return 0;
  1864. {
  1865. int new_level = 0;
  1866. for(i = 0; i < GM_num; i++)
  1867. if (gm_account[i].account_id == RFIFOL(fd,2)) {
  1868. if (gm_account[i].level != (int)RFIFOB(fd,6)) {
  1869. gm_account[i].level = (int)RFIFOB(fd,6);
  1870. new_level = 1;
  1871. }
  1872. break;
  1873. }
  1874. // if not found, add it
  1875. if (i == GM_num) {
  1876. // limited to 4000, because we send information to char-servers (more than 4000 GM accounts???)
  1877. // int (id) + int (level) = 8 bytes * 4000 = 32k (limit of packets in windows)
  1878. if (((int)RFIFOB(fd,6)) > 0 && GM_num < 4000) {
  1879. if (GM_num == 0) {
  1880. gm_account = (struct gm_account*)aMalloc(sizeof(struct gm_account));
  1881. } else {
  1882. gm_account = (struct gm_account*)aRealloc(gm_account, sizeof(struct gm_account) * (GM_num + 1));
  1883. }
  1884. gm_account[GM_num].account_id = RFIFOL(fd,2);
  1885. gm_account[GM_num].level = (int)RFIFOB(fd,6);
  1886. new_level = 1;
  1887. GM_num++;
  1888. if (GM_num >= 4000)
  1889. ShowWarning("4000 GM accounts found. Next GM accounts are not readed.\n");
  1890. }
  1891. }
  1892. if (new_level == 1) {
  1893. ShowStatus("From login-server: receiving GM account information (%d: level %d).\n", RFIFOL(fd,2), (int)RFIFOB(fd,6));
  1894. mapif_send_gmaccounts();
  1895. //create_online_files(); // not change online file for only 1 player (in next timer, that will be done
  1896. // send gm acccounts level to map-servers
  1897. }
  1898. }
  1899. RFIFOSKIP(fd,7);
  1900. break;
  1901. //Login server request to kick a character out. [Skotlex]
  1902. case 0x2734:
  1903. if (RFIFOREST(fd) < 6)
  1904. return 0;
  1905. {
  1906. struct online_char_data* character;
  1907. int aid = RFIFOL(fd,2);
  1908. if ((character = idb_get(online_char_db, aid)) != NULL)
  1909. { //Kick out this player.
  1910. if (character->server > -1)
  1911. { //Kick it from the map server it is on.
  1912. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  1913. if (!character->waiting_disconnect)
  1914. add_timer(gettick()+15000, chardb_waiting_disconnect, character->account_id, 0);
  1915. character->waiting_disconnect = 1;
  1916. } else { //Manual kick from char server.
  1917. struct char_session_data *tsd;
  1918. int i;
  1919. for(i = 0; i < fd_max; i++) {
  1920. if (session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid)
  1921. {
  1922. WFIFOW(i,0) = 0x81;
  1923. WFIFOB(i,2) = 2;
  1924. WFIFOSET(i,3);
  1925. break;
  1926. }
  1927. }
  1928. if (i == fd_max) //Shouldn't happen, but just in case.
  1929. set_char_offline(99, aid);
  1930. }
  1931. }
  1932. RFIFOSKIP(fd,6);
  1933. }
  1934. break;
  1935. default:
  1936. ShowError("Unknown packet 0x%04x from login server, disconnecting.\n", RFIFOW(fd, 0));
  1937. session[fd]->eof = 1;
  1938. return 0;
  1939. }
  1940. }
  1941. RFIFOFLUSH(fd);
  1942. return 0;
  1943. }
  1944. int request_accreg2(int account_id, int char_id) {
  1945. if (login_fd > 0) {
  1946. WFIFOW(login_fd, 0) = 0x272e;
  1947. WFIFOL(login_fd, 2) = account_id;
  1948. WFIFOL(login_fd, 6) = char_id;
  1949. WFIFOSET(login_fd, 10);
  1950. return 1;
  1951. }
  1952. return 0;
  1953. }
  1954. //Send packet forward to login-server for account saving
  1955. int save_accreg2(unsigned char* buf, int len) {
  1956. if (login_fd > 0) {
  1957. WFIFOHEAD(login_fd, len+4);
  1958. memcpy(WFIFOP(login_fd,4), buf, len);
  1959. WFIFOW(login_fd,0) = 0x2728;
  1960. WFIFOW(login_fd,2) = len+4;
  1961. WFIFOSET(login_fd,len+4);
  1962. return 1;
  1963. }
  1964. return 0;
  1965. }
  1966. int search_mapserver(unsigned short map, long ip, short port);
  1967. int parse_frommap(int fd) {
  1968. int i = 0, j = 0;
  1969. int id;
  1970. // int auth_fifo_flag=0;
  1971. // Sometimes fd=0, and it will cause server crash. Don't know why. :(
  1972. if (fd <= 0) {
  1973. ShowError("parse_frommap error fd=%d\n", fd);
  1974. return 0;
  1975. }
  1976. for(id = 0; id < MAX_MAP_SERVERS; id++)
  1977. if (server_fd[id] == fd)
  1978. break;
  1979. if(id == MAX_MAP_SERVERS)
  1980. session[fd]->eof = 1;
  1981. if(session[fd]->eof) {
  1982. if (id < MAX_MAP_SERVERS) {
  1983. unsigned char buf[16384];
  1984. ShowStatus("Map-server %d (session #%d) has disconnected.\n", id, fd);
  1985. //Notify other map servers that this one is gone. [Skotlex]
  1986. WBUFW(buf,0) = 0x2b20;
  1987. WBUFL(buf,4) = server[id].ip;
  1988. WBUFW(buf,8) = server[id].port;
  1989. j = 0;
  1990. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  1991. if (server[id].map[i])
  1992. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  1993. if (j > 0) {
  1994. WBUFW(buf,2) = j * 4 + 10;
  1995. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  1996. }
  1997. memset(&server[id], 0, sizeof(struct mmo_map_server));
  1998. sprintf(tmp_sql, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server_fd[id]);
  1999. if (mysql_query(&mysql_handle, tmp_sql)) {
  2000. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2001. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2002. }
  2003. server_fd[id] = -1;
  2004. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2005. }
  2006. do_close(fd);
  2007. return 0;
  2008. }
  2009. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  2010. // printf("parse_frommap : %d %d %x\n", fd, RFIFOREST(fd), RFIFOW(fd,0));
  2011. switch(RFIFOW(fd, 0)) {
  2012. // map-server alive packet
  2013. case 0x2718:
  2014. RFIFOSKIP(fd,2);
  2015. break;
  2016. case 0x2af7:
  2017. RFIFOSKIP(fd,2);
  2018. if(char_gm_read) //Re-read gm accounts.
  2019. read_gm_account();
  2020. //Send to login request to reload gm accounts.
  2021. else if (login_fd > 0) { // don't send request if no login-server
  2022. WFIFOW(login_fd,0) = 0x2709;
  2023. WFIFOSET(login_fd, 2);
  2024. }
  2025. break;
  2026. // mapserver -> map names recv.
  2027. case 0x2afa:
  2028. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2029. return 0;
  2030. memset(server[id].map, 0, sizeof(server[id].map));
  2031. j = 0;
  2032. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2033. server[id].map[j] = RFIFOW(fd,i);
  2034. // printf("set map %d.%d : %s\n", id, j, server[id].map[j]);
  2035. j++;
  2036. }
  2037. i = server[id].ip;
  2038. {
  2039. unsigned char *p = (unsigned char *)&server[id].ip;
  2040. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2041. id, j, p[0], p[1], p[2], p[3], server[id].port);
  2042. ShowStatus("Map-server %d loading complete.\n", id);
  2043. if (kick_on_disconnect)
  2044. set_all_offline();
  2045. if (max_account_id != DEFAULT_MAX_ACCOUNT_ID || max_char_id != DEFAULT_MAX_CHAR_ID)
  2046. mapif_send_maxid(max_account_id, max_char_id); //Send the current max ids to the server to keep in sync [Skotlex]
  2047. }
  2048. WFIFOW(fd,0) = 0x2afb;
  2049. WFIFOB(fd,2) = 0;
  2050. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH); // name for wisp to player
  2051. WFIFOSET(fd,3+NAME_LENGTH);
  2052. //WFIFOSET(fd,27);
  2053. {
  2054. unsigned char buf[16384];
  2055. int x;
  2056. if (j == 0) {
  2057. ShowWarning("Map-Server %d have NO maps.\n", id);
  2058. // Transmitting maps information to the other map-servers
  2059. } else {
  2060. WBUFW(buf,0) = 0x2b04;
  2061. WBUFW(buf,2) = j * 4 + 10;
  2062. WBUFL(buf,4) = server[id].ip;
  2063. WBUFW(buf,8) = server[id].port;
  2064. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2065. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2066. }
  2067. // Transmitting the maps of the other map-servers to the new map-server
  2068. for(x = 0; x < MAX_MAP_SERVERS; x++) {
  2069. if (server_fd[x] > 0 && x != id) {
  2070. WFIFOW(fd,0) = 0x2b04;
  2071. WFIFOL(fd,4) = server[x].ip;
  2072. WFIFOW(fd,8) = server[x].port;
  2073. j = 0;
  2074. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2075. if (server[x].map[i])
  2076. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2077. if (j > 0) {
  2078. WFIFOW(fd,2) = j * 4 + 10;
  2079. WFIFOSET(fd,WFIFOW(fd,2));
  2080. }
  2081. }
  2082. }
  2083. }
  2084. RFIFOSKIP(fd,RFIFOW(fd,2));
  2085. break;
  2086. //Packet command is now used for sc_data request. [Skotlex]
  2087. case 0x2afc:
  2088. if (RFIFOREST(fd) < 10)
  2089. return 0;
  2090. {
  2091. int aid, cid;
  2092. aid = RFIFOL(fd,2);
  2093. cid = RFIFOL(fd,6);
  2094. RFIFOSKIP(fd, 10);
  2095. #ifdef ENABLE_SC_SAVING
  2096. sprintf(tmp_sql, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2097. scdata_db, aid, cid);
  2098. if (mysql_query(&mysql_handle, tmp_sql)) {
  2099. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2100. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2101. break;
  2102. }
  2103. sql_res = mysql_store_result(&mysql_handle);
  2104. if (sql_res) {
  2105. struct status_change_data data;
  2106. int count = 0;
  2107. WFIFOW(fd, 0) = 0x2b1d;
  2108. WFIFOL(fd, 4) = aid;
  2109. WFIFOL(fd, 8) = cid;
  2110. while((sql_row = mysql_fetch_row(sql_res)))
  2111. {
  2112. data.type = atoi(sql_row[0]);
  2113. data.tick = atoi(sql_row[1]);
  2114. data.val1 = atoi(sql_row[2]);
  2115. data.val2 = atoi(sql_row[3]);
  2116. data.val3 = atoi(sql_row[4]);
  2117. data.val4 = atoi(sql_row[5]);
  2118. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &data, sizeof(struct status_change_data));
  2119. count++;
  2120. }
  2121. mysql_free_result(sql_res);
  2122. if (count > 0)
  2123. {
  2124. WFIFOW(fd, 2) = 14 + count*sizeof(struct status_change_data);
  2125. WFIFOW(fd, 12) = count;
  2126. WFIFOSET(fd, WFIFOW(fd,2));
  2127. //Clear the data once loaded.
  2128. sprintf(tmp_sql, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid);
  2129. if (mysql_query(&mysql_handle, tmp_sql)) {
  2130. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2131. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2132. }
  2133. }
  2134. }
  2135. #endif
  2136. break;
  2137. }
  2138. //set MAP user count
  2139. case 0x2afe:
  2140. if (RFIFOREST(fd) < 4)
  2141. return 0;
  2142. if (RFIFOW(fd,2) != server[id].users) {
  2143. server[id].users = RFIFOW(fd,2);
  2144. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2145. }
  2146. RFIFOSKIP(fd, 4);
  2147. break;
  2148. // set MAP user
  2149. case 0x2aff:
  2150. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2151. return 0;
  2152. {
  2153. int i, aid, cid;
  2154. struct online_char_data* character;
  2155. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2156. server[id].users = RFIFOW(fd,4);
  2157. for(i = 0; i < server[id].users; i++) {
  2158. aid = RFIFOL(fd,6+i*8);
  2159. cid = RFIFOL(fd,6+i*8+4);
  2160. character = idb_ensure(online_char_db, aid, create_online_char_data);
  2161. if (character->server > -1 && character->server != id)
  2162. {
  2163. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2164. character->account_id, character->char_id, character->server, id, aid, cid);
  2165. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  2166. }
  2167. character->server = id;
  2168. character->char_id = cid;
  2169. }
  2170. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2171. RFIFOSKIP(fd,RFIFOW(fd,2));
  2172. break;
  2173. }
  2174. // char saving
  2175. case 0x2b01:
  2176. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2177. return 0;
  2178. {
  2179. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8);
  2180. i = 0;
  2181. //check account
  2182. sprintf(tmp_sql, "SELECT count(*) FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",char_db, aid, cid); // TBR
  2183. if (mysql_query(&mysql_handle, tmp_sql)) {
  2184. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2185. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2186. }
  2187. sql_res = mysql_store_result(&mysql_handle);
  2188. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2189. if (sql_row) i = atoi(sql_row[0]);
  2190. if (sql_res) mysql_free_result(sql_res);
  2191. if (i == 1) {
  2192. memcpy(&char_dat[0], RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2193. mmo_char_tosql(cid, char_dat);
  2194. //save to DB
  2195. } else
  2196. ShowError("parse_from_map (save-char): Received data for non-existant character (%d:%d)!\n", aid, cid);
  2197. if (RFIFOB(fd,12)) { //Flag? Set character offline after saving [Skotlex]
  2198. set_char_offline(cid, aid);
  2199. }
  2200. RFIFOSKIP(fd,RFIFOW(fd,2));
  2201. break;
  2202. }
  2203. // req char selection
  2204. case 0x2b02:
  2205. if (RFIFOREST(fd) < 18)
  2206. return 0;
  2207. if (auth_fifo_pos >= AUTH_FIFO_SIZE)
  2208. auth_fifo_pos = 0;
  2209. auth_fifo[auth_fifo_pos].account_id = RFIFOL(fd, 2);
  2210. auth_fifo[auth_fifo_pos].char_id = 0;
  2211. auth_fifo[auth_fifo_pos].login_id1 = RFIFOL(fd, 6);
  2212. auth_fifo[auth_fifo_pos].login_id2 = RFIFOL(fd,10);
  2213. auth_fifo[auth_fifo_pos].delflag = 2;
  2214. auth_fifo[auth_fifo_pos].char_pos = 0;
  2215. auth_fifo[auth_fifo_pos].connect_until_time = 0; // unlimited/unknown time by default (not display in map-server)
  2216. auth_fifo[auth_fifo_pos].ip = RFIFOL(fd,14);
  2217. auth_fifo_pos++;
  2218. WFIFOW(fd, 0) = 0x2b03;
  2219. WFIFOL(fd, 2) = RFIFOL(fd, 2);
  2220. WFIFOB(fd, 6) = 0;
  2221. WFIFOSET(fd, 7);
  2222. RFIFOSKIP(fd, 18);
  2223. break;
  2224. // request "change map server"
  2225. case 0x2b05:
  2226. if (RFIFOREST(fd) < 35)
  2227. return 0;
  2228. {
  2229. unsigned short name;
  2230. int map_id, map_fd = -1;
  2231. struct online_char_data* data;
  2232. struct mmo_charstatus* char_data;
  2233. name = RFIFOW(fd,18);
  2234. map_id = search_mapserver(name, RFIFOL(fd,24), RFIFOW(fd,28)); //Locate mapserver by ip and port.
  2235. if (map_id >= 0)
  2236. map_fd = server_fd[map_id];
  2237. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2238. char_data = uidb_get(char_db_,RFIFOL(fd,14));
  2239. if (char_data == NULL)
  2240. { //Really shouldn't happen.
  2241. mmo_char_fromsql(RFIFOL(fd,14), char_dat);
  2242. char_data = char_dat;
  2243. }
  2244. //Tell the new map server about this player using Kevin's new auth packet. [Skotlex]
  2245. if (map_fd>=0 && session[map_fd] && char_data)
  2246. { //Send the map server the auth of this player.
  2247. //Update the "last map" as this is where the player must be spawned on the new map server.
  2248. char_data->last_point.map = RFIFOW(fd,18);
  2249. char_data->last_point.x = RFIFOW(fd,20);
  2250. char_data->last_point.y = RFIFOW(fd,22);
  2251. WFIFOW(map_fd,0) = 0x2afd;
  2252. WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus);
  2253. WFIFOL(map_fd,4) = RFIFOL(fd, 2); //Account ID
  2254. WFIFOL(map_fd,8) = RFIFOL(fd, 6); //Login1
  2255. WFIFOL(map_fd,16) = RFIFOL(fd,10); //Login2
  2256. WFIFOL(map_fd,12) = (unsigned long)0; //TODO: connect_until_time, how do I figure it out right now?
  2257. memcpy(WFIFOP(map_fd,20), char_data, sizeof(struct mmo_charstatus));
  2258. WFIFOSET(map_fd, WFIFOW(map_fd,2));
  2259. data = idb_ensure(online_char_db, RFIFOL(fd, 2), create_online_char_data);
  2260. data->char_id = char_data->char_id;
  2261. data->server = map_id; //Update server where char is.
  2262. //Reply with an ack.
  2263. WFIFOW(fd, 0) = 0x2b06;
  2264. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2265. WFIFOSET(fd, 30);
  2266. } else { //Reply with nak
  2267. WFIFOW(fd, 0) = 0x2b06;
  2268. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2269. WFIFOL(fd, 6) = 0; //Set login1 to 0.
  2270. WFIFOSET(fd, 30);
  2271. }
  2272. RFIFOSKIP(fd, 35);
  2273. }
  2274. break;
  2275. // char name check
  2276. case 0x2b08:
  2277. if (RFIFOREST(fd) < 6)
  2278. return 0;
  2279. sprintf(tmp_sql, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, (int)RFIFOL(fd,2));
  2280. if (mysql_query(&mysql_handle, tmp_sql)) {
  2281. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2282. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2283. }
  2284. sql_res = mysql_store_result(&mysql_handle);
  2285. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2286. WFIFOW(fd,0) = 0x2b09;
  2287. WFIFOL(fd,2) = RFIFOL(fd,2);
  2288. if (sql_row)
  2289. memcpy(WFIFOP(fd,6), sql_row[0], NAME_LENGTH);
  2290. else
  2291. memcpy(WFIFOP(fd,6), unknown_char_name, NAME_LENGTH);
  2292. if (sql_res) mysql_free_result(sql_res);
  2293. WFIFOSET(fd,30);
  2294. RFIFOSKIP(fd,6);
  2295. break;
  2296. // I want become GM - fuck!
  2297. case 0x2b0a:
  2298. if(RFIFOREST(fd)<4)
  2299. return 0;
  2300. if(RFIFOREST(fd)<RFIFOW(fd,2))
  2301. return 0;
  2302. /*
  2303. memcpy(WFIFOP(login_fd,2),RFIFOP(fd,2),RFIFOW(fd,2)-2);
  2304. WFIFOW(login_fd,0)=0x2720;
  2305. WFIFOSET(login_fd,RFIFOW(fd,2));
  2306. // printf("char : change gm -> login %d %s %d\n", RFIFOL(fd, 4), RFIFOP(fd, 8), RFIFOW(fd, 2));
  2307. */
  2308. ShowWarning("packet 0x2ba (become GM) is not supported by the Char-Server.\n");
  2309. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2310. break;
  2311. //Packet 0x2b10 deprecated in favor of packet 0x3004 for registry saving. [Skotlex]
  2312. //case 0x2b10:
  2313. // Map server send information to change an email of an account -> login-server
  2314. case 0x2b0c:
  2315. if (RFIFOREST(fd) < 86)
  2316. return 0;
  2317. if (login_fd > 0) { // don't send request if no login-server
  2318. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0), 86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2319. WFIFOW(login_fd,0) = 0x2722;
  2320. WFIFOSET(login_fd, 86);
  2321. }
  2322. RFIFOSKIP(fd, 86);
  2323. break;
  2324. // Receiving from map-server a status change resquest. Transmission to login-server (by Yor)
  2325. case 0x2b0e:
  2326. if (RFIFOREST(fd) < 44)
  2327. return 0;
  2328. {
  2329. char character_name[NAME_LENGTH], t_name[NAME_LENGTH*2];
  2330. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if nobody)
  2331. memcpy(character_name, RFIFOP(fd,6), NAME_LENGTH);
  2332. character_name[NAME_LENGTH-1] = '\0';
  2333. jstrescapecpy(t_name, character_name); //Escape string for sql use... [Skotlex]
  2334. // prepare answer
  2335. WFIFOW(fd,0) = 0x2b0f; // answer
  2336. WFIFOL(fd,2) = acc; // who want do operation
  2337. WFIFOW(fd,30) = RFIFOW(fd, 30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  2338. sprintf(tmp_sql, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'",char_db, t_name);
  2339. if (mysql_query(&mysql_handle, tmp_sql)) {
  2340. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2341. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2342. }
  2343. sql_res = mysql_store_result(&mysql_handle);
  2344. if (sql_res) {
  2345. if (mysql_num_rows(sql_res)) {
  2346. sql_row = mysql_fetch_row(sql_res);
  2347. memcpy(WFIFOP(fd,6), sql_row[1], NAME_LENGTH); // put correct name if found
  2348. WFIFOW(fd,32) = 0; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2349. switch(RFIFOW(fd, 30)) {
  2350. case 1: // block
  2351. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2352. if (login_fd > 0) { // don't send request if no login-server
  2353. WFIFOW(login_fd,0) = 0x2724;
  2354. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  2355. WFIFOL(login_fd,6) = 5; // status of the account
  2356. WFIFOSET(login_fd, 10);
  2357. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 5);
  2358. } else
  2359. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2360. } else
  2361. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2362. break;
  2363. case 2: // ban
  2364. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2365. if (login_fd > 0) { // don't send request if no login-server
  2366. WFIFOW(login_fd, 0) = 0x2725;
  2367. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2368. WFIFOW(login_fd, 6) = RFIFOW(fd,32); // year
  2369. WFIFOW(login_fd, 8) = RFIFOW(fd,34); // month
  2370. WFIFOW(login_fd,10) = RFIFOW(fd,36); // day
  2371. WFIFOW(login_fd,12) = RFIFOW(fd,38); // hour
  2372. WFIFOW(login_fd,14) = RFIFOW(fd,40); // minute
  2373. WFIFOW(login_fd,16) = RFIFOW(fd,42); // second
  2374. WFIFOSET(login_fd,18);
  2375. // printf("char : status -> login: account %d, ban: %dy %dm %dd %dh %dmn %ds\n",
  2376. // char_dat[i].account_id, (short)RFIFOW(fd,32), (short)RFIFOW(fd,34), (short)RFIFOW(fd,36), (short)RFIFOW(fd,38), (short)RFIFOW(fd,40), (short)RFIFOW(fd,42));
  2377. } else
  2378. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2379. } else
  2380. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2381. break;
  2382. case 3: // unblock
  2383. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2384. if (login_fd > 0) { // don't send request if no login-server
  2385. WFIFOW(login_fd,0) = 0x2724;
  2386. WFIFOL(login_fd,2) = atoi(sql_row[0]); // account value
  2387. WFIFOL(login_fd,6) = 0; // status of the account
  2388. WFIFOSET(login_fd, 10);
  2389. // printf("char : status -> login: account %d, status: %d \n", char_dat[i].account_id, 0);
  2390. } else
  2391. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2392. } else
  2393. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2394. break;
  2395. case 4: // unban
  2396. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2397. if (login_fd > 0) { // don't send request if no login-server
  2398. WFIFOW(login_fd, 0) = 0x272a;
  2399. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2400. WFIFOSET(login_fd, 6);
  2401. // printf("char : status -> login: account %d, unban request\n", char_dat[i].account_id);
  2402. } else
  2403. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2404. } else
  2405. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2406. break;
  2407. case 5: // changesex
  2408. if (acc == -1 || isGM(acc) >= isGM(atoi(sql_row[0]))) {
  2409. if (login_fd > 0) { // don't send request if no login-server
  2410. WFIFOW(login_fd, 0) = 0x2727;
  2411. WFIFOL(login_fd, 2) = atoi(sql_row[0]); // account value
  2412. WFIFOSET(login_fd, 6);
  2413. // printf("char : status -> login: account %d, change sex request\n", char_dat[i].account_id);
  2414. } else
  2415. WFIFOW(fd,32) = 3; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2416. } else
  2417. WFIFOW(fd,32) = 2; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2418. break;
  2419. }
  2420. } else {
  2421. // character name not found
  2422. memcpy(WFIFOP(fd,6), character_name, NAME_LENGTH);
  2423. WFIFOW(fd,32) = 1; // answer: 0-login-server resquest done, 1-player not found, 2-gm level too low, 3-login-server offline
  2424. }
  2425. // send answer if a player ask, not if the server ask
  2426. if (acc != -1) {
  2427. WFIFOSET(fd, 34);
  2428. }
  2429. mysql_free_result(sql_res);
  2430. }
  2431. }
  2432. RFIFOSKIP(fd, 44);
  2433. break;
  2434. // Recieve rates [Wizputer]
  2435. case 0x2b16:
  2436. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,8))
  2437. return 0;
  2438. {
  2439. char motd[256], t_name[512]; //Required for jstrescapecpy [Skotlex]
  2440. strncpy(motd, RFIFOP(fd,10), 255); //First copy it to make sure the motd fits.
  2441. motd[255]='\0';
  2442. jstrescapecpy(t_name,motd);
  2443. sprintf(tmp_sql, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d',`motd`='%s'",
  2444. fd, server_name, RFIFOW(fd,2), RFIFOW(fd,4), RFIFOW(fd,6), t_name);
  2445. if (mysql_query(&mysql_handle, tmp_sql)) {
  2446. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2447. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2448. }
  2449. RFIFOSKIP(fd,RFIFOW(fd,8));
  2450. break;
  2451. }
  2452. // Character disconnected set online 0 [Wizputer]
  2453. case 0x2b17:
  2454. if (RFIFOREST(fd) < 6 )
  2455. return 0;
  2456. //printf("Setting %d char offline\n",RFIFOL(fd,2));
  2457. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  2458. RFIFOSKIP(fd,10);
  2459. break;
  2460. // Reset all chars to offline [Wizputer]
  2461. case 0x2b18:
  2462. ShowNotice("Map server [%d] requested to set all characters offline.\n", id);
  2463. set_all_offline();
  2464. RFIFOSKIP(fd,2);
  2465. break;
  2466. // Character set online [Wizputer]
  2467. case 0x2b19:
  2468. if (RFIFOREST(fd) < 6 )
  2469. return 0;
  2470. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  2471. RFIFOSKIP(fd,10);
  2472. break;
  2473. // Request sending of fame list
  2474. case 0x2b1a:
  2475. if (RFIFOREST(fd) < 2)
  2476. return 0;
  2477. {
  2478. int len = 8, num = 0;
  2479. unsigned char buf[32000];
  2480. struct fame_list fame_item;
  2481. WBUFW(buf,0) = 0x2b1b;
  2482. sprintf(tmp_sql, "SELECT `char_id`,`fame`, `name` FROM `%s` WHERE `fame`>0 AND (`class`='10' OR `class`='4011'OR `class`='4033') ORDER BY `fame` DESC LIMIT 0,%d", char_db, fame_list_size_smith);
  2483. if (mysql_query(&mysql_handle, tmp_sql)) {
  2484. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2485. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2486. }
  2487. sql_res = mysql_store_result(&mysql_handle);
  2488. if (sql_res) {
  2489. while((sql_row = mysql_fetch_row(sql_res))) {
  2490. fame_item.id = atoi(sql_row[0]);
  2491. fame_item.fame = atoi(sql_row[1]);
  2492. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2493. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2494. len += sizeof(struct fame_list);
  2495. if (++num == fame_list_size_smith)
  2496. break;
  2497. }
  2498. mysql_free_result(sql_res);
  2499. }
  2500. WBUFW(buf, 6) = len; //Blacksmith block size
  2501. num = 0;
  2502. sprintf(tmp_sql, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='18' OR `class`='4019' OR `class`='4041') ORDER BY `fame` DESC LIMIT 0,%d", char_db, fame_list_size_chemist);
  2503. if (mysql_query(&mysql_handle, tmp_sql)) {
  2504. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2505. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2506. }
  2507. sql_res = mysql_store_result(&mysql_handle);
  2508. if (sql_res) {
  2509. while((sql_row = mysql_fetch_row(sql_res))) {
  2510. fame_item.id = atoi(sql_row[0]);
  2511. fame_item.fame = atoi(sql_row[1]);
  2512. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2513. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2514. len += sizeof(struct fame_list);
  2515. if (++num == fame_list_size_chemist)
  2516. break;
  2517. }
  2518. mysql_free_result(sql_res);
  2519. }
  2520. WBUFW(buf, 4) = len; //Alchemist block size
  2521. num = 0;
  2522. sprintf(tmp_sql, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND `class`='4046' ORDER BY `fame` DESC LIMIT 0,%d", char_db, fame_list_size_taekwon);
  2523. if (mysql_query(&mysql_handle, tmp_sql)) {
  2524. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2525. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2526. }
  2527. sql_res = mysql_store_result(&mysql_handle);
  2528. if (sql_res) {
  2529. while((sql_row = mysql_fetch_row(sql_res))) {
  2530. fame_item.id = atoi(sql_row[0]);
  2531. fame_item.fame = atoi(sql_row[1]);
  2532. strncpy(fame_item.name, sql_row[2], NAME_LENGTH);
  2533. memcpy(WBUFP(buf,len), &fame_item, sizeof(struct fame_list));
  2534. len += sizeof(struct fame_list);
  2535. if (++num == fame_list_size_taekwon)
  2536. break;
  2537. }
  2538. mysql_free_result(sql_res);
  2539. }
  2540. WBUFW(buf, 2) = len; //Total packet length
  2541. mapif_sendall(buf, len);
  2542. RFIFOSKIP(fd,2);
  2543. break;
  2544. }
  2545. //Request saving sc_data of a player. [Skotlex]
  2546. case 0x2b1c:
  2547. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2548. return 0;
  2549. {
  2550. #ifdef ENABLE_SC_SAVING
  2551. int count, aid, cid, i;
  2552. struct status_change_data data;
  2553. char *p = tmp_sql;
  2554. aid = RFIFOL(fd, 4);
  2555. cid = RFIFOL(fd, 8);
  2556. count = RFIFOW(fd, 12);
  2557. p+= sprintf(p, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  2558. for (i = 0; i < count; i++)
  2559. {
  2560. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  2561. p += sprintf (p, " ('%d','%d','%hu','%d','%d','%d','%d','%d'),", aid, cid,
  2562. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  2563. }
  2564. if (count > 0)
  2565. {
  2566. *--p = '\0'; //Remove final comma.
  2567. if (mysql_query(&mysql_handle, tmp_sql)) {
  2568. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2569. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2570. }
  2571. }
  2572. #endif
  2573. RFIFOSKIP(fd, RFIFOW(fd, 2));
  2574. break;
  2575. }
  2576. default:
  2577. // inter server - packet
  2578. {
  2579. int r = inter_parse_frommap(fd);
  2580. if (r == 1) break; // processed
  2581. if (r == 2) return 0; // need more packet
  2582. }
  2583. // no inter server packet. no char server packet -> disconnect
  2584. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  2585. session[fd]->eof = 1;
  2586. return 0;
  2587. }
  2588. }
  2589. return 0;
  2590. }
  2591. int search_mapserver(unsigned short map, long ip, short port) {
  2592. int i, j;
  2593. if (!map)
  2594. return -1;
  2595. for(i = 0; i < MAX_MAP_SERVERS; i++)
  2596. if (server_fd[i] > 0)
  2597. for (j = 0; server[i].map[j]; j++)
  2598. if (server[i].map[j] == map) {
  2599. if (ip > 0 && server[i].ip != ip)
  2600. continue;
  2601. if (port > 0 && server[i].port != port)
  2602. continue;
  2603. return i;
  2604. }
  2605. return -1;
  2606. }
  2607. int char_mapif_init(int fd) {
  2608. return inter_mapif_init(fd);
  2609. }
  2610. //--------------------------------------------
  2611. // Test to know if an IP come from LAN or WAN.
  2612. // Rewrote: Adnvanced subnet check [LuzZza]
  2613. //--------------------------------------------
  2614. int lan_subnetcheck(long *p) {
  2615. int i;
  2616. unsigned char *sbn, *msk, *src = (unsigned char *)p;
  2617. for(i=0; i<subnet_count; i++) {
  2618. if((subnet[i].subnet & subnet[i].mask) == (*p & subnet[i].mask)) {
  2619. sbn = (unsigned char *)&subnet[i].subnet;
  2620. msk = (unsigned char *)&subnet[i].mask;
  2621. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n",
  2622. src[0], src[1], src[2], src[3], sbn[0], sbn[1], sbn[2], sbn[3], msk[0], msk[1], msk[2], msk[3]);
  2623. return subnet[i].map_ip;
  2624. }
  2625. }
  2626. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", src[0], src[1], src[2], src[3]);
  2627. return 0;
  2628. }
  2629. int parse_char(int fd) {
  2630. int i, ch = 0;
  2631. char email[40];
  2632. unsigned char buf[64];
  2633. unsigned short cmd;
  2634. int map_fd;
  2635. struct char_session_data *sd;
  2636. unsigned char *p = (unsigned char *) &session[fd]->client_addr.sin_addr;
  2637. long subnet_map_ip;
  2638. sd = (struct char_session_data*)session[fd]->session_data;
  2639. if(login_fd < 0)
  2640. session[fd]->eof = 1;
  2641. if(session[fd]->eof) {
  2642. if (fd == login_fd)
  2643. login_fd = -1;
  2644. if (sd != NULL)
  2645. {
  2646. struct online_char_data* data = idb_get(online_char_db, sd->account_id);
  2647. if (!data || data->server== -1) //If it is not in any server, send it offline. [Skotlex]
  2648. set_char_offline(99,sd->account_id);
  2649. }
  2650. do_close(fd);
  2651. return 0;
  2652. }
  2653. while(RFIFOREST(fd) >= 2 && !session[fd]->eof) {
  2654. cmd = RFIFOW(fd,0);
  2655. // crc32のスキップ用
  2656. if( sd==NULL && // 未ログインor管理パケット
  2657. RFIFOREST(fd)>=4 && // 最低バイト数制限 & 0x7530,0x7532管理パケ除去
  2658. RFIFOREST(fd)<=21 && // 最大バイト数制限 & サーバーログイン除去
  2659. cmd!=0x20b && // md5通知パケット除去
  2660. (RFIFOREST(fd)<6 || RFIFOW(fd,4)==0x65) ){ // 次に何かパケットが来てるなら、接続でないとだめ
  2661. RFIFOSKIP(fd,4);
  2662. cmd = RFIFOW(fd,0);
  2663. ShowDebug("parse_char : %d crc32 skipped\n",fd);
  2664. if(RFIFOREST(fd)==0)
  2665. return 0;
  2666. }
  2667. //For use in packets that depend on an sd being present [Skotlex]
  2668. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL) { RFIFOSKIP(fd,rest); return 0; } }
  2669. switch(cmd){
  2670. case 0x20b: //20040622 encryption ragexe correspondence
  2671. if (RFIFOREST(fd) < 19)
  2672. return 0;
  2673. RFIFOSKIP(fd,19);
  2674. break;
  2675. case 0x65: // request to connect
  2676. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", RFIFOL(fd, 2), RFIFOL(fd, 6), RFIFOL(fd, 10));
  2677. if (RFIFOREST(fd) < 17)
  2678. return 0;
  2679. {
  2680. if (sd == NULL) {
  2681. CREATE(session[fd]->session_data, struct char_session_data, 1);
  2682. sd = (struct char_session_data*)session[fd]->session_data;
  2683. sd->connect_until_time = 0; // unknow or illimited (not displaying on map-server)
  2684. }
  2685. sd->account_id = RFIFOL(fd, 2);
  2686. sd->login_id1 = RFIFOL(fd, 6);
  2687. sd->login_id2 = RFIFOL(fd, 10);
  2688. sd->sex = RFIFOB(fd, 16);
  2689. WFIFOL(fd, 0) = RFIFOL(fd, 2);
  2690. WFIFOSET(fd, 4);
  2691. for(i = 0; i < AUTH_FIFO_SIZE; i++) {
  2692. if (auth_fifo[i].account_id == sd->account_id &&
  2693. auth_fifo[i].login_id1 == sd->login_id1 &&
  2694. #if CMP_AUTHFIFO_LOGIN2 != 0
  2695. auth_fifo[i].login_id2 == sd->login_id2 && // relate to the versions higher than 18
  2696. #endif
  2697. (!check_ip_flag || auth_fifo[i].ip == session[fd]->client_addr.sin_addr.s_addr) &&
  2698. auth_fifo[i].delflag == 2) {
  2699. auth_fifo[i].delflag = 1;
  2700. if (online_check)
  2701. { // check if character is not online already. [Skotlex]
  2702. struct online_char_data* character;
  2703. character = idb_get(online_char_db, sd->account_id);
  2704. if (character)
  2705. {
  2706. if (character->server > -1)
  2707. { //Character already online. KICK KICK KICK
  2708. mapif_disconnectplayer(server_fd[character->server], character->account_id, character->char_id, 2);
  2709. if (!character->waiting_disconnect)
  2710. add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  2711. character->waiting_disconnect = 1;
  2712. /* Not a good idea because this would trigger when you do a char-change from the map server! [Skotlex]
  2713. } else { //Kick from char server.
  2714. struct char_session_data *tsd;
  2715. int i;
  2716. for(i = 0; i < fd_max; i++) {
  2717. if (session[i] && i != fd && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == sd->account_id)
  2718. {
  2719. WFIFOW(i,0) = 0x81;
  2720. WFIFOB(i,2) = 2;
  2721. WFIFOSET(i,3);
  2722. break;
  2723. }
  2724. if (i == fd_max) //Shouldn't happen, but just in case.
  2725. set_char_offline(99, sd->account_id);
  2726. }
  2727. */
  2728. WFIFOW(fd,0) = 0x81;
  2729. WFIFOB(fd,2) = 8;
  2730. WFIFOSET(fd,3);
  2731. break;
  2732. }
  2733. }
  2734. }
  2735. if (max_connect_user == 0 || count_users() < max_connect_user) {
  2736. if (login_fd > 0) { // don't send request if no login-server
  2737. // request to login-server to obtain e-mail/time limit
  2738. WFIFOW(login_fd,0) = 0x2716;
  2739. WFIFOL(login_fd,2) = sd->account_id;
  2740. WFIFOSET(login_fd,6);
  2741. }
  2742. // send characters to player
  2743. mmo_char_send006b(fd, sd);
  2744. } else {
  2745. // refuse connection (over populated)
  2746. WFIFOW(fd,0) = 0x6c;
  2747. WFIFOW(fd,2) = 0;
  2748. WFIFOSET(fd,3);
  2749. }
  2750. // printf("connection request> set delflag 1(o:2)- account_id:%d/login_id1:%d(fifo_id:%d)\n", sd->account_id, sd->login_id1, i);
  2751. break;
  2752. }
  2753. }
  2754. if (i == AUTH_FIFO_SIZE) {
  2755. if (login_fd > 0) { // don't send request if no login-server
  2756. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  2757. WFIFOL(login_fd,2) = sd->account_id;
  2758. WFIFOL(login_fd,6) = sd->login_id1;
  2759. WFIFOL(login_fd,10) = sd->login_id2;
  2760. WFIFOB(login_fd,14) = sd->sex;
  2761. WFIFOL(login_fd,15) = session[fd]->client_addr.sin_addr.s_addr;
  2762. WFIFOSET(login_fd,19);
  2763. } else { // if no login-server, we must refuse connection
  2764. WFIFOW(fd,0) = 0x6c;
  2765. WFIFOB(fd,2) = 0;
  2766. WFIFOSET(fd,3);
  2767. }
  2768. }
  2769. }
  2770. RFIFOSKIP(fd, 17);
  2771. break;
  2772. case 0x66: // char select
  2773. FIFOSD_CHECK(3);
  2774. sprintf(tmp_sql, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'",char_db, sd->account_id, RFIFOB(fd, 2));
  2775. RFIFOSKIP(fd, 3);
  2776. if (mysql_query(&mysql_handle, tmp_sql)) {
  2777. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2778. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2779. }
  2780. sql_res = mysql_store_result(&mysql_handle);
  2781. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  2782. if (sql_row)
  2783. {
  2784. int char_id = atoi(sql_row[0]);
  2785. mysql_free_result(sql_res); //Free'd as soon as possible
  2786. mmo_char_fromsql(char_id, char_dat);
  2787. char_dat[0].sex = sd->sex;
  2788. } else {
  2789. mysql_free_result(sql_res);
  2790. break;
  2791. }
  2792. if (log_char) {
  2793. char escaped_name[NAME_LENGTH*2];
  2794. sprintf(tmp_sql,"INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  2795. charlog_db, sd->account_id, RFIFOB(fd, 2), jstrescapecpy(escaped_name, char_dat[0].name));
  2796. //query
  2797. if(mysql_query(&mysql_handle, tmp_sql)) {
  2798. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  2799. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2800. }
  2801. }
  2802. ShowInfo("Selected char: (Account %d: %d - %s)" RETCODE, sd->account_id, RFIFOB(fd, 2), char_dat[0].name);
  2803. i = search_mapserver(char_dat[0].last_point.map, -1, -1);
  2804. // if map is not found, we check major cities
  2805. if (i < 0) {
  2806. unsigned short j;
  2807. ShowWarning("Unable to find map-server for '%s', resorting to sending to a major city.\n", mapindex_id2name(char_dat[0].last_point.map));
  2808. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  2809. char_dat[0].last_point.map = j;
  2810. char_dat[0].last_point.x = 273; // savepoint coordinates
  2811. char_dat[0].last_point.y = 354;
  2812. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  2813. char_dat[0].last_point.map = j;
  2814. char_dat[0].last_point.x = 120; // savepoint coordinates
  2815. char_dat[0].last_point.y = 100;
  2816. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  2817. char_dat[0].last_point.map = j;
  2818. char_dat[0].last_point.x = 160; // savepoint coordinates
  2819. char_dat[0].last_point.y = 94;
  2820. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  2821. char_dat[0].last_point.map = j;
  2822. char_dat[0].last_point.x = 116; // savepoint coordinates
  2823. char_dat[0].last_point.y = 57;
  2824. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  2825. char_dat[0].last_point.map = j;
  2826. char_dat[0].last_point.x = 87; // savepoint coordinates
  2827. char_dat[0].last_point.y = 117;
  2828. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  2829. char_dat[0].last_point.map = j;
  2830. char_dat[0].last_point.x = 94; // savepoint coordinates
  2831. char_dat[0].last_point.y = 103;
  2832. } else {
  2833. // get first online server
  2834. i = 0;
  2835. for(j = 0; j < MAX_MAP_SERVERS; j++)
  2836. if (server_fd[j] > 0 && server[j].map[0]) {
  2837. i = j;
  2838. ShowDebug("Map-server #%d found with a map: '%s'.\n", j, server[j].map[0]);
  2839. break;
  2840. }
  2841. // if no map-servers are connected, we send: server closed
  2842. if (j == MAX_MAP_SERVERS) {
  2843. WFIFOW(fd,0) = 0x81;
  2844. WFIFOB(fd,2) = 1; // 01 = Server closed
  2845. WFIFOSET(fd,3);
  2846. break;
  2847. }
  2848. }
  2849. }
  2850. WFIFOW(fd, 0) =0x71;
  2851. WFIFOL(fd, 2) =char_dat[0].char_id;
  2852. memcpy(WFIFOP(fd,6), mapindex_id2name(char_dat[0].last_point.map), MAP_NAME_LENGTH);
  2853. // Andvanced subnet check [LuzZza]
  2854. if((subnet_map_ip = lan_subnetcheck((long *)p)))
  2855. WFIFOL(fd,22) = subnet_map_ip;
  2856. else
  2857. WFIFOL(fd,22) = server[i].ip;
  2858. WFIFOW(fd,26) = server[i].port;
  2859. WFIFOSET(fd,28);
  2860. if (auth_fifo_pos >= AUTH_FIFO_SIZE) {
  2861. auth_fifo_pos = 0;
  2862. }
  2863. auth_fifo[auth_fifo_pos].account_id = sd->account_id;
  2864. auth_fifo[auth_fifo_pos].char_id = char_dat[0].char_id;
  2865. auth_fifo[auth_fifo_pos].login_id1 = sd->login_id1;
  2866. auth_fifo[auth_fifo_pos].login_id2 = sd->login_id2;
  2867. auth_fifo[auth_fifo_pos].delflag = 0;
  2868. auth_fifo[auth_fifo_pos].char_pos = 0;
  2869. auth_fifo[auth_fifo_pos].sex = sd->sex;
  2870. auth_fifo[auth_fifo_pos].connect_until_time = sd->connect_until_time;
  2871. auth_fifo[auth_fifo_pos].ip = session[fd]->client_addr.sin_addr.s_addr;
  2872. //Send NEW auth packet [Kevin]
  2873. if ((map_fd = server_fd[i]) < 1 || session[map_fd] == NULL)
  2874. {
  2875. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  2876. server_fd[i] = -1;
  2877. memset(&server[i], 0, sizeof(struct mmo_map_server));
  2878. break;
  2879. }
  2880. WFIFOW(map_fd,0) = 0x2afd;
  2881. WFIFOW(map_fd,2) = 20 + sizeof(struct mmo_charstatus);
  2882. WFIFOL(map_fd,4) = auth_fifo[auth_fifo_pos].account_id;
  2883. WFIFOL(map_fd,8) = auth_fifo[auth_fifo_pos].login_id1;
  2884. WFIFOL(map_fd,16) = auth_fifo[auth_fifo_pos].login_id2;
  2885. WFIFOL(map_fd,12) = (unsigned long)auth_fifo[auth_fifo_pos].connect_until_time;
  2886. memcpy(WFIFOP(map_fd,20), &char_dat[0], sizeof(struct mmo_charstatus));
  2887. WFIFOSET(map_fd, WFIFOW(map_fd,2));
  2888. set_char_online(i, auth_fifo[auth_fifo_pos].char_id, auth_fifo[auth_fifo_pos].account_id);
  2889. //Checks to see if the even share setting of the party must be broken.
  2890. inter_party_logged(char_dat[0].party_id, char_dat[0].account_id, char_dat[0].char_id);
  2891. auth_fifo_pos++;
  2892. break;
  2893. case 0x67: // make new
  2894. FIFOSD_CHECK(37);
  2895. if(char_new == 0) //turn character creation on/off [Kevin]
  2896. i = -2;
  2897. else
  2898. i = make_new_char_sql(fd, RFIFOP(fd, 2));
  2899. //if (i < 0) {
  2900. // WFIFOW(fd, 0) = 0x6e;
  2901. // WFIFOB(fd, 2) = 0x00;
  2902. // WFIFOSET(fd, 3);
  2903. // RFIFOSKIP(fd, 37);
  2904. // break;
  2905. //}
  2906. //Changed that we can support 'Charname already exists' (-1) amd 'Char creation denied' (-2)
  2907. //And 'You are underaged' (-3) (XD) [Sirius]
  2908. if(i == -1){
  2909. //already exists
  2910. WFIFOW(fd, 0) = 0x6e;
  2911. WFIFOB(fd, 2) = 0x00;
  2912. WFIFOSET(fd, 3);
  2913. RFIFOSKIP(fd, 37);
  2914. break;
  2915. }else if(i == -2){
  2916. //denied
  2917. WFIFOW(fd, 0) = 0x6e;
  2918. WFIFOB(fd, 2) = 0x02;
  2919. WFIFOSET(fd, 3);
  2920. RFIFOSKIP(fd, 37);
  2921. break;
  2922. }else if(i == -3){
  2923. //underaged XD
  2924. WFIFOW(fd, 0) = 0x6e;
  2925. WFIFOB(fd, 2) = 0x01;
  2926. WFIFOSET(fd, 3);
  2927. RFIFOSKIP(fd, 37);
  2928. break;
  2929. }
  2930. WFIFOW(fd, 0) = 0x6d;
  2931. memset(WFIFOP(fd, 2), 0x00, 106);
  2932. mmo_char_fromsql_short(i, char_dat); //Only the short data is needed.
  2933. //mmo_char_fromsql(i, char_dat);
  2934. i = 0;
  2935. WFIFOL(fd, 2) = char_dat[i].char_id;
  2936. WFIFOL(fd,2+4) = char_dat[i].base_exp>LONG_MAX?LONG_MAX:char_dat[i].base_exp;
  2937. WFIFOL(fd,2+8) = char_dat[i].zeny;
  2938. WFIFOL(fd,2+12) = char_dat[i].job_exp>LONG_MAX?LONG_MAX:char_dat[i].job_exp;
  2939. WFIFOL(fd,2+16) = char_dat[i].job_level;
  2940. WFIFOL(fd,2+28) = char_dat[i].karma;
  2941. WFIFOL(fd,2+32) = char_dat[i].manner;
  2942. WFIFOW(fd,2+40) = 0x30;
  2943. WFIFOW(fd,2+42) = (char_dat[i].hp > SHRT_MAX) ? SHRT_MAX : char_dat[i].hp;
  2944. WFIFOW(fd,2+44) = (char_dat[i].max_hp > SHRT_MAX) ? SHRT_MAX : char_dat[i].max_hp;
  2945. WFIFOW(fd,2+46) = (char_dat[i].sp > SHRT_MAX) ? SHRT_MAX : char_dat[i].sp;
  2946. WFIFOW(fd,2+48) = (char_dat[i].max_sp > SHRT_MAX) ? SHRT_MAX : char_dat[i].max_sp;
  2947. WFIFOW(fd,2+50) = DEFAULT_WALK_SPEED; // char_dat[i].speed;
  2948. WFIFOW(fd,2+52) = char_dat[i].class_;
  2949. WFIFOW(fd,2+54) = char_dat[i].hair;
  2950. WFIFOW(fd,2+58) = char_dat[i].base_level;
  2951. WFIFOW(fd,2+60) = (char_dat[i].skill_point > SHRT_MAX) ? SHRT_MAX : char_dat[i].skill_point;
  2952. WFIFOW(fd,2+64) = char_dat[i].shield;
  2953. WFIFOW(fd,2+66) = char_dat[i].head_top;
  2954. WFIFOW(fd,2+68) = char_dat[i].head_mid;
  2955. WFIFOW(fd,2+70) = char_dat[i].hair_color;
  2956. memcpy(WFIFOP(fd,2+74), char_dat[i].name, NAME_LENGTH);
  2957. WFIFOB(fd,2+98) = char_dat[i].str>UCHAR_MAX?UCHAR_MAX:char_dat[i].str;
  2958. WFIFOB(fd,2+99) = char_dat[i].agi>UCHAR_MAX?UCHAR_MAX:char_dat[i].agi;
  2959. WFIFOB(fd,2+100) = char_dat[i].vit>UCHAR_MAX?UCHAR_MAX:char_dat[i].vit;
  2960. WFIFOB(fd,2+101) = char_dat[i].int_>UCHAR_MAX?UCHAR_MAX:char_dat[i].int_;
  2961. WFIFOB(fd,2+102) = char_dat[i].dex>UCHAR_MAX?UCHAR_MAX:char_dat[i].dex;
  2962. WFIFOB(fd,2+103) = char_dat[i].luk>UCHAR_MAX?UCHAR_MAX:char_dat[i].luk;
  2963. WFIFOB(fd,2+104) = char_dat[i].char_num;
  2964. WFIFOSET(fd, 108);
  2965. RFIFOSKIP(fd, 37);
  2966. //to do
  2967. for(ch = 0; ch < 9; ch++) {
  2968. if (sd->found_char[ch] == -1) {
  2969. sd->found_char[ch] = char_dat[i].char_id;
  2970. break;
  2971. }
  2972. }
  2973. break;
  2974. case 0x68: /* delete char */
  2975. FIFOSD_CHECK(46);
  2976. {
  2977. int cid = RFIFOL(fd,2);
  2978. ShowInfo(CL_RED" Request Char Deletion:"CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  2979. memcpy(email, RFIFOP(fd,6), 40);
  2980. /* Check if e-mail is correct */
  2981. if(strcmpi(email, sd->email)){
  2982. if(strcmp("a@a.com", sd->email) == 0){
  2983. if(strcmp("a@a.com", email) == 0 || strcmp("", email) == 0){
  2984. //ignore
  2985. }else{
  2986. //del fail
  2987. WFIFOW(fd, 0) = 0x70;
  2988. WFIFOB(fd, 2) = 0;
  2989. WFIFOSET(fd, 3);
  2990. RFIFOSKIP(fd, 46);
  2991. break;
  2992. }
  2993. }else{
  2994. //del fail
  2995. WFIFOW(fd, 0) = 0x70;
  2996. WFIFOB(fd, 2) = 0;
  2997. WFIFOSET(fd, 3);
  2998. RFIFOSKIP(fd, 46);
  2999. break;
  3000. }
  3001. }
  3002. for(i = 0; i < 9; i++) {
  3003. /* Debug:
  3004. printf("Checking if char to be deleted: %d - %d (%d)\n", sd->found_char[i], RFIFOL(fd, 2), sd->account_id);
  3005. */
  3006. if (sd->found_char[i] == cid) {
  3007. for(ch = i; ch < 9-1; ch++)
  3008. sd->found_char[ch] = sd->found_char[ch+1];
  3009. sd->found_char[8] = -1;
  3010. break;
  3011. }
  3012. }
  3013. /* Such a character does not exist in the account */
  3014. /* If so, you are so screwed. */
  3015. if (i == 9) {
  3016. WFIFOW(fd, 0) = 0x70;
  3017. WFIFOB(fd, 2) = 0;
  3018. WFIFOSET(fd, 3);
  3019. break;
  3020. }
  3021. /* Grab the partner id */
  3022. sprintf(tmp_sql, "SELECT `partner_id` FROM `%s` WHERE `char_id`='%d'",char_db, cid);
  3023. if (mysql_query(&mysql_handle, tmp_sql)) {
  3024. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3025. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3026. }
  3027. sql_res = mysql_store_result(&mysql_handle);
  3028. if(sql_res)
  3029. {
  3030. int char_pid=0;
  3031. sql_row = mysql_fetch_row(sql_res);
  3032. if (sql_row)
  3033. char_pid = atoi(sql_row[0]);
  3034. mysql_free_result(sql_res);
  3035. /* Delete character and partner (if any) */
  3036. delete_char_sql(cid, char_pid);
  3037. if (char_pid != 0)
  3038. { /* If there is partner, tell map server to do divorce */
  3039. WBUFW(buf,0) = 0x2b12;
  3040. WBUFL(buf,2) = RFIFOL(fd,2);
  3041. WBUFL(buf,6) = char_pid;
  3042. mapif_sendall(buf,10);
  3043. }
  3044. }
  3045. /* Char successfully deleted. <- For sure? There could had been an sql db error, what is done then?. [Skotlex] */
  3046. WFIFOW(fd, 0) = 0x6f;
  3047. WFIFOSET(fd, 2);
  3048. RFIFOSKIP(fd, 46);
  3049. break;
  3050. }
  3051. case 0x2af8: // login as map-server
  3052. if (RFIFOREST(fd) < 60)
  3053. return 0;
  3054. WFIFOW(fd, 0) = 0x2af9;
  3055. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3056. if (server_fd[i] <= 0)
  3057. break;
  3058. }
  3059. if (i == MAX_MAP_SERVERS || strcmp((const char*)RFIFOP(fd,2), userid) || strcmp((const char*)RFIFOP(fd,26), passwd)) {
  3060. WFIFOB(fd,2) = 3;
  3061. WFIFOSET(fd, 3);
  3062. } else {
  3063. int len;
  3064. WFIFOB(fd,2) = 0;
  3065. WFIFOSET(fd, 3);
  3066. session[fd]->func_parse = parse_frommap;
  3067. server_fd[i] = fd;
  3068. server[i].ip = RFIFOL(fd, 54);
  3069. server[i].port = RFIFOW(fd, 58);
  3070. server[i].users = 0;
  3071. memset(server[i].map, 0, sizeof(server[i].map));
  3072. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3073. char_mapif_init(fd);
  3074. // send gm acccounts level to map-servers
  3075. len = 4;
  3076. WFIFOW(fd,0) = 0x2b15;
  3077. for(i = 0; i < GM_num; i++) {
  3078. WFIFOL(fd,len) = gm_account[i].account_id;
  3079. WFIFOB(fd,len+4) = (unsigned char)gm_account[i].level;
  3080. len += 5;
  3081. }
  3082. WFIFOW(fd,2) = len;
  3083. WFIFOSET(fd,len);
  3084. }
  3085. RFIFOSKIP(fd,60);
  3086. break;
  3087. case 0x187: // Alive?
  3088. if (RFIFOREST(fd) < 6) {
  3089. return 0;
  3090. }
  3091. RFIFOSKIP(fd, 6);
  3092. break;
  3093. case 0x7530: // Athena info get
  3094. WFIFOW(fd, 0) = 0x7531;
  3095. WFIFOB(fd, 2) = ATHENA_MAJOR_VERSION;
  3096. WFIFOB(fd, 3) = ATHENA_MINOR_VERSION;
  3097. WFIFOB(fd, 4) = ATHENA_REVISION;
  3098. WFIFOB(fd, 5) = ATHENA_RELEASE_FLAG;
  3099. WFIFOB(fd, 6) = ATHENA_OFFICIAL_FLAG;
  3100. WFIFOB(fd, 7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  3101. WFIFOW(fd, 8) = ATHENA_MOD_VERSION;
  3102. WFIFOSET(fd, 10);
  3103. RFIFOSKIP(fd, 2);
  3104. return 0;
  3105. case 0x7532: // disconnect(default also disconnect)
  3106. default:
  3107. session[fd]->eof = 1;
  3108. return 0;
  3109. }
  3110. }
  3111. RFIFOFLUSH(fd);
  3112. return 0;
  3113. }
  3114. // Console Command Parser [Wizputer]
  3115. int parse_console(char *buf) {
  3116. char *type,*command;
  3117. type = (char *)aMalloc(64);
  3118. command = (char *)aMalloc(64);
  3119. memset(type,0,64);
  3120. memset(command,0,64);
  3121. ShowNotice("Console: %s\n",buf);
  3122. if ( sscanf(buf, "%[^:]:%[^\n]", type , command ) < 2 )
  3123. sscanf(buf,"%[^\n]",type);
  3124. ShowNotice("Type of command: %s || Command: %s \n",type,command);
  3125. if(buf) aFree(buf);
  3126. if(type) aFree(type);
  3127. if(command) aFree(command);
  3128. return 0;
  3129. }
  3130. // MAP send all
  3131. int mapif_sendall(unsigned char *buf, unsigned int len) {
  3132. int i, c;
  3133. int fd;
  3134. c = 0;
  3135. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3136. if ((fd = server_fd[i]) > 0) { //0 Should not be a valid server_fd [Skotlex]
  3137. if (session[fd] == NULL)
  3138. { //Could this be the crash's source? [Skotlex]
  3139. ShowError("mapif_sendall: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", fd, i);
  3140. server_fd[i] = -1;
  3141. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3142. continue;
  3143. }
  3144. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3145. realloc_writefifo(fd, len);
  3146. memcpy(WFIFOP(fd,0), buf, len);
  3147. WFIFOSET(fd,len);
  3148. c++;
  3149. }
  3150. }
  3151. return c;
  3152. }
  3153. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len) {
  3154. int i, c;
  3155. int fd;
  3156. c = 0;
  3157. for(i=0, c=0;i<MAX_MAP_SERVERS;i++){
  3158. if ((fd = server_fd[i]) > 0 && fd != sfd) {
  3159. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3160. realloc_writefifo(fd, len);
  3161. memcpy(WFIFOP(fd,0), buf, len);
  3162. WFIFOSET(fd, len);
  3163. c++;
  3164. }
  3165. }
  3166. return c;
  3167. }
  3168. int mapif_send(int fd, unsigned char *buf, unsigned int len) {
  3169. int i;
  3170. if (fd >= 0) {
  3171. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3172. if (fd == server_fd[i]) {
  3173. if (WFIFOSPACE(fd) < len) //Increase buffer size.
  3174. realloc_writefifo(fd, len);
  3175. memcpy(WFIFOP(fd,0), buf, len);
  3176. WFIFOSET(fd,len);
  3177. return 1;
  3178. }
  3179. }
  3180. }
  3181. return 0;
  3182. }
  3183. int send_users_tologin(int tid, unsigned int tick, int id, int data) {
  3184. int users = count_users();
  3185. unsigned char buf[16];
  3186. if (login_fd > 0 && session[login_fd]) {
  3187. // send number of user to login server
  3188. WFIFOW(login_fd,0) = 0x2714;
  3189. WFIFOL(login_fd,2) = users;
  3190. WFIFOSET(login_fd,6);
  3191. }
  3192. // send number of players to all map-servers
  3193. WBUFW(buf,0) = 0x2b00;
  3194. WBUFL(buf,2) = users;
  3195. mapif_sendall(buf, 6);
  3196. return 0;
  3197. }
  3198. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap) {
  3199. struct online_char_data* character = (struct online_char_data*)data;
  3200. int *i = va_arg(ap, int*);
  3201. int count = va_arg(ap, int);
  3202. if ((*i) >= count)
  3203. return 0; //This is an error that shouldn't happen....
  3204. if(character->server > -1) {
  3205. WFIFOHEAD(login_fd, 8+count*4);
  3206. WFIFOL(login_fd, 8+(*i)*4) =character->account_id;
  3207. (*i)++;
  3208. return 1;
  3209. }
  3210. return 0;
  3211. }
  3212. int send_accounts_tologin(int tid, unsigned int tick, int id, int data) {
  3213. int users = count_users(), i=0;
  3214. if (login_fd > 0 && session[login_fd]) {
  3215. // send account list to login server
  3216. WFIFOHEAD(login_fd, 8+users*4);
  3217. WFIFOW(login_fd,0) = 0x272d;
  3218. WFIFOL(login_fd,4) = users;
  3219. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  3220. WFIFOW(login_fd,2) = 8+ i*4;
  3221. if (i > 0)
  3222. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  3223. }
  3224. return 0;
  3225. }
  3226. int check_connect_login_server(int tid, unsigned int tick, int id, int data) {
  3227. if (login_fd <= 0 || session[login_fd] == NULL) {
  3228. struct char_session_data *sd;
  3229. int i, cc;
  3230. unsigned char buf[16];
  3231. ShowInfo("Attempt to connect to login-server...\n");
  3232. login_fd = make_connection(login_ip, login_port);
  3233. if (login_fd == -1)
  3234. { //Try again later. [Skotlex]
  3235. login_fd = 0;
  3236. return 0;
  3237. }
  3238. session[login_fd]->func_parse = parse_tologin;
  3239. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  3240. WFIFOW(login_fd,0) = 0x2710;
  3241. memcpy(WFIFOP(login_fd,2), userid, 24);
  3242. memcpy(WFIFOP(login_fd,26), passwd, 24);
  3243. WFIFOL(login_fd,50) = 0;
  3244. WFIFOL(login_fd,54) = char_ip;
  3245. WFIFOL(login_fd,58) = char_port;
  3246. memset(WFIFOP(login_fd,60), 0, 20);
  3247. memcpy(WFIFOP(login_fd,60), server_name, strlen(server_name) < 20 ? strlen(server_name) : 20);
  3248. WFIFOW(login_fd,80) = 0;
  3249. WFIFOW(login_fd,82) = char_maintenance;
  3250. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  3251. WFIFOSET(login_fd,86);
  3252. //(re)connected to login-server,
  3253. //now wi'll look in sql which player's are ON and set them OFF
  3254. //AND send to all mapservers (if we have one / ..) to kick the players
  3255. //so the bug is fixed, if'ure using more than one charservers (worlds)
  3256. //that the player'S got reejected from server after a 'world' crash^^
  3257. //2b1f AID.L B1
  3258. sprintf(tmp_sql, "SELECT `account_id`, `online` FROM `%s` WHERE `online` = '1'", char_db);
  3259. if(mysql_query(&mysql_handle, tmp_sql)){
  3260. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3261. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3262. return -1;
  3263. }
  3264. sql_res = mysql_store_result(&mysql_handle);
  3265. if(sql_res){
  3266. cc = (int)mysql_num_rows(sql_res);
  3267. ShowStatus("Setting %d Players offline\n", cc);
  3268. while((sql_row = mysql_fetch_row(sql_res))){
  3269. //sql_row[0] == AID
  3270. //tell the loginserver
  3271. WFIFOW(login_fd, 0) = 0x272c; //set off
  3272. WFIFOL(login_fd, 2) = atoi(sql_row[0]); //AID
  3273. WFIFOSET(login_fd, 6);
  3274. //tell map to 'kick' the player (incase of 'on' ..)
  3275. WBUFW(buf, 0) = 0x2b1f;
  3276. WBUFL(buf, 2) = atoi(sql_row[0]);
  3277. WBUFB(buf, 6) = 1;
  3278. mapif_sendall(buf, 7);
  3279. //kick the player if he's on charselect
  3280. for(i = 0; i < fd_max; i++){
  3281. if(session[i] && (sd = (struct char_session_data*)session[i]->session_data)){
  3282. if(sd->account_id == atoi(sql_row[0])){
  3283. session[i]->eof = 1;
  3284. break;
  3285. }
  3286. }
  3287. }
  3288. }
  3289. mysql_free_result(sql_res);
  3290. }else{
  3291. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3292. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3293. return -1;
  3294. }
  3295. //Now Update all players to 'OFFLINE'
  3296. sprintf(tmp_sql, "UPDATE `%s` SET `online` = '0'", char_db);
  3297. if(mysql_query(&mysql_handle, tmp_sql)){
  3298. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3299. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3300. }
  3301. sprintf(tmp_sql, "UPDATE `%s` SET `online` = '0'", guild_member_db);
  3302. if(mysql_query(&mysql_handle, tmp_sql)){
  3303. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3304. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3305. }
  3306. sprintf(tmp_sql, "UPDATE `%s` SET `connect_member` = '0'", guild_db);
  3307. if(mysql_query(&mysql_handle, tmp_sql)){
  3308. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3309. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3310. }
  3311. }
  3312. return 0;
  3313. }
  3314. //------------------------------------------------
  3315. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  3316. //replies/disconnect the player we tried to kick. [Skotlex]
  3317. //------------------------------------------------
  3318. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, int data)
  3319. {
  3320. struct online_char_data* character;
  3321. if ((character = idb_get(online_char_db, id)) != NULL && character->waiting_disconnect)
  3322. { //Mark it offline due to timeout.
  3323. set_char_offline(character->char_id, character->account_id);
  3324. }
  3325. return 0;
  3326. }
  3327. //----------------------------------------------------------
  3328. // Return numerical value of a switch configuration by [Yor]
  3329. // on/off, english, fran軋is, deutsch, espa�l
  3330. //----------------------------------------------------------
  3331. int config_switch(const char *str) {
  3332. if (strcmpi(str, "on") == 0 || strcmpi(str, "yes") == 0 || strcmpi(str, "oui") == 0 || strcmpi(str, "ja") == 0 || strcmpi(str, "si") == 0)
  3333. return 1;
  3334. if (strcmpi(str, "off") == 0 || strcmpi(str, "no") == 0 || strcmpi(str, "non") == 0 || strcmpi(str, "nein") == 0)
  3335. return 0;
  3336. return atoi(str);
  3337. }
  3338. //----------------------------------
  3339. // Reading Lan Support configuration
  3340. // Rewrote: Anvanced subnet check [LuzZza]
  3341. //----------------------------------
  3342. int char_lan_config_read(const char *lancfgName) {
  3343. FILE *fp;
  3344. int line_num = 0;
  3345. char line[1024], w1[64], w2[64], w3[64], w4[64], w5[64];
  3346. if((fp = fopen(lancfgName, "r")) == NULL) {
  3347. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  3348. return 1;
  3349. }
  3350. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  3351. while(fgets(line, sizeof(line)-1, fp)) {
  3352. line_num++;
  3353. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  3354. continue;
  3355. line[sizeof(line)-1] = '\0';
  3356. if(sscanf(line,"%[^:]: %[^/]/%[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4, w5) != 5) {
  3357. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  3358. continue;
  3359. }
  3360. remove_control_chars((unsigned char *)w1);
  3361. remove_control_chars((unsigned char *)w2);
  3362. remove_control_chars((unsigned char *)w3);
  3363. remove_control_chars((unsigned char *)w4);
  3364. remove_control_chars((unsigned char *)w5);
  3365. if(strcmpi(w1, "subnet") == 0) {
  3366. subnet[subnet_count].subnet = inet_addr(w2);
  3367. subnet[subnet_count].mask = inet_addr(w3);
  3368. subnet[subnet_count].char_ip = inet_addr(w4);
  3369. subnet[subnet_count].map_ip = inet_addr(w5);
  3370. subnet_count++;
  3371. }
  3372. ShowStatus("Information about %d subnetworks readen.\n", subnet_count);
  3373. }
  3374. fclose(fp);
  3375. return 0;
  3376. }
  3377. void do_final(void) {
  3378. ShowInfo("Doing final stage...\n");
  3379. //mmo_char_sync();
  3380. //inter_save();
  3381. do_final_itemdb();
  3382. //check SQL save progress.
  3383. //wait until save char complete
  3384. set_all_offline();
  3385. inter_final();
  3386. flush_fifos();
  3387. mapindex_final();
  3388. sprintf(tmp_sql,"DELETE FROM `ragsrvinfo");
  3389. if (mysql_query(&mysql_handle, tmp_sql))
  3390. {
  3391. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3392. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3393. }
  3394. if(gm_account) {
  3395. aFree(gm_account);
  3396. gm_account = 0;
  3397. }
  3398. if(char_dat) {
  3399. aFree(char_dat);
  3400. char_dat = 0;
  3401. }
  3402. delete_session(login_fd);
  3403. delete_session(char_fd);
  3404. char_db_->destroy(char_db_, NULL);
  3405. online_char_db->destroy(online_char_db, NULL);
  3406. mysql_close(&mysql_handle);
  3407. mysql_close(&lmysql_handle);
  3408. ShowInfo("ok! all done...\n");
  3409. }
  3410. void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */
  3411. char line[1024], w1[1024], w2[1024];
  3412. FILE *fp;
  3413. ShowInfo("Reading file %s...\n", cfgName);
  3414. if ((fp = fopen(cfgName, "r")) == NULL) {
  3415. ShowFatalError("file not found: %s\n", cfgName);
  3416. exit(1);
  3417. }
  3418. while(fgets(line, sizeof(line)-1, fp)){
  3419. if(line[0] == '/' && line[1] == '/')
  3420. continue;
  3421. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  3422. continue;
  3423. if(strcmpi(w1, "gm_read_method") == 0) {
  3424. if(atoi(w2) != 0)
  3425. char_gm_read = true;
  3426. else
  3427. char_gm_read = false;
  3428. } else if(strcmpi(w1, "gm_db") == 0) {
  3429. strcpy(gm_db, w2);
  3430. } else if(strcmpi(w1,"char_db")==0){
  3431. strcpy(char_db,w2);
  3432. }else if(strcmpi(w1,"scdata_db")==0){
  3433. strcpy(scdata_db,w2);
  3434. }else if(strcmpi(w1,"cart_db")==0){
  3435. strcpy(cart_db,w2);
  3436. }else if(strcmpi(w1,"inventory_db")==0){
  3437. strcpy(inventory_db,w2);
  3438. }else if(strcmpi(w1,"charlog_db")==0){
  3439. strcpy(charlog_db,w2);
  3440. }else if(strcmpi(w1,"storage_db")==0){
  3441. strcpy(storage_db,w2);
  3442. }else if(strcmpi(w1,"reg_db")==0){
  3443. strcpy(reg_db,w2);
  3444. }else if(strcmpi(w1,"skill_db")==0){
  3445. strcpy(skill_db,w2);
  3446. }else if(strcmpi(w1,"interlog_db")==0){
  3447. strcpy(interlog_db,w2);
  3448. }else if(strcmpi(w1,"memo_db")==0){
  3449. strcpy(memo_db,w2);
  3450. }else if(strcmpi(w1,"guild_db")==0){
  3451. strcpy(guild_db,w2);
  3452. }else if(strcmpi(w1,"guild_alliance_db")==0){
  3453. strcpy(guild_alliance_db,w2);
  3454. }else if(strcmpi(w1,"guild_castle_db")==0){
  3455. strcpy(guild_castle_db,w2);
  3456. }else if(strcmpi(w1,"guild_expulsion_db")==0){
  3457. strcpy(guild_expulsion_db,w2);
  3458. }else if(strcmpi(w1,"guild_member_db")==0){
  3459. strcpy(guild_member_db,w2);
  3460. }else if(strcmpi(w1,"guild_skill_db")==0){
  3461. strcpy(guild_skill_db,w2);
  3462. }else if(strcmpi(w1,"guild_position_db")==0){
  3463. strcpy(guild_position_db,w2);
  3464. }else if(strcmpi(w1,"guild_storage_db")==0){
  3465. strcpy(guild_storage_db,w2);
  3466. }else if(strcmpi(w1,"party_db")==0){
  3467. strcpy(party_db,w2);
  3468. }else if(strcmpi(w1,"pet_db")==0){
  3469. strcpy(pet_db,w2);
  3470. }else if(strcmpi(w1,"friend_db")==0){
  3471. strcpy(friend_db,w2);
  3472. }else if(strcmpi(w1,"db_path")==0){
  3473. strcpy(db_path,w2);
  3474. //Map server option to use SQL db or not
  3475. }else if(strcmpi(w1,"use_sql_db")==0){ // added for sql item_db read for char server [Valaris]
  3476. db_use_sqldbs = config_switch(w2);
  3477. ShowStatus("Using SQL dbs: %s\n",w2);
  3478. //custom columns for login database
  3479. }else if(strcmpi(w1,"login_db_level")==0){
  3480. strcpy(login_db_level,w2);
  3481. }else if(strcmpi(w1,"login_db_account_id")==0){
  3482. strcpy(login_db_account_id,w2);
  3483. }else if(strcmpi(w1,"lowest_gm_level")==0){
  3484. lowest_gm_level = atoi(w2);
  3485. ShowStatus("set lowest_gm_level : %s\n",w2);
  3486. //support the import command, just like any other config
  3487. }else if(strcmpi(w1,"import")==0){
  3488. sql_config_read(w2);
  3489. }
  3490. }
  3491. fclose(fp);
  3492. ShowInfo("done reading %s.\n", cfgName);
  3493. }
  3494. int char_config_read(const char *cfgName) {
  3495. struct hostent *h = NULL;
  3496. char line[1024], w1[1024], w2[1024];
  3497. FILE *fp;
  3498. if ((fp = fopen(cfgName, "r")) == NULL) {
  3499. ShowFatalError("Configuration file not found: %s.\n", cfgName);
  3500. exit(1);
  3501. }
  3502. ShowInfo("Reading file %s...\n", cfgName);
  3503. while(fgets(line, sizeof(line)-1, fp)) {
  3504. if (line[0] == '/' && line[1] == '/')
  3505. continue;
  3506. line[sizeof(line)-1] = '\0';
  3507. if (sscanf(line,"%[^:]: %[^\r\n]", w1, w2) != 2)
  3508. continue;
  3509. remove_control_chars((unsigned char *) w1);
  3510. remove_control_chars((unsigned char *) w2);
  3511. if(strcmpi(w1,"timestamp_format")==0) {
  3512. strncpy(timestamp_format, w2, 20);
  3513. } else if(strcmpi(w1,"console_silent")==0){
  3514. msg_silent = 0; //To always allow the next line to show up.
  3515. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  3516. msg_silent = atoi(w2);
  3517. } else if (strcmpi(w1, "userid") == 0) {
  3518. strncpy(userid, w2, 24);
  3519. } else if (strcmpi(w1, "passwd") == 0) {
  3520. strncpy(passwd, w2, 24);
  3521. } else if (strcmpi(w1, "server_name") == 0) {
  3522. memcpy(server_name, w2, sizeof(server_name));
  3523. server_name[sizeof(server_name) - 1] = '\0';
  3524. ShowStatus("%s server has been initialized\n", w2);
  3525. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  3526. if (strlen(w2) >= 4) {
  3527. memcpy(wisp_server_name, w2, sizeof(wisp_server_name));
  3528. wisp_server_name[sizeof(wisp_server_name) - 1] = '\0';
  3529. }
  3530. } else if (strcmpi(w1, "login_ip") == 0) {
  3531. login_ip_set_ = 1;
  3532. h = gethostbyname (w2);
  3533. if (h != NULL) {
  3534. ShowStatus("Login server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3535. sprintf(login_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3536. } else
  3537. memcpy(login_ip_str,w2,16);
  3538. } else if (strcmpi(w1, "login_port") == 0) {
  3539. login_port=atoi(w2);
  3540. } else if (strcmpi(w1, "char_ip") == 0) {
  3541. char_ip_set_ = 1;
  3542. h = gethostbyname (w2);
  3543. if(h != NULL) {
  3544. ShowStatus("Character server IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3545. sprintf(char_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3546. } else
  3547. memcpy(char_ip_str, w2, 16);
  3548. } else if (strcmpi(w1, "bind_ip") == 0) {
  3549. bind_ip_set_ = 1;
  3550. h = gethostbyname (w2);
  3551. if(h != NULL) {
  3552. ShowStatus("Character server binding IP address : %s -> %d.%d.%d.%d\n", w2, (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3553. sprintf(bind_ip_str, "%d.%d.%d.%d", (unsigned char)h->h_addr[0], (unsigned char)h->h_addr[1], (unsigned char)h->h_addr[2], (unsigned char)h->h_addr[3]);
  3554. } else
  3555. memcpy(bind_ip_str, w2, 16);
  3556. } else if (strcmpi(w1, "char_port") == 0) {
  3557. char_port = atoi(w2);
  3558. } else if (strcmpi(w1, "char_maintenance") == 0) {
  3559. char_maintenance = atoi(w2);
  3560. } else if (strcmpi(w1, "char_new")==0){
  3561. char_new = atoi(w2);
  3562. } else if (strcmpi(w1, "char_new_display")==0){
  3563. char_new_display = atoi(w2);
  3564. } else if (strcmpi(w1, "max_connect_user") == 0) {
  3565. max_connect_user = atoi(w2);
  3566. if (max_connect_user < 0)
  3567. max_connect_user = 0; // unlimited online players
  3568. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  3569. gm_allow_level = atoi(w2);
  3570. if(gm_allow_level < 0)
  3571. gm_allow_level = 99;
  3572. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  3573. check_ip_flag = config_switch(w2);
  3574. } else if (strcmpi(w1, "online_check") == 0) {
  3575. online_check = config_switch(w2);
  3576. } else if (strcmpi(w1, "autosave_time") == 0) {
  3577. autosave_interval = atoi(w2)*1000;
  3578. if (autosave_interval <= 0)
  3579. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  3580. } else if (strcmpi(w1, "save_log") == 0) {
  3581. save_log = config_switch(w2);
  3582. } else if (strcmpi(w1, "start_point") == 0) {
  3583. char map[MAP_NAME_LENGTH];
  3584. int x, y;
  3585. if (sscanf(w2,"%16[^,],%d,%d", map, &x, &y) < 3)
  3586. continue;
  3587. if (strstr(map, ".gat") != NULL) { // Verify at least if '.gat' is in the map name
  3588. start_point.map = mapindex_name2id(map);
  3589. if (!start_point.map)
  3590. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  3591. start_point.x = x;
  3592. start_point.y = y;
  3593. }
  3594. } else if (strcmpi(w1, "start_zeny") == 0) {
  3595. start_zeny = atoi(w2);
  3596. if (start_zeny < 0)
  3597. start_zeny = 0;
  3598. } else if (strcmpi(w1, "start_weapon") == 0) {
  3599. start_weapon = atoi(w2);
  3600. if (start_weapon < 0)
  3601. start_weapon = 0;
  3602. } else if (strcmpi(w1, "start_armor") == 0) {
  3603. start_armor = atoi(w2);
  3604. if (start_armor < 0)
  3605. start_armor = 0;
  3606. } else if(strcmpi(w1,"log_char")==0){ //log char or not [devil]
  3607. log_char = atoi(w2);
  3608. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  3609. strcpy(unknown_char_name, w2);
  3610. unknown_char_name[NAME_LENGTH-1] = 0;
  3611. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  3612. name_ignoring_case = config_switch(w2);
  3613. } else if (strcmpi(w1, "char_name_option") == 0) {
  3614. char_name_option = atoi(w2);
  3615. } else if (strcmpi(w1, "char_name_letters") == 0) {
  3616. strcpy(char_name_letters, w2);
  3617. } else if (strcmpi(w1, "check_ip_flag") == 0) {
  3618. check_ip_flag = config_switch(w2);
  3619. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  3620. char_per_account = atoi(w2);
  3621. } else if (strcmpi(w1, "console") == 0) {
  3622. if(strcmpi(w2,"on") == 0 || strcmpi(w2,"yes") == 0 )
  3623. console = 1;
  3624. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  3625. fame_list_size_chemist = atoi(w2);
  3626. if (fame_list_size_chemist > MAX_FAME_LIST) {
  3627. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  3628. fame_list_size_chemist = MAX_FAME_LIST;
  3629. }
  3630. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  3631. fame_list_size_smith = atoi(w2);
  3632. if (fame_list_size_smith > MAX_FAME_LIST) {
  3633. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  3634. fame_list_size_smith = MAX_FAME_LIST;
  3635. }
  3636. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  3637. fame_list_size_taekwon = atoi(w2);
  3638. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  3639. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  3640. fame_list_size_taekwon = MAX_FAME_LIST;
  3641. }
  3642. } else if (strcmpi(w1, "import") == 0) {
  3643. char_config_read(w2);
  3644. }
  3645. }
  3646. fclose(fp);
  3647. ShowInfo("Done reading %s.\n", cfgName);
  3648. return 0;
  3649. }
  3650. void set_server_type(void)
  3651. {
  3652. SERVER_TYPE = ATHENA_SERVER_CHAR;
  3653. }
  3654. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  3655. {
  3656. struct online_char_data *character= (struct online_char_data*)data;
  3657. if (character->server == -2) //Unknown server.. set them offline
  3658. set_char_offline(character->char_id, character->account_id);
  3659. if (character->server < 0)
  3660. //Free data from players that have not been online for a while.
  3661. db_remove(online_char_db, key);
  3662. return 0;
  3663. }
  3664. static int online_data_cleanup(int tid, unsigned int tick, int id, int data)
  3665. {
  3666. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  3667. return 0;
  3668. }
  3669. int do_init(int argc, char **argv){
  3670. int i;
  3671. for(i = 0; i < MAX_MAP_SERVERS; i++) {
  3672. memset(&server[i], 0, sizeof(struct mmo_map_server));
  3673. server_fd[i] = -1;
  3674. }
  3675. //Read map indexes
  3676. mapindex_init();
  3677. start_point.map = mapindex_name2id("new_1-1.gat");
  3678. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  3679. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  3680. sql_config_read(SQL_CONF_NAME);
  3681. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  3682. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  3683. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  3684. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  3685. }
  3686. ShowInfo("Finished reading the char-server configuration.\n");
  3687. inter_init((argc > 2) ? argv[2] : inter_cfgName); // inter server テハア篳ュ
  3688. ShowInfo("Finished reading the inter-server configuration.\n");
  3689. //Read ItemDB
  3690. do_init_itemdb();
  3691. ShowInfo("Initializing char server.\n");
  3692. online_char_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3693. mmo_char_sql_init();
  3694. ShowInfo("char server initialized.\n");
  3695. // ShowDebug("set parser -> parse_char()...\n");
  3696. set_defaultparse(parse_char);
  3697. // ShowDebug("set terminate function -> do_final().....\n");
  3698. if ((naddr_ != 0) && (login_ip_set_ == 0 || char_ip_set_ == 0)) {
  3699. // The char server should know what IP address it is running on
  3700. // - MouseJstr
  3701. int localaddr = ntohl(addr_[0]);
  3702. unsigned char *ptr = (unsigned char *) &localaddr;
  3703. char buf[16];
  3704. sprintf(buf, "%d.%d.%d.%d", ptr[0], ptr[1], ptr[2], ptr[3]);
  3705. if (naddr_ != 1)
  3706. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", buf);
  3707. else
  3708. ShowStatus("Defaulting to %s as our IP address\n", buf);
  3709. if (login_ip_set_ == 0)
  3710. strcpy(login_ip_str, buf);
  3711. if (char_ip_set_ == 0)
  3712. strcpy(char_ip_str, buf);
  3713. if (ptr[0] == 192 && ptr[1] == 168)
  3714. ShowWarning("Firewall detected.. edit subnet_athena.conf and char_athena.conf\n");
  3715. }
  3716. login_ip = inet_addr(login_ip_str);
  3717. char_ip = inet_addr(char_ip_str);
  3718. ShowInfo("open port %d.....\n",char_port);
  3719. //char_fd = make_listen_port(char_port);
  3720. if (bind_ip_set_)
  3721. char_fd = make_listen_bind(inet_addr(bind_ip_str),char_port);
  3722. else
  3723. char_fd = make_listen_bind(INADDR_ANY,char_port);
  3724. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  3725. add_timer_func_list(send_users_tologin, "send_users_tologin");
  3726. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  3727. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  3728. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  3729. add_timer_interval(gettick() + 600*1000, online_data_cleanup, 0, 0, 600 * 1000);
  3730. // send ALIVE PING to login server.
  3731. add_timer_interval(gettick() + 10, check_connect_login_server, 0, 0, 10 * 1000);
  3732. // send USER COUNT PING to login server.
  3733. add_timer_interval(gettick() + 10, send_users_tologin, 0, 0, 5 * 1000);
  3734. add_timer_interval(gettick() + 3600*1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour.
  3735. if(char_gm_read)
  3736. read_gm_account();
  3737. if ( console ) {
  3738. set_defaultconsoleparse(parse_console);
  3739. start_console();
  3740. }
  3741. //Cleaning the tables for NULL entrys @ startup [Sirius]
  3742. //Chardb clean
  3743. ShowInfo("Cleaning the '%s' table...\n", char_db);
  3744. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `account_id` = '0'", char_db);
  3745. if(mysql_query(&mysql_handle, tmp_sql)){
  3746. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3747. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3748. }
  3749. //guilddb clean
  3750. ShowInfo("Cleaning the '%s' table...\n", guild_db);
  3751. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db);
  3752. if(mysql_query(&mysql_handle, tmp_sql)){
  3753. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3754. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3755. }
  3756. //guildmemberdb clean
  3757. ShowInfo("Cleaning the '%s' table...\n", guild_member_db);
  3758. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db);
  3759. if(mysql_query(&mysql_handle, tmp_sql)){
  3760. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3761. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3762. }
  3763. ShowInfo("End of char server initilization function.\n");
  3764. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  3765. return 0;
  3766. }
  3767. #undef mysql_query
  3768. int debug_mysql_query(char *file, int line, void *mysql, const char *q) {
  3769. #ifdef TWILIGHT
  3770. ShowDebug("sql: %s:%d# %s\n", file, line, q);
  3771. #endif
  3772. return mysql_query((MYSQL *) mysql, q);
  3773. }
  3774. int char_child(int parent_id, int child_id) {
  3775. int tmp_id = 0;
  3776. sprintf (tmp_sql, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id);
  3777. if (mysql_query (&mysql_handle, tmp_sql)) {
  3778. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3779. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3780. }
  3781. sql_res = mysql_store_result (&mysql_handle);
  3782. sql_row = sql_res?mysql_fetch_row (sql_res):NULL;
  3783. if (sql_row)
  3784. tmp_id = atoi (sql_row[0]);
  3785. else
  3786. ShowError("CHAR: child Failed!\n");
  3787. if (sql_res) mysql_free_result (sql_res);
  3788. if ( tmp_id == child_id )
  3789. return 1;
  3790. else
  3791. return 0;
  3792. }
  3793. int char_married(int pl1,int pl2) {
  3794. int tmp_id = 0;
  3795. sprintf (tmp_sql, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1);
  3796. if (mysql_query (&mysql_handle, tmp_sql)) {
  3797. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3798. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3799. }
  3800. sql_res = mysql_store_result (&mysql_handle);
  3801. sql_row = sql_res?mysql_fetch_row (sql_res):NULL;
  3802. if (sql_row)
  3803. tmp_id = atoi (sql_row[0]);
  3804. else
  3805. ShowError("CHAR: married Failed!\n");
  3806. if (sql_res) mysql_free_result (sql_res);
  3807. if ( tmp_id == pl2 )
  3808. return 1;
  3809. else
  3810. return 0;
  3811. }
  3812. int char_nick2id (char *name) {
  3813. int char_id = 0;
  3814. sprintf (tmp_sql, "SELECT `char_id` FROM `%s` WHERE `name` = '%s'", char_db, name);
  3815. if (mysql_query (&mysql_handle, tmp_sql)) {
  3816. ShowSQL("DB error - %s\n",mysql_error(&mysql_handle));
  3817. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3818. }
  3819. sql_res = mysql_store_result (&mysql_handle);
  3820. sql_row = sql_res?mysql_fetch_row(sql_res):NULL;
  3821. if (sql_row)
  3822. char_id = atoi (sql_row[0]);
  3823. else
  3824. ShowError ("CHAR: nick2id Failed!\n");
  3825. if (sql_res) mysql_free_result (sql_res);
  3826. return char_id;
  3827. }