char.c 153 KB

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