mail.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef TXT_ONLY
  4. // Mail System for eAthena SQL
  5. // Created by Valaris
  6. // moved all strings to msg_athena.conf [Lupus]
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "../common/strlib.h"
  11. #include "../common/socket.h"
  12. #include "../common/timer.h"
  13. #include "../common/nullpo.h"
  14. #include "../common/showmsg.h"
  15. #include "map.h"
  16. #include "clif.h"
  17. #include "chrif.h"
  18. #include "intif.h"
  19. #include "atcommand.h"
  20. #include "pc.h"
  21. #include "mail.h"
  22. #ifndef TXT_ONLY
  23. #ifndef SQL_DEBUG
  24. #define mysql_query(_x, _y) mysql_real_query(_x, _y, strlen(_y))
  25. #else
  26. #define mysql_query(_x, _y) debug_mysql_query(__FILE__, __LINE__, _x, _y)
  27. #endif
  28. #endif
  29. int MAIL_CHECK_TIME = 120000;
  30. int mail_timer;
  31. //extern char *msg_table[1000]; // Server messages (0-499 reserved for GM commands, 500-999 reserved for others)
  32. int mail_check(struct map_session_data *sd,int type)
  33. {
  34. int i = 0, new_ = 0, priority = 0;
  35. char message[50];
  36. nullpo_retr (0, sd);
  37. sprintf(tmp_sql,"SELECT `message_id`,`to_account_id`,`from_char_name`,`read_flag`,`priority`,`check_flag` "
  38. "FROM `%s` WHERE `to_account_id` = \"%d\" ORDER by `message_id`", mail_db, sd->status.account_id);
  39. if (mysql_query(&mail_handle, tmp_sql)) {
  40. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  41. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  42. return 0;
  43. }
  44. mail_res = mysql_store_result(&mail_handle);
  45. if(mail_res) {
  46. if (mysql_num_rows(mail_res) == 0) {
  47. //clif_displaymessage(sd->fd,"You have no messages.");
  48. clif_displaymessage(sd->fd, msg_txt(516));
  49. mysql_free_result(mail_res);
  50. return 0;
  51. }
  52. while ((mail_row = mysql_fetch_row(mail_res))) {
  53. i++;
  54. if(!atoi(mail_row[5])) {
  55. sprintf(tmp_sql,"UPDATE `%s` SET `check_flag`='1' WHERE `message_id`= \"%d\"", mail_db, atoi(mail_row[0]));
  56. if(mysql_query(&mail_handle, tmp_sql) ) {
  57. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  58. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  59. }
  60. }
  61. if(!atoi(mail_row[3])) {
  62. new_++;
  63. if(atoi(mail_row[4]))
  64. priority++;
  65. if(type==2 || type==3) {
  66. if(atoi(mail_row[4])) {
  67. //sprintf(message, "%d - From : %s (New - Priority)", i, mail_row[2]);
  68. sprintf(message, msg_txt(511), i, mail_row[2]);
  69. clif_displaymessage(sd->fd, jstrescape(message));
  70. } else {
  71. //sprintf(message, "%d - From : %s (New)", i, mail_row[2]);
  72. sprintf(message, msg_txt(512), i, mail_row[2]);
  73. clif_displaymessage(sd->fd, jstrescape(message));
  74. }
  75. }
  76. } else if(type==2){
  77. //sprintf(message, "%d - From : %s", i, mail_row[2]);
  78. sprintf(message, msg_txt(513), i, mail_row[2]);
  79. clif_displaymessage(sd->fd, jstrescape(message));
  80. }
  81. }
  82. mysql_free_result(mail_res);
  83. } else {
  84. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  85. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  86. return 0;
  87. }
  88. if(i>0 && new_>0 && type==1) {
  89. //sprintf(message, "You have %d new messages.", new_);
  90. sprintf(message, msg_txt(514), new_);
  91. clif_displaymessage(sd->fd, jstrescape(message));
  92. }
  93. if(i>0 && new_>0 && priority>0 && type==1) {
  94. //sprintf(message, "You have %d unread priority messages.", priority);
  95. sprintf(message, msg_txt(515), priority);
  96. clif_displaymessage(sd->fd, jstrescape(message));
  97. }
  98. if(!new_) {
  99. //clif_displaymessage(sd->fd, "You have no new messages.");
  100. clif_displaymessage(sd->fd, msg_txt(516));
  101. }
  102. return 0;
  103. }
  104. int mail_read(struct map_session_data *sd, int message_id)
  105. {
  106. char message[80];
  107. nullpo_retr (0, sd);
  108. sprintf(tmp_sql,"SELECT `message_id`,`to_account_id`,`from_char_name`,`message`,`read_flag`,`priority`,`check_flag` from `%s` WHERE `to_account_id` = \"%d\" ORDER by `message_id` LIMIT %d, 1",mail_db,sd->status.account_id,message_id-1);
  109. if (mysql_query(&mail_handle, tmp_sql)) {
  110. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  111. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  112. return 0;
  113. }
  114. mail_res = mysql_store_result(&mail_handle);
  115. if(mail_res) {
  116. if (mysql_num_rows(mail_res) == 0) {
  117. mysql_free_result(mail_res);
  118. //clif_displaymessage(sd->fd, "Message not found.");
  119. clif_displaymessage(sd->fd, msg_txt(517));
  120. return 0;
  121. }
  122. if ((mail_row = mysql_fetch_row(mail_res))) {
  123. if(!atoi(mail_row[6])) {
  124. sprintf(tmp_sql,"UPDATE `%s` SET `check_flag`='1' WHERE `message_id`= \"%d\"", mail_db, atoi(mail_row[0]));
  125. if(mysql_query(&mail_handle, tmp_sql) ) {
  126. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  127. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  128. }
  129. }
  130. //sprintf(message, "Reading message from %s", mail_row[2]);
  131. sprintf(message, msg_txt(518), mail_row[2]);
  132. clif_displaymessage(sd->fd, jstrescape(message));
  133. sprintf(message, "%s", mail_row[3]);
  134. clif_displaymessage(sd->fd, jstrescape(message));
  135. sprintf(tmp_sql,"UPDATE `%s` SET `read_flag`='1' WHERE `message_id`= \"%d\"", mail_db, atoi(mail_row[0]));
  136. if(mysql_query(&mail_handle, tmp_sql) ) {
  137. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  138. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  139. }
  140. }
  141. mysql_free_result(mail_res);
  142. } else {
  143. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  144. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  145. }
  146. return 0;
  147. }
  148. int mail_delete(struct map_session_data *sd, int message_id)
  149. {
  150. nullpo_retr (0, sd);
  151. sprintf(tmp_sql,"SELECT `message_id`,`to_account_id`,`read_flag`,`priority`,`check_flag` from `%s` WHERE `to_account_id` = \"%d\" ORDER by `message_id` LIMIT %d, 1",mail_db,sd->status.account_id,message_id-1);
  152. if (mysql_query(&mail_handle, tmp_sql)) {
  153. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  154. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  155. return 0;
  156. }
  157. mail_res = mysql_store_result(&mail_handle);
  158. if(mail_res) {
  159. if (mysql_num_rows(mail_res) == 0) {
  160. mysql_free_result(mail_res);
  161. //clif_displaymessage(sd->fd, "Message not found.");
  162. clif_displaymessage(sd->fd, msg_txt(517));
  163. return 0;
  164. }
  165. if ((mail_row = mysql_fetch_row(mail_res))) {
  166. if(!atoi(mail_row[2]) && atoi(mail_row[3])) {
  167. mysql_free_result(mail_res);
  168. //clif_displaymessage(sd->fd,"Cannot delete unread priority mail.");
  169. clif_displaymessage(sd->fd,msg_txt(519));
  170. return 0;
  171. }
  172. if(!atoi(mail_row[4])) {
  173. mysql_free_result(mail_res);
  174. //clif_displaymessage(sd->fd,"You have recieved new mail, use @listmail before deleting.");
  175. clif_displaymessage(sd->fd,msg_txt(520));
  176. return 0;
  177. }
  178. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `message_id` = \"%d\"", mail_db, atoi(mail_row[0]));
  179. if(mysql_query(&mail_handle, tmp_sql) ) {
  180. mysql_free_result(mail_res);
  181. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  182. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  183. return 0;
  184. }
  185. //else clif_displaymessage(sd->fd,"Message deleted.");
  186. else clif_displaymessage(sd->fd,msg_txt(521));
  187. }
  188. mysql_free_result(mail_res);
  189. } else {
  190. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  191. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  192. }
  193. return 0;
  194. }
  195. int mail_send(struct map_session_data *sd, char *name, char *message, int flag)
  196. {
  197. nullpo_retr (0, sd);
  198. if(pc_isGM(sd) < 80 && sd->mail_counter > 0) {
  199. //clif_displaymessage(sd->fd,"You must wait 10 minutes before sending another message");
  200. clif_displaymessage(sd->fd,msg_txt(522));
  201. return 0;
  202. }
  203. if(strcmp(name,"*")==0) {
  204. if(pc_isGM(sd) < 80) {
  205. //clif_displaymessage(sd->fd, "Access Denied.");
  206. clif_displaymessage(sd->fd, msg_txt(523));
  207. return 0;
  208. }
  209. else
  210. sprintf(tmp_sql,"SELECT DISTINCT `account_id` FROM `%s` WHERE `account_id` <> '%d' ORDER BY `account_id`", char_db, sd->status.account_id);
  211. }
  212. else
  213. sprintf(tmp_sql,"SELECT `account_id`,`name` FROM `%s` WHERE `name` = \"%s\"", char_db, jstrescape(name));
  214. if (mysql_query(&mail_handle, tmp_sql)) {
  215. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  216. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  217. return 0;
  218. }
  219. mail_res = mysql_store_result(&mail_handle);
  220. if(mail_res) {
  221. if (mysql_num_rows(mail_res) == 0) {
  222. mysql_free_result(mail_res);
  223. //clif_displaymessage(sd->fd,"Character does not exist.");
  224. clif_displaymessage(sd->fd,msg_txt(524));
  225. return 0;
  226. }
  227. while ((mail_row = mysql_fetch_row(mail_res))) {
  228. if(strcmp(name,"*")==0) {
  229. sprintf(tmp_sql, "INSERT DELAYED INTO `%s` (`to_account_id`,`from_account_id`,`from_char_name`,`message`,`priority`)"
  230. " VALUES ('%d', '%d', '%s', '%s', '%d')",mail_db, atoi(mail_row[0]), sd->status.account_id, sd->status.name, jstrescape(message), flag);
  231. }
  232. else {
  233. sprintf(tmp_sql, "INSERT DELAYED INTO `%s` (`to_account_id`,`to_char_name`,`from_account_id`,`from_char_name`,`message`,`priority`)"
  234. " VALUES ('%d', '%s', '%d', '%s', '%s', '%d')",mail_db, atoi(mail_row[0]), mail_row[1], sd->status.account_id, sd->status.name, jstrescape(message), flag);
  235. if(pc_isGM(sd) < 80)
  236. sd->mail_counter=5;
  237. }
  238. if(mysql_query(&mail_handle, tmp_sql) ) {
  239. mysql_free_result(mail_res);
  240. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  241. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  242. return 0;
  243. }
  244. }
  245. }
  246. //clif_displaymessage(sd->fd,"Mail has been sent.");
  247. clif_displaymessage(sd->fd,msg_txt(525));
  248. return 0;
  249. }
  250. static int mail_check_timer_sub(struct map_session_data *sd, va_list va)
  251. {
  252. int id = va_arg(va, int);
  253. if(pc_isGM(sd) < 80 && sd->mail_counter > 0)
  254. sd->mail_counter--;
  255. if(sd->status.account_id==id)
  256. clif_displaymessage(sd->fd, msg_txt(526)); //you got new email.
  257. return 0;
  258. }
  259. int mail_check_timer(int tid,unsigned int tick,int id,int data)
  260. {
  261. if(mail_timer != tid)
  262. return 0;
  263. sprintf(tmp_sql,"SELECT DISTINCT `to_account_id` FROM `%s` WHERE `read_flag` = '0' AND `check_flag` = '0'", mail_db);
  264. if (mysql_query(&mail_handle, tmp_sql)) {
  265. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  266. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  267. mail_timer=add_timer(gettick()+MAIL_CHECK_TIME,mail_check_timer,0,0);
  268. return 0;
  269. }
  270. mail_res = mysql_store_result(&mail_handle);
  271. if (mail_res) {
  272. if (mysql_num_rows(mail_res) == 0) {
  273. mysql_free_result(mail_res);
  274. mail_timer=add_timer(gettick()+MAIL_CHECK_TIME,mail_check_timer,0,0);
  275. return 0;
  276. }
  277. while ((mail_row = mysql_fetch_row(mail_res)))
  278. clif_foreachclient(mail_check_timer_sub, atoi(mail_row[0]));
  279. }
  280. sprintf(tmp_sql,"UPDATE `%s` SET `check_flag`='1' WHERE `check_flag`= '0' ", mail_db);
  281. if(mysql_query(&mail_handle, tmp_sql) ) {
  282. ShowSQL("DB error - %s\n",mysql_error(&mail_handle));
  283. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  284. }
  285. mail_timer=add_timer(gettick()+MAIL_CHECK_TIME,mail_check_timer,0,0);
  286. return 0;
  287. }
  288. int do_init_mail(void)
  289. {
  290. add_timer_func_list(mail_check_timer,"mail_check_timer");
  291. mail_timer=add_timer(gettick()+MAIL_CHECK_TIME,mail_check_timer,0,0);
  292. return 0;
  293. }
  294. #endif