chrif.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <time.h>
  8. #include <limits.h>
  9. #include "../common/malloc.h"
  10. #include "../common/socket.h"
  11. #include "../common/timer.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "map.h"
  15. #include "battle.h"
  16. #include "chrif.h"
  17. #include "clif.h"
  18. #include "intif.h"
  19. #include "npc.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #ifndef TXT_ONLY
  23. #include "charsave.h"
  24. #endif
  25. //Updated table (only doc^^) [Sirius]
  26. //Used Packets: U->2af8
  27. //Free Packets: F->2af8
  28. struct dbt *auth_db;
  29. static const int packet_len_table[0x3d] = {
  30. 60, 3,-1,27,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,18, 7,-1,49,30,10, // 2b00-2b07: U->2b00, U->2b01, U->2b02, U->2b03, U->2b04, U->2b05, U->2b06, U->2b07
  32. 6,30,-1,10,86, 7,44,34, // 2b08-2b0f: U->2b08, U->2b09, U->2b0a, U->2b0b, U->2b0c, U->2b0d, U->2b0e, U->2b0f
  33. 0,-1,10, 6,11,-1, 0, 0, // 2b10-2b17: U->2b10, U->2b11, U->2b12, U->2b13, U->2b14, U->2b15, U->2b16, U->2b17
  34. -1,-1,-1,-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,-1,-1,-1,-1,-1,-1,-1, // 2b20-2b27: U->2b20, F->2b21, F->2b22, F->2b23, F->2b24, F->2b25, F->2b26, F->2b27
  36. };
  37. //Used Packets:
  38. //2af8: Outgoing, chrif_connect -> 'connect to charserver / auth @ charserver'
  39. //2af9: Incomming, chrif_connectack -> 'answer of the 2af8 login(ok / fail)'
  40. //2afa: Outgoing, chrif_sendmap -> 'sending our maps'
  41. //2afb: Incomming, chrif_sendmapack -> 'Maps received successfully / or not ..'
  42. //2afc: Outgoing, chrif_scdata_request -> request sc_data for pc_authok'ed char. <- new command reuses previous one.
  43. //2afd: Incomming, chrif_authok -> 'character selected, add to auth db'
  44. //2afe: Outgoing, send_usercount_tochar -> 'sends player count of this map server to charserver'
  45. //2aff: Outgoing, send_users_tochar -> 'sends all actual connected character ids to charserver'
  46. //2b00: Incomming, map_setusers -> 'set the actual usercount? PACKET.2B COUNT.L.. ?' (not sure)
  47. //2b01: Outgoing, chrif_save -> 'charsave of char XY account XY (complete struct)'
  48. //2b02: Outgoing, chrif_charselectreq -> 'player returns from ingame to charserver to select another char.., this packets includes sessid etc' ? (not 100% sure)
  49. //2b03: Incomming, clif_charselectok -> '' (i think its the packet after enterworld?) (not sure)
  50. //2b04: Incomming, chrif_recvmap -> 'getting maps from charserver of other mapserver's'
  51. //2b05: Outgoing, chrif_changemapserver -> 'Tell the charserver the mapchange / quest for ok...'
  52. //2b06: Incomming, chrif_changemapserverack -> 'awnser of 2b05, ok/fail, data: dunno^^'
  53. //2b07: Incoming, clif_updatemaxid -> Received when updating the max account/char known
  54. //2b08: Outgoing, chrif_searchcharid -> '...'
  55. //2b09: Incomming, map_addchariddb -> 'Adds a name to the nick db'
  56. //2b0a: Outgoing, chrif_changegm -> 'level change of acc/char XY'
  57. //2b0b: Incomming, chrif_changedgm -> 'answer of 2b0a..'
  58. //2b0c: Outgoing, chrif_changeemail -> 'change mail address ...'
  59. //2b0d: Incomming, chrif_changedsex -> 'Change sex of acc XY'
  60. //2b0e: Outgoing, chrif_char_ask_name -> 'Do some operations (change sex, ban / unban etc)'
  61. //2b0f: Incomming, chrif_char_ask_name_answer -> 'answer of the 2b0e'
  62. //2b10: Outgoing, chrif_updatefamelist -> 'Update the fame ranking lists and send them'
  63. //2b11: Outgoing, chrif_changesex -> 'change sex of acc X'
  64. //2b12: Incomming, chrif_divorce -> 'divorce a wedding of charid X and partner id X'
  65. //2b13: Incomming, chrif_accountdeletion -> 'Delete acc XX, if the player is on, kick ....'
  66. //2b14: Incomming, chrif_accountban -> 'not sure: kick the player with message XY'
  67. //2b15: Incomming, chrif_recvgmaccounts -> 'recieve gm accs from charserver (seems to be incomplete !)'
  68. //2b16: Outgoing, chrif_ragsrvinfo -> 'sends motd / rates ....'
  69. //2b17: Outgoing, chrif_char_offline -> 'tell the charserver that the char is now offline'
  70. //2b18: Outgoing, chrif_char_reset_offline -> 'set all players OFF!'
  71. //2b19: Outgoing, chrif_char_online -> 'tell the charserver that the char .. is online'
  72. //2b1a: Outgoing, chrif_buildfamelist -> 'Build the fame ranking lists and send them'
  73. //2b1b: Incomming, chrif_recvfamelist -> 'Receive fame ranking lists'
  74. //2b1c: Outgoing, chrif_save_scdata -> 'Send sc_data of player for saving.'
  75. //2b1d: Incomming, chrif_load_scdata -> 'received sc_data of player for loading.'
  76. //2b1e: Incoming, chrif_update_ip -> 'Reqest forwarded from char-server for interserver IP sync.' [Lance]
  77. //2b1f: Incomming, chrif_disconnectplayer -> 'disconnects a player (aid X) with the message XY ... 0x81 ..' [Sirius]
  78. //2b20: Incomming, chrif_removemap -> 'remove maps of a server (sample: its going offline)' [Sirius]
  79. //2b21-2b27: FREE
  80. int chrif_connected;
  81. int char_fd = 0; //Using 0 instead of -1 is safer against crashes. [Skotlex]
  82. int srvinfo;
  83. static char char_ip_str[128];
  84. static in_addr_t char_ip= 0;
  85. static int char_port = 6121;
  86. static char userid[NAME_LENGTH], passwd[NAME_LENGTH];
  87. static int chrif_state = 0;
  88. static int char_init_done = 0;
  89. int other_mapserver_count=0; //Holds count of how many other map servers are online (apart of this instance) [Skotlex]
  90. //Interval at which map server updates online listing. [Valaris]
  91. #define CHECK_INTERVAL 3600000
  92. //Interval at which map server sends number of connected users. [Skotlex]
  93. #define UPDATE_INTERVAL 10000
  94. //This define should spare writing the check in every function. [Skotlex]
  95. #define chrif_check(a) { if(!chrif_isconnect()) return a; }
  96. // 設定ファイル読み込み関係
  97. /*==========================================
  98. *
  99. *------------------------------------------
  100. */
  101. void chrif_setuserid(char *id)
  102. {
  103. memcpy(userid, id, NAME_LENGTH);
  104. }
  105. /*==========================================
  106. *
  107. *------------------------------------------
  108. */
  109. void chrif_setpasswd(char *pwd)
  110. {
  111. memcpy(passwd, pwd, NAME_LENGTH);
  112. }
  113. /*==========================================
  114. *
  115. *------------------------------------------
  116. */
  117. void chrif_checkdefaultlogin(void)
  118. {
  119. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  120. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  121. #ifdef TXT_ONLY
  122. ShowNotice("Please edit your save/account.txt file to create a proper inter-server user/password (gender 'S')\n");
  123. #else
  124. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  125. #endif
  126. ShowNotice("and then edit your user/password in conf/map_athena.conf (or conf/import/map_conf.txt)\n");
  127. }
  128. }
  129. /*==========================================
  130. *
  131. *------------------------------------------
  132. */
  133. int chrif_setip(char *ip)
  134. {
  135. char ip_str[16];
  136. char_ip = resolve_hostbyname(ip,NULL,ip_str);
  137. if (!char_ip) {
  138. ShowWarning("Failed to Resolve Char Server Address! (%s)\n", ip);
  139. return 0;
  140. }
  141. strncpy(char_ip_str, ip, sizeof(char_ip_str));
  142. ShowInfo("Char Server IP Address : '"CL_WHITE"%s"CL_RESET"' -> '"CL_WHITE"%s"CL_RESET"'.\n", ip, ip_str);
  143. return 1;
  144. }
  145. /*==========================================
  146. *
  147. *------------------------------------------
  148. */
  149. void chrif_setport(int port)
  150. {
  151. char_port = port;
  152. }
  153. /*==========================================
  154. *
  155. *------------------------------------------
  156. */
  157. int chrif_isconnect(void)
  158. {
  159. return (char_fd > 0 && session[char_fd] != NULL && chrif_state == 2);
  160. }
  161. /*==========================================
  162. * Saves char.
  163. * Flag = 1: Character is quitting.
  164. * Flag = 2: Character is changing map-servers
  165. *------------------------------------------
  166. */
  167. int chrif_save(struct map_session_data *sd, int flag)
  168. {
  169. nullpo_retr(-1, sd);
  170. chrif_check(-1);
  171. pc_makesavestatus(sd);
  172. if (sd->state.finalsave)
  173. return -1; //Refuse to save a char already tagged for final saving. [Skotlex]
  174. //For data sync
  175. if (sd->state.storage_flag == 1)
  176. storage_storage_save(sd->status.account_id, flag);
  177. else if (sd->state.storage_flag == 2)
  178. storage_guild_storagesave(sd->status.account_id, sd->status.guild_id);
  179. //Saving of registry values.
  180. if (sd->state.reg_dirty&4)
  181. intif_saveregistry(sd, 3); //Save char regs
  182. if (sd->state.reg_dirty&2)
  183. intif_saveregistry(sd, 2); //Save account regs
  184. if (sd->state.reg_dirty&1)
  185. intif_saveregistry(sd, 1); //Save account2 regs
  186. #ifndef TXT_ONLY
  187. if(charsave_method){ //New 'Local' save
  188. charsave_savechar(sd->char_id, &sd->status);
  189. if (flag == 1) chrif_char_offline(sd); //Tell char server that character went offline.
  190. }else{
  191. #endif
  192. WFIFOHEAD(char_fd, sizeof(sd->status) + 13);
  193. WFIFOW(char_fd,0) = 0x2b01;
  194. WFIFOW(char_fd,2) = sizeof(sd->status) + 13;
  195. WFIFOL(char_fd,4) = sd->bl.id;
  196. WFIFOL(char_fd,8) = sd->char_id;
  197. WFIFOB(char_fd,12) = (flag==1)?1:0; //Flag to tell char-server this character is quitting.
  198. memcpy(WFIFOP(char_fd,13), &sd->status, sizeof(sd->status));
  199. WFIFOSET(char_fd, WFIFOW(char_fd,2));
  200. #ifndef TXT_ONLY
  201. }
  202. #endif
  203. if (flag)
  204. sd->state.finalsave = 1; //Mark the last save as done.
  205. return 0;
  206. }
  207. /*==========================================
  208. *
  209. *------------------------------------------
  210. */
  211. int chrif_connect(int fd)
  212. {
  213. ShowStatus("Logging in to char server...\n", char_fd);
  214. WFIFOHEAD(fd, 60);
  215. WFIFOW(fd,0) = 0x2af8;
  216. memcpy(WFIFOP(fd,2), userid, NAME_LENGTH);
  217. memcpy(WFIFOP(fd,26), passwd, NAME_LENGTH);
  218. WFIFOL(fd,50) = 0;
  219. WFIFOL(fd,54) = clif_getip_long();
  220. WFIFOW(fd,58) = clif_getport(); // [Valaris] thanks to fov
  221. WFIFOSET(fd,60);
  222. return 0;
  223. }
  224. /*==========================================
  225. * マップ送信
  226. *------------------------------------------
  227. */
  228. int chrif_sendmap(int fd)
  229. {
  230. int i;
  231. ShowStatus("Sending maps to char server...\n");
  232. WFIFOHEAD(fd, 4 + map_num * 4);
  233. WFIFOW(fd,0) = 0x2afa;
  234. for(i = 0; i < map_num; i++)
  235. WFIFOW(fd,4+i*4) = map[i].index;
  236. WFIFOW(fd,2) = 4 + i * 4;
  237. WFIFOSET(fd,WFIFOW(fd,2));
  238. return 0;
  239. }
  240. /*==========================================
  241. * マップ受信
  242. *------------------------------------------
  243. */
  244. int chrif_recvmap(int fd)
  245. {
  246. int i, j, ip, port;
  247. unsigned char *p = (unsigned char *)&ip;
  248. RFIFOHEAD(fd);
  249. ip = RFIFOL(fd,4);
  250. port = RFIFOW(fd,8);
  251. for(i = 10, j = 0; i < RFIFOW(fd,2); i += 4, j++) {
  252. map_setipport(RFIFOW(fd,i), ip, port);
  253. // if (battle_config.etc_log)
  254. // printf("recv map %d %s\n", j, RFIFOP(fd,i));
  255. }
  256. if (battle_config.etc_log)
  257. ShowStatus("recv map on %d.%d.%d.%d:%d (%d maps)\n", p[0], p[1], p[2], p[3], port, j);
  258. other_mapserver_count++;
  259. return 0;
  260. }
  261. /*==========================================
  262. * Delete maps of other servers, (if an other mapserver is going OFF)
  263. *------------------------------------------
  264. */
  265. int chrif_removemap(int fd){
  266. int i, j, ip, port;
  267. unsigned char *p = (unsigned char *)&ip;
  268. RFIFOHEAD(fd);
  269. ip = RFIFOL(fd, 4);
  270. port = RFIFOW(fd, 8);
  271. for(i = 10, j = 0; i < RFIFOW(fd, 2); i += 4, j++){
  272. map_eraseipport(RFIFOW(fd, i), ip, port);
  273. }
  274. other_mapserver_count--;
  275. if(battle_config.etc_log)
  276. ShowStatus("remove map of server %d.%d.%d.%d:%d (%d maps)\n", p[0], p[1], p[2], p[3], port, j);
  277. return 0;
  278. }
  279. /*==========================================
  280. * マップ鯖間移動のためのデータ準備要求
  281. *------------------------------------------
  282. */
  283. int chrif_changemapserver(struct map_session_data *sd, short map, int x, int y, int ip, short port)
  284. {
  285. int s_ip;
  286. nullpo_retr(-1, sd);
  287. chrif_check(-1);
  288. if (other_mapserver_count < 1)
  289. { //No other map servers are online!
  290. pc_authfail(sd);
  291. return -1;
  292. }
  293. if (sd->fd && sd->fd < fd_max && session[sd->fd])
  294. s_ip = session[sd->fd]->client_addr.sin_addr.s_addr;
  295. else //Not connected? Can't retrieve IP
  296. s_ip = 0;
  297. WFIFOHEAD(char_fd, 35);
  298. WFIFOW(char_fd, 0) = 0x2b05;
  299. WFIFOL(char_fd, 2) = sd->bl.id;
  300. WFIFOL(char_fd, 6) = sd->login_id1;
  301. WFIFOL(char_fd,10) = sd->login_id2;
  302. WFIFOL(char_fd,14) = sd->status.char_id;
  303. WFIFOW(char_fd,18) = map;
  304. WFIFOW(char_fd,20) = x;
  305. WFIFOW(char_fd,22) = y;
  306. WFIFOL(char_fd,24) = ip;
  307. WFIFOW(char_fd,28) = port;
  308. WFIFOB(char_fd,30) = sd->status.sex;
  309. WFIFOL(char_fd,31) = s_ip;
  310. WFIFOSET(char_fd,35);
  311. return 0;
  312. }
  313. /*==========================================
  314. * マップ鯖間移動ack
  315. *------------------------------------------
  316. */
  317. int chrif_changemapserverack(int fd)
  318. {
  319. struct map_session_data *sd;
  320. RFIFOHEAD(fd);
  321. sd = map_id2sd(RFIFOL(fd,2));
  322. if (sd == NULL || sd->status.char_id != RFIFOL(fd,14))
  323. return -1;
  324. if (RFIFOL(fd,6) == 1) {
  325. if (battle_config.error_log)
  326. ShowError("map server change failed.\n");
  327. pc_authfail(sd);
  328. return 0;
  329. }
  330. clif_changemapserver(sd, (char*)mapindex_id2name(RFIFOW(fd,18)), RFIFOW(fd,20), RFIFOW(fd,22), RFIFOL(fd,24), RFIFOW(fd,28));
  331. return 0;
  332. }
  333. /*==========================================
  334. *
  335. *------------------------------------------
  336. */
  337. int chrif_connectack(int fd)
  338. {
  339. RFIFOHEAD(fd);
  340. if (RFIFOB(fd,2)) {
  341. ShowFatalError("Connection to char-server failed %d.\n", RFIFOB(fd,2));
  342. exit(1);
  343. }
  344. ShowStatus("Successfully logged on to Char Server (Connection: '"CL_WHITE"%d"CL_RESET"').\n",fd);
  345. chrif_state = 1;
  346. chrif_connected=1;
  347. chrif_sendmap(fd);
  348. ShowStatus("Event '"CL_WHITE"OnCharIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnCharIfInit"));
  349. ShowStatus("Event '"CL_WHITE"OnInterIfInit"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInit"));
  350. if(!char_init_done) {
  351. char_init_done = 1;
  352. ShowStatus("Event '"CL_WHITE"OnInterIfInitOnce"CL_RESET"' executed with '"CL_WHITE"%d"CL_RESET"' NPCs.\n", npc_event_doall("OnInterIfInitOnce"));
  353. }
  354. return 0;
  355. }
  356. /*==========================================
  357. *
  358. *------------------------------------------
  359. */
  360. int chrif_sendmapack(int fd)
  361. {
  362. RFIFOHEAD(fd);
  363. if (RFIFOB(fd,2)) {
  364. ShowFatalError("chrif : send map list to char server failed %d\n", RFIFOB(fd,2));
  365. exit(1);
  366. }
  367. memcpy(wisp_server_name, RFIFOP(fd,3), NAME_LENGTH);
  368. ShowStatus("Map sending complete. Map Server is now online.\n");
  369. chrif_state = 2;
  370. //If there are players online, send them to the char-server. [Skotlex]
  371. if (!kick_on_disconnect)
  372. send_users_tochar(-1, gettick(), 0, 0);
  373. //Re-save any storages that were modified in the disconnection time. [Skotlex]
  374. do_reconnect_storage();
  375. return 0;
  376. }
  377. /*==========================================
  378. * Request sc_data from charserver [Skotlex]
  379. *------------------------------------------
  380. */
  381. int chrif_scdata_request(int account_id, int char_id)
  382. {
  383. #ifdef ENABLE_SC_SAVING
  384. #ifndef TXT_ONLY
  385. if (charsave_method)
  386. return charsave_load_scdata(account_id, char_id);
  387. #endif
  388. chrif_check(-1);
  389. WFIFOHEAD(char_fd, 10);
  390. WFIFOW(char_fd, 0) = 0x2afc;
  391. WFIFOL(char_fd, 2) = account_id;
  392. WFIFOL(char_fd, 6) = char_id;
  393. WFIFOSET(char_fd,10);
  394. #endif
  395. return 0;
  396. }
  397. /*==========================================
  398. * new auth system [Kevin]
  399. *------------------------------------------
  400. */
  401. void chrif_authreq(struct map_session_data *sd)
  402. {
  403. struct auth_node *auth_data;
  404. auth_data=idb_get(auth_db, sd->bl.id);
  405. if(auth_data) {
  406. if(auth_data->char_dat &&
  407. auth_data->account_id== sd->bl.id &&
  408. auth_data->login_id1 == sd->login_id1)
  409. { //auth ok
  410. pc_authok(sd, auth_data->login_id2, auth_data->connect_until_time, auth_data->char_dat);
  411. chrif_scdata_request(auth_data->account_id, auth_data->char_dat->char_id);
  412. } else { //auth failed
  413. pc_authfail(sd);
  414. chrif_char_offline(sd); //Set him offline, the char server likely has it set as online already.
  415. }
  416. if (auth_data->char_dat)
  417. aFree(auth_data->char_dat);
  418. idb_remove(auth_db, sd->bl.id);
  419. } else { //data from char server has not arrived yet.
  420. auth_data = aCalloc(1,sizeof(struct auth_node));
  421. auth_data->sd = sd;
  422. auth_data->fd = sd->fd;
  423. auth_data->account_id = sd->bl.id;
  424. auth_data->login_id1 = sd->login_id1;
  425. auth_data->node_created = gettick();
  426. uidb_put(auth_db, sd->bl.id, auth_data);
  427. }
  428. return;
  429. }
  430. //character selected, insert into auth db
  431. void chrif_authok(int fd) {
  432. struct auth_node *auth_data;
  433. RFIFOHEAD(fd);
  434. if (map_id2sd(RFIFOL(fd, 4)) != NULL)
  435. //Someone with this account is already in! Do not store the info to prevent possible sync exploits. [Skotlex]
  436. return;
  437. if ((auth_data =uidb_get(auth_db, RFIFOL(fd, 4))) != NULL)
  438. { //Is the character already awaiting authorization?
  439. if (auth_data->sd)
  440. {
  441. //First, check to see if the session data still exists (avoid dangling pointers)
  442. if(session[auth_data->fd] && session[auth_data->fd]->session_data == auth_data->sd)
  443. {
  444. if (auth_data->char_dat == NULL &&
  445. auth_data->account_id == RFIFOL(fd, 4) &&
  446. auth_data->login_id1 == RFIFOL(fd, 8))
  447. { //Auth Ok
  448. pc_authok(auth_data->sd, RFIFOL(fd, 16), RFIFOL(fd, 12), (struct mmo_charstatus*)RFIFOP(fd, 20));
  449. chrif_scdata_request(auth_data->account_id, auth_data->sd->status.char_id);
  450. } else { //Auth Failed
  451. pc_authfail(auth_data->sd);
  452. chrif_char_offline(auth_data->sd); //Set him offline, the char server likely has it set as online already.
  453. }
  454. } //else: Character no longer exists, just go through.
  455. }
  456. //Delete the data of this node...
  457. if (auth_data->char_dat)
  458. aFree (auth_data->char_dat);
  459. uidb_remove(auth_db, RFIFOL(fd, 4));
  460. return;
  461. }
  462. // Awaiting for client to connect.
  463. auth_data = (struct auth_node *)aCalloc(1,sizeof(struct auth_node));
  464. auth_data->char_dat = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  465. auth_data->account_id=RFIFOL(fd, 4);
  466. auth_data->login_id1=RFIFOL(fd, 8);
  467. auth_data->connect_until_time=RFIFOL(fd, 12);
  468. auth_data->login_id2=RFIFOL(fd, 16);
  469. memcpy(auth_data->char_dat,RFIFOP(fd, 20),sizeof(struct mmo_charstatus));
  470. auth_data->node_created=gettick();
  471. uidb_put(auth_db, RFIFOL(fd, 4), auth_data);
  472. }
  473. int auth_db_cleanup_sub(DBKey key,void *data,va_list ap)
  474. {
  475. struct auth_node *node=(struct auth_node*)data;
  476. if(DIFF_TICK(gettick(),node->node_created)>30000) {
  477. ShowNotice("Character (aid: %d) not authed within 30 seconds of character select!\n", node->account_id);
  478. if (node->char_dat)
  479. aFree(node->char_dat);
  480. db_remove(auth_db, key);
  481. return 1;
  482. }
  483. return 0;
  484. }
  485. int auth_db_cleanup(int tid, unsigned int tick, int id, int data) {
  486. auth_db->foreach(auth_db, auth_db_cleanup_sub);
  487. return 0;
  488. }
  489. /*==========================================
  490. *
  491. *------------------------------------------
  492. */
  493. int chrif_charselectreq(struct map_session_data *sd)
  494. {
  495. int i, s_ip;
  496. nullpo_retr(-1, sd);
  497. if( !sd || !sd->bl.id || !sd->login_id1 )
  498. return -1;
  499. chrif_check(-1);
  500. s_ip = 0;
  501. for(i = 0; i < fd_max; i++)
  502. if (session[i] && session[i]->session_data == sd) {
  503. s_ip = session[i]->client_addr.sin_addr.s_addr;
  504. break;
  505. }
  506. WFIFOHEAD(char_fd, 18);
  507. WFIFOW(char_fd, 0) = 0x2b02;
  508. WFIFOL(char_fd, 2) = sd->bl.id;
  509. WFIFOL(char_fd, 6) = sd->login_id1;
  510. WFIFOL(char_fd,10) = sd->login_id2;
  511. WFIFOL(char_fd,14) = s_ip;
  512. WFIFOSET(char_fd,18);
  513. return 0;
  514. }
  515. /*==========================================
  516. * キャラ名問い合わせ
  517. *------------------------------------------
  518. */
  519. int chrif_searchcharid(int char_id)
  520. {
  521. if( !char_id )
  522. return -1;
  523. chrif_check(-1);
  524. WFIFOHEAD(char_fd, 6);
  525. WFIFOW(char_fd,0) = 0x2b08;
  526. WFIFOL(char_fd,2) = char_id;
  527. WFIFOSET(char_fd,6);
  528. return 0;
  529. }
  530. /*==========================================
  531. * GMに変化要求
  532. *------------------------------------------
  533. */
  534. int chrif_changegm(int id, const char *pass, int len)
  535. {
  536. if (battle_config.etc_log)
  537. ShowInfo("chrif_changegm: account: %d, password: '%s'.\n", id, pass);
  538. chrif_check(-1);
  539. WFIFOHEAD(char_fd, len + 8);
  540. WFIFOW(char_fd,0) = 0x2b0a;
  541. WFIFOW(char_fd,2) = len + 8;
  542. WFIFOL(char_fd,4) = id;
  543. memcpy(WFIFOP(char_fd,8), pass, len);
  544. WFIFOSET(char_fd, len + 8);
  545. return 0;
  546. }
  547. /*==========================================
  548. * Change Email
  549. *------------------------------------------
  550. */
  551. int chrif_changeemail(int id, const char *actual_email, const char *new_email)
  552. {
  553. if (battle_config.etc_log)
  554. ShowInfo("chrif_changeemail: account: %d, actual_email: '%s', new_email: '%s'.\n", id, actual_email, new_email);
  555. chrif_check(-1);
  556. WFIFOHEAD(char_fd, 86);
  557. WFIFOW(char_fd,0) = 0x2b0c;
  558. WFIFOL(char_fd,2) = id;
  559. memcpy(WFIFOP(char_fd,6), actual_email, 40);
  560. memcpy(WFIFOP(char_fd,46), new_email, 40);
  561. WFIFOSET(char_fd,86);
  562. return 0;
  563. }
  564. /*==========================================
  565. * Send message to char-server with a character name to do some operations (by Yor)
  566. * Used to ask Char-server about a character name to have the account number to modify account file in login-server.
  567. * type of operation:
  568. * 1: block
  569. * 2: ban
  570. * 3: unblock
  571. * 4: unban
  572. * 5: changesex
  573. *------------------------------------------
  574. */
  575. int chrif_char_ask_name(int id, char * character_name, short operation_type, int year, int month, int day, int hour, int minute, int second)
  576. {
  577. chrif_check(-1);
  578. WFIFOHEAD(char_fd, 44);
  579. WFIFOW(char_fd, 0) = 0x2b0e;
  580. WFIFOL(char_fd, 2) = id; // account_id of who ask (for answer) -1 if nobody
  581. memcpy(WFIFOP(char_fd,6), character_name, NAME_LENGTH);
  582. WFIFOW(char_fd, 30) = operation_type; // type of operation
  583. if (operation_type == 2) {
  584. WFIFOW(char_fd, 32) = year;
  585. WFIFOW(char_fd, 34) = month;
  586. WFIFOW(char_fd, 36) = day;
  587. WFIFOW(char_fd, 38) = hour;
  588. WFIFOW(char_fd, 40) = minute;
  589. WFIFOW(char_fd, 42) = second;
  590. }
  591. ShowInfo("chrif : sended 0x2b0e\n");
  592. WFIFOSET(char_fd,44);
  593. return 0;
  594. }
  595. /*==========================================
  596. * 性別変化要求
  597. *------------------------------------------
  598. */
  599. int chrif_changesex(int id, int sex) {
  600. chrif_check(-1);
  601. WFIFOHEAD(char_fd, 9);
  602. WFIFOW(char_fd,0) = 0x2b11;
  603. WFIFOW(char_fd,2) = 9;
  604. WFIFOL(char_fd,4) = id;
  605. WFIFOB(char_fd,8) = sex;
  606. ShowInfo("chrif : sent 0x3000(changesex)\n");
  607. WFIFOSET(char_fd,9);
  608. return 0;
  609. }
  610. /*==========================================
  611. * Answer after a request about a character name to do some operations (by Yor)
  612. * Used to answer of chrif_char_ask_name.
  613. * type of operation:
  614. * 1: block
  615. * 2: ban
  616. * 3: unblock
  617. * 4: unban
  618. * 5: changesex
  619. * type of answer:
  620. * 0: login-server resquest done
  621. * 1: player not found
  622. * 2: gm level too low
  623. * 3: login-server offline
  624. *------------------------------------------
  625. */
  626. int chrif_char_ask_name_answer(int fd)
  627. {
  628. int acc;
  629. struct map_session_data *sd;
  630. char output[256];
  631. char player_name[NAME_LENGTH];
  632. RFIFOHEAD(fd);
  633. acc = RFIFOL(fd,2); // account_id of who has asked (-1 if nobody)
  634. memcpy(player_name, RFIFOP(fd,6), NAME_LENGTH-1);
  635. player_name[NAME_LENGTH-1] = '\0';
  636. sd = map_id2sd(acc);
  637. if (acc >= 0 && sd != NULL) {
  638. if (RFIFOW(fd, 32) == 1) // player not found
  639. sprintf(output, "The player '%s' doesn't exist.", player_name);
  640. else {
  641. switch(RFIFOW(fd, 30)) {
  642. case 1: // block
  643. switch(RFIFOW(fd, 32)) {
  644. case 0: // login-server resquest done
  645. sprintf(output, "Login-server has been asked to block the player '%s'.", player_name);
  646. break;
  647. //case 1: // player not found
  648. case 2: // gm level too low
  649. sprintf(output, "Your GM level don't authorise you to block the player '%s'.", player_name);
  650. break;
  651. case 3: // login-server offline
  652. sprintf(output, "Login-server is offline. Impossible to block the the player '%s'.", player_name);
  653. break;
  654. }
  655. break;
  656. case 2: // ban
  657. switch(RFIFOW(fd, 32)) {
  658. case 0: // login-server resquest done
  659. sprintf(output, "Login-server has been asked to ban the player '%s'.", player_name);
  660. break;
  661. //case 1: // player not found
  662. case 2: // gm level too low
  663. sprintf(output, "Your GM level don't authorise you to ban the player '%s'.", player_name);
  664. break;
  665. case 3: // login-server offline
  666. sprintf(output, "Login-server is offline. Impossible to ban the the player '%s'.", player_name);
  667. break;
  668. }
  669. break;
  670. case 3: // unblock
  671. switch(RFIFOW(fd, 32)) {
  672. case 0: // login-server resquest done
  673. sprintf(output, "Login-server has been asked to unblock the player '%s'.", player_name);
  674. break;
  675. //case 1: // player not found
  676. case 2: // gm level too low
  677. sprintf(output, "Your GM level don't authorise you to unblock the player '%s'.", player_name);
  678. break;
  679. case 3: // login-server offline
  680. sprintf(output, "Login-server is offline. Impossible to unblock the the player '%s'.", player_name);
  681. break;
  682. }
  683. break;
  684. case 4: // unban
  685. switch(RFIFOW(fd, 32)) {
  686. case 0: // login-server resquest done
  687. sprintf(output, "Login-server has been asked to unban the player '%s'.", player_name);
  688. break;
  689. //case 1: // player not found
  690. case 2: // gm level too low
  691. sprintf(output, "Your GM level don't authorise you to unban the player '%s'.", player_name);
  692. break;
  693. case 3: // login-server offline
  694. sprintf(output, "Login-server is offline. Impossible to unban the the player '%s'.", player_name);
  695. break;
  696. }
  697. break;
  698. case 5: // changesex
  699. switch(RFIFOW(fd, 32)) {
  700. case 0: // login-server resquest done
  701. sprintf(output, "Login-server has been asked to change the sex of the player '%s'.", player_name);
  702. break;
  703. //case 1: // player not found
  704. case 2: // gm level too low
  705. sprintf(output, "Your GM level don't authorise you to change the sex of the player '%s'.", player_name);
  706. break;
  707. case 3: // login-server offline
  708. sprintf(output, "Login-server is offline. Impossible to change the sex of the the player '%s'.", player_name);
  709. break;
  710. }
  711. break;
  712. }
  713. }
  714. if (output[0] != '\0') {
  715. output[sizeof(output)-1] = '\0';
  716. clif_displaymessage(sd->fd, output);
  717. }
  718. } else
  719. ShowError("chrif_char_ask_name_answer failed - player not online.\n");
  720. return 0;
  721. }
  722. /*==========================================
  723. * End of GM change (@GM) (modified by Yor)
  724. *------------------------------------------
  725. */
  726. int chrif_changedgm(int fd)
  727. {
  728. int acc, level;
  729. struct map_session_data *sd = NULL;
  730. RFIFOHEAD(fd);
  731. acc = RFIFOL(fd,2);
  732. level = RFIFOL(fd,6);
  733. sd = map_id2sd(acc);
  734. if (battle_config.etc_log)
  735. ShowNotice("chrif_changedgm: account: %d, GM level 0 -> %d.\n", acc, level);
  736. if (sd != NULL) {
  737. if (level > 0)
  738. clif_displaymessage(sd->fd, "GM modification success.");
  739. else
  740. clif_displaymessage(sd->fd, "Failure of GM modification.");
  741. }
  742. return 0;
  743. }
  744. /*==========================================
  745. * 性別変化終了 (modified by Yor)
  746. *------------------------------------------
  747. */
  748. int chrif_changedsex(int fd)
  749. {
  750. int acc, sex, i;
  751. struct map_session_data *sd;
  752. RFIFOHEAD(fd);
  753. acc = RFIFOL(fd,2);
  754. sex = RFIFOL(fd,6);
  755. if (battle_config.etc_log)
  756. ShowNotice("chrif_changedsex %d.\n", acc);
  757. sd = map_id2sd(acc);
  758. if (acc > 0) {
  759. if (sd != NULL && sd->status.sex != sex) {
  760. if (sd->status.sex == 0) {
  761. sd->status.sex = 1;
  762. sd->sex = 1;
  763. } else if (sd->status.sex == 1) {
  764. sd->status.sex = 0;
  765. sd->sex = 0;
  766. }
  767. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  768. for (i = 0; i < MAX_INVENTORY; i++) {
  769. if (sd->status.inventory[i].nameid && sd->status.inventory[i].equip)
  770. pc_unequipitem((struct map_session_data*)sd, i, 2);
  771. }
  772. // reset skill of some job
  773. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER) {
  774. // remove specifical skills of Bard classes
  775. for(i = 315; i <= 322; i++) {
  776. if (sd->status.skill[i].id > 0 && !sd->status.skill[i].flag) {
  777. if (sd->status.skill_point > USHRT_MAX - sd->status.skill[i].lv)
  778. sd->status.skill_point = USHRT_MAX;
  779. else
  780. sd->status.skill_point += sd->status.skill[i].lv;
  781. sd->status.skill[i].id = 0;
  782. sd->status.skill[i].lv = 0;
  783. }
  784. }
  785. // remove specifical skills of Dancer classes
  786. for(i = 323; i <= 330; i++) {
  787. if (sd->status.skill[i].id > 0 && !sd->status.skill[i].flag) {
  788. if (sd->status.skill_point > USHRT_MAX - sd->status.skill[i].lv)
  789. sd->status.skill_point = USHRT_MAX;
  790. else
  791. sd->status.skill_point += sd->status.skill[i].lv;
  792. sd->status.skill[i].id = 0;
  793. sd->status.skill[i].lv = 0;
  794. }
  795. }
  796. clif_updatestatus(sd, SP_SKILLPOINT);
  797. // change job if necessary
  798. if (sd->status.sex) //Changed from Dancer
  799. sd->status.class_ -= 1;
  800. else //Changed from Bard
  801. sd->status.class_ += 1;
  802. //sd->class_ needs not be updated as both Dancer/Bard are the same.
  803. }
  804. // save character
  805. //chrif_save(sd,1); Character will be saved on session closed -> map_quit
  806. sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
  807. // 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)
  808. clif_displaymessage(sd->fd, "Your sex has been changed (need disconnection by the server)...");
  809. clif_setwaitclose(sd->fd); // forced to disconnect for the change
  810. }
  811. } else {
  812. if (sd != NULL) {
  813. ShowError("chrif_changedsex failed.\n");
  814. }
  815. }
  816. return 0;
  817. }
  818. /*==========================================
  819. * 離婚情報同期要求
  820. *------------------------------------------
  821. */
  822. int chrif_divorce(int char_id, int partner_id)
  823. {
  824. struct map_session_data *sd = NULL;
  825. if (!char_id || !partner_id)
  826. return 0;
  827. nullpo_retr(0, sd = map_nick2sd(map_charid2nick(partner_id)));
  828. if (sd->status.partner_id == char_id) {
  829. int i;
  830. //離婚(相方は既にキャラが消えている筈なので)
  831. sd->status.partner_id = 0;
  832. //相方の結婚指輪を剥奪
  833. for(i = 0; i < MAX_INVENTORY; i++)
  834. if (sd->status.inventory[i].nameid == WEDDING_RING_M || sd->status.inventory[i].nameid == WEDDING_RING_F)
  835. pc_delitem(sd, i, 1, 0);
  836. }
  837. return 0;
  838. }
  839. /*==========================================
  840. * Disconnection of a player (account has been deleted in login-server) by [Yor]
  841. *------------------------------------------
  842. */
  843. int chrif_accountdeletion(int fd)
  844. {
  845. int acc;
  846. struct map_session_data *sd;
  847. RFIFOHEAD(fd);
  848. acc = RFIFOL(fd,2);
  849. if (battle_config.etc_log)
  850. ShowNotice("chrif_accountdeletion %d.\n", acc);
  851. sd = map_id2sd(acc);
  852. if (acc > 0) {
  853. if (sd != NULL) {
  854. sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
  855. clif_displaymessage(sd->fd, "Your account has been deleted (disconnection)...");
  856. clif_setwaitclose(sd->fd); // forced to disconnect for the change
  857. }
  858. } else {
  859. if (sd != NULL)
  860. ShowError("chrif_accountdeletion failed - player not online.\n");
  861. }
  862. return 0;
  863. }
  864. /*==========================================
  865. * Disconnection of a player (account has been banned of has a status, from login-server) by [Yor]
  866. *------------------------------------------
  867. */
  868. int chrif_accountban(int fd)
  869. {
  870. int acc;
  871. struct map_session_data *sd;
  872. RFIFOHEAD(fd);
  873. acc = RFIFOL(fd,2);
  874. if (battle_config.etc_log)
  875. ShowNotice("chrif_accountban %d.\n", acc);
  876. sd = map_id2sd(acc);
  877. if (acc > 0) {
  878. if (sd != NULL) {
  879. sd->login_id1++; // change identify, because if player come back in char within the 5 seconds, he can change its characters
  880. if (RFIFOB(fd,6) == 0) { // 0: change of statut, 1: ban
  881. switch (RFIFOL(fd,7)) { // status or final date of a banishment
  882. case 1: // 0 = Unregistered ID
  883. clif_displaymessage(sd->fd, "Your account has 'Unregistered'.");
  884. break;
  885. case 2: // 1 = Incorrect Password
  886. clif_displaymessage(sd->fd, "Your account has an 'Incorrect Password'...");
  887. break;
  888. case 3: // 2 = This ID is expired
  889. clif_displaymessage(sd->fd, "Your account has expired.");
  890. break;
  891. case 4: // 3 = Rejected from Server
  892. clif_displaymessage(sd->fd, "Your account has been rejected from server.");
  893. break;
  894. case 5: // 4 = You have been blocked by the GM Team
  895. clif_displaymessage(sd->fd, "Your account has been blocked by the GM Team.");
  896. break;
  897. case 6: // 5 = Your Game's EXE file is not the latest version
  898. clif_displaymessage(sd->fd, "Your Game's EXE file is not the latest version.");
  899. break;
  900. case 7: // 6 = Your are Prohibited to log in until %s
  901. clif_displaymessage(sd->fd, "Your account has been prohibited to log in.");
  902. break;
  903. case 8: // 7 = Server is jammed due to over populated
  904. clif_displaymessage(sd->fd, "Server is jammed due to over populated.");
  905. break;
  906. case 9: // 8 = No MSG (actually, all states after 9 except 99 are No MSG, use only this)
  907. clif_displaymessage(sd->fd, "Your account has not more authorised.");
  908. break;
  909. case 100: // 99 = This ID has been totally erased
  910. clif_displaymessage(sd->fd, "Your account has been totally erased.");
  911. break;
  912. default:
  913. clif_displaymessage(sd->fd, "Your account has not more authorised.");
  914. break;
  915. }
  916. } else if (RFIFOB(fd,6) == 1) { // 0: change of statut, 1: ban
  917. time_t timestamp;
  918. char tmpstr[2048];
  919. timestamp = (time_t)RFIFOL(fd,7); // status or final date of a banishment
  920. strcpy(tmpstr, "Your account has been banished until ");
  921. strftime(tmpstr + strlen(tmpstr), 24, "%d-%m-%Y %H:%M:%S", localtime(&timestamp));
  922. clif_displaymessage(sd->fd, tmpstr);
  923. }
  924. clif_setwaitclose(sd->fd); // forced to disconnect for the change
  925. }
  926. } else {
  927. if (sd != NULL)
  928. ShowError("chrif_accountban failed - player not online.\n");
  929. }
  930. return 0;
  931. }
  932. //Disconnect the player out of the game, simple packet
  933. //packet.w AID.L WHY.B 2+4+1 = 7byte
  934. int chrif_disconnectplayer(int fd){
  935. struct map_session_data *sd;
  936. RFIFOHEAD(fd);
  937. sd = map_id2sd(RFIFOL(fd, 2));
  938. if(sd == NULL){
  939. return -1;
  940. }
  941. if (!sd->fd)
  942. { //No connection
  943. if (sd->state.autotrade)
  944. map_quit(sd); //Remove it.
  945. //Else we don't remove it because the char should have a timer to remove the player because it force-quit before,
  946. //and we don't want them kicking their previous instance before the 10 secs penalty time passes. [Skotlex]
  947. return 0;
  948. }
  949. switch(RFIFOB(fd, 6)){
  950. //clif_authfail_fd
  951. case 1: //server closed
  952. clif_authfail_fd(sd->fd, 1);
  953. break;
  954. case 2: //someone else logged in
  955. clif_authfail_fd(sd->fd, 2);
  956. break;
  957. case 3: //server overpopulated
  958. clif_authfail_fd(sd->fd, 4);
  959. break;
  960. case 4: //out of time payd for .. (avail)
  961. clif_authfail_fd(sd->fd, 10);
  962. break;
  963. case 5: //forced to dc by gm
  964. clif_authfail_fd(sd->fd, 15);
  965. break;
  966. }
  967. return 0;
  968. }
  969. /*==========================================
  970. * Request to reload GM accounts and their levels: send to char-server by [Yor]
  971. *------------------------------------------
  972. */
  973. int chrif_reloadGMdb(void)
  974. {
  975. chrif_check(-1);
  976. WFIFOHEAD(char_fd, 2);
  977. WFIFOW(char_fd,0) = 0x2af7;
  978. WFIFOSET(char_fd, 2);
  979. return 0;
  980. }
  981. /*==========================================
  982. * Receiving GM accounts and their levels from char-server by [Yor]
  983. *------------------------------------------
  984. */
  985. int chrif_recvgmaccounts(int fd)
  986. {
  987. ShowInfo("From login-server: receiving information of '"CL_WHITE"%d"CL_RESET"' GM accounts.\n", pc_read_gm_account(fd));
  988. return 0;
  989. }
  990. /*==========================================
  991. * Request/Receive top 10 Fame character list
  992. *------------------------------------------
  993. */
  994. int chrif_updatefamelist(struct map_session_data *sd)
  995. {
  996. char type;
  997. chrif_check(-1);
  998. switch(sd->class_&MAPID_UPPERMASK) {
  999. case MAPID_BLACKSMITH:
  1000. type = 1;
  1001. break;
  1002. case MAPID_ALCHEMIST:
  1003. type = 2;
  1004. break;
  1005. case MAPID_TAEKWON:
  1006. type = 3;
  1007. break;
  1008. default:
  1009. return 0;
  1010. }
  1011. WFIFOHEAD(char_fd, 12);
  1012. WFIFOW(char_fd, 0) = 0x2b10;
  1013. WFIFOL(char_fd, 2) = sd->char_id;
  1014. WFIFOL(char_fd, 6) = sd->status.fame;
  1015. WFIFOB(char_fd, 10) = type;
  1016. WFIFOB(char_fd, 11) = pc_famerank(sd->char_id, sd->class_&MAPID_UPPERMASK);
  1017. WFIFOSET(char_fd, 12);
  1018. return 0;
  1019. }
  1020. int chrif_buildfamelist(void)
  1021. {
  1022. chrif_check(-1);
  1023. WFIFOHEAD(char_fd, 2);
  1024. WFIFOW(char_fd, 0) = 0x2b1a;
  1025. WFIFOSET(char_fd, 2);
  1026. return 0;
  1027. }
  1028. int chrif_recvfamelist(int fd)
  1029. {
  1030. int num, size;
  1031. int total = 0, len = 8;
  1032. RFIFOHEAD(fd);
  1033. memset (smith_fame_list, 0, sizeof(smith_fame_list));
  1034. memset (chemist_fame_list, 0, sizeof(chemist_fame_list));
  1035. memset (taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  1036. size = RFIFOW(fd, 6); //Blacksmith block size
  1037. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1038. memcpy(&smith_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1039. len += sizeof(struct fame_list);
  1040. }
  1041. total += num;
  1042. size = RFIFOW(fd, 4); //Alchemist block size
  1043. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1044. memcpy(&chemist_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1045. len += sizeof(struct fame_list);
  1046. }
  1047. total += num;
  1048. size = RFIFOW(fd, 2); //Total packet length
  1049. for (num = 0; len < size && num < MAX_FAME_LIST; num++) {
  1050. memcpy(&taekwon_fame_list[num], RFIFOP(fd,len), sizeof(struct fame_list));
  1051. len += sizeof(struct fame_list);
  1052. }
  1053. total += num;
  1054. ShowInfo("Received Fame List of '"CL_WHITE"%d"CL_RESET"' characters.\n", total);
  1055. return 0;
  1056. }
  1057. int chrif_save_scdata(struct map_session_data *sd)
  1058. { //parses the sc_data of the player and sends it to the char-server for saving. [Skotlex]
  1059. #ifdef ENABLE_SC_SAVING
  1060. int i, count=0;
  1061. unsigned int tick;
  1062. struct status_change_data data;
  1063. struct TimerData *timer;
  1064. if (sd->state.finalsave) //Character was already saved?
  1065. return -1;
  1066. #ifndef TXT_ONLY
  1067. if(charsave_method) //New 'Local' save
  1068. {
  1069. charsave_save_scdata(sd->status.account_id, sd->status.char_id, &sd->sc, MAX_STATUSCHANGE);
  1070. return 0;
  1071. }
  1072. #endif
  1073. chrif_check(-1);
  1074. tick = gettick();
  1075. WFIFOHEAD(char_fd, 14 + SC_MAX*sizeof(struct status_change_data));
  1076. WFIFOW(char_fd,0) = 0x2b1c;
  1077. WFIFOL(char_fd,4) = sd->status.account_id;
  1078. WFIFOL(char_fd,8) = sd->status.char_id;
  1079. for (i = 0; i < SC_MAX; i++)
  1080. {
  1081. if (sd->sc.data[i].timer == -1)
  1082. continue;
  1083. timer = get_timer(sd->sc.data[i].timer);
  1084. if (timer == NULL || timer->func != status_change_timer || DIFF_TICK(timer->tick,tick) < 0)
  1085. continue;
  1086. data.tick = DIFF_TICK(timer->tick,tick); //Duration that is left before ending.
  1087. data.type = i;
  1088. data.val1 = sd->sc.data[i].val1;
  1089. data.val2 = sd->sc.data[i].val2;
  1090. data.val3 = sd->sc.data[i].val3;
  1091. data.val4 = sd->sc.data[i].val4;
  1092. memcpy(WFIFOP(char_fd,14 +count*sizeof(struct status_change_data)),
  1093. &data, sizeof(struct status_change_data));
  1094. count++;
  1095. }
  1096. if (count == 0)
  1097. return 0; //Nothing to save.
  1098. WFIFOW(char_fd,12) = count;
  1099. WFIFOW(char_fd,2) = 14 +count*sizeof(struct status_change_data); //Total packet size
  1100. WFIFOSET(char_fd,WFIFOW(char_fd,2));
  1101. #endif
  1102. return 0;
  1103. }
  1104. int chrif_load_scdata(int fd)
  1105. { //Retrieve and load sc_data for a player. [Skotlex]
  1106. #ifdef ENABLE_SC_SAVING
  1107. struct map_session_data *sd;
  1108. struct status_change_data data;
  1109. int aid, cid, i, count;
  1110. RFIFOHEAD(fd);
  1111. aid = RFIFOL(fd,4); //Player Account ID
  1112. cid = RFIFOL(fd,8); //Player Char ID
  1113. sd = map_id2sd(aid);
  1114. if (!sd)
  1115. {
  1116. ShowError("chrif_load_scdata: Player of AID %d not found!\n", aid);
  1117. return -1;
  1118. }
  1119. if (sd->status.char_id != cid)
  1120. {
  1121. ShowError("chrif_load_scdata: Receiving data for account %d, char id does not matches (%d != %d)!\n", aid, sd->status.char_id, cid);
  1122. return -1;
  1123. }
  1124. count = RFIFOW(fd,12); //sc_count
  1125. for (i = 0; i < count; i++)
  1126. {
  1127. memcpy(&data, RFIFOP(fd,14 + i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  1128. if (data.tick < 1)
  1129. { //Protection against invalid tick values. [Skotlex]
  1130. ShowWarning("chrif_load_scdata: Received invalid duration (%d ms) for status change %d (character %s)\n", data.tick, data.type, sd->status.name);
  1131. continue;
  1132. }
  1133. status_change_start(&sd->bl, data.type, 10000, data.val1, data.val2, data.val3, data.val4, data.tick, 15);
  1134. }
  1135. #endif
  1136. return 0;
  1137. }
  1138. /*==========================================
  1139. * Send rates and motd to char server [Wizputer]
  1140. *------------------------------------------
  1141. */
  1142. int chrif_ragsrvinfo(int base_rate, int job_rate, int drop_rate)
  1143. {
  1144. char buf[256];
  1145. FILE *fp;
  1146. int i;
  1147. chrif_check(-1);
  1148. WFIFOHEAD(char_fd, sizeof(buf) + 10);
  1149. WFIFOW(char_fd,0) = 0x2b16;
  1150. WFIFOW(char_fd,2) = base_rate;
  1151. WFIFOW(char_fd,4) = job_rate;
  1152. WFIFOW(char_fd,6) = drop_rate;
  1153. if ((fp = fopen(motd_txt, "r")) != NULL) {
  1154. if (fgets(buf, 250, fp) != NULL) {
  1155. for(i = 0; buf[i]; i++) {
  1156. if (buf[i] == '\r' || buf[i] == '\n') {
  1157. buf[i] = 0;
  1158. break;
  1159. }
  1160. }
  1161. WFIFOW(char_fd,8) = sizeof(buf) + 10;
  1162. memcpy(WFIFOP(char_fd,10), buf, sizeof(buf));
  1163. }
  1164. fclose(fp);
  1165. } else {
  1166. memset(buf, 0, sizeof(buf)); //No data found, send empty packets?
  1167. WFIFOW(char_fd,8) = sizeof(buf) + 10;
  1168. memcpy(WFIFOP(char_fd,10), buf, sizeof(buf));
  1169. }
  1170. WFIFOSET(char_fd,WFIFOW(char_fd,8));
  1171. return 0;
  1172. }
  1173. /*=========================================
  1174. * Tell char-server charcter disconnected [Wizputer]
  1175. *-----------------------------------------
  1176. */
  1177. int chrif_char_offline(struct map_session_data *sd)
  1178. {
  1179. chrif_check(-1);
  1180. WFIFOHEAD(char_fd, 10);
  1181. WFIFOW(char_fd,0) = 0x2b17;
  1182. WFIFOL(char_fd,2) = sd->status.char_id;
  1183. WFIFOL(char_fd,6) = sd->status.account_id;
  1184. WFIFOSET(char_fd,10);
  1185. return 0;
  1186. }
  1187. /*=========================================
  1188. * Tell char-server to reset all chars offline [Wizputer]
  1189. *-----------------------------------------
  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. */
  1202. int chrif_char_reset_offline(void) {
  1203. chrif_check(-1);
  1204. WFIFOHEAD(char_fd, 2);
  1205. WFIFOW(char_fd,0) = 0x2b18;
  1206. WFIFOSET(char_fd,2);
  1207. return 0;
  1208. }
  1209. /*=========================================
  1210. * Tell char-server charcter is online [Wizputer]
  1211. *-----------------------------------------
  1212. */
  1213. int chrif_char_online(struct map_session_data *sd)
  1214. {
  1215. chrif_check(-1);
  1216. WFIFOHEAD(char_fd, 10);
  1217. WFIFOW(char_fd,0) = 0x2b19;
  1218. WFIFOL(char_fd,2) = sd->status.char_id;
  1219. WFIFOL(char_fd,6) = sd->status.account_id;
  1220. WFIFOSET(char_fd,10);
  1221. return 0;
  1222. }
  1223. /*==========================================
  1224. *
  1225. *------------------------------------------
  1226. */
  1227. int chrif_disconnect_sub(struct map_session_data* sd,va_list va) {
  1228. if (sd->fd)
  1229. clif_authfail_fd(sd->fd,1);
  1230. else
  1231. map_quit(sd);
  1232. return 0;
  1233. }
  1234. int chrif_disconnect(int fd) {
  1235. if(fd == char_fd) {
  1236. char_fd = 0;
  1237. ShowWarning("Map Server disconnected from Char Server.\n\n");
  1238. if (kick_on_disconnect)
  1239. clif_foreachclient(chrif_disconnect_sub);
  1240. chrif_connected = 0;
  1241. other_mapserver_count=0; //Reset counter. We receive ALL maps from all map-servers on reconnect.
  1242. map_eraseallipport();
  1243. // 倉庫キャッシュを消す
  1244. if (kick_on_disconnect)
  1245. { //Do not clean the storage if players are gonna be left inside. [Skotlex]
  1246. do_final_storage();
  1247. do_init_storage();
  1248. }
  1249. //Attempt to reconnect in a second. [Skotlex]
  1250. add_timer(gettick() + 1000, check_connect_char_server, 0, 0);
  1251. }
  1252. return 0;
  1253. }
  1254. void chrif_update_ip(int fd){
  1255. unsigned long new_ip;
  1256. new_ip = resolve_hostbyname(char_ip_str, NULL, NULL);
  1257. if (new_ip && new_ip != char_ip)
  1258. char_ip = new_ip; //Update char_ip
  1259. new_ip = clif_refresh_ip();
  1260. if (!new_ip) return; //No change
  1261. WFIFOW(fd, 0) = 0x2736;
  1262. WFIFOL(fd, 2) = new_ip;
  1263. WFIFOSET(fd, 6);
  1264. }
  1265. /*==========================================
  1266. *
  1267. *------------------------------------------
  1268. */
  1269. int chrif_parse(int fd)
  1270. {
  1271. int packet_len, cmd;
  1272. // only char-server can have an access to here.
  1273. // so, if it isn't the char-server, we disconnect the session (fd != char_fd).
  1274. if (fd != char_fd || session[fd]->eof) {
  1275. if (fd == char_fd && chrif_connected == 1) {
  1276. chrif_disconnect (fd);
  1277. }
  1278. else if (fd != char_fd)
  1279. ShowDebug("chrif_parse: Disconnecting invalid session #%d (is not the char-server)\n", fd);
  1280. do_close(fd);
  1281. return 0;
  1282. }
  1283. while (RFIFOREST(fd) >= 2 && !session[fd]->eof) { //Infinite loop on broken pipe fix. [Skotlex]
  1284. RFIFOHEAD(fd);
  1285. cmd = RFIFOW(fd,0);
  1286. if (cmd < 0x2af8 || cmd >= 0x2af8 + (sizeof(packet_len_table) / sizeof(packet_len_table[0])) ||
  1287. packet_len_table[cmd-0x2af8] == 0) {
  1288. int r = intif_parse(fd); // intifに渡す
  1289. if (r == 1) continue; // intifで処理した
  1290. if (r == 2) return 0; // intifで処理したが、データが足りない
  1291. session[fd]->eof = 1;
  1292. ShowWarning("chrif_parse: session #%d, intif_parse failed -> disconnected.\n", fd);
  1293. return 0;
  1294. }
  1295. packet_len = packet_len_table[cmd-0x2af8];
  1296. if (packet_len == -1) {
  1297. if (RFIFOREST(fd) < 4)
  1298. return 0;
  1299. packet_len = RFIFOW(fd,2);
  1300. }
  1301. if ((int)RFIFOREST(fd) < packet_len)
  1302. return 0;
  1303. switch(cmd) {
  1304. case 0x2af9: chrif_connectack(fd); break;
  1305. case 0x2afb: chrif_sendmapack(fd); break;
  1306. case 0x2afd: chrif_authok(fd); break;
  1307. case 0x2b00: map_setusers(fd); break;
  1308. case 0x2b03: clif_charselectok(RFIFOL(fd,2)); break;
  1309. case 0x2b04: chrif_recvmap(fd); break;
  1310. case 0x2b06: chrif_changemapserverack(fd); break;
  1311. case 0x2b07: clif_updatemaxid(RFIFOL(fd,2), RFIFOL(fd,6)); break;
  1312. case 0x2b09: map_addchariddb(RFIFOL(fd,2), (char*)RFIFOP(fd,6)); break;
  1313. case 0x2b0b: chrif_changedgm(fd); break;
  1314. case 0x2b0d: chrif_changedsex(fd); break;
  1315. case 0x2b0f: chrif_char_ask_name_answer(fd); break;
  1316. case 0x2b12: chrif_divorce(RFIFOL(fd,2), RFIFOL(fd,6)); break;
  1317. case 0x2b13: chrif_accountdeletion(fd); break;
  1318. case 0x2b14: chrif_accountban(fd); break;
  1319. case 0x2b15: chrif_recvgmaccounts(fd); break;
  1320. case 0x2b1b: chrif_recvfamelist(fd); break;
  1321. case 0x2b1d: chrif_load_scdata(fd); break;
  1322. case 0x2b1e: chrif_update_ip(fd); break;
  1323. case 0x2b1f: chrif_disconnectplayer(fd); break;
  1324. case 0x2b20: chrif_removemap(fd); break; //Remove maps of a server [Sirius]
  1325. default:
  1326. if (battle_config.error_log)
  1327. ShowError("chrif_parse : unknown packet (session #%d): 0x%x. Disconnecting.\n", fd, cmd);
  1328. session[fd]->eof = 1;
  1329. return 0;
  1330. }
  1331. 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]
  1332. RFIFOSKIP(fd, packet_len);
  1333. }
  1334. return 0;
  1335. }
  1336. int send_usercount_tochar(int tid, unsigned int tick, int id, int data) {
  1337. int count;
  1338. static int last_count = 0;
  1339. chrif_check(-1);
  1340. map_getallusers(&count);
  1341. if (count == last_count) //No need to waste packets.
  1342. return 0;
  1343. last_count = count;
  1344. WFIFOHEAD(char_fd, 4);
  1345. WFIFOW(char_fd,0) = 0x2afe;
  1346. WFIFOW(char_fd,2) = count;
  1347. WFIFOSET(char_fd,4);
  1348. return 0;
  1349. }
  1350. /*==========================================
  1351. * timer関数
  1352. * 今このmap鯖に繋がっているクライアント人数をchar鯖へ送る
  1353. *------------------------------------------
  1354. */
  1355. int send_users_tochar(int tid, unsigned int tick, int id, int data) {
  1356. int count, users=0, i;
  1357. struct map_session_data **all_sd;
  1358. chrif_check(-1);
  1359. all_sd = map_getallusers(&count);
  1360. WFIFOHEAD(char_fd, 6+8*users);
  1361. WFIFOW(char_fd,0) = 0x2aff;
  1362. for (i = 0; i < count; i++) {
  1363. WFIFOL(char_fd,6+8*users) = all_sd[i]->status.account_id;
  1364. WFIFOL(char_fd,6+8*users+4) = all_sd[i]->status.char_id;
  1365. users++;
  1366. }
  1367. WFIFOW(char_fd,2) = 6 + 8 * users;
  1368. WFIFOW(char_fd,4) = users;
  1369. WFIFOSET(char_fd,6+8*users);
  1370. return 0;
  1371. }
  1372. /*==========================================
  1373. * timer関数
  1374. * char鯖との接続を確認し、もし切れていたら再度接続する
  1375. *------------------------------------------
  1376. */
  1377. int check_connect_char_server(int tid, unsigned int tick, int id, int data) {
  1378. static int displayed = 0;
  1379. if (char_fd <= 0 || session[char_fd] == NULL) {
  1380. if (!displayed) {
  1381. ShowStatus("Attempting to connect to Char Server. Please wait.\n");
  1382. displayed = 1;
  1383. }
  1384. chrif_state = 0;
  1385. char_fd = make_connection(char_ip, char_port);
  1386. if (char_fd == -1)
  1387. { //Attempt to connect later. [Skotlex]
  1388. char_fd = 0;
  1389. return 0;
  1390. }
  1391. session[char_fd]->func_parse = chrif_parse;
  1392. realloc_fifo(char_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  1393. chrif_connect(char_fd);
  1394. chrif_connected = (chrif_state == 2);
  1395. #ifndef TXT_ONLY
  1396. srvinfo = 0;
  1397. #endif /* not TXT_ONLY */
  1398. } else {
  1399. #ifndef TXT_ONLY
  1400. if (srvinfo == 0) {
  1401. chrif_ragsrvinfo(battle_config.base_exp_rate, battle_config.job_exp_rate, battle_config.item_rate_common);
  1402. srvinfo = 1;
  1403. }
  1404. #endif /* not TXT_ONLY */
  1405. /* There is no need, the connection is TCP, so the packet is assured to arrive unless the connection dies [Skotlex]
  1406. //If for some reason the next iteration (10 secs) we are still not connected,
  1407. //assume the packets got lost, so we need to resend them. [Skotlex]
  1408. if (chrif_state == 0)
  1409. chrif_connect(char_fd);
  1410. else if (chrif_state == 1)
  1411. chrif_sendmap(char_fd);
  1412. */
  1413. }
  1414. if (chrif_isconnect()) displayed = 0;
  1415. return 0;
  1416. }
  1417. int auth_db_final(DBKey k,void *d,va_list ap) {
  1418. struct auth_node *node=(struct auth_node*)d;
  1419. if (node->char_dat)
  1420. aFree(node->char_dat);
  1421. return 0;
  1422. }
  1423. /*==========================================
  1424. * 終了
  1425. *------------------------------------------
  1426. */
  1427. int do_final_chrif(void)
  1428. {
  1429. delete_session(char_fd);
  1430. auth_db->destroy(auth_db, auth_db_final);
  1431. return 0;
  1432. }
  1433. /*==========================================
  1434. *
  1435. *------------------------------------------
  1436. */
  1437. int do_init_chrif(void)
  1438. {
  1439. add_timer_func_list(check_connect_char_server, "check_connect_char_server");
  1440. add_timer_func_list(send_usercount_tochar, "send_usercount_tochar");
  1441. add_timer_func_list(send_users_tochar, "send_users_tochar");
  1442. add_timer_func_list(auth_db_cleanup, "auth_db_cleanup");
  1443. add_timer_interval(gettick() + 1000, check_connect_char_server, 0, 0, 10 * 1000);
  1444. #ifdef TXT_ONLY
  1445. //Txt needs this more frequently because it is used for the online.html file.
  1446. add_timer_interval(gettick() + 1000, send_users_tochar, 0, 0, UPDATE_INTERVAL);
  1447. #else
  1448. add_timer_interval(gettick() + 1000, send_users_tochar, 0, 0, CHECK_INTERVAL);
  1449. add_timer_interval(gettick() + 1000, send_usercount_tochar, 0, 0, UPDATE_INTERVAL);
  1450. #endif
  1451. add_timer_interval(gettick() + 1000, auth_db_cleanup, 0, 0, 30 * 1000);
  1452. auth_db = db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  1453. return 0;
  1454. }