char.c 151 KB

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