char.c 133 KB

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