int_mail.cpp 23 KB

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