char_mapif.cpp 47 KB

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