Advertisement
Guest User

sdfsd

a guest
Nov 24th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. // (c) 2008 - 2013 Harmony Project; Daniel Stelter-Gliese / Sirius_White
  2. // For more information contact info@harmonize.it
  3. //
  4. // This file is NOT public - you are not allowed to distribute it.
  5. #include "../common/cbasetypes.h"
  6. #include "../common/showmsg.h"
  7. #include "../common/db.h"
  8. #include "../common/strlib.h"
  9. #include "../common/mmo.h"
  10. #include "../common/socket.h"
  11. #include "../common/timer.h"
  12. #include "../common/sql.h"
  13. #include "../common/harmony.h"
  14.  
  15. #ifndef HARMSW
  16. #define HARMSW HARMSW_EATHENA
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22.  
  23. #include "pc.h"
  24. #include "clif.h"
  25. #include "atcommand.h"
  26. #include "chrif.h"
  27. #if HARMSW == HARMSW_RATHENA_GROUP
  28. #include "pc_groups.h"
  29. #endif
  30. #include "npc.h"
  31. #include "harmony.h"
  32.  
  33. void _FASTCALL harmony_action_request(int fd, int task, int id, intptr data);
  34. void _FASTCALL harmony_log(int fd, const char *msg);
  35. static int harmony_group_register_timer(int tid, unsigned int tick, int id, intptr data);
  36.  
  37. // ----
  38.  
  39. static SqlStmt* log_stmt = NULL;
  40. static SqlStmt* ban_stmt = NULL;
  41. static SqlStmt* admin_stmt = NULL;
  42.  
  43. enum {
  44. LOG_SQL = 1,
  45. LOG_TXT,
  46. LOG_TRYSQL
  47. };
  48. static int log_method = LOG_TRYSQL;
  49.  
  50. static int tid_group_register = INVALID_TIMER;
  51. static int current_groupscan_minlevel = 0;
  52.  
  53. #ifdef HARMONY_USE_POINTLESS_OOP_INTERFACE
  54. #define SqlStmt_Malloc SQL->StmtMalloc
  55. #define SqlStmt_Prepare SQL->StmtPrepare
  56. #define SqlStmt_BindParam SQL->StmtBindParam
  57. #define SqlStmt_BindColumn SQL->StmtBindColumn
  58. #define SqlStmt_Execute SQL->StmtExecute
  59. #define SqlStmt_Free SQL->StmtFree
  60. #define Sql_EscapeStringLen SQL->EscapeStringLen
  61. #define SqlStmt_NextRow SQL->StmtNextRow
  62. #define Sql_QueryStr SQL->QueryStr
  63. #define Sql_Query SQL->Query
  64. #define Sql_NumRows SQL->NumRows
  65.  
  66. #define chrif_isconnected chrif->isconnected
  67. #define map_id2bl iMap->id2bl
  68. #define map_mapname2mapid iMap->mapname2mapid
  69.  
  70. #define clif_send clif->send
  71. #define clif_authfail_fd clif->authfail_fd
  72. #define clif_displaymessage clif->message
  73. #define is_atcommand atcommand->parse
  74. #define run_script script->run
  75.  
  76. #define pc_group_id2level _harmony_localfix_pc_group_id2level
  77. #endif
  78.  
  79. int _harmony_localfix_pc_group_id2level(int id) {
  80. GroupSettings* group = pc_group_id2group(id);
  81. if (!group)
  82. return 0;
  83. return pc_group_get_level(group);
  84. }
  85.  
  86. // ----
  87.  
  88. void harmony_init() {
  89. if (logmysql_handle != NULL) {
  90. log_stmt = SqlStmt_Malloc(logmysql_handle);
  91. if (SQL_SUCCESS != SqlStmt_Prepare(log_stmt, "INSERT DELAYED INTO harmony_log (`account_id`, `char_name`, `IP`, `data`) VALUES (?, ?, ?, ?)")) {
  92. ShowFatalError("Harmony: Preparing statement 1 failed.\n");
  93. Sql_ShowDebug(logmysql_handle);
  94. exit(EXIT_FAILURE);
  95. }
  96. }
  97.  
  98. if (mmysql_handle == NULL) {
  99. ShowFatalError("Harmony: SQL is not yet initialized. Please report this!\n");
  100. exit(EXIT_FAILURE);
  101. }
  102.  
  103. ban_stmt = SqlStmt_Malloc(mmysql_handle);
  104. if (SQL_SUCCESS != SqlStmt_Prepare(ban_stmt, "UPDATE login SET state = ? WHERE account_id = ?")) {
  105. ShowFatalError("Harmony: Preparing statement 2 failed.\n");
  106. Sql_ShowDebug(mmysql_handle);
  107. exit(EXIT_FAILURE);
  108. }
  109.  
  110. admin_stmt = SqlStmt_Malloc(mmysql_handle);
  111. #if HARMSW == HARMSW_RATHENA_GROUP
  112. if (SQL_SUCCESS != SqlStmt_Prepare(admin_stmt, "SELECT account_id FROM `login` WHERE `group_id` = ?")) {
  113. #else
  114. if (SQL_SUCCESS != SqlStmt_Prepare(admin_stmt, "SELECT account_id FROM `login` WHERE `level` >= ?")) {
  115. #endif
  116. ShowFatalError("Harmony: Preparing statement 3 failed.\n");
  117. Sql_ShowDebug(mmysql_handle);
  118. exit(EXIT_FAILURE);
  119. }
  120.  
  121. ea_funcs->action_request = harmony_action_request;
  122. ea_funcs->player_log = harmony_log;
  123.  
  124. harm_funcs->zone_init();
  125. }
  126.  
  127. void harmony_final() {
  128. harm_funcs->zone_final();
  129. if (log_stmt) {
  130. SqlStmt_Free(log_stmt);
  131. log_stmt = NULL;
  132. }
  133. SqlStmt_Free(ban_stmt);
  134. SqlStmt_Free(admin_stmt);
  135. if (tid_group_register != INVALID_TIMER) {
  136. _athena_delete_timer(tid_group_register, harmony_group_register_timer);
  137. tid_group_register = INVALID_TIMER;
  138. }
  139. }
  140.  
  141. void harmony_parse(int fd,struct map_session_data *sd) {
  142. }
  143.  
  144. void harmony_ban(int32 account_id, int state) {
  145. if (!ban_stmt)
  146. return;
  147. if (SQL_SUCCESS != SqlStmt_BindParam(ban_stmt, 0, SQLDT_INT, (void*)&state, sizeof(state)) ||
  148. SQL_SUCCESS != SqlStmt_BindParam(ban_stmt, 1, SQLDT_INT, (void*)&account_id, sizeof(account_id)) ||
  149. SQL_SUCCESS != SqlStmt_Execute(ban_stmt))
  150. {
  151. Sql_ShowDebug(mmysql_handle);
  152. }
  153. ShowMessage(""CL_MAGENTA"[Harmony]"CL_RESET": Banned account #%d (state %d)\n", account_id, state);
  154. }
  155.  
  156. int harmony_log_sql(TBL_PC* sd, const char* ip, const char* msg) {
  157. if (!logmysql_handle || !log_stmt)
  158. return 0;
  159. if (SQL_SUCCESS != SqlStmt_BindParam(log_stmt, 0, SQLDT_INT, (void*)&sd->status.account_id, sizeof(sd->status.account_id)) ||
  160. SQL_SUCCESS != SqlStmt_BindParam(log_stmt, 1, SQLDT_STRING, (void*)&sd->status.name, strlen(sd->status.name)) ||
  161. SQL_SUCCESS != SqlStmt_BindParam(log_stmt, 2, SQLDT_STRING, (void*)ip, strlen(ip)) ||
  162. SQL_SUCCESS != SqlStmt_BindParam(log_stmt, 3, SQLDT_STRING, (void*)msg, strlen(msg)) ||
  163. SQL_SUCCESS != SqlStmt_Execute(log_stmt))
  164. {
  165. Sql_ShowDebug(logmysql_handle);
  166. return 0;
  167. }
  168. return 1;
  169. }
  170.  
  171. int harmony_log_txt(TBL_PC* sd, const char* ip, const char* msg) {
  172. FILE* logfp;
  173. static char timestring[255];
  174. time_t curtime;
  175.  
  176. if((logfp = fopen("./log/harmony.log", "a+")) == NULL)
  177. return 0;
  178. time(&curtime);
  179. strftime(timestring, 254, "%m/%d/%Y %H:%M:%S", localtime(&curtime));
  180. fprintf(logfp,"%s - %s[%d:%d] - %s\t%s\n", timestring, sd->status.name, sd->status.account_id, sd->status.char_id, ip, msg);
  181. fclose(logfp);
  182. return 1;
  183. }
  184.  
  185. void _FASTCALL harmony_log(int fd, const char *msg) {
  186. char ip[20];
  187. TBL_PC *sd = (TBL_PC*)session[fd]->session_data;
  188.  
  189. if (!sd)
  190. return;
  191.  
  192. sprintf(ip, "%u.%u.%u.%u", CONVIP(session[fd]->client_addr));
  193.  
  194. if (log_method == LOG_SQL && !harmony_log_sql(sd, ip, msg)) {
  195. ShowError("Logging to harmony_log failed. Please check your Harmony setup.\n");
  196. } else if (log_method == LOG_TRYSQL && log_stmt && harmony_log_sql(sd, ip, msg)) {
  197. ;
  198. } else if (log_method == LOG_TXT || log_method == LOG_TRYSQL) {
  199. harmony_log_txt(sd, ip, msg);
  200. }
  201.  
  202. ShowMessage(""CL_MAGENTA"[Harmony]"CL_RESET": %s (Player: %s, IP: %s)\n", msg, sd->status.name, ip);
  203. }
  204.  
  205. static bool harmony_iterate_groups(int group_id, int level, const char* name) {
  206. harm_funcs->zone_register_group(group_id, level);
  207. return true;
  208. }
  209.  
  210. static void harmony_register_groups() {
  211. harm_funcs->zone_register_group(25311, 25311);
  212. #if HARMSW == HARMSW_RATHENA_GROUP
  213. pc_group_iterate(harmony_iterate_groups);
  214. #else
  215. harm_funcs->zone_register_group(25312, 25312);
  216. #endif
  217. }
  218.  
  219. static int harmony_group_register_timer(int tid, unsigned int tick, int id, intptr data) {
  220. if (chrif_isconnected()) {
  221. harmony_register_groups();
  222.  
  223. // We will re-send group associations every thirty seconds, otherwise the login server would never be able to recover from a restart
  224. _athena_delete_timer(tid, harmony_group_register_timer);
  225. tid_group_register = _athena_add_timer_interval(_athena_gettick()+30*1000, harmony_group_register_timer, 0, 0, 30*1000);
  226. }
  227. return 0;
  228. }
  229.  
  230. static bool harmony_iterate_groups_adminlevel(int group_id, int level, const char* name) {
  231. int account_id;
  232.  
  233. if (level < current_groupscan_minlevel)
  234. return true;
  235.  
  236. ShowInfo("Registering group %d..\n", group_id);
  237. if (SQL_SUCCESS != SqlStmt_BindParam(admin_stmt, 0, SQLDT_INT, (void*)&group_id, sizeof(group_id)) ||
  238. SQL_SUCCESS != SqlStmt_Execute(admin_stmt))
  239. {
  240. ShowError("Fetching GM accounts from group %d failed.\n", group_id);
  241. Sql_ShowDebug(mmysql_handle);
  242. return true;
  243. }
  244.  
  245. SqlStmt_BindColumn(admin_stmt, 0, SQLDT_INT, &account_id, 0, NULL, NULL);
  246. while (SQL_SUCCESS == SqlStmt_NextRow(admin_stmt)) {
  247. harm_funcs->zone_register_admin(account_id, false);
  248. }
  249. return true;
  250. }
  251.  
  252. void harmony_action_request_global(int task, int id, intptr data) {
  253. switch (task) {
  254. case HARMTASK_LOGIN_ACTION:
  255. chrif_harmony_request((uint8*)data, id);
  256. break;
  257. case HARMTASK_GET_FD:
  258. {
  259. TBL_PC *sd = BL_CAST(BL_PC, map_id2bl(id));
  260. *(int32*)data = (sd ? sd->fd : 0);
  261. }
  262. break;
  263. case HARMTASK_SET_LOG_METHOD:
  264. log_method = id;
  265. break;
  266. case HARMTASK_INIT_GROUPS:
  267. if (chrif_isconnected())
  268. harmony_register_groups();
  269. else {
  270. // Register groups as soon as the char server is available again
  271. if (tid_group_register != INVALID_TIMER)
  272. _athena_delete_timer(tid_group_register, harmony_group_register_timer);
  273. tid_group_register = _athena_add_timer_interval(_athena_gettick()+1000, harmony_group_register_timer, 0, 0, 500);
  274. }
  275. break;
  276. case HARMTASK_RESOLVE_GROUP:
  277. #if HARMSW == HARMSW_RATHENA_GROUP
  278. *(int32*)data = pc_group_id2level(id);
  279. #else
  280. *(int32*)data = id;
  281. #endif
  282. break;
  283. case HARMTASK_PACKET:
  284. clif_send((const uint8*)data, id, NULL, ALL_CLIENT);
  285. break;
  286. case HARMTASK_GET_ADMINS:
  287. {
  288. #if HARMSW == HARMSW_RATHENA_GROUP
  289. // Iterate groups and register each group individually
  290. current_groupscan_minlevel = id;
  291. pc_group_iterate(harmony_iterate_groups_adminlevel);
  292. #else
  293. //
  294. int account_id;
  295. int level = id;
  296. if (SQL_SUCCESS != SqlStmt_BindParam(admin_stmt, 0, SQLDT_INT, (void*)&level, sizeof(level)) ||
  297. SQL_SUCCESS != SqlStmt_Execute(admin_stmt))
  298. {
  299. ShowError("Fetching GM accounts failed.\n");
  300. Sql_ShowDebug(mmysql_handle);
  301. break;
  302. }
  303.  
  304. SqlStmt_BindColumn(admin_stmt, 0, SQLDT_INT, &account_id, 0, NULL, NULL);
  305. while (SQL_SUCCESS == SqlStmt_NextRow(admin_stmt)) {
  306. harm_funcs->zone_register_admin(account_id, false);
  307. }
  308. #endif
  309. break;
  310. }
  311. case HARMTASK_IS_CHAR_CONNECTED:
  312. *(int*)data = chrif_isconnected();
  313. break;
  314. default:
  315. ShowError("Harmony requested unknown action! (Global; ID=%d)\n", task);
  316. ShowError("This indicates that you are running an incompatible version.\n");
  317. break;
  318. }
  319. }
  320.  
  321. void _FASTCALL harmony_action_request(int fd, int task, int id, intptr data) {
  322. TBL_PC *sd;
  323.  
  324. if (fd == 0) {
  325. harmony_action_request_global(task, id, data);
  326. return;
  327. }
  328.  
  329. switch (task) {
  330. case HARMTASK_PACKET:
  331. memcpy(WFIFOP(fd, 0), (const void*)data, id);
  332. //ShowInfo("Sending %d bytes to session #%d (%x)\n", id, fd, WFIFOW(fd, 0));
  333. WFIFOSET(fd, id);
  334. return;
  335. }
  336.  
  337. sd = (TBL_PC *)session[fd]->session_data;
  338. if (!sd)
  339. return;
  340.  
  341. switch (task) {
  342. case HARMTASK_DC:
  343. ShowInfo("-- Harmony requested disconnect.\n");
  344. set_eof(fd);
  345. break;
  346. case HARMTASK_KICK:
  347. ShowInfo("-- Harmony requested kick.\n");
  348. if (id == 99)
  349. set_eof(fd);
  350. else
  351. clif_authfail_fd(fd, id);
  352. break;
  353. case HARMTASK_JAIL:
  354. {
  355. char msg[64];
  356. snprintf(msg, sizeof(msg)-1, "@jail %s", sd->status.name);
  357. is_atcommand(0, sd, msg, 0);
  358. }
  359. break;
  360. case HARMTASK_BAN:
  361. harmony_ban(sd->status.account_id, id);
  362. break;
  363. case HARMTASK_ATCMD:
  364. is_atcommand(fd, sd, (const char*)data, 0);
  365. break;
  366. case HARMTASK_MSG:
  367. clif_displaymessage(fd, (const char*)data);
  368. break;
  369. case HARMTASK_IS_ACTIVE:
  370. *(int32*)data = (sd->bl.prev == NULL || sd->invincible_timer != INVALID_TIMER) ? 0 : 1;
  371. break;
  372. case HARMTASK_GET_ID:
  373. switch (id) {
  374. case HARMID_AID:
  375. *(int*)data = sd->status.account_id;
  376. break;
  377. case HARMID_GID:
  378. *(int*)data = sd->status.char_id;
  379. break;
  380. case HARMID_GDID:
  381. *(int*)data = sd->status.guild_id;
  382. break;
  383. case HARMID_PID:
  384. *(int*)data = sd->status.party_id;
  385. break;
  386. case HARMID_CLASS:
  387. *(short*)data = sd->status.class_;
  388. break;
  389. case HARMID_GM:
  390. #if HARMSW == HARMSW_RATHENA_GROUP
  391. *(int*)data = pc_group_id2level(sd->group_id);
  392. #else
  393. *(int*)data = pc_isGM(sd);
  394. #endif
  395. break;
  396. default:
  397. ShowError("Harmony requested unknown ID! (ID=%d)\n", id);
  398. ShowError("This indicates that you are running an incompatible version.\n");
  399. break;
  400. }
  401. break;
  402. case HARMTASK_SCRIPT:
  403. {
  404. struct npc_data* nd = npc_name2id((const char*)data);
  405. if (nd) {
  406. run_script(nd->u.scr.script, 0, sd->bl.id, fake_nd->bl.id);
  407. } else {
  408. ShowError("A Harmony action chain tried to execute non-existing script '%s'\n", data);
  409. }
  410. }
  411. break;
  412. default:
  413. ShowError("Harmony requested unknown action! (ID=%d)\n", task);
  414. ShowError("This indicates that you are running an incompatible version.\n");
  415. break;
  416. }
  417. }
  418.  
  419. void harmony_logout(TBL_PC* sd) {
  420. harm_funcs->zone_logout(sd->fd);
  421. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement