mail.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. time_t mail_calctimes(void)
  15. {
  16. time_t temp = time(NULL);
  17. return mktime(localtime(&temp));
  18. }
  19. void mail_clear(struct map_session_data *sd)
  20. {
  21. sd->mail.nameid = 0;
  22. sd->mail.index = 0;
  23. sd->mail.amount = 0;
  24. sd->mail.zeny = 0;
  25. return;
  26. }
  27. int mail_removeitem(struct map_session_data *sd, short flag)
  28. {
  29. nullpo_retr(0,sd);
  30. if( sd->mail.amount )
  31. {
  32. if (flag)
  33. { // Item send
  34. if(log_config.enable_logs&0x2000)
  35. log_pick_pc(sd, "E", sd->mail.nameid, -sd->mail.amount, &sd->status.inventory[sd->mail.index]);
  36. pc_delitem(sd, sd->mail.index, sd->mail.amount, 1);
  37. }
  38. else
  39. clif_additem(sd, sd->mail.index, sd->mail.amount, 0);
  40. }
  41. sd->mail.nameid = 0;
  42. sd->mail.index = 0;
  43. sd->mail.amount = 0;
  44. return 1;
  45. }
  46. int mail_removezeny(struct map_session_data *sd, short flag)
  47. {
  48. nullpo_retr(0,sd);
  49. if (flag && sd->mail.zeny > 0)
  50. sd->status.zeny -= sd->mail.zeny;
  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)
  60. return 0;
  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. memcpy(&msg->item, &sd->status.inventory[n], sizeof(struct item));
  98. msg->item.amount = sd->mail.amount;
  99. }
  100. else
  101. memset(&msg->item, 0x00, sizeof(struct item));
  102. msg->zeny = sd->mail.zeny;
  103. // Removes the attachment from sender
  104. mail_removeitem(sd,1);
  105. mail_removezeny(sd,1);
  106. return true;
  107. }
  108. void mail_getattachment(struct map_session_data* sd, int zeny, struct item* item)
  109. {
  110. if( item->nameid > 0 && item->amount > 0 )
  111. {
  112. pc_additem(sd, item, item->amount);
  113. if(log_config.enable_logs&0x2000)
  114. log_pick_pc(sd, "E", item->nameid, item->amount, item);
  115. clif_Mail_getattachment(sd->fd, 0);
  116. }
  117. if( zeny > 0 )
  118. pc_getzeny(sd, zeny);
  119. }
  120. int mail_openmail(struct map_session_data *sd)
  121. {
  122. nullpo_retr(0,sd);
  123. if( sd->state.storage_flag || sd->vender_id || sd->state.trading )
  124. return 0;
  125. clif_Mail_window(sd->fd, 0);
  126. return 1;
  127. }
  128. void mail_deliveryfail(struct map_session_data *sd, struct mail_message *msg)
  129. {
  130. nullpo_retv(sd);
  131. nullpo_retv(msg);
  132. pc_additem(sd, &msg->item, msg->item.amount);
  133. if( msg->zeny > 0 )
  134. {
  135. sd->status.zeny += msg->zeny;
  136. clif_updatestatus(sd, SP_ZENY);
  137. }
  138. clif_Mail_send(sd->fd, true);
  139. }
  140. // This function only check if the mail operations are valid
  141. bool mail_invalid_operation(struct map_session_data *sd)
  142. {
  143. if( !map[sd->bl.m].flag.town && pc_isGM(sd) < get_atcommand_level(atcommand_mail) )
  144. {
  145. ShowWarning("clif_parse_Mail: char '%s' trying to do invalid mail operations.\n", sd->status.name);
  146. return true;
  147. }
  148. return false;
  149. }
  150. #endif