chrif.cpp 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "chrif.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include "../common/cbasetypes.hpp"
  7. #include "../common/ers.hpp"
  8. #include "../common/malloc.hpp"
  9. #include "../common/nullpo.hpp"
  10. #include "../common/showmsg.hpp"
  11. #include "../common/socket.hpp"
  12. #include "../common/strlib.hpp"
  13. #include "../common/timer.hpp"
  14. #include "battle.hpp"
  15. #include "clan.hpp"
  16. #include "clif.hpp"
  17. #include "elemental.hpp"
  18. #include "guild.hpp"
  19. #include "homunculus.hpp"
  20. #include "instance.hpp"
  21. #include "intif.hpp"
  22. #include "log.hpp"
  23. #include "map.hpp"
  24. #include "mercenary.hpp"
  25. #include "npc.hpp"
  26. #include "pc.hpp"
  27. #include "pc_groups.hpp"
  28. #include "pet.hpp"
  29. #include "script.hpp" // script_config
  30. #include "storage.hpp"
  31. static TIMER_FUNC(check_connect_char_server);
  32. static struct eri *auth_db_ers; //For reutilizing player login structures.
  33. static DBMap* auth_db; // int id -> struct auth_node*
  34. static bool char_init_done = false; //server already initialized? Used for InterInitOnce and vending loadings
  35. static const int packet_len_table[0x3d] = { // U - used, F - free
  36. 60, 3,-1,-1,10,-1, 6,-1, // 2af8-2aff: U->2af8, U->2af9, U->2afa, U->2afb, U->2afc, U->2afd, U->2afe, U->2aff
  37. 6,-1,18, 7,-1,39,30, 10, // 2b00-2b07: U->2b00, U->2b01, U->2b02, U->2b03, U->2b04, U->2b05, U->2b06, U->2b07
  38. 6,30, 10, -1,86, 7,44,34, // 2b08-2b0f: U->2b08, U->2b09, U->2b0a, U->2b0b, U->2b0c, U->2b0d, U->2b0e, U->2b0f
  39. 11,10,10, 0,11, -1, 0,10, // 2b10-2b17: U->2b10, U->2b11, U->2b12, F->2b13, U->2b14, U->2b15, F->2b16, U->2b17
  40. 2,10, 2,-1,-1,-1, 2, 7, // 2b18-2b1f: U->2b18, U->2b19, U->2b1a, U->2b1b, U->2b1c, U->2b1d, U->2b1e, U->2b1f
  41. -1,10, 8, 2, 2,14,19,19, // 2b20-2b27: U->2b20, U->2b21, U->2b22, U->2b23, U->2b24, U->2b25, U->2b26, U->2b27
  42. -1, 0, 6,15, 0, 6,-1,-1, // 2b28-2b2f: U->2b28, F->2b29, U->2b2a, U->2b2b, F->2b2c, U->2b2d, U->2b2e, U->2b2f
  43. };
  44. //Used Packets:
  45. //2af8: Outgoing, chrif_connect -> 'connect to charserver / auth @ charserver'
  46. //2af9: Incoming, chrif_connectack -> 'answer of the 2af8 login(ok / fail)'
  47. //2afa: Outgoing, chrif_sendmap -> 'sending our maps'
  48. //2afb: Incoming, chrif_sendmapack -> 'Maps received successfully / or not .. also received server name & default map'
  49. //2afc: Outgoing, chrif_scdata_request -> request sc_data for pc_authok'ed char. <- new command reuses previous one.
  50. //2afd: Incoming, chrif_authok -> 'client authentication ok'
  51. //2afe: Outgoing, send_usercount_tochar -> 'sends player count of this map server to charserver'
  52. //2aff: Outgoing, send_users_tochar -> 'sends all actual connected character ids to charserver'
  53. //2b00: Incoming, map_setusers -> 'set the actual usercount? PACKET.2B COUNT.L.. ?' (not sure)
  54. //2b01: Outgoing, chrif_save -> 'charsave of char XY account XY (complete struct)'
  55. //2b02: Outgoing, chrif_charselectreq -> 'player returns from ingame to charserver to select another char.., this packets includes sessid etc' ? (not 100% sure)
  56. //2b03: Incoming, clif_charselectok -> '' (i think its the packet after enterworld?) (not sure)
  57. //2b04: Incoming, chrif_recvmap -> 'getting maps from charserver of other mapserver's'
  58. //2b05: Outgoing, chrif_changemapserver -> 'Tell the charserver the mapchange / quest for ok...'
  59. //2b06: Incoming, chrif_changemapserverack -> 'awnser of 2b05, ok/fail, data: dunno^^'
  60. //2b07: Outgoing, chrif_removefriend -> 'Tell charserver to remove friend_id from char_id friend list'
  61. //2b08: Outgoing, chrif_searchcharid -> '...'
  62. //2b09: Incoming, map_addchariddb -> 'Adds a name to the nick db'
  63. //2b0a: Outgoing, chrif_skillcooldown_request -> requesting the list of skillcooldown for char
  64. //2b0b: Incoming, chrif_skillcooldown_load -> received the list of cooldown for char
  65. //2b0c: Outgoing, chrif_changeemail -> 'change mail address ...'
  66. //2b0d: Incoming, chrif_changedsex -> 'Change sex of acc XY' (or char)
  67. //2b0e: Outgoing, chrif_req_login_operation -> 'Do some operations (change sex, ban / unban etc)'
  68. //2b0f: Incoming, chrif_ack_login_req -> 'answer of the 2b0e'
  69. //2b10: Outgoing, chrif_updatefamelist -> 'Update the fame ranking lists and send them'
  70. //2b11: Outgoing, chrif_divorce -> 'tell the charserver to do divorce'
  71. //2b12: Incoming, chrif_divorceack -> 'divorce chars
  72. //2b13: Outgoing, chrif_update_ip -> 'tell the change of map-server IP'
  73. //2b14: Incoming, chrif_accountban -> 'not sure: kick the player with message XY'
  74. //2b15: Outgoing, chrif_skillcooldown_save -> request to save skillcooldown
  75. //2b17: Outgoing, chrif_char_offline -> 'tell the charserver that the char is now offline'
  76. //2b18: Outgoing, chrif_char_reset_offline -> 'set all players OFF!'
  77. //2b19: Outgoing, chrif_char_online -> 'tell the charserver that the char .. is online'
  78. //2b1a: Outgoing, chrif_buildfamelist -> 'Build the fame ranking lists and send them'
  79. //2b1b: Incoming, chrif_recvfamelist -> 'Receive fame ranking lists'
  80. //2b1c: Outgoing, chrif_save_scdata -> 'Send sc_data of player for saving.'
  81. //2b1d: Incoming, chrif_load_scdata -> 'received sc_data of player for loading.'
  82. //2b1e: Incoming, chrif_update_ip -> 'Reqest forwarded from char-server for interserver IP sync.' [Lance]
  83. //2b1f: Incoming, chrif_disconnectplayer -> 'disconnects a player (aid X) with the message XY ... 0x81 ..' [Sirius]
  84. //2b20: Incoming, chrif_removemap -> 'remove maps of a server (sample: its going offline)' [Sirius]
  85. //2b21: Incoming, chrif_save_ack. Returned after a character has been "final saved" on the char-server. [Skotlex]
  86. //2b22: Incoming, chrif_updatefamelist_ack. Updated one position in the fame list.
  87. //2b23: Outgoing, chrif_keepalive. charserver ping.
  88. //2b24: Incoming, chrif_keepalive_ack. charserver ping reply.
  89. //2b25: Incoming, chrif_deadopt -> 'Removes baby from Father ID and Mother ID'
  90. //2b26: Outgoing, chrif_authreq -> 'client authentication request'
  91. //2b27: Incoming, chrif_authfail -> 'client authentication failed'
  92. //2b28: Outgoing, chrif_req_charban -> 'ban a specific char '
  93. //2b29: FREE
  94. //2b2a: Outgoing, chrif_req_charunban -> 'unban a specific char '
  95. //2b2b: Incoming, chrif_parse_ack_vipActive -> vip info result
  96. //2b2c: FREE
  97. //2b2d: Outgoing, chrif_bsdata_request -> request bonus_script for pc_authok'ed char.
  98. //2b2e: Outgoing, chrif_bsdata_save -> Send bonus_script of player for saving.
  99. //2b2f: Incoming, chrif_bsdata_received -> received bonus_script of player for loading.
  100. int chrif_connected = 0;
  101. int char_fd = -1;
  102. static char char_ip_str[128];
  103. static uint32 char_ip = 0;
  104. static uint16 char_port = 6121;
  105. static char userid[NAME_LENGTH], passwd[NAME_LENGTH];
  106. static int chrif_state = 0;
  107. int other_mapserver_count=0; //Holds count of how many other map servers are online (apart of this instance) [Skotlex]
  108. char charserver_name[NAME_LENGTH];
  109. //Interval at which map server updates online listing. [Valaris]
  110. #define CHECK_INTERVAL 3600000
  111. //Interval at which map server sends number of connected users. [Skotlex]
  112. #define UPDATE_INTERVAL 10000
  113. //This define should spare writing the check in every function. [Skotlex]
  114. #define chrif_check(a) { if(!chrif_isconnected()) return a; }
  115. /// Resets all the data.
  116. void chrif_reset(void) {
  117. // TODO kick everyone out and reset everything [FlavioJS]
  118. exit(EXIT_FAILURE);
  119. }
  120. /// Checks the conditions for the server to stop.
  121. /// Releases the cookie when all characters are saved.
  122. /// If all the conditions are met, it stops the core loop.
  123. void chrif_check_shutdown(void) {
  124. if( runflag != MAPSERVER_ST_SHUTDOWN )
  125. return;
  126. if( auth_db->size(auth_db) > 0 )
  127. return;
  128. runflag = CORE_ST_STOP;
  129. }
  130. struct auth_node* chrif_search(uint32 account_id) {
  131. return (struct auth_node*)idb_get(auth_db, account_id);
  132. }
  133. struct auth_node* chrif_auth_check(uint32 account_id, uint32 char_id, enum sd_state state) {
  134. struct auth_node *node = chrif_search(account_id);
  135. return ( node && node->char_id == char_id && node->state == state ) ? node : NULL;
  136. }
  137. bool chrif_auth_delete(uint32 account_id, uint32 char_id, enum sd_state state) {
  138. struct auth_node *node;
  139. if ( (node = chrif_auth_check(account_id, char_id, state) ) ) {
  140. int fd = node->sd ? node->sd->fd : node->fd;
  141. if ( session[fd] && session[fd]->session_data == node->sd )
  142. session[fd]->session_data = NULL;
  143. if ( node->char_dat )
  144. aFree(node->char_dat);
  145. if ( node->sd ) {
  146. if (node->sd->regs.vars)
  147. node->sd->regs.vars->destroy(node->sd->regs.vars, script_reg_destroy);
  148. if (node->sd->regs.arrays)
  149. node->sd->regs.arrays->destroy(node->sd->regs.arrays, script_free_array_db);
  150. aFree(node->sd);
  151. }
  152. ers_free(auth_db_ers, node);
  153. idb_remove(auth_db,account_id);
  154. return true;
  155. }
  156. return false;
  157. }
  158. //Moves the sd character to the auth_db structure.
  159. static bool chrif_sd_to_auth(TBL_PC* sd, enum sd_state state) {
  160. struct auth_node *node;
  161. if ( chrif_search(sd->status.account_id) )
  162. return false; //Already exists?
  163. node = ers_alloc(auth_db_ers, struct auth_node);
  164. memset(node, 0, sizeof(struct auth_node));
  165. node->account_id = sd->status.account_id;
  166. node->char_id = sd->status.char_id;
  167. node->login_id1 = sd->login_id1;
  168. node->login_id2 = sd->login_id2;
  169. node->sex = sd->status.sex;
  170. node->fd = sd->fd;
  171. node->sd = sd; //Data from logged on char.
  172. node->node_created = gettick(); //timestamp for node timeouts
  173. node->state = state;
  174. sd->state.active = 0;
  175. idb_put(auth_db, node->account_id, node);
  176. return true;
  177. }
  178. static bool chrif_auth_logout(TBL_PC* sd, enum sd_state state) {
  179. if(sd->fd && state == ST_LOGOUT) { //Disassociate player, and free it after saving ack returns. [Skotlex]
  180. //fd info must not be lost for ST_MAPCHANGE as a final packet needs to be sent to the player.
  181. if ( session[sd->fd] )
  182. session[sd->fd]->session_data = NULL;
  183. sd->fd = 0;
  184. }
  185. return chrif_sd_to_auth(sd, state);
  186. }
  187. bool chrif_auth_finished(struct map_session_data* sd) {
  188. struct auth_node *node= chrif_search(sd->status.account_id);
  189. if ( node && node->sd == sd && node->state == ST_LOGIN ) {
  190. node->sd = NULL;
  191. return chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
  192. }
  193. return false;
  194. }
  195. // sets char-server's user id
  196. void chrif_setuserid(char *id) {
  197. safestrncpy(userid, id, NAME_LENGTH);
  198. }
  199. // sets char-server's password
  200. void chrif_setpasswd(char *pwd) {
  201. safestrncpy(passwd, pwd, NAME_LENGTH);
  202. }
  203. // security check, prints warning if using default password
  204. void chrif_checkdefaultlogin(void) {
  205. // Skip this check if the server is run with run-once flag
  206. if ( runflag != CORE_ST_STOP && strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  207. ShowWarning("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  208. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  209. ShowNotice("and then edit your user/password in conf/map_athena.conf (or conf/import/map_conf.txt)\n");
  210. }
  211. }
  212. // sets char-server's ip address
  213. int chrif_setip(const char* ip) {
  214. char ip_str[16];
  215. if ( !( char_ip = host2ip(ip) ) ) {
  216. ShowWarning("Failed to Resolve Char Server Address! (%s)\n", ip);
  217. return 0;
  218. }
  219. safestrncpy(char_ip_str, ip, sizeof(char_ip_str));
  220. ShowInfo("Char Server IP Address : '" CL_WHITE "%s" CL_RESET "' -> '" CL_WHITE "%s" CL_RESET "'.\n", ip, ip2str(char_ip, ip_str));
  221. return 1;
  222. }
  223. // sets char-server's port number
  224. void chrif_setport(uint16 port) {
  225. char_port = port;
  226. }
  227. // says whether the char-server is connected or not
  228. int chrif_isconnected(void) {
  229. return (session_isValid(char_fd) && chrif_state == 2);
  230. }
  231. /**
  232. * Saves character data.
  233. * @param sd: Player data
  234. * @param flag: Save flag types:
  235. * CSAVE_NORMAL: Normal save
  236. * CSAVE_QUIT: Character is quitting
  237. * CSAVE_CHANGE_MAPSERV: Character is changing map-servers
  238. * CSAVE_AUTOTRADE: Character used @autotrade
  239. * CSAVE_INVENTORY: Character changed inventory data
  240. * CSAVE_CART: Character changed cart data
  241. */
  242. int chrif_save(struct map_session_data *sd, int flag) {
  243. uint16 mmo_charstatus_len = 0;
  244. nullpo_retr(-1, sd);
  245. pc_makesavestatus(sd);
  246. if ( (flag&CSAVE_QUITTING) && sd->state.active) { //Store player data which is quitting
  247. if (chrif_isconnected()) {
  248. chrif_save_scdata(sd);
  249. chrif_skillcooldown_save(sd);
  250. }
  251. if ( !(flag&CSAVE_AUTOTRADE) && !chrif_auth_logout(sd, (flag&CSAVE_QUIT) ? ST_LOGOUT : ST_MAPCHANGE) )
  252. ShowError("chrif_save: Failed to set up player %d:%d for proper quitting!\n", sd->status.account_id, sd->status.char_id);
  253. }
  254. chrif_check(-1); //Character is saved on reconnect.
  255. chrif_bsdata_save(sd, ((flag&CSAVE_QUITTING) && !(flag&CSAVE_AUTOTRADE)));
  256. if (sd->storage.dirty)
  257. storage_storagesave(sd);
  258. if (flag&CSAVE_INVENTORY)
  259. intif_storage_save(sd,&sd->inventory);
  260. if (flag&CSAVE_CART)
  261. intif_storage_save(sd,&sd->cart);
  262. //For data sync
  263. if (sd->state.storage_flag == 2)
  264. storage_guild_storagesave(sd->status.account_id, sd->status.guild_id, flag);
  265. if (sd->premiumStorage.dirty)
  266. storage_premiumStorage_save(sd);
  267. if( flag&CSAVE_QUITTING ){
  268. sd->state.storage_flag = 0; //Force close it.
  269. if( sd->goldpc_tid != INVALID_TIMER ){
  270. const struct TimerData* td = get_timer( sd->goldpc_tid );
  271. if( td != nullptr ){
  272. // Get the remaining milliseconds until the next reward
  273. t_tick remaining = td->tick - gettick();
  274. // Always round up to full second and a little safety delay
  275. remaining += ( remaining % 1000 ) + 2000;
  276. // Store the seconds that already fully passed
  277. pc_setreg2( sd, GOLDPC_SECONDS_VAR, battle_config.feature_goldpc_time - remaining / 1000 );
  278. // If a player logs out or starts autotrade, stop counting
  279. delete_timer( sd->goldpc_tid, pc_goldpc_update );
  280. sd->goldpc_tid = INVALID_TIMER;
  281. }
  282. }
  283. }
  284. //Saving of registry values.
  285. if (sd->vars_dirty)
  286. intif_saveregistry(sd);
  287. mmo_charstatus_len = sizeof(sd->status) + 13;
  288. WFIFOHEAD(char_fd, mmo_charstatus_len);
  289. WFIFOW(char_fd,0) = 0x2b01;
  290. WFIFOW(char_fd,2) = mmo_charstatus_len;
  291. WFIFOL(char_fd,4) = sd->status.account_id;
  292. WFIFOL(char_fd,8) = sd->status.char_id;
  293. WFIFOB(char_fd,12) = (flag&CSAVE_QUIT) ? 1 : 0; //Flag to tell char-server this character is quitting.
  294. // If the user is on a instance map, we have to fake his current position
  295. if( map_getmapdata(sd->bl.m)->instance_id ){
  296. struct mmo_charstatus status;
  297. // Copy the whole status
  298. memcpy( &status, &sd->status, sizeof( struct mmo_charstatus ) );
  299. // Change his current position to his savepoint
  300. memcpy( &status.last_point, &status.save_point, sizeof( struct point ) );
  301. // Copy the copied status into the packet
  302. memcpy( WFIFOP( char_fd, 13 ), &status, sizeof( struct mmo_charstatus ) );
  303. } else {
  304. // Copy the whole status into the packet
  305. memcpy( WFIFOP( char_fd, 13 ), &sd->status, sizeof( struct mmo_charstatus ) );
  306. }
  307. WFIFOSET(char_fd, WFIFOW(char_fd,2));
  308. if( sd->status.pet_id > 0 && sd->pd )
  309. intif_save_petdata(sd->status.account_id,&sd->pd->pet);
  310. if( hom_is_active(sd->hd) )
  311. hom_save(sd->hd);
  312. if( sd->md && mercenary_get_lifetime(sd->md) > 0 )
  313. mercenary_save(sd->md);
  314. if( sd->ed && elemental_get_lifetime(sd->ed) > 0 )
  315. elemental_save(sd->ed);
  316. if( sd->save_quest )
  317. intif_quest_save(sd);
  318. if (sd->achievement_data.save)
  319. intif_achievement_save(sd);
  320. return 0;
  321. }
  322. // connects to char-server (plaintext)
  323. /**
  324. * Map-serv request to login into char-serv
  325. * @param fd : char-serv fd to log into
  326. * @return 0:request sent
  327. */
  328. int chrif_connect(int fd) {
  329. ShowStatus("Logging in to char server...\n", char_fd);
  330. WFIFOHEAD(fd,60);
  331. WFIFOW(fd,0) = 0x2af8;
  332. memcpy(WFIFOP(fd,2), userid, NAME_LENGTH);
  333. memcpy(WFIFOP(fd,26), passwd, NAME_LENGTH);
  334. WFIFOL(fd,50) = 0;
  335. WFIFOL(fd,54) = htonl(clif_getip());
  336. WFIFOW(fd,58) = htons(clif_getport());
  337. WFIFOSET(fd,60);
  338. return 0;
  339. }
  340. // sends maps to char-server
  341. int chrif_sendmap(int fd) {
  342. ShowStatus("Sending maps to char server...\n");
  343. // Sending normal maps, not instances
  344. WFIFOHEAD(fd, 4 + instance_start * 4);
  345. WFIFOW(fd,0) = 0x2afa;
  346. for (int i = 0; i < instance_start; i++)
  347. WFIFOW(fd, 4 + i * 4) = map[i].index;
  348. WFIFOW(fd, 2) = 4 + instance_start * 4;
  349. WFIFOSET(fd, WFIFOW(fd, 2));
  350. return 0;
  351. }
  352. // receive maps from some other map-server (relayed via char-server)
  353. int chrif_recvmap(int fd) {
  354. int i, j;
  355. uint32 ip = ntohl(RFIFOL(fd,4));
  356. uint16 port = ntohs(RFIFOW(fd,8));
  357. for(i = 10, j = 0; i < RFIFOW(fd,2); i += 4, j++) {
  358. map_setipport(RFIFOW(fd,i), ip, port);
  359. }
  360. if (battle_config.etc_log)
  361. ShowStatus("Received maps from %d.%d.%d.%d:%d (%d maps)\n", CONVIP(ip), port, j);
  362. other_mapserver_count++;
  363. return 0;
  364. }
  365. // remove specified maps (used when some other map-server disconnects)
  366. int chrif_removemap(int fd) {
  367. int i, j;
  368. uint32 ip = RFIFOL(fd,4);
  369. uint16 port = RFIFOW(fd,8);
  370. for(i = 10, j = 0; i < RFIFOW(fd, 2); i += 4, j++)
  371. map_eraseipport(RFIFOW(fd, i), ip, port);
  372. other_mapserver_count--;
  373. if(battle_config.etc_log)
  374. ShowStatus("remove map of server %d.%d.%d.%d:%d (%d maps)\n", CONVIP(ip), port, j);
  375. return 0;
  376. }
  377. // received after a character has been "final saved" on the char-server
  378. static void chrif_save_ack(int fd) {
  379. chrif_auth_delete(RFIFOL(fd,2), RFIFOL(fd,6), ST_LOGOUT);
  380. chrif_check_shutdown();
  381. }
  382. // request to move a character between mapservers
  383. int chrif_changemapserver(struct map_session_data* sd, uint32 ip, uint16 port) {
  384. nullpo_retr(-1, sd);
  385. if (other_mapserver_count < 1) {//No other map servers are online!
  386. clif_authfail_fd(sd->fd, 0);
  387. return -1;
  388. }
  389. chrif_check(-1);
  390. WFIFOHEAD(char_fd,39);
  391. WFIFOW(char_fd, 0) = 0x2b05;
  392. WFIFOL(char_fd, 2) = sd->bl.id;
  393. WFIFOL(char_fd, 6) = sd->login_id1;
  394. WFIFOL(char_fd,10) = sd->login_id2;
  395. WFIFOL(char_fd,14) = sd->status.char_id;
  396. WFIFOW(char_fd,18) = sd->mapindex;
  397. WFIFOW(char_fd,20) = sd->bl.x;
  398. WFIFOW(char_fd,22) = sd->bl.y;
  399. WFIFOL(char_fd,24) = htonl(ip);
  400. WFIFOW(char_fd,28) = htons(port);
  401. WFIFOB(char_fd,30) = sd->status.sex;
  402. WFIFOL(char_fd,31) = htonl(session[sd->fd]->client_addr);
  403. WFIFOL(char_fd,35) = sd->group_id;
  404. WFIFOSET(char_fd,39);
  405. return 0;
  406. }
  407. /// map-server change (mapserv) request acknowledgement (positive or negative)
  408. /// R 2b06 <account_id>.L <login_id1>.L <login_id2>.L <char_id>.L <map_index>.W <x>.W <y>.W <ip>.L <port>.W
  409. int chrif_changemapserverack(uint32 account_id, int login_id1, int login_id2, uint32 char_id, short map_index, short x, short y, uint32 ip, uint16 port) {
  410. struct auth_node *node;
  411. if ( !( node = chrif_auth_check(account_id, char_id, ST_MAPCHANGE) ) )
  412. return -1;
  413. if ( !login_id1 ) {
  414. ShowError("map server change failed.\n");
  415. clif_authfail_fd(node->fd, 0);
  416. chrif_char_offline(node->sd);
  417. } else
  418. clif_changemapserver(node->sd, map_index, x, y, ntohl(ip), ntohs(port));
  419. //Player has been saved already, remove him from memory. [Skotlex]
  420. chrif_auth_delete(account_id, char_id, ST_MAPCHANGE);
  421. return 0;
  422. }
  423. /**
  424. * Does the char_serv have validate our connection to him ?
  425. * If yes then
  426. * - Send all our mapname to charserv
  427. * - Retrieve guild castle
  428. * - Do OnInterIfInit and OnInterIfInitOnce on all npc
  429. * 0x2af9 <errCode>B
  430. */
  431. int chrif_connectack(int fd) {
  432. if (RFIFOB(fd,2)) {
  433. ShowFatalError("Connection to char-server failed %d, please check conf/import/map_conf userid and passwd.\n", RFIFOB(fd,2));
  434. exit(EXIT_FAILURE);
  435. }
  436. ShowStatus("Successfully logged on to Char Server (Connection: '" CL_WHITE "%d" CL_RESET "').\n",fd);
  437. chrif_state = 1;
  438. chrif_connected = 1;
  439. chrif_sendmap(fd);
  440. npc_event_runall(script_config.inter_init_event_name);
  441. if( !char_init_done ) {
  442. npc_event_runall(script_config.inter_init_once_event_name);
  443. guild_castle_map_init();
  444. intif_clan_requestclans();
  445. }
  446. return 0;
  447. }
  448. /**
  449. * @see DBApply
  450. */
  451. static int chrif_reconnect(DBKey key, DBData *data, va_list ap) {
  452. struct auth_node *node = (struct auth_node *)db_data2ptr(data);
  453. switch (node->state) {
  454. case ST_LOGIN:
  455. if ( node->sd && node->char_dat == NULL ) {//Since there is no way to request the char auth, make it fail.
  456. pc_authfail(node->sd);
  457. chrif_char_offline(node->sd);
  458. chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
  459. }
  460. break;
  461. case ST_LOGOUT:
  462. //Re-send final save
  463. chrif_save(node->sd, CSAVE_QUIT|CSAVE_INVENTORY|CSAVE_CART);
  464. break;
  465. case ST_MAPCHANGE: { //Re-send map-change request.
  466. struct map_session_data *sd = node->sd;
  467. uint32 ip;
  468. uint16 port;
  469. if( map_mapname2ipport(sd->mapindex,&ip,&port) == 0 )
  470. chrif_changemapserver(sd, ip, port);
  471. else //too much lag/timeout is the closest explanation for this error.
  472. clif_authfail_fd(sd->fd, 3);
  473. break;
  474. }
  475. }
  476. return 0;
  477. }
  478. /// Called when all the connection steps are completed.
  479. void chrif_on_ready(void) {
  480. ShowStatus("Map Server is now online.\n");
  481. chrif_state = 2;
  482. chrif_check_shutdown();
  483. //If there are players online, send them to the char-server. [Skotlex]
  484. send_users_tochar();
  485. //Auth db reconnect handling
  486. auth_db->foreach(auth_db,chrif_reconnect);
  487. //Re-save any storages that were modified in the disconnection time. [Skotlex]
  488. do_reconnect_storage();
  489. //Re-save any guild castles that were modified in the disconnection time.
  490. guild_castle_reconnect(-1, CD_NONE, 0);
  491. // Charserver is ready for loading autotrader
  492. if (!char_init_done)
  493. {
  494. do_init_buyingstore_autotrade();
  495. do_init_vending_autotrade();
  496. char_init_done = true;
  497. }
  498. }
  499. /**
  500. * Maps are sent, then received misc info from char-server
  501. * - Server name
  502. * - Default map
  503. * HZ 0x2afb
  504. **/
  505. int chrif_sendmapack(int fd) {
  506. uint16 offs = 5;
  507. if (RFIFOB(fd,4)) {
  508. ShowFatalError("chrif : send map list to char server failed %d\n", RFIFOB(fd,2));
  509. exit(EXIT_FAILURE);
  510. }
  511. // Whisper name
  512. safestrncpy( wisp_server_name, RFIFOCP( fd, offs ), NAME_LENGTH );
  513. // Default map
  514. safestrncpy( map_default.mapname, RFIFOCP( fd, ( offs += NAME_LENGTH ) ), MAP_NAME_LENGTH );
  515. map_default.x = RFIFOW(fd, (offs+=MAP_NAME_LENGTH));
  516. map_default.y = RFIFOW(fd, (offs+=2));
  517. // Server name
  518. safestrncpy( charserver_name, RFIFOCP( fd, ( offs += 2 ) ), NAME_LENGTH );
  519. ShowStatus( "Map-server connected to char-server '" CL_WHITE "%s" CL_RESET "' (whispername: %s).\n", charserver_name, wisp_server_name );
  520. if (battle_config.etc_log)
  521. ShowInfo("Received default map from char-server '" CL_WHITE "%s %d,%d" CL_RESET "'.\n", map_default.mapname, map_default.x, map_default.y);
  522. chrif_on_ready();
  523. return 0;
  524. }
  525. /*==========================================
  526. * Request sc_data from charserver [Skotlex]
  527. *------------------------------------------*/
  528. int chrif_scdata_request(uint32 account_id, uint32 char_id) {
  529. #ifdef ENABLE_SC_SAVING
  530. chrif_check(-1);
  531. WFIFOHEAD(char_fd,10);
  532. WFIFOW(char_fd,0) = 0x2afc;
  533. WFIFOL(char_fd,2) = account_id;
  534. WFIFOL(char_fd,6) = char_id;
  535. WFIFOSET(char_fd,10);
  536. #endif
  537. return 0;
  538. }
  539. /*==========================================
  540. * Request skillcooldown from charserver
  541. *------------------------------------------*/
  542. int chrif_skillcooldown_request(uint32 account_id, uint32 char_id) {
  543. chrif_check(-1);
  544. WFIFOHEAD(char_fd, 10);
  545. WFIFOW(char_fd, 0) = 0x2b0a;
  546. WFIFOL(char_fd, 2) = account_id;
  547. WFIFOL(char_fd, 6) = char_id;
  548. WFIFOSET(char_fd, 10);
  549. return 0;
  550. }
  551. /*==========================================
  552. * Request auth confirmation
  553. *------------------------------------------*/
  554. void chrif_authreq(struct map_session_data *sd, bool autotrade) {
  555. struct auth_node *node= chrif_search(sd->bl.id);
  556. if( node != NULL || !chrif_isconnected() ) {
  557. set_eof(sd->fd);
  558. return;
  559. }
  560. WFIFOHEAD(char_fd,20);
  561. WFIFOW(char_fd,0) = 0x2b26;
  562. WFIFOL(char_fd,2) = sd->status.account_id;
  563. WFIFOL(char_fd,6) = sd->status.char_id;
  564. WFIFOL(char_fd,10) = sd->login_id1;
  565. WFIFOB(char_fd,14) = sd->status.sex;
  566. WFIFOL(char_fd,15) = htonl(session[sd->fd]->client_addr);
  567. WFIFOB(char_fd,19) = autotrade;
  568. WFIFOSET(char_fd,20);
  569. chrif_sd_to_auth(sd, ST_LOGIN);
  570. }
  571. /*==========================================
  572. * Auth confirmation ack
  573. *------------------------------------------*/
  574. void chrif_authok(int fd) {
  575. uint32 account_id, group_id, char_id;
  576. uint32 login_id1,login_id2;
  577. time_t expiration_time;
  578. struct mmo_charstatus* status;
  579. struct auth_node *node;
  580. bool changing_mapservers;
  581. TBL_PC* sd;
  582. //Check if both servers agree on the struct's size
  583. if( RFIFOW(fd,2) - 25 != sizeof(struct mmo_charstatus) ) {
  584. ShowError("chrif_authok: Data size mismatch! %d != %" PRIuPTR "\n", RFIFOW(fd,2) - 25, sizeof(struct mmo_charstatus));
  585. return;
  586. }
  587. account_id = RFIFOL(fd,4);
  588. login_id1 = RFIFOL(fd,8);
  589. login_id2 = RFIFOL(fd,12);
  590. expiration_time = (time_t)(int32)RFIFOL(fd,16);
  591. group_id = RFIFOL(fd,20);
  592. changing_mapservers = (RFIFOB(fd,24)) > 0;
  593. status = (struct mmo_charstatus*)RFIFOP(fd,25);
  594. char_id = status->char_id;
  595. //Check if we don't already have player data in our server
  596. //Causes problems if the currently connected player tries to quit or this data belongs to an already connected player which is trying to re-auth.
  597. if ( ( sd = map_id2sd(account_id) ) != NULL )
  598. return;
  599. if ( ( node = chrif_search(account_id) ) == NULL )
  600. return; // should not happen
  601. if ( node->state != ST_LOGIN )
  602. return; //character in logout phase, do not touch that data.
  603. if ( node->sd == NULL ) {
  604. /*
  605. //When we receive double login info and the client has not connected yet,
  606. //discard the older one and keep the new one.
  607. chrif_auth_delete(node->account_id, node->char_id, ST_LOGIN);
  608. */
  609. return; // should not happen
  610. }
  611. sd = node->sd;
  612. if( runflag == MAPSERVER_ST_RUNNING &&
  613. node->char_dat == NULL &&
  614. node->account_id == account_id &&
  615. node->char_id == char_id &&
  616. node->login_id1 == login_id1 )
  617. { //Auth Ok
  618. if (pc_authok(sd, login_id2, expiration_time, group_id, status, changing_mapservers))
  619. return;
  620. } else { //Auth Failed
  621. pc_authfail(sd);
  622. }
  623. chrif_char_offline(sd); //Set him offline, the char server likely has it set as online already.
  624. chrif_auth_delete(account_id, char_id, ST_LOGIN);
  625. }
  626. // client authentication failed
  627. void chrif_authfail(int fd) {/* HELLO WORLD. ip in RFIFOL 15 is not being used (but is available) */
  628. uint32 account_id, char_id;
  629. uint32 login_id1;
  630. char sex;
  631. struct auth_node* node;
  632. account_id = RFIFOL(fd,2);
  633. char_id = RFIFOL(fd,6);
  634. login_id1 = RFIFOL(fd,10);
  635. sex = RFIFOB(fd,14);
  636. node = chrif_search(account_id);
  637. if( node != NULL &&
  638. node->account_id == account_id &&
  639. node->char_id == char_id &&
  640. node->login_id1 == login_id1 &&
  641. node->sex == sex &&
  642. node->state == ST_LOGIN )
  643. {// found a match
  644. clif_authfail_fd(node->fd, 0);
  645. chrif_auth_delete(account_id, char_id, ST_LOGIN);
  646. }
  647. }
  648. /**
  649. * This can still happen (client times out while waiting for char to confirm auth data)
  650. * @see DBApply
  651. */
  652. int auth_db_cleanup_sub(DBKey key, DBData *data, va_list ap) {
  653. struct auth_node *node = (struct auth_node *)db_data2ptr(data);
  654. if(DIFF_TICK(gettick(),node->node_created)>60000) {
  655. const char* states[] = { "Login", "Logout", "Map change" };
  656. switch (node->state) {
  657. case ST_LOGOUT:
  658. //Re-save attempt (->sd should never be null here).
  659. node->node_created = gettick(); //Refresh tick (avoid char-server load if connection is really bad)
  660. chrif_save(node->sd, CSAVE_QUIT|CSAVE_INVENTORY|CSAVE_CART);
  661. break;
  662. default:
  663. //Clear data. any connected players should have timed out by now.
  664. ShowInfo("auth_db: Node (state %s) timed out for %d:%d\n", states[node->state], node->account_id, node->char_id);
  665. chrif_char_offline_nsd(node->account_id, node->char_id);
  666. chrif_auth_delete(node->account_id, node->char_id, node->state);
  667. break;
  668. }
  669. return 1;
  670. }
  671. return 0;
  672. }
  673. TIMER_FUNC(auth_db_cleanup){
  674. chrif_check(0);
  675. auth_db->foreach(auth_db, auth_db_cleanup_sub);
  676. return 0;
  677. }
  678. /*==========================================
  679. *
  680. *------------------------------------------*/
  681. int chrif_charselectreq(struct map_session_data* sd, uint32 s_ip) {
  682. nullpo_retr(-1, sd);
  683. if( !sd || !sd->bl.id || !sd->login_id1 )
  684. return -1;
  685. chrif_check(-1);
  686. WFIFOHEAD(char_fd,18);
  687. WFIFOW(char_fd, 0) = 0x2b02;
  688. WFIFOL(char_fd, 2) = sd->bl.id;
  689. WFIFOL(char_fd, 6) = sd->login_id1;
  690. WFIFOL(char_fd,10) = sd->login_id2;
  691. WFIFOL(char_fd,14) = htonl(s_ip);
  692. WFIFOSET(char_fd,18);
  693. return 0;
  694. }
  695. /*==========================================
  696. * Search Char trough id on char serv
  697. *------------------------------------------*/
  698. int chrif_searchcharid(uint32 char_id) {
  699. if( !char_id )
  700. return -1;
  701. chrif_check(-1);
  702. WFIFOHEAD(char_fd,6);
  703. WFIFOW(char_fd,0) = 0x2b08;
  704. WFIFOL(char_fd,2) = char_id;
  705. WFIFOSET(char_fd,6);
  706. return 0;
  707. }
  708. /*==========================================
  709. * Change Email
  710. *------------------------------------------*/
  711. int chrif_changeemail(int id, const char *actual_email, const char *new_email) {
  712. if (battle_config.etc_log)
  713. ShowInfo("chrif_changeemail: account: %d, actual_email: '%s', new_email: '%s'.\n", id, actual_email, new_email);
  714. chrif_check(-1);
  715. WFIFOHEAD(char_fd,86);
  716. WFIFOW(char_fd,0) = 0x2b0c;
  717. WFIFOL(char_fd,2) = id;
  718. memcpy(WFIFOP(char_fd,6), actual_email, 40);
  719. memcpy(WFIFOP(char_fd,46), new_email, 40);
  720. WFIFOSET(char_fd,86);
  721. return 0;
  722. }
  723. /**
  724. * S 2b0e <accid>.l <name>.24B <operation_type>.w <timediff>L <val1>L <val2>L
  725. * Send an account modification request to the login server (via char server).
  726. * @aid : Player requesting operation account id
  727. * @character_name : Target of operation Player name
  728. * @operation_type : see chrif_req_op
  729. * @timediff : tick to add or remove to unixtimestamp
  730. * @val1 : extra data value to transfer for operation
  731. * CHRIF_OP_LOGIN_VIP: 0x1 : Select info and update old_groupid
  732. * 0x2 : VIP duration is changed by atcommand or script
  733. * 0x4 : Show status reply by char-server through 0x2b0f
  734. * 0x8 : First request on player login
  735. * @val2 : extra data value to transfer for operation
  736. */
  737. int chrif_req_login_operation(int aid, const char* character_name, enum chrif_req_op operation_type, int32 timediff, int val1, int val2) {
  738. chrif_check(-1);
  739. WFIFOHEAD(char_fd,44);
  740. WFIFOW(char_fd,0) = 0x2b0e;
  741. WFIFOL(char_fd,2) = aid;
  742. safestrncpy(WFIFOCP(char_fd,6), character_name, NAME_LENGTH);
  743. WFIFOW(char_fd,30) = operation_type;
  744. if ( operation_type == CHRIF_OP_LOGIN_BAN || operation_type == CHRIF_OP_LOGIN_VIP)
  745. WFIFOL(char_fd,32) = timediff;
  746. WFIFOL(char_fd,36) = val1;
  747. WFIFOL(char_fd,40) = val2;
  748. WFIFOSET(char_fd,44);
  749. return 0;
  750. }
  751. /**
  752. * S 2b0e <accid>.l <name>.24B <operation_type>.w <timediff>L <val1>L <val2>L
  753. * Send a sex change (for account or character) request to the login server (via char server).
  754. * @sd : Player requesting operation
  755. */
  756. int chrif_changesex(struct map_session_data *sd, bool change_account) {
  757. chrif_check(-1);
  758. WFIFOHEAD(char_fd,44);
  759. WFIFOW(char_fd,0) = 0x2b0e;
  760. WFIFOL(char_fd,2) = sd->status.account_id;
  761. safestrncpy(WFIFOCP(char_fd,6), sd->status.name, NAME_LENGTH);
  762. WFIFOW(char_fd,30) = (change_account ? CHRIF_OP_LOGIN_CHANGESEX : CHRIF_OP_CHANGECHARSEX);
  763. if (!change_account)
  764. WFIFOB(char_fd,32) = sd->status.sex == SEX_MALE ? SEX_FEMALE : SEX_MALE;
  765. WFIFOSET(char_fd,44);
  766. clif_displaymessage(sd->fd, msg_txt(sd,408)); //"Need disconnection to perform change-sex request..."
  767. if (sd->fd)
  768. clif_authfail_fd(sd->fd, 15);
  769. else
  770. map_quit(sd);
  771. return 0;
  772. }
  773. /**
  774. * R 2b0f <accid>.l <name>.24B <type>.w <answer>.w
  775. * Processing a reply to chrif_req_login_operation() (request to modify an account).
  776. * NB: That ack is received just after the char has sent the request to login and therefore didn't have login reply yet
  777. * @param aid : player account id the request was concerning
  778. * @param player_name : name the request was concerning
  779. * @param type : code of operation done. See enum chrif_req_op
  780. * @param answer : type of answer
  781. * 0: login-server request done
  782. * 1: player not found
  783. * 2: gm level too low
  784. * 3: login-server offline
  785. */
  786. static void chrif_ack_login_req(int aid, const char* player_name, uint16 type, uint16 answer) {
  787. struct map_session_data* sd;
  788. char action[25];
  789. char output[256];
  790. sd = map_id2sd(aid);
  791. if( aid < 0 || sd == NULL ) {
  792. ShowError("chrif_ack_login_req failed - player not online.\n");
  793. return;
  794. }
  795. switch (type) {
  796. case CHRIF_OP_LOGIN_CHANGESEX:
  797. case CHRIF_OP_CHANGECHARSEX:
  798. type = CHRIF_OP_LOGIN_CHANGESEX; // So we don't have to create a new msgstring.
  799. case CHRIF_OP_LOGIN_BLOCK:
  800. case CHRIF_OP_LOGIN_BAN:
  801. case CHRIF_OP_LOGIN_UNBLOCK:
  802. case CHRIF_OP_LOGIN_UNBAN:
  803. snprintf(action,25,"%s",msg_txt(sd,427+type)); //block|ban|unblock|unban|change the sex of
  804. break;
  805. case CHRIF_OP_LOGIN_VIP:
  806. if (!battle_config.disp_servervip_msg)
  807. return;
  808. snprintf(action,25,"%s",msg_txt(sd,436)); //VIP
  809. break;
  810. default:
  811. snprintf(action,25,"???");
  812. break;
  813. }
  814. switch (answer) {
  815. case 0: sprintf(output, msg_txt(sd,424), action, NAME_LENGTH, player_name); break; //Login-serv has been asked to %s '%.*s'.
  816. case 1: sprintf(output, msg_txt(sd,425), NAME_LENGTH, player_name); break;
  817. case 2: sprintf(output, msg_txt(sd,426), action, NAME_LENGTH, player_name); break;
  818. case 3: sprintf(output, msg_txt(sd,427), action, NAME_LENGTH, player_name); break;
  819. case 4: sprintf(output, msg_txt(sd,424), action, NAME_LENGTH, player_name); break;
  820. default: output[0] = '\0'; break;
  821. }
  822. clif_displaymessage(sd->fd, output);
  823. }
  824. /*==========================================
  825. * Request char server to change sex of char (modified by Yor)
  826. *------------------------------------------*/
  827. int chrif_changedsex(int fd) {
  828. int acc, sex;
  829. struct map_session_data *sd;
  830. acc = RFIFOL(fd,2);
  831. sex = RFIFOL(fd,6);
  832. if ( battle_config.etc_log )
  833. ShowNotice("chrif_changedsex %d.\n", acc);
  834. sd = map_id2sd(acc);
  835. if ( sd ) { //Normally there should not be a char logged on right now!
  836. if ( sd->status.sex == sex )
  837. return 0; //Do nothing? Likely safe.
  838. sd->status.sex = !sd->status.sex;
  839. // Reset skills of gender split jobs.
  840. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) {
  841. const static struct {
  842. e_skill start;
  843. e_skill end;
  844. } ranges[] = {
  845. // Bard class exclusive skills
  846. { BA_MUSICALLESSON, BA_APPLEIDUN },
  847. // Dancer class exclusive skills
  848. { DC_DANCINGLESSON, DC_SERVICEFORYOU },
  849. // Minstrel class exclusive skills
  850. { MI_RUSH_WINDMILL, MI_HARMONIZE },
  851. // Wanderer class exclusive skills
  852. { WA_SWING_DANCE, WA_MOONLIT_SERENADE },
  853. // Kagerou class exclusive skills
  854. { KG_KAGEHUMI, KG_KAGEMUSYA },
  855. // Oboro class exclusive skills
  856. { OB_ZANGETSU, OB_AKAITSUKI },
  857. };
  858. for( const auto& range : ranges ){
  859. for( uint16 skill_id = range.start; skill_id <= range.end; skill_id++ ){
  860. uint16 sk_idx = skill_get_index( skill_id );
  861. if( sd->status.skill[sk_idx].id > 0 && sd->status.skill[sk_idx].flag == SKILL_FLAG_PERMANENT ){
  862. sd->status.skill_point += sd->status.skill[sk_idx].lv;
  863. sd->status.skill[sk_idx].id = 0;
  864. sd->status.skill[sk_idx].lv = 0;
  865. }
  866. }
  867. }
  868. clif_updatestatus(sd, SP_SKILLPOINT);
  869. // Change to other gender version of the job if needed.
  870. if (sd->status.sex)// Changed from female version of job.
  871. sd->status.class_ -= 1;
  872. else// Changed from male version of job.
  873. sd->status.class_ += 1;
  874. //sd->class_ Does not need to be updated as both versions of the job are the same.
  875. }
  876. // save character
  877. sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
  878. // do same modify in login-server for the account, but no in char-server (it ask again login_id1 to login, and don't remember it)
  879. clif_displaymessage(sd->fd, msg_txt(sd,409)); //"Your sex has been changed (need disconnection by the server)..."
  880. set_eof(sd->fd); // forced to disconnect for the change
  881. map_quit(sd); // Remove leftovers (e.g. autotrading) [Paradox924X]
  882. }
  883. return 0;
  884. }
  885. /*==========================================
  886. * Request Char Server to Divorce Players
  887. *------------------------------------------*/
  888. int chrif_divorce(int partner_id1, int partner_id2) {
  889. chrif_check(-1);
  890. WFIFOHEAD(char_fd,10);
  891. WFIFOW(char_fd,0) = 0x2b11;
  892. WFIFOL(char_fd,2) = partner_id1;
  893. WFIFOL(char_fd,6) = partner_id2;
  894. WFIFOSET(char_fd,10);
  895. return 0;
  896. }
  897. /*==========================================
  898. * Divorce players
  899. * only used if 'partner_id' is offline
  900. *------------------------------------------*/
  901. int chrif_divorceack(uint32 char_id, int partner_id) {
  902. struct map_session_data* sd;
  903. int i;
  904. if( !char_id || !partner_id )
  905. return 0;
  906. if( ( sd = map_charid2sd(char_id) ) != NULL && sd->status.partner_id == partner_id ) {
  907. sd->status.partner_id = 0;
  908. for(i = 0; i < MAX_INVENTORY; i++)
  909. if (sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F)
  910. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  911. }
  912. if( ( sd = map_charid2sd(partner_id) ) != NULL && sd->status.partner_id == char_id ) {
  913. sd->status.partner_id = 0;
  914. for(i = 0; i < MAX_INVENTORY; i++)
  915. if (sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F)
  916. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  917. }
  918. return 0;
  919. }
  920. /*==========================================
  921. * Removes Baby from parents
  922. *------------------------------------------*/
  923. int chrif_deadopt(uint32 father_id, uint32 mother_id, uint32 child_id) {
  924. struct map_session_data* sd;
  925. uint16 idx = skill_get_index(WE_CALLBABY);
  926. if( father_id && ( sd = map_charid2sd(father_id) ) != NULL && sd->status.child == child_id ) {
  927. sd->status.child = 0;
  928. sd->status.skill[idx].id = 0;
  929. sd->status.skill[idx].lv = 0;
  930. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  931. clif_deleteskill(sd,WE_CALLBABY);
  932. }
  933. if( mother_id && ( sd = map_charid2sd(mother_id) ) != NULL && sd->status.child == child_id ) {
  934. sd->status.child = 0;
  935. sd->status.skill[idx].id = 0;
  936. sd->status.skill[idx].lv = 0;
  937. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  938. clif_deleteskill(sd,WE_CALLBABY);
  939. }
  940. return 0;
  941. }
  942. /*==========================================
  943. * Disconnection of a player (account has been banned of has a status, from login/char-server) by [Yor]
  944. *------------------------------------------*/
  945. int chrif_ban(int fd) {
  946. int id, res=0;
  947. struct map_session_data *sd;
  948. id = RFIFOL(fd,2);
  949. res = RFIFOB(fd,6); // 0: change of statut, 1: ban, 2 charban
  950. if ( battle_config.etc_log )
  951. ShowNotice("chrif_ban %d.type = %s \n", id, res==1?"account":"char");
  952. if(res==2) sd = map_charid2sd(id);
  953. else sd = map_id2sd(id);
  954. if ( id < 0 || sd == NULL ) {
  955. //nothing to do on map if player not connected
  956. return 0;
  957. }
  958. sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
  959. if (res == 0) {
  960. int ret_status = RFIFOL(fd,7); // status or final date of a banishment
  961. if(0<ret_status && ret_status<=9)
  962. clif_displaymessage(sd->fd, msg_txt(sd,411+ret_status));
  963. else if(ret_status==100)
  964. clif_displaymessage(sd->fd, msg_txt(sd,421));
  965. else
  966. clif_displaymessage(sd->fd, msg_txt(sd,420)); //"Your account has not more authorised."
  967. } else if (res == 1 || res == 2) {
  968. time_t timestamp;
  969. char tmpstr[256];
  970. char strtime[25];
  971. timestamp = (time_t)RFIFOL(fd,7); // status or final date of a banishment
  972. strftime(strtime, 24, "%d-%m-%Y %H:%M:%S", localtime(&timestamp));
  973. safesnprintf(tmpstr,sizeof(tmpstr),msg_txt(sd,423),res==2?"char":"account",strtime); //"Your %s has been banished until %s "
  974. clif_displaymessage(sd->fd, tmpstr);
  975. }
  976. set_eof(sd->fd); // forced to disconnect for the change
  977. map_quit(sd); // Remove leftovers (e.g. autotrading) [Paradox924X]
  978. return 0;
  979. }
  980. int chrif_req_charban(int aid, const char* character_name, int32 timediff){
  981. chrif_check(-1);
  982. WFIFOHEAD(char_fd,10+NAME_LENGTH);
  983. WFIFOW(char_fd,0) = 0x2b28;
  984. WFIFOL(char_fd,2) = aid;
  985. WFIFOL(char_fd,6) = timediff;
  986. safestrncpy(WFIFOCP(char_fd,10), character_name, NAME_LENGTH);
  987. WFIFOSET(char_fd,10+NAME_LENGTH); //default 34
  988. return 0;
  989. }
  990. int chrif_req_charunban(int aid, const char* character_name){
  991. chrif_check(-1);
  992. WFIFOHEAD(char_fd,6+NAME_LENGTH);
  993. WFIFOW(char_fd,0) = 0x2b2a;
  994. WFIFOL(char_fd,2) = aid;
  995. safestrncpy(WFIFOCP(char_fd,6), character_name, NAME_LENGTH);
  996. WFIFOSET(char_fd,6+NAME_LENGTH);
  997. return 0;
  998. }
  999. //Disconnect the player out of the game, simple packet
  1000. //packet.w AID.L WHY.B 2+4+1 = 7byte
  1001. int chrif_disconnectplayer(int fd) {
  1002. struct map_session_data* sd;
  1003. uint32 account_id = RFIFOL(fd, 2);
  1004. sd = map_id2sd(account_id);
  1005. if( sd == NULL ) {
  1006. struct auth_node* auth = chrif_search(account_id);
  1007. if( auth != NULL && chrif_auth_delete(account_id, auth->char_id, ST_LOGIN) )
  1008. return 0;
  1009. return -1;
  1010. }
  1011. if (!sd->fd) {
  1012. if (sd->state.autotrade)
  1013. map_quit(sd);
  1014. //Else we don't remove it because the char should have a timer to remove the player because it force-quit before,
  1015. //and we don't want them kicking their previous instance before the 10 secs penalty time passes. [Skotlex]
  1016. return 0;
  1017. }
  1018. switch(RFIFOB(fd, 6)) {
  1019. case 1: clif_authfail_fd(sd->fd, 1); break; //server closed
  1020. case 2: clif_authfail_fd(sd->fd, 2); break; //someone else logged in
  1021. case 3: clif_authfail_fd(sd->fd, 4); break; //server overpopulated
  1022. case 4: clif_authfail_fd(sd->fd, 10); break; //out of available time paid for
  1023. case 5: clif_authfail_fd(sd->fd, 15); break; //forced to dc by gm
  1024. }
  1025. return 0;
  1026. }
  1027. /*==========================================
  1028. * Request/Receive top 10 Fame character list
  1029. *------------------------------------------*/
  1030. int chrif_updatefamelist(map_session_data &sd, e_rank ranktype) {
  1031. chrif_check(-1);
  1032. WFIFOHEAD(char_fd, 11);
  1033. WFIFOW(char_fd,0) = 0x2b10;
  1034. WFIFOL(char_fd,2) = sd.status.char_id;
  1035. WFIFOL(char_fd,6) = sd.status.fame;
  1036. WFIFOB(char_fd,10) = ranktype;
  1037. WFIFOSET(char_fd,11);
  1038. return 0;
  1039. }
  1040. int chrif_buildfamelist(void) {
  1041. chrif_check(-1);
  1042. WFIFOHEAD(char_fd,2);
  1043. WFIFOW(char_fd,0) = 0x2b1a;
  1044. WFIFOSET(char_fd,2);
  1045. return 0;
  1046. }
  1047. int chrif_recvfamelist(int fd) {
  1048. int num, size;
  1049. int total = 0, len = 8;
  1050. memset (smith_fame_list, 0, sizeof(smith_fame_list));
  1051. memset (chemist_fame_list, 0, sizeof(chemist_fame_list));
  1052. memset (taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1053. size = RFIFOW(fd, 6); //Blacksmith block size
  1054. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1055. memcpy(&smith_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1056. len += sizeof(struct fame_list);
  1057. }
  1058. total += num;
  1059. size = RFIFOW(fd, 4); //Alchemist block size
  1060. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1061. memcpy(&chemist_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1062. len += sizeof(struct fame_list);
  1063. }
  1064. total += num;
  1065. size = RFIFOW(fd, 2); //Total packet length
  1066. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1067. memcpy(&taekwon_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1068. len += sizeof(struct fame_list);
  1069. }
  1070. total += num;
  1071. ShowInfo("Received Fame List of '" CL_WHITE "%d" CL_RESET "' characters.\n", total);
  1072. return 0;
  1073. }
  1074. /// fame ranking update confirmation
  1075. /// R 2b22 <table>.B <index>.B <value>.L
  1076. int chrif_updatefamelist_ack(int fd) {
  1077. struct fame_list* list;
  1078. uint8 index;
  1079. switch (RFIFOB(fd,2)) {
  1080. case RANK_BLACKSMITH: list = smith_fame_list; break;
  1081. case RANK_ALCHEMIST: list = chemist_fame_list; break;
  1082. case RANK_TAEKWON: list = taekwon_fame_list; break;
  1083. default: return 0;
  1084. }
  1085. index = RFIFOB(fd, 3);
  1086. if (index >= MAX_FAME_LIST)
  1087. return 0;
  1088. list[index].fame = RFIFOL(fd,4);
  1089. return 1;
  1090. }
  1091. int chrif_save_scdata(struct map_session_data *sd) { //parses the sc_data of the player and sends it to the char-server for saving. [Skotlex]
  1092. #ifdef ENABLE_SC_SAVING
  1093. int i, count=0;
  1094. t_tick tick;
  1095. struct status_change_data data;
  1096. struct status_change *sc = &sd->sc;
  1097. const struct TimerData *timer;
  1098. chrif_check(-1);
  1099. tick = gettick();
  1100. WFIFOHEAD(char_fd, 14 + SC_MAX*sizeof(struct status_change_data));
  1101. WFIFOW(char_fd,0) = 0x2b1c;
  1102. WFIFOL(char_fd,4) = sd->status.account_id;
  1103. WFIFOL(char_fd,8) = sd->status.char_id;
  1104. for (i = 0; i < SC_MAX; i++) {
  1105. if (!sc->data[i])
  1106. continue;
  1107. if (sc->data[i]->timer != INVALID_TIMER) {
  1108. timer = get_timer(sc->data[i]->timer);
  1109. if (timer == NULL || timer->func != status_change_timer)
  1110. continue;
  1111. if (DIFF_TICK(timer->tick,tick) > 0)
  1112. data.tick = DIFF_TICK(timer->tick,tick); //Duration that is left before ending.
  1113. else
  1114. data.tick = 0; //Negative tick does not necessarily mean that sc has expired
  1115. } else
  1116. data.tick = INFINITE_TICK; //Infinite duration
  1117. data.type = i;
  1118. data.val1 = sc->data[i]->val1;
  1119. data.val2 = sc->data[i]->val2;
  1120. data.val3 = sc->data[i]->val3;
  1121. data.val4 = sc->data[i]->val4;
  1122. memcpy(WFIFOP(char_fd,14 +count*sizeof(struct status_change_data)),
  1123. &data, sizeof(struct status_change_data));
  1124. count++;
  1125. }
  1126. WFIFOW(char_fd,12) = count;
  1127. WFIFOW(char_fd,2) = 14 +count*sizeof(struct status_change_data); //Total packet size
  1128. WFIFOSET(char_fd,WFIFOW(char_fd,2));
  1129. #endif
  1130. return 0;
  1131. }
  1132. int chrif_skillcooldown_save(struct map_session_data *sd) {
  1133. int i, count = 0;
  1134. struct skill_cooldown_data data;
  1135. t_tick tick;
  1136. const struct TimerData *timer;
  1137. chrif_check(-1);
  1138. tick = gettick();
  1139. WFIFOHEAD(char_fd, 14 + MAX_SKILLCOOLDOWN * sizeof (struct skill_cooldown_data));
  1140. WFIFOW(char_fd, 0) = 0x2b15;
  1141. WFIFOL(char_fd, 4) = sd->status.account_id;
  1142. WFIFOL(char_fd, 8) = sd->status.char_id;
  1143. for (i = 0; i < MAX_SKILLCOOLDOWN; i++) {
  1144. if (!sd->scd[i])
  1145. continue;
  1146. if (battle_config.guild_skill_relog_type == 1 && SKILL_CHK_GUILD(sd->scd[i]->skill_id))
  1147. continue;
  1148. timer = get_timer(sd->scd[i]->timer);
  1149. if (timer == NULL || timer->func != skill_blockpc_end || DIFF_TICK(timer->tick, tick) < 0)
  1150. continue;
  1151. data.tick = DIFF_TICK(timer->tick, tick);
  1152. data.skill_id = sd->scd[i]->skill_id;
  1153. memcpy(WFIFOP(char_fd, 14 + count * sizeof (struct skill_cooldown_data)), &data, sizeof (struct skill_cooldown_data));
  1154. count++;
  1155. }
  1156. if (count == 0)
  1157. return 0;
  1158. WFIFOW(char_fd, 12) = count;
  1159. WFIFOW(char_fd, 2) = 14 + count * sizeof (struct skill_cooldown_data);
  1160. WFIFOSET(char_fd, WFIFOW(char_fd, 2));
  1161. return 0;
  1162. }
  1163. //Retrieve and load sc_data for a player. [Skotlex]
  1164. int chrif_load_scdata(int fd) {
  1165. #ifdef ENABLE_SC_SAVING
  1166. struct map_session_data *sd;
  1167. int aid, cid, i, count;
  1168. aid = RFIFOL(fd,4); //Player Account ID
  1169. cid = RFIFOL(fd,8); //Player Char ID
  1170. sd = map_id2sd(aid);
  1171. if ( !sd ) {
  1172. ShowError("chrif_load_scdata: Player of AID %d not found!\n", aid);
  1173. return -1;
  1174. }
  1175. if ( sd->status.char_id != cid ) {
  1176. ShowError("chrif_load_scdata: Receiving data for account %d, char id does not matches (%d != %d)!\n", aid, sd->status.char_id, cid);
  1177. return -1;
  1178. }
  1179. count = RFIFOW(fd,12); //sc_count
  1180. for (i = 0; i < count; i++) {
  1181. struct status_change_data *data = (struct status_change_data*)RFIFOP(fd,14 + i*sizeof(struct status_change_data));
  1182. status_change_start(NULL,&sd->bl, (sc_type)data->type, 10000, data->val1, data->val2, data->val3, data->val4, data->tick, SCSTART_NOAVOID|SCSTART_NOTICKDEF|SCSTART_LOADED|SCSTART_NORATEDEF);
  1183. }
  1184. pc_scdata_received(sd);
  1185. #endif
  1186. return 0;
  1187. }
  1188. //Retrieve and load skillcooldown for a player
  1189. int chrif_skillcooldown_load(int fd) {
  1190. struct map_session_data *sd;
  1191. int aid, cid, i, count;
  1192. aid = RFIFOL(fd, 4);
  1193. cid = RFIFOL(fd, 8);
  1194. sd = map_id2sd(aid);
  1195. if (!sd) {
  1196. ShowError("chrif_skillcooldown_load: Player of AID %d not found!\n", aid);
  1197. return -1;
  1198. }
  1199. if (sd->status.char_id != cid) {
  1200. ShowError("chrif_skillcooldown_load: Receiving data for account %d, char id does not matches (%d != %d)!\n", aid, sd->status.char_id, cid);
  1201. return -1;
  1202. }
  1203. count = RFIFOW(fd, 12); //sc_count
  1204. for (i = 0; i < count; i++) {
  1205. struct skill_cooldown_data *data = (struct skill_cooldown_data*) RFIFOP(fd, 14 + i * sizeof (struct skill_cooldown_data));
  1206. skill_blockpc_start(sd, data->skill_id, data->tick);
  1207. }
  1208. return 0;
  1209. }
  1210. /*=========================================
  1211. * Tell char-server charcter disconnected [Wizputer]
  1212. *-----------------------------------------*/
  1213. int chrif_char_offline(struct map_session_data *sd) {
  1214. chrif_check(-1);
  1215. WFIFOHEAD(char_fd,10);
  1216. WFIFOW(char_fd,0) = 0x2b17;
  1217. WFIFOL(char_fd,2) = sd->status.char_id;
  1218. WFIFOL(char_fd,6) = sd->status.account_id;
  1219. WFIFOSET(char_fd,10);
  1220. return 0;
  1221. }
  1222. int chrif_char_offline_nsd(uint32 account_id, uint32 char_id) {
  1223. chrif_check(-1);
  1224. WFIFOHEAD(char_fd,10);
  1225. WFIFOW(char_fd,0) = 0x2b17;
  1226. WFIFOL(char_fd,2) = char_id;
  1227. WFIFOL(char_fd,6) = account_id;
  1228. WFIFOSET(char_fd,10);
  1229. return 0;
  1230. }
  1231. /*=========================================
  1232. * Tell char-server to reset all chars offline [Wizputer]
  1233. *-----------------------------------------*/
  1234. int chrif_flush_fifo(void) {
  1235. chrif_check(-1);
  1236. set_nonblocking(char_fd, 0);
  1237. flush_fifos();
  1238. set_nonblocking(char_fd, 1);
  1239. return 0;
  1240. }
  1241. /*=========================================
  1242. * Tell char-server to reset all chars offline [Wizputer]
  1243. *-----------------------------------------*/
  1244. int chrif_char_reset_offline(void) {
  1245. chrif_check(-1);
  1246. WFIFOHEAD(char_fd,2);
  1247. WFIFOW(char_fd,0) = 0x2b18;
  1248. WFIFOSET(char_fd,2);
  1249. return 0;
  1250. }
  1251. /*=========================================
  1252. * Tell char-server charcter is online [Wizputer]
  1253. *-----------------------------------------*/
  1254. int chrif_char_online(struct map_session_data *sd) {
  1255. chrif_check(-1);
  1256. WFIFOHEAD(char_fd,10);
  1257. WFIFOW(char_fd,0) = 0x2b19;
  1258. WFIFOL(char_fd,2) = sd->status.char_id;
  1259. WFIFOL(char_fd,6) = sd->status.account_id;
  1260. WFIFOSET(char_fd,10);
  1261. return 0;
  1262. }
  1263. /// Called when the connection to Char Server is disconnected.
  1264. void chrif_on_disconnect(void) {
  1265. if( chrif_connected != 1 )
  1266. ShowWarning("Connection to Char Server lost.\n\n");
  1267. chrif_connected = 0;
  1268. other_mapserver_count = 0; //Reset counter. We receive ALL maps from all map-servers on reconnect.
  1269. map_eraseallipport();
  1270. //Attempt to reconnect in a second. [Skotlex]
  1271. add_timer(gettick() + 1000, check_connect_char_server, 0, 0);
  1272. }
  1273. /**
  1274. * !CHECKME: This is intended?
  1275. * On sync request received, map-server only send its own IP
  1276. * without change the char IP (if any)?
  1277. * Since no IP info sent by 0x2b1e (chlogif_parse_updip)
  1278. **/
  1279. void chrif_update_ip(int fd) {
  1280. uint32 new_ip;
  1281. WFIFOHEAD(fd,6);
  1282. new_ip = host2ip(char_ip_str);
  1283. if (new_ip && new_ip != char_ip)
  1284. char_ip = new_ip; //Update char_ip
  1285. new_ip = clif_refresh_ip();
  1286. if (!new_ip)
  1287. return; //No change
  1288. WFIFOW(fd,0) = 0x2b13;
  1289. WFIFOL(fd,2) = htonl(new_ip);
  1290. WFIFOSET(fd,6);
  1291. }
  1292. // pings the charserver
  1293. void chrif_keepalive(int fd) {
  1294. WFIFOHEAD(fd,2);
  1295. WFIFOW(fd,0) = 0x2b23;
  1296. WFIFOSET(fd,2);
  1297. }
  1298. void chrif_keepalive_ack(int fd) {
  1299. session[fd]->flag.ping = 0;/* reset ping state, we received a packet */
  1300. }
  1301. /**
  1302. * Received vip-data from char-serv, fill map-serv data
  1303. * @param fd : char-serv file descriptor (link to char-serv)
  1304. */
  1305. void chrif_parse_ack_vipActive(int fd) {
  1306. #ifdef VIP_ENABLE
  1307. int aid = RFIFOL(fd,2);
  1308. uint32 vip_time = RFIFOL(fd,6);
  1309. uint32 groupid = RFIFOL(fd,10);
  1310. uint8 flag = RFIFOB(fd,14);
  1311. TBL_PC *sd = map_id2sd(aid);
  1312. bool changed = false;
  1313. if(sd == NULL) return;
  1314. sd->group_id = groupid;
  1315. pc_group_pc_load(sd);
  1316. if ((flag&0x2)) //isgm
  1317. clif_displaymessage(sd->fd,msg_txt(sd,437));
  1318. else {
  1319. changed = (sd->vip.enabled != (flag&0x1));
  1320. if((flag&0x1)) { //isvip
  1321. sd->vip.enabled = 1;
  1322. sd->vip.time = vip_time;
  1323. // Increase storage size for VIP.
  1324. sd->storage.max_amount = battle_config.vip_storage_increase + MIN_STORAGE;
  1325. if (sd->storage.max_amount > MAX_STORAGE) {
  1326. ShowError("intif_parse_ack_vipActive: Storage size for player %s (%d:%d) is larger than MAX_STORAGE. Storage size has been set to MAX_STORAGE.\n", sd->status.name, sd->status.account_id, sd->status.char_id);
  1327. sd->storage.max_amount = MAX_STORAGE;
  1328. }
  1329. } else if (sd->vip.enabled) {
  1330. sd->vip.enabled = 0;
  1331. sd->vip.time = 0;
  1332. sd->storage.max_amount = MIN_STORAGE;
  1333. sd->special_state.no_gemstone = 0;
  1334. clif_displaymessage(sd->fd,msg_txt(sd,438));
  1335. }
  1336. }
  1337. clif_goldpc_info( *sd );
  1338. // Show info if status changed
  1339. if (((flag&0x4) || changed) && !sd->vip.disableshowrate) {
  1340. clif_display_pinfo(sd,ZC_PERSONAL_INFOMATION);
  1341. //clif_vip_display_info(sd,ZC_PERSONAL_INFOMATION_CHN);
  1342. }
  1343. #endif
  1344. }
  1345. /**
  1346. * ZA 0x2b2d
  1347. * <cmd>.W <char_id>.L
  1348. * Requets bonus_script datas
  1349. * @param char_id
  1350. * @author [Cydh]
  1351. **/
  1352. int chrif_bsdata_request(uint32 char_id) {
  1353. chrif_check(-1);
  1354. WFIFOHEAD(char_fd,6);
  1355. WFIFOW(char_fd,0) = 0x2b2d;
  1356. WFIFOL(char_fd,2) = char_id;
  1357. WFIFOSET(char_fd,6);
  1358. return 0;
  1359. }
  1360. /**
  1361. * ZA 0x2b2e
  1362. * <cmd>.W <len>.W <char_id>.L <count>.B { <bonus_script>.?B }
  1363. * Stores bonus_script data(s) to the table
  1364. * @param sd
  1365. * @author [Cydh]
  1366. **/
  1367. int chrif_bsdata_save(struct map_session_data *sd, bool quit) {
  1368. uint8 i = 0;
  1369. chrif_check(-1);
  1370. if (!sd)
  1371. return 0;
  1372. // Removing...
  1373. if (quit && sd->bonus_script.head) {
  1374. uint32 flag = BSF_REM_ON_LOGOUT; //Remove bonus when logout
  1375. if (battle_config.debuff_on_logout&1) //Remove negative buffs
  1376. flag |= BSF_REM_DEBUFF;
  1377. if (battle_config.debuff_on_logout&2) //Remove positive buffs
  1378. flag |= BSF_REM_BUFF;
  1379. pc_bonus_script_clear(sd, flag);
  1380. }
  1381. //ShowInfo("Saving %d bonus script for CID=%d\n", sd->bonus_script.count, sd->status.char_id);
  1382. WFIFOHEAD(char_fd, 9 + sd->bonus_script.count * sizeof(struct bonus_script_data));
  1383. WFIFOW(char_fd, 0) = 0x2b2e;
  1384. WFIFOL(char_fd, 4) = sd->status.char_id;
  1385. if (sd->bonus_script.count) {
  1386. t_tick tick = gettick();
  1387. struct linkdb_node *node = NULL;
  1388. for (node = sd->bonus_script.head; node && i < MAX_PC_BONUS_SCRIPT; node = node->next) {
  1389. const struct TimerData *timer = NULL;
  1390. struct bonus_script_data bs;
  1391. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  1392. if (!entry || !(timer = get_timer(entry->tid)) || DIFF_TICK(timer->tick,tick) < 0)
  1393. continue;
  1394. memset(&bs, 0, sizeof(bs));
  1395. safestrncpy(bs.script_str, StringBuf_Value(entry->script_buf), StringBuf_Length(entry->script_buf)+1);
  1396. bs.tick = DIFF_TICK(timer->tick, tick);
  1397. bs.flag = entry->flag;
  1398. bs.type = entry->type;
  1399. bs.icon = entry->icon;
  1400. memcpy(WFIFOP(char_fd, 9 + i * sizeof(struct bonus_script_data)), &bs, sizeof(struct bonus_script_data));
  1401. i++;
  1402. }
  1403. if (i != sd->bonus_script.count && sd->bonus_script.count > MAX_PC_BONUS_SCRIPT)
  1404. ShowWarning("Only allowed to save %d (mmo.hpp::MAX_PC_BONUS_SCRIPT) bonus script each player.\n", MAX_PC_BONUS_SCRIPT);
  1405. }
  1406. WFIFOB(char_fd, 8) = i;
  1407. WFIFOW(char_fd, 2) = 9 + sd->bonus_script.count * sizeof(struct bonus_script_data);
  1408. WFIFOSET(char_fd, WFIFOW(char_fd, 2));
  1409. return 0;
  1410. }
  1411. /**
  1412. * AZ 0x2b2f
  1413. * <cmd>.W <len>.W <cid>.L <count>.B { <bonus_script_data>.?B }
  1414. * Bonus script received, set to player
  1415. * @param fd
  1416. * @author [Cydh]
  1417. **/
  1418. int chrif_bsdata_received(int fd) {
  1419. struct map_session_data *sd;
  1420. uint32 cid = RFIFOL(fd,4);
  1421. uint8 count = 0;
  1422. sd = map_charid2sd(cid);
  1423. if (!sd) {
  1424. ShowError("chrif_bsdata_received: Player with CID %d not found!\n",cid);
  1425. return -1;
  1426. }
  1427. if ((count = RFIFOB(fd,8))) {
  1428. uint8 i = 0;
  1429. //ShowInfo("Loaded %d bonus script for CID=%d\n", count, sd->status.char_id);
  1430. for (i = 0; i < count; i++) {
  1431. struct bonus_script_data *bs = (struct bonus_script_data*)RFIFOP(fd,9 + i*sizeof(struct bonus_script_data));
  1432. struct s_bonus_script_entry *entry = NULL;
  1433. if (bs->script_str[0] == '\0' || !bs->tick)
  1434. continue;
  1435. if (!(entry = pc_bonus_script_add(sd, bs->script_str, bs->tick, (enum efst_type)bs->icon, bs->flag, bs->type)))
  1436. continue;
  1437. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  1438. }
  1439. if (sd->bonus_script.head)
  1440. status_calc_pc(sd,SCO_NONE);
  1441. }
  1442. return 0;
  1443. }
  1444. /*==========================================
  1445. *
  1446. *------------------------------------------*/
  1447. int chrif_parse(int fd) {
  1448. int packet_len;
  1449. // only process data from the char-server
  1450. if ( fd != char_fd ) {
  1451. ShowDebug("chrif_parse: Disconnecting invalid session #%d (is not the char-server)\n", fd);
  1452. do_close(fd);
  1453. return 0;
  1454. }
  1455. if ( session[fd]->flag.eof ) {
  1456. do_close(fd);
  1457. char_fd = -1;
  1458. chrif_on_disconnect();
  1459. return 0;
  1460. } else if ( session[fd]->flag.ping ) {/* we've reached stall time */
  1461. if( DIFF_TICK(last_tick, session[fd]->rdata_tick) > (stall_time * 2) ) {/* we can't wait any longer */
  1462. set_eof(fd);
  1463. return 0;
  1464. } else if( session[fd]->flag.ping != 2 ) { /* we haven't sent ping out yet */
  1465. chrif_keepalive(fd);
  1466. session[fd]->flag.ping = 2;
  1467. }
  1468. }
  1469. while ( RFIFOREST(fd) >= 2 ) {
  1470. int cmd = RFIFOW(fd,0);
  1471. if (cmd < 0x2af8 || cmd >= 0x2af8 + ARRAYLENGTH(packet_len_table) || packet_len_table[cmd-0x2af8] == 0) {
  1472. int r = intif_parse(fd); // Passed on to the intif
  1473. if (r == 1) continue; // Treated in intif
  1474. if (r == 2) return 0; // Didn't have enough data (len==-1)
  1475. ShowWarning("chrif_parse: session #%d, intif_parse failed (unrecognized command 0x%.4x).\n", fd, cmd);
  1476. set_eof(fd);
  1477. return 0;
  1478. }
  1479. if ( ( packet_len = packet_len_table[cmd-0x2af8] ) == -1) { // dynamic-length packet, second WORD holds the length
  1480. if (RFIFOREST(fd) < 4)
  1481. return 0;
  1482. packet_len = RFIFOW(fd,2);
  1483. }
  1484. if ((int)RFIFOREST(fd) < packet_len)
  1485. return 0;
  1486. //ShowDebug("Received packet 0x%4x (%d bytes) from char-server (connection %d)\n", RFIFOW(fd,0), packet_len, fd);
  1487. switch(cmd) {
  1488. case 0x2af9: chrif_connectack(fd); break;
  1489. case 0x2afb: chrif_sendmapack(fd); break;
  1490. case 0x2afd: chrif_authok(fd); break;
  1491. case 0x2b00: map_setusers(RFIFOL(fd,2)); chrif_keepalive(fd); break;
  1492. case 0x2b03: clif_charselectok(RFIFOL(fd,2), RFIFOB(fd,6)); break;
  1493. case 0x2b04: chrif_recvmap(fd); break;
  1494. case 0x2b06: chrif_changemapserverack(RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10), RFIFOL(fd,14), RFIFOW(fd,18), RFIFOW(fd,20), RFIFOW(fd,22), RFIFOL(fd,24), RFIFOW(fd,28)); break;
  1495. case 0x2b09: map_addnickdb(RFIFOL(fd,2), RFIFOCP(fd,6)); break;
  1496. case 0x2b0b: chrif_skillcooldown_load(fd); break;
  1497. case 0x2b0d: chrif_changedsex(fd); break;
  1498. case 0x2b0f: chrif_ack_login_req(RFIFOL(fd,2), RFIFOCP(fd,6), RFIFOW(fd,30), RFIFOW(fd,32)); break;
  1499. case 0x2b12: chrif_divorceack(RFIFOL(fd,2), RFIFOL(fd,6)); break;
  1500. case 0x2b14: chrif_ban(fd); break;
  1501. case 0x2b1b: chrif_recvfamelist(fd); break;
  1502. case 0x2b1d: chrif_load_scdata(fd); break;
  1503. case 0x2b1e: chrif_update_ip(fd); break;
  1504. case 0x2b1f: chrif_disconnectplayer(fd); break;
  1505. case 0x2b20: chrif_removemap(fd); break;
  1506. case 0x2b21: chrif_save_ack(fd); break;
  1507. case 0x2b22: chrif_updatefamelist_ack(fd); break;
  1508. case 0x2b24: chrif_keepalive_ack(fd); break;
  1509. case 0x2b25: chrif_deadopt(RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)); break;
  1510. case 0x2b27: chrif_authfail(fd); break;
  1511. case 0x2b2b: chrif_parse_ack_vipActive(fd); break;
  1512. case 0x2b2f: chrif_bsdata_received(fd); break;
  1513. default:
  1514. ShowError("chrif_parse : unknown packet (session #%d): 0x%x. Disconnecting.\n", fd, cmd);
  1515. set_eof(fd);
  1516. return 0;
  1517. }
  1518. if ( fd == char_fd ) //There's the slight chance we lost the connection during parse, in which case this would segfault if not checked [Skotlex]
  1519. RFIFOSKIP(fd, packet_len);
  1520. }
  1521. return 0;
  1522. }
  1523. // unused
  1524. TIMER_FUNC(send_usercount_tochar){
  1525. chrif_check(-1);
  1526. WFIFOHEAD(char_fd,4);
  1527. WFIFOW(char_fd,0) = 0x2afe;
  1528. WFIFOW(char_fd,2) = map_usercount();
  1529. WFIFOSET(char_fd,4);
  1530. return 0;
  1531. }
  1532. /*==========================================
  1533. * timerFunction
  1534. * Send to char the number of client connected to map
  1535. *------------------------------------------*/
  1536. int send_users_tochar(void) {
  1537. int users = 0, i = 0;
  1538. struct map_session_data* sd;
  1539. struct s_mapiterator* iter;
  1540. chrif_check(-1);
  1541. users = map_usercount();
  1542. WFIFOHEAD(char_fd, 6+8*users);
  1543. WFIFOW(char_fd,0) = 0x2aff;
  1544. iter = mapit_getallusers();
  1545. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) ) {
  1546. WFIFOL(char_fd,6+8*i) = sd->status.account_id;
  1547. WFIFOL(char_fd,6+8*i+4) = sd->status.char_id;
  1548. i++;
  1549. }
  1550. mapit_free(iter);
  1551. WFIFOW(char_fd,2) = 6 + 8*users;
  1552. WFIFOW(char_fd,4) = users;
  1553. WFIFOSET(char_fd, 6+8*users);
  1554. return 0;
  1555. }
  1556. /*==========================================
  1557. * timerFunction
  1558. * Chk the connection to char server, (if it down)
  1559. *------------------------------------------*/
  1560. static TIMER_FUNC(check_connect_char_server){
  1561. static int displayed = 0;
  1562. if ( char_fd <= 0 || session[char_fd] == NULL ) {
  1563. if ( !displayed ) {
  1564. ShowStatus("Attempting to connect to Char Server. Please wait.\n");
  1565. displayed = 1;
  1566. }
  1567. chrif_state = 0;
  1568. char_fd = make_connection(char_ip, char_port,false,10);
  1569. if (char_fd == -1)//Attempt to connect later. [Skotlex]
  1570. return 0;
  1571. session[char_fd]->func_parse = chrif_parse;
  1572. session[char_fd]->flag.server = 1;
  1573. realloc_fifo(char_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  1574. chrif_connect(char_fd);
  1575. chrif_connected = (chrif_state == 2);
  1576. }
  1577. if ( chrif_isconnected() )
  1578. displayed = 0;
  1579. return 0;
  1580. }
  1581. /*==========================================
  1582. * Asks char server to remove friend_id from the friend list of char_id
  1583. *------------------------------------------*/
  1584. int chrif_removefriend(uint32 char_id, int friend_id) {
  1585. chrif_check(-1);
  1586. WFIFOHEAD(char_fd,10);
  1587. WFIFOW(char_fd,0) = 0x2b07;
  1588. WFIFOL(char_fd,2) = char_id;
  1589. WFIFOL(char_fd,6) = friend_id;
  1590. WFIFOSET(char_fd,10);
  1591. return 0;
  1592. }
  1593. /**
  1594. * @see DBApply
  1595. */
  1596. int auth_db_final(DBKey key, DBData *data, va_list ap) {
  1597. struct auth_node *node = (struct auth_node *)db_data2ptr(data);
  1598. if (node->char_dat)
  1599. aFree(node->char_dat);
  1600. if (node->sd) {
  1601. if (node->sd->regs.vars)
  1602. node->sd->regs.vars->destroy(node->sd->regs.vars, script_reg_destroy);
  1603. if (node->sd->regs.arrays)
  1604. node->sd->regs.arrays->destroy(node->sd->regs.arrays, script_free_array_db);
  1605. aFree(node->sd);
  1606. }
  1607. ers_free(auth_db_ers, node);
  1608. return 0;
  1609. }
  1610. /*==========================================
  1611. * Destructor
  1612. *------------------------------------------*/
  1613. void do_final_chrif(void) {
  1614. if( char_fd != -1 ) {
  1615. do_close(char_fd);
  1616. char_fd = -1;
  1617. }
  1618. auth_db->destroy(auth_db, auth_db_final);
  1619. ers_destroy(auth_db_ers);
  1620. }
  1621. /*==========================================
  1622. *
  1623. *------------------------------------------*/
  1624. void do_init_chrif(void) {
  1625. if(sizeof(struct mmo_charstatus) > 0xFFFF){
  1626. ShowError("mmo_charstatus size = %" PRIuPTR " is too big to be transmitted. (must be below 0xFFFF)\n",
  1627. sizeof(struct mmo_charstatus));
  1628. exit(EXIT_FAILURE);
  1629. }
  1630. if (sizeof(struct s_storage) > 0xFFFF) {
  1631. ShowError("s_storage size = %" PRIuPTR " is too big to be transmitted. (must be below 0xFFFF)\n", sizeof(struct s_storage));
  1632. exit(EXIT_FAILURE);
  1633. }
  1634. if((sizeof(struct bonus_script_data) * MAX_PC_BONUS_SCRIPT) > 0xFFFF){
  1635. ShowError("bonus_script_data size = %d is too big, please reduce MAX_PC_BONUS_SCRIPT (%d) size. (must be below 0xFFFF).\n",
  1636. (sizeof(struct bonus_script_data) * MAX_PC_BONUS_SCRIPT), MAX_PC_BONUS_SCRIPT);
  1637. exit(EXIT_FAILURE);
  1638. }
  1639. auth_db = idb_alloc(DB_OPT_BASE);
  1640. auth_db_ers = ers_new(sizeof(struct auth_node),"chrif.cpp::auth_db_ers",ERS_OPT_NONE);
  1641. add_timer_func_list(check_connect_char_server, "check_connect_char_server");
  1642. add_timer_func_list(auth_db_cleanup, "auth_db_cleanup");
  1643. // establish map-char connection if not present
  1644. add_timer_interval(gettick() + 1000, check_connect_char_server, 0, 0, 10 * 1000);
  1645. // wipe stale data for timed-out client connection requests
  1646. add_timer_interval(gettick() + 1000, auth_db_cleanup, 0, 0, 30 * 1000);
  1647. // send the user count every 10 seconds, to hide the charserver's online counting problem
  1648. add_timer_interval(gettick() + 1000, send_usercount_tochar, 0, 0, UPDATE_INTERVAL);
  1649. }