mail.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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(struct 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(struct 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( struct 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(struct 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. total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 );
  155. }
  156. // Add the newly added weight to the current total
  157. total += amount * sd->inventory_data[idx]->weight / 10;
  158. if( total > battle_config.mail_attachment_weight ){
  159. return MAIL_ATTACH_WEIGHT;
  160. }
  161. }
  162. sd->mail.item[i].amount += amount;
  163. return MAIL_ATTACH_SUCCESS;
  164. }else{
  165. ARR_FIND(0, MAIL_MAX_ITEM, i, sd->mail.item[i].nameid == 0);
  166. if( i == MAIL_MAX_ITEM ){
  167. return MAIL_ATTACH_SPACE;
  168. }
  169. // Check if it exceeds the total weight
  170. if( battle_config.mail_attachment_weight ){
  171. // Only need to sum up all entries until the new entry
  172. for( j = 0; j < i; j++ ){
  173. total += sd->mail.item[j].amount * ( sd->inventory_data[sd->mail.item[j].index]->weight / 10 );
  174. }
  175. // Add the newly added weight to the current total
  176. total += amount * sd->inventory_data[idx]->weight / 10;
  177. if( total > battle_config.mail_attachment_weight ){
  178. return MAIL_ATTACH_WEIGHT;
  179. }
  180. }
  181. }
  182. #endif
  183. if( amount > sd->inventory.u.items_inventory[idx].amount )
  184. return MAIL_ATTACH_ERROR;
  185. if( !pc_can_give_items(sd) || sd->inventory.u.items_inventory[idx].expire_time
  186. || !itemdb_available(sd->inventory.u.items_inventory[idx].nameid)
  187. || !itemdb_canmail(&sd->inventory.u.items_inventory[idx],pc_get_group_level(sd))
  188. || (sd->inventory.u.items_inventory[idx].bound && !pc_can_give_bounded_items(sd)) )
  189. return MAIL_ATTACH_UNTRADEABLE;
  190. sd->mail.item[i].index = idx;
  191. sd->mail.item[i].nameid = sd->inventory.u.items_inventory[idx].nameid;
  192. sd->mail.item[i].amount = amount;
  193. return MAIL_ATTACH_SUCCESS;
  194. }
  195. }
  196. bool mail_setattachment(struct map_session_data *sd, struct mail_message *msg)
  197. {
  198. int i, amount;
  199. nullpo_retr(false,sd);
  200. nullpo_retr(false,msg);
  201. for( i = 0, amount = 0; i < MAIL_MAX_ITEM; i++ ){
  202. int index = sd->mail.item[i].index;
  203. if( sd->mail.item[i].nameid == 0 || sd->mail.item[i].amount == 0 ){
  204. memset(&msg->item[i], 0x00, sizeof(struct item));
  205. continue;
  206. }
  207. amount++;
  208. if( sd->inventory.u.items_inventory[index].nameid != sd->mail.item[i].nameid )
  209. return false;
  210. if( sd->inventory.u.items_inventory[index].amount < sd->mail.item[i].amount )
  211. return false;
  212. if( sd->weight > sd->max_weight ) // TODO: Why check something weird like this here?
  213. return false;
  214. memcpy(&msg->item[i], &sd->inventory.u.items_inventory[index], sizeof(struct item));
  215. msg->item[i].amount = sd->mail.item[i].amount;
  216. }
  217. 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 )
  218. return false;
  219. msg->zeny = sd->mail.zeny;
  220. // Removes the attachment from sender
  221. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  222. if( sd->mail.item[i].nameid == 0 || sd->mail.item[i].amount == 0 ){
  223. // Exit the loop on the first empty entry
  224. break;
  225. }
  226. mail_removeitem(sd,1,sd->mail.item[i].index + 2,sd->mail.item[i].amount);
  227. }
  228. mail_removezeny(sd,true);
  229. return true;
  230. }
  231. void mail_getattachment(struct map_session_data* sd, struct mail_message* msg, int zeny, struct item* item){
  232. int i;
  233. bool item_received = false;
  234. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  235. if( item[i].nameid > 0 && item[i].amount > 0 ){
  236. struct item_data* id = itemdb_search( item[i].nameid );
  237. // Item does not exist (anymore?)
  238. if( id == nullptr ){
  239. continue;
  240. }
  241. // Reduce the pending weight
  242. sd->mail.pending_weight -= ( id->weight * item[i].amount );
  243. // Check if it is a pet egg
  244. std::shared_ptr<s_pet_db> pet = pet_db_search( item[i].nameid, PET_EGG );
  245. // If it is a pet egg and the card data does not contain a pet id or other special ids are set
  246. if( pet != nullptr && item[i].card[0] == 0 ){
  247. // Create a new pet
  248. if( pet_create_egg( sd, item[i].nameid ) ){
  249. sd->mail.pending_slots--;
  250. item_received = true;
  251. }else{
  252. // Do not send receive packet so that the mail is still displayed with item attachment
  253. item_received = false;
  254. // Additionally stop the processing
  255. break;
  256. }
  257. }else{
  258. char check = pc_checkadditem( sd, item[i].nameid, item[i].amount );
  259. // Add the item normally
  260. if( check != CHKADDITEM_OVERAMOUNT && pc_additem( sd, &item[i], item[i].amount, LOG_TYPE_MAIL ) == ADDITEM_SUCCESS ){
  261. item_received = true;
  262. // Only reduce slots if it really required a new slot
  263. if( check == CHKADDITEM_NEW ){
  264. sd->mail.pending_slots -= id->inventorySlotNeeded( item[i].amount );
  265. }
  266. }else{
  267. // Do not send receive packet so that the mail is still displayed with item attachment
  268. item_received = false;
  269. // Additionally stop the processing
  270. break;
  271. }
  272. }
  273. // Make sure no requests are possible anymore
  274. item[i].amount = 0;
  275. }
  276. }
  277. if( item_received ){
  278. clif_mail_getattachment( sd, msg, 0, MAIL_ATT_ITEM );
  279. }
  280. // Zeny receive
  281. if( zeny > 0 ){
  282. // Reduce the pending zeny
  283. sd->mail.pending_zeny -= zeny;
  284. // Add the zeny
  285. pc_getzeny(sd, zeny,LOG_TYPE_MAIL, NULL);
  286. clif_mail_getattachment( sd, msg, 0, MAIL_ATT_ZENY );
  287. }
  288. }
  289. int mail_openmail(struct map_session_data *sd)
  290. {
  291. nullpo_ret(sd);
  292. if( sd->state.storage_flag || sd->state.vending || sd->state.buyingstore || sd->state.trading )
  293. return 0;
  294. clif_Mail_window(sd->fd, 0);
  295. return 1;
  296. }
  297. void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg){
  298. int i, zeny = 0;
  299. nullpo_retv(sd);
  300. nullpo_retv(msg);
  301. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  302. if( msg->item[i].amount > 0 ){
  303. // Item receive (due to failure)
  304. pc_additem(sd, &msg->item[i], msg->item[i].amount, LOG_TYPE_MAIL);
  305. zeny += battle_config.mail_attachment_price;
  306. }
  307. }
  308. if( msg->zeny > 0 ){
  309. pc_getzeny(sd,msg->zeny + msg->zeny*battle_config.mail_zeny_fee/100 + zeny,LOG_TYPE_MAIL, NULL); //Zeny receive (due to failure)
  310. }
  311. clif_Mail_send(sd, WRITE_MAIL_FAILED);
  312. }
  313. // This function only check if the mail operations are valid
  314. bool mail_invalid_operation(struct map_session_data *sd)
  315. {
  316. #if PACKETVER < 20150513
  317. if( !map_getmapflag(sd->bl.m, MF_TOWN) && !pc_can_use_command(sd, "mail", COMMAND_ATCOMMAND) )
  318. {
  319. ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
  320. return true;
  321. }
  322. #else
  323. if( map_getmapflag( sd->bl.m, MF_NORODEX ) ){
  324. clif_displaymessage( sd->fd, msg_txt( sd, 796 ) ); // You cannot use RODEX on this map.
  325. return true;
  326. }
  327. #endif
  328. return false;
  329. }
  330. /**
  331. * Attempt to send mail
  332. * @param sd Sender
  333. * @param dest_name Destination name
  334. * @param title Mail title
  335. * @param body_msg Mail message
  336. * @param body_len Message's length
  337. */
  338. void mail_send(struct map_session_data *sd, const char *dest_name, const char *title, const char *body_msg, int body_len) {
  339. struct mail_message msg;
  340. nullpo_retv(sd);
  341. if( sd->state.trading )
  342. return;
  343. if( DIFF_TICK(sd->cansendmail_tick, gettick()) > 0 ) {
  344. clif_displaymessage(sd->fd,msg_txt(sd,675)); //"Cannot send mails too fast!!."
  345. clif_Mail_send(sd, WRITE_MAIL_FAILED); // fail
  346. return;
  347. }
  348. if( battle_config.mail_daily_count ){
  349. mail_refresh_remaining_amount(sd);
  350. // After calling mail_refresh_remaining_amount the status should always be there
  351. if( sd->sc.data[SC_DAILYSENDMAILCNT] == NULL || sd->sc.data[SC_DAILYSENDMAILCNT]->val2 >= battle_config.mail_daily_count ){
  352. clif_Mail_send(sd, WRITE_MAIL_FAILED_CNT);
  353. return;
  354. }else{
  355. sc_start2( &sd->bl, &sd->bl, SC_DAILYSENDMAILCNT, 100, date_get_dayofyear(), sd->sc.data[SC_DAILYSENDMAILCNT]->val2 + 1, INFINITE_TICK );
  356. }
  357. }
  358. if( body_len > MAIL_BODY_LENGTH )
  359. body_len = MAIL_BODY_LENGTH;
  360. if( !mail_setattachment(sd, &msg) ) { // Invalid Append condition
  361. int i;
  362. clif_Mail_send(sd, WRITE_MAIL_FAILED); // fail
  363. for( i = 0; i < MAIL_MAX_ITEM; i++ ){
  364. mail_removeitem(sd,0,sd->mail.item[i].index + 2, sd->mail.item[i].amount);
  365. }
  366. mail_removezeny(sd,false);
  367. return;
  368. }
  369. msg.id = 0; // id will be assigned by charserver
  370. msg.send_id = sd->status.char_id;
  371. msg.dest_id = 0; // will attempt to resolve name
  372. safestrncpy(msg.send_name, sd->status.name, NAME_LENGTH);
  373. safestrncpy(msg.dest_name, (char*)dest_name, NAME_LENGTH);
  374. safestrncpy(msg.title, (char*)title, MAIL_TITLE_LENGTH);
  375. msg.type = MAIL_INBOX_NORMAL;
  376. if (msg.title[0] == '\0') {
  377. return; // Message has no length and somehow client verification was skipped.
  378. }
  379. if (body_len)
  380. safestrncpy(msg.body, (char*)body_msg, min(body_len + 1, MAIL_BODY_LENGTH));
  381. else
  382. memset(msg.body, 0x00, MAIL_BODY_LENGTH);
  383. msg.timestamp = time(NULL);
  384. if( !intif_Mail_send(sd->status.account_id, &msg) )
  385. mail_deliveryfail(sd, &msg);
  386. sd->cansendmail_tick = gettick() + battle_config.mail_delay; // Flood Protection
  387. }
  388. void mail_refresh_remaining_amount( struct map_session_data* sd ){
  389. int doy = date_get_dayofyear();
  390. nullpo_retv(sd);
  391. // If it was not yet started or it was started on another day
  392. if( sd->sc.data[SC_DAILYSENDMAILCNT] == NULL || sd->sc.data[SC_DAILYSENDMAILCNT]->val1 != doy ){
  393. sc_start2( &sd->bl, &sd->bl, SC_DAILYSENDMAILCNT, 100, doy, 0, INFINITE_TICK );
  394. }
  395. }