mail.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef TXT_ONLY
  4. #include "../common/nullpo.h"
  5. #include "../common/showmsg.h"
  6. #include "mail.h"
  7. #include "atcommand.h"
  8. #include "itemdb.h"
  9. #include "clif.h"
  10. #include "pc.h"
  11. #include "log.h"
  12. #include <time.h>
  13. #include <string.h>
  14. void mail_clear(struct map_session_data *sd)
  15. {
  16. sd->mail.nameid = 0;
  17. sd->mail.index = 0;
  18. sd->mail.amount = 0;
  19. sd->mail.zeny = 0;
  20. sd->auction.amount = 0;
  21. return;
  22. }
  23. int mail_removeitem(struct map_session_data *sd, short flag)
  24. {
  25. nullpo_ret(sd);
  26. if( sd->mail.amount )
  27. {
  28. if (flag)
  29. { // Item send
  30. if(log_config.enable_logs&0x2000)
  31. log_pick_pc(sd, "E", sd->mail.nameid, -sd->mail.amount, &sd->status.inventory[sd->mail.index]);
  32. pc_delitem(sd, sd->mail.index, sd->mail.amount, 1, 0);
  33. }
  34. else
  35. clif_additem(sd, sd->mail.index, sd->mail.amount, 0);
  36. }
  37. sd->mail.nameid = 0;
  38. sd->mail.index = 0;
  39. sd->mail.amount = 0;
  40. return 1;
  41. }
  42. int mail_removezeny(struct map_session_data *sd, short flag)
  43. {
  44. nullpo_ret(sd);
  45. if (flag && sd->mail.zeny > 0)
  46. { //Zeny send
  47. if(log_config.zeny)
  48. log_zeny(sd, "E", sd, -sd->mail.zeny);
  49. sd->status.zeny -= sd->mail.zeny;
  50. }
  51. sd->mail.zeny = 0;
  52. clif_updatestatus(sd, SP_ZENY);
  53. return 1;
  54. }
  55. unsigned char mail_setitem(struct map_session_data *sd, int idx, int amount)
  56. {
  57. if( idx == 0 )
  58. { // Zeny Transfer
  59. if( amount < 0 || !pc_can_give_items(pc_isGM(sd)) )
  60. return 1;
  61. if( amount > sd->status.zeny )
  62. amount = sd->status.zeny;
  63. sd->mail.zeny = amount;
  64. // clif_updatestatus(sd, SP_ZENY);
  65. return 0;
  66. }
  67. else
  68. { // Item Transfer
  69. idx -= 2;
  70. mail_removeitem(sd, 0);
  71. if( idx < 0 || idx >= MAX_INVENTORY )
  72. return 1;
  73. if( amount < 0 || amount > sd->status.inventory[idx].amount )
  74. return 1;
  75. if( !pc_candrop(sd, &sd->status.inventory[idx]) )
  76. return 1;
  77. sd->mail.index = idx;
  78. sd->mail.nameid = sd->status.inventory[idx].nameid;
  79. sd->mail.amount = amount;
  80. return 0;
  81. }
  82. }
  83. bool mail_setattachment(struct map_session_data *sd, struct mail_message *msg)
  84. {
  85. int n;
  86. nullpo_retr(false,sd);
  87. nullpo_retr(false,msg);
  88. if( sd->mail.zeny < 0 || sd->mail.zeny > sd->status.zeny )
  89. return false;
  90. n = sd->mail.index;
  91. if( sd->mail.amount )
  92. {
  93. if( sd->status.inventory[n].nameid != sd->mail.nameid )
  94. return false;
  95. if( sd->status.inventory[n].amount < sd->mail.amount )
  96. return false;
  97. if( sd->weight > sd->max_weight )
  98. return false;
  99. memcpy(&msg->item, &sd->status.inventory[n], sizeof(struct item));
  100. msg->item.amount = sd->mail.amount;
  101. }
  102. else
  103. memset(&msg->item, 0x00, sizeof(struct item));
  104. msg->zeny = sd->mail.zeny;
  105. // Removes the attachment from sender
  106. mail_removeitem(sd,1);
  107. mail_removezeny(sd,1);
  108. return true;
  109. }
  110. void mail_getattachment(struct map_session_data* sd, int zeny, struct item* item)
  111. {
  112. if( item->nameid > 0 && item->amount > 0 )
  113. {
  114. pc_additem(sd, item, item->amount);
  115. if(log_config.enable_logs&0x2000)
  116. log_pick_pc(sd, "E", item->nameid, item->amount, item);
  117. clif_Mail_getattachment(sd->fd, 0);
  118. }
  119. if( zeny > 0 )
  120. { //Zeny recieve
  121. if(log_config.zeny)
  122. log_zeny(sd, "E", sd, zeny);
  123. pc_getzeny(sd, zeny);
  124. }
  125. }
  126. int mail_openmail(struct map_session_data *sd)
  127. {
  128. nullpo_ret(sd);
  129. if( sd->state.storage_flag || sd->vender_id || sd->state.trading )
  130. return 0;
  131. clif_Mail_window(sd->fd, 0);
  132. return 1;
  133. }
  134. void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg)
  135. {
  136. nullpo_retv(sd);
  137. nullpo_retv(msg);
  138. if( msg->item.amount > 0 )
  139. {
  140. // Item recieve (due to failure)
  141. if(log_config.enable_logs&0x2000)
  142. log_pick_pc(sd, "E", msg->item.nameid, msg->item.amount, &msg->item);
  143. pc_additem(sd, &msg->item, msg->item.amount);
  144. }
  145. if( msg->zeny > 0 )
  146. {
  147. //Zeny recieve (due to failure)
  148. if(log_config.zeny)
  149. log_zeny(sd, "E", sd, msg->zeny);
  150. sd->status.zeny += msg->zeny;
  151. clif_updatestatus(sd, SP_ZENY);
  152. }
  153. clif_Mail_send(sd->fd, true);
  154. }
  155. // This function only check if the mail operations are valid
  156. bool mail_invalid_operation(struct map_session_data *sd)
  157. {
  158. if( !map[sd->bl.m].flag.town && pc_isGM(sd) < get_atcommand_level(atcommand_mail) )
  159. {
  160. ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
  161. return true;
  162. }
  163. return false;
  164. }
  165. #endif