char.c 155 KB

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