int_mail.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "int_mail.hpp"
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <memory>
  7. #include <common/mmo.hpp>
  8. #include <common/showmsg.hpp>
  9. #include <common/socket.hpp>
  10. #include <common/sql.hpp>
  11. #include <common/strlib.hpp>
  12. #include <common/utilities.hpp>
  13. #include "char.hpp"
  14. #include "char_mapif.hpp"
  15. #include "inter.hpp"
  16. using namespace rathena;
  17. bool mail_loadmessage(int32 mail_id, struct mail_message* msg);
  18. void mapif_Mail_return( int32 fd, uint32 char_id, int32 mail_id, uint32 account_id_receiver = 0, uint32 account_id_sender = 0 );
  19. bool mapif_Mail_delete( int32 fd, uint32 char_id, int32 mail_id, uint32 account_id = 0 );
  20. int32 mail_fromsql(uint32 char_id, struct mail_data* md)
  21. {
  22. int32 i;
  23. char *data;
  24. memset(md, 0, sizeof(struct mail_data));
  25. md->amount = 0;
  26. md->full = false;
  27. // First we prefill the msg ids
  28. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id` FROM `%s` WHERE `dest_id`='%d' AND `status` < 3 ORDER BY `id` LIMIT %d", schema_config.mail_db, char_id, MAIL_MAX_INBOX + 1) ){
  29. Sql_ShowDebug(sql_handle);
  30. return 0;
  31. }
  32. md->full = (Sql_NumRows(sql_handle) > MAIL_MAX_INBOX);
  33. for( i = 0; i < MAIL_MAX_INBOX && SQL_SUCCESS == Sql_NextRow(sql_handle); i++ ){
  34. Sql_GetData(sql_handle, 0, &data, nullptr); md->msg[i].id = atoi(data);
  35. }
  36. md->amount = i;
  37. Sql_FreeResult(sql_handle);
  38. // Now we load them
  39. for( i = 0; i < md->amount; i++ ){
  40. if( !mail_loadmessage( md->msg[i].id, &md->msg[i] ) ){
  41. return 0;
  42. }
  43. }
  44. md->unchecked = 0;
  45. md->unread = 0;
  46. for (i = 0; i < md->amount; i++)
  47. {
  48. struct mail_message *msg = &md->msg[i];
  49. if( msg->status == MAIL_NEW )
  50. {
  51. if ( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `status` = '%d' WHERE `id` = '%d'", schema_config.mail_db, MAIL_UNREAD, msg->id) )
  52. Sql_ShowDebug(sql_handle);
  53. msg->status = MAIL_UNREAD;
  54. md->unchecked++;
  55. }
  56. else if ( msg->status == MAIL_UNREAD )
  57. md->unread++;
  58. }
  59. ShowInfo("mail load complete from DB - id: %d (total: %d)\n", char_id, md->amount);
  60. return 1;
  61. }
  62. /// Stores a single message in the database.
  63. /// Returns the message's ID if successful (or 0 if it fails).
  64. int32 mail_savemessage(struct mail_message* msg)
  65. {
  66. StringBuf buf;
  67. SqlStmt* stmt;
  68. int32 i, j;
  69. bool found = false;
  70. if( SQL_ERROR == Sql_QueryStr( sql_handle, "START TRANSACTION" ) ){
  71. Sql_ShowDebug( sql_handle );
  72. return 0;
  73. }
  74. // build message save query
  75. StringBuf_Init(&buf);
  76. StringBuf_Printf(&buf, "INSERT INTO `%s` (`send_name`, `send_id`, `dest_name`, `dest_id`, `title`, `message`, `time`, `status`, `zeny`,`type`", schema_config.mail_db);
  77. StringBuf_Printf(&buf, ") VALUES (?, '%d', ?, '%d', ?, ?, '%lu', '%d', '%d', '%d'", msg->send_id, msg->dest_id, (unsigned long)msg->timestamp, msg->status, msg->zeny, msg->type);
  78. StringBuf_AppendStr(&buf, ")");
  79. // prepare and execute query
  80. stmt = SqlStmt_Malloc(sql_handle);
  81. if( SQL_SUCCESS != SqlStmt_PrepareStr(stmt, StringBuf_Value(&buf))
  82. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 0, SQLDT_STRING, msg->send_name, strnlen(msg->send_name, NAME_LENGTH))
  83. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 1, SQLDT_STRING, msg->dest_name, strnlen(msg->dest_name, NAME_LENGTH))
  84. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 2, SQLDT_STRING, msg->title, strnlen(msg->title, MAIL_TITLE_LENGTH))
  85. || SQL_SUCCESS != SqlStmt_BindParam(stmt, 3, SQLDT_STRING, msg->body, strnlen(msg->body, MAIL_BODY_LENGTH))
  86. || SQL_SUCCESS != SqlStmt_Execute(stmt) )
  87. {
  88. SqlStmt_ShowDebug(stmt);
  89. StringBuf_Destroy(&buf);
  90. Sql_QueryStr( sql_handle, "ROLLBACK" );
  91. return msg->id = 0;
  92. } else
  93. msg->id = (int)SqlStmt_LastInsertId(stmt);
  94. SqlStmt_Free(stmt);
  95. StringBuf_Clear(&buf);
  96. StringBuf_Printf(&buf,"INSERT INTO `%s` (`id`, `index`, `amount`, `nameid`, `refine`, `attribute`, `identify`, `unique_id`, `bound`, `enchantgrade`", schema_config.mail_attachment_db);
  97. for (j = 0; j < MAX_SLOTS; j++)
  98. StringBuf_Printf(&buf, ", `card%d`", j);
  99. for (j = 0; j < MAX_ITEM_RDM_OPT; ++j) {
  100. StringBuf_Printf(&buf, ", `option_id%d`", j);
  101. StringBuf_Printf(&buf, ", `option_val%d`", j);
  102. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  103. }
  104. StringBuf_AppendStr(&buf, ") VALUES ");
  105. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  106. // skip empty and already matched entries
  107. if( msg->item[i].nameid == 0 )
  108. continue;
  109. if( found ){
  110. StringBuf_AppendStr(&buf, ",");
  111. }else{
  112. found = true;
  113. }
  114. StringBuf_Printf(&buf, "('%" PRIu64 "', '%hu', '%d', '%u', '%d', '%d', '%d', '%" PRIu64 "', '%d', '%d'", (uint64)msg->id, i, msg->item[i].amount, msg->item[i].nameid, msg->item[i].refine, msg->item[i].attribute, msg->item[i].identify, msg->item[i].unique_id, msg->item[i].bound, msg->item[i].enchantgrade);
  115. for (j = 0; j < MAX_SLOTS; j++)
  116. StringBuf_Printf(&buf, ", '%u'", msg->item[i].card[j]);
  117. for (j = 0; j < MAX_ITEM_RDM_OPT; ++j) {
  118. StringBuf_Printf(&buf, ", '%d'", msg->item[i].option[j].id);
  119. StringBuf_Printf(&buf, ", '%d'", msg->item[i].option[j].value);
  120. StringBuf_Printf(&buf, ", '%d'", msg->item[i].option[j].param);
  121. }
  122. StringBuf_AppendStr(&buf, ")");
  123. }
  124. if( found && SQL_ERROR == Sql_QueryStr(sql_handle, StringBuf_Value(&buf)) ){
  125. Sql_ShowDebug(sql_handle);
  126. msg->id = 0;
  127. Sql_QueryStr( sql_handle, "ROLLBACK" );
  128. }
  129. StringBuf_Destroy(&buf);
  130. if( msg->id && SQL_ERROR == Sql_QueryStr( sql_handle, "COMMIT" ) ){
  131. Sql_ShowDebug( sql_handle );
  132. return 0;
  133. }
  134. return msg->id;
  135. }
  136. /// Retrieves a single message from the database.
  137. /// Returns true if the operation succeeds (or false if it fails).
  138. bool mail_loadmessage(int32 mail_id, struct mail_message* msg)
  139. {
  140. int32 i, j;
  141. StringBuf buf;
  142. char* data;
  143. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `id`,`send_name`,`send_id`,`dest_name`,`dest_id`,`title`,`message`,`time`,`status`,`zeny`,`type` FROM `%s` WHERE `id` = '%d'", schema_config.mail_db, mail_id )
  144. || SQL_SUCCESS != Sql_NextRow(sql_handle) ){
  145. Sql_ShowDebug(sql_handle);
  146. Sql_FreeResult(sql_handle);
  147. return false;
  148. }else{
  149. Sql_GetData(sql_handle, 0, &data, nullptr); msg->id = atoi(data);
  150. Sql_GetData(sql_handle, 1, &data, nullptr); safestrncpy(msg->send_name, data, NAME_LENGTH);
  151. Sql_GetData(sql_handle, 2, &data, nullptr); msg->send_id = atoi(data);
  152. Sql_GetData(sql_handle, 3, &data, nullptr); safestrncpy(msg->dest_name, data, NAME_LENGTH);
  153. Sql_GetData(sql_handle, 4, &data, nullptr); msg->dest_id = atoi(data);
  154. Sql_GetData(sql_handle, 5, &data, nullptr); safestrncpy(msg->title, data, MAIL_TITLE_LENGTH);
  155. Sql_GetData(sql_handle, 6, &data, nullptr); safestrncpy(msg->body, data, MAIL_BODY_LENGTH);
  156. Sql_GetData(sql_handle, 7, &data, nullptr); msg->timestamp = atoi(data);
  157. Sql_GetData(sql_handle, 8, &data, nullptr); msg->status = (mail_status)atoi(data);
  158. Sql_GetData(sql_handle, 9, &data, nullptr); msg->zeny = atoi(data);
  159. Sql_GetData(sql_handle,10, &data, nullptr); msg->type = (mail_inbox_type)atoi(data);
  160. if( msg->type == MAIL_INBOX_NORMAL && charserv_config.mail_return_days > 0 ){
  161. msg->scheduled_deletion = msg->timestamp + charserv_config.mail_return_days * 24 * 60 * 60;
  162. }else if( msg->type == MAIL_INBOX_RETURNED && charserv_config.mail_delete_days > 0 ){
  163. msg->scheduled_deletion = msg->timestamp + charserv_config.mail_delete_days * 24 * 60 * 60;
  164. }else{
  165. msg->scheduled_deletion = 0;
  166. }
  167. Sql_FreeResult(sql_handle);
  168. }
  169. StringBuf_Init(&buf);
  170. StringBuf_AppendStr(&buf, "SELECT `amount`,`nameid`,`refine`,`attribute`,`identify`,`unique_id`,`bound`,`enchantgrade`");
  171. for (j = 0; j < MAX_SLOTS; j++)
  172. StringBuf_Printf(&buf, ",`card%d`", j);
  173. for (j = 0; j < MAX_ITEM_RDM_OPT; ++j) {
  174. StringBuf_Printf(&buf, ", `option_id%d`", j);
  175. StringBuf_Printf(&buf, ", `option_val%d`", j);
  176. StringBuf_Printf(&buf, ", `option_parm%d`", j);
  177. }
  178. StringBuf_Printf(&buf, " FROM `%s`", schema_config.mail_attachment_db);
  179. StringBuf_Printf(&buf, " WHERE `id` = '%d'", mail_id);
  180. StringBuf_AppendStr(&buf, " ORDER BY `index` ASC");
  181. StringBuf_Printf(&buf, " LIMIT %d", MAIL_MAX_ITEM);
  182. if( SQL_ERROR == Sql_Query(sql_handle, StringBuf_Value(&buf)) ){
  183. Sql_ShowDebug(sql_handle);
  184. Sql_FreeResult(sql_handle);
  185. StringBuf_Destroy(&buf);
  186. return false;
  187. }
  188. memset(msg->item, 0, sizeof(struct item) * MAIL_MAX_ITEM);
  189. for( i = 0; i < MAIL_MAX_ITEM && SQL_SUCCESS == Sql_NextRow(sql_handle); i++ ){
  190. Sql_GetData(sql_handle,0, &data, nullptr); msg->item[i].amount = (short)atoi(data);
  191. Sql_GetData(sql_handle,1, &data, nullptr); msg->item[i].nameid = strtoul(data, nullptr, 10);
  192. Sql_GetData(sql_handle,2, &data, nullptr); msg->item[i].refine = atoi(data);
  193. Sql_GetData(sql_handle,3, &data, nullptr); msg->item[i].attribute = atoi(data);
  194. Sql_GetData(sql_handle,4, &data, nullptr); msg->item[i].identify = atoi(data);
  195. Sql_GetData(sql_handle,5, &data, nullptr); msg->item[i].unique_id = strtoull(data, nullptr, 10);
  196. Sql_GetData(sql_handle,6, &data, nullptr); msg->item[i].bound = atoi(data);
  197. Sql_GetData(sql_handle,7, &data, nullptr); msg->item[i].enchantgrade = atoi(data);
  198. msg->item[i].expire_time = 0;
  199. for( j = 0; j < MAX_SLOTS; j++ ){
  200. Sql_GetData(sql_handle,8 + j, &data, nullptr); msg->item[i].card[j] = strtoul(data, nullptr, 10);
  201. }
  202. for( j = 0; j < MAX_ITEM_RDM_OPT; j++ ){
  203. Sql_GetData(sql_handle, 8 + MAX_SLOTS + j * 3, &data, nullptr); msg->item[i].option[j].id = atoi(data);
  204. Sql_GetData(sql_handle, 9 + MAX_SLOTS + j * 3, &data, nullptr); msg->item[i].option[j].value = atoi(data);
  205. Sql_GetData(sql_handle,10 + MAX_SLOTS + j * 3, &data, nullptr); msg->item[i].option[j].param = atoi(data);
  206. }
  207. }
  208. StringBuf_Destroy(&buf);
  209. Sql_FreeResult(sql_handle);
  210. return true;
  211. }
  212. int32 mail_timer_sub( int32 limit, enum mail_inbox_type type ){
  213. // Start by deleting all expired mails sent by the server
  214. if( SQL_ERROR == Sql_Query( sql_handle, "DELETE FROM `%s`WHERE `type` = '%d' AND `send_id` = '0' AND `time` <= UNIX_TIMESTAMP( NOW() - INTERVAL %d DAY )", schema_config.mail_db, type, limit ) ){
  215. Sql_ShowDebug( sql_handle );
  216. return 0;
  217. }
  218. struct{
  219. int32 mail_id;
  220. int32 char_id;
  221. int32 account_id;
  222. int32 account_id_sender;
  223. }mails[MAIL_ITERATION_SIZE];
  224. if( limit <= 0 ){
  225. return 0;
  226. }
  227. memset(mails, 0, sizeof(mails));
  228. if( SQL_ERROR == Sql_Query( sql_handle,
  229. "SELECT `m`.`id`, `c`.`char_id`, `c`.`account_id`, `c2`.`account_id` "
  230. "FROM `%s` `m` "
  231. "INNER JOIN `%s` `c` ON `c`.`char_id`=`m`.`dest_id` "
  232. "INNER JOIN `%s` `c2` ON `c2`.`char_id`=`m`.`send_id` "
  233. "WHERE `type` = '%d' AND `time` <= UNIX_TIMESTAMP( NOW() - INTERVAL %d DAY ) "
  234. "ORDER BY `id` LIMIT %d",
  235. schema_config.mail_db, schema_config.char_db, schema_config.char_db, type, limit, MAIL_ITERATION_SIZE + 1 ) ){
  236. Sql_ShowDebug(sql_handle);
  237. return 0;
  238. }
  239. if( Sql_NumRows(sql_handle) <= 0 ){
  240. return 0;
  241. }
  242. for( int32 i = 0; i < MAIL_ITERATION_SIZE && SQL_SUCCESS == Sql_NextRow( sql_handle ); i++ ){
  243. char* data;
  244. Sql_GetData(sql_handle, 0, &data, nullptr); mails[i].mail_id = atoi(data);
  245. Sql_GetData(sql_handle, 1, &data, nullptr); mails[i].char_id = atoi(data);
  246. Sql_GetData(sql_handle, 2, &data, nullptr); mails[i].account_id = atoi(data);
  247. Sql_GetData( sql_handle, 3, &data, nullptr ); mails[i].account_id_sender = atoi( data );
  248. }
  249. Sql_FreeResult(sql_handle);
  250. for( int32 i = 0; i < MAIL_ITERATION_SIZE; i++ ){
  251. if( mails[i].mail_id == 0 ){
  252. break;
  253. }
  254. if( type == MAIL_INBOX_NORMAL ){
  255. mapif_Mail_return( 0, mails[i].char_id, mails[i].mail_id, mails[i].account_id, mails[i].account_id_sender );
  256. }else if( type == MAIL_INBOX_RETURNED ){
  257. mapif_Mail_delete( 0, mails[i].char_id, mails[i].mail_id, mails[i].account_id );
  258. }else{
  259. // Should not happen
  260. continue;
  261. }
  262. }
  263. return 0;
  264. }
  265. TIMER_FUNC(mail_return_timer){
  266. return mail_timer_sub( charserv_config.mail_return_days, MAIL_INBOX_NORMAL );
  267. }
  268. TIMER_FUNC(mail_delete_timer){
  269. return mail_timer_sub( charserv_config.mail_delete_days, MAIL_INBOX_RETURNED );
  270. }
  271. /*==========================================
  272. * Client Inbox Request
  273. *------------------------------------------*/
  274. void mapif_Mail_sendinbox(int32 fd, uint32 char_id, unsigned char flag, enum mail_inbox_type type)
  275. {
  276. struct mail_data md;
  277. mail_fromsql(char_id, &md);
  278. //FIXME: dumping the whole structure like this is unsafe [ultramage]
  279. WFIFOHEAD(fd, sizeof(md) + 10);
  280. WFIFOW(fd,0) = 0x3848;
  281. WFIFOW(fd,2) = sizeof(md) + 10;
  282. WFIFOL(fd,4) = char_id;
  283. WFIFOB(fd,8) = flag;
  284. WFIFOB(fd,9) = type;
  285. memcpy(WFIFOP(fd,10),&md,sizeof(md));
  286. WFIFOSET(fd,WFIFOW(fd,2));
  287. }
  288. void mapif_parse_Mail_requestinbox(int32 fd)
  289. {
  290. mapif_Mail_sendinbox(fd, RFIFOL(fd,2), RFIFOB(fd,6), (mail_inbox_type)RFIFOB(fd,7));
  291. }
  292. /*==========================================
  293. * Mark mail as 'Read'
  294. *------------------------------------------*/
  295. void mapif_parse_Mail_read(int32 fd)
  296. {
  297. int32 mail_id = RFIFOL(fd,2);
  298. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `status` = '%d' WHERE `id` = '%d'", schema_config.mail_db, MAIL_READ, mail_id) )
  299. Sql_ShowDebug(sql_handle);
  300. }
  301. /*==========================================
  302. * Client Attachment Request
  303. *------------------------------------------*/
  304. bool mail_DeleteAttach(int32 mail_id){
  305. if( SQL_ERROR == Sql_Query(sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", schema_config.mail_attachment_db, mail_id ) ){
  306. Sql_ShowDebug(sql_handle);
  307. return false;
  308. }
  309. return true;
  310. }
  311. void mapif_Mail_getattach(int32 fd, uint32 char_id, int32 mail_id, int32 type)
  312. {
  313. struct mail_message msg;
  314. if( ( type&MAIL_ATT_ALL ) == 0 ){
  315. return;
  316. }
  317. if( !mail_loadmessage(mail_id, &msg) )
  318. return;
  319. if( msg.dest_id != char_id )
  320. return;
  321. if( charserv_config.mail_retrieve == 0 && msg.status != MAIL_READ )
  322. return;
  323. if( type & MAIL_ATT_ZENY ){
  324. if( msg.zeny > 0 ){
  325. if( SQL_ERROR == Sql_Query(sql_handle, "UPDATE `%s` SET `zeny` = 0 WHERE `id` = '%d'", schema_config.mail_db, mail_id ) ){
  326. Sql_ShowDebug(sql_handle);
  327. return;
  328. }
  329. }else{
  330. type &= ~MAIL_ATT_ZENY;
  331. }
  332. }
  333. if( type & MAIL_ATT_ITEM ){
  334. int32 i;
  335. ARR_FIND(0, MAIL_MAX_ITEM, i, msg.item[i].nameid > 0 && msg.item[i].amount > 0);
  336. // No item was found
  337. if( i == MAIL_MAX_ITEM ){
  338. type &= ~MAIL_ATT_ITEM;
  339. }else{
  340. if( !mail_DeleteAttach(mail_id) ){
  341. return;
  342. }
  343. }
  344. }
  345. if( type == MAIL_ATT_NONE )
  346. return; // No Attachment
  347. WFIFOHEAD(fd, sizeof(struct item)*MAIL_MAX_ITEM + 16);
  348. WFIFOW(fd,0) = 0x384a;
  349. WFIFOW(fd,2) = sizeof(struct item)*MAIL_MAX_ITEM + 16;
  350. WFIFOL(fd,4) = char_id;
  351. WFIFOL(fd,8) = mail_id;
  352. if( type & MAIL_ATT_ZENY ){
  353. WFIFOL(fd,12) = msg.zeny;
  354. }else{
  355. WFIFOL(fd, 12) = 0;
  356. }
  357. if( type & MAIL_ATT_ITEM ){
  358. memcpy(WFIFOP(fd, 16), &msg.item, sizeof(struct item)*MAIL_MAX_ITEM);
  359. }else{
  360. memset(WFIFOP(fd, 16), 0, sizeof(struct item)*MAIL_MAX_ITEM);
  361. }
  362. WFIFOSET(fd,WFIFOW(fd,2));
  363. }
  364. void mapif_parse_Mail_getattach(int32 fd)
  365. {
  366. mapif_Mail_getattach(fd, RFIFOL(fd,2), RFIFOL(fd,6),RFIFOB(fd,10));
  367. }
  368. /*==========================================
  369. * Delete Mail
  370. *------------------------------------------*/
  371. bool mapif_Mail_delete( int32 fd, uint32 char_id, int32 mail_id, uint32 account_id ){
  372. bool failed = false;
  373. if( SQL_ERROR == Sql_QueryStr( sql_handle, "START TRANSACTION" ) ||
  374. SQL_ERROR == Sql_Query( sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", schema_config.mail_db, mail_id ) ||
  375. SQL_ERROR == Sql_Query( sql_handle, "DELETE FROM `%s` WHERE `id` = '%d'", schema_config.mail_attachment_db, mail_id ) ||
  376. SQL_ERROR == Sql_QueryStr( sql_handle, "COMMIT" ) ){
  377. Sql_ShowDebug( sql_handle );
  378. Sql_QueryStr( sql_handle, "ROLLBACK" );
  379. // We do not want to trigger failure messages, if the map server did not send a request
  380. if( fd <= 0 ){
  381. return false;
  382. }
  383. failed = true;
  384. }
  385. // If the char server triggered this, check if we have to notify a map server
  386. if( fd <= 0 ){
  387. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), account_id );
  388. // Check for online players
  389. if( character != nullptr && character->server >= 0 ){
  390. fd = map_server[character->server].fd;
  391. }else{
  392. // The request was triggered inside the character server or the player is offline now
  393. return !failed;
  394. }
  395. }
  396. WFIFOHEAD(fd,11);
  397. WFIFOW(fd,0) = 0x384b;
  398. WFIFOL(fd,2) = char_id;
  399. WFIFOL(fd,6) = mail_id;
  400. WFIFOB(fd,10) = failed;
  401. WFIFOSET(fd,11);
  402. return !failed;
  403. }
  404. void mapif_parse_Mail_delete(int32 fd)
  405. {
  406. mapif_Mail_delete( fd, RFIFOL( fd, 2 ), RFIFOL( fd, 6 ) );
  407. }
  408. /*==========================================
  409. * Report New Mail to Map Server
  410. *------------------------------------------*/
  411. void mapif_Mail_new(struct mail_message *msg)
  412. {
  413. unsigned char buf[75];
  414. if( !msg || !msg->id )
  415. return;
  416. WBUFW(buf,0) = 0x3849;
  417. WBUFL(buf,2) = msg->dest_id;
  418. WBUFL(buf,6) = msg->id;
  419. memcpy(WBUFP(buf,10), msg->send_name, NAME_LENGTH);
  420. memcpy(WBUFP(buf,34), msg->title, MAIL_TITLE_LENGTH);
  421. WBUFB(buf,74) = msg->type;
  422. chmapif_sendall(buf, 75);
  423. }
  424. /*==========================================
  425. * Return Mail
  426. *------------------------------------------*/
  427. void mapif_Mail_return( int32 fd, uint32 char_id, int32 mail_id, uint32 account_id_receiver, uint32 account_id_sender ){
  428. struct mail_message msg;
  429. if( !mail_loadmessage( mail_id, &msg ) ){
  430. return;
  431. }
  432. if( msg.dest_id != char_id ){
  433. return;
  434. }
  435. if( !mapif_Mail_delete( 0, char_id, mail_id, account_id_receiver ) ){
  436. // Stop processing to not duplicate the mail
  437. return;
  438. }
  439. // If it was sent by the server we do not want to return the mail
  440. if( msg.send_id == 0 ){
  441. return;
  442. }
  443. // If we do not want to return mails without any attachments and the request was not sent by a user
  444. if( fd <= 0 && !charserv_config.mail_return_empty ){
  445. int32 i;
  446. ARR_FIND( 0, MAIL_MAX_ITEM, i, msg.item[i].nameid > 0 );
  447. if( i == MAIL_MAX_ITEM && msg.zeny == 0 ){
  448. return;
  449. }
  450. }
  451. char temp_[MAIL_TITLE_LENGTH + 3];
  452. // swap sender and receiver
  453. std::swap( msg.send_id, msg.dest_id );
  454. safestrncpy( temp_, msg.send_name, NAME_LENGTH );
  455. safestrncpy( msg.send_name, msg.dest_name, NAME_LENGTH );
  456. safestrncpy( msg.dest_name, temp_, NAME_LENGTH );
  457. // set reply message title
  458. snprintf( temp_, sizeof( temp_ ), "RE:%s", msg.title );
  459. safestrncpy( msg.title, temp_, sizeof( temp_ ) );
  460. msg.status = MAIL_NEW;
  461. msg.type = MAIL_INBOX_RETURNED;
  462. msg.timestamp = time( nullptr );
  463. int32 new_mail = mail_savemessage( &msg );
  464. mapif_Mail_new( &msg );
  465. // If the char server triggered this, check if we have to notify a map server
  466. if( fd <= 0 ){
  467. std::shared_ptr<struct online_char_data> character = util::umap_find( char_get_onlinedb(), account_id_sender );
  468. // Check for online players
  469. if( character != nullptr && character->server >= 0 ){
  470. fd = map_server[character->server].fd;
  471. }else{
  472. // The request was triggered inside the character server or the player is offline now
  473. return;
  474. }
  475. }
  476. WFIFOHEAD(fd,11);
  477. WFIFOW(fd,0) = 0x384c;
  478. WFIFOL(fd,2) = char_id;
  479. WFIFOL(fd,6) = mail_id;
  480. WFIFOB(fd,10) = (new_mail == 0);
  481. WFIFOSET(fd,11);
  482. }
  483. void mapif_parse_Mail_return(int32 fd)
  484. {
  485. mapif_Mail_return(fd, RFIFOL(fd,2), RFIFOL(fd,6));
  486. }
  487. /*==========================================
  488. * Send Mail
  489. *------------------------------------------*/
  490. void mapif_Mail_send(int32 fd, struct mail_message* msg)
  491. {
  492. int32 len = sizeof(struct mail_message) + 4;
  493. WFIFOHEAD(fd,len);
  494. WFIFOW(fd,0) = 0x384d;
  495. WFIFOW(fd,2) = len;
  496. memcpy(WFIFOP(fd,4), msg, sizeof(struct mail_message));
  497. WFIFOSET(fd,len);
  498. }
  499. void mapif_parse_Mail_send(int32 fd)
  500. {
  501. struct mail_message msg;
  502. char esc_name[NAME_LENGTH*2+1];
  503. char *data;
  504. size_t len;
  505. if(RFIFOW(fd,2) != 8 + sizeof(struct mail_message))
  506. return;
  507. memcpy(&msg, RFIFOP(fd,8), sizeof(struct mail_message));
  508. if( msg.dest_id != 0 ){
  509. if( SQL_ERROR == Sql_Query(sql_handle, "SELECT `char_id`, `name` FROM `%s` WHERE `char_id` = '%u'", schema_config.char_db, msg.dest_id) ){
  510. Sql_ShowDebug(sql_handle);
  511. return;
  512. }
  513. msg.dest_id = 0;
  514. msg.dest_name[0] = '\0';
  515. if( SQL_SUCCESS == Sql_NextRow(sql_handle) ){
  516. Sql_GetData(sql_handle, 0, &data, nullptr);
  517. msg.dest_id = atoi(data);
  518. Sql_GetData(sql_handle, 1, &data, &len);
  519. safestrncpy(msg.dest_name, data, NAME_LENGTH);
  520. }
  521. Sql_FreeResult(sql_handle);
  522. }
  523. // Try to find the Dest Char by Name
  524. Sql_EscapeStringLen(sql_handle, esc_name, msg.dest_name, strnlen(msg.dest_name, NAME_LENGTH));
  525. if ( SQL_ERROR == Sql_Query(sql_handle, "SELECT `account_id`, `char_id` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name) ){
  526. Sql_ShowDebug(sql_handle);
  527. }else if ( SQL_SUCCESS == Sql_NextRow(sql_handle) ){
  528. #if PACKETVER < 20150513
  529. uint32 account_id = RFIFOL(fd,4);
  530. Sql_GetData(sql_handle, 0, &data, nullptr);
  531. if (atoi(data) != account_id)
  532. { // Cannot send mail to char in the same account
  533. Sql_GetData(sql_handle, 1, &data, nullptr);
  534. msg.dest_id = atoi(data);
  535. }
  536. #else
  537. // In RODEX you can even send mails to yourself
  538. Sql_GetData(sql_handle, 1, &data, nullptr);
  539. msg.dest_id = atoi(data);
  540. #endif
  541. }
  542. Sql_FreeResult(sql_handle);
  543. msg.status = MAIL_NEW;
  544. if( msg.dest_id > 0 )
  545. msg.id = mail_savemessage(&msg);
  546. mapif_Mail_send(fd, &msg); // notify sender
  547. mapif_Mail_new(&msg); // notify recipient
  548. }
  549. bool mail_sendmail(int32 send_id, const char* send_name, int32 dest_id, const char* dest_name, const char* title, const char* body, int32 zeny, struct item *item, int32 amount)
  550. {
  551. struct mail_message msg;
  552. memset(&msg, 0, sizeof(struct mail_message));
  553. msg.send_id = send_id;
  554. safestrncpy(msg.send_name, send_name, NAME_LENGTH);
  555. msg.dest_id = dest_id;
  556. safestrncpy(msg.dest_name, dest_name, NAME_LENGTH);
  557. safestrncpy(msg.title, title, MAIL_TITLE_LENGTH);
  558. safestrncpy(msg.body, body, MAIL_BODY_LENGTH);
  559. msg.zeny = zeny;
  560. if( item != nullptr ){
  561. int32 i;
  562. for( i = 0; i < amount && i < MAIL_MAX_ITEM; i++ ){
  563. memcpy(&msg.item[i], &item[i], sizeof(struct item));
  564. }
  565. }
  566. msg.timestamp = time(nullptr);
  567. msg.type = MAIL_INBOX_NORMAL;
  568. if( !mail_savemessage(&msg) ){
  569. return false;
  570. }
  571. mapif_Mail_new(&msg);
  572. return true;
  573. }
  574. void mapif_Mail_receiver_send( int32 fd, int32 requesting_char_id, int32 char_id, int32 class_, int32 base_level, const char* name ){
  575. WFIFOHEAD(fd,38);
  576. WFIFOW(fd,0) = 0x384e;
  577. WFIFOL(fd,2) = requesting_char_id;
  578. WFIFOL(fd,6) = char_id;
  579. WFIFOW(fd,10) = class_;
  580. WFIFOW(fd,12) = base_level;
  581. strncpy(WFIFOCP(fd, 14), name, NAME_LENGTH);
  582. WFIFOSET(fd,38);
  583. }
  584. void mapif_parse_Mail_receiver_check( int32 fd ){
  585. char name[NAME_LENGTH], esc_name[NAME_LENGTH * 2 + 1];
  586. uint32 char_id = 0;
  587. uint16 class_ = 0, base_level = 0;
  588. safestrncpy( name, RFIFOCP(fd, 6), NAME_LENGTH );
  589. // Try to find the Dest Char by Name
  590. Sql_EscapeStringLen( sql_handle, esc_name, name, strnlen( name, NAME_LENGTH ) );
  591. if( SQL_ERROR == Sql_Query( sql_handle, "SELECT `char_id`,`class`,`base_level` FROM `%s` WHERE `name` = '%s'", schema_config.char_db, esc_name ) ){
  592. Sql_ShowDebug(sql_handle);
  593. }else if( SQL_SUCCESS == Sql_NextRow(sql_handle) ){
  594. char *data;
  595. Sql_GetData(sql_handle, 0, &data, nullptr); char_id = atoi(data);
  596. Sql_GetData(sql_handle, 1, &data, nullptr); class_ = atoi(data);
  597. Sql_GetData(sql_handle, 2, &data, nullptr); base_level = atoi(data);
  598. }
  599. Sql_FreeResult(sql_handle);
  600. mapif_Mail_receiver_send( fd, RFIFOL(fd, 2), char_id, class_, base_level, name );
  601. }
  602. /*==========================================
  603. * Packets From Map Server
  604. *------------------------------------------*/
  605. int32 inter_mail_parse_frommap(int32 fd)
  606. {
  607. switch(RFIFOW(fd,0))
  608. {
  609. case 0x3048: mapif_parse_Mail_requestinbox(fd); break;
  610. case 0x3049: mapif_parse_Mail_read(fd); break;
  611. case 0x304a: mapif_parse_Mail_getattach(fd); break;
  612. case 0x304b: mapif_parse_Mail_delete(fd); break;
  613. case 0x304c: mapif_parse_Mail_return(fd); break;
  614. case 0x304d: mapif_parse_Mail_send(fd); break;
  615. case 0x304e: mapif_parse_Mail_receiver_check(fd); break;
  616. default:
  617. return 0;
  618. }
  619. return 1;
  620. }
  621. int32 inter_mail_sql_init(void)
  622. {
  623. return 1;
  624. }
  625. void inter_mail_sql_final(void)
  626. {
  627. return;
  628. }