char.c 151 KB

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