chrif.cpp 60 KB

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