char.c 147 KB

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