char.c 146 KB

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