Guest User

Untitled

a guest
Feb 28th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 156.63 KB | None | 0 0
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3.  
  4. #include "../common/cbasetypes.h"
  5. #include "../common/core.h"
  6. #include "../common/db.h"
  7. #include "../common/malloc.h"
  8. #include "../common/mapindex.h"
  9. #include "../common/mmo.h"
  10. #include "../common/showmsg.h"
  11. #include "../common/socket.h"
  12. #include "../common/strlib.h"
  13. #include "../common/timer.h"
  14. #include "../common/utils.h"
  15. #include "../common/version.h"
  16. #include "inter.h"
  17. #include "int_guild.h"
  18. #include "int_homun.h"
  19. #include "int_mercenary.h"
  20. #include "int_elemental.h"
  21. #include "int_party.h"
  22. #include "int_storage.h"
  23. #include "char.h"
  24.  
  25. #include <sys/types.h>
  26. #include <time.h>
  27. #include <signal.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32.  
  33. // private declarations
  34. #define CHAR_CONF_NAME "conf/char_athena.conf"
  35. #define LAN_CONF_NAME "conf/subnet_athena.conf"
  36. #define SQL_CONF_NAME "conf/inter_athena.conf"
  37.  
  38. char char_db[256] = "char";
  39. char scdata_db[256] = "sc_data";
  40. char skillcooldown_db[256] = "skillcooldown";
  41. char cart_db[256] = "cart_inventory";
  42. char inventory_db[256] = "inventory";
  43. char charlog_db[256] = "charlog";
  44. char storage_db[256] = "storage";
  45. char interlog_db[256] = "interlog";
  46. char reg_db[256] = "global_reg_value";
  47. char skill_db[256] = "skill";
  48. char memo_db[256] = "memo";
  49. char guild_db[256] = "guild";
  50. char guild_alliance_db[256] = "guild_alliance";
  51. char guild_castle_db[256] = "guild_castle";
  52. char guild_expulsion_db[256] = "guild_expulsion";
  53. char guild_member_db[256] = "guild_member";
  54. char guild_position_db[256] = "guild_position";
  55. char guild_skill_db[256] = "guild_skill";
  56. char guild_storage_db[256] = "guild_storage";
  57. char party_db[256] = "party";
  58. char pet_db[256] = "pet";
  59. char mail_db[256] = "mail"; // MAIL SYSTEM
  60. char auction_db[256] = "auction"; // Auctions System
  61. char friend_db[256] = "friends";
  62. char hotkey_db[256] = "hotkey";
  63. char quest_db[256] = "quest";
  64.  
  65. // show loading/saving messages
  66.  
  67. #ifdef TXT_SQL_CONVERT
  68.  
  69. int save_log = 0; //Have the logs be off by default when converting
  70.  
  71. #else
  72.  
  73. int save_log = 1;
  74.  
  75. #endif
  76.  
  77. //If your code editor is having problems syntax highlighting this file, uncomment this and RECOMMENT IT BEFORE COMPILING
  78. //#undef TXT_SQL_CONVERT
  79. #ifndef TXT_SQL_CONVERT
  80. static DBMap* char_db_; // int char_id -> struct mmo_charstatus*
  81.  
  82. char db_path[1024] = "db";
  83.  
  84. int db_use_sqldbs;
  85.  
  86. struct mmo_map_server {
  87. int fd;
  88. uint32 ip;
  89. uint16 port;
  90. int users;
  91. unsigned short map[MAX_MAP_PER_SERVER];
  92. } server[MAX_MAP_SERVERS];
  93.  
  94. int login_fd=-1, char_fd=-1;
  95. char userid[24];
  96. char passwd[24];
  97. char server_name[20];
  98. char wisp_server_name[NAME_LENGTH] = "Server";
  99. char login_ip_str[128];
  100. uint32 login_ip = 0;
  101. uint16 login_port = 6900;
  102. char char_ip_str[128];
  103. uint32 char_ip = 0;
  104. char bind_ip_str[128];
  105. uint32 bind_ip = INADDR_ANY;
  106. uint16 char_port = 6121;
  107. int char_maintenance = 0;
  108. bool char_new = true;
  109. int char_new_display = 0;
  110.  
  111. bool name_ignoring_case = false; // Allow or not identical name for characters but with a different case by [Yor]
  112. int char_name_option = 0; // Option to know which letters/symbols are authorised in the name of a character (0: all, 1: only those in char_name_letters, 2: all EXCEPT those in char_name_letters) by [Yor]
  113. char unknown_char_name[NAME_LENGTH] = "Unknown"; // Name to use when the requested name cannot be determined
  114. #define TRIM_CHARS "\032\t\x0A\x0D " //The following characters are trimmed regardless because they cause confusion and problems on the servers. [Skotlex]
  115. char char_name_letters[1024] = ""; // list of letters/symbols allowed (or not) in a character name. by [Yor]
  116.  
  117. int char_per_account = 0; //Maximum charas per account (default unlimited) [Sirius]
  118. int char_del_level = 0; //From which level u can delete character [Lupus]
  119. int char_del_delay = 86400;
  120. int log_char = 1; // loggin char or not [devil]
  121. int log_inter = 1; // loggin inter or not [devil]
  122.  
  123.  
  124.  
  125. // Advanced subnet check [LuzZza]
  126. struct s_subnet {
  127. uint32 mask;
  128. uint32 char_ip;
  129. uint32 map_ip;
  130. } subnet[16];
  131. int subnet_count = 0;
  132.  
  133. struct char_session_data {
  134. bool auth; // whether the session is authed or not
  135. int account_id, login_id1, login_id2, sex;
  136. int found_char[MAX_CHARS]; // ids of chars on this account
  137. char email[40]; // e-mail (default: a@a.com) by [Yor]
  138. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  139. int gmlevel;
  140. uint32 version;
  141. uint8 clienttype;
  142. char new_name[NAME_LENGTH];
  143. char birthdate[10+1]; // YYYY-MM-DD
  144. };
  145.  
  146. int max_connect_user = 0;
  147. int gm_allow_level = 99;
  148. int autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  149. int start_zeny = 0;
  150. int start_weapon = 1201;
  151. int start_armor = 2301;
  152. int guild_exp_rate = 100;
  153.  
  154. //Custom limits for the fame lists. [Skotlex]
  155. int fame_list_size_chemist = MAX_FAME_LIST;
  156. int fame_list_size_smith = MAX_FAME_LIST;
  157. int fame_list_size_taekwon = MAX_FAME_LIST;
  158.  
  159. // Char-server-side stored fame lists [DracoRPG]
  160. struct fame_list smith_fame_list[MAX_FAME_LIST];
  161. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  162. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  163.  
  164. // check for exit signal
  165. // 0 is saving complete
  166. // other is char_id
  167. unsigned int save_flag = 0;
  168.  
  169. // Initial position (it's possible to set it in conf file)
  170. struct point start_point = { 0, 53, 111 };
  171.  
  172. int console = 0;
  173.  
  174. //-----------------------------------------------------
  175. // Auth database
  176. //-----------------------------------------------------
  177. #define AUTH_TIMEOUT 30000
  178.  
  179. struct auth_node {
  180. int account_id;
  181. int char_id;
  182. uint32 login_id1;
  183. uint32 login_id2;
  184. uint32 ip;
  185. int sex;
  186. time_t expiration_time; // # of seconds 1/1/1970 (timestamp): Validity limit of the account (0 = unlimited)
  187. int gmlevel;
  188. };
  189.  
  190. static DBMap* auth_db; // int account_id -> struct auth_node*
  191.  
  192. //-----------------------------------------------------
  193. // Online User Database
  194. //-----------------------------------------------------
  195.  
  196. struct online_char_data {
  197. int account_id;
  198. int char_id;
  199. int fd;
  200. int waiting_disconnect;
  201. short server; // -2: unknown server, -1: not connected, 0+: id of server
  202. };
  203.  
  204. static DBMap* online_char_db; // int account_id -> struct online_char_data*
  205. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data);
  206.  
  207. static void* create_online_char_data(DBKey key, va_list args)
  208. {
  209. struct online_char_data* character;
  210. CREATE(character, struct online_char_data, 1);
  211. character->account_id = key.i;
  212. character->char_id = -1;
  213. character->server = -1;
  214. character->fd = -1;
  215. character->waiting_disconnect = -1;
  216. return character;
  217. }
  218.  
  219. void set_char_charselect(int account_id)
  220. {
  221. struct online_char_data* character;
  222.  
  223. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  224.  
  225. if( character->server > -1 )
  226. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  227. server[character->server].users--;
  228.  
  229. character->char_id = -1;
  230. character->server = -1;
  231.  
  232. if(character->waiting_disconnect != -1) {
  233. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  234. character->waiting_disconnect = -1;
  235. }
  236.  
  237. if (login_fd > 0 && !session[login_fd]->flag.eof)
  238. {
  239. WFIFOHEAD(login_fd,6);
  240. WFIFOW(login_fd,0) = 0x272b;
  241. WFIFOL(login_fd,2) = account_id;
  242. WFIFOSET(login_fd,6);
  243. }
  244.  
  245. }
  246.  
  247. void set_char_online(int map_id, int char_id, int account_id)
  248. {
  249. struct online_char_data* character;
  250. struct mmo_charstatus *cp;
  251.  
  252. //Update DB
  253. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='1' WHERE `char_id`='%d'", char_db, char_id) )
  254. Sql_ShowDebug(sql_handle);
  255.  
  256. //Check to see for online conflicts
  257. character = (struct online_char_data*)idb_ensure(online_char_db, account_id, create_online_char_data);
  258. if( character->char_id != -1 && character->server > -1 && character->server != map_id )
  259. {
  260. ShowNotice("set_char_online: Character %d:%d marked in map server %d, but map server %d claims to have (%d:%d) online!\n",
  261. character->account_id, character->char_id, character->server, map_id, account_id, char_id);
  262. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  263. }
  264.  
  265. //Update state data
  266. character->char_id = char_id;
  267. character->server = map_id;
  268.  
  269. if( character->server > -1 )
  270. server[character->server].users++;
  271.  
  272. //Get rid of disconnect timer
  273. if(character->waiting_disconnect != -1) {
  274. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  275. character->waiting_disconnect = -1;
  276. }
  277.  
  278. //Set char online in guild cache. If char is in memory, use the guild id on it, otherwise seek it.
  279. cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  280. inter_guild_CharOnline(char_id, cp?cp->guild_id:-1);
  281.  
  282. //Notify login server
  283. if (login_fd > 0 && !session[login_fd]->flag.eof)
  284. {
  285. WFIFOHEAD(login_fd,6);
  286. WFIFOW(login_fd,0) = 0x272b;
  287. WFIFOL(login_fd,2) = account_id;
  288. WFIFOSET(login_fd,6);
  289. }
  290. }
  291.  
  292. void set_char_offline(int char_id, int account_id)
  293. {
  294. struct online_char_data* character;
  295.  
  296. if ( char_id == -1 )
  297. {
  298. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `account_id`='%d'", char_db, account_id) )
  299. Sql_ShowDebug(sql_handle);
  300. }
  301. else
  302. {
  303. struct mmo_charstatus* cp = (struct mmo_charstatus*)idb_get(char_db_,char_id);
  304. inter_guild_CharOffline(char_id, cp?cp->guild_id:-1);
  305. if (cp)
  306. idb_remove(char_db_,char_id);
  307.  
  308. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online`='0' WHERE `char_id`='%d'", char_db, char_id) )
  309. Sql_ShowDebug(sql_handle);
  310. }
  311.  
  312. if ((character = (struct online_char_data*)idb_get(online_char_db, account_id)) != NULL)
  313. { //We don't free yet to avoid aCalloc/aFree spamming during char change. [Skotlex]
  314. if( character->server > -1 )
  315. if( server[character->server].users > 0 ) // Prevent this value from going negative.
  316. server[character->server].users--;
  317.  
  318. if(character->waiting_disconnect != -1){
  319. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  320. character->waiting_disconnect = -1;
  321. }
  322.  
  323. if(character->char_id == char_id)
  324. {
  325. character->char_id = -1;
  326. character->server = -1;
  327. }
  328.  
  329. //FIXME? Why Kevin free'd the online information when the char was effectively in the map-server?
  330. }
  331.  
  332. //Remove char if 1- Set all offline, or 2- character is no longer connected to char-server.
  333. if (login_fd > 0 && !session[login_fd]->flag.eof && (char_id == -1 || character == NULL || character->fd == -1))
  334. {
  335. WFIFOHEAD(login_fd,6);
  336. WFIFOW(login_fd,0) = 0x272c;
  337. WFIFOL(login_fd,2) = account_id;
  338. WFIFOSET(login_fd,6);
  339. }
  340. }
  341.  
  342. static int char_db_setoffline(DBKey key, void* data, va_list ap)
  343. {
  344. struct online_char_data* character = (struct online_char_data*)data;
  345. int server = va_arg(ap, int);
  346. if (server == -1) {
  347. character->char_id = -1;
  348. character->server = -1;
  349. if(character->waiting_disconnect != -1){
  350. delete_timer(character->waiting_disconnect, chardb_waiting_disconnect);
  351. character->waiting_disconnect = -1;
  352. }
  353. } else if (character->server == server)
  354. character->server = -2; //In some map server that we aren't connected to.
  355. return 0;
  356. }
  357.  
  358. static int char_db_kickoffline(DBKey key, void* data, va_list ap)
  359. {
  360. struct online_char_data* character = (struct online_char_data*)data;
  361. int server_id = va_arg(ap, int);
  362.  
  363. if (server_id > -1 && character->server != server_id)
  364. return 0;
  365.  
  366. //Kick out any connected characters, and set them offline as appropiate.
  367. if (character->server > -1)
  368. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 1);
  369. else if (character->waiting_disconnect == -1)
  370. set_char_offline(character->char_id, character->account_id);
  371. else
  372. return 0; // fail
  373.  
  374. return 1;
  375. }
  376.  
  377. void set_all_offline(int id)
  378. {
  379. if (id < 0)
  380. ShowNotice("Sending all users offline.\n");
  381. else
  382. ShowNotice("Sending users of map-server %d offline.\n",id);
  383. online_char_db->foreach(online_char_db,char_db_kickoffline,id);
  384.  
  385. if (id >= 0 || login_fd <= 0 || session[login_fd]->flag.eof)
  386. return;
  387. //Tell login-server to also mark all our characters as offline.
  388. WFIFOHEAD(login_fd,2);
  389. WFIFOW(login_fd,0) = 0x2737;
  390. WFIFOSET(login_fd,2);
  391. }
  392.  
  393. void set_all_offline_sql(void)
  394. {
  395. //Set all players to 'OFFLINE'
  396. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", char_db) )
  397. Sql_ShowDebug(sql_handle);
  398. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `online` = '0'", guild_member_db) )
  399. Sql_ShowDebug(sql_handle);
  400. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `connect_member` = '0'", guild_db) )
  401. Sql_ShowDebug(sql_handle);
  402. }
  403.  
  404. static void* create_charstatus(DBKey key, va_list args)
  405. {
  406. struct mmo_charstatus *cp;
  407. cp = (struct mmo_charstatus *) aCalloc(1,sizeof(struct mmo_charstatus));
  408. cp->char_id = key.i;
  409. return cp;
  410. }
  411. #endif //TXT_SQL_CONVERT
  412.  
  413. int mmo_char_tosql(int char_id, struct mmo_charstatus* p)
  414. {
  415. int i = 0;
  416. int count = 0;
  417. int diff = 0;
  418. char save_status[128]; //For displaying save information. [Skotlex]
  419. struct mmo_charstatus *cp;
  420. StringBuf buf;
  421.  
  422. if (char_id!=p->char_id) return 0;
  423.  
  424. #ifndef TXT_SQL_CONVERT
  425. cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
  426. #else
  427. cp = (struct mmo_charstatus*)aCalloc(1, sizeof(struct mmo_charstatus));
  428. #endif
  429.  
  430. StringBuf_Init(&buf);
  431. memset(save_status, 0, sizeof(save_status));
  432.  
  433. //map inventory data
  434. if( memcmp(p->inventory, cp->inventory, sizeof(p->inventory)) )
  435. {
  436. memitemdata_to_sql(p->inventory, MAX_INVENTORY, p->char_id, TABLE_INVENTORY);
  437. strcat(save_status, " inventory");
  438. }
  439.  
  440. //map cart data
  441. if( memcmp(p->cart, cp->cart, sizeof(p->cart)) )
  442. {
  443. memitemdata_to_sql(p->cart, MAX_CART, p->char_id, TABLE_CART);
  444. strcat(save_status, " cart");
  445. }
  446.  
  447. //map storage data
  448. if( memcmp(p->storage.items, cp->storage.items, sizeof(p->storage.items)) )
  449. {
  450. memitemdata_to_sql(p->storage.items, MAX_STORAGE, p->account_id, TABLE_STORAGE);
  451. strcat(save_status, " storage");
  452. }
  453.  
  454. #ifdef TXT_SQL_CONVERT
  455. { //Insert the barebones to then update the rest.
  456. char esc_name[NAME_LENGTH*2+1];
  457.  
  458. Sql_EscapeStringLen(sql_handle, esc_name, p->name, strnlen(p->name, NAME_LENGTH));
  459. if( SQL_ERROR == Sql_Query(sql_handle, "REPLACE INTO `%s` (`char_id`, `account_id`, `char_num`, `name`) VALUES ('%d', '%d', '%d', '%s')",
  460. char_db, p->char_id, p->account_id, p->slot, esc_name) )
  461. {
  462. Sql_ShowDebug(sql_handle);
  463. }
  464.  
  465. strcat(save_status, " creation");
  466. }
  467. #endif
  468.  
  469. int low, top, mid;
  470.  
  471. if (p->costume_low > 0)
  472. top = p->costume_low;
  473. else
  474. top = p->head_low;
  475.  
  476. if (p->costume_top > 0)
  477. mid = p->costume_top;
  478. else
  479. mid = p->head_top;
  480.  
  481. if (p->costume_mid > 0)
  482. low = p->costume_mid;
  483. else
  484. low = p->head_mid;
  485.  
  486.  
  487. if (
  488. (p->base_exp != cp->base_exp) || (p->base_level != cp->base_level) ||
  489. (p->job_level != cp->job_level) || (p->job_exp != cp->job_exp) ||
  490. (p->zeny != cp->zeny) ||
  491. (p->last_point.map != cp->last_point.map) ||
  492. (p->last_point.x != cp->last_point.x) || (p->last_point.y != cp->last_point.y) ||
  493. (p->max_hp != cp->max_hp) || (p->hp != cp->hp) ||
  494. (p->max_sp != cp->max_sp) || (p->sp != cp->sp) ||
  495. (p->status_point != cp->status_point) || (p->skill_point != cp->skill_point) ||
  496. (p->str != cp->str) || (p->agi != cp->agi) || (p->vit != cp->vit) ||
  497. (p->int_ != cp->int_) || (p->dex != cp->dex) || (p->luk != cp->luk) ||
  498. (p->option != cp->option) ||
  499. (p->party_id != cp->party_id) || (p->guild_id != cp->guild_id) ||
  500. (p->pet_id != cp->pet_id) || (p->weapon != cp->weapon) || (p->hom_id != cp->hom_id) ||
  501. (p->ele_id != cp->ele_id) || (p->shield != cp->shield) ||
  502. (p->delete_date != cp->delete_date) ||
  503. (p->rename != cp->rename) || (p->robe != cp->robe)
  504. )
  505. { //Save status
  506. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `base_level`='%d', `job_level`='%d',"
  507. "`base_exp`='%u', `job_exp`='%u', `zeny`='%d',"
  508. "`max_hp`='%d',`hp`='%d',`max_sp`='%d',`sp`='%d',`status_point`='%d',`skill_point`='%d',"
  509. "`str`='%d',`agi`='%d',`vit`='%d',`int`='%d',`dex`='%d',`luk`='%d',"
  510. "`option`='%d',`party_id`='%d',`guild_id`='%d',`pet_id`='%d',`homun_id`='%d',`elemental_id`='%d',"
  511. "`weapon`='%d',`shield`='%d',`head_top`='%d',`head_mid`='%d',`head_bottom`='%d',"
  512. "`last_map`='%s',`last_x`='%d',`last_y`='%d',`save_map`='%s',`save_x`='%d',`save_y`='%d', `rename`='%d',"
  513. "`delete_date`='%lu',`robe`='%d'"
  514. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  515. char_db, p->base_level, p->job_level,
  516. p->base_exp, p->job_exp, p->zeny,
  517. p->max_hp, p->hp, p->max_sp, p->sp, p->status_point, p->skill_point,
  518. p->str, p->agi, p->vit, p->int_, p->dex, p->luk,
  519. p->option, p->party_id, p->guild_id, p->pet_id, p->hom_id, p->ele_id,
  520. p->weapon, p->shield, top, mid, low,
  521. mapindex_id2name(p->last_point.map), p->last_point.x, p->last_point.y,
  522. mapindex_id2name(p->save_point.map), p->save_point.x, p->save_point.y, p->rename,
  523. (unsigned long)p->delete_date, // FIXME: platform-dependent size
  524. p->robe,
  525. p->account_id, p->char_id) )
  526. {
  527. Sql_ShowDebug(sql_handle);
  528. }
  529. strcat(save_status, " status");
  530. }
  531.  
  532. //Values that will seldom change (to speed up saving)
  533. if (
  534. (p->hair != cp->hair) || (p->hair_color != cp->hair_color) || (p->clothes_color != cp->clothes_color) ||
  535. (p->class_ != cp->class_) ||
  536. (p->partner_id != cp->partner_id) || (p->father != cp->father) ||
  537. (p->mother != cp->mother) || (p->child != cp->child) ||
  538. (p->karma != cp->karma) || (p->manner != cp->manner) ||
  539. (p->fame != cp->fame)
  540. )
  541. {
  542. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d',"
  543. "`hair`='%d',`hair_color`='%d',`clothes_color`='%d',"
  544. "`partner_id`='%d', `father`='%d', `mother`='%d', `child`='%d',"
  545. "`karma`='%d',`manner`='%d', `fame`='%d'"
  546. " WHERE `account_id`='%d' AND `char_id` = '%d'",
  547. char_db, p->class_,
  548. p->hair, p->hair_color, p->clothes_color,
  549. p->partner_id, p->father, p->mother, p->child,
  550. p->karma, p->manner, p->fame,
  551. p->account_id, p->char_id) )
  552. {
  553. Sql_ShowDebug(sql_handle);
  554. }
  555.  
  556. strcat(save_status, " status2");
  557. }
  558.  
  559. /* Mercenary Owner */
  560. if( (p->mer_id != cp->mer_id) ||
  561. (p->arch_calls != cp->arch_calls) || (p->arch_faith != cp->arch_faith) ||
  562. (p->spear_calls != cp->spear_calls) || (p->spear_faith != cp->spear_faith) ||
  563. (p->sword_calls != cp->sword_calls) || (p->sword_faith != cp->sword_faith) )
  564. {
  565. mercenary_owner_tosql(char_id, p);
  566. strcat(save_status, " mercenary");
  567. }
  568.  
  569. //memo points
  570. if( memcmp(p->memo_point, cp->memo_point, sizeof(p->memo_point)) )
  571. {
  572. char esc_mapname[NAME_LENGTH*2+1];
  573.  
  574. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  575. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, p->char_id) )
  576. Sql_ShowDebug(sql_handle);
  577.  
  578. //insert here.
  579. StringBuf_Clear(&buf);
  580. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`map`,`x`,`y`) VALUES ", memo_db);
  581. for( i = 0, count = 0; i < MAX_MEMOPOINTS; ++i )
  582. {
  583. if( p->memo_point[i].map )
  584. {
  585. if( count )
  586. StringBuf_AppendStr(&buf, ",");
  587. Sql_EscapeString(sql_handle, esc_mapname, mapindex_id2name(p->memo_point[i].map));
  588. StringBuf_Printf(&buf, "('%d', '%s', '%d', '%d')", char_id, esc_mapname, p->memo_point[i].x, p->memo_point[i].y);
  589. ++count;
  590. }
  591. }
  592. if( count )
  593. {
  594. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  595. Sql_ShowDebug(sql_handle);
  596. }
  597.  
  598. strcat(save_status, " memo");
  599. }
  600.  
  601. //FIXME: is this neccessary? [ultramage]
  602. for(i=0;i<MAX_SKILL;i++)
  603. if ((p->skill[i].lv != 0) && (p->skill[i].id == 0))
  604. p->skill[i].id = i; // Fix skill tree
  605.  
  606.  
  607. //skills
  608. if( memcmp(p->skill, cp->skill, sizeof(p->skill)) )
  609. {
  610. //`skill` (`char_id`, `id`, `lv`)
  611. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, p->char_id) )
  612. Sql_ShowDebug(sql_handle);
  613.  
  614. StringBuf_Clear(&buf);
  615. StringBuf_Printf(&buf, "INSERT INTO `%s`(`char_id`,`id`,`lv`) VALUES ", skill_db);
  616. //insert here.
  617. for( i = 0, count = 0; i < MAX_SKILL; ++i )
  618. {
  619. if(p->skill[i].id && p->skill[i].flag!=1)
  620. {
  621. if( count )
  622. StringBuf_AppendStr(&buf, ",");
  623. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->skill[i].id, (p->skill[i].flag == 0 ? p->skill[i].lv : p->skill[i].flag - 2));
  624. ++count;
  625. }
  626. }
  627. if( count )
  628. {
  629. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  630. Sql_ShowDebug(sql_handle);
  631. }
  632.  
  633. strcat(save_status, " skills");
  634. }
  635.  
  636. diff = 0;
  637. for(i = 0; i < MAX_FRIENDS; i++){
  638. if(p->friends[i].char_id != cp->friends[i].char_id ||
  639. p->friends[i].account_id != cp->friends[i].account_id){
  640. diff = 1;
  641. break;
  642. }
  643. }
  644.  
  645. if(diff == 1)
  646. { //Save friends
  647. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", friend_db, char_id) )
  648. Sql_ShowDebug(sql_handle);
  649.  
  650. StringBuf_Clear(&buf);
  651. StringBuf_Printf(&buf, "INSERT INTO `%s` (`char_id`, `friend_account`, `friend_id`) VALUES ", friend_db);
  652. for( i = 0, count = 0; i < MAX_FRIENDS; ++i )
  653. {
  654. if( p->friends[i].char_id > 0 )
  655. {
  656. if( count )
  657. StringBuf_AppendStr(&buf, ",");
  658. StringBuf_Printf(&buf, "('%d','%d','%d')", char_id, p->friends[i].account_id, p->friends[i].char_id);
  659. count++;
  660. }
  661. }
  662. if( count )
  663. {
  664. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  665. Sql_ShowDebug(sql_handle);
  666. else
  667. strcat(save_status, " friends");
  668. }
  669. else //Friend list cleared.
  670. strcat(save_status, " friends");
  671. }
  672.  
  673. #ifdef HOTKEY_SAVING
  674. // hotkeys
  675. StringBuf_Clear(&buf);
  676. StringBuf_Printf(&buf, "REPLACE INTO `%s` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`) VALUES ", hotkey_db);
  677. diff = 0;
  678. for(i = 0; i < ARRAYLENGTH(p->hotkeys); i++){
  679. if(memcmp(&p->hotkeys[i], &cp->hotkeys[i], sizeof(struct hotkey)))
  680. {
  681. if( diff )
  682. StringBuf_AppendStr(&buf, ",");// not the first hotkey
  683. StringBuf_Printf(&buf, "('%d','%u','%u','%u','%u')", char_id, (unsigned int)i, (unsigned int)p->hotkeys[i].type, p->hotkeys[i].id , (unsigned int)p->hotkeys[i].lv);
  684. diff = 1;
  685. }
  686. }
  687. if(diff) {
  688. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  689. Sql_ShowDebug(sql_handle);
  690. else
  691. strcat(save_status, " hotkeys");
  692. }
  693. #endif
  694. StringBuf_Destroy(&buf);
  695. if (save_status[0]!='\0' && save_log)
  696. ShowInfo("Saved char %d - %s:%s.\n", char_id, p->name, save_status);
  697. #ifndef TXT_SQL_CONVERT
  698. memcpy(cp, p, sizeof(struct mmo_charstatus));
  699. #else
  700. aFree(cp);
  701. #endif
  702. return 0;
  703. }
  704.  
  705. /// Saves an array of 'item' entries into the specified table.
  706. int memitemdata_to_sql(const struct item items[], int max, int id, int tableswitch)
  707. {
  708. StringBuf buf;
  709. SqlStmt* stmt;
  710. int i;
  711. int j;
  712. const char* tablename;
  713. const char* selectoption;
  714. struct item item; // temp storage variable
  715. bool* flag; // bit array for inventory matching
  716. bool found;
  717.  
  718. switch (tableswitch) {
  719. case TABLE_INVENTORY: tablename = inventory_db; selectoption = "char_id"; break;
  720. case TABLE_CART: tablename = cart_db; selectoption = "char_id"; break;
  721. case TABLE_STORAGE: tablename = storage_db; selectoption = "account_id"; break;
  722. case TABLE_GUILD_STORAGE: tablename = guild_storage_db; selectoption = "guild_id"; break;
  723. default:
  724. ShowError("Invalid table name!\n");
  725. return 1;
  726. }
  727.  
  728.  
  729. // The following code compares inventory with current database values
  730. // and performs modification/deletion/insertion only on relevant rows.
  731. // This approach is more complicated than a trivial delete&insert, but
  732. // it significantly reduces cpu load on the database server.
  733.  
  734. StringBuf_Init(&buf);
  735. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`, `view`");
  736. for( j = 0; j < MAX_SLOTS; ++j )
  737. StringBuf_Printf(&buf, ", `card%d`", j);
  738. StringBuf_Printf(&buf, " FROM `%s` WHERE `%s`='%d'", tablename, selectoption, id);
  739.  
  740. stmt = SqlStmt_Malloc(sql_handle);
  741. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  742. || SQL_ERROR == SqlStmt_Execute(stmt) )
  743. {
  744. SqlStmt_ShowDebug(stmt);
  745. SqlStmt_Free(stmt);
  746. StringBuf_Destroy(&buf);
  747. return 1;
  748. }
  749.  
  750. SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &item.id, 0, NULL, NULL);
  751. SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &item.nameid, 0, NULL, NULL);
  752. SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &item.amount, 0, NULL, NULL);
  753. SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &item.equip, 0, NULL, NULL);
  754. SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &item.identify, 0, NULL, NULL);
  755. SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &item.refine, 0, NULL, NULL);
  756. SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &item.attribute, 0, NULL, NULL);
  757. SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &item.expire_time, 0, NULL, NULL);
  758. SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &item.vid, 0, NULL, NULL);
  759. for( j = 0; j < MAX_SLOTS; ++j )
  760. SqlStmt_BindColumn(stmt, 9+j, SQLDT_SHORT, &item.card[j], 0, NULL, NULL);
  761.  
  762. // bit array indicating which inventory items have already been matched
  763. flag = (bool*) aCallocA(max, sizeof(bool));
  764.  
  765. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  766. {
  767. found = false;
  768. // search for the presence of the item in the char's inventory
  769. for( i = 0; i < max; ++i )
  770. {
  771. // skip empty and already matched entries
  772. if( items[i].nameid == 0 || flag[i] )
  773. continue;
  774.  
  775. if( items[i].nameid == item.nameid
  776. && items[i].card[0] == item.card[0]
  777. && items[i].card[2] == item.card[2]
  778. && items[i].card[3] == item.card[3]
  779. ) { //They are the same item.
  780. ARR_FIND( 0, MAX_SLOTS, j, items[i].card[j] != item.card[j] );
  781. if( j == MAX_SLOTS &&
  782. items[i].amount == item.amount &&
  783. items[i].equip == item.equip &&
  784. items[i].identify == item.identify &&
  785. items[i].refine == item.refine &&
  786. items[i].attribute == item.attribute &&
  787. items[i].vid == item.vid &&
  788. items[i].expire_time == item.expire_time )
  789. ; //Do nothing.
  790. else
  791. {
  792. // update all fields.
  793. StringBuf_Clear(&buf);
  794. StringBuf_Printf(&buf, "UPDATE `%s` SET `amount`='%d', `equip`='%d', `identify`='%d', `refine`='%d',`attribute`='%d', `expire_time`='%u', `view`='%d'",
  795. tablename, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].vid);
  796. for( j = 0; j < MAX_SLOTS; ++j )
  797. StringBuf_Printf(&buf, ", `card%d`=%d", j, items[i].card[j]);
  798. StringBuf_Printf(&buf, " WHERE `id`='%d' LIMIT 1", item.id);
  799.  
  800. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  801. Sql_ShowDebug(sql_handle);
  802. }
  803.  
  804. found = flag[i] = true; //Item dealt with,
  805. break; //skip to next item in the db.
  806. }
  807. }
  808. if( !found )
  809. {// Item not present in inventory, remove it.
  810. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE from `%s` where `id`='%d'", tablename, item.id) )
  811. Sql_ShowDebug(sql_handle);
  812. }
  813. }
  814. SqlStmt_Free(stmt);
  815.  
  816. StringBuf_Clear(&buf);
  817. StringBuf_Printf(&buf, "INSERT INTO `%s`(`%s`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`,`view`", tablename, selectoption);
  818. for( j = 0; j < MAX_SLOTS; ++j )
  819. StringBuf_Printf(&buf, ", `card%d`", j);
  820. StringBuf_AppendStr(&buf, ") VALUES ");
  821.  
  822. found = false;
  823. // insert non-matched items into the db as new items
  824. for( i = 0; i < max; ++i )
  825. {
  826. // skip empty and already matched entries
  827. if( items[i].nameid == 0 || flag[i] )
  828. continue;
  829.  
  830. if( found )
  831. StringBuf_AppendStr(&buf, ",");
  832. else
  833. found = true;
  834.  
  835. StringBuf_Printf(&buf, "('%d', '%d', '%d', '%d', '%d', '%d', '%d', '%u','%d'",
  836. id, items[i].nameid, items[i].amount, items[i].equip, items[i].identify, items[i].refine, items[i].attribute, items[i].expire_time, items[i].vid);
  837. for( j = 0; j < MAX_SLOTS; ++j )
  838. StringBuf_Printf(&buf, ", '%d'", items[i].card[j]);
  839. StringBuf_AppendStr(&buf, ")");
  840. }
  841.  
  842. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  843. Sql_ShowDebug(sql_handle);
  844.  
  845. StringBuf_Destroy(&buf);
  846. aFree(flag);
  847.  
  848. return 0;
  849. }
  850.  
  851.  
  852. int mmo_char_tobuf(uint8* buf, struct mmo_charstatus* p);
  853.  
  854. #ifndef TXT_SQL_CONVERT
  855. //=====================================================================================================
  856. // Loads the basic character rooster for the given account. Returns total buffer used.
  857. int mmo_chars_fromsql(struct char_session_data* sd, uint8* buf)
  858. {
  859. SqlStmt* stmt;
  860. struct mmo_charstatus p;
  861. int j = 0, i;
  862. char last_map[MAP_NAME_LENGTH_EXT];
  863.  
  864. stmt = SqlStmt_Malloc(sql_handle);
  865. if( stmt == NULL )
  866. {
  867. SqlStmt_ShowDebug(stmt);
  868. return 0;
  869. }
  870. memset(&p, 0, sizeof(p));
  871.  
  872. // read char data
  873. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  874. "`char_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  875. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  876. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`hair`,`hair_color`,"
  877. "`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`rename`,`delete_date`,"
  878.  
  879. "`robe`"
  880. " FROM `%s` WHERE `account_id`='%d' AND `char_num` < '%d'", char_db, sd->account_id, MAX_CHARS)
  881. || SQL_ERROR == SqlStmt_Execute(stmt)
  882. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p.char_id, 0, NULL, NULL)
  883. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &p.slot, 0, NULL, NULL)
  884. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &p.name, sizeof(p.name), NULL, NULL)
  885. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_SHORT, &p.class_, 0, NULL, NULL)
  886. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_UINT, &p.base_level, 0, NULL, NULL)
  887. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p.job_level, 0, NULL, NULL)
  888. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p.base_exp, 0, NULL, NULL)
  889. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p.job_exp, 0, NULL, NULL)
  890. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &p.zeny, 0, NULL, NULL)
  891. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_SHORT, &p.str, 0, NULL, NULL)
  892. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p.agi, 0, NULL, NULL)
  893. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p.vit, 0, NULL, NULL)
  894. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p.int_, 0, NULL, NULL)
  895. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p.dex, 0, NULL, NULL)
  896. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p.luk, 0, NULL, NULL)
  897. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_INT, &p.max_hp, 0, NULL, NULL)
  898. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p.hp, 0, NULL, NULL)
  899. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p.max_sp, 0, NULL, NULL)
  900. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p.sp, 0, NULL, NULL)
  901. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_UINT, &p.status_point, 0, NULL, NULL)
  902. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p.skill_point, 0, NULL, NULL)
  903. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p.option, 0, NULL, NULL)
  904. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UCHAR, &p.karma, 0, NULL, NULL)
  905. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_SHORT, &p.manner, 0, NULL, NULL)
  906. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p.hair, 0, NULL, NULL)
  907. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_SHORT, &p.hair_color, 0, NULL, NULL)
  908. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_SHORT, &p.clothes_color, 0, NULL, NULL)
  909. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_SHORT, &p.weapon, 0, NULL, NULL)
  910. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_SHORT, &p.shield, 0, NULL, NULL)
  911. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_SHORT, &p.head_top, 0, NULL, NULL)
  912. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p.head_mid, 0, NULL, NULL)
  913. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p.head_bottom, 0, NULL, NULL)
  914. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  915. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p.rename, 0, NULL, NULL)
  916. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_UINT32, &p.delete_date, 0, NULL, NULL)
  917. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p.robe, 0, NULL, NULL)
  918. )
  919. {
  920. SqlStmt_ShowDebug(stmt);
  921. SqlStmt_Free(stmt);
  922. return 0;
  923. }
  924. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == SqlStmt_NextRow(stmt); i++ )
  925. {
  926. p.last_point.map = mapindex_name2id(last_map);
  927. sd->found_char[i] = p.char_id;
  928. j += mmo_char_tobuf(WBUFP(buf, j), &p);
  929. }
  930. for( ; i < MAX_CHARS; i++ )
  931. sd->found_char[i] = -1;
  932.  
  933. memset(sd->new_name,0,sizeof(sd->new_name));
  934.  
  935. SqlStmt_Free(stmt);
  936. return j;
  937. }
  938.  
  939. //=====================================================================================================
  940. int mmo_char_fromsql(int char_id, struct mmo_charstatus* p, bool load_everything)
  941. {
  942. int i,j;
  943. char t_msg[128] = "";
  944. struct mmo_charstatus* cp;
  945. StringBuf buf;
  946. SqlStmt* stmt;
  947. char last_map[MAP_NAME_LENGTH_EXT];
  948. char save_map[MAP_NAME_LENGTH_EXT];
  949. char point_map[MAP_NAME_LENGTH_EXT];
  950. struct point tmp_point;
  951. struct item tmp_item;
  952. struct s_skill tmp_skill;
  953. struct s_friend tmp_friend;
  954. #ifdef HOTKEY_SAVING
  955. struct hotkey tmp_hotkey;
  956. int hotkey_num;
  957. #endif
  958.  
  959. memset(p, 0, sizeof(struct mmo_charstatus));
  960.  
  961. if (save_log) ShowInfo("Char load request (%d)\n", char_id);
  962.  
  963. stmt = SqlStmt_Malloc(sql_handle);
  964. if( stmt == NULL )
  965. {
  966. SqlStmt_ShowDebug(stmt);
  967. return 0;
  968. }
  969.  
  970. // read char data
  971. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT "
  972. "`char_id`,`account_id`,`char_num`,`name`,`class`,`base_level`,`job_level`,`base_exp`,`job_exp`,`zeny`,"
  973. "`str`,`agi`,`vit`,`int`,`dex`,`luk`,`max_hp`,`hp`,`max_sp`,`sp`,"
  974. "`status_point`,`skill_point`,`option`,`karma`,`manner`,`party_id`,`guild_id`,`pet_id`,`homun_id`,`elemental_id`,`hair`,"
  975. "`hair_color`,`clothes_color`,`weapon`,`shield`,`head_top`,`head_mid`,`head_bottom`,`last_map`,`last_x`,`last_y`,"
  976. "`save_map`,`save_x`,`save_y`,`partner_id`,`father`,`mother`,`child`,`fame`,`rename`,`delete_date`,`robe`"
  977. " FROM `%s` WHERE `char_id`=? LIMIT 1", char_db)
  978. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  979. || SQL_ERROR == SqlStmt_Execute(stmt)
  980. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &p->char_id, 0, NULL, NULL)
  981. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &p->account_id, 0, NULL, NULL)
  982. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UCHAR, &p->slot, 0, NULL, NULL)
  983. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_STRING, &p->name, sizeof(p->name), NULL, NULL)
  984. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_SHORT, &p->class_, 0, NULL, NULL)
  985. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_UINT, &p->base_level, 0, NULL, NULL)
  986. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_UINT, &p->job_level, 0, NULL, NULL)
  987. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &p->base_exp, 0, NULL, NULL)
  988. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_UINT, &p->job_exp, 0, NULL, NULL)
  989. || SQL_ERROR == SqlStmt_BindColumn(stmt, 9, SQLDT_INT, &p->zeny, 0, NULL, NULL)
  990. || SQL_ERROR == SqlStmt_BindColumn(stmt, 10, SQLDT_SHORT, &p->str, 0, NULL, NULL)
  991. || SQL_ERROR == SqlStmt_BindColumn(stmt, 11, SQLDT_SHORT, &p->agi, 0, NULL, NULL)
  992. || SQL_ERROR == SqlStmt_BindColumn(stmt, 12, SQLDT_SHORT, &p->vit, 0, NULL, NULL)
  993. || SQL_ERROR == SqlStmt_BindColumn(stmt, 13, SQLDT_SHORT, &p->int_, 0, NULL, NULL)
  994. || SQL_ERROR == SqlStmt_BindColumn(stmt, 14, SQLDT_SHORT, &p->dex, 0, NULL, NULL)
  995. || SQL_ERROR == SqlStmt_BindColumn(stmt, 15, SQLDT_SHORT, &p->luk, 0, NULL, NULL)
  996. || SQL_ERROR == SqlStmt_BindColumn(stmt, 16, SQLDT_INT, &p->max_hp, 0, NULL, NULL)
  997. || SQL_ERROR == SqlStmt_BindColumn(stmt, 17, SQLDT_INT, &p->hp, 0, NULL, NULL)
  998. || SQL_ERROR == SqlStmt_BindColumn(stmt, 18, SQLDT_INT, &p->max_sp, 0, NULL, NULL)
  999. || SQL_ERROR == SqlStmt_BindColumn(stmt, 19, SQLDT_INT, &p->sp, 0, NULL, NULL)
  1000. || SQL_ERROR == SqlStmt_BindColumn(stmt, 20, SQLDT_UINT, &p->status_point, 0, NULL, NULL)
  1001. || SQL_ERROR == SqlStmt_BindColumn(stmt, 21, SQLDT_UINT, &p->skill_point, 0, NULL, NULL)
  1002. || SQL_ERROR == SqlStmt_BindColumn(stmt, 22, SQLDT_UINT, &p->option, 0, NULL, NULL)
  1003. || SQL_ERROR == SqlStmt_BindColumn(stmt, 23, SQLDT_UCHAR, &p->karma, 0, NULL, NULL)
  1004. || SQL_ERROR == SqlStmt_BindColumn(stmt, 24, SQLDT_SHORT, &p->manner, 0, NULL, NULL)
  1005. || SQL_ERROR == SqlStmt_BindColumn(stmt, 25, SQLDT_INT, &p->party_id, 0, NULL, NULL)
  1006. || SQL_ERROR == SqlStmt_BindColumn(stmt, 26, SQLDT_INT, &p->guild_id, 0, NULL, NULL)
  1007. || SQL_ERROR == SqlStmt_BindColumn(stmt, 27, SQLDT_INT, &p->pet_id, 0, NULL, NULL)
  1008. || SQL_ERROR == SqlStmt_BindColumn(stmt, 28, SQLDT_INT, &p->hom_id, 0, NULL, NULL)
  1009. || SQL_ERROR == SqlStmt_BindColumn(stmt, 29, SQLDT_INT, &p->ele_id, 0, NULL, NULL)
  1010. || SQL_ERROR == SqlStmt_BindColumn(stmt, 30, SQLDT_SHORT, &p->hair, 0, NULL, NULL)
  1011. || SQL_ERROR == SqlStmt_BindColumn(stmt, 31, SQLDT_SHORT, &p->hair_color, 0, NULL, NULL)
  1012. || SQL_ERROR == SqlStmt_BindColumn(stmt, 32, SQLDT_SHORT, &p->clothes_color, 0, NULL, NULL)
  1013. || SQL_ERROR == SqlStmt_BindColumn(stmt, 33, SQLDT_SHORT, &p->weapon, 0, NULL, NULL)
  1014. || SQL_ERROR == SqlStmt_BindColumn(stmt, 34, SQLDT_SHORT, &p->shield, 0, NULL, NULL)
  1015. || SQL_ERROR == SqlStmt_BindColumn(stmt, 35, SQLDT_SHORT, &p->head_top, 0, NULL, NULL)
  1016. || SQL_ERROR == SqlStmt_BindColumn(stmt, 36, SQLDT_SHORT, &p->head_mid, 0, NULL, NULL)
  1017. || SQL_ERROR == SqlStmt_BindColumn(stmt, 37, SQLDT_SHORT, &p->head_bottom, 0, NULL, NULL)
  1018. || SQL_ERROR == SqlStmt_BindColumn(stmt, 38, SQLDT_STRING, &last_map, sizeof(last_map), NULL, NULL)
  1019. || SQL_ERROR == SqlStmt_BindColumn(stmt, 39, SQLDT_SHORT, &p->last_point.x, 0, NULL, NULL)
  1020. || SQL_ERROR == SqlStmt_BindColumn(stmt, 40, SQLDT_SHORT, &p->last_point.y, 0, NULL, NULL)
  1021. || SQL_ERROR == SqlStmt_BindColumn(stmt, 41, SQLDT_STRING, &save_map, sizeof(save_map), NULL, NULL)
  1022. || SQL_ERROR == SqlStmt_BindColumn(stmt, 42, SQLDT_SHORT, &p->save_point.x, 0, NULL, NULL)
  1023. || SQL_ERROR == SqlStmt_BindColumn(stmt, 43, SQLDT_SHORT, &p->save_point.y, 0, NULL, NULL)
  1024. || SQL_ERROR == SqlStmt_BindColumn(stmt, 44, SQLDT_INT, &p->partner_id, 0, NULL, NULL)
  1025. || SQL_ERROR == SqlStmt_BindColumn(stmt, 45, SQLDT_INT, &p->father, 0, NULL, NULL)
  1026. || SQL_ERROR == SqlStmt_BindColumn(stmt, 46, SQLDT_INT, &p->mother, 0, NULL, NULL)
  1027. || SQL_ERROR == SqlStmt_BindColumn(stmt, 47, SQLDT_INT, &p->child, 0, NULL, NULL)
  1028. || SQL_ERROR == SqlStmt_BindColumn(stmt, 48, SQLDT_INT, &p->fame, 0, NULL, NULL)
  1029. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_SHORT, &p->rename, 0, NULL, NULL)
  1030. || SQL_ERROR == SqlStmt_BindColumn(stmt, 49, SQLDT_UINT32, &p->delete_date, 0, NULL, NULL)
  1031.  
  1032. )
  1033. {
  1034. SqlStmt_ShowDebug(stmt);
  1035. SqlStmt_Free(stmt);
  1036. return 0;
  1037. }
  1038. if( SQL_ERROR == SqlStmt_NextRow(stmt) )
  1039. {
  1040. ShowError("Requested non-existant character id: %d!\n", char_id);
  1041. SqlStmt_Free(stmt);
  1042. return 0;
  1043. }
  1044. p->last_point.map = mapindex_name2id(last_map);
  1045. p->save_point.map = mapindex_name2id(save_map);
  1046.  
  1047. strcat(t_msg, " status");
  1048.  
  1049. if (!load_everything) // For quick selection of data when displaying the char menu
  1050. {
  1051. SqlStmt_Free(stmt);
  1052. return 1;
  1053. }
  1054.  
  1055. //read memo data
  1056. //`memo` (`memo_id`,`char_id`,`map`,`x`,`y`)
  1057. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `map`,`x`,`y` FROM `%s` WHERE `char_id`=? ORDER by `memo_id` LIMIT %d", memo_db, MAX_MEMOPOINTS)
  1058. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1059. || SQL_ERROR == SqlStmt_Execute(stmt)
  1060. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &point_map, sizeof(point_map), NULL, NULL)
  1061. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_point.x, 0, NULL, NULL)
  1062. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_point.y, 0, NULL, NULL) )
  1063. SqlStmt_ShowDebug(stmt);
  1064.  
  1065. for( i = 0; i < MAX_MEMOPOINTS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1066. {
  1067. tmp_point.map = mapindex_name2id(point_map);
  1068. memcpy(&p->memo_point[i], &tmp_point, sizeof(tmp_point));
  1069. }
  1070. strcat(t_msg, " memo");
  1071.  
  1072. //read inventory
  1073. //`inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  1074. StringBuf_Init(&buf);
  1075. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`,`view`");
  1076. for( i = 0; i < MAX_SLOTS; ++i )
  1077. StringBuf_Printf(&buf, ", `card%d`", i);
  1078. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", inventory_db, MAX_INVENTORY);
  1079.  
  1080. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1081. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1082. || SQL_ERROR == SqlStmt_Execute(stmt)
  1083. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1084. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1085. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1086. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1087. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1088. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1089. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1090. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1091. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &tmp_item.vid, 0, NULL, NULL)
  1092. )
  1093. SqlStmt_ShowDebug(stmt);
  1094. for( i = 0; i < MAX_SLOTS; ++i )
  1095. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 9+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1096. SqlStmt_ShowDebug(stmt);
  1097.  
  1098. for( i = 0; i < MAX_INVENTORY && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1099. memcpy(&p->inventory[i], &tmp_item, sizeof(tmp_item));
  1100.  
  1101. strcat(t_msg, " inventory");
  1102.  
  1103. //read cart
  1104. //`cart_inventory` (`id`,`char_id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `card0`, `card1`, `card2`, `card3`)
  1105. StringBuf_Clear(&buf);
  1106. StringBuf_AppendStr(&buf, "SELECT `id`, `nameid`, `amount`, `equip`, `identify`, `refine`, `attribute`, `expire_time`,`view`");
  1107. for( j = 0; j < MAX_SLOTS; ++j )
  1108. StringBuf_Printf(&buf, ", `card%d`", j);
  1109. StringBuf_Printf(&buf, " FROM `%s` WHERE `char_id`=? LIMIT %d", cart_db, MAX_CART);
  1110.  
  1111. if( SQL_ERROR == SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  1112. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1113. || SQL_ERROR == SqlStmt_Execute(stmt)
  1114. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_item.id, 0, NULL, NULL)
  1115. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_SHORT, &tmp_item.nameid, 0, NULL, NULL)
  1116. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_SHORT, &tmp_item.amount, 0, NULL, NULL)
  1117. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_item.equip, 0, NULL, NULL)
  1118. || SQL_ERROR == SqlStmt_BindColumn(stmt, 4, SQLDT_CHAR, &tmp_item.identify, 0, NULL, NULL)
  1119. || SQL_ERROR == SqlStmt_BindColumn(stmt, 5, SQLDT_CHAR, &tmp_item.refine, 0, NULL, NULL)
  1120. || SQL_ERROR == SqlStmt_BindColumn(stmt, 6, SQLDT_CHAR, &tmp_item.attribute, 0, NULL, NULL)
  1121. || SQL_ERROR == SqlStmt_BindColumn(stmt, 7, SQLDT_UINT, &tmp_item.expire_time, 0, NULL, NULL)
  1122. || SQL_ERROR == SqlStmt_BindColumn(stmt, 8, SQLDT_INT, &tmp_item.vid, 0, NULL, NULL)
  1123. )
  1124. SqlStmt_ShowDebug(stmt);
  1125. for( i = 0; i < MAX_SLOTS; ++i )
  1126. if( SQL_ERROR == SqlStmt_BindColumn(stmt, 9+i, SQLDT_SHORT, &tmp_item.card[i], 0, NULL, NULL) )
  1127. SqlStmt_ShowDebug(stmt);
  1128.  
  1129. for( i = 0; i < MAX_CART && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1130. memcpy(&p->cart[i], &tmp_item, sizeof(tmp_item));
  1131. strcat(t_msg, " cart");
  1132.  
  1133. //read storage
  1134. storage_fromsql(p->account_id, &p->storage);
  1135. strcat(t_msg, " storage");
  1136.  
  1137. //read skill
  1138. //`skill` (`char_id`, `id`, `lv`)
  1139. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `id`, `lv` FROM `%s` WHERE `char_id`=? LIMIT %d", skill_db, MAX_SKILL)
  1140. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1141. || SQL_ERROR == SqlStmt_Execute(stmt)
  1142. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_USHORT, &tmp_skill.id, 0, NULL, NULL)
  1143. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_USHORT, &tmp_skill.lv, 0, NULL, NULL) )
  1144. SqlStmt_ShowDebug(stmt);
  1145. tmp_skill.flag = 0;
  1146.  
  1147. for( i = 0; i < MAX_SKILL && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1148. {
  1149. if( tmp_skill.id < ARRAYLENGTH(p->skill) )
  1150. memcpy(&p->skill[tmp_skill.id], &tmp_skill, sizeof(tmp_skill));
  1151. else
  1152. ShowWarning("mmo_char_fromsql: ignoring invalid skill (id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", tmp_skill.id, tmp_skill.lv, p->name, p->account_id, p->char_id);
  1153. }
  1154. strcat(t_msg, " skills");
  1155.  
  1156. //read friends
  1157. //`friends` (`char_id`, `friend_account`, `friend_id`)
  1158. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT c.`account_id`, c.`char_id`, c.`name` FROM `%s` c LEFT JOIN `%s` f ON f.`friend_account` = c.`account_id` AND f.`friend_id` = c.`char_id` WHERE f.`char_id`=? LIMIT %d", char_db, friend_db, MAX_FRIENDS)
  1159. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1160. || SQL_ERROR == SqlStmt_Execute(stmt)
  1161. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &tmp_friend.account_id, 0, NULL, NULL)
  1162. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &tmp_friend.char_id, 0, NULL, NULL)
  1163. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &tmp_friend.name, sizeof(tmp_friend.name), NULL, NULL) )
  1164. SqlStmt_ShowDebug(stmt);
  1165.  
  1166. for( i = 0; i < MAX_FRIENDS && SQL_SUCCESS == SqlStmt_NextRow(stmt); ++i )
  1167. memcpy(&p->friends[i], &tmp_friend, sizeof(tmp_friend));
  1168. strcat(t_msg, " friends");
  1169.  
  1170. #ifdef HOTKEY_SAVING
  1171. //read hotkeys
  1172. //`hotkey` (`char_id`, `hotkey`, `type`, `itemskill_id`, `skill_lvl`
  1173. if( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `hotkey`, `type`, `itemskill_id`, `skill_lvl` FROM `%s` WHERE `char_id`=?", hotkey_db)
  1174. || SQL_ERROR == SqlStmt_BindParam(stmt, 0, SQLDT_INT, &char_id, 0)
  1175. || SQL_ERROR == SqlStmt_Execute(stmt)
  1176. || SQL_ERROR == SqlStmt_BindColumn(stmt, 0, SQLDT_INT, &hotkey_num, 0, NULL, NULL)
  1177. || SQL_ERROR == SqlStmt_BindColumn(stmt, 1, SQLDT_UCHAR, &tmp_hotkey.type, 0, NULL, NULL)
  1178. || SQL_ERROR == SqlStmt_BindColumn(stmt, 2, SQLDT_UINT, &tmp_hotkey.id, 0, NULL, NULL)
  1179. || SQL_ERROR == SqlStmt_BindColumn(stmt, 3, SQLDT_USHORT, &tmp_hotkey.lv, 0, NULL, NULL) )
  1180. SqlStmt_ShowDebug(stmt);
  1181.  
  1182. while( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  1183. {
  1184. if( hotkey_num >= 0 && hotkey_num < MAX_HOTKEYS )
  1185. memcpy(&p->hotkeys[hotkey_num], &tmp_hotkey, sizeof(tmp_hotkey));
  1186. else
  1187. ShowWarning("mmo_char_fromsql: ignoring invalid hotkey (hotkey=%d,type=%u,id=%u,lv=%u) of character %s (AID=%d,CID=%d)\n", hotkey_num, tmp_hotkey.type, tmp_hotkey.id, tmp_hotkey.lv, p->name, p->account_id, p->char_id);
  1188. }
  1189. strcat(t_msg, " hotkeys");
  1190. #endif
  1191.  
  1192. /* Mercenary Owner DataBase */
  1193. mercenary_owner_fromsql(char_id, p);
  1194. strcat(t_msg, " mercenary");
  1195.  
  1196.  
  1197. if (save_log) ShowInfo("Loaded char (%d - %s): %s\n", char_id, p->name, t_msg); //ok. all data load successfuly!
  1198. SqlStmt_Free(stmt);
  1199. StringBuf_Destroy(&buf);
  1200.  
  1201. cp = (struct mmo_charstatus*)idb_ensure(char_db_, char_id, create_charstatus);
  1202. memcpy(cp, p, sizeof(struct mmo_charstatus));
  1203. return 1;
  1204. }
  1205.  
  1206. //==========================================================================================================
  1207. int mmo_char_sql_init(void)
  1208. {
  1209. ShowInfo("Begin Initializing.......\n");
  1210. char_db_= idb_alloc(DB_OPT_RELEASE_DATA);
  1211.  
  1212. if(char_per_account == 0){
  1213. ShowStatus("Chars per Account: 'Unlimited'.......\n");
  1214. }else{
  1215. ShowStatus("Chars per Account: '%d'.......\n", char_per_account);
  1216. }
  1217.  
  1218. //the 'set offline' part is now in check_login_conn ...
  1219. //if the server connects to loginserver
  1220. //it will dc all off players
  1221. //and send the loginserver the new state....
  1222.  
  1223. // Force all users offline in sql when starting char-server
  1224. // (useful when servers crashs and don't clean the database)
  1225. set_all_offline_sql();
  1226.  
  1227. ShowInfo("Finished initilizing.......\n");
  1228.  
  1229. return 0;
  1230. }
  1231.  
  1232. //-----------------------------------
  1233. // Function to change chararcter's names
  1234. //-----------------------------------
  1235. int rename_char_sql(struct char_session_data *sd, int char_id)
  1236. {
  1237. struct mmo_charstatus char_dat;
  1238. char esc_name[NAME_LENGTH*2+1];
  1239.  
  1240. if( sd->new_name[0] == 0 ) // Not ready for rename
  1241. return 2;
  1242.  
  1243. if( !mmo_char_fromsql(char_id, &char_dat, false) ) // Only the short data is needed.
  1244. return 2;
  1245.  
  1246. if( char_dat.rename == 0 )
  1247. return 1;
  1248.  
  1249. Sql_EscapeStringLen(sql_handle, esc_name, sd->new_name, strnlen(sd->new_name, NAME_LENGTH));
  1250.  
  1251. // check if the char exist
  1252. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` LIKE '%s'", char_db, esc_name) )
  1253. {
  1254. Sql_ShowDebug(sql_handle);
  1255. return 4;
  1256. }
  1257.  
  1258. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `name` = '%s', `rename` = '%d' WHERE `char_id` = '%d'", char_db, esc_name, --char_dat.rename, char_id) )
  1259. {
  1260. Sql_ShowDebug(sql_handle);
  1261. return 3;
  1262. }
  1263.  
  1264. // Change character's name into guild_db.
  1265. if( char_dat.guild_id )
  1266. inter_guild_charname_changed(char_dat.guild_id, sd->account_id, char_id, sd->new_name);
  1267.  
  1268. safestrncpy(char_dat.name, sd->new_name, NAME_LENGTH);
  1269. memset(sd->new_name,0,sizeof(sd->new_name));
  1270.  
  1271. // log change
  1272. if( log_char )
  1273. {
  1274. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1275. "VALUES (NOW(), '%s', '%d', '%d', '%s', '0', '0', '0', '0', '0', '0', '0', '0')",
  1276. charlog_db, "change char name", sd->account_id, char_dat.slot, esc_name) )
  1277. Sql_ShowDebug(sql_handle);
  1278. }
  1279.  
  1280. return 0;
  1281. }
  1282.  
  1283. int check_char_name(char * name, char * esc_name)
  1284. {
  1285. int i;
  1286.  
  1287. // check length of character name
  1288. if( name[0] == '\0' )
  1289. return -2; // empty character name
  1290.  
  1291. // check content of character name
  1292. if( remove_control_chars(name) )
  1293. return -2; // control chars in name
  1294.  
  1295. // check for reserved names
  1296. if( strcmpi(name, main_chat_nick) == 0 || strcmpi(name, wisp_server_name) == 0 )
  1297. return -1; // nick reserved for internal server messages
  1298.  
  1299. // Check Authorised letters/symbols in the name of the character
  1300. if( char_name_option == 1 )
  1301. { // only letters/symbols in char_name_letters are authorised
  1302. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1303. if( strchr(char_name_letters, name[i]) == NULL )
  1304. return -2;
  1305. }
  1306. else if( char_name_option == 2 )
  1307. { // letters/symbols in char_name_letters are forbidden
  1308. for( i = 0; i < NAME_LENGTH && name[i]; i++ )
  1309. if( strchr(char_name_letters, name[i]) != NULL )
  1310. return -2;
  1311. }
  1312. // check name (already in use?)
  1313. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
  1314. {
  1315. Sql_ShowDebug(sql_handle);
  1316. return -2;
  1317. }
  1318. if( Sql_NumRows(sql_handle) > 0 )
  1319. return -1; // name already exists
  1320.  
  1321. return 0;
  1322. }
  1323.  
  1324. //-----------------------------------
  1325. // Function to create a new character
  1326. //-----------------------------------
  1327. int make_new_char_sql(struct char_session_data* sd, char* name_, int str, int agi, int vit, int int_, int dex, int luk, int slot, int hair_color, int hair_style)
  1328. {
  1329. char name[NAME_LENGTH];
  1330. char esc_name[NAME_LENGTH*2+1];
  1331. int char_id, flag;
  1332.  
  1333. safestrncpy(name, name_, NAME_LENGTH);
  1334. normalize_name(name,TRIM_CHARS);
  1335. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  1336.  
  1337. flag = check_char_name(name,esc_name);
  1338. if( flag < 0 )
  1339. return flag;
  1340.  
  1341. //check other inputs
  1342. if((slot >= MAX_CHARS) // slots
  1343.  
  1344. || (str + agi + vit + int_ + dex + luk != 6*5 ) // stats
  1345. || (str < 1 || str > 9 || agi < 1 || agi > 9 || vit < 1 || vit > 9 || int_ < 1 || int_ > 9 || dex < 1 || dex > 9 || luk < 1 || luk > 9) // individual stat values
  1346. || (str + int_ != 10 || agi + luk != 10 || vit + dex != 10) ) // pairs
  1347. return -2; // invalid input
  1348.  
  1349. // check the number of already existing chars in this account
  1350. if( char_per_account != 0 ) {
  1351. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) )
  1352. Sql_ShowDebug(sql_handle);
  1353. if( Sql_NumRows(sql_handle) >= char_per_account )
  1354. return -2; // character account limit exceeded
  1355. }
  1356. //[Ind/ro-resources.net] this locks gm accounts #1~98 from creating new chars.
  1357. if( sd->gmlevel > 1 && sd->gmlevel < 99 ) {
  1358. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d'", char_db, sd->account_id) )
  1359. Sql_ShowDebug(sql_handle);
  1360. if( Sql_NumRows(sql_handle) >= 1 )
  1361. return -2; // character account limit exceeded
  1362. }
  1363. // check char slot
  1364. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT 1 FROM `%s` WHERE `account_id` = '%d' AND `char_num` = '%d'", char_db, sd->account_id, slot) )
  1365. Sql_ShowDebug(sql_handle);
  1366. if( Sql_NumRows(sql_handle) > 0 )
  1367. return -2; // slot already in use
  1368.  
  1369. // validation success, log result
  1370. if (log_char) {
  1371. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`time`, `char_msg`,`account_id`,`char_num`,`name`,`str`,`agi`,`vit`,`int`,`dex`,`luk`,`hair`,`hair_color`)"
  1372. "VALUES (NOW(), '%s', '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d')",
  1373. charlog_db, "make new char", sd->account_id, slot, esc_name, str, agi, vit, int_, dex, luk, hair_style, hair_color) )
  1374. Sql_ShowDebug(sql_handle);
  1375. }
  1376.  
  1377. //Insert the new char entry to the database
  1378. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`account_id`, `char_num`, `name`, `zeny`, `str`, `agi`, `vit`, `int`, `dex`, `luk`, `max_hp`, `hp`,"
  1379. "`max_sp`, `sp`, `hair`, `hair_color`, `last_map`, `last_x`, `last_y`, `save_map`, `save_x`, `save_y`) VALUES ("
  1380. "'%d', '%d', '%s', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d', '%d','%d', '%d','%d', '%d', '%s', '%d', '%d', '%s', '%d', '%d')",
  1381. char_db, sd->account_id , slot, esc_name, start_zeny, str, agi, vit, int_, dex, luk,
  1382. (40 * (100 + vit)/100) , (40 * (100 + vit)/100 ), (11 * (100 + int_)/100), (11 * (100 + int_)/100), hair_style, hair_color,
  1383. mapindex_id2name(start_point.map), start_point.x, start_point.y, mapindex_id2name(start_point.map), start_point.x, start_point.y) )
  1384. {
  1385. Sql_ShowDebug(sql_handle);
  1386. return -2; //No, stop the procedure!
  1387. }
  1388. //Retrieve the newly auto-generated char id
  1389. char_id = (int)Sql_LastInsertId(sql_handle);
  1390. //Give the char the default items
  1391. if (start_weapon > 0) { //add Start Weapon (Knife?)
  1392. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_weapon, 1, 1) )
  1393. Sql_ShowDebug(sql_handle);
  1394. }
  1395. if (start_armor > 0) { //Add default armor (cotton shirt?)
  1396. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s` (`char_id`,`nameid`, `amount`, `identify`) VALUES ('%d', '%d', '%d', '%d')", inventory_db, char_id, start_armor, 1, 1) )
  1397. Sql_ShowDebug(sql_handle);
  1398. }
  1399.  
  1400. ShowInfo("Created char: account: %d, char: %d, slot: %d, name: %s\n", sd->account_id, char_id, slot, name);
  1401. return char_id;
  1402. }
  1403.  
  1404. /*----------------------------------------------------------------------------------------------------------*/
  1405. /* Divorce Players */
  1406. /*----------------------------------------------------------------------------------------------------------*/
  1407. int divorce_char_sql(int partner_id1, int partner_id2)
  1408. {
  1409. unsigned char buf[64];
  1410.  
  1411. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `partner_id`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, partner_id1, partner_id2) )
  1412. Sql_ShowDebug(sql_handle);
  1413. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE (`nameid`='%d' OR `nameid`='%d') AND (`char_id`='%d' OR `char_id`='%d')", inventory_db, WEDDING_RING_M, WEDDING_RING_F, partner_id1, partner_id2) )
  1414. Sql_ShowDebug(sql_handle);
  1415.  
  1416. WBUFW(buf,0) = 0x2b12;
  1417. WBUFL(buf,2) = partner_id1;
  1418. WBUFL(buf,6) = partner_id2;
  1419. mapif_sendall(buf,10);
  1420.  
  1421. return 0;
  1422. }
  1423.  
  1424. /*----------------------------------------------------------------------------------------------------------*/
  1425. /* Delete char - davidsiaw */
  1426. /*----------------------------------------------------------------------------------------------------------*/
  1427. /* Returns 0 if successful
  1428. * Returns < 0 for error
  1429. */
  1430. int delete_char_sql(int char_id)
  1431. {
  1432. char name[NAME_LENGTH];
  1433. char esc_name[NAME_LENGTH*2+1]; //Name needs be escaped.
  1434. int account_id, party_id, guild_id, hom_id, base_level, partner_id, father_id, mother_id;
  1435. char* data;
  1436. size_t len;
  1437.  
  1438. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name`,`account_id`,`party_id`,`guild_id`,`base_level`,`homun_id`,`partner_id`,`father`,`mother` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1439. Sql_ShowDebug(sql_handle);
  1440.  
  1441. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  1442. {
  1443. ShowError("delete_char_sql: Unable to fetch character data, deletion aborted.\n");
  1444. Sql_FreeResult(sql_handle);
  1445. return -1;
  1446. }
  1447.  
  1448. Sql_GetData(sql_handle, 0, &data, &len); safestrncpy(name, data, NAME_LENGTH);
  1449. Sql_GetData(sql_handle, 1, &data, NULL); account_id = atoi(data);
  1450. Sql_GetData(sql_handle, 2, &data, NULL); party_id = atoi(data);
  1451. Sql_GetData(sql_handle, 3, &data, NULL); guild_id = atoi(data);
  1452. Sql_GetData(sql_handle, 4, &data, NULL); base_level = atoi(data);
  1453. Sql_GetData(sql_handle, 5, &data, NULL); hom_id = atoi(data);
  1454. Sql_GetData(sql_handle, 6, &data, NULL); partner_id = atoi(data);
  1455. Sql_GetData(sql_handle, 7, &data, NULL); father_id = atoi(data);
  1456. Sql_GetData(sql_handle, 8, &data, NULL); mother_id = atoi(data);
  1457.  
  1458. Sql_EscapeStringLen(sql_handle, esc_name, name, min(len, NAME_LENGTH));
  1459. Sql_FreeResult(sql_handle);
  1460.  
  1461. //check for config char del condition [Lupus]
  1462. // TODO: Move this out to packet processing (0x68/0x1fb).
  1463. if( ( char_del_level > 0 && base_level >= char_del_level )
  1464. || ( char_del_level < 0 && base_level <= -char_del_level )
  1465. ) {
  1466. ShowInfo("Char deletion aborted: %s, BaseLevel: %i\n", name, base_level);
  1467. return -1;
  1468. }
  1469.  
  1470. /* Divorce [Wizputer] */
  1471. if( partner_id )
  1472. divorce_char_sql(char_id, partner_id);
  1473.  
  1474. /* De-addopt [Zephyrus] */
  1475. if( father_id || mother_id )
  1476. { // Char is Baby
  1477. unsigned char buf[64];
  1478.  
  1479. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `child`='0' WHERE `char_id`='%d' OR `char_id`='%d'", char_db, father_id, mother_id) )
  1480. Sql_ShowDebug(sql_handle);
  1481. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '410'AND (`char_id`='%d' OR `char_id`='%d')", skill_db, father_id, mother_id) )
  1482. Sql_ShowDebug(sql_handle);
  1483.  
  1484. WBUFW(buf,0) = 0x2b25;
  1485. WBUFL(buf,2) = father_id;
  1486. WBUFL(buf,6) = mother_id;
  1487. WBUFL(buf,10) = char_id; // Baby
  1488. mapif_sendall(buf,14);
  1489. }
  1490.  
  1491. //Make the character leave the party [Skotlex]
  1492. if (party_id)
  1493. inter_party_leave(party_id, account_id, char_id);
  1494.  
  1495. /* delete char's pet */
  1496. //Delete the hatched pet if you have one...
  1497. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d' AND `incuvate` = '0'", pet_db, char_id) )
  1498. Sql_ShowDebug(sql_handle);
  1499.  
  1500. //Delete all pets that are stored in eggs (inventory + cart)
  1501. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, inventory_db, inventory_db, char_id) )
  1502. Sql_ShowDebug(sql_handle);
  1503. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` USING `%s` JOIN `%s` ON `pet_id` = `card1`|`card2`<<16 WHERE `%s`.char_id = '%d' AND card0 = -256", pet_db, pet_db, cart_db, cart_db, char_id) )
  1504. Sql_ShowDebug(sql_handle);
  1505.  
  1506. /* remove homunculus */
  1507. if( hom_id )
  1508. mapif_homunculus_delete(hom_id);
  1509.  
  1510. /* remove mercenary data */
  1511. mercenary_owner_delete(char_id);
  1512.  
  1513. /* delete char's friends list */
  1514. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d'", friend_db, char_id) )
  1515. Sql_ShowDebug(sql_handle);
  1516.  
  1517. /* delete char from other's friend list */
  1518. //NOTE: Won't this cause problems for people who are already online? [Skotlex]
  1519. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `friend_id` = '%d'", friend_db, char_id) )
  1520. Sql_ShowDebug(sql_handle);
  1521.  
  1522. #ifdef HOTKEY_SAVING
  1523. /* delete hotkeys */
  1524. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", hotkey_db, char_id) )
  1525. Sql_ShowDebug(sql_handle);
  1526. #endif
  1527.  
  1528. /* delete inventory */
  1529. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", inventory_db, char_id) )
  1530. Sql_ShowDebug(sql_handle);
  1531.  
  1532. /* delete cart inventory */
  1533. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", cart_db, char_id) )
  1534. Sql_ShowDebug(sql_handle);
  1535.  
  1536. /* delete memo areas */
  1537. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", memo_db, char_id) )
  1538. Sql_ShowDebug(sql_handle);
  1539.  
  1540. /* delete character registry */
  1541. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `type`=3 AND `char_id`='%d'", reg_db, char_id) )
  1542. Sql_ShowDebug(sql_handle);
  1543.  
  1544. /* delete skills */
  1545. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", skill_db, char_id) )
  1546. Sql_ShowDebug(sql_handle);
  1547.  
  1548. #ifdef ENABLE_SC_SAVING
  1549. /* status changes */
  1550. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, account_id, char_id) )
  1551. Sql_ShowDebug(sql_handle);
  1552. #endif
  1553.  
  1554. if (log_char) {
  1555. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`char_msg`,`name`) VALUES (NOW(), '%d', '%d', 'Deleted char (CID %d)', '%s')",
  1556. charlog_db, account_id, 0, char_id, esc_name) )
  1557. Sql_ShowDebug(sql_handle);
  1558. }
  1559.  
  1560. /* delete character */
  1561. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  1562. Sql_ShowDebug(sql_handle);
  1563.  
  1564. /* No need as we used inter_guild_leave [Skotlex]
  1565. // Also delete info from guildtables.
  1566. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id`='%d'", guild_member_db, char_id) )
  1567. Sql_ShowDebug(sql_handle);
  1568. */
  1569.  
  1570. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `guild_id` FROM `%s` WHERE `char_id` = '%d'", guild_db, char_id) )
  1571. Sql_ShowDebug(sql_handle);
  1572. else if( Sql_NumRows(sql_handle) > 0 )
  1573. mapif_parse_BreakGuild(0,guild_id);
  1574. else if( guild_id )
  1575. inter_guild_leave(guild_id, account_id, char_id);// Leave your guild.
  1576. return 0;
  1577. }
  1578.  
  1579. //---------------------------------------------------------------------
  1580.  
  1581. // This function return the number of online players in all map-servers
  1582.  
  1583. //---------------------------------------------------------------------
  1584. int count_users(void)
  1585. {
  1586. int i, users;
  1587.  
  1588. users = 0;
  1589.  
  1590. for(i = 0; i < ARRAYLENGTH(server); i++) {
  1591.  
  1592. if (server[i].fd > 0) {
  1593.  
  1594. users += server[i].users;
  1595.  
  1596. }
  1597.  
  1598. }
  1599.  
  1600. return users;
  1601. }
  1602.  
  1603. // Writes char data to the buffer in the format used by the client.
  1604.  
  1605. // Used in packets 0x6b (chars info) and 0x6d (new char info)
  1606. // Returns the size
  1607. #define MAX_CHAR_BUF 136 //Max size (for WFIFOHEAD calls)
  1608. int mmo_char_tobuf(uint8* buffer, struct mmo_charstatus* p)
  1609. {
  1610. unsigned short offset = 0;
  1611. uint8* buf;
  1612.  
  1613. if( buffer == NULL || p == NULL )
  1614. return 0;
  1615.  
  1616. buf = WBUFP(buffer,0);
  1617. WBUFL(buf,0) = p->char_id;
  1618. WBUFL(buf,4) = min(p->base_exp, INT32_MAX);
  1619. WBUFL(buf,8) = p->zeny;
  1620. WBUFL(buf,12) = min(p->job_exp, INT32_MAX);
  1621. WBUFL(buf,16) = p->job_level;
  1622. WBUFL(buf,20) = 0; // probably opt1
  1623. WBUFL(buf,24) = 0; // probably opt2
  1624. WBUFL(buf,28) = p->option;
  1625. WBUFL(buf,32) = p->karma;
  1626. WBUFL(buf,36) = p->manner;
  1627. WBUFW(buf,40) = min(p->status_point, INT16_MAX);
  1628. #if PACKETVER > 20081217
  1629. WBUFL(buf,42) = p->hp;
  1630. WBUFL(buf,46) = p->max_hp;
  1631. offset+=4;
  1632. buf = WBUFP(buffer,offset);
  1633. #else
  1634. WBUFW(buf,42) = min(p->hp, INT16_MAX);
  1635.  
  1636. WBUFW(buf,44) = min(p->max_hp, INT16_MAX);
  1637. #endif
  1638. WBUFW(buf,46) = min(p->sp, INT16_MAX);
  1639.  
  1640. WBUFW(buf,48) = min(p->max_sp, INT16_MAX);
  1641. WBUFW(buf,50) = DEFAULT_WALK_SPEED; // p->speed;
  1642. WBUFW(buf,52) = p->class_;
  1643. WBUFW(buf,54) = p->hair;
  1644. WBUFW(buf,56) = p->option&0x7E80020 ? 0 : p->weapon; //When the weapon is sent and your option is riding, the client crashes on login!?
  1645. WBUFW(buf,58) = p->base_level;
  1646. WBUFW(buf,60) = min(p->skill_point, INT16_MAX);
  1647. WBUFW(buf,62) = p->head_bottom;
  1648. WBUFW(buf,64) = p->shield;
  1649. WBUFW(buf,66) = p->head_top;
  1650. WBUFW(buf,68) = p->head_mid;
  1651. WBUFW(buf,70) = p->hair_color;
  1652. WBUFW(buf,72) = p->clothes_color;
  1653. memcpy(WBUFP(buf,74), p->name, NAME_LENGTH);
  1654. WBUFB(buf,98) = min(p->str, UINT8_MAX);
  1655.  
  1656. WBUFB(buf,99) = min(p->agi, UINT8_MAX);
  1657.  
  1658. WBUFB(buf,100) = min(p->vit, UINT8_MAX);
  1659.  
  1660. WBUFB(buf,101) = min(p->int_, UINT8_MAX);
  1661.  
  1662. WBUFB(buf,102) = min(p->dex, UINT8_MAX);
  1663.  
  1664. WBUFB(buf,103) = min(p->luk, UINT8_MAX);
  1665. WBUFW(buf,104) = p->slot;
  1666. #if PACKETVER >= 20061023
  1667. WBUFW(buf,106) = ( p->rename > 0 ) ? 0 : 1;
  1668. offset += 2;
  1669. #endif
  1670. #if (PACKETVER >= 20100720 && PACKETVER <= 20100727) || PACKETVER >= 20100803
  1671. mapindex_getmapname_ext(mapindex_id2name(p->last_point.map), (char*)WBUFP(buf,108));
  1672. offset += MAP_NAME_LENGTH_EXT;
  1673. #endif
  1674. #if PACKETVER >= 20100803
  1675.  
  1676. WBUFL(buf,124) = TOL(p->delete_date);
  1677.  
  1678. offset += 4;
  1679.  
  1680. #endif
  1681. #if PACKETVER >= 20110111
  1682.  
  1683. WBUFL(buf,128) = p->robe;
  1684.  
  1685. offset += 4;
  1686.  
  1687. #endif
  1688. return 106+offset;
  1689. }
  1690. //----------------------------------------
  1691.  
  1692. // Function to send characters to a player
  1693.  
  1694. //----------------------------------------
  1695. int mmo_char_send006b(int fd, struct char_session_data* sd)
  1696. {
  1697. int j, offset = 0;
  1698. #if PACKETVER >= 20100413
  1699. offset += 3;
  1700. #endif
  1701.  
  1702. if (save_log)
  1703. ShowInfo("Loading Char Data ("CL_BOLD"%d"CL_RESET")\n",sd->account_id);
  1704.  
  1705. j = 24 + offset; // offset
  1706. WFIFOHEAD(fd,j + MAX_CHARS*MAX_CHAR_BUF);
  1707. WFIFOW(fd,0) = 0x6b;
  1708. #if PACKETVER >= 20100413
  1709. WFIFOB(fd,4) = MAX_CHARS_SLOTS; // Max available slots.
  1710. WFIFOB(fd,5) = MAX_CHARS; // Available slots.
  1711. WFIFOB(fd,6) = MAX_CHARS; // Premium slots.
  1712. #endif
  1713. memset(WFIFOP(fd,4 + offset), 0, 20); // unknown bytes
  1714. j+=mmo_chars_fromsql(sd, WFIFOP(fd,j));
  1715. WFIFOW(fd,2) = j; // packet len
  1716. WFIFOSET(fd,j);
  1717.  
  1718. return 0;
  1719. }
  1720.  
  1721. int char_married(int pl1, int pl2)
  1722. {
  1723. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `partner_id` FROM `%s` WHERE `char_id` = '%d'", char_db, pl1) )
  1724. Sql_ShowDebug(sql_handle);
  1725. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1726. {
  1727. char* data;
  1728.  
  1729. Sql_GetData(sql_handle, 0, &data, NULL);
  1730. if( pl2 == atoi(data) )
  1731. {
  1732. Sql_FreeResult(sql_handle);
  1733. return 1;
  1734. }
  1735. }
  1736. Sql_FreeResult(sql_handle);
  1737. return 0;
  1738. }
  1739.  
  1740. int char_child(int parent_id, int child_id)
  1741. {
  1742. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `child` FROM `%s` WHERE `char_id` = '%d'", char_db, parent_id) )
  1743. Sql_ShowDebug(sql_handle);
  1744. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1745. {
  1746. char* data;
  1747.  
  1748. Sql_GetData(sql_handle, 0, &data, NULL);
  1749. if( child_id == atoi(data) )
  1750. {
  1751. Sql_FreeResult(sql_handle);
  1752. return 1;
  1753. }
  1754. }
  1755. Sql_FreeResult(sql_handle);
  1756. return 0;
  1757. }
  1758.  
  1759. int char_family(int cid1, int cid2, int cid3)
  1760.  
  1761. {
  1762.  
  1763. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`partner_id`,`child` FROM `%s` WHERE `char_id` IN ('%d','%d','%d')", char_db, cid1, cid2, cid3) )
  1764. Sql_ShowDebug(sql_handle);
  1765. else while( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  1766. {
  1767. int charid;
  1768. int partnerid;
  1769. int childid;
  1770. char* data;
  1771.  
  1772. Sql_GetData(sql_handle, 0, &data, NULL); charid = atoi(data);
  1773. Sql_GetData(sql_handle, 1, &data, NULL); partnerid = atoi(data);
  1774. Sql_GetData(sql_handle, 2, &data, NULL); childid = atoi(data);
  1775.  
  1776. if( (cid1 == charid && ((cid2 == partnerid && cid3 == childid ) || (cid2 == childid && cid3 == partnerid))) ||
  1777.  
  1778. (cid1 == partnerid && ((cid2 == charid && cid3 == childid ) || (cid2 == childid && cid3 == charid ))) ||
  1779.  
  1780. (cid1 == childid && ((cid2 == charid && cid3 == partnerid) || (cid2 == partnerid && cid3 == charid ))) )
  1781. {
  1782. Sql_FreeResult(sql_handle);
  1783. return childid;
  1784. }
  1785. }
  1786. Sql_FreeResult(sql_handle);
  1787. return 0;
  1788. }
  1789.  
  1790. //----------------------------------------------------------------------
  1791.  
  1792. // Force disconnection of an online player (with account value) by [Yor]
  1793.  
  1794. //----------------------------------------------------------------------
  1795.  
  1796. void disconnect_player(int account_id)
  1797.  
  1798. {
  1799.  
  1800. int i;
  1801.  
  1802. struct char_session_data* sd;
  1803.  
  1804. // disconnect player if online on char-server
  1805.  
  1806. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->account_id == account_id );
  1807.  
  1808. if( i < fd_max )
  1809.  
  1810. set_eof(i);
  1811.  
  1812. }
  1813.  
  1814. static void char_auth_ok(int fd, struct char_session_data *sd)
  1815. {
  1816. struct online_char_data* character;
  1817.  
  1818. if( (character = (struct online_char_data*)idb_get(online_char_db, sd->account_id)) != NULL )
  1819. { // check if character is not online already. [Skotlex]
  1820. if (character->server > -1)
  1821. { //Character already online. KICK KICK KICK
  1822. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  1823. if (character->waiting_disconnect == -1)
  1824. character->waiting_disconnect = add_timer(gettick()+20000, chardb_waiting_disconnect, character->account_id, 0);
  1825. WFIFOW(fd,0) = 0x81;
  1826. WFIFOB(fd,2) = 8;
  1827. WFIFOSET(fd,3);
  1828. return;
  1829. }
  1830. if (character->fd >= 0 && character->fd != fd)
  1831. { //There's already a connection from this account that hasn't picked a char yet.
  1832. WFIFOW(fd,0) = 0x81;
  1833. WFIFOB(fd,2) = 8;
  1834. WFIFOSET(fd,3);
  1835. return;
  1836. }
  1837. character->fd = fd;
  1838. }
  1839.  
  1840. if (login_fd > 0) {
  1841. // request account data
  1842. WFIFOHEAD(login_fd,6);
  1843. WFIFOW(login_fd,0) = 0x2716;
  1844. WFIFOL(login_fd,2) = sd->account_id;
  1845. WFIFOSET(login_fd,6);
  1846. }
  1847.  
  1848. // mark session as 'authed'
  1849. sd->auth = true;
  1850.  
  1851. // set char online on charserver
  1852. set_char_charselect(sd->account_id);
  1853.  
  1854. // continues when account data is received...
  1855. }
  1856.  
  1857. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data);
  1858. void mapif_server_reset(int id);
  1859.  
  1860. /// Resets all the data.
  1861.  
  1862. void loginif_reset(void)
  1863.  
  1864. {
  1865.  
  1866. int id;
  1867.  
  1868. // TODO kick everyone out and reset everything or wait for connect and try to reaquire locks [FlavioJS]
  1869.  
  1870. for( id = 0; id < ARRAYLENGTH(server); ++id )
  1871.  
  1872. mapif_server_reset(id);
  1873.  
  1874. flush_fifos();
  1875.  
  1876. exit(EXIT_FAILURE);
  1877.  
  1878. }
  1879.  
  1880. /// Checks the conditions for the server to stop.
  1881.  
  1882. /// Releases the cookie when all characters are saved.
  1883.  
  1884. /// If all the conditions are met, it stops the core loop.
  1885.  
  1886. void loginif_check_shutdown(void)
  1887.  
  1888. {
  1889.  
  1890. if( runflag != CHARSERVER_ST_SHUTDOWN )
  1891.  
  1892. return;
  1893.  
  1894. runflag = CORE_ST_STOP;
  1895.  
  1896. }
  1897.  
  1898. /// Called when the connection to Login Server is disconnected.
  1899.  
  1900. void loginif_on_disconnect(void)
  1901.  
  1902. {
  1903.  
  1904. ShowWarning("Connection to Login Server lost.\n\n");
  1905.  
  1906. }
  1907.  
  1908. /// Called when all the connection steps are completed.
  1909.  
  1910. void loginif_on_ready(void)
  1911.  
  1912. {
  1913.  
  1914. int i;
  1915.  
  1916. loginif_check_shutdown();
  1917.  
  1918. //Send online accounts to login server.
  1919.  
  1920. send_accounts_tologin(INVALID_TIMER, gettick(), 0, 0);
  1921.  
  1922. // if no map-server already connected, display a message...
  1923.  
  1924. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd > 0 && server[i].map[0] );
  1925.  
  1926. if( i == ARRAYLENGTH(server) )
  1927.  
  1928. ShowStatus("Awaiting maps from map-server.\n");
  1929.  
  1930. }
  1931.  
  1932.  
  1933.  
  1934. int parse_fromlogin(int fd)
  1935. {
  1936. struct char_session_data* sd = NULL;
  1937. int i;
  1938. // only process data from the login-server
  1939. if( fd != login_fd )
  1940. {
  1941.  
  1942. ShowDebug("parse_fromlogin: Disconnecting invalid session #%d (is not the login-server)\n", fd);
  1943.  
  1944. do_close(fd);
  1945.  
  1946. return 0;
  1947.  
  1948. }
  1949.  
  1950. if( session[fd]->flag.eof )
  1951.  
  1952. {
  1953. do_close(fd);
  1954. login_fd = -1;
  1955.  
  1956. loginif_on_disconnect();
  1957. return 0;
  1958. }
  1959.  
  1960. sd = (struct char_session_data*)session[fd]->session_data;
  1961.  
  1962. while(RFIFOREST(fd) >= 2)
  1963. {
  1964. uint16 command = RFIFOW(fd,0);
  1965.  
  1966. switch( command )
  1967. {
  1968.  
  1969. // acknowledgement of connect-to-loginserver request
  1970. case 0x2711:
  1971. if (RFIFOREST(fd) < 3)
  1972. return 0;
  1973.  
  1974. if (RFIFOB(fd,2)) {
  1975. //printf("connect login server error : %d\n", RFIFOB(fd,2));
  1976. ShowError("Can not connect to login-server.\n");
  1977. ShowError("The server communication passwords (default s1/p1) are probably invalid.\n");
  1978. ShowError("Also, please make sure your login db has the correct communication username/passwords and the gender of the account is S.\n");
  1979. ShowError("The communication passwords are set in map_athena.conf and char_athena.conf\n");
  1980. set_eof(fd);
  1981.  
  1982. return 0;
  1983. } else {
  1984. ShowStatus("Connected to login-server (connection #%d).\n", fd);
  1985.  
  1986. loginif_on_ready();
  1987. }
  1988. RFIFOSKIP(fd,3);
  1989. break;
  1990.  
  1991. // acknowledgement of account authentication request
  1992. case 0x2713:
  1993. if (RFIFOREST(fd) < 25)
  1994. return 0;
  1995. {
  1996. int account_id = RFIFOL(fd,2);
  1997. uint32 login_id1 = RFIFOL(fd,6);
  1998. uint32 login_id2 = RFIFOL(fd,10);
  1999. uint8 sex = RFIFOB(fd,14);
  2000. uint8 result = RFIFOB(fd,15);
  2001. int request_id = RFIFOL(fd,16);
  2002. uint32 version = RFIFOL(fd,20);
  2003. uint8 clienttype = RFIFOB(fd,24);
  2004. RFIFOSKIP(fd,25);
  2005.  
  2006. if( session_isActive(request_id) && (sd=(struct char_session_data*)session[request_id]->session_data) &&
  2007. !sd->auth && sd->account_id == account_id && sd->login_id1 == login_id1 && sd->login_id2 == login_id2 && sd->sex == sex )
  2008. {
  2009. int client_fd = request_id;
  2010. sd->version = version;
  2011. sd->clienttype = clienttype;
  2012. switch( result )
  2013. {
  2014. case 0:// ok
  2015. char_auth_ok(client_fd, sd);
  2016. break;
  2017. case 1:// auth failed
  2018. WFIFOHEAD(client_fd,3);
  2019. WFIFOW(client_fd,0) = 0x6c;
  2020. WFIFOB(client_fd,2) = 0;// rejected from server
  2021. WFIFOSET(client_fd,3);
  2022. break;
  2023. }
  2024. }
  2025. }
  2026. break;
  2027.  
  2028. case 0x2717: // account data
  2029. if (RFIFOREST(fd) < 62)
  2030. return 0;
  2031.  
  2032. // find the authenticated session with this account id
  2033. ARR_FIND( 0, fd_max, i, session[i] && (sd = (struct char_session_data*)session[i]->session_data) && sd->auth && sd->account_id == RFIFOL(fd,2) );
  2034. if( i < fd_max )
  2035. {
  2036. memcpy(sd->email, RFIFOP(fd,6), 40);
  2037. sd->expiration_time = (time_t)RFIFOL(fd,46);
  2038. sd->gmlevel = RFIFOB(fd,50);
  2039. safestrncpy(sd->birthdate, RFIFOP(fd,51), sizeof(sd->birthdate));
  2040. // continued from char_auth_ok...
  2041. if( max_connect_user && count_users() >= max_connect_user && sd->gmlevel < gm_allow_level )
  2042. {
  2043. // refuse connection (over populated)
  2044. WFIFOW(i,0) = 0x6c;
  2045. WFIFOW(i,2) = 0;
  2046. WFIFOSET(i,3);
  2047. }
  2048. else
  2049. {
  2050. // send characters to player
  2051. mmo_char_send006b(i, sd);
  2052. WFIFOHEAD(i, 12);
  2053. WFIFOW(i, 0) = 0x08B9;
  2054. WFIFOW(i, 2) = 0;
  2055. WFIFOW(i, 4) = 0;
  2056. WFIFOL(i, 6) = sd->account_id;
  2057. WFIFOW(i, 10) = 0;
  2058. WFIFOSET(i, 12);
  2059. }
  2060. }
  2061. RFIFOSKIP(fd,62);
  2062. break;
  2063.  
  2064. // login-server alive packet
  2065. case 0x2718:
  2066. if (RFIFOREST(fd) < 2)
  2067. return 0;
  2068. RFIFOSKIP(fd,2);
  2069. break;
  2070.  
  2071. // changesex reply
  2072. case 0x2723:
  2073. if (RFIFOREST(fd) < 7)
  2074. return 0;
  2075. {
  2076. unsigned char buf[7];
  2077.  
  2078. int acc = RFIFOL(fd,2);
  2079. int sex = RFIFOB(fd,6);
  2080. RFIFOSKIP(fd,7);
  2081.  
  2082. if( acc > 0 )
  2083. {// TODO: Is this even possible?
  2084. int char_id[MAX_CHARS];
  2085. int class_[MAX_CHARS];
  2086. int guild_id[MAX_CHARS];
  2087. int num;
  2088.  
  2089. char* data;
  2090.  
  2091. struct auth_node* node = (struct auth_node*)idb_get(auth_db, acc);
  2092. if( node != NULL )
  2093. node->sex = sex;
  2094.  
  2095. // get characters
  2096. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`class`,`guild_id` FROM `%s` WHERE `account_id` = '%d'", char_db, acc) )
  2097. Sql_ShowDebug(sql_handle);
  2098. for( i = 0; i < MAX_CHARS && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2099. {
  2100. Sql_GetData(sql_handle, 0, &data, NULL); char_id[i] = atoi(data);
  2101. Sql_GetData(sql_handle, 1, &data, NULL); class_[i] = atoi(data);
  2102. Sql_GetData(sql_handle, 2, &data, NULL); guild_id[i] = atoi(data);
  2103. }
  2104. num = i;
  2105. for( i = 0; i < num; ++i )
  2106. {
  2107. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER ||
  2108. class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY ||
  2109. class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER ||
  2110. class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER ||
  2111. class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T ||
  2112. class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER )
  2113. {
  2114. // job modification
  2115. if( class_[i] == JOB_BARD || class_[i] == JOB_DANCER )
  2116. class_[i] = (sex ? JOB_BARD : JOB_DANCER);
  2117. else if( class_[i] == JOB_CLOWN || class_[i] == JOB_GYPSY )
  2118. class_[i] = (sex ? JOB_CLOWN : JOB_GYPSY);
  2119. else if( class_[i] == JOB_BABY_BARD || class_[i] == JOB_BABY_DANCER )
  2120. class_[i] = (sex ? JOB_BABY_BARD : JOB_BABY_DANCER);
  2121. else if( class_[i] == JOB_MINSTREL || class_[i] == JOB_WANDERER )
  2122. class_[i] = (sex ? JOB_MINSTREL : JOB_WANDERER);
  2123. else if( class_[i] == JOB_MINSTREL_T || class_[i] == JOB_WANDERER_T )
  2124. class_[i] = (sex ? JOB_MINSTREL_T : JOB_WANDERER_T);
  2125. else if( class_[i] == JOB_BABY_MINSTREL || class_[i] == JOB_BABY_WANDERER )
  2126. class_[i] = (sex ? JOB_BABY_MINSTREL : JOB_BABY_WANDERER);
  2127. // remove specifical skills of classes 19,20 4020,4021 and 4042,4043
  2128. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `skill_point` = `skill_point` +"
  2129. " (SELECT SUM(lv) FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330' AND `lv` > '0')"
  2130. " WHERE `char_id` = '%d'",
  2131. char_db, skill_db, char_id[i], char_id[i]) )
  2132. Sql_ShowDebug(sql_handle);
  2133. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `char_id` = '%d' AND `id` >= '315' AND `id` <= '330'", skill_db, char_id[i]) )
  2134. Sql_ShowDebug(sql_handle);
  2135. }
  2136. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  2137. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `equip` = '0' WHERE `char_id` = '%d'", inventory_db, char_id[i]) )
  2138. Sql_ShowDebug(sql_handle);
  2139. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `class`='%d', `weapon`='0', `shield`='0', `head_top`='0', `head_mid`='0', `head_bottom`='0' WHERE `char_id`='%d'", char_db, class_[i], char_id[i]) )
  2140. Sql_ShowDebug(sql_handle);
  2141.  
  2142. if( guild_id[i] )// If there is a guild, update the guild_member data [Skotlex]
  2143. inter_guild_sex_changed(guild_id[i], acc, char_id[i], sex);
  2144. }
  2145. Sql_FreeResult(sql_handle);
  2146. // disconnect player if online on char-server
  2147.  
  2148. disconnect_player(acc);
  2149.  
  2150. }
  2151.  
  2152. // notify all mapservers about this change
  2153. WBUFW(buf,0) = 0x2b0d;
  2154. WBUFL(buf,2) = acc;
  2155. WBUFB(buf,6) = sex;
  2156. mapif_sendall(buf, 7);
  2157. }
  2158. break;
  2159.  
  2160. // reply to an account_reg2 registry request
  2161. case 0x2729:
  2162. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2163. return 0;
  2164.  
  2165. { //Receive account_reg2 registry, forward to map servers.
  2166. unsigned char buf[13+ACCOUNT_REG2_NUM*sizeof(struct global_reg)];
  2167. memcpy(buf,RFIFOP(fd,0), RFIFOW(fd,2));
  2168. WBUFW(buf,0) = 0x3804; //Map server can now receive all kinds of reg values with the same packet. [Skotlex]
  2169. mapif_sendall(buf, WBUFW(buf,2));
  2170.  
  2171. RFIFOSKIP(fd, RFIFOW(fd,2));
  2172. }
  2173.  
  2174.  
  2175. break;
  2176.  
  2177. // State change of account/ban notification (from login-server)
  2178. case 0x2731:
  2179. if (RFIFOREST(fd) < 11)
  2180. return 0;
  2181.  
  2182. { // send to all map-servers to disconnect the player
  2183. unsigned char buf[11];
  2184. WBUFW(buf,0) = 0x2b14;
  2185. WBUFL(buf,2) = RFIFOL(fd,2);
  2186. WBUFB(buf,6) = RFIFOB(fd,6); // 0: change of statut, 1: ban
  2187. WBUFL(buf,7) = RFIFOL(fd,7); // status or final date of a banishment
  2188. mapif_sendall(buf, 11);
  2189. }
  2190. // disconnect player if online on char-server
  2191. disconnect_player(RFIFOL(fd,2));
  2192.  
  2193. RFIFOSKIP(fd,11);
  2194. break;
  2195.  
  2196. // Login server request to kick a character out. [Skotlex]
  2197. case 0x2734:
  2198. if (RFIFOREST(fd) < 6)
  2199. return 0;
  2200. {
  2201. int aid = RFIFOL(fd,2);
  2202. struct online_char_data* character = (struct online_char_data*)idb_get(online_char_db, aid);
  2203. RFIFOSKIP(fd,6);
  2204. if( character != NULL )
  2205. {// account is already marked as online!
  2206. if( character->server > -1 )
  2207. { //Kick it from the map server it is on.
  2208. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2209. if (character->waiting_disconnect == -1)
  2210. character->waiting_disconnect = add_timer(gettick()+AUTH_TIMEOUT, chardb_waiting_disconnect, character->account_id, 0);
  2211. }
  2212. else
  2213. {// Manual kick from char server.
  2214. struct char_session_data *tsd;
  2215. int i;
  2216. ARR_FIND( 0, fd_max, i, session[i] && (tsd = (struct char_session_data*)session[i]->session_data) && tsd->account_id == aid );
  2217. if( i < fd_max )
  2218. {
  2219. WFIFOHEAD(i,3);
  2220. WFIFOW(i,0) = 0x81;
  2221. WFIFOB(i,2) = 2; // "Someone has already logged in with this id"
  2222. WFIFOSET(i,3);
  2223. set_eof(i);
  2224. }
  2225. else // still moving to the map-server
  2226. set_char_offline(-1, aid);
  2227. }
  2228. }
  2229. idb_remove(auth_db, aid);// reject auth attempts from map-server
  2230. }
  2231. break;
  2232.  
  2233. // ip address update signal from login server
  2234. case 0x2735:
  2235. {
  2236. unsigned char buf[2];
  2237. uint32 new_ip = 0;
  2238.  
  2239. WBUFW(buf,0) = 0x2b1e;
  2240. mapif_sendall(buf, 2);
  2241.  
  2242. new_ip = host2ip(login_ip_str);
  2243. if (new_ip && new_ip != login_ip)
  2244.  
  2245. login_ip = new_ip; //Update login ip, too.
  2246.  
  2247. new_ip = host2ip(char_ip_str);
  2248. if (new_ip && new_ip != char_ip)
  2249. { //Update ip.
  2250. char_ip = new_ip;
  2251. ShowInfo("Updating IP for [%s].\n", char_ip_str);
  2252. // notify login server about the change
  2253. WFIFOHEAD(fd,6);
  2254. WFIFOW(fd,0) = 0x2736;
  2255. WFIFOL(fd,2) = htonl(char_ip);
  2256. WFIFOSET(fd,6);
  2257. }
  2258.  
  2259.  
  2260. RFIFOSKIP(fd,2);
  2261. }
  2262. break;
  2263.  
  2264. default:
  2265. ShowError("Unknown packet 0x%04x received from login-server, disconnecting.\n", command);
  2266. set_eof(fd);
  2267. return 0;
  2268. }
  2269. }
  2270.  
  2271. RFIFOFLUSH(fd);
  2272. return 0;
  2273. }
  2274.  
  2275. int check_connect_login_server(int tid, unsigned int tick, int id, intptr data);
  2276.  
  2277. int ping_login_server(int tid, unsigned int tick, int id, intptr data);
  2278.  
  2279. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data);
  2280.  
  2281. void do_init_loginif(void)
  2282.  
  2283. {
  2284.  
  2285. // establish char-login connection if not present
  2286.  
  2287. add_timer_func_list(check_connect_login_server, "check_connect_login_server");
  2288.  
  2289. add_timer_interval(gettick() + 1000, check_connect_login_server, 0, 0, 10 * 1000);
  2290.  
  2291. // keep the char-login connection alive
  2292.  
  2293. add_timer_func_list(ping_login_server, "ping_login_server");
  2294.  
  2295. add_timer_interval(gettick() + 1000, ping_login_server, 0, 0, ((int)stall_time-2) * 1000);
  2296.  
  2297. // send a list of all online account IDs to login server
  2298.  
  2299. add_timer_func_list(send_accounts_tologin, "send_accounts_tologin");
  2300.  
  2301. add_timer_interval(gettick() + 1000, send_accounts_tologin, 0, 0, 3600 * 1000); //Sync online accounts every hour
  2302.  
  2303. }
  2304.  
  2305. void do_final_loginif(void)
  2306.  
  2307. {
  2308.  
  2309. if( login_fd != -1 )
  2310.  
  2311. {
  2312.  
  2313. do_close(login_fd);
  2314.  
  2315. login_fd = -1;
  2316.  
  2317. }
  2318.  
  2319. }
  2320.  
  2321. int request_accreg2(int account_id, int char_id)
  2322. {
  2323. if (login_fd > 0) {
  2324. WFIFOHEAD(login_fd,10);
  2325. WFIFOW(login_fd,0) = 0x272e;
  2326. WFIFOL(login_fd,2) = account_id;
  2327. WFIFOL(login_fd,6) = char_id;
  2328. WFIFOSET(login_fd,10);
  2329. return 1;
  2330. }
  2331. return 0;
  2332. }
  2333.  
  2334. //Send packet forward to login-server for account saving
  2335. int save_accreg2(unsigned char* buf, int len)
  2336. {
  2337. if (login_fd > 0) {
  2338. WFIFOHEAD(login_fd,len+4);
  2339. memcpy(WFIFOP(login_fd,4), buf, len);
  2340. WFIFOW(login_fd,0) = 0x2728;
  2341. WFIFOW(login_fd,2) = len+4;
  2342. WFIFOSET(login_fd,len+4);
  2343. return 1;
  2344. }
  2345. return 0;
  2346. }
  2347.  
  2348. void char_read_fame_list(void)
  2349. {
  2350. int i;
  2351. char* data;
  2352. size_t len;
  2353.  
  2354. // Empty ranking lists
  2355. memset(smith_fame_list, 0, sizeof(smith_fame_list));
  2356. memset(chemist_fame_list, 0, sizeof(chemist_fame_list));
  2357. memset(taekwon_fame_list, 0, sizeof(taekwon_fame_list));
  2358. // Build Blacksmith ranking list
  2359. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_BLACKSMITH, JOB_WHITESMITH, JOB_BABY_BLACKSMITH, JOB_MECHANIC, JOB_MECHANIC_T, JOB_BABY_MECHANIC, fame_list_size_smith) )
  2360. Sql_ShowDebug(sql_handle);
  2361. for( i = 0; i < fame_list_size_smith && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2362. {
  2363. // char_id
  2364. Sql_GetData(sql_handle, 0, &data, NULL);
  2365. smith_fame_list[i].id = atoi(data);
  2366. // fame
  2367. Sql_GetData(sql_handle, 1, &data, &len);
  2368. smith_fame_list[i].fame = atoi(data);
  2369. // name
  2370. Sql_GetData(sql_handle, 2, &data, &len);
  2371. memcpy(smith_fame_list[i].name, data, min(len, NAME_LENGTH));
  2372. }
  2373. // Build Alchemist ranking list
  2374. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d' OR `class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_ALCHEMIST, JOB_CREATOR, JOB_BABY_ALCHEMIST, JOB_GENETIC, JOB_GENETIC_T, JOB_BABY_GENETIC, fame_list_size_chemist) )
  2375. Sql_ShowDebug(sql_handle);
  2376. for( i = 0; i < fame_list_size_chemist && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2377. {
  2378. // char_id
  2379. Sql_GetData(sql_handle, 0, &data, NULL);
  2380. chemist_fame_list[i].id = atoi(data);
  2381. // fame
  2382. Sql_GetData(sql_handle, 1, &data, &len);
  2383. chemist_fame_list[i].fame = atoi(data);
  2384. // name
  2385. Sql_GetData(sql_handle, 2, &data, &len);
  2386. memcpy(chemist_fame_list[i].name, data, min(len, NAME_LENGTH));
  2387. }
  2388. // Build Taekwon ranking list
  2389. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`,`fame`,`name` FROM `%s` WHERE `fame`>0 AND (`class`='%d') ORDER BY `fame` DESC LIMIT 0,%d", char_db, JOB_TAEKWON, fame_list_size_taekwon) )
  2390. Sql_ShowDebug(sql_handle);
  2391. for( i = 0; i < fame_list_size_taekwon && SQL_SUCCESS == Sql_NextRow(sql_handle); ++i )
  2392. {
  2393. // char_id
  2394. Sql_GetData(sql_handle, 0, &data, NULL);
  2395. taekwon_fame_list[i].id = atoi(data);
  2396. // fame
  2397. Sql_GetData(sql_handle, 1, &data, &len);
  2398. taekwon_fame_list[i].fame = atoi(data);
  2399. // name
  2400. Sql_GetData(sql_handle, 2, &data, &len);
  2401. memcpy(taekwon_fame_list[i].name, data, min(len, NAME_LENGTH));
  2402. }
  2403. Sql_FreeResult(sql_handle);
  2404. }
  2405.  
  2406. // Send map-servers the fame ranking lists
  2407. int char_send_fame_list(int fd)
  2408. {
  2409. int i, len = 8;
  2410. unsigned char buf[32000];
  2411.  
  2412. WBUFW(buf,0) = 0x2b1b;
  2413.  
  2414. for(i = 0; i < fame_list_size_smith && smith_fame_list[i].id; i++) {
  2415. memcpy(WBUFP(buf, len), &smith_fame_list[i], sizeof(struct fame_list));
  2416. len += sizeof(struct fame_list);
  2417. }
  2418. // add blacksmith's block length
  2419. WBUFW(buf, 6) = len;
  2420.  
  2421. for(i = 0; i < fame_list_size_chemist && chemist_fame_list[i].id; i++) {
  2422. memcpy(WBUFP(buf, len), &chemist_fame_list[i], sizeof(struct fame_list));
  2423. len += sizeof(struct fame_list);
  2424. }
  2425. // add alchemist's block length
  2426. WBUFW(buf, 4) = len;
  2427.  
  2428. for(i = 0; i < fame_list_size_taekwon && taekwon_fame_list[i].id; i++) {
  2429. memcpy(WBUFP(buf, len), &taekwon_fame_list[i], sizeof(struct fame_list));
  2430. len += sizeof(struct fame_list);
  2431. }
  2432. // add total packet length
  2433. WBUFW(buf, 2) = len;
  2434.  
  2435. if (fd != -1)
  2436. mapif_send(fd, buf, len);
  2437. else
  2438. mapif_sendall(buf, len);
  2439. return 0;
  2440. }
  2441.  
  2442. void char_update_fame_list(int type, int index, int fame)
  2443. {
  2444. unsigned char buf[8];
  2445. WBUFW(buf,0) = 0x2b22;
  2446. WBUFB(buf,2) = type;
  2447. WBUFB(buf,3) = index;
  2448. WBUFL(buf,4) = fame;
  2449. mapif_sendall(buf, 8);
  2450. }
  2451.  
  2452. //Loads a character's name and stores it in the buffer given (must be NAME_LENGTH in size)
  2453. //Returns 1 on found, 0 on not found (buffer is filled with Unknown char name)
  2454. int char_loadName(int char_id, char* name)
  2455. {
  2456. char* data;
  2457. size_t len;
  2458.  
  2459. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `name` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) )
  2460. Sql_ShowDebug(sql_handle);
  2461. else if( SQL_SUCCESS == Sql_NextRow(sql_handle) )
  2462. {
  2463. Sql_GetData(sql_handle, 0, &data, &len);
  2464. safestrncpy(name, data, NAME_LENGTH);
  2465. return 1;
  2466. }
  2467. else
  2468. {
  2469. safestrncpy(name, unknown_char_name, NAME_LENGTH);
  2470. }
  2471. return 0;
  2472. }
  2473.  
  2474. int search_mapserver(unsigned short map, uint32 ip, uint16 port);
  2475.  
  2476. /// Initializes a server structure.
  2477.  
  2478. void mapif_server_init(int id)
  2479.  
  2480. {
  2481.  
  2482. memset(&server[id], 0, sizeof(server[id]));
  2483.  
  2484. server[id].fd = -1;
  2485.  
  2486. }
  2487.  
  2488. /// Destroys a server structure.
  2489.  
  2490. void mapif_server_destroy(int id)
  2491.  
  2492. {
  2493.  
  2494. if( server[id].fd == -1 )
  2495.  
  2496. {
  2497.  
  2498. do_close(server[id].fd);
  2499.  
  2500. server[id].fd = -1;
  2501.  
  2502. }
  2503.  
  2504. }
  2505.  
  2506. /// Resets all the data related to a server.
  2507.  
  2508. void mapif_server_reset(int id)
  2509.  
  2510. {
  2511.  
  2512. int i,j;
  2513.  
  2514. unsigned char buf[16384];
  2515.  
  2516. int fd = server[id].fd;
  2517.  
  2518. //Notify other map servers that this one is gone. [Skotlex]
  2519.  
  2520. WBUFW(buf,0) = 0x2b20;
  2521.  
  2522. WBUFL(buf,4) = htonl(server[id].ip);
  2523.  
  2524. WBUFW(buf,8) = htons(server[id].port);
  2525.  
  2526. j = 0;
  2527.  
  2528. for(i = 0; i < MAX_MAP_PER_SERVER; i++)
  2529.  
  2530. if (server[id].map[i])
  2531.  
  2532. WBUFW(buf,10+(j++)*4) = server[id].map[i];
  2533.  
  2534. if (j > 0) {
  2535.  
  2536. WBUFW(buf,2) = j * 4 + 10;
  2537.  
  2538. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2539.  
  2540. }
  2541.  
  2542. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo` WHERE `index`='%d'", server[id].fd) )
  2543.  
  2544. Sql_ShowDebug(sql_handle);
  2545.  
  2546. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Tag relevant chars as 'in disconnected' server.
  2547.  
  2548. mapif_server_destroy(id);
  2549.  
  2550. mapif_server_init(id);
  2551.  
  2552. }
  2553.  
  2554. /// Called when the connection to a Map Server is disconnected.
  2555.  
  2556. void mapif_on_disconnect(int id)
  2557.  
  2558. {
  2559.  
  2560. ShowStatus("Map-server #%d has disconnected.\n", id);
  2561.  
  2562. mapif_server_reset(id);
  2563.  
  2564. }
  2565.  
  2566.  
  2567.  
  2568. int parse_frommap(int fd)
  2569. {
  2570. int i, j;
  2571. int id;
  2572.  
  2573.  
  2574.  
  2575. ARR_FIND( 0, ARRAYLENGTH(server), id, server[id].fd == fd );
  2576.  
  2577. if( id == ARRAYLENGTH(server) )
  2578.  
  2579. {// not a map server
  2580.  
  2581. ShowDebug("parse_frommap: Disconnecting invalid session #%d (is not a map-server)\n", fd);
  2582. do_close(fd);
  2583. return 0;
  2584. }
  2585. if( session[fd]->flag.eof )
  2586.  
  2587. {
  2588.  
  2589. do_close(fd);
  2590.  
  2591. server[id].fd = -1;
  2592.  
  2593. mapif_on_disconnect(id);
  2594.  
  2595. return 0;
  2596.  
  2597. }
  2598.  
  2599. while(RFIFOREST(fd) >= 2)
  2600. {
  2601. switch(RFIFOW(fd,0))
  2602. {
  2603.  
  2604. case 0x2afa: // Receiving map names list from the map-server
  2605. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2606. return 0;
  2607.  
  2608. memset(server[id].map, 0, sizeof(server[id].map));
  2609. j = 0;
  2610. for(i = 4; i < RFIFOW(fd,2); i += 4) {
  2611. server[id].map[j] = RFIFOW(fd,i);
  2612. j++;
  2613. }
  2614.  
  2615. ShowStatus("Map-Server %d connected: %d maps, from IP %d.%d.%d.%d port %d.\n",
  2616. id, j, CONVIP(server[id].ip), server[id].port);
  2617. ShowStatus("Map-server %d loading complete.\n", id);
  2618.  
  2619. // send name for wisp to player
  2620. WFIFOHEAD(fd, 3 + NAME_LENGTH);
  2621. WFIFOW(fd,0) = 0x2afb;
  2622. WFIFOB(fd,2) = 0;
  2623. memcpy(WFIFOP(fd,3), wisp_server_name, NAME_LENGTH);
  2624. WFIFOSET(fd,3+NAME_LENGTH);
  2625.  
  2626. char_send_fame_list(fd); //Send fame list.
  2627.  
  2628. {
  2629. unsigned char buf[16384];
  2630. int x;
  2631. if (j == 0) {
  2632. ShowWarning("Map-server %d has NO maps.\n", id);
  2633. } else {
  2634. // Transmitting maps information to the other map-servers
  2635. WBUFW(buf,0) = 0x2b04;
  2636. WBUFW(buf,2) = j * 4 + 10;
  2637. WBUFL(buf,4) = htonl(server[id].ip);
  2638. WBUFW(buf,8) = htons(server[id].port);
  2639. memcpy(WBUFP(buf,10), RFIFOP(fd,4), j * 4);
  2640. mapif_sendallwos(fd, buf, WBUFW(buf,2));
  2641. }
  2642. // Transmitting the maps of the other map-servers to the new map-server
  2643. for(x = 0; x < ARRAYLENGTH(server); x++) {
  2644. if (server[x].fd > 0 && x != id) {
  2645. WFIFOHEAD(fd,10 +4*ARRAYLENGTH(server));
  2646. WFIFOW(fd,0) = 0x2b04;
  2647. WFIFOL(fd,4) = htonl(server[x].ip);
  2648. WFIFOW(fd,8) = htons(server[x].port);
  2649. j = 0;
  2650. for(i = 0; i < ARRAYLENGTH(server); i++)
  2651. if (server[x].map[i])
  2652. WFIFOW(fd,10+(j++)*4) = server[x].map[i];
  2653. if (j > 0) {
  2654. WFIFOW(fd,2) = j * 4 + 10;
  2655. WFIFOSET(fd,WFIFOW(fd,2));
  2656. }
  2657. }
  2658. }
  2659. }
  2660. RFIFOSKIP(fd,RFIFOW(fd,2));
  2661. break;
  2662.  
  2663. case 0x2afc: //Packet command is now used for sc_data request. [Skotlex]
  2664. if (RFIFOREST(fd) < 10)
  2665. return 0;
  2666. {
  2667. #ifdef ENABLE_SC_SAVING
  2668. int aid, cid;
  2669. aid = RFIFOL(fd,2);
  2670. cid = RFIFOL(fd,6);
  2671. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT type, tick, val1, val2, val3, val4 from `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2672. scdata_db, aid, cid) )
  2673. {
  2674. Sql_ShowDebug(sql_handle);
  2675. break;
  2676. }
  2677. if( Sql_NumRows(sql_handle) > 0 )
  2678. {
  2679. struct status_change_data scdata;
  2680. int count;
  2681. char* data;
  2682.  
  2683. WFIFOHEAD(fd,14+50*sizeof(struct status_change_data));
  2684. WFIFOW(fd,0) = 0x2b1d;
  2685. WFIFOL(fd,4) = aid;
  2686. WFIFOL(fd,8) = cid;
  2687. for( count = 0; count < 50 && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  2688. {
  2689. Sql_GetData(sql_handle, 0, &data, NULL); scdata.type = atoi(data);
  2690. Sql_GetData(sql_handle, 1, &data, NULL); scdata.tick = atoi(data);
  2691. Sql_GetData(sql_handle, 2, &data, NULL); scdata.val1 = atoi(data);
  2692. Sql_GetData(sql_handle, 3, &data, NULL); scdata.val2 = atoi(data);
  2693. Sql_GetData(sql_handle, 4, &data, NULL); scdata.val3 = atoi(data);
  2694. Sql_GetData(sql_handle, 5, &data, NULL); scdata.val4 = atoi(data);
  2695. memcpy(WFIFOP(fd, 14+count*sizeof(struct status_change_data)), &scdata, sizeof(struct status_change_data));
  2696. }
  2697. if (count >= 50)
  2698. ShowWarning("Too many status changes for %d:%d, some of them were not loaded.\n", aid, cid);
  2699. if (count > 0)
  2700. {
  2701. WFIFOW(fd,2) = 14 + count*sizeof(struct status_change_data);
  2702. WFIFOW(fd,12) = count;
  2703. WFIFOSET(fd,WFIFOW(fd,2));
  2704.  
  2705. //Clear the data once loaded.
  2706. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", scdata_db, aid, cid) )
  2707. Sql_ShowDebug(sql_handle);
  2708. }
  2709. }
  2710. Sql_FreeResult(sql_handle);
  2711. #endif
  2712. RFIFOSKIP(fd, 10);
  2713. }
  2714. break;
  2715.  
  2716. case 0x2b0a: //Request skillcooldown data
  2717. if (RFIFOREST(fd) < 10)
  2718. return 0;
  2719. {
  2720. int aid, cid;
  2721. aid = RFIFOL(fd,2);
  2722. cid = RFIFOL(fd,6);
  2723. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT skill, tick FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'",
  2724. skillcooldown_db, aid, cid) )
  2725. {
  2726. Sql_ShowDebug(sql_handle);
  2727. break;
  2728. }
  2729. if( Sql_NumRows(sql_handle) > 0 )
  2730. {
  2731. int count;
  2732. char* data;
  2733. struct skill_cooldown_data scd;
  2734.  
  2735. WFIFOHEAD(fd,14 + MAX_SKILLCOOLDOWN * sizeof(struct skill_cooldown_data));
  2736. WFIFOW(fd,0) = 0x2b0b;
  2737. WFIFOL(fd,4) = aid;
  2738. WFIFOL(fd,8) = cid;
  2739. for( count = 0; count < MAX_SKILLCOOLDOWN && SQL_SUCCESS == Sql_NextRow(sql_handle); ++count )
  2740. {
  2741. Sql_GetData(sql_handle, 0, &data, NULL); scd.skill_id = atoi(data);
  2742. Sql_GetData(sql_handle, 1, &data, NULL); scd.tick = atoi(data);
  2743. memcpy(WFIFOP(fd,14+count*sizeof(struct skill_cooldown_data)), &scd, sizeof(struct skill_cooldown_data));
  2744. }
  2745. if( count >= MAX_SKILLCOOLDOWN )
  2746. ShowWarning("Too many skillcooldowns for %d:%d, some of them were not loaded.\n", aid, cid);
  2747. if( count > 0 )
  2748. {
  2749. WFIFOW(fd,2) = 14 + count * sizeof(struct skill_cooldown_data);
  2750. WFIFOW(fd,12) = count;
  2751. WFIFOSET(fd,WFIFOW(fd,2));
  2752. //Clear the data once loaded.
  2753. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '%d' AND `char_id`='%d'", skillcooldown_db, aid, cid) )
  2754. Sql_ShowDebug(sql_handle);
  2755. }
  2756. }
  2757. Sql_FreeResult(sql_handle);
  2758. RFIFOSKIP(fd, 10);
  2759. }
  2760. break;
  2761.  
  2762. case 0x2afe: //set MAP user count
  2763. if (RFIFOREST(fd) < 4)
  2764. return 0;
  2765. if (RFIFOW(fd,2) != server[id].users) {
  2766. server[id].users = RFIFOW(fd,2);
  2767. ShowInfo("User Count: %d (Server: %d)\n", server[id].users, id);
  2768. }
  2769. RFIFOSKIP(fd, 4);
  2770. break;
  2771.  
  2772. case 0x2aff: //set MAP users
  2773. if (RFIFOREST(fd) < 6 || RFIFOREST(fd) < RFIFOW(fd,2))
  2774. return 0;
  2775. {
  2776. //TODO: When data mismatches memory, update guild/party online/offline states.
  2777. int aid, cid;
  2778. struct online_char_data* character;
  2779.  
  2780.  
  2781. server[id].users = RFIFOW(fd,4);
  2782. online_char_db->foreach(online_char_db,char_db_setoffline,id); //Set all chars from this server as 'unknown'
  2783. for(i = 0; i < server[id].users; i++) {
  2784. aid = RFIFOL(fd,6+i*8);
  2785. cid = RFIFOL(fd,6+i*8+4);
  2786. character = (struct online_char_data*)idb_ensure(online_char_db, aid, create_online_char_data);
  2787. if( character->server > -1 && character->server != id )
  2788. {
  2789. ShowNotice("Set map user: Character (%d:%d) marked on map server %d, but map server %d claims to have (%d:%d) online!\n",
  2790. character->account_id, character->char_id, character->server, id, aid, cid);
  2791. mapif_disconnectplayer(server[character->server].fd, character->account_id, character->char_id, 2);
  2792. }
  2793. character->server = id;
  2794. character->char_id = cid;
  2795. }
  2796. //If any chars remain in -2, they will be cleaned in the cleanup timer.
  2797. RFIFOSKIP(fd,RFIFOW(fd,2));
  2798. }
  2799. break;
  2800.  
  2801. case 0x2b01: // Receive character data from map-server for saving
  2802. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  2803. return 0;
  2804. {
  2805. int aid = RFIFOL(fd,4), cid = RFIFOL(fd,8), size = RFIFOW(fd,2);
  2806. struct online_char_data* character;
  2807. if (size - 13 != sizeof(struct mmo_charstatus))
  2808. {
  2809. ShowError("parse_from_map (save-char): Size mismatch! %d != %d\n", size-13, sizeof(struct mmo_charstatus));
  2810. RFIFOSKIP(fd,size);
  2811. break;
  2812. }
  2813. //Check account only if this ain't final save. Final-save goes through because of the char-map reconnect
  2814. if (RFIFOB(fd,12) || (
  2815. (character = (struct online_char_data*)idb_get(online_char_db, aid)) != NULL &&
  2816. character->char_id == cid))
  2817. {
  2818. struct mmo_charstatus char_dat;
  2819. memcpy(&char_dat, RFIFOP(fd,13), sizeof(struct mmo_charstatus));
  2820. mmo_char_tosql(cid, &char_dat);
  2821. } else { //This may be valid on char-server reconnection, when re-sending characters that already logged off.
  2822. ShowError("parse_from_map (save-char): Received data for non-existant/offline character (%d:%d).\n", aid, cid);
  2823. set_char_online(id, cid, aid);
  2824. }
  2825.  
  2826. if (RFIFOB(fd,12))
  2827. { //Flag, set character offline after saving. [Skotlex]
  2828. set_char_offline(cid, aid);
  2829. WFIFOHEAD(fd,10);
  2830. WFIFOW(fd,0) = 0x2b21; //Save ack only needed on final save.
  2831. WFIFOL(fd,2) = aid;
  2832. WFIFOL(fd,6) = cid;
  2833. WFIFOSET(fd,10);
  2834. }
  2835. RFIFOSKIP(fd,size);
  2836. }
  2837. break;
  2838.  
  2839. case 0x2b02: // req char selection
  2840. if( RFIFOREST(fd) < 18 )
  2841. return 0;
  2842. {
  2843. struct auth_node* node;
  2844.  
  2845. int account_id = RFIFOL(fd,2);
  2846. uint32 login_id1 = RFIFOL(fd,6);
  2847. uint32 login_id2 = RFIFOL(fd,10);
  2848. uint32 ip = RFIFOL(fd,14);
  2849. RFIFOSKIP(fd,18);
  2850.  
  2851. if( runflag != CHARSERVER_ST_RUNNING )
  2852.  
  2853. {
  2854.  
  2855. WFIFOHEAD(fd,7);
  2856.  
  2857. WFIFOW(fd,0) = 0x2b03;
  2858.  
  2859. WFIFOL(fd,2) = account_id;
  2860.  
  2861. WFIFOB(fd,6) = 0;// not ok
  2862.  
  2863. WFIFOSET(fd,7);
  2864.  
  2865. }
  2866.  
  2867. else
  2868.  
  2869. {
  2870.  
  2871. // create temporary auth entry
  2872.  
  2873. CREATE(node, struct auth_node, 1);
  2874.  
  2875. node->account_id = account_id;
  2876.  
  2877. node->char_id = 0;
  2878.  
  2879. node->login_id1 = login_id1;
  2880.  
  2881. node->login_id2 = login_id2;
  2882.  
  2883. //node->sex = 0;
  2884.  
  2885. node->ip = ntohl(ip);
  2886.  
  2887. //node->expiration_time = 0; // unlimited/unknown time by default (not display in map-server)
  2888.  
  2889. //node->gmlevel = 0;
  2890.  
  2891. idb_put(auth_db, account_id, node);
  2892.  
  2893. //Set char to "@ char select" in online db [Kevin]
  2894.  
  2895. set_char_charselect(account_id);
  2896.  
  2897. WFIFOHEAD(fd,7);
  2898.  
  2899. WFIFOW(fd,0) = 0x2b03;
  2900.  
  2901. WFIFOL(fd,2) = account_id;
  2902.  
  2903. WFIFOB(fd,6) = 1;// ok
  2904.  
  2905. WFIFOSET(fd,7);
  2906.  
  2907. }
  2908. }
  2909. break;
  2910.  
  2911. case 0x2b05: // request "change map server"
  2912. if (RFIFOREST(fd) < 35)
  2913. return 0;
  2914. {
  2915. int map_id, map_fd = -1;
  2916. struct online_char_data* data;
  2917. struct mmo_charstatus* char_data;
  2918. struct mmo_charstatus char_dat;
  2919.  
  2920. map_id = search_mapserver(RFIFOW(fd,18), ntohl(RFIFOL(fd,24)), ntohs(RFIFOW(fd,28))); //Locate mapserver by ip and port.
  2921. if (map_id >= 0)
  2922. map_fd = server[map_id].fd;
  2923. //Char should just had been saved before this packet, so this should be safe. [Skotlex]
  2924. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2925. if (char_data == NULL)
  2926. { //Really shouldn't happen.
  2927. mmo_char_fromsql(RFIFOL(fd,14), &char_dat, true);
  2928. char_data = (struct mmo_charstatus*)uidb_get(char_db_,RFIFOL(fd,14));
  2929. }
  2930.  
  2931. if( runflag == CHARSERVER_ST_RUNNING &&
  2932.  
  2933. session_isActive(map_fd) &&
  2934.  
  2935. char_data )
  2936. { //Send the map server the auth of this player.
  2937. struct auth_node* node;
  2938.  
  2939. //Update the "last map" as this is where the player must be spawned on the new map server.
  2940. char_data->last_point.map = RFIFOW(fd,18);
  2941. char_data->last_point.x = RFIFOW(fd,20);
  2942. char_data->last_point.y = RFIFOW(fd,22);
  2943. char_data->sex = RFIFOB(fd,30);
  2944.  
  2945. // create temporary auth entry
  2946. CREATE(node, struct auth_node, 1);
  2947. node->account_id = RFIFOL(fd,2);
  2948. node->char_id = RFIFOL(fd,14);
  2949. node->login_id1 = RFIFOL(fd,6);
  2950. node->login_id2 = RFIFOL(fd,10);
  2951. node->sex = RFIFOB(fd,30);
  2952. node->expiration_time = 0; // FIXME
  2953. node->ip = ntohl(RFIFOL(fd,31));
  2954. idb_put(auth_db, RFIFOL(fd,2), node);
  2955.  
  2956. data = (struct online_char_data*)idb_ensure(online_char_db, RFIFOL(fd,2), create_online_char_data);
  2957. data->char_id = char_data->char_id;
  2958. data->server = map_id; //Update server where char is.
  2959.  
  2960. //Reply with an ack.
  2961. WFIFOHEAD(fd,30);
  2962. WFIFOW(fd,0) = 0x2b06;
  2963. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2964. WFIFOSET(fd,30);
  2965. } else { //Reply with nak
  2966. WFIFOHEAD(fd,30);
  2967. WFIFOW(fd,0) = 0x2b06;
  2968. memcpy(WFIFOP(fd,2), RFIFOP(fd,2), 28);
  2969. WFIFOL(fd,6) = 0; //Set login1 to 0.
  2970. WFIFOSET(fd,30);
  2971. }
  2972. RFIFOSKIP(fd,35);
  2973. }
  2974. break;
  2975.  
  2976. case 0x2b08: // char name request
  2977. if (RFIFOREST(fd) < 6)
  2978. return 0;
  2979.  
  2980. WFIFOHEAD(fd,30);
  2981. WFIFOW(fd,0) = 0x2b09;
  2982. WFIFOL(fd,2) = RFIFOL(fd,2);
  2983. char_loadName((int)RFIFOL(fd,2), (char*)WFIFOP(fd,6));
  2984. WFIFOSET(fd,30);
  2985.  
  2986. RFIFOSKIP(fd,6);
  2987. break;
  2988.  
  2989. case 0x2b0c: // Map server send information to change an email of an account -> login-server
  2990. if (RFIFOREST(fd) < 86)
  2991. return 0;
  2992. if (login_fd > 0) { // don't send request if no login-server
  2993. WFIFOHEAD(login_fd,86);
  2994. memcpy(WFIFOP(login_fd,0), RFIFOP(fd,0),86); // 0x2722 <account_id>.L <actual_e-mail>.40B <new_e-mail>.40B
  2995. WFIFOW(login_fd,0) = 0x2722;
  2996. WFIFOSET(login_fd,86);
  2997. }
  2998. RFIFOSKIP(fd, 86);
  2999. break;
  3000.  
  3001. case 0x2b0e: // Request from map-server to change an account's status (will just be forwarded to login server)
  3002. if (RFIFOREST(fd) < 44)
  3003. return 0;
  3004. {
  3005. int result = 0; // 0-login-server request done, 1-player not found, 2-gm level too low, 3-login-server offline
  3006. char esc_name[NAME_LENGTH*2+1];
  3007.  
  3008. int acc = RFIFOL(fd,2); // account_id of who ask (-1 if server itself made this request)
  3009. const char* name = (char*)RFIFOP(fd,6); // name of the target character
  3010. int type = RFIFOW(fd,30); // type of operation: 1-block, 2-ban, 3-unblock, 4-unban
  3011. short year = RFIFOW(fd,32);
  3012. short month = RFIFOW(fd,34);
  3013. short day = RFIFOW(fd,36);
  3014. short hour = RFIFOW(fd,38);
  3015. short minute = RFIFOW(fd,40);
  3016. short second = RFIFOW(fd,42);
  3017. RFIFOSKIP(fd,44);
  3018.  
  3019. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  3020. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`,`name` FROM `%s` WHERE `name` = '%s'", char_db, esc_name) )
  3021. Sql_ShowDebug(sql_handle);
  3022. else
  3023. if( Sql_NumRows(sql_handle) == 0 )
  3024. {
  3025. result = 1; // 1-player not found
  3026. }
  3027. else
  3028. if( SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3029. Sql_ShowDebug(sql_handle);
  3030. //FIXME: set proper result value?
  3031. else
  3032. {
  3033. char name[NAME_LENGTH];
  3034. int account_id;
  3035. char* data;
  3036.  
  3037. Sql_GetData(sql_handle, 0, &data, NULL); account_id = atoi(data);
  3038. Sql_GetData(sql_handle, 1, &data, NULL); safestrncpy(name, data, sizeof(name));
  3039.  
  3040. if( login_fd <= 0 )
  3041. result = 3; // 3-login-server offline
  3042. //FIXME: need to move this check to login server [ultramage]
  3043. // else
  3044. // if( acc != -1 && isGM(acc) < isGM(account_id) )
  3045. // result = 2; // 2-gm level too low
  3046. else
  3047. switch( type ) {
  3048. case 1: // block
  3049. WFIFOHEAD(login_fd,10);
  3050. WFIFOW(login_fd,0) = 0x2724;
  3051. WFIFOL(login_fd,2) = account_id;
  3052. WFIFOL(login_fd,6) = 5; // new account status
  3053. WFIFOSET(login_fd,10);
  3054. break;
  3055. case 2: // ban
  3056. WFIFOHEAD(login_fd,18);
  3057. WFIFOW(login_fd, 0) = 0x2725;
  3058. WFIFOL(login_fd, 2) = account_id;
  3059. WFIFOW(login_fd, 6) = year;
  3060. WFIFOW(login_fd, 8) = month;
  3061. WFIFOW(login_fd,10) = day;
  3062. WFIFOW(login_fd,12) = hour;
  3063. WFIFOW(login_fd,14) = minute;
  3064. WFIFOW(login_fd,16) = second;
  3065. WFIFOSET(login_fd,18);
  3066. break;
  3067. case 3: // unblock
  3068. WFIFOHEAD(login_fd,10);
  3069. WFIFOW(login_fd,0) = 0x2724;
  3070. WFIFOL(login_fd,2) = account_id;
  3071. WFIFOL(login_fd,6) = 0; // new account status
  3072. WFIFOSET(login_fd,10);
  3073. break;
  3074. case 4: // unban
  3075. WFIFOHEAD(login_fd,6);
  3076. WFIFOW(login_fd,0) = 0x272a;
  3077. WFIFOL(login_fd,2) = account_id;
  3078. WFIFOSET(login_fd,6);
  3079. break;
  3080. case 5: // changesex
  3081. WFIFOHEAD(login_fd,6);
  3082. WFIFOW(login_fd,0) = 0x2727;
  3083. WFIFOL(login_fd,2) = account_id;
  3084. WFIFOSET(login_fd,6);
  3085. break;
  3086. }
  3087. }
  3088.  
  3089. Sql_FreeResult(sql_handle);
  3090.  
  3091. // send answer if a player ask, not if the server ask
  3092. if( acc != -1 && type != 5) { // Don't send answer for changesex
  3093. WFIFOHEAD(fd,34);
  3094. WFIFOW(fd, 0) = 0x2b0f;
  3095. WFIFOL(fd, 2) = acc;
  3096. safestrncpy((char*)WFIFOP(fd,6), name, NAME_LENGTH);
  3097. WFIFOW(fd,30) = type;
  3098. WFIFOW(fd,32) = result;
  3099. WFIFOSET(fd,34);
  3100. }
  3101. }
  3102. break;
  3103.  
  3104. case 0x2b10: // Update and send fame ranking list
  3105. if (RFIFOREST(fd) < 11)
  3106. return 0;
  3107. {
  3108. int cid = RFIFOL(fd, 2);
  3109. int fame = RFIFOL(fd, 6);
  3110. char type = RFIFOB(fd, 10);
  3111. int size;
  3112. struct fame_list* list;
  3113. int player_pos;
  3114. int fame_pos;
  3115.  
  3116. switch(type)
  3117. {
  3118. case 1: size = fame_list_size_smith; list = smith_fame_list; break;
  3119. case 2: size = fame_list_size_chemist; list = chemist_fame_list; break;
  3120. case 3: size = fame_list_size_taekwon; list = taekwon_fame_list; break;
  3121. default: size = 0; list = NULL; break;
  3122. }
  3123.  
  3124. ARR_FIND(0, size, player_pos, list[player_pos].id == cid);// position of the player
  3125. ARR_FIND(0, size, fame_pos, list[fame_pos].fame <= fame);// where the player should be
  3126.  
  3127. if( player_pos == size && fame_pos == size )
  3128. ;// not on list and not enough fame to get on it
  3129. else if( fame_pos == player_pos )
  3130. {// same position
  3131. list[player_pos].fame = fame;
  3132. char_update_fame_list(type, player_pos, fame);
  3133. }
  3134. else
  3135. {// move in the list
  3136. if( player_pos == size )
  3137. {// new ranker - not in the list
  3138. ARR_MOVE(size - 1, fame_pos, list, struct fame_list);
  3139. list[fame_pos].id = cid;
  3140. list[fame_pos].fame = fame;
  3141. char_loadName(cid, list[fame_pos].name);
  3142. }
  3143. else
  3144. {// already in the list
  3145. if( fame_pos == size )
  3146. --fame_pos;// move to the end of the list
  3147. ARR_MOVE(player_pos, fame_pos, list, struct fame_list);
  3148. list[fame_pos].fame = fame;
  3149. }
  3150. char_send_fame_list(-1);
  3151. }
  3152.  
  3153. RFIFOSKIP(fd,11);
  3154. }
  3155. break;
  3156.  
  3157. // Divorce chars
  3158. case 0x2b11:
  3159. if( RFIFOREST(fd) < 10 )
  3160. return 0;
  3161.  
  3162. divorce_char_sql(RFIFOL(fd,2), RFIFOL(fd,6));
  3163. RFIFOSKIP(fd,10);
  3164. break;
  3165.  
  3166. case 0x2b16: // Receive rates [Wizputer]
  3167. if( RFIFOREST(fd) < 14 )
  3168. return 0;
  3169. {
  3170.  
  3171. char esc_server_name[sizeof(server_name)*2+1];
  3172.  
  3173.  
  3174. Sql_EscapeString(sql_handle, esc_server_name, server_name);
  3175.  
  3176. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `ragsrvinfo` SET `index`='%d',`name`='%s',`exp`='%d',`jexp`='%d',`drop`='%d'",
  3177.  
  3178. fd, esc_server_name, RFIFOL(fd,2), RFIFOL(fd,6), RFIFOL(fd,10)) )
  3179. Sql_ShowDebug(sql_handle);
  3180. RFIFOSKIP(fd,14);
  3181. }
  3182. break;
  3183.  
  3184. case 0x2b17: // Character disconnected set online 0 [Wizputer]
  3185. if (RFIFOREST(fd) < 6)
  3186. return 0;
  3187. set_char_offline(RFIFOL(fd,2),RFIFOL(fd,6));
  3188. RFIFOSKIP(fd,10);
  3189. break;
  3190.  
  3191. case 0x2b18: // Reset all chars to offline [Wizputer]
  3192. set_all_offline(id);
  3193. RFIFOSKIP(fd,2);
  3194. break;
  3195.  
  3196. case 0x2b19: // Character set online [Wizputer]
  3197. if (RFIFOREST(fd) < 10)
  3198. return 0;
  3199. set_char_online(id, RFIFOL(fd,2),RFIFOL(fd,6));
  3200. RFIFOSKIP(fd,10);
  3201. break;
  3202.  
  3203. case 0x2b1a: // Build and send fame ranking lists [DracoRPG]
  3204. if (RFIFOREST(fd) < 2)
  3205. return 0;
  3206. char_read_fame_list();
  3207. char_send_fame_list(-1);
  3208. RFIFOSKIP(fd,2);
  3209. break;
  3210.  
  3211. case 0x2b1c: //Request to save status change data. [Skotlex]
  3212. if (RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2))
  3213. return 0;
  3214. {
  3215. #ifdef ENABLE_SC_SAVING
  3216. int count, aid, cid;
  3217.  
  3218. aid = RFIFOL(fd, 4);
  3219. cid = RFIFOL(fd, 8);
  3220. count = RFIFOW(fd, 12);
  3221.  
  3222. if( count > 0 )
  3223. {
  3224. struct status_change_data data;
  3225. StringBuf buf;
  3226. int i;
  3227.  
  3228. StringBuf_Init(&buf);
  3229. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `type`, `tick`, `val1`, `val2`, `val3`, `val4`) VALUES ", scdata_db);
  3230. for( i = 0; i < count; ++i )
  3231. {
  3232. memcpy (&data, RFIFOP(fd, 14+i*sizeof(struct status_change_data)), sizeof(struct status_change_data));
  3233. if( i > 0 )
  3234. StringBuf_AppendStr(&buf, ", ");
  3235. StringBuf_Printf(&buf, "('%d','%d','%hu','%d','%d','%d','%d','%d')", aid, cid,
  3236. data.type, data.tick, data.val1, data.val2, data.val3, data.val4);
  3237. }
  3238. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  3239. Sql_ShowDebug(sql_handle);
  3240. StringBuf_Destroy(&buf);
  3241. }
  3242. #endif
  3243. RFIFOSKIP(fd, RFIFOW(fd, 2));
  3244. }
  3245. break;
  3246.  
  3247. case 0x2b15: //Request to save skill cooldown data
  3248. if( RFIFOREST(fd) < 4 || RFIFOREST(fd) < RFIFOW(fd,2) )
  3249. return 0;
  3250. {
  3251. int count, aid, cid;
  3252. aid = RFIFOL(fd,4);
  3253. cid = RFIFOL(fd,8);
  3254. count = RFIFOW(fd,12);
  3255. if( count > 0 )
  3256. {
  3257. struct skill_cooldown_data data;
  3258. StringBuf buf;
  3259. int i;
  3260.  
  3261. StringBuf_Init(&buf);
  3262. StringBuf_Printf(&buf, "INSERT INTO `%s` (`account_id`, `char_id`, `skill`, `tick`) VALUES ", skillcooldown_db);
  3263. for( i = 0; i < count; ++i )
  3264. {
  3265. memcpy(&data,RFIFOP(fd,14+i*sizeof(struct skill_cooldown_data)),sizeof(struct skill_cooldown_data));
  3266. if( i > 0 )
  3267. StringBuf_AppendStr(&buf, ", ");
  3268. StringBuf_Printf(&buf, "('%d','%d','%d','%d')", aid, cid, data.skill_id, data.tick);
  3269. }
  3270. if( SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) )
  3271. Sql_ShowDebug(sql_handle);
  3272. StringBuf_Destroy(&buf);
  3273. }
  3274. RFIFOSKIP(fd,RFIFOW(fd,2));
  3275. }
  3276. break;
  3277.  
  3278. case 0x2b23: // map-server alive packet
  3279. WFIFOHEAD(fd,2);
  3280. WFIFOW(fd,0) = 0x2b24;
  3281. WFIFOSET(fd,2);
  3282. RFIFOSKIP(fd,2);
  3283. break;
  3284.  
  3285. case 0x2b26: // auth request from map-server
  3286. if (RFIFOREST(fd) < 19)
  3287. return 0;
  3288.  
  3289. {
  3290. int account_id;
  3291. int char_id;
  3292. int login_id1;
  3293. char sex;
  3294. uint32 ip;
  3295. struct auth_node* node;
  3296. struct mmo_charstatus* cd;
  3297. struct mmo_charstatus char_dat;
  3298.  
  3299. account_id = RFIFOL(fd,2);
  3300. char_id = RFIFOL(fd,6);
  3301. login_id1 = RFIFOL(fd,10);
  3302. sex = RFIFOB(fd,14);
  3303. ip = ntohl(RFIFOL(fd,15));
  3304. RFIFOSKIP(fd,19);
  3305.  
  3306. node = (struct auth_node*)idb_get(auth_db, account_id);
  3307. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  3308. if( cd == NULL )
  3309. { //Really shouldn't happen.
  3310. mmo_char_fromsql(char_id, &char_dat, true);
  3311. cd = (struct mmo_charstatus*)uidb_get(char_db_,char_id);
  3312. }
  3313. if( runflag == CHARSERVER_ST_RUNNING &&
  3314.  
  3315. cd != NULL &&
  3316.  
  3317. node != NULL &&
  3318. node->account_id == account_id &&
  3319. node->char_id == char_id &&
  3320. node->login_id1 == login_id1 &&
  3321. node->sex == sex /*&&
  3322. node->ip == ip*/ )
  3323. {// auth ok
  3324. cd->sex = sex;
  3325.  
  3326. WFIFOHEAD(fd,24 + sizeof(struct mmo_charstatus));
  3327. WFIFOW(fd,0) = 0x2afd;
  3328. WFIFOW(fd,2) = 24 + sizeof(struct mmo_charstatus);
  3329. WFIFOL(fd,4) = account_id;
  3330. WFIFOL(fd,8) = node->login_id1;
  3331. WFIFOL(fd,12) = node->login_id2;
  3332. WFIFOL(fd,16) = (uint32)node->expiration_time; // FIXME: will wrap to negative after "19-Jan-2038, 03:14:07 AM GMT"
  3333. WFIFOL(fd,20) = node->gmlevel;
  3334. memcpy(WFIFOP(fd,24), cd, sizeof(struct mmo_charstatus));
  3335. WFIFOSET(fd, WFIFOW(fd,2));
  3336.  
  3337. // only use the auth once and mark user online
  3338. idb_remove(auth_db, account_id);
  3339. set_char_online(id, char_id, account_id);
  3340. }
  3341. else
  3342. {// auth failed
  3343. WFIFOHEAD(fd,19);
  3344. WFIFOW(fd,0) = 0x2b27;
  3345. WFIFOL(fd,2) = account_id;
  3346. WFIFOL(fd,6) = char_id;
  3347. WFIFOL(fd,10) = login_id1;
  3348. WFIFOB(fd,14) = sex;
  3349. WFIFOL(fd,15) = htonl(ip);
  3350. WFIFOSET(fd,19);
  3351. }
  3352. }
  3353. break;
  3354.  
  3355. case 0x2736: // ip address update
  3356. if (RFIFOREST(fd) < 6) return 0;
  3357. server[id].ip = ntohl(RFIFOL(fd, 2));
  3358. ShowInfo("Updated IP address of map-server #%d to %d.%d.%d.%d.\n", id, CONVIP(server[id].ip));
  3359. RFIFOSKIP(fd,6);
  3360. break;
  3361.  
  3362. default:
  3363. {
  3364. // inter server - packet
  3365. int r = inter_parse_frommap(fd);
  3366. if (r == 1) break; // processed
  3367. if (r == 2) return 0; // need more packet
  3368.  
  3369. // no inter server packet. no char server packet -> disconnect
  3370. ShowError("Unknown packet 0x%04x from map server, disconnecting.\n", RFIFOW(fd,0));
  3371. set_eof(fd);
  3372. return 0;
  3373. }
  3374. } // switch
  3375. } // while
  3376.  
  3377. return 0;
  3378. }
  3379.  
  3380. void do_init_mapif(void)
  3381.  
  3382. {
  3383.  
  3384. int i;
  3385.  
  3386. for( i = 0; i < ARRAYLENGTH(server); ++i )
  3387.  
  3388. mapif_server_init(i);
  3389.  
  3390. }
  3391.  
  3392. void do_final_mapif(void)
  3393.  
  3394. {
  3395.  
  3396. int i;
  3397.  
  3398. for( i = 0; i < ARRAYLENGTH(server); ++i )
  3399.  
  3400. mapif_server_destroy(i);
  3401.  
  3402. }
  3403.  
  3404. // Searches for the mapserver that has a given map (and optionally ip/port, if not -1).
  3405. // If found, returns the server's index in the 'server' array (otherwise returns -1).
  3406. int search_mapserver(unsigned short map, uint32 ip, uint16 port)
  3407. {
  3408. int i, j;
  3409.  
  3410. for(i = 0; i < ARRAYLENGTH(server); i++)
  3411. {
  3412. if (server[i].fd > 0
  3413. && (ip == (uint32)-1 || server[i].ip == ip)
  3414. && (port == (uint16)-1 || server[i].port == port))
  3415. {
  3416. for (j = 0; server[i].map[j]; j++)
  3417. if (server[i].map[j] == map)
  3418. return i;
  3419. }
  3420. }
  3421.  
  3422. return -1;
  3423. }
  3424.  
  3425. // char_mapif‚̏‰Šú‰»ˆ—iŒ»Ý‚Íinter_mapif‰Šú‰»‚̂݁j
  3426.  
  3427. static int char_mapif_init(int fd)
  3428. {
  3429. return inter_mapif_init(fd);
  3430. }
  3431.  
  3432. //--------------------------------------------
  3433. // Test to know if an IP come from LAN or WAN.
  3434. //--------------------------------------------
  3435. int lan_subnetcheck(uint32 ip)
  3436. {
  3437. int i;
  3438. ARR_FIND( 0, subnet_count, i, (subnet[i].char_ip & subnet[i].mask) == (ip & subnet[i].mask) );
  3439. if( i < subnet_count ) {
  3440. ShowInfo("Subnet check [%u.%u.%u.%u]: Matches "CL_CYAN"%u.%u.%u.%u/%u.%u.%u.%u"CL_RESET"\n", CONVIP(ip), CONVIP(subnet[i].char_ip & subnet[i].mask), CONVIP(subnet[i].mask));
  3441. return subnet[i].char_ip;
  3442. } else {
  3443. ShowInfo("Subnet check [%u.%u.%u.%u]: "CL_CYAN"WAN"CL_RESET"\n", CONVIP(ip));
  3444. return 0;
  3445. }
  3446. }
  3447.  
  3448. /// @param result
  3449.  
  3450. /// 0 (0x718): An unknown error has occurred.
  3451.  
  3452. /// 1: none/success
  3453.  
  3454. /// 3 (0x719): A database error occurred.
  3455.  
  3456. /// 4 (0x71a): To delete a character you must withdraw from the guild.
  3457.  
  3458. /// 5 (0x71b): To delete a character you must withdraw from the party.
  3459.  
  3460. /// Any (0x718): An unknown error has occurred.
  3461.  
  3462. void char_delete2_ack(int fd, int char_id, uint32 result, time_t delete_date)
  3463.  
  3464. {// HC: <0828>.W <char id>.L <Msg:0-5>.L <deleteDate>.L
  3465.  
  3466. WFIFOHEAD(fd,14);
  3467.  
  3468. WFIFOW(fd,0) = 0x828;
  3469.  
  3470. WFIFOL(fd,2) = char_id;
  3471.  
  3472. WFIFOL(fd,6) = result;
  3473.  
  3474. WFIFOL(fd,10) = TOL(delete_date);
  3475.  
  3476. WFIFOSET(fd,14);
  3477.  
  3478. }
  3479.  
  3480. /// @param result
  3481.  
  3482. /// 0 (0x718): An unknown error has occurred.
  3483.  
  3484. /// 1: none/success
  3485.  
  3486. /// 2 (0x71c): Due to system settings can not be deleted.
  3487.  
  3488. /// 3 (0x719): A database error occurred.
  3489.  
  3490. /// 4 (0x71d): Deleting not yet possible time.
  3491.  
  3492. /// 5 (0x71e): Date of birth do not match.
  3493.  
  3494. /// Any (0x718): An unknown error has occurred.
  3495.  
  3496. void char_delete2_accept_ack(int fd, int char_id, uint32 result)
  3497.  
  3498. {// HC: <082a>.W <char id>.L <Msg:0-5>.L
  3499.  
  3500. WFIFOHEAD(fd,10);
  3501.  
  3502. WFIFOW(fd,0) = 0x82a;
  3503.  
  3504. WFIFOL(fd,2) = char_id;
  3505.  
  3506. WFIFOL(fd,6) = result;
  3507.  
  3508. WFIFOSET(fd,10);
  3509.  
  3510. }
  3511.  
  3512. /// @param result
  3513.  
  3514. /// 1 (0x718): none/success, (if char id not in deletion process): An unknown error has occurred.
  3515.  
  3516. /// 2 (0x719): A database error occurred.
  3517.  
  3518. /// Any (0x718): An unknown error has occurred.
  3519.  
  3520. void char_delete2_cancel_ack(int fd, int char_id, uint32 result)
  3521.  
  3522. {// HC: <082c>.W <char id>.L <Msg:1-2>.L
  3523.  
  3524. WFIFOHEAD(fd,10);
  3525.  
  3526. WFIFOW(fd,0) = 0x82c;
  3527.  
  3528. WFIFOL(fd,2) = char_id;
  3529.  
  3530. WFIFOL(fd,6) = result;
  3531.  
  3532. WFIFOSET(fd,10);
  3533.  
  3534. }
  3535.  
  3536. static void char_delete2_req(int fd, struct char_session_data* sd)
  3537.  
  3538. {// CH: <0827>.W <char id>.L
  3539.  
  3540. int char_id, i, guild_id, party_id;
  3541.  
  3542. char* data;
  3543.  
  3544. time_t delete_date;
  3545.  
  3546. char_id = RFIFOL(fd,2);
  3547.  
  3548. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3549.  
  3550. if( i == MAX_CHARS )
  3551.  
  3552. {// character not found
  3553.  
  3554. char_delete2_ack(fd, char_id, 3, 0);
  3555.  
  3556. return;
  3557.  
  3558. }
  3559.  
  3560. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `guild_id`,`party_id`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3561.  
  3562. {
  3563.  
  3564. Sql_ShowDebug(sql_handle);
  3565.  
  3566. char_delete2_ack(fd, char_id, 3, 0);
  3567.  
  3568. return;
  3569.  
  3570. }
  3571.  
  3572. Sql_GetData(sql_handle, 0, &data, NULL); guild_id = atoi(data);
  3573.  
  3574. Sql_GetData(sql_handle, 1, &data, NULL); party_id = atoi(data);
  3575.  
  3576. Sql_GetData(sql_handle, 2, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3577.  
  3578. if( delete_date )
  3579.  
  3580. {// character already queued for deletion
  3581.  
  3582. char_delete2_ack(fd, char_id, 0, 0);
  3583.  
  3584. return;
  3585.  
  3586. }
  3587.  
  3588. /*
  3589.  
  3590. // Aegis imposes these checks probably to avoid dead member
  3591.  
  3592. // entries in guilds/parties, otherwise they are not required.
  3593.  
  3594. // TODO: Figure out how these are enforced during waiting.
  3595.  
  3596. if( guild_id )
  3597.  
  3598. {// character in guild
  3599.  
  3600. char_delete2_ack(fd, char_id, 4, 0);
  3601.  
  3602. return;
  3603.  
  3604. }
  3605.  
  3606. if( party_id )
  3607.  
  3608. {// character in party
  3609.  
  3610. char_delete2_ack(fd, char_id, 5, 0);
  3611.  
  3612. return;
  3613.  
  3614. }
  3615.  
  3616. */
  3617.  
  3618. // success
  3619.  
  3620. delete_date = time(NULL)+char_del_delay;
  3621.  
  3622. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='%lu' WHERE `char_id`='%d'", char_db, (unsigned long)delete_date, char_id) )
  3623.  
  3624. {
  3625.  
  3626. Sql_ShowDebug(sql_handle);
  3627.  
  3628. char_delete2_ack(fd, char_id, 3, 0);
  3629.  
  3630. return;
  3631.  
  3632. }
  3633.  
  3634. char_delete2_ack(fd, char_id, 1, delete_date);
  3635.  
  3636. }
  3637.  
  3638. static void char_delete2_accept(int fd, struct char_session_data* sd)
  3639.  
  3640. {// CH: <0829>.W <char id>.L <birth date:YYMMDD>.6B
  3641.  
  3642. char birthdate[8+1];
  3643.  
  3644. int char_id, i, k;
  3645.  
  3646. unsigned int base_level;
  3647.  
  3648. char* data;
  3649.  
  3650. time_t delete_date;
  3651.  
  3652. char_id = RFIFOL(fd,2);
  3653.  
  3654. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, char_id);
  3655.  
  3656. // construct "YY-MM-DD"
  3657.  
  3658. birthdate[0] = RFIFOB(fd,6);
  3659.  
  3660. birthdate[1] = RFIFOB(fd,7);
  3661.  
  3662. birthdate[2] = '-';
  3663.  
  3664. birthdate[3] = RFIFOB(fd,8);
  3665.  
  3666. birthdate[4] = RFIFOB(fd,9);
  3667.  
  3668. birthdate[5] = '-';
  3669.  
  3670. birthdate[6] = RFIFOB(fd,10);
  3671.  
  3672. birthdate[7] = RFIFOB(fd,11);
  3673.  
  3674. birthdate[8] = 0;
  3675.  
  3676. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3677.  
  3678. if( i == MAX_CHARS )
  3679.  
  3680. {// character not found
  3681.  
  3682. char_delete2_accept_ack(fd, char_id, 3);
  3683.  
  3684. return;
  3685.  
  3686. }
  3687.  
  3688. if( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `base_level`,`delete_date` FROM `%s` WHERE `char_id`='%d'", char_db, char_id) || SQL_SUCCESS != Sql_NextRow(sql_handle) )
  3689.  
  3690. {// data error
  3691.  
  3692. Sql_ShowDebug(sql_handle);
  3693.  
  3694. char_delete2_accept_ack(fd, char_id, 3);
  3695.  
  3696. return;
  3697.  
  3698. }
  3699.  
  3700. Sql_GetData(sql_handle, 0, &data, NULL); base_level = (unsigned int)strtoul(data, NULL, 10);
  3701.  
  3702. Sql_GetData(sql_handle, 1, &data, NULL); delete_date = strtoul(data, NULL, 10);
  3703.  
  3704. if( !delete_date || delete_date>time(NULL) )
  3705.  
  3706. {// not queued or delay not yet passed
  3707.  
  3708. char_delete2_accept_ack(fd, char_id, 4);
  3709.  
  3710. return;
  3711.  
  3712. }
  3713.  
  3714. if( strcmp(sd->birthdate+2, birthdate) ) // +2 to cut off the century
  3715.  
  3716. {// birth date is wrong
  3717.  
  3718. char_delete2_accept_ack(fd, char_id, 5);
  3719.  
  3720. return;
  3721.  
  3722. }
  3723.  
  3724. if( ( char_del_level > 0 && base_level >= (unsigned int)char_del_level ) || ( char_del_level < 0 && base_level <= (unsigned int)(-char_del_level) ) )
  3725.  
  3726. {// character level config restriction
  3727.  
  3728. char_delete2_accept_ack(fd, char_id, 2);
  3729.  
  3730. return;
  3731.  
  3732. }
  3733.  
  3734. // success
  3735.  
  3736. if( delete_char_sql(char_id) < 0 )
  3737.  
  3738. {
  3739.  
  3740. char_delete2_accept_ack(fd, char_id, 3);
  3741.  
  3742. return;
  3743.  
  3744. }
  3745.  
  3746. // refresh character list cache
  3747.  
  3748. for(k = i; k < MAX_CHARS-1; k++)
  3749.  
  3750. {
  3751.  
  3752. sd->found_char[k] = sd->found_char[k+1];
  3753.  
  3754. }
  3755.  
  3756. sd->found_char[MAX_CHARS-1] = -1;
  3757.  
  3758. char_delete2_accept_ack(fd, char_id, 1);
  3759.  
  3760. }
  3761.  
  3762. static void char_delete2_cancel(int fd, struct char_session_data* sd)
  3763.  
  3764. {// CH: <082b>.W <char id>.L
  3765.  
  3766. int char_id, i;
  3767.  
  3768. char_id = RFIFOL(fd,2);
  3769.  
  3770. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == char_id );
  3771.  
  3772. if( i == MAX_CHARS )
  3773.  
  3774. {// character not found
  3775.  
  3776. char_delete2_cancel_ack(fd, char_id, 2);
  3777.  
  3778. return;
  3779.  
  3780. }
  3781.  
  3782. // there is no need to check, whether or not the character was
  3783.  
  3784. // queued for deletion, as the client prints an error message by
  3785.  
  3786. // itself, if it was not the case (@see char_delete2_cancel_ack)
  3787.  
  3788. if( SQL_SUCCESS != Sql_Query(sql_handle, "UPDATE `%s` SET `delete_date`='0' WHERE `char_id`='%d'", char_db, char_id) )
  3789.  
  3790. {
  3791.  
  3792. Sql_ShowDebug(sql_handle);
  3793.  
  3794. char_delete2_cancel_ack(fd, char_id, 2);
  3795.  
  3796. return;
  3797.  
  3798. }
  3799.  
  3800. char_delete2_cancel_ack(fd, char_id, 1);
  3801.  
  3802. }
  3803.  
  3804.  
  3805.  
  3806. int parse_char(int fd)
  3807. {
  3808. int i, ch;
  3809. char email[40];
  3810. unsigned short cmd;
  3811. int map_fd;
  3812. struct char_session_data* sd;
  3813. uint32 ipl = session[fd]->client_addr;
  3814.  
  3815. sd = (struct char_session_data*)session[fd]->session_data;
  3816.  
  3817. // disconnect any player if no login-server.
  3818. if(login_fd < 0)
  3819. set_eof(fd);
  3820.  
  3821. if(session[fd]->flag.eof)
  3822. {
  3823. if( sd != NULL && sd->auth )
  3824. { // already authed client
  3825. struct online_char_data* data = (struct online_char_data*)idb_get(online_char_db, sd->account_id);
  3826. if( data != NULL && data->fd == fd)
  3827. data->fd = -1;
  3828. if( data == NULL || data->server == -1) //If it is not in any server, send it offline. [Skotlex]
  3829. set_char_offline(-1,sd->account_id);
  3830. }
  3831. do_close(fd);
  3832. return 0;
  3833. }
  3834.  
  3835. while( RFIFOREST(fd) >= 2 )
  3836. {
  3837. //For use in packets that depend on an sd being present [Skotlex]
  3838. #define FIFOSD_CHECK(rest) { if(RFIFOREST(fd) < rest) return 0; if (sd==NULL || !sd->auth) { RFIFOSKIP(fd,rest); return 0; } }
  3839.  
  3840. cmd = RFIFOW(fd,0);
  3841. switch( cmd )
  3842. {
  3843.  
  3844. // request to connect
  3845. // 0065 <account id>.L <login id1>.L <login id2>.L <???>.W <sex>.B
  3846. case 0x65:
  3847. if( RFIFOREST(fd) < 17 )
  3848. return 0;
  3849. {
  3850. struct auth_node* node;
  3851.  
  3852. int account_id = RFIFOL(fd,2);
  3853. uint32 login_id1 = RFIFOL(fd,6);
  3854. uint32 login_id2 = RFIFOL(fd,10);
  3855. int sex = RFIFOB(fd,16);
  3856. RFIFOSKIP(fd,17);
  3857.  
  3858. ShowInfo("request connect - account_id:%d/login_id1:%d/login_id2:%d\n", account_id, login_id1, login_id2);
  3859.  
  3860. if (sd) {
  3861. //Received again auth packet for already authentified account?? Discard it.
  3862. //TODO: Perhaps log this as a hack attempt?
  3863. //TODO: and perhaps send back a reply?
  3864. break;
  3865. }
  3866.  
  3867. CREATE(session[fd]->session_data, struct char_session_data, 1);
  3868. sd = (struct char_session_data*)session[fd]->session_data;
  3869. sd->account_id = account_id;
  3870. sd->login_id1 = login_id1;
  3871. sd->login_id2 = login_id2;
  3872. sd->sex = sex;
  3873. sd->auth = false; // not authed yet
  3874.  
  3875. // send back account_id
  3876. WFIFOHEAD(fd,4);
  3877. WFIFOL(fd,0) = account_id;
  3878. WFIFOSET(fd,4);
  3879. if( runflag != CHARSERVER_ST_RUNNING )
  3880.  
  3881. {
  3882.  
  3883. WFIFOHEAD(fd,3);
  3884.  
  3885. WFIFOW(fd,0) = 0x6c;
  3886.  
  3887. WFIFOB(fd,2) = 0;// rejected from server
  3888.  
  3889. WFIFOSET(fd,3);
  3890.  
  3891. break;
  3892.  
  3893. }
  3894.  
  3895. // search authentification
  3896. node = (struct auth_node*)idb_get(auth_db, account_id);
  3897. if( node != NULL &&
  3898. node->account_id == account_id &&
  3899. node->login_id1 == login_id1 &&
  3900. node->login_id2 == login_id2 /*&&
  3901. node->ip == ipl*/ )
  3902. {// authentication found (coming from map server)
  3903. idb_remove(auth_db, account_id);
  3904. char_auth_ok(fd, sd);
  3905. }
  3906. else
  3907. {// authentication not found (coming from login server)
  3908. if (login_fd > 0) { // don't send request if no login-server
  3909. WFIFOHEAD(login_fd,23);
  3910. WFIFOW(login_fd,0) = 0x2712; // ask login-server to authentify an account
  3911. WFIFOL(login_fd,2) = sd->account_id;
  3912. WFIFOL(login_fd,6) = sd->login_id1;
  3913. WFIFOL(login_fd,10) = sd->login_id2;
  3914. WFIFOB(login_fd,14) = sd->sex;
  3915. WFIFOL(login_fd,15) = htonl(ipl);
  3916. WFIFOL(login_fd,19) = fd;
  3917. WFIFOSET(login_fd,23);
  3918. } else { // if no login-server, we must refuse connection
  3919. WFIFOHEAD(fd,3);
  3920. WFIFOW(fd,0) = 0x6c;
  3921. WFIFOB(fd,2) = 0;
  3922. WFIFOSET(fd,3);
  3923. }
  3924. }
  3925. }
  3926. break;
  3927.  
  3928. // char select
  3929. case 0x66:
  3930. FIFOSD_CHECK(3);
  3931. {
  3932. struct mmo_charstatus char_dat;
  3933. struct mmo_charstatus *cd;
  3934. char* data;
  3935. int char_id;
  3936. uint32 subnet_map_ip;
  3937. struct auth_node* node;
  3938.  
  3939. int slot = RFIFOB(fd,2);
  3940. RFIFOSKIP(fd,3);
  3941.  
  3942. if ( SQL_SUCCESS != Sql_Query(sql_handle, "SELECT `char_id` FROM `%s` WHERE `account_id`='%d' AND `char_num`='%d'", char_db, sd->account_id, slot)
  3943. || SQL_SUCCESS != Sql_NextRow(sql_handle)
  3944. || SQL_SUCCESS != Sql_GetData(sql_handle, 0, &data, NULL) )
  3945. {
  3946. //Not found?? May be forged packet.
  3947. Sql_ShowDebug(sql_handle);
  3948. Sql_FreeResult(sql_handle);
  3949. WFIFOHEAD(fd,3);
  3950.  
  3951. WFIFOW(fd,0) = 0x6c;
  3952.  
  3953. WFIFOB(fd,2) = 0; // rejected from server
  3954.  
  3955. WFIFOSET(fd,3);
  3956. break;
  3957. }
  3958.  
  3959. char_id = atoi(data);
  3960. Sql_FreeResult(sql_handle);
  3961. mmo_char_fromsql(char_id, &char_dat, true);
  3962.  
  3963. //Have to switch over to the DB instance otherwise data won't propagate [Kevin]
  3964. cd = (struct mmo_charstatus *)idb_get(char_db_, char_id);
  3965.  
  3966. cd->sex = sd->sex;
  3967.  
  3968. if (log_char) {
  3969. char esc_name[NAME_LENGTH*2+1];
  3970.  
  3971. Sql_EscapeStringLen(sql_handle, esc_name, char_dat.name, strnlen(char_dat.name, NAME_LENGTH));
  3972. if( SQL_ERROR == Sql_Query(sql_handle, "INSERT INTO `%s`(`time`, `account_id`,`char_num`,`name`) VALUES (NOW(), '%d', '%d', '%s')",
  3973. charlog_db, sd->account_id, slot, esc_name) )
  3974. Sql_ShowDebug(sql_handle);
  3975. }
  3976. ShowInfo("Selected char: (Account %d: %d - %s)\n", sd->account_id, slot, char_dat.name);
  3977.  
  3978. // searching map server
  3979. i = search_mapserver(cd->last_point.map, -1, -1);
  3980.  
  3981. // if map is not found, we check major cities
  3982. if (i < 0) {
  3983. unsigned short j;
  3984. //First check that there's actually a map server online.
  3985. ARR_FIND( 0, ARRAYLENGTH(server), j, server[j].fd >= 0 && server[j].map[0] );
  3986.  
  3987. if (j == ARRAYLENGTH(server)) {
  3988. ShowInfo("Connection Closed. No map servers available.\n");
  3989. WFIFOHEAD(fd,3);
  3990. WFIFOW(fd,0) = 0x81;
  3991. WFIFOB(fd,2) = 1; // 01 = Server closed
  3992. WFIFOSET(fd,3);
  3993. break;
  3994. }
  3995. if ((i = search_mapserver((j=mapindex_name2id(MAP_PRONTERA)),-1,-1)) >= 0) {
  3996. cd->last_point.x = 273;
  3997.  
  3998. cd->last_point.y = 354;
  3999. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_GEFFEN)),-1,-1)) >= 0) {
  4000. cd->last_point.x = 120;
  4001.  
  4002. cd->last_point.y = 100;
  4003. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_MORROC)),-1,-1)) >= 0) {
  4004. cd->last_point.x = 160;
  4005.  
  4006. cd->last_point.y = 94;
  4007. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_ALBERTA)),-1,-1)) >= 0) {
  4008. cd->last_point.x = 116;
  4009.  
  4010. cd->last_point.y = 57;
  4011. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_PAYON)),-1,-1)) >= 0) {
  4012. cd->last_point.y = 117;
  4013. } else if ((i = search_mapserver((j=mapindex_name2id(MAP_IZLUDE)),-1,-1)) >= 0) {
  4014. cd->last_point.x = 94;
  4015.  
  4016. cd->last_point.y = 103;
  4017. } else {
  4018. ShowInfo("Connection Closed. No map server available that has a major city, and unable to find map-server for '%s'.\n", mapindex_id2name(cd->last_point.map));
  4019. WFIFOHEAD(fd,3);
  4020. WFIFOW(fd,0) = 0x81;
  4021. WFIFOB(fd,2) = 1; // 01 = Server closed
  4022. WFIFOSET(fd,3);
  4023. break;
  4024. }
  4025. ShowWarning("Unable to find map-server for '%s', sending to major city '%s'.\n", mapindex_id2name(cd->last_point.map), mapindex_id2name(j));
  4026.  
  4027. cd->last_point.map = j;
  4028. }
  4029.  
  4030. //Send NEW auth packet [Kevin]
  4031. //FIXME: is this case even possible? [ultramage]
  4032. if ((map_fd = server[i].fd) < 1 || session[map_fd] == NULL)
  4033. {
  4034. ShowError("parse_char: Attempting to write to invalid session %d! Map Server #%d disconnected.\n", map_fd, i);
  4035. server[i].fd = -1;
  4036. memset(&server[i], 0, sizeof(struct mmo_map_server));
  4037. //Send server closed.
  4038. WFIFOHEAD(fd,3);
  4039. WFIFOW(fd,0) = 0x81;
  4040. WFIFOB(fd,2) = 1; // 01 = Server closed
  4041. WFIFOSET(fd,3);
  4042. break;
  4043. }
  4044.  
  4045. //Send player to map
  4046. WFIFOHEAD(fd,28);
  4047. WFIFOW(fd,0) = 0x71;
  4048. WFIFOL(fd,2) = cd->char_id;
  4049.  
  4050. mapindex_getmapname_ext(mapindex_id2name(cd->last_point.map), (char*)WFIFOP(fd,6));
  4051.  
  4052. subnet_map_ip = lan_subnetcheck(ipl); // Advanced subnet check [LuzZza]
  4053. WFIFOL(fd,22) = htonl((subnet_map_ip) ? subnet_map_ip : server[i].ip);
  4054. WFIFOW(fd,26) = ntows(htons(server[i].port)); // [!] LE byte order here [!]
  4055. WFIFOSET(fd,28);
  4056.  
  4057. // create temporary auth entry
  4058. CREATE(node, struct auth_node, 1);
  4059. node->account_id = sd->account_id;
  4060. node->char_id = cd->char_id;
  4061. node->login_id1 = sd->login_id1;
  4062. node->login_id2 = sd->login_id2;
  4063. node->sex = sd->sex;
  4064. node->expiration_time = sd->expiration_time;
  4065. node->gmlevel = sd->gmlevel;
  4066. node->ip = ipl;
  4067. idb_put(auth_db, sd->account_id, node);
  4068.  
  4069. set_char_online(-2,node->char_id,sd->account_id);
  4070.  
  4071. }
  4072. break;
  4073.  
  4074. // create new char
  4075. // S 0067 <name>.24B <str>.B <agi>.B <vit>.B <int>.B <dex>.B <luk>.B <slot>.B <hair color>.W <hair style>.W
  4076. case 0x67:
  4077. FIFOSD_CHECK(37);
  4078.  
  4079. if( !char_new ) //turn character creation on/off [Kevin]
  4080. i = -2;
  4081. else
  4082. i = make_new_char_sql(sd, (char*)RFIFOP(fd,2),RFIFOB(fd,26),RFIFOB(fd,27),RFIFOB(fd,28),RFIFOB(fd,29),RFIFOB(fd,30),RFIFOB(fd,31),RFIFOB(fd,32),RFIFOW(fd,33),RFIFOW(fd,35));
  4083.  
  4084. //'Charname already exists' (-1), 'Char creation denied' (-2) and 'You are underaged' (-3)
  4085. if (i < 0)
  4086. {
  4087. WFIFOHEAD(fd,3);
  4088. WFIFOW(fd,0) = 0x6e;
  4089. switch (i) {
  4090. case -1: WFIFOB(fd,2) = 0x00; break;
  4091. case -2: WFIFOB(fd,2) = 0xFF; break;
  4092. case -3: WFIFOB(fd,2) = 0x01; break;
  4093. }
  4094. WFIFOSET(fd,3);
  4095.  
  4096. }
  4097. else
  4098. {
  4099. int len;
  4100. // retrieve data
  4101. struct mmo_charstatus char_dat;
  4102. mmo_char_fromsql(i, &char_dat, false); //Only the short data is needed.
  4103.  
  4104. // send to player
  4105. WFIFOHEAD(fd,2+MAX_CHAR_BUF);
  4106. WFIFOW(fd,0) = 0x6d;
  4107. len = 2 + mmo_char_tobuf(WFIFOP(fd,2), &char_dat);
  4108. WFIFOSET(fd,len);
  4109.  
  4110. // add new entry to the chars list
  4111. ARR_FIND( 0, MAX_CHARS, ch, sd->found_char[ch] == -1 );
  4112. if( ch < MAX_CHARS )
  4113. sd->found_char[ch] = i; // the char_id of the new char
  4114. }
  4115.  
  4116. RFIFOSKIP(fd,37);
  4117. break;
  4118.  
  4119. // delete char
  4120. case 0x68:
  4121. // 2004-04-19aSakexe+ langtype 12 char deletion packet
  4122. case 0x1fb:
  4123. if (cmd == 0x68) FIFOSD_CHECK(46);
  4124. if (cmd == 0x1fb) FIFOSD_CHECK(56);
  4125. {
  4126. int cid = RFIFOL(fd,2);
  4127.  
  4128.  
  4129. ShowInfo(CL_RED"Request Char Deletion: "CL_GREEN"%d (%d)"CL_RESET"\n", sd->account_id, cid);
  4130. memcpy(email, RFIFOP(fd,6), 40);
  4131. RFIFOSKIP(fd,( cmd == 0x68) ? 46 : 56);
  4132.  
  4133. // Check if e-mail is correct
  4134. if(strcmpi(email, sd->email) && //email does not matches and
  4135. (
  4136. strcmp("a@a.com", sd->email) || //it is not default email, or
  4137. (strcmp("a@a.com", email) && strcmp("", email)) //email sent does not matches default
  4138. )) { //Fail
  4139. WFIFOHEAD(fd,3);
  4140. WFIFOW(fd,0) = 0x70;
  4141. WFIFOB(fd,2) = 0; // 00 = Incorrect Email address
  4142. WFIFOSET(fd,3);
  4143. break;
  4144. }
  4145.  
  4146. // check if this char exists
  4147. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  4148. if( i == MAX_CHARS )
  4149. { // Such a character does not exist in the account
  4150. WFIFOHEAD(fd,3);
  4151. WFIFOW(fd,0) = 0x70;
  4152. WFIFOB(fd,2) = 0;
  4153. WFIFOSET(fd,3);
  4154. break;
  4155. }
  4156.  
  4157. // remove char from list and compact it
  4158. for(ch = i; ch < MAX_CHARS-1; ch++)
  4159. sd->found_char[ch] = sd->found_char[ch+1];
  4160. sd->found_char[MAX_CHARS-1] = -1;
  4161.  
  4162. /* Delete character */
  4163. if(delete_char_sql(cid)<0){
  4164. //can't delete the char
  4165. //either SQL error or can't delete by some CONFIG conditions
  4166. //del fail
  4167. WFIFOW(fd, 0) = 0x70;
  4168. WFIFOB(fd, 2) = 0;
  4169. WFIFOSET(fd, 3);
  4170. break;
  4171. }
  4172. /* Char successfully deleted.*/
  4173. WFIFOHEAD(fd,2);
  4174. WFIFOW(fd,0) = 0x6f;
  4175. WFIFOSET(fd,2);
  4176. }
  4177. break;
  4178.  
  4179. // client keep-alive packet (every 12 seconds)
  4180. // R 0187 <account ID>.l
  4181. case 0x187:
  4182. if (RFIFOREST(fd) < 6)
  4183. return 0;
  4184. RFIFOSKIP(fd,6);
  4185. break;
  4186.  
  4187. // char rename request
  4188. // R 028d <account ID>.l <char ID>.l <new name>.24B
  4189. case 0x28d:
  4190. FIFOSD_CHECK(34);
  4191. {
  4192. int i, aid = RFIFOL(fd,2), cid =RFIFOL(fd,6);
  4193. char name[NAME_LENGTH];
  4194. char esc_name[NAME_LENGTH*2+1];
  4195. safestrncpy(name, (char *)RFIFOP(fd,10), NAME_LENGTH);
  4196. RFIFOSKIP(fd,34);
  4197.  
  4198. if( aid != sd->account_id )
  4199. break;
  4200. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  4201. if( i == MAX_CHARS )
  4202. break;
  4203.  
  4204. normalize_name(name,TRIM_CHARS);
  4205. Sql_EscapeStringLen(sql_handle, esc_name, name, strnlen(name, NAME_LENGTH));
  4206. if( !check_char_name(name,esc_name) )
  4207. {
  4208. i = 1;
  4209. safestrncpy(sd->new_name, name, NAME_LENGTH);
  4210. }
  4211. else
  4212. i = 0;
  4213.  
  4214. WFIFOHEAD(fd, 4);
  4215. WFIFOW(fd,0) = 0x28e;
  4216. WFIFOW(fd,2) = i;
  4217. WFIFOSET(fd,4);
  4218. }
  4219. break;
  4220. //Confirm change name.
  4221. // 0x28f <char_id>.L
  4222. case 0x28f:
  4223. // 0: Sucessfull
  4224. // 1: This character's name has already been changed. You cannot change a character's name more than once.
  4225. // 2: User information is not correct.
  4226. // 3: You have failed to change this character's name.
  4227. // 4: Another user is using this character name, so please select another one.
  4228. FIFOSD_CHECK(6);
  4229. {
  4230. int i;
  4231. int cid = RFIFOL(fd,2);
  4232. RFIFOSKIP(fd,6);
  4233.  
  4234. ARR_FIND( 0, MAX_CHARS, i, sd->found_char[i] == cid );
  4235. if( i == MAX_CHARS )
  4236. break;
  4237. i = rename_char_sql(sd, cid);
  4238.  
  4239. WFIFOHEAD(fd, 4);
  4240. WFIFOW(fd,0) = 0x290;
  4241. WFIFOW(fd,2) = i;
  4242. WFIFOSET(fd,4);
  4243. }
  4244. break;
  4245.  
  4246. // captcha code request (not implemented)
  4247. // R 07e5 <?>.w <aid>.l
  4248. case 0x7e5:
  4249. WFIFOHEAD(fd,5);
  4250. WFIFOW(fd,0) = 0x7e9;
  4251. WFIFOW(fd,2) = 5;
  4252. WFIFOB(fd,4) = 1;
  4253. WFIFOSET(fd,5);
  4254. RFIFOSKIP(fd,8);
  4255. break;
  4256.  
  4257. // captcha code check (not implemented)
  4258. // R 07e7 <len>.w <aid>.l <code>.b10 <?>.b14
  4259. case 0x7e7:
  4260. WFIFOHEAD(fd,5);
  4261. WFIFOW(fd,0) = 0x7e9;
  4262. WFIFOW(fd,2) = 5;
  4263. WFIFOB(fd,4) = 1;
  4264. WFIFOSET(fd,5);
  4265. RFIFOSKIP(fd,32);
  4266. break;
  4267. // deletion timer request
  4268.  
  4269. case 0x827:
  4270.  
  4271. FIFOSD_CHECK(6);
  4272.  
  4273. char_delete2_req(fd, sd);
  4274.  
  4275. RFIFOSKIP(fd,6);
  4276.  
  4277. break;
  4278.  
  4279. // deletion accept request
  4280.  
  4281. case 0x829:
  4282.  
  4283. FIFOSD_CHECK(12);
  4284.  
  4285. char_delete2_accept(fd, sd);
  4286.  
  4287. RFIFOSKIP(fd,12);
  4288.  
  4289. break;
  4290.  
  4291. // deletion cancel request
  4292.  
  4293. case 0x82b:
  4294.  
  4295. FIFOSD_CHECK(6);
  4296.  
  4297. char_delete2_cancel(fd, sd);
  4298.  
  4299. RFIFOSKIP(fd,6);
  4300.  
  4301. break;
  4302.  
  4303.  
  4304. // login as map-server
  4305. case 0x2af8:
  4306. if (RFIFOREST(fd) < 60)
  4307. return 0;
  4308. {
  4309. char* l_user = (char*)RFIFOP(fd,2);
  4310. char* l_pass = (char*)RFIFOP(fd,26);
  4311. l_user[23] = '\0';
  4312. l_pass[23] = '\0';
  4313. ARR_FIND( 0, ARRAYLENGTH(server), i, server[i].fd <= 0 );
  4314.  
  4315. if( runflag != CHARSERVER_ST_RUNNING ||
  4316.  
  4317. i == ARRAYLENGTH(server) ||
  4318.  
  4319. strcmp(l_user, userid) != 0 ||
  4320.  
  4321. strcmp(l_pass, passwd) != 0 )
  4322.  
  4323. {
  4324. WFIFOHEAD(fd,3);
  4325. WFIFOW(fd,0) = 0x2af9;
  4326. WFIFOB(fd,2) = 3;
  4327. WFIFOSET(fd,3);
  4328. } else {
  4329. WFIFOHEAD(fd,3);
  4330. WFIFOW(fd,0) = 0x2af9;
  4331. WFIFOB(fd,2) = 0;
  4332. WFIFOSET(fd,3);
  4333.  
  4334. server[i].fd = fd;
  4335. server[i].ip = ntohl(RFIFOL(fd,54));
  4336. server[i].port = ntohs(RFIFOW(fd,58));
  4337. server[i].users = 0;
  4338. memset(server[i].map, 0, sizeof(server[i].map));
  4339. session[fd]->func_parse = parse_frommap;
  4340. session[fd]->flag.server = 1;
  4341. realloc_fifo(fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  4342. char_mapif_init(fd);
  4343. }
  4344.  
  4345. RFIFOSKIP(fd,60);
  4346. }
  4347. return 0; // avoid processing of followup packets here
  4348.  
  4349. // Athena info get
  4350. case 0x7530:
  4351. WFIFOHEAD(fd,10);
  4352. WFIFOW(fd,0) = 0x7531;
  4353. WFIFOB(fd,2) = ATHENA_MAJOR_VERSION;
  4354. WFIFOB(fd,3) = ATHENA_MINOR_VERSION;
  4355. WFIFOB(fd,4) = ATHENA_REVISION;
  4356. WFIFOB(fd,5) = ATHENA_RELEASE_FLAG;
  4357. WFIFOB(fd,6) = ATHENA_OFFICIAL_FLAG;
  4358. WFIFOB(fd,7) = ATHENA_SERVER_INTER | ATHENA_SERVER_CHAR;
  4359. WFIFOW(fd,8) = ATHENA_MOD_VERSION;
  4360. WFIFOSET(fd,10);
  4361.  
  4362. RFIFOSKIP(fd,2);
  4363. break;
  4364.  
  4365. // unknown packet received
  4366. default:
  4367. ShowError("parse_char: Received unknown packet "CL_WHITE"0x%x"CL_RESET" from ip '"CL_WHITE"%s"CL_RESET"'! Disconnecting!\n", RFIFOW(fd,0), ip2str(ipl, NULL));
  4368. set_eof(fd);
  4369. return 0;
  4370. }
  4371. }
  4372.  
  4373. RFIFOFLUSH(fd);
  4374. return 0;
  4375. }
  4376.  
  4377. // Console Command Parser [Wizputer]
  4378. int parse_console(const char* command)
  4379.  
  4380. {
  4381.  
  4382. ShowNotice("Console command: %s\n", command);
  4383.  
  4384. if( strcmpi("shutdown", command) == 0 || strcmpi("exit", command) == 0 || strcmpi("quit", command) == 0 || strcmpi("end", command) == 0 )
  4385. runflag = 0;
  4386. else if( strcmpi("alive", command) == 0 || strcmpi("status", command) == 0 )
  4387. ShowInfo(CL_CYAN"Console: "CL_BOLD"I'm Alive."CL_RESET"\n");
  4388. else if( strcmpi("help", command) == 0 )
  4389.  
  4390. {
  4391.  
  4392. ShowInfo("To shutdown the server:\n");
  4393.  
  4394. ShowInfo(" 'shutdown|exit|quit|end'\n");
  4395.  
  4396. ShowInfo("To know if server is alive:\n");
  4397. ShowInfo(" 'alive|status'\n");
  4398. }
  4399.  
  4400. return 0;
  4401. }
  4402.  
  4403.  
  4404. int mapif_sendall(unsigned char *buf, unsigned int len)
  4405. {
  4406. int i, c;
  4407.  
  4408. c = 0;
  4409. for(i = 0; i < ARRAYLENGTH(server); i++) {
  4410. int fd;
  4411. if ((fd = server[i].fd) > 0) {
  4412. WFIFOHEAD(fd,len);
  4413. memcpy(WFIFOP(fd,0), buf, len);
  4414. WFIFOSET(fd,len);
  4415. c++;
  4416. }
  4417. }
  4418.  
  4419. return c;
  4420. }
  4421.  
  4422. int mapif_sendallwos(int sfd, unsigned char *buf, unsigned int len)
  4423. {
  4424. int i, c;
  4425.  
  4426. c = 0;
  4427. for(i = 0; i < ARRAYLENGTH(server); i++) {
  4428. int fd;
  4429. if ((fd = server[i].fd) > 0 && fd != sfd) {
  4430. WFIFOHEAD(fd,len);
  4431. memcpy(WFIFOP(fd,0), buf, len);
  4432. WFIFOSET(fd,len);
  4433. c++;
  4434. }
  4435. }
  4436.  
  4437. return c;
  4438. }
  4439.  
  4440. int mapif_send(int fd, unsigned char *buf, unsigned int len)
  4441. {
  4442. int i;
  4443.  
  4444. if (fd >= 0) {
  4445. ARR_FIND( 0, ARRAYLENGTH(server), i, fd == server[i].fd );
  4446.  
  4447. if( i < ARRAYLENGTH(server) )
  4448. {
  4449. WFIFOHEAD(fd,len);
  4450. memcpy(WFIFOP(fd,0), buf, len);
  4451. WFIFOSET(fd,len);
  4452. return 1;
  4453. }
  4454. }
  4455. return 0;
  4456. }
  4457.  
  4458. int broadcast_user_count(int tid, unsigned int tick, int id, intptr data)
  4459. {
  4460. uint8 buf[6];
  4461. int users = count_users();
  4462.  
  4463. // only send an update when needed
  4464. static int prev_users = 0;
  4465. if( prev_users == users )
  4466. return 0;
  4467. prev_users = users;
  4468.  
  4469. if( login_fd > 0 && session[login_fd] )
  4470. {
  4471. // send number of user to login server
  4472. WFIFOHEAD(login_fd,6);
  4473. WFIFOW(login_fd,0) = 0x2714;
  4474. WFIFOL(login_fd,2) = users;
  4475. WFIFOSET(login_fd,6);
  4476. }
  4477.  
  4478. // send number of players to all map-servers
  4479. WBUFW(buf,0) = 0x2b00;
  4480. WBUFL(buf,2) = users;
  4481. mapif_sendall(buf,6);
  4482.  
  4483. return 0;
  4484. }
  4485.  
  4486. /// load this char's account id into the 'online accounts' packet
  4487. static int send_accounts_tologin_sub(DBKey key, void* data, va_list ap)
  4488. {
  4489. struct online_char_data* character = (struct online_char_data*)data;
  4490. int* i = va_arg(ap, int*);
  4491.  
  4492. if(character->server > -1)
  4493. {
  4494. WFIFOL(login_fd,8+(*i)*4) = character->account_id;
  4495. (*i)++;
  4496. return 1;
  4497. }
  4498. return 0;
  4499. }
  4500.  
  4501. int send_accounts_tologin(int tid, unsigned int tick, int id, intptr data)
  4502. {
  4503. if (login_fd > 0 && session[login_fd])
  4504. {
  4505. // send account list to login server
  4506. int users = online_char_db->size(online_char_db);
  4507. int i = 0;
  4508.  
  4509. WFIFOHEAD(login_fd,8+users*4);
  4510. WFIFOW(login_fd,0) = 0x272d;
  4511. online_char_db->foreach(online_char_db, send_accounts_tologin_sub, &i, users);
  4512. WFIFOW(login_fd,2) = 8+ i*4;
  4513. WFIFOL(login_fd,4) = i;
  4514. WFIFOSET(login_fd,WFIFOW(login_fd,2));
  4515. }
  4516. return 0;
  4517. }
  4518.  
  4519. int check_connect_login_server(int tid, unsigned int tick, int id, intptr data)
  4520. {
  4521. if (login_fd > 0 && session[login_fd] != NULL)
  4522. return 0;
  4523.  
  4524. ShowInfo("Attempt to connect to login-server...\n");
  4525. login_fd = make_connection(login_ip, login_port);
  4526. if (login_fd == -1)
  4527. { //Try again later. [Skotlex]
  4528. login_fd = 0;
  4529. return 0;
  4530. }
  4531. session[login_fd]->func_parse = parse_fromlogin;
  4532. session[login_fd]->flag.server = 1;
  4533. realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK);
  4534.  
  4535. WFIFOHEAD(login_fd,86);
  4536. WFIFOW(login_fd,0) = 0x2710;
  4537. memcpy(WFIFOP(login_fd,2), userid, 24);
  4538. memcpy(WFIFOP(login_fd,26), passwd, 24);
  4539. WFIFOL(login_fd,50) = 0;
  4540. WFIFOL(login_fd,54) = htonl(char_ip);
  4541. WFIFOW(login_fd,58) = htons(char_port);
  4542. memcpy(WFIFOP(login_fd,60), server_name, 20);
  4543. WFIFOW(login_fd,80) = 0;
  4544. WFIFOW(login_fd,82) = char_maintenance;
  4545. WFIFOW(login_fd,84) = char_new_display; //only display (New) if they want to [Kevin]
  4546. WFIFOSET(login_fd,86);
  4547.  
  4548. return 1;
  4549. }
  4550.  
  4551. // sends a ping packet to login server (will receive pong 0x2718)
  4552. int ping_login_server(int tid, unsigned int tick, int id, intptr data)
  4553. {
  4554. if (login_fd > 0 && session[login_fd] != NULL)
  4555. {
  4556. WFIFOHEAD(login_fd,2);
  4557. WFIFOW(login_fd,0) = 0x2719;
  4558. WFIFOSET(login_fd,2);
  4559. }
  4560. return 0;
  4561. }
  4562.  
  4563. //------------------------------------------------
  4564. //Invoked 15 seconds after mapif_disconnectplayer in case the map server doesn't
  4565. //replies/disconnect the player we tried to kick. [Skotlex]
  4566. //------------------------------------------------
  4567. static int chardb_waiting_disconnect(int tid, unsigned int tick, int id, intptr data)
  4568. {
  4569. struct online_char_data* character;
  4570. if ((character = (struct online_char_data*)idb_get(online_char_db, id)) != NULL && character->waiting_disconnect == tid)
  4571. { //Mark it offline due to timeout.
  4572. character->waiting_disconnect = -1;
  4573. set_char_offline(character->char_id, character->account_id);
  4574. }
  4575. return 0;
  4576. }
  4577.  
  4578. static int online_data_cleanup_sub(DBKey key, void *data, va_list ap)
  4579. {
  4580. struct online_char_data *character= (struct online_char_data*)data;
  4581. if (character->fd != -1)
  4582. return 0; //Character still connected
  4583. if (character->server == -2) //Unknown server.. set them offline
  4584. set_char_offline(character->char_id, character->account_id);
  4585. if (character->server < 0)
  4586. //Free data from players that have not been online for a while.
  4587. db_remove(online_char_db, key);
  4588. return 0;
  4589. }
  4590.  
  4591. static int online_data_cleanup(int tid, unsigned int tick, int id, intptr data)
  4592. {
  4593. online_char_db->foreach(online_char_db, online_data_cleanup_sub);
  4594. return 0;
  4595. }
  4596.  
  4597. //----------------------------------
  4598. // Reading Lan Support configuration
  4599. // Rewrote: Anvanced subnet check [LuzZza]
  4600. //----------------------------------
  4601. int char_lan_config_read(const char *lancfgName)
  4602. {
  4603. FILE *fp;
  4604. int line_num = 0;
  4605. char line[1024], w1[64], w2[64], w3[64], w4[64];
  4606.  
  4607. if((fp = fopen(lancfgName, "r")) == NULL) {
  4608. ShowWarning("LAN Support configuration file is not found: %s\n", lancfgName);
  4609. return 1;
  4610. }
  4611.  
  4612. ShowInfo("Reading the configuration file %s...\n", lancfgName);
  4613.  
  4614. while(fgets(line, sizeof(line), fp))
  4615. {
  4616. line_num++;
  4617. if ((line[0] == '/' && line[1] == '/') || line[0] == '\n' || line[1] == '\n')
  4618. continue;
  4619.  
  4620. if(sscanf(line,"%[^:]: %[^:]:%[^:]:%[^\r\n]", w1, w2, w3, w4) != 4) {
  4621.  
  4622. ShowWarning("Error syntax of configuration file %s in line %d.\n", lancfgName, line_num);
  4623. continue;
  4624. }
  4625.  
  4626. remove_control_chars(w1);
  4627. remove_control_chars(w2);
  4628. remove_control_chars(w3);
  4629. remove_control_chars(w4);
  4630.  
  4631. if( strcmpi(w1, "subnet") == 0 )
  4632. {
  4633. subnet[subnet_count].mask = str2ip(w2);
  4634. subnet[subnet_count].char_ip = str2ip(w3);
  4635. subnet[subnet_count].map_ip = str2ip(w4);
  4636.  
  4637. if( (subnet[subnet_count].char_ip & subnet[subnet_count].mask) != (subnet[subnet_count].map_ip & subnet[subnet_count].mask) )
  4638. {
  4639. ShowError("%s: Configuration Error: The char server (%s) and map server (%s) belong to different subnetworks!\n", lancfgName, w3, w4);
  4640. continue;
  4641. }
  4642.  
  4643. subnet_count++;
  4644. }
  4645. }
  4646.  
  4647. ShowStatus("Read information about %d subnetworks.\n", subnet_count);
  4648.  
  4649. fclose(fp);
  4650. return 0;
  4651. }
  4652. #endif //TXT_SQL_CONVERT
  4653.  
  4654. void sql_config_read(const char* cfgName)
  4655. {
  4656. char line[1024], w1[1024], w2[1024];
  4657. FILE* fp;
  4658.  
  4659. ShowInfo("Reading file %s...\n", cfgName);
  4660.  
  4661. if ((fp = fopen(cfgName, "r")) == NULL) {
  4662. ShowError("file not found: %s\n", cfgName);
  4663. return;
  4664. }
  4665.  
  4666. while(fgets(line, sizeof(line), fp))
  4667. {
  4668. if(line[0] == '/' && line[1] == '/')
  4669. continue;
  4670.  
  4671. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  4672. continue;
  4673.  
  4674. if(!strcmpi(w1,"char_db"))
  4675. safestrncpy(char_db, w2, sizeof(char_db));
  4676. else if(!strcmpi(w1,"scdata_db"))
  4677. safestrncpy(scdata_db, w2, sizeof(scdata_db));
  4678. else if(!strcmpi(w1,"cart_db"))
  4679. safestrncpy(cart_db, w2, sizeof(cart_db));
  4680. else if(!strcmpi(w1,"inventory_db"))
  4681. safestrncpy(inventory_db, w2, sizeof(inventory_db));
  4682. else if(!strcmpi(w1,"charlog_db"))
  4683. safestrncpy(charlog_db, w2, sizeof(charlog_db));
  4684. else if(!strcmpi(w1,"storage_db"))
  4685. safestrncpy(storage_db, w2, sizeof(storage_db));
  4686. else if(!strcmpi(w1,"reg_db"))
  4687. safestrncpy(reg_db, w2, sizeof(reg_db));
  4688. else if(!strcmpi(w1,"skill_db"))
  4689. safestrncpy(skill_db, w2, sizeof(skill_db));
  4690. else if(!strcmpi(w1,"interlog_db"))
  4691. safestrncpy(interlog_db, w2, sizeof(interlog_db));
  4692. else if(!strcmpi(w1,"memo_db"))
  4693. safestrncpy(memo_db, w2, sizeof(memo_db));
  4694. else if(!strcmpi(w1,"guild_db"))
  4695. safestrncpy(guild_db, w2, sizeof(guild_db));
  4696. else if(!strcmpi(w1,"guild_alliance_db"))
  4697. safestrncpy(guild_alliance_db, w2, sizeof(guild_alliance_db));
  4698. else if(!strcmpi(w1,"guild_castle_db"))
  4699. safestrncpy(guild_castle_db, w2, sizeof(guild_castle_db));
  4700. else if(!strcmpi(w1,"guild_expulsion_db"))
  4701. safestrncpy(guild_expulsion_db, w2, sizeof(guild_expulsion_db));
  4702. else if(!strcmpi(w1,"guild_member_db"))
  4703. safestrncpy(guild_member_db, w2, sizeof(guild_member_db));
  4704. else if(!strcmpi(w1,"guild_skill_db"))
  4705. safestrncpy(guild_skill_db, w2, sizeof(guild_skill_db));
  4706. else if(!strcmpi(w1,"guild_position_db"))
  4707. safestrncpy(guild_position_db, w2, sizeof(guild_position_db));
  4708. else if(!strcmpi(w1,"guild_storage_db"))
  4709. safestrncpy(guild_storage_db, w2, sizeof(guild_storage_db));
  4710. else if(!strcmpi(w1,"party_db"))
  4711. safestrncpy(party_db, w2, sizeof(party_db));
  4712. else if(!strcmpi(w1,"pet_db"))
  4713. safestrncpy(pet_db, w2, sizeof(pet_db));
  4714. else if(!strcmpi(w1,"mail_db"))
  4715. safestrncpy(mail_db, w2, sizeof(mail_db));
  4716. else if(!strcmpi(w1,"auction_db"))
  4717. safestrncpy(auction_db, w2, sizeof(auction_db));
  4718. else if(!strcmpi(w1,"friend_db"))
  4719. safestrncpy(friend_db, w2, sizeof(friend_db));
  4720. else if(!strcmpi(w1,"hotkey_db"))
  4721. safestrncpy(hotkey_db, w2, sizeof(hotkey_db));
  4722. else if(!strcmpi(w1,"quest_db"))
  4723. safestrncpy(quest_db,w2,sizeof(quest_db));
  4724. //support the import command, just like any other config
  4725. else if(!strcmpi(w1,"import"))
  4726. sql_config_read(w2);
  4727. }
  4728. fclose(fp);
  4729. ShowInfo("Done reading %s.\n", cfgName);
  4730. }
  4731. #ifndef TXT_SQL_CONVERT
  4732.  
  4733. int char_config_read(const char* cfgName)
  4734. {
  4735. char line[1024], w1[1024], w2[1024];
  4736. FILE* fp = fopen(cfgName, "r");
  4737.  
  4738. if (fp == NULL) {
  4739. ShowError("Configuration file not found: %s.\n", cfgName);
  4740. return 1;
  4741. }
  4742.  
  4743. ShowInfo("Reading configuration file %s...\n", cfgName);
  4744. while(fgets(line, sizeof(line), fp))
  4745. {
  4746. if (line[0] == '/' && line[1] == '/')
  4747. continue;
  4748.  
  4749. if (sscanf(line, "%[^:]: %[^\r\n]", w1, w2) != 2)
  4750. continue;
  4751.  
  4752. remove_control_chars(w1);
  4753. remove_control_chars(w2);
  4754. if(strcmpi(w1,"timestamp_format") == 0) {
  4755. safestrncpy(timestamp_format, w2, sizeof(timestamp_format));
  4756. } else if(strcmpi(w1,"console_silent")==0){
  4757. ShowInfo("Console Silent Setting: %d\n", atoi(w2));
  4758. msg_silent = atoi(w2);
  4759. } else if(strcmpi(w1,"stdout_with_ansisequence")==0){
  4760. stdout_with_ansisequence = config_switch(w2);
  4761. } else if (strcmpi(w1, "userid") == 0) {
  4762. safestrncpy(userid, w2, sizeof(userid));
  4763. } else if (strcmpi(w1, "passwd") == 0) {
  4764. safestrncpy(passwd, w2, sizeof(passwd));
  4765. } else if (strcmpi(w1, "server_name") == 0) {
  4766. safestrncpy(server_name, w2, sizeof(server_name));
  4767. ShowStatus("%s server has been initialized\n", w2);
  4768. } else if (strcmpi(w1, "wisp_server_name") == 0) {
  4769. if (strlen(w2) >= 4) {
  4770. safestrncpy(wisp_server_name, w2, sizeof(wisp_server_name));
  4771. }
  4772. } else if (strcmpi(w1, "login_ip") == 0) {
  4773. char ip_str[16];
  4774. login_ip = host2ip(w2);
  4775. if (login_ip) {
  4776. safestrncpy(login_ip_str, w2, sizeof(login_ip_str));
  4777. ShowStatus("Login server IP address : %s -> %s\n", w2, ip2str(login_ip, ip_str));
  4778. }
  4779. } else if (strcmpi(w1, "login_port") == 0) {
  4780. login_port = atoi(w2);
  4781. } else if (strcmpi(w1, "char_ip") == 0) {
  4782. char ip_str[16];
  4783. char_ip = host2ip(w2);
  4784. if (char_ip){
  4785. safestrncpy(char_ip_str, w2, sizeof(char_ip_str));
  4786. ShowStatus("Character server IP address : %s -> %s\n", w2, ip2str(char_ip, ip_str));
  4787. }
  4788. } else if (strcmpi(w1, "bind_ip") == 0) {
  4789. char ip_str[16];
  4790. bind_ip = host2ip(w2);
  4791. if (bind_ip) {
  4792. safestrncpy(bind_ip_str, w2, sizeof(bind_ip_str));
  4793. ShowStatus("Character server binding IP address : %s -> %s\n", w2, ip2str(bind_ip, ip_str));
  4794. }
  4795. } else if (strcmpi(w1, "char_port") == 0) {
  4796. char_port = atoi(w2);
  4797. } else if (strcmpi(w1, "char_maintenance") == 0) {
  4798. char_maintenance = atoi(w2);
  4799. } else if (strcmpi(w1, "char_new") == 0) {
  4800. char_new = (bool)atoi(w2);
  4801. } else if (strcmpi(w1, "char_new_display") == 0) {
  4802. char_new_display = atoi(w2);
  4803. } else if (strcmpi(w1, "max_connect_user") == 0) {
  4804. max_connect_user = atoi(w2);
  4805. if (max_connect_user < 0)
  4806. max_connect_user = 0; // unlimited online players
  4807. } else if(strcmpi(w1, "gm_allow_level") == 0) {
  4808. gm_allow_level = atoi(w2);
  4809. if(gm_allow_level < 0)
  4810. gm_allow_level = 99;
  4811. } else if (strcmpi(w1, "autosave_time") == 0) {
  4812. autosave_interval = atoi(w2)*1000;
  4813. if (autosave_interval <= 0)
  4814. autosave_interval = DEFAULT_AUTOSAVE_INTERVAL;
  4815. } else if (strcmpi(w1, "save_log") == 0) {
  4816. save_log = config_switch(w2);
  4817. } else if (strcmpi(w1, "start_point") == 0) {
  4818. char map[MAP_NAME_LENGTH_EXT];
  4819. int x, y;
  4820. if (sscanf(w2, "%15[^,],%d,%d", map, &x, &y) < 3)
  4821. continue;
  4822. start_point.map = mapindex_name2id(map);
  4823. if (!start_point.map)
  4824. ShowError("Specified start_point %s not found in map-index cache.\n", map);
  4825. start_point.x = x;
  4826. start_point.y = y;
  4827. } else if (strcmpi(w1, "start_zeny") == 0) {
  4828. start_zeny = atoi(w2);
  4829. if (start_zeny < 0)
  4830. start_zeny = 0;
  4831. } else if (strcmpi(w1, "start_weapon") == 0) {
  4832. start_weapon = atoi(w2);
  4833. if (start_weapon < 0)
  4834. start_weapon = 0;
  4835. } else if (strcmpi(w1, "start_armor") == 0) {
  4836. start_armor = atoi(w2);
  4837. if (start_armor < 0)
  4838. start_armor = 0;
  4839. } else if(strcmpi(w1,"log_char")==0) { //log char or not [devil]
  4840. log_char = atoi(w2);
  4841. } else if (strcmpi(w1, "unknown_char_name") == 0) {
  4842. safestrncpy(unknown_char_name, w2, sizeof(unknown_char_name));
  4843. unknown_char_name[NAME_LENGTH-1] = '\0';
  4844. } else if (strcmpi(w1, "name_ignoring_case") == 0) {
  4845. name_ignoring_case = (bool)config_switch(w2);
  4846. } else if (strcmpi(w1, "char_name_option") == 0) {
  4847. char_name_option = atoi(w2);
  4848. } else if (strcmpi(w1, "char_name_letters") == 0) {
  4849. safestrncpy(char_name_letters, w2, sizeof(char_name_letters));
  4850. } else if (strcmpi(w1, "chars_per_account") == 0) { //maxchars per account [Sirius]
  4851. char_per_account = atoi(w2);
  4852. } else if (strcmpi(w1, "char_del_level") == 0) { //disable/enable char deletion by its level condition [Lupus]
  4853. char_del_level = atoi(w2);
  4854. } else if (strcmpi(w1, "char_del_delay") == 0) {
  4855.  
  4856. char_del_delay = atoi(w2);
  4857. } else if(strcmpi(w1,"db_path")==0) {
  4858.  
  4859. safestrncpy(db_path, w2, sizeof(db_path));
  4860. } else if (strcmpi(w1, "console") == 0) {
  4861. console = config_switch(w2);
  4862. } else if (strcmpi(w1, "fame_list_alchemist") == 0) {
  4863. fame_list_size_chemist = atoi(w2);
  4864. if (fame_list_size_chemist > MAX_FAME_LIST) {
  4865. ShowWarning("Max fame list size is %d (fame_list_alchemist)\n", MAX_FAME_LIST);
  4866. fame_list_size_chemist = MAX_FAME_LIST;
  4867. }
  4868. } else if (strcmpi(w1, "fame_list_blacksmith") == 0) {
  4869. fame_list_size_smith = atoi(w2);
  4870. if (fame_list_size_smith > MAX_FAME_LIST) {
  4871. ShowWarning("Max fame list size is %d (fame_list_blacksmith)\n", MAX_FAME_LIST);
  4872. fame_list_size_smith = MAX_FAME_LIST;
  4873. }
  4874. } else if (strcmpi(w1, "fame_list_taekwon") == 0) {
  4875. fame_list_size_taekwon = atoi(w2);
  4876. if (fame_list_size_taekwon > MAX_FAME_LIST) {
  4877. ShowWarning("Max fame list size is %d (fame_list_taekwon)\n", MAX_FAME_LIST);
  4878. fame_list_size_taekwon = MAX_FAME_LIST;
  4879. }
  4880. } else if (strcmpi(w1, "guild_exp_rate") == 0) {
  4881. guild_exp_rate = atoi(w2);
  4882. } else if (strcmpi(w1, "import") == 0) {
  4883. char_config_read(w2);
  4884. }
  4885. }
  4886. fclose(fp);
  4887.  
  4888. ShowInfo("Done reading %s.\n", cfgName);
  4889. return 0;
  4890. }
  4891.  
  4892. void do_final(void)
  4893. {
  4894. ShowStatus("Terminating...\n");
  4895.  
  4896. set_all_offline(-1);
  4897. set_all_offline_sql();
  4898.  
  4899. inter_final();
  4900.  
  4901. flush_fifos();
  4902.  
  4903. do_final_mapif();
  4904.  
  4905. do_final_loginif();
  4906.  
  4907.  
  4908.  
  4909. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `ragsrvinfo`") )
  4910. Sql_ShowDebug(sql_handle);
  4911. char_db_->destroy(char_db_, NULL);
  4912.  
  4913. online_char_db->destroy(online_char_db, NULL);
  4914.  
  4915. auth_db->destroy(auth_db, NULL);
  4916. if( char_fd != -1 )
  4917.  
  4918. {
  4919. do_close(char_fd);
  4920. char_fd = -1;
  4921.  
  4922. }
  4923.  
  4924.  
  4925. Sql_Free(sql_handle);
  4926.  
  4927. mapindex_final();
  4928. ShowStatus("Finished.\n");
  4929. }
  4930.  
  4931. //------------------------------
  4932. // Function called when the server
  4933. // has received a crash signal.
  4934. //------------------------------
  4935. void do_abort(void)
  4936. {
  4937. }
  4938.  
  4939. void set_server_type(void)
  4940. {
  4941. SERVER_TYPE = ATHENA_SERVER_CHAR;
  4942. }
  4943.  
  4944. /// Called when a terminate signal is received.
  4945.  
  4946. void do_shutdown(void)
  4947.  
  4948. {
  4949.  
  4950. if( runflag != CHARSERVER_ST_SHUTDOWN )
  4951.  
  4952. {
  4953.  
  4954. int id;
  4955.  
  4956. runflag = CHARSERVER_ST_SHUTDOWN;
  4957.  
  4958. ShowStatus("Shutting down...\n");
  4959.  
  4960. // TODO proper shutdown procedure; wait for acks?, kick all characters, ... [FlavoJS]
  4961.  
  4962. for( id = 0; id < ARRAYLENGTH(server); ++id )
  4963.  
  4964. mapif_server_reset(id);
  4965.  
  4966. loginif_check_shutdown();
  4967.  
  4968. flush_fifos();
  4969.  
  4970. runflag = CORE_ST_STOP;
  4971.  
  4972. }
  4973.  
  4974. }
  4975.  
  4976. int do_init(int argc, char **argv)
  4977.  
  4978. {
  4979.  
  4980. //Read map indexes
  4981. mapindex_init();
  4982. start_point.map = mapindex_name2id("new_zone01");
  4983.  
  4984. char_config_read((argc < 2) ? CHAR_CONF_NAME : argv[1]);
  4985. char_lan_config_read((argc > 3) ? argv[3] : LAN_CONF_NAME);
  4986. sql_config_read(SQL_CONF_NAME);
  4987.  
  4988. if (strcmp(userid, "s1")==0 && strcmp(passwd, "p1")==0) {
  4989. ShowError("Using the default user/password s1/p1 is NOT RECOMMENDED.\n");
  4990. ShowNotice("Please edit your 'login' table to create a proper inter-server user/password (gender 'S')\n");
  4991. ShowNotice("And then change the user/password to use in conf/char_athena.conf (or conf/import/char_conf.txt)\n");
  4992. }
  4993.  
  4994. ShowInfo("Finished reading the char-server configuration.\n");
  4995.  
  4996. inter_init_sql((argc > 2) ? argv[2] : inter_cfgName); // inter server ÃʱâÈ­
  4997. ShowInfo("Finished reading the inter-server configuration.\n");
  4998.  
  4999. ShowInfo("Initializing char server.\n");
  5000. auth_db = idb_alloc(DB_OPT_RELEASE_DATA);
  5001. online_char_db = idb_alloc(DB_OPT_RELEASE_DATA);
  5002. mmo_char_sql_init();
  5003. char_read_fame_list(); //Read fame lists.
  5004. ShowInfo("char server initialized.\n");
  5005.  
  5006.  
  5007.  
  5008. if ((naddr_ != 0) && (!login_ip || !char_ip))
  5009. {
  5010. char ip_str[16];
  5011. ip2str(addr_[0], ip_str);
  5012.  
  5013. if (naddr_ > 1)
  5014. ShowStatus("Multiple interfaces detected.. using %s as our IP address\n", ip_str);
  5015. else
  5016. ShowStatus("Defaulting to %s as our IP address\n", ip_str);
  5017. if (!login_ip) {
  5018. safestrncpy(login_ip_str, ip_str, sizeof(login_ip_str));
  5019. login_ip = str2ip(login_ip_str);
  5020. }
  5021. if (!char_ip) {
  5022. safestrncpy(char_ip_str, ip_str, sizeof(char_ip_str));
  5023. char_ip = str2ip(char_ip_str);
  5024. }
  5025. }
  5026.  
  5027. do_init_loginif();
  5028.  
  5029. do_init_mapif();
  5030.  
  5031. // periodically update the overall user count on all mapservers + login server
  5032. add_timer_func_list(broadcast_user_count, "broadcast_user_count");
  5033. add_timer_interval(gettick() + 1000, broadcast_user_count, 0, 0, 5 * 1000);
  5034.  
  5035.  
  5036.  
  5037. // ???
  5038. add_timer_func_list(chardb_waiting_disconnect, "chardb_waiting_disconnect");
  5039.  
  5040. // ???
  5041. add_timer_func_list(online_data_cleanup, "online_data_cleanup");
  5042. add_timer_interval(gettick() + 1000, online_data_cleanup, 0, 0, 600 * 1000);
  5043.  
  5044. if( console )
  5045. {
  5046. //##TODO invoke a CONSOLE_START plugin event
  5047. }
  5048.  
  5049. //Cleaning the tables for NULL entrys @ startup [Sirius]
  5050. //Chardb clean
  5051. ShowInfo("Cleaning the '%s' table...\n", char_db);
  5052. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `account_id` = '0'", char_db) )
  5053. Sql_ShowDebug(sql_handle);
  5054.  
  5055.  
  5056.  
  5057. //guilddb clean
  5058. ShowInfo("Cleaning the '%s' table...\n", guild_db);
  5059. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_lv` = '0' AND `max_member` = '0' AND `exp` = '0' AND `next_exp` = '0' AND `average_lv` = '0'", guild_db) )
  5060. Sql_ShowDebug(sql_handle);
  5061.  
  5062. //guildmemberdb clean
  5063. ShowInfo("Cleaning the '%s' table...\n", guild_member_db);
  5064. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `guild_id` = '0' AND `account_id` = '0' AND `char_id` = '0'", guild_member_db) )
  5065. Sql_ShowDebug(sql_handle);
  5066.  
  5067. ShowInfo("End of char server initilization function.\n");
  5068. set_defaultparse(parse_char);
  5069. ShowInfo("open port %d.....\n",char_port);
  5070. char_fd = make_listen_bind(bind_ip, char_port);
  5071. ShowStatus("The char-server is "CL_GREEN"ready"CL_RESET" (Server is listening on the port %d).\n\n", char_port);
  5072. if( runflag != CORE_ST_STOP )
  5073.  
  5074. {
  5075.  
  5076. shutdown_callback = do_shutdown;
  5077.  
  5078. runflag = CHARSERVER_ST_RUNNING;
  5079.  
  5080. }
  5081.  
  5082. return 0;
  5083. }
  5084.  
  5085. #endif //TXT_SQL_CONVERT
Add Comment
Please, Sign In to add comment