chrif.c 48 KB

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