mail.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "mail.hpp"
  4. #include "../common/nullpo.hpp"
  5. #include "../common/showmsg.hpp"
  6. #include "../common/strlib.hpp"
  7. #include "../common/timer.hpp"
  8. #include "atcommand.hpp"
  9. #include "battle.hpp"
  10. #include "clif.hpp"
  11. #include "date.hpp" // date_get_dayofyear
  12. #include "intif.hpp"
  13. #include "itemdb.hpp"
  14. #include "log.hpp"
  15. #include "pc.hpp"
  16. #include "pet.hpp"
  17. void mail_clear(map_session_data *sd)
  18. {
  19. int i;
  20. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  21. sd->mail.item[i].nameid = 0;
  22. sd->mail.item[i].index = 0;
  23. sd->mail.item[i].amount = 0;
  24. }
  25. sd->mail.zeny = 0;
  26. return;
  27. }
  28. int mail_removeitem(map_session_data *sd, short flag, int idx, int amount)
  29. {
  30. int i;
  31. nullpo_ret(sd);
  32. idx -= 2;
  33. if( idx < 0 || idx >= MAX_INVENTORY )
  34. return false;
  35. if( amount <= 0 || amount > sd->inventory.u.items_inventory[idx].amount )
  36. return false;
  37. ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].index == idx && sd->mail.item[i].nameid > 0);
  38. if( i == MAIL_MAX_ITEM ){
  39. return false;
  40. }
  41. if( flag ){
  42. if( battle_config.mail_attachment_price > 0 ){
  43. if( pc_payzeny( sd, battle_config.mail_attachment_price, LOG_TYPE_MAIL, NULL ) ){
  44. return false;
  45. }
  46. }
  47. #if PACKETVER < 20150513
  48. // With client update packet
  49. pc_delitem(sd, idx, amount, 1, 0, LOG_TYPE_MAIL);
  50. #else
  51. // RODEX refreshes the client inventory from the ACK packet
  52. pc_delitem(sd, idx, amount, 0, 0, LOG_TYPE_MAIL);
  53. #endif
  54. }else{
  55. sd->mail.item[i].amount -= amount;
  56. // Item was removed completely
  57. if( sd->mail.item[i].amount <= 0 ){
  58. // Move the rest of the array forward
  59. for( ; i < MAIL_MAX_ITEM - 1; i++ ){
  60. if ( sd->mail.item[i + 1].nameid == 0 ){
  61. break;
  62. }
  63. sd->mail.item[i].index = sd->mail.item[i+1].index;
  64. sd->mail.item[i].nameid = sd->mail.item[i+1].nameid;
  65. sd->mail.item[i].amount = sd->mail.item[i+1].amount;
  66. }
  67. // Zero the rest
  68. for( ; i < MAIL_MAX_ITEM; i++ ){
  69. sd->mail.item[i].index = 0;
  70. sd->mail.item[i].nameid = 0;
  71. sd->mail.item[i].amount = 0;
  72. }
  73. }
  74. #if PACKETVER < 20150513
  75. clif_additem(sd, idx, amount, 0);
  76. #else
  77. clif_mail_removeitem(sd, true, idx + 2, amount);
  78. #endif
  79. }
  80. return 1;
  81. }
  82. bool mail_removezeny( map_session_data *sd, bool flag ){
  83. nullpo_retr( false, sd );
  84. if( sd->mail.zeny > 0 ){
  85. //Zeny send
  86. if( flag ){
  87. if( pc_payzeny( sd, sd->mail.zeny + sd->mail.zeny * battle_config.mail_zeny_fee / 100, LOG_TYPE_MAIL, NULL ) ){
  88. return false;
  89. }
  90. }else{
  91. // Update is called by pc_payzeny, so only call it in the else condition
  92. clif_updatestatus(sd, SP_ZENY);
  93. }
  94. }
  95. sd->mail.zeny = 0;
  96. return true;
  97. }
  98. /**
  99. * Attempt to set item or zeny to a mail
  100. * @param sd : player attaching the content
  101. * @param idx 0 - Zeny; >= 2 - Inventory item
  102. * @param amount : amout of zeny or number of item
  103. * @return see enum mail_attach_result in mail.hpp
  104. */
  105. enum mail_attach_result mail_setitem(map_session_data *sd, short idx, uint32 amount) {
  106. if( pc_istrading(sd) )
  107. return MAIL_ATTACH_ERROR;
  108. if( idx == 0 ) { // Zeny Transfer
  109. if( !pc_can_give_items(sd) )
  110. return MAIL_ATTACH_UNTRADEABLE;
  111. #if PACKETVER < 20150513
  112. if( amount > sd->status.zeny )
  113. amount = sd->status.zeny; // TODO: confirm this behavior for old mail system
  114. #else
  115. if( ( amount + battle_config.mail_zeny_fee / 100 * amount ) > sd->status.zeny )
  116. return MAIL_ATTACH_ERROR;
  117. #endif
  118. sd->mail.zeny = amount;
  119. // clif_updatestatus(sd, SP_ZENY);
  120. return MAIL_ATTACH_SUCCESS;
  121. } else { // Item Transfer
  122. int i;
  123. #if PACKETVER >= 20150513
  124. int j, total = 0;
  125. #endif
  126. idx -= 2;
  127. if( idx < 0 || idx >= MAX_INVENTORY || sd->inventory_data[idx] == nullptr )
  128. return MAIL_ATTACH_ERROR;
  129. if (itemdb_ishatched_egg(&sd->inventory.u.items_inventory[idx]))
  130. return MAIL_ATTACH_ERROR;
  131. if( sd->inventory.u.items_inventory[idx].equipSwitch ){
  132. return MAIL_ATTACH_EQUIPSWITCH;
  133. }
  134. #if PACKETVER < 20150513
  135. i = 0;
  136. // Remove existing item
  137. mail_removeitem(sd, 0, sd->mail.item[i].index + 2, sd->mail.item[i].amount);
  138. #else
  139. ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].index == idx && sd->mail.item[i].nameid > 0 );
  140. // The same item had already been added to the mail
  141. if( i < MAIL_MAX_ITEM ){
  142. // Check if it is stackable
  143. if( !itemdb_isstackable(sd->mail.item[i].nameid) ){
  144. return MAIL_ATTACH_ERROR;
  145. }
  146. // Check if it exceeds the total amount
  147. if( ( amount + sd->mail.item[i].amount ) > sd->inventory.u.items_inventory[idx].amount ){
  148. return MAIL_ATTACH_ERROR;
  149. }
  150. // Check if it exceeds the total weight
  151. if( battle_config.mail_attachment_weight ){
  152. // Sum up all items to get the current total weight
  153. for( j = 0; j < MAIL_MAX_ITEM; j++ ){
  154. if (sd->mail.item[j].nameid == 0)
  155. continue;
  156. total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 );
  157. }
  158. // Add the newly added weight to the current total
  159. total += amount * sd->inventory_data[idx]->weight / 10;
  160. if( total > battle_config.mail_attachment_weight ){
  161. return MAIL_ATTACH_WEIGHT;
  162. }
  163. }
  164. sd->mail.item[i].amount += amount;
  165. return MAIL_ATTACH_SUCCESS;
  166. }else{
  167. ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].nameid == 0);
  168. if( i == MAIL_MAX_ITEM ){
  169. return MAIL_ATTACH_SPACE;
  170. }
  171. // Check if it exceeds the total weight
  172. if( battle_config.mail_attachment_weight ){
  173. // Only need to sum up all entries until the new entry
  174. for( j = 0; j < i; j++ ){
  175. total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 );
  176. }
  177. // Add the newly added weight to the current total
  178. total += amount * sd->inventory_data[idx]->weight / 10;
  179. if( total > battle_config.mail_attachment_weight ){
  180. return MAIL_ATTACH_WEIGHT;
  181. }
  182. }
  183. }
  184. #endif
  185. if( amount > sd->inventory.u.items_inventory[idx].amount )
  186. return MAIL_ATTACH_ERROR;
  187. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time
  188. || !itemdb_available(sd->inventory.u.items_inventory[idx].nameid)
  189. || !itemdb_canmail(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd))
  190. || (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) )
  191. return MAIL_ATTACH_UNTRADEABLE;
  192. sd->mail.item[i].index = idx;
  193. sd->mail.item[i].nameid = sd->inventory.u.items_inventory[idx].nameid;
  194. sd->mail.item[i].amount = amount;
  195. return MAIL_ATTACH_SUCCESS;
  196. }
  197. }
  198. bool mail_setattachment(map_session_data *sd, struct mail_message *msg)
  199. {
  200. int i, amount;
  201. nullpo_retr(false,sd);
  202. nullpo_retr(false,msg);
  203. for( i = 0, amount = 0; i < MAIL_MAX_ITEM; i++ ){
  204. int index = sd->mail.item[i].index;
  205. if( sd->mail.item[i].nameid == 0 || sd->mail.item[i].amount == 0 ){
  206. memset(&msg->item[i], 0x00, sizeof(struct item));
  207. continue;
  208. }
  209. amount++;
  210. if( sd->inventory.u.items_inventory[index].nameid != sd->mail.item[i].nameid )
  211. return false;
  212. if( sd->inventory.u.items_inventory[index].amount < sd->mail.item[i].amount )
  213. return false;
  214. if( sd->weight > sd->max_weight ) // TODO: Why check something weird like this here?
  215. return false;
  216. memcpy(&msg->item[i], &sd->inventory.u.items_inventory[index], sizeof(struct item));
  217. msg->item[i].amount = sd->mail.item[i].amount;
  218. }
  219. if( sd->mail.zeny < 0 || ( sd->mail.zeny + sd->mail.zeny * battle_config.mail_zeny_fee / 100 + amount * battle_config.mail_attachment_price ) > sd->status.zeny )
  220. return false;
  221. msg->zeny = sd->mail.zeny;
  222. // Removes the attachment from sender
  223. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  224. if( sd->mail.item[i].nameid == 0 || sd->mail.item[i].amount == 0 ){
  225. // Exit the loop on the first empty entry
  226. break;
  227. }
  228. mail_removeitem(sd,1,sd->mail.item[i].index + 2,sd->mail.item[i].amount);
  229. }
  230. mail_removezeny(sd,true);
  231. return true;
  232. }
  233. void mail_getattachment(map_session_data* sd, struct mail_message* msg, int zeny, struct item* item){
  234. int i;
  235. bool item_received = false;
  236. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  237. if( item[i].nameid > 0 && item[i].amount > 0 ){
  238. struct item_data* id = itemdb_search( item[i].nameid );
  239. // Item does not exist (anymore?)
  240. if( id == nullptr ){
  241. continue;
  242. }
  243. // Reduce the pending weight
  244. sd->mail.pending_weight -= ( id->weight * item[i].amount );
  245. // Check if it is a pet egg
  246. std::shared_ptr<s_pet_db> pet = pet_db_search( item[i].nameid, PET_EGG );
  247. // If it is a pet egg and the card data does not contain a pet id or other special ids are set
  248. if( pet != nullptr && item[i].card[0] == 0 ){
  249. // Create a new pet
  250. if( pet_create_egg( sd, item[i].nameid ) ){
  251. sd->mail.pending_slots--;
  252. item_received = true;
  253. }else{
  254. // Do not send receive packet so that the mail is still displayed with item attachment
  255. item_received = false;
  256. // Additionally stop the processing
  257. break;
  258. }
  259. }else{
  260. char check = pc_checkadditem( sd, item[i].nameid, item[i].amount );
  261. // Add the item normally
  262. if( check != CHKADDITEM_OVERAMOUNT && pc_additem( sd, &item[i], item[i].amount, LOG_TYPE_MAIL ) == ADDITEM_SUCCESS ){
  263. item_received = true;
  264. // Only reduce slots if it really required a new slot
  265. if( check == CHKADDITEM_NEW ){
  266. sd->mail.pending_slots -= id->inventorySlotNeeded( item[i].amount );
  267. }
  268. }else{
  269. // Do not send receive packet so that the mail is still displayed with item attachment
  270. item_received = false;
  271. // Additionally stop the processing
  272. break;
  273. }
  274. }
  275. // Make sure no requests are possible anymore
  276. item[i].amount = 0;
  277. }
  278. }
  279. if( item_received ){
  280. clif_mail_getattachment( sd, msg, 0, MAIL_ATT_ITEM );
  281. }
  282. // Zeny receive
  283. if( zeny > 0 ){
  284. // Reduce the pending zeny
  285. sd->mail.pending_zeny -= zeny;
  286. // Add the zeny
  287. pc_getzeny(sd, zeny,LOG_TYPE_MAIL, NULL);
  288. clif_mail_getattachment( sd, msg, 0, MAIL_ATT_ZENY );
  289. }
  290. }
  291. int mail_openmail(map_session_data *sd)
  292. {
  293. nullpo_ret(sd);
  294. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  295. return 0;
  296. clif_Mail_window(sd->fd, 0);
  297. return 1;
  298. }
  299. void mail_deliveryfail(map_session_data *sd, struct mail_message *msg){
  300. int i, zeny = 0;
  301. nullpo_retv(sd);
  302. nullpo_retv(msg);
  303. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  304. if( msg->item[i].amount > 0 ){
  305. // Item receive (due to failure)
  306. pc_additem(sd, &msg->item[i], msg->item[i].amount, LOG_TYPE_MAIL);
  307. zeny += battle_config.mail_attachment_price;
  308. }
  309. }
  310. if( msg->zeny > 0 ){
  311. pc_getzeny(sd,msg->zeny + msg->zeny*battle_config.mail_zeny_fee/100 + zeny,LOG_TYPE_MAIL, NULL); //Zeny receive (due to failure)
  312. }
  313. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  314. }
  315. // This function only check if the mail operations are valid
  316. bool mail_invalid_operation(map_session_data *sd)
  317. {
  318. #if PACKETVER < 20150513
  319. if( !map_getmapflag(sd->bl.m, MF_TOWN) && !pc_can_use_command(sd, "mail", COMMAND_ATCOMMAND) )
  320. {
  321. ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
  322. return true;
  323. }
  324. #else
  325. if( map_getmapflag( sd->bl.m, MF_NORODEX ) ){
  326. clif_displaymessage( sd->fd, msg_txt( sd, 796 ) ); // You cannot use RODEX on this map.
  327. return true;
  328. }
  329. #endif
  330. return false;
  331. }
  332. /**
  333. * Attempt to send mail
  334. * @param sd Sender
  335. * @param dest_name Destination name
  336. * @param title Mail title
  337. * @param body_msg Mail message
  338. * @param body_len Message's length
  339. */
  340. void mail_send(map_session_data *sd, const char *dest_name, const char *title, const char *body_msg, int body_len) {
  341. struct mail_message msg;
  342. nullpo_retv(sd);
  343. if( sd->state.trading )
  344. return;
  345. if( DIFF_TICK(sd->cansendmail_tick, gettick()) > 0 ) {
  346. clif_displaymessage(sd->fd,msg_txt(sd,675)); //"Cannot send mails too fast!!."
  347. clif_Mail_send(sd, WRITE_MAIL_FAILED); // fail
  348. return;
  349. }
  350. if( battle_config.mail_daily_count ){
  351. mail_refresh_remaining_amount(sd);
  352. // After calling mail_refresh_remaining_amount the status should always be there
  353. if( sd->sc.getSCE(SC_DAILYSENDMAILCNT) == NULL || sd->sc.getSCE(SC_DAILYSENDMAILCNT)->val2 >= battle_config.mail_daily_count ){
  354. clif_Mail_send(sd, WRITE_MAIL_FAILED_CNT);
  355. return;
  356. }else{
  357. sc_start2( &sd->bl, &sd->bl, SC_DAILYSENDMAILCNT, 100, date_get_dayofyear(), sd->sc.getSCE(SC_DAILYSENDMAILCNT)->val2 + 1, INFINITE_TICK );
  358. }
  359. }
  360. if( body_len > MAIL_BODY_LENGTH )
  361. body_len = MAIL_BODY_LENGTH;
  362. if( !mail_setattachment(sd, &msg) ) { // Invalid Append condition
  363. int i;
  364. clif_Mail_send(sd, WRITE_MAIL_FAILED); // fail
  365. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  366. mail_removeitem(sd,0,sd->mail.item[i].index + 2, sd->mail.item[i].amount);
  367. }
  368. mail_removezeny(sd,false);
  369. return;
  370. }
  371. msg.id = 0; // id will be assigned by charserver
  372. msg.send_id = sd->status.char_id;
  373. msg.dest_id = 0; // will attempt to resolve name
  374. safestrncpy(msg.send_name, sd->status.name, NAME_LENGTH);
  375. safestrncpy(msg.dest_name, (char*)dest_name, NAME_LENGTH);
  376. safestrncpy(msg.title, (char*)title, MAIL_TITLE_LENGTH);
  377. msg.type = MAIL_INBOX_NORMAL;
  378. if (msg.title[0] == '\0') {
  379. return; // Message has no length and somehow client verification was skipped.
  380. }
  381. if (body_len)
  382. safestrncpy(msg.body, (char*)body_msg, min(body_len + 1, MAIL_BODY_LENGTH));
  383. else
  384. memset(msg.body, 0x00, MAIL_BODY_LENGTH);
  385. msg.timestamp = time(NULL);
  386. if( !intif_Mail_send(sd->status.account_id, &msg) )
  387. mail_deliveryfail(sd, &msg);
  388. sd->cansendmail_tick = gettick() + battle_config.mail_delay; // Flood Protection
  389. }
  390. void mail_refresh_remaining_amount( map_session_data* sd ){
  391. int doy = date_get_dayofyear();
  392. nullpo_retv(sd);
  393. // If it was not yet started or it was started on another day
  394. if( sd->sc.getSCE(SC_DAILYSENDMAILCNT) == NULL || sd->sc.getSCE(SC_DAILYSENDMAILCNT)->val1 != doy ){
  395. sc_start2( &sd->bl, &sd->bl, SC_DAILYSENDMAILCNT, 100, doy, 0, INFINITE_TICK );
  396. }
  397. }