char.c 130 KB

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