mail.c 10 KB

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