char.c 154 KB

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