char.c 147 KB

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