char_mapif.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "char_mapif.hpp"
  4. #include <memory>
  5. #include <stdlib.h>
  6. #include <string.h> //memcpy
  7. #include <common/malloc.hpp>
  8. #include <common/showmsg.hpp>
  9. #include <common/socket.hpp>
  10. #include <common/sql.hpp>
  11. #include <common/strlib.hpp>
  12. #include <common/timer.hpp>
  13. #include <common/utilities.hpp>
  14. #include "char.hpp"
  15. #include "char_logif.hpp"
  16. #include "inter.hpp"
  17. using namespace rathena;
  18. /**
  19. * Packet send to all map-servers, attach to ourself
  20. * @param buf: packet to send in form of an array buffer
  21. * @param len: size of packet
  22. * @return : the number of map-serv the packet was sent to
  23. */
  24. int chmapif_sendall(unsigned char *buf, unsigned int len){
  25. int i, c;
  26. c = 0;
  27. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  28. int fd;
  29. if (session_isValid(fd = map_server[i].fd)) {
  30. WFIFOHEAD(fd,len);
  31. memcpy(WFIFOP(fd,0), buf, len);
  32. WFIFOSET(fd,len);
  33. c++;
  34. }
  35. }
  36. return c;
  37. }
  38. /**
  39. * Packet send to all map-servers, except one. (wos: without our self) attach to ourself
  40. * @param sfd: fd to discard sending to
  41. * @param buf: packet to send in form of an array buffer
  42. * @param len: size of packet
  43. * @return : the number of map-serv the packet was sent to
  44. */
  45. int chmapif_sendallwos(int sfd, unsigned char *buf, unsigned int len){
  46. int i, c;
  47. c = 0;
  48. for(i = 0; i < ARRAYLENGTH(map_server); i++) {
  49. int fd;
  50. if (session_isValid(fd = map_server[i].fd) && fd != sfd) {
  51. WFIFOHEAD(fd,len);
  52. memcpy(WFIFOP(fd,0), buf, len);
  53. WFIFOSET(fd,len);
  54. c++;
  55. }
  56. }
  57. return c;
  58. }
  59. /**
  60. * Packet send to all char-servers, except one. (wos: without our self)
  61. * @param fd: fd to send packet too
  62. * @param buf: packet to send in form of an array buffer
  63. * @param len: size of packet
  64. * @return : the number of map-serv the packet was sent to (O|1)
  65. */
  66. int chmapif_send(int fd, unsigned char *buf, unsigned int len){
  67. if (session_isValid(fd)) {
  68. int i;
  69. ARR_FIND( 0, ARRAYLENGTH(map_server), i, fd == map_server[i].fd );
  70. if( i < ARRAYLENGTH(map_server) )
  71. {
  72. WFIFOHEAD(fd,len);
  73. memcpy(WFIFOP(fd,0), buf, len);
  74. WFIFOSET(fd,len);
  75. return 1;
  76. }
  77. }
  78. return 0;
  79. }
  80. /**
  81. * Send map-servers fames ranking lists
  82. * Defaut fame list are 32B, (id+point+names)
  83. * S <len>.W <len bs + alchi>.W <len bs>.W <smith_rank>?B <alchi_rank>?B <taek_rank>?B
  84. * @param fd: fd to send packet too (map-serv) if -1 send to all
  85. * @return : 0 success
  86. */
  87. int chmapif_send_fame_list(int fd){
  88. int i, len = 8;
  89. unsigned char buf[32000];
  90. WBUFW(buf,0) = 0x2b1b;
  91. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  92. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  93. len += sizeof(struct fame_list);
  94. }
  95. // add blacksmith's block length
  96. WBUFW(buf, 6) = len;
  97. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  98. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  99. len += sizeof(struct fame_list);
  100. }
  101. // add alchemist's block length
  102. WBUFW(buf, 4) = len;
  103. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  104. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  105. len += sizeof(struct fame_list);
  106. }
  107. // add total packet length
  108. WBUFW(buf, 2) = len;
  109. if (session_isValid(fd))
  110. chmapif_send(fd, buf, len);
  111. else
  112. chmapif_sendall(buf, len);
  113. return 0;
  114. }
  115. /**
  116. * Send to map-servers the updated fame ranking lists
  117. * We actually just send this one when we only need to update rankpoint but pos didn't change
  118. * @param type: ranking type
  119. * @param index: position in the ranking
  120. * @param fame: number of points
  121. */
  122. void chmapif_update_fame_list(int type, int index, int fame) {
  123. unsigned char buf[8];
  124. WBUFW(buf,0) = 0x2b22;
  125. WBUFB(buf,2) = type;
  126. WBUFB(buf,3) = index;
  127. WBUFL(buf,4) = fame;
  128. chmapif_sendall(buf, 8);
  129. }
  130. /**
  131. * Send to map-servers the users count on this char-serv, (meaning the total of all mapserv)
  132. * @param users: number of players on this char-serv
  133. */
  134. void chmapif_sendall_playercount(int users){
  135. uint8 buf[6];
  136. // send number of players to all map-servers
  137. WBUFW(buf,0) = 0x2b00;
  138. WBUFL(buf,2) = users;
  139. chmapif_sendall(buf,6);
  140. }
  141. /**
  142. * Send some misc info to new map-server.
  143. * - Server name for whisper name
  144. * - Default map
  145. * HZ 0x2afb <size>.W <status>.B <whisper name>.24B <server name>.24B
  146. * @param fd
  147. **/
  148. void chmapif_send_misc(int fd) {
  149. uint16 offs = 5;
  150. unsigned char buf[5+NAME_LENGTH+NAME_LENGTH];
  151. memset(buf, '\0', sizeof(buf));
  152. WBUFW(buf, 0) = 0x2afb;
  153. // 0 succes, 1:failure
  154. WBUFB(buf, 4) = 0;
  155. // Send name for wisp to player
  156. safestrncpy( WBUFCP( buf, offs ), charserv_config.wisp_server_name, NAME_LENGTH );
  157. offs += NAME_LENGTH;
  158. safestrncpy( WBUFCP( buf, offs ), charserv_config.server_name, sizeof( charserv_config.server_name ) );
  159. offs += NAME_LENGTH;
  160. // Length
  161. WBUFW(buf, 2) = offs;
  162. chmapif_send(fd, buf, offs);
  163. }
  164. /**
  165. * Sends maps to all map-server
  166. * HZ 0x2b04 <size>.W <ip>.L <port>.W { <map>.?B }.?B
  167. * @param fd
  168. * @param map_id
  169. * @param count Number of map from new map-server has
  170. **/
  171. void chmapif_send_maps(int fd, int map_id, int count, unsigned char *mapbuf) {
  172. uint16 x;
  173. if (count == 0) {
  174. ShowWarning("Map-server %d has NO maps.\n", map_id);
  175. }
  176. else {
  177. unsigned char buf[INT16_MAX];
  178. // Transmitting maps information to the other map-servers
  179. WBUFW(buf,0) = 0x2b04;
  180. WBUFW( buf, 2 ) = count * MAP_NAME_LENGTH_EXT + 10;
  181. WBUFL(buf,4) = htonl(map_server[map_id].ip);
  182. WBUFW(buf,8) = htons(map_server[map_id].port);
  183. memcpy( WBUFP( buf, 10 ), mapbuf, count * MAP_NAME_LENGTH_EXT );
  184. chmapif_sendallwos(fd, buf, WBUFW(buf,2));
  185. }
  186. // Transmitting the maps of the other map-servers to the new map-server
  187. for (x = 0; x < ARRAYLENGTH(map_server); x++) {
  188. if (session_isValid(map_server[x].fd) && x != map_id) {
  189. WFIFOHEAD( fd, 10 + MAP_NAME_LENGTH_EXT * map_server[x].maps.size() );
  190. WFIFOW(fd,0) = 0x2b04;
  191. WFIFOL(fd,4) = htonl(map_server[x].ip);
  192. WFIFOW(fd,8) = htons(map_server[x].port);
  193. uint16 j = 0;
  194. for( std::string& map : map_server[x].maps ){
  195. safestrncpy( WFIFOCP( fd, 10 + j * MAP_NAME_LENGTH_EXT ), map.c_str(), MAP_NAME_LENGTH_EXT );
  196. j++;
  197. }
  198. if (j > 0) {
  199. WFIFOW( fd, 2 ) = j * MAP_NAME_LENGTH_EXT + 10;
  200. WFIFOSET(fd,WFIFOW(fd,2));
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * This function is called when the map-serv initialise is chrif interface.
  207. * Map-serv sent us his map indexes so we can transfert a player from a map-serv to another when necessary
  208. * We reply by sending back the char_serv_wisp_name fame list and
  209. * @param fd: wich fd to parse from
  210. * @param id: wich map_serv id
  211. * @return : 0 not enough data received, 1 success
  212. */
  213. int chmapif_parse_getmapname(int fd, int id){
  214. int i = 0;
  215. unsigned char *mapbuf;
  216. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  217. return 0;
  218. //Retain what map-index that map-serv contains
  219. map_server[id].maps.clear();
  220. for( int i = 4; i < RFIFOW( fd, 2 ); i += MAP_NAME_LENGTH_EXT ){
  221. char mapname[MAP_NAME_LENGTH_EXT];
  222. safestrncpy( mapname, RFIFOCP( fd, i ), sizeof( mapname ) );
  223. map_server[id].maps.push_back( mapname );
  224. }
  225. mapbuf = RFIFOP(fd,4);
  226. RFIFOSKIP(fd,RFIFOW(fd,2));
  227. ShowStatus("Map-Server %d connected: %" PRIuPTR " maps, from IP %d.%d.%d.%d port %d.\n",
  228. id, map_server[id].maps.size(), CONVIP(map_server[id].ip), map_server[id].port);
  229. ShowStatus("Map-server %d loading complete.\n", id);
  230. chmapif_send_misc(fd);
  231. chmapif_send_fame_list(fd); //Send fame list.
  232. chmapif_send_maps(fd, id, map_server[id].maps.size(), mapbuf);
  233. return 1;
  234. }
  235. /**
  236. * Map-serv requesting to send the list of sc_data the player has saved
  237. * @author [Skotlex]
  238. * @param fd: wich fd to parse from
  239. * @return : 0 not enough data received, 1 success
  240. */
  241. int chmapif_parse_askscdata(int fd){
  242. if (RFIFOREST(fd) < 10)
  243. return 0;
  244. else {
  245. #ifdef ENABLE_SC_SAVING
  246. int aid, cid;
  247. aid = RFIFOL(fd,2);
  248. cid = RFIFOL(fd,6);
  249. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  250. schema_config.scdata_db, aid, cid) )
  251. {
  252. Sql_ShowDebug(sql_handle);
  253. return 1;
  254. }
  255. if( Sql_NumRows(sql_handle) > 0 )
  256. {
  257. struct status_change_data scdata;
  258. int count;
  259. char* data;
  260. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  261. WFIFOW(fd,0) = 0x2b1d;
  262. WFIFOL(fd,4) = aid;
  263. WFIFOL(fd,8) = cid;
  264. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  265. {
  266. Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data);
  267. Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = strtoll( data, nullptr, 10 );
  268. Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data);
  269. Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data);
  270. Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data);
  271. Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data);
  272. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  273. }
  274. if (count >= 50)
  275. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  276. if (count > 0)
  277. {
  278. WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data);
  279. WFIFOW(fd,12) = count;
  280. WFIFOSET(fd,WFIFOW(fd,2));
  281. }
  282. } else { // No Status Changes to load but still send a response
  283. WFIFOHEAD(fd,14);
  284. WFIFOW(fd,0) = 0x2b1d;
  285. WFIFOW(fd,2) = 14;
  286. WFIFOL(fd,4) = aid;
  287. WFIFOL(fd,8) = cid;
  288. WFIFOW(fd,12) = 0;
  289. WFIFOSET(fd,WFIFOW(fd,2));
  290. }
  291. Sql_FreeResult(sql_handle);
  292. #endif
  293. RFIFOSKIP(fd, 10);
  294. }
  295. return 1;
  296. }
  297. /**
  298. * Map-serv sent us his new users count, updating info
  299. * @param fd: wich fd to parse from
  300. * @param id: wich map_serv id
  301. * @return : 0 not enough data received, 1 success
  302. */
  303. int chmapif_parse_getusercount(int fd, int id){
  304. if (RFIFOREST(fd) < 4)
  305. return 0;
  306. if (RFIFOW(fd,2) != map_server[id].users) {
  307. map_server[id].users = RFIFOW(fd,2);
  308. ShowInfo("User Count: %d (Server: %d)\n", map_server[id].users, id);
  309. }
  310. RFIFOSKIP(fd, 4);
  311. return 1;
  312. }
  313. /**
  314. * Map-serv sent us all his users info, (aid and cid) so we can update online_char_db
  315. * @param fd: wich fd to parse from
  316. * @param id: wich map_serv id
  317. * @return : 0 not enough data received, 1 success
  318. */
  319. int chmapif_parse_regmapuser(int fd, int id){
  320. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  321. return 0;
  322. else {
  323. //TODO: When data mismatches memory, update guild/party online/offline states.
  324. map_server[id].users = RFIFOW(fd,4);
  325. // Set all chars from this server as 'unknown'
  326. for( const auto& pair : char_get_onlinedb() ){
  327. char_db_setoffline( pair.second, id );
  328. }
  329. for( int i = 0; i < map_server[id].users; i++ ){
  330. uint32 aid = RFIFOL(fd,6+i*8);
  331. uint32 cid = RFIFOL(fd,6+i*8+4);
  332. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), aid );
  333. if( character != nullptr ){
  334. if( character->server > -1 && character->server != id ){
  335. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  336. character->account_id, character->char_id, character->server, id, aid, cid);
  337. mapif_disconnectplayer(map_server[character->server].fd, character->account_id, character->char_id, 2);
  338. }
  339. }else{
  340. character = std::make_shared<struct online_char_data>( aid );
  341. char_get_onlinedb()[aid] = character;
  342. }
  343. character->server = id;
  344. character->char_id = cid;
  345. }
  346. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  347. RFIFOSKIP(fd,RFIFOW(fd,2));
  348. }
  349. return 1;
  350. }
  351. /**
  352. * Map-serv request to save mmo_char_status in sql
  353. * Receive character data from map-server for saving
  354. * @param fd: wich fd to parse from
  355. * @param id: wich map_serv id
  356. * @return : 0 not enough data received, 1 success
  357. */
  358. int chmapif_parse_reqsavechar(int fd, int id){
  359. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  360. return 0;
  361. else {
  362. uint32 aid = RFIFOL( fd, 4 ), cid = RFIFOL( fd, 8 );
  363. uint16 size = RFIFOW( fd, 2 );
  364. if (size - 13 != sizeof(struct mmo_charstatus))
  365. {
  366. ShowError("parse_from_map (save-char): Size mismatch! %d != %" PRIuPTR "\n", size-13, sizeof(struct mmo_charstatus));
  367. RFIFOSKIP(fd,size);
  368. return 1;
  369. }
  370. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), aid );
  371. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  372. if( RFIFOB( fd, 12 ) || RFIFOB( fd, 13 ) || ( character != nullptr && character->char_id == cid ) ){
  373. struct mmo_charstatus char_dat;
  374. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  375. char_mmo_char_tosql(cid, &char_dat);
  376. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  377. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  378. char_set_char_online(id, cid, aid);
  379. }
  380. if (RFIFOB(fd,12))
  381. { //Flag, set character offline after saving. [Skotlex]
  382. char_set_char_offline(cid, aid);
  383. WFIFOHEAD(fd,10);
  384. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  385. WFIFOL(fd,2) = aid;
  386. WFIFOL(fd,6) = cid;
  387. WFIFOSET(fd,10);
  388. }
  389. RFIFOSKIP(fd,size);
  390. }
  391. return 1;
  392. }
  393. /**
  394. * Inform mapserv of a new character selection request
  395. * @param fd : FD link tomapserv
  396. * @param aid : Player account id
  397. * @param res : result, 0=not ok, 1=ok
  398. */
  399. void chmapif_charselres(int fd, uint32 aid, uint8 res){
  400. WFIFOHEAD(fd,7);
  401. WFIFOW(fd,0) = 0x2b03;
  402. WFIFOL(fd,2) = aid;
  403. WFIFOB(fd,6) = res;
  404. WFIFOSET(fd,7);
  405. }
  406. /**
  407. * Player Requesting char-select from map_serv
  408. * @param fd: wich fd to parse from
  409. * @return : 0 not enough data received, 1 success
  410. */
  411. int chmapif_parse_authok(int fd){
  412. if( RFIFOREST(fd) < 18 )
  413. return 0;
  414. else{
  415. uint32 account_id = RFIFOL(fd,2);
  416. uint32 login_id1 = RFIFOL(fd,6);
  417. uint32 login_id2 = RFIFOL(fd,10);
  418. uint32 ip = RFIFOL(fd,14);
  419. RFIFOSKIP(fd,18);
  420. if( !global_core->is_running() ){
  421. chmapif_charselres(fd,account_id,0);
  422. }else{
  423. // create temporary auth entry
  424. std::shared_ptr<struct auth_node> node = std::make_shared<struct auth_node>();
  425. node->account_id = account_id;
  426. node->char_id = 0;
  427. node->login_id1 = login_id1;
  428. node->login_id2 = login_id2;
  429. //node->sex = 0;
  430. node->ip = ntohl(ip);
  431. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  432. //node->gmlevel = 0;
  433. char_get_authdb()[node->account_id] = node;
  434. //Set char to "@ char select" in online db [Kevin]
  435. char_set_charselect(account_id);
  436. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), account_id );
  437. if( character != nullptr ){
  438. character->pincode_success = true;
  439. }
  440. chmapif_charselres(fd,account_id,1);
  441. }
  442. }
  443. return 1;
  444. }
  445. //Request to save skill cooldown data
  446. int chmapif_parse_req_saveskillcooldown(int fd){
  447. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  448. return 0;
  449. else {
  450. int count, aid, cid;
  451. aid = RFIFOL(fd,4);
  452. cid = RFIFOL(fd,8);
  453. count = RFIFOW(fd,12);
  454. if( count > 0 )
  455. {
  456. struct skill_cooldown_data data;
  457. StringBuf buf;
  458. int i;
  459. StringBuf_Init(&buf);
  460. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `skill`, `tick`) VALUES ", schema_config.skillcooldown_db);
  461. for( i = 0; i < count; ++i )
  462. {
  463. memcpy(&data,RFIFOP(fd,14+i*sizeof(struct skill_cooldown_data)),sizeof(struct skill_cooldown_data));
  464. if( i > 0 )
  465. StringBuf_AppendStr(&buf, ", ");
  466. StringBuf_Printf(&buf, "('%d','%d','%d','%" PRtf "')", aid, cid, data.skill_id, data.tick);
  467. }
  468. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  469. Sql_ShowDebug(sql_handle);
  470. StringBuf_Destroy(&buf);
  471. }
  472. RFIFOSKIP(fd, RFIFOW(fd, 2));
  473. }
  474. return 1;
  475. }
  476. //Request skillcooldown data 0x2b0a
  477. int chmapif_parse_req_skillcooldown(int fd){
  478. if (RFIFOREST(fd) < 10)
  479. return 0;
  480. else {
  481. int aid, cid;
  482. aid = RFIFOL(fd,2);
  483. cid = RFIFOL(fd,6);
  484. RFIFOSKIP(fd, 10);
  485. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT skill, tick FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  486. schema_config.skillcooldown_db, aid, cid) )
  487. {
  488. Sql_ShowDebug(sql_handle);
  489. return 1;
  490. }
  491. if( Sql_NumRows(sql_handle) > 0 )
  492. {
  493. int count;
  494. char* data;
  495. struct skill_cooldown_data scd;
  496. WFIFOHEAD(fd,14 + MAX_SKILLCOOLDOWN * sizeof(struct skill_cooldown_data));
  497. WFIFOW(fd,0) = 0x2b0b;
  498. WFIFOL(fd,4) = aid;
  499. WFIFOL(fd,8) = cid;
  500. for( count = 0; count < MAX_SKILLCOOLDOWN && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  501. {
  502. Sql_GetData(sql_handle, 0, &data, NULL); scd.skill_id = atoi(data);
  503. Sql_GetData(sql_handle, 1, &data, NULL); scd.tick = strtoll( data, nullptr, 10 );
  504. memcpy(WFIFOP(fd,14+count*sizeof(struct skill_cooldown_data)), &scd, sizeof(struct skill_cooldown_data));
  505. }
  506. if( count >= MAX_SKILLCOOLDOWN )
  507. ShowWarning("Too many skillcooldowns for %d:%d, some of them were not loaded.\n", aid, cid);
  508. if( count > 0 )
  509. {
  510. WFIFOW(fd,2) = 14 + count * sizeof(struct skill_cooldown_data);
  511. WFIFOW(fd,12) = count;
  512. WFIFOSET(fd,WFIFOW(fd,2));
  513. //Clear the data once loaded.
  514. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", schema_config.skillcooldown_db, aid, cid) )
  515. Sql_ShowDebug(sql_handle);
  516. }
  517. }
  518. Sql_FreeResult(sql_handle);
  519. }
  520. return 1;
  521. }
  522. /**
  523. * Inform the mapserv, of a change mapserv request
  524. * @param fd :Link to mapserv
  525. * @param nok : 0=accepted or no=1
  526. */
  527. void chmapif_changemapserv_ack(int fd, bool nok){
  528. // TODO: Refactor... You crazy *** [Lemongrass]
  529. WFIFOHEAD( fd, 28 + MAP_NAME_LENGTH_EXT );
  530. WFIFOW(fd,0) = 0x2b06;
  531. memcpy( WFIFOP( fd, 2 ), RFIFOP( fd, 2 ), 26 + MAP_NAME_LENGTH_EXT );
  532. if(nok)
  533. WFIFOL(fd,6) = 0; //Set login1 to 0.(not ok)
  534. WFIFOSET( fd, 28 + MAP_NAME_LENGTH_EXT );
  535. }
  536. /**
  537. * Player requesting to change map-serv
  538. * @param fd: wich fd to parse from
  539. * @return : 0 not enough data received, 1 success
  540. */
  541. int chmapif_parse_reqchangemapserv(int fd){
  542. if( RFIFOREST( fd ) < ( 37 + MAP_NAME_LENGTH_EXT ) ){
  543. return 0;
  544. }
  545. else {
  546. int map_id, map_fd = -1;
  547. struct mmo_charstatus char_dat;
  548. int offset = 18 + MAP_NAME_LENGTH_EXT;
  549. map_id = char_search_mapserver( RFIFOCP( fd, 18 ), ntohl( RFIFOL( fd, offset + 4 ) ), ntohs( RFIFOW( fd, offset + 8 ) ) ); //Locate mapserver by ip and port.
  550. if (map_id >= 0)
  551. map_fd = map_server[map_id].fd;
  552. uint32 char_id = RFIFOL( fd, 14 );
  553. // Char should just had been saved before this packet, so this should be safe. [Skotlex]
  554. std::shared_ptr<struct mmo_charstatus> char_data = util::umap_find( char_get_chardb(), char_id );
  555. // Really shouldn't happen.
  556. if( char_data == nullptr ){
  557. char_mmo_char_fromsql( char_id, &char_dat, true );
  558. char_data = util::umap_find( char_get_chardb(), char_id );
  559. }
  560. if( global_core->is_running() &&
  561. session_isActive(map_fd) &&
  562. char_data )
  563. { //Send the map server the auth of this player.
  564. uint32 aid = RFIFOL( fd, 2 );
  565. //Update the "last map" as this is where the player must be spawned on the new map server.
  566. safestrncpy( char_data->last_point.map, RFIFOCP( fd, 18 ), MAP_NAME_LENGTH_EXT );
  567. char_data->last_point.x = RFIFOW( fd, offset + 0 );
  568. char_data->last_point.y = RFIFOW( fd, offset + 2 );
  569. char_data->sex = RFIFOB( fd, offset + 10 );
  570. // create temporary auth entry
  571. std::shared_ptr<struct auth_node> node = std::make_shared<struct auth_node>();
  572. node->account_id = aid;
  573. node->char_id = char_id;
  574. node->login_id1 = RFIFOL(fd,6);
  575. node->login_id2 = RFIFOL(fd,10);
  576. node->sex = char_data->sex;
  577. node->expiration_time = 0; // FIXME (this thing isn't really supported we could as well purge it instead of fixing)
  578. node->ip = ntohl( RFIFOL( fd, offset + 11 ) );
  579. node->group_id = RFIFOL( fd, offset + 15 );
  580. node->changing_mapservers = 1;
  581. char_get_authdb()[node->account_id] = node;
  582. std::shared_ptr<struct online_char_data> data = util::umap_find( char_get_onlinedb(), aid );
  583. if( data == nullptr ){
  584. data = std::make_shared<struct online_char_data>( aid );
  585. char_get_onlinedb()[aid] = data;
  586. }
  587. data->char_id = char_data->char_id;
  588. data->server = map_id; //Update server where char is.
  589. //Reply with an ack.
  590. chmapif_changemapserv_ack(fd,0);
  591. } else { //Reply with nak
  592. chmapif_changemapserv_ack(fd,1);
  593. }
  594. RFIFOSKIP( fd, 37 + MAP_NAME_LENGTH_EXT );
  595. }
  596. return 1;
  597. }
  598. /**
  599. * Player requesting to remove friend from list
  600. * Remove RFIFOL(fd,6) (friend_id) from RFIFOL(fd,2) (char_id) friend list
  601. * @author [Ind]
  602. * @param fd: wich fd to parse from
  603. * @return : 0 not enough data received, 1 success
  604. */
  605. int chmapif_parse_askrmfriend(int fd){
  606. if (RFIFOREST(fd) < 10)
  607. return 0;
  608. else {
  609. uint32 char_id, friend_id;
  610. char_id = RFIFOL(fd,2);
  611. friend_id = RFIFOL(fd,6);
  612. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `friend_id`='%d' LIMIT 1",
  613. schema_config.friend_db, char_id, friend_id) ) {
  614. Sql_ShowDebug(sql_handle);
  615. return 1;
  616. }
  617. RFIFOSKIP(fd,10);
  618. }
  619. return 1;
  620. }
  621. /**
  622. * Lookup to search if that char_id correspond to a name.
  623. * Comming from map-serv to search on other map-serv
  624. * @param fd: wich fd to parse from
  625. * @return : 0 not enough data received, 1 success
  626. */
  627. int chmapif_parse_reqcharname(int fd){
  628. if (RFIFOREST(fd) < 6)
  629. return 0;
  630. WFIFOHEAD(fd,30);
  631. WFIFOW(fd,0) = 0x2b09;
  632. WFIFOL(fd,2) = RFIFOL(fd,2);
  633. char_loadName((int)RFIFOL(fd,2), WFIFOCP(fd,6));
  634. WFIFOSET(fd,30);
  635. RFIFOSKIP(fd,6);
  636. return 1;
  637. }
  638. /**
  639. * Forward an email update request to login-serv
  640. * Map server send information to change an email of an account -> login-server
  641. * @param fd: wich fd to parse from
  642. * @return : 0 not enough data received, 1 success
  643. */
  644. int chmapif_parse_reqnewemail(int fd){
  645. if (RFIFOREST(fd) < 86)
  646. return 0;
  647. if (chlogif_isconnected()) { // don't send request if no login-server
  648. WFIFOHEAD(login_fd,86);
  649. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  650. WFIFOW(login_fd,0) = 0x2722;
  651. WFIFOSET(login_fd,86);
  652. }
  653. RFIFOSKIP(fd, 86);
  654. return 1;
  655. }
  656. /**
  657. * Forward a change of status for account to login-serv
  658. * @param fd: which fd to parse from
  659. * @return : 0 not enough data received, 1 success
  660. */
  661. int chmapif_parse_fwlog_changestatus(int fd){
  662. if (RFIFOREST(fd) < 44)
  663. return 0;
  664. else {
  665. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline, 4-current group level > VIP group level
  666. char esc_name[NAME_LENGTH*2+1];
  667. char answer = true;
  668. int aid = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  669. const char* name = RFIFOCP(fd,6); // name of the target character
  670. int operation = RFIFOW(fd,30); // type of operation @see enum chrif_req_op
  671. int32 timediff = 0;
  672. int val1 = 0, sex = SEX_MALE;
  673. if (operation == CHRIF_OP_LOGIN_BAN || operation == CHRIF_OP_LOGIN_VIP) {
  674. timediff = RFIFOL(fd, 32);
  675. val1 = RFIFOL(fd, 36);
  676. } else if (operation == CHRIF_OP_CHANGECHARSEX)
  677. sex = RFIFOB(fd, 32);
  678. RFIFOSKIP(fd,44);
  679. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  680. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`, `char_id` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name) )
  681. Sql_ShowDebug(sql_handle);
  682. else if( Sql_NumRows(sql_handle) == 0 ) {
  683. result = 1; // 1-player not found
  684. }
  685. else if( SQL_SUCCESS != Sql_NextRow(sql_handle) ) {
  686. Sql_ShowDebug(sql_handle);
  687. result = 1;
  688. } else {
  689. int t_aid; // target account id
  690. int t_cid; // target char id
  691. char* data;
  692. Sql_GetData(sql_handle, 0, &data, NULL); t_aid = atoi(data);
  693. Sql_GetData(sql_handle, 1, &data, NULL); t_cid = atoi(data);
  694. Sql_FreeResult(sql_handle);
  695. if(!chlogif_isconnected())
  696. result = 3; // 3-login-server offline
  697. //FIXME: need to move this check to login server [ultramage]
  698. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  699. // result = 2; // 2-gm level too low
  700. else {
  701. //! NOTE: See src/char/chrif.hpp::enum chrif_req_op for the number
  702. switch( operation ) {
  703. case CHRIF_OP_LOGIN_BLOCK: // block
  704. WFIFOHEAD(login_fd,10);
  705. WFIFOW(login_fd,0) = 0x2724;
  706. WFIFOL(login_fd,2) = t_aid;
  707. WFIFOL(login_fd,6) = 5; // new account status
  708. WFIFOSET(login_fd,10);
  709. break;
  710. case CHRIF_OP_LOGIN_BAN: // ban
  711. WFIFOHEAD(login_fd,10);
  712. WFIFOW(login_fd, 0) = 0x2725;
  713. WFIFOL(login_fd, 2) = t_aid;
  714. WFIFOL(login_fd, 6) = timediff;
  715. WFIFOSET(login_fd,10);
  716. break;
  717. case CHRIF_OP_LOGIN_UNBLOCK: // unblock
  718. WFIFOHEAD(login_fd,10);
  719. WFIFOW(login_fd,0) = 0x2724;
  720. WFIFOL(login_fd,2) = t_aid;
  721. WFIFOL(login_fd,6) = 0; // new account status
  722. WFIFOSET(login_fd,10);
  723. break;
  724. case CHRIF_OP_LOGIN_UNBAN: // unban
  725. WFIFOHEAD(login_fd,6);
  726. WFIFOW(login_fd,0) = 0x272a;
  727. WFIFOL(login_fd,2) = t_aid;
  728. WFIFOSET(login_fd,6);
  729. break;
  730. case CHRIF_OP_LOGIN_CHANGESEX: // changesex
  731. answer = false;
  732. WFIFOHEAD(login_fd,6);
  733. WFIFOW(login_fd,0) = 0x2727;
  734. WFIFOL(login_fd,2) = t_aid;
  735. WFIFOSET(login_fd,6);
  736. break;
  737. case CHRIF_OP_LOGIN_VIP: // vip
  738. answer = (val1&4); // vip_req val1=type, &1 login send return, &2 update timestamp, &4 map send answer
  739. chlogif_reqvipdata(t_aid, val1, timediff, fd);
  740. break;
  741. case CHRIF_OP_CHANGECHARSEX: // changecharsex
  742. answer = false;
  743. chlogif_parse_ackchangecharsex(t_cid, sex);
  744. break;
  745. } //end switch operation
  746. } //login is connected
  747. }
  748. // send answer if a player asks, not if the server asks
  749. if( aid != -1 && answer) { // Don't send answer for changesex/changecharsex
  750. WFIFOHEAD(fd,34);
  751. WFIFOW(fd, 0) = 0x2b0f;
  752. WFIFOL(fd, 2) = aid;
  753. safestrncpy(WFIFOCP(fd,6), name, NAME_LENGTH);
  754. WFIFOW(fd,30) = operation;
  755. WFIFOW(fd,32) = result;
  756. WFIFOSET(fd,34);
  757. }
  758. }
  759. return 1;
  760. }
  761. /**
  762. * Transmit the acknowledgement of divorce of partner_id1 and partner_id2
  763. * Update the list associated and transmit the new ranking
  764. * @param partner_id1: char id1 divorced
  765. * @param partner_id2: char id2 divorced
  766. */
  767. void chmapif_send_ackdivorce(int partner_id1, int partner_id2){
  768. unsigned char buf[11];
  769. WBUFW(buf,0) = 0x2b12;
  770. WBUFL(buf,2) = partner_id1;
  771. WBUFL(buf,6) = partner_id2;
  772. chmapif_sendall(buf,10);
  773. }
  774. /**
  775. * Received a divorce request
  776. * @param fd: wich fd to parse from
  777. * @return : 0 not enough data received, 1 success
  778. */
  779. int chmapif_parse_reqdivorce(int fd){
  780. if( RFIFOREST(fd) < 10 )
  781. return 0;
  782. char_divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  783. RFIFOSKIP(fd,10);
  784. return 1;
  785. }
  786. /**
  787. * Character disconnected set online 0
  788. * @author [Wizputer]
  789. * @param fd: wich fd to parse from
  790. * @return : 0 not enough data received, 1 success
  791. */
  792. int chmapif_parse_setcharoffline(int fd){
  793. if (RFIFOREST(fd) < 6)
  794. return 0;
  795. char_set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  796. RFIFOSKIP(fd,10);
  797. return 1;
  798. }
  799. /**
  800. * Reset all chars to offline
  801. * @author [Wizputer]
  802. * @param fd: wich fd to parse from
  803. * @param id: wich map_serv id
  804. * @return : 0 not enough data received, 1 success
  805. */
  806. int chmapif_parse_setalloffline(int fd, int id){
  807. char_set_all_offline(id);
  808. RFIFOSKIP(fd,2);
  809. return 1;
  810. }
  811. /**
  812. * Character set online
  813. * @author [Wizputer]
  814. * @param fd: wich fd to parse from
  815. * @param id: wich map_serv id
  816. * @return : 0 not enough data received, 1 success
  817. */
  818. int chmapif_parse_setcharonline(int fd, int id){
  819. if (RFIFOREST(fd) < 10)
  820. return 0;
  821. char_set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  822. RFIFOSKIP(fd,10);
  823. return 1;
  824. }
  825. /**
  826. * Build and send fame ranking lists
  827. * @author [DracoRPG]
  828. * @param fd: wich fd to parse from
  829. * @param id: wich map_serv id
  830. * @return : 0 not enough data received, 1 success
  831. */
  832. int chmapif_parse_reqfamelist(int fd){
  833. if (RFIFOREST(fd) < 2)
  834. return 0;
  835. char_read_fame_list();
  836. chmapif_send_fame_list(-1);
  837. RFIFOSKIP(fd,2);
  838. return 1;
  839. }
  840. /**
  841. * Request to save status change data.s
  842. * @author [Skotlex]
  843. * @param fd: wich fd to parse from
  844. * @return : 0 not enough data received, 1 success
  845. */
  846. int chmapif_parse_save_scdata(int fd){
  847. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  848. return 0;
  849. {
  850. #ifdef ENABLE_SC_SAVING
  851. int count, aid, cid;
  852. aid = RFIFOL(fd, 4);
  853. cid = RFIFOL(fd, 8);
  854. count = RFIFOW(fd, 12);
  855. // Whatever comes from the mapserver, now is the time to drop previous entries
  856. if( Sql_Query( sql_handle, "DELETE FROM `%s` where `account_id` = %d and `char_id` = %d;", schema_config.scdata_db, aid, cid ) != SQL_SUCCESS ){
  857. Sql_ShowDebug( sql_handle );
  858. }
  859. else if( count > 0 )
  860. {
  861. struct status_change_data data;
  862. StringBuf buf;
  863. int i;
  864. StringBuf_Init(&buf);
  865. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", schema_config.scdata_db);
  866. for( i = 0; i < count; ++i )
  867. {
  868. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  869. if( i > 0 )
  870. StringBuf_AppendStr(&buf, ", ");
  871. StringBuf_Printf(&buf, "('%d','%d','%hu','%" PRtf "','%ld','%ld','%ld','%ld')", aid, cid,
  872. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  873. }
  874. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  875. Sql_ShowDebug(sql_handle);
  876. StringBuf_Destroy(&buf);
  877. }
  878. #endif
  879. RFIFOSKIP(fd, RFIFOW(fd, 2));
  880. }
  881. return 1;
  882. }
  883. /**
  884. * map-server keep alive packet, awnser back map that we alive as well
  885. * @param fd: wich fd to parse from
  886. * @return : 0 not enough data received, 1 success
  887. */
  888. int chmapif_parse_keepalive(int fd){
  889. WFIFOHEAD(fd,2);
  890. WFIFOW(fd,0) = 0x2b24;
  891. WFIFOSET(fd,2);
  892. RFIFOSKIP(fd,2);
  893. return 1;
  894. }
  895. /**
  896. * auth request from map-server
  897. * @param fd: wich fd to parse from
  898. * @return : 0 not enough data received, 1 success
  899. */
  900. int chmapif_parse_reqauth(int fd, int id){
  901. if (RFIFOREST(fd) < 20)
  902. return 0;
  903. else {
  904. uint32 account_id;
  905. uint32 char_id;
  906. uint32 login_id1;
  907. unsigned char sex;
  908. uint32 ip;
  909. struct mmo_charstatus char_dat;
  910. bool autotrade;
  911. account_id = RFIFOL(fd,2);
  912. char_id = RFIFOL(fd,6);
  913. login_id1 = RFIFOL(fd,10);
  914. sex = RFIFOB(fd,14);
  915. ip = ntohl(RFIFOL(fd,15));
  916. autotrade = RFIFOB(fd,19) != 0;
  917. RFIFOSKIP(fd,20);
  918. std::shared_ptr<struct auth_node> node = util::umap_find( char_get_authdb(), account_id );
  919. std::shared_ptr<struct mmo_charstatus> cd = util::umap_find( char_get_chardb(), char_id );
  920. if( cd == nullptr ){
  921. // Really shouldn't happen. (or autotrade)
  922. char_mmo_char_fromsql( char_id, &char_dat, true );
  923. cd = util::umap_find( char_get_chardb(), char_id );
  924. }
  925. if( global_core->is_running() && autotrade && cd ){
  926. uint16 mmo_charstatus_len = sizeof(struct mmo_charstatus) + 25;
  927. WFIFOHEAD(fd,mmo_charstatus_len);
  928. WFIFOW(fd,0) = 0x2afd;
  929. WFIFOW(fd,2) = mmo_charstatus_len;
  930. WFIFOL(fd,4) = account_id;
  931. WFIFOL(fd,8) = 0;
  932. WFIFOL(fd,12) = 0;
  933. WFIFOL(fd,16) = 0;
  934. WFIFOL(fd,20) = 0;
  935. WFIFOB(fd,24) = 0;
  936. memcpy( WFIFOP( fd, 25 ), cd.get(), sizeof(struct mmo_charstatus));
  937. WFIFOSET(fd, WFIFOW(fd,2));
  938. char_set_char_online(id, char_id, account_id);
  939. } else if( global_core->is_running() &&
  940. cd != NULL &&
  941. node != NULL &&
  942. node->account_id == account_id &&
  943. node->char_id == char_id &&
  944. node->login_id1 == login_id1
  945. //&& node->ip == ip
  946. #if PACKETVER < 20141016
  947. && node->sex == sex
  948. #endif
  949. )
  950. {// auth ok
  951. uint16 mmo_charstatus_len = sizeof(struct mmo_charstatus) + 25;
  952. WFIFOHEAD(fd,mmo_charstatus_len);
  953. WFIFOW(fd,0) = 0x2afd;
  954. WFIFOW(fd,2) = mmo_charstatus_len;
  955. WFIFOL(fd,4) = account_id;
  956. WFIFOL(fd,8) = node->login_id1;
  957. WFIFOL(fd,12) = node->login_id2;
  958. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  959. WFIFOL(fd,20) = node->group_id;
  960. WFIFOB(fd,24) = node->changing_mapservers;
  961. memcpy( WFIFOP( fd, 25 ), cd.get(), sizeof( struct mmo_charstatus ) );
  962. WFIFOSET(fd, WFIFOW(fd,2));
  963. // only use the auth once and mark user online
  964. char_get_authdb().erase( account_id );
  965. char_set_char_online(id, char_id, account_id);
  966. } else {// auth failed
  967. WFIFOHEAD(fd,19);
  968. WFIFOW(fd,0) = 0x2b27;
  969. WFIFOL(fd,2) = account_id;
  970. WFIFOL(fd,6) = char_id;
  971. WFIFOL(fd,10) = login_id1;
  972. WFIFOB(fd,14) = sex;
  973. WFIFOL(fd,15) = htonl(ip);
  974. WFIFOSET(fd,19);
  975. }
  976. }
  977. return 1;
  978. }
  979. /**
  980. * ip address update
  981. * @param fd: wich fd to parse from
  982. * @return : 0 not enough data received, 1 success
  983. */
  984. int chmapif_parse_updmapip(int fd, int id){
  985. if (RFIFOREST(fd) < 6)
  986. return 0;
  987. map_server[id].ip = ntohl(RFIFOL(fd, 2));
  988. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(map_server[id].ip));
  989. RFIFOSKIP(fd,6);
  990. return 1;
  991. }
  992. /**
  993. * Received an update of fame point for char_id cid
  994. * Update the list associated and transmit the new ranking
  995. * @param fd: wich fd to parse from
  996. * @return : 0 not enough data received, 1 success
  997. */
  998. int chmapif_parse_updfamelist(int fd){
  999. if (RFIFOREST(fd) < 11)
  1000. return 0;
  1001. {
  1002. int cid = RFIFOL(fd, 2);
  1003. int fame = RFIFOL(fd, 6);
  1004. char type = RFIFOB(fd, 10);
  1005. int size;
  1006. struct fame_list* list;
  1007. int player_pos;
  1008. int fame_pos;
  1009. switch(type)
  1010. {
  1011. case RANK_BLACKSMITH: size = fame_list_size_smith; list = smith_fame_list; break;
  1012. case RANK_ALCHEMIST: size = fame_list_size_chemist; list = chemist_fame_list; break;
  1013. case RANK_TAEKWON: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  1014. default: size = 0; list = NULL; break;
  1015. }
  1016. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  1017. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  1018. if( player_pos == size && fame_pos == size )
  1019. ;// not on list and not enough fame to get on it
  1020. else if( fame_pos == player_pos )
  1021. {// same position
  1022. list[player_pos].fame = fame;
  1023. chmapif_update_fame_list(type, player_pos, fame);
  1024. }
  1025. else
  1026. {// move in the list
  1027. if( player_pos == size )
  1028. {// new ranker - not in the list
  1029. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  1030. list[fame_pos].id = cid;
  1031. list[fame_pos].fame = fame;
  1032. char_loadName(cid, list[fame_pos].name);
  1033. }
  1034. else
  1035. {// already in the list
  1036. if( fame_pos == size )
  1037. --fame_pos;// move to the end of the list
  1038. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  1039. list[fame_pos].fame = fame;
  1040. }
  1041. chmapif_send_fame_list(-1);
  1042. }
  1043. RFIFOSKIP(fd,11);
  1044. }
  1045. return 1;
  1046. }
  1047. /*
  1048. * HZ 0x2b2b
  1049. * Transmist vip data to mapserv
  1050. */
  1051. int chmapif_vipack(int mapfd, uint32 aid, uint32 vip_time, uint32 groupid, uint8 flag) {
  1052. #ifdef VIP_ENABLE
  1053. uint8 buf[15];
  1054. WBUFW(buf,0) = 0x2b2b;
  1055. WBUFL(buf,2) = aid;
  1056. WBUFL(buf,6) = vip_time;
  1057. WBUFL(buf,10) = groupid;
  1058. WBUFB(buf,14) = flag;
  1059. chmapif_send(mapfd,buf,15); // inform the mapserv back
  1060. #endif
  1061. return 0;
  1062. }
  1063. int chmapif_parse_reqcharban(int fd){
  1064. if (RFIFOREST(fd) < 10+NAME_LENGTH)
  1065. return 0;
  1066. else {
  1067. //int aid = RFIFOL(fd,2); aid of player who as requested the ban
  1068. int timediff = RFIFOL(fd,6);
  1069. const char* name = RFIFOCP(fd,10); // name of the target character
  1070. char esc_name[NAME_LENGTH*2+1];
  1071. RFIFOSKIP(fd,10+NAME_LENGTH);
  1072. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1073. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`char_id`,`unban_time` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name) )
  1074. Sql_ShowDebug(sql_handle);
  1075. else if( Sql_NumRows(sql_handle) == 0 ){
  1076. return 1; // 1-player not found
  1077. }
  1078. else if( SQL_SUCCESS != Sql_NextRow(sql_handle) ){
  1079. Sql_ShowDebug(sql_handle);
  1080. Sql_FreeResult(sql_handle);
  1081. return 1;
  1082. } else {
  1083. int t_cid=0,t_aid=0;
  1084. char* data;
  1085. time_t unban_time;
  1086. time_t now = time(NULL);
  1087. SqlStmt* stmt = SqlStmt_Malloc(sql_handle);
  1088. Sql_GetData(sql_handle, 0, &data, NULL); t_aid = atoi(data);
  1089. Sql_GetData(sql_handle, 1, &data, NULL); t_cid = atoi(data);
  1090. Sql_GetData(sql_handle, 2, &data, NULL); unban_time = atol(data);
  1091. Sql_FreeResult(sql_handle);
  1092. if(timediff<0 && unban_time==0)
  1093. return 1; //attemp to reduce time of a non banned account ?!?
  1094. else if(unban_time<now) unban_time=now; //new entry
  1095. unban_time += timediff; //alterate the time
  1096. if( unban_time < now ) unban_time=0; //we have totally reduce the time
  1097. if( SQL_SUCCESS != SqlStmt_Prepare(stmt,
  1098. "UPDATE `%s` SET `unban_time` = ? WHERE `char_id` = ? LIMIT 1",
  1099. schema_config.char_db)
  1100. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_LONG, (void*)&unban_time, sizeof(unban_time))
  1101. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_INT, (void*)&t_cid, sizeof(t_cid))
  1102. || SQL_SUCCESS != SqlStmt_Execute(stmt)
  1103. )
  1104. {
  1105. SqlStmt_ShowDebug(stmt);
  1106. SqlStmt_Free(stmt);
  1107. return 1;
  1108. }
  1109. SqlStmt_Free(stmt);
  1110. // condition applies; send to all map-servers to disconnect the player
  1111. if( unban_time > now ) {
  1112. unsigned char buf[11];
  1113. WBUFW(buf,0) = 0x2b14;
  1114. WBUFL(buf,2) = t_cid;
  1115. WBUFB(buf,6) = 2;
  1116. WBUFL(buf,7) = (unsigned int)unban_time;
  1117. chmapif_sendall(buf, 11);
  1118. // disconnect player if online on char-server
  1119. char_disconnect_player(t_aid);
  1120. }
  1121. }
  1122. }
  1123. return 1;
  1124. }
  1125. int chmapif_parse_reqcharunban(int fd){
  1126. if (RFIFOREST(fd) < 6+NAME_LENGTH)
  1127. return 0;
  1128. else {
  1129. const char* name = RFIFOCP(fd,6);
  1130. char esc_name[NAME_LENGTH*2+1];
  1131. RFIFOSKIP(fd,6+NAME_LENGTH);
  1132. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1133. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `unban_time` = '0' WHERE `name` = '%s' LIMIT 1", schema_config.char_db, esc_name) ) {
  1134. Sql_ShowDebug(sql_handle);
  1135. return 1;
  1136. }
  1137. }
  1138. return 1;
  1139. }
  1140. /**
  1141. * ZA 0x2b2d
  1142. * <cmd>.W <char_id>.L
  1143. * AZ 0x2b2f
  1144. * <cmd>.W <len>.W <cid>.L <count>.B { <bonus_script_data>.?B }
  1145. * Get bonus_script data(s) from table to load then send to player
  1146. * @param fd
  1147. * @author [Cydh]
  1148. **/
  1149. int chmapif_bonus_script_get(int fd) {
  1150. if (RFIFOREST(fd) < 6)
  1151. return 0;
  1152. else {
  1153. uint8 num_rows = 0;
  1154. uint32 cid = RFIFOL(fd,2);
  1155. struct bonus_script_data tmp_bsdata;
  1156. SqlStmt* stmt = SqlStmt_Malloc(sql_handle);
  1157. RFIFOSKIP(fd,6);
  1158. if (SQL_ERROR == SqlStmt_Prepare(stmt,
  1159. "SELECT `script`, `tick`, `flag`, `type`, `icon` FROM `%s` WHERE `char_id` = '%d' LIMIT %d",
  1160. schema_config.bonus_script_db, cid, MAX_PC_BONUS_SCRIPT) ||
  1161. SQL_ERROR == SqlStmt_Execute(stmt) ||
  1162. SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &tmp_bsdata.script_str, sizeof(tmp_bsdata.script_str), NULL, NULL) ||
  1163. SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT64, &tmp_bsdata.tick, 0, NULL, NULL) ||
  1164. SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT16, &tmp_bsdata.flag, 0, NULL, NULL) ||
  1165. SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_UINT8, &tmp_bsdata.type, 0, NULL, NULL) ||
  1166. SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_INT16, &tmp_bsdata.icon, 0, NULL, NULL)
  1167. )
  1168. {
  1169. SqlStmt_ShowDebug(stmt);
  1170. SqlStmt_Free(stmt);
  1171. return 1;
  1172. }
  1173. if ((num_rows = (uint8)SqlStmt_NumRows(stmt)) > 0) {
  1174. uint8 i;
  1175. uint32 size = 9 + num_rows * sizeof(struct bonus_script_data);
  1176. WFIFOHEAD(fd, size);
  1177. WFIFOW(fd, 0) = 0x2b2f;
  1178. WFIFOW(fd, 2) = size;
  1179. WFIFOL(fd, 4) = cid;
  1180. WFIFOB(fd, 8) = num_rows;
  1181. for (i = 0; i < num_rows && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++) {
  1182. struct bonus_script_data bsdata;
  1183. memset(&bsdata, 0, sizeof(bsdata));
  1184. memset(bsdata.script_str, '\0', sizeof(bsdata.script_str));
  1185. safestrncpy(bsdata.script_str, tmp_bsdata.script_str, strlen(tmp_bsdata.script_str)+1);
  1186. bsdata.tick = tmp_bsdata.tick;
  1187. bsdata.flag = tmp_bsdata.flag;
  1188. bsdata.type = tmp_bsdata.type;
  1189. bsdata.icon = tmp_bsdata.icon;
  1190. memcpy(WFIFOP(fd, 9 + i * sizeof(struct bonus_script_data)), &bsdata, sizeof(struct bonus_script_data));
  1191. }
  1192. WFIFOSET(fd, size);
  1193. ShowInfo("Bonus Script loaded for CID=%d. Total: %d.\n", cid, i);
  1194. if (SQL_ERROR == SqlStmt_Prepare(stmt,"DELETE FROM `%s` WHERE `char_id`='%d'",schema_config.bonus_script_db,cid) ||
  1195. SQL_ERROR == SqlStmt_Execute(stmt))
  1196. SqlStmt_ShowDebug(stmt);
  1197. }
  1198. SqlStmt_Free(stmt);
  1199. }
  1200. return 1;
  1201. }
  1202. /**
  1203. * ZA 0x2b2e
  1204. * <cmd>.W <len>.W <char_id>.L <count>.B { <bonus_script>.?B }
  1205. * Save bonus_script data(s) to the table
  1206. * @param fd
  1207. * @author [Cydh]
  1208. **/
  1209. int chmapif_bonus_script_save(int fd) {
  1210. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  1211. return 0;
  1212. else {
  1213. uint32 cid = RFIFOL(fd,4);
  1214. uint8 count = RFIFOB(fd,8);
  1215. if (SQL_ERROR == Sql_Query(sql_handle,"DELETE FROM `%s` WHERE `char_id` = '%d'", schema_config.bonus_script_db, cid))
  1216. Sql_ShowDebug(sql_handle);
  1217. if (count > 0) {
  1218. char esc_script[MAX_BONUS_SCRIPT_LENGTH*2];
  1219. struct bonus_script_data bsdata;
  1220. StringBuf buf;
  1221. uint8 i;
  1222. StringBuf_Init(&buf);
  1223. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `script`, `tick`, `flag`, `type`, `icon`) VALUES ", schema_config.bonus_script_db);
  1224. for (i = 0; i < count; ++i) {
  1225. memcpy(&bsdata, RFIFOP(fd, 9 + i*sizeof(struct bonus_script_data)), sizeof(struct bonus_script_data));
  1226. Sql_EscapeString(sql_handle, esc_script, bsdata.script_str);
  1227. if (i > 0)
  1228. StringBuf_AppendStr(&buf,", ");
  1229. StringBuf_Printf(&buf, "('%d','%s','%" PRtf "','%d','%d','%d')", cid, esc_script, bsdata.tick, bsdata.flag, bsdata.type, bsdata.icon);
  1230. }
  1231. if (SQL_ERROR == Sql_QueryStr(sql_handle,StringBuf_Value(&buf)))
  1232. Sql_ShowDebug(sql_handle);
  1233. StringBuf_Destroy(&buf);
  1234. ShowInfo("Bonus Script saved for CID=%d. Total: %d.\n", cid, count);
  1235. }
  1236. RFIFOSKIP(fd,RFIFOW(fd,2));
  1237. }
  1238. return 1;
  1239. }
  1240. /**
  1241. * Inform the mapserv wheater his login attemp to us was a success or not
  1242. * @param fd : file descriptor to parse, (link to mapserv)
  1243. * @param errCode 0:success, 3:fail
  1244. */
  1245. void chmapif_connectack(int fd, uint8 errCode){
  1246. WFIFOHEAD(fd,3);
  1247. WFIFOW(fd,0) = 0x2af9;
  1248. WFIFOB(fd,2) = errCode;
  1249. WFIFOSET(fd,3);
  1250. }
  1251. /**
  1252. * Entry point from map-server to char-server.
  1253. * Function that checks incoming command, then splits it to the correct handler.
  1254. * If not found any hander here transmis packet to inter
  1255. * @param fd: file descriptor to parse, (link to map-serv)
  1256. * @return 0=invalid server,marked for disconnection,unknow packet; 1=success
  1257. */
  1258. int chmapif_parse(int fd){
  1259. int id; //mapserv id
  1260. ARR_FIND( 0, ARRAYLENGTH(map_server), id, map_server[id].fd == fd );
  1261. if( id == ARRAYLENGTH(map_server) )
  1262. {// not a map server
  1263. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  1264. do_close(fd);
  1265. return 0;
  1266. }
  1267. if( session[fd]->flag.eof )
  1268. {
  1269. do_close(fd);
  1270. map_server[id].fd = -1;
  1271. chmapif_on_disconnect(id);
  1272. return 0;
  1273. }
  1274. while(RFIFOREST(fd) >= 2){
  1275. int next=1;
  1276. switch(RFIFOW(fd,0)){
  1277. case 0x2afa: next=chmapif_parse_getmapname(fd,id); break;
  1278. case 0x2afc: next=chmapif_parse_askscdata(fd); break;
  1279. case 0x2afe: next=chmapif_parse_getusercount(fd,id); break; //get nb user
  1280. case 0x2aff: next=chmapif_parse_regmapuser(fd,id); break; //register users
  1281. case 0x2b01: next=chmapif_parse_reqsavechar(fd,id); break;
  1282. case 0x2b02: next=chmapif_parse_authok(fd); break;
  1283. case 0x2b05: next=chmapif_parse_reqchangemapserv(fd); break;
  1284. case 0x2b07: next=chmapif_parse_askrmfriend(fd); break;
  1285. case 0x2b08: next=chmapif_parse_reqcharname(fd); break;
  1286. case 0x2b0a: next=chmapif_parse_req_skillcooldown(fd); break;
  1287. case 0x2b0c: next=chmapif_parse_reqnewemail(fd); break;
  1288. case 0x2b0e: next=chmapif_parse_fwlog_changestatus(fd); break;
  1289. case 0x2b10: next=chmapif_parse_updfamelist(fd); break;
  1290. case 0x2b11: next=chmapif_parse_reqdivorce(fd); break;
  1291. case 0x2b13: next=chmapif_parse_updmapip(fd,id); break;
  1292. case 0x2b15: next=chmapif_parse_req_saveskillcooldown(fd); break;
  1293. case 0x2b17: next=chmapif_parse_setcharoffline(fd); break;
  1294. case 0x2b18: next=chmapif_parse_setalloffline(fd,id); break;
  1295. case 0x2b19: next=chmapif_parse_setcharonline(fd,id); break;
  1296. case 0x2b1a: next=chmapif_parse_reqfamelist(fd); break;
  1297. case 0x2b1c: next=chmapif_parse_save_scdata(fd); break;
  1298. case 0x2b23: next=chmapif_parse_keepalive(fd); break;
  1299. case 0x2b26: next=chmapif_parse_reqauth(fd,id); break;
  1300. case 0x2b28: next=chmapif_parse_reqcharban(fd); break; //charban
  1301. case 0x2b2a: next=chmapif_parse_reqcharunban(fd); break; //charunban
  1302. //case 0x2b2c: /*free*/; break;
  1303. case 0x2b2d: next=chmapif_bonus_script_get(fd); break; //Load data
  1304. case 0x2b2e: next=chmapif_bonus_script_save(fd); break;//Save data
  1305. default:
  1306. {
  1307. // inter server - packet
  1308. int r = inter_parse_frommap(fd);
  1309. if (r == 1) break; // processed
  1310. if (r == 2) return 0; // need more packet
  1311. // no inter server packet. no char server packet -> disconnect
  1312. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  1313. set_eof(fd);
  1314. return 0;
  1315. }
  1316. } // switch
  1317. if(next==0) return 0; //avoid processing rest of packet
  1318. } // while
  1319. return 1;
  1320. }
  1321. // Initialization process (currently only initialization inter_mapif)
  1322. int chmapif_init(int fd){
  1323. return inter_mapif_init(fd);
  1324. }
  1325. /**
  1326. * Initializes a server structure.
  1327. * @param id: id of map-serv (should be >0, FIXME)
  1328. */
  1329. void chmapif_server_init(int id) {
  1330. memset(&map_server[id], 0, sizeof(map_server[id]));
  1331. map_server[id].fd = -1;
  1332. }
  1333. /**
  1334. * Destroys a server structure.
  1335. * @param id: id of map-serv (should be >0, FIXME)
  1336. */
  1337. void chmapif_server_destroy(int id){
  1338. if( map_server[id].fd == -1 ){
  1339. do_close(map_server[id].fd);
  1340. map_server[id].fd = -1;
  1341. }
  1342. }
  1343. /**
  1344. * chmapif constructor
  1345. * Initialisation, function called at start of the char-serv.
  1346. */
  1347. void do_init_chmapif(void){
  1348. int i;
  1349. for( i = 0; i < ARRAYLENGTH(map_server); ++i )
  1350. chmapif_server_init(i);
  1351. }
  1352. /**
  1353. * Resets all the data related to a server.
  1354. * Actually destroys then recreates the struct.
  1355. * @param id: id of map-serv (should be >0, FIXME)
  1356. */
  1357. void chmapif_server_reset(int id){
  1358. int j = 0;
  1359. unsigned char buf[INT16_MAX];
  1360. int fd = map_server[id].fd;
  1361. //Notify other map servers that this one is gone. [Skotlex]
  1362. WBUFW(buf,0) = 0x2b20;
  1363. WBUFL(buf,4) = htonl(map_server[id].ip);
  1364. WBUFW(buf,8) = htons(map_server[id].port);
  1365. for( std::string& map : map_server[id].maps ){
  1366. safestrncpy( WBUFCP( buf, 10 + j * MAP_NAME_LENGTH_EXT ), map.c_str(), MAP_NAME_LENGTH_EXT );
  1367. j++;
  1368. }
  1369. if (j > 0) {
  1370. WBUFW(buf,2) = j * MAP_NAME_LENGTH_EXT + 10;
  1371. chmapif_sendallwos(fd, buf, WBUFW(buf,2));
  1372. }
  1373. // Tag relevant chars as 'in disconnected' server.
  1374. for( const auto& pair : char_get_onlinedb() ){
  1375. char_db_setoffline( pair.second, id );
  1376. }
  1377. chmapif_server_destroy(id);
  1378. chmapif_server_init(id);
  1379. }
  1380. /**
  1381. * Called when the connection to Map Server is disconnected.
  1382. * @param id: id of map-serv (should be >0, FIXME)
  1383. */
  1384. void chmapif_on_disconnect(int id){
  1385. ShowStatus("Map-server #%d has disconnected.\n", id);
  1386. chmapif_server_reset(id);
  1387. }
  1388. /**
  1389. * chmapif destructor
  1390. * dealloc..., function called at exit of the char-serv
  1391. */
  1392. void do_final_chmapif(void){
  1393. int i;
  1394. for( i = 0; i < ARRAYLENGTH(map_server); ++i )
  1395. chmapif_server_destroy(i);
  1396. }