chrif.c 62 KB

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