chrif.c 49 KB

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