RequestSendPost.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * This program is free software: you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation, either version 3 of the License, or (at your option) any later
  5. * version.
  6. *
  7. * This program is distributed in the hope that it will be useful, but WITHOUT
  8. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  9. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  10. * details.
  11. *
  12. * You should have received a copy of the GNU General Public License along with
  13. * this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. package com.l2jserver.gameserver.network.clientpackets;
  16. import java.util.logging.Logger;
  17. import com.l2jserver.Config;
  18. import com.l2jserver.gameserver.datatables.AccessLevels;
  19. import com.l2jserver.gameserver.datatables.CharNameTable;
  20. import com.l2jserver.gameserver.instancemanager.MailManager;
  21. import com.l2jserver.gameserver.model.L2AccessLevel;
  22. import com.l2jserver.gameserver.model.L2ItemInstance;
  23. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  24. import com.l2jserver.gameserver.model.entity.Message;
  25. import com.l2jserver.gameserver.model.itemcontainer.Mail;
  26. import com.l2jserver.gameserver.network.SystemMessageId;
  27. import com.l2jserver.gameserver.network.serverpackets.ExNoticePostSent;
  28. import com.l2jserver.gameserver.network.serverpackets.InventoryUpdate;
  29. import com.l2jserver.gameserver.network.serverpackets.ItemList;
  30. import com.l2jserver.gameserver.network.serverpackets.StatusUpdate;
  31. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  32. import static com.l2jserver.gameserver.model.actor.L2Character.ZONE_PEACE;
  33. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.ADENA_ID;
  34. import static com.l2jserver.gameserver.model.itemcontainer.PcInventory.MAX_ADENA;
  35. /**
  36. * @author Migi, DS
  37. */
  38. public final class RequestSendPost extends L2GameClientPacket
  39. {
  40. private static final String _C__D0_66_REQUESTSENDPOST = "[C] D0:66 RequestSendPost";
  41. private static final Logger _log = Logger.getLogger(RequestSendPost.class.getName());
  42. private static final int BATCH_LENGTH = 12; // length of the one item
  43. private static final int MAX_RECV_LENGTH = 16;
  44. private static final int MAX_SUBJ_LENGTH = 128;
  45. private static final int MAX_TEXT_LENGTH = 512;
  46. private static final int MAX_ATTACHMENTS = 8;
  47. private static final int INBOX_SIZE = 240;
  48. private static final int OUTBOX_SIZE = 240;
  49. private static final int MESSAGE_FEE = 100;
  50. private static final int MESSAGE_FEE_PER_SLOT = 1000; // 100 adena message fee + 1000 per each item slot
  51. private String _receiver;
  52. private boolean _isCod;
  53. private String _subject;
  54. private String _text;
  55. private AttachmentItem _items[] = null;
  56. private long _reqAdena;
  57. public RequestSendPost()
  58. {
  59. }
  60. @Override
  61. protected void readImpl()
  62. {
  63. _receiver = readS();
  64. _isCod = readD() == 0 ? false : true;
  65. _subject = readS();
  66. _text = readS();
  67. int attachCount = readD();
  68. if (attachCount < 0
  69. || attachCount > Config.MAX_ITEM_IN_PACKET
  70. || attachCount * BATCH_LENGTH + 8 != _buf.remaining())
  71. return;
  72. if (attachCount > 0)
  73. {
  74. _items = new AttachmentItem[attachCount];
  75. for (int i = 0; i < attachCount; i++)
  76. {
  77. int objectId = readD();
  78. long count = readQ();
  79. if (objectId < 1 || count < 0)
  80. {
  81. _items = null;
  82. return;
  83. }
  84. _items[i] = new AttachmentItem(objectId, count);
  85. }
  86. }
  87. _reqAdena = readQ();
  88. }
  89. @Override
  90. public void runImpl()
  91. {
  92. if (!Config.ALLOW_MAIL)
  93. return;
  94. final L2PcInstance activeChar = getClient().getActiveChar();
  95. if (activeChar == null)
  96. return;
  97. if (!activeChar.getFloodProtectors().getSendMail().tryPerformAction("sendmail"))
  98. {
  99. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_LESS_THAN_MINUTE));
  100. return;
  101. }
  102. if (!Config.ALLOW_ATTACHMENTS)
  103. {
  104. _items = null;
  105. _isCod = false;
  106. _reqAdena = 0;
  107. }
  108. if (!activeChar.getAccessLevel().allowTransaction())
  109. {
  110. activeChar.sendMessage("Transactions are disable for your Access Level.");
  111. return;
  112. }
  113. if (!activeChar.isInsideZone(ZONE_PEACE))
  114. {
  115. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_NOT_IN_PEACE_ZONE));
  116. return;
  117. }
  118. if (activeChar.getActiveTradeList() != null)
  119. {
  120. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_DURING_EXCHANGE));
  121. return;
  122. }
  123. if (activeChar.isEnchanting())
  124. {
  125. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_DURING_ENCHANT));
  126. return;
  127. }
  128. if (activeChar.getPrivateStoreType() > 0)
  129. {
  130. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_PRIVATE_STORE));
  131. return;
  132. }
  133. if (_receiver.length() > MAX_RECV_LENGTH)
  134. {
  135. activeChar.sendPacket(new SystemMessage(SystemMessageId.ALLOWED_LENGTH_FOR_RECIPIENT_EXCEEDED));
  136. return;
  137. }
  138. if (_subject.length() > MAX_SUBJ_LENGTH)
  139. {
  140. activeChar.sendPacket(new SystemMessage(SystemMessageId.ALLOWED_LENGTH_FOR_TITLE_EXCEEDED));
  141. return;
  142. }
  143. if (_text.length() > MAX_TEXT_LENGTH)
  144. {
  145. // not found message for this
  146. activeChar.sendPacket(new SystemMessage(SystemMessageId.ALLOWED_LENGTH_FOR_TITLE_EXCEEDED));
  147. return;
  148. }
  149. if (_items != null && _items.length > MAX_ATTACHMENTS)
  150. {
  151. activeChar.sendPacket(new SystemMessage(SystemMessageId.ITEM_SELECTION_POSSIBLE_UP_TO_8));
  152. return;
  153. }
  154. if (_reqAdena < 0 || _reqAdena > MAX_ADENA)
  155. return;
  156. if (_isCod)
  157. {
  158. if (_reqAdena == 0)
  159. {
  160. activeChar.sendPacket(new SystemMessage(SystemMessageId.PAYMENT_AMOUNT_NOT_ENTERED));
  161. return;
  162. }
  163. if (_items == null || _items.length == 0)
  164. {
  165. activeChar.sendPacket(new SystemMessage(SystemMessageId.PAYMENT_REQUEST_NO_ITEM));
  166. return;
  167. }
  168. }
  169. final int receiverId = CharNameTable.getInstance().getIdByName(_receiver);
  170. if (receiverId <= 0)
  171. {
  172. activeChar.sendPacket(new SystemMessage(SystemMessageId.RECIPIENT_NOT_EXIST));
  173. return;
  174. }
  175. if (receiverId == activeChar.getObjectId())
  176. {
  177. activeChar.sendPacket(new SystemMessage(SystemMessageId.YOU_CANT_SEND_MAIL_TO_YOURSELF));
  178. return;
  179. }
  180. L2AccessLevel accessLevel;
  181. final int level = CharNameTable.getInstance().getAccessLevelById(receiverId);
  182. if (level == AccessLevels._masterAccessLevelNum)
  183. accessLevel = AccessLevels._masterAccessLevel;
  184. else if (level == AccessLevels._userAccessLevelNum)
  185. {
  186. accessLevel = AccessLevels._userAccessLevel;
  187. }
  188. else
  189. {
  190. accessLevel = AccessLevels.getInstance().getAccessLevel(level);
  191. if (accessLevel == null)
  192. accessLevel = AccessLevels._userAccessLevel;
  193. }
  194. if (accessLevel.isGm() && !activeChar.getAccessLevel().isGm())
  195. {
  196. SystemMessage sm = new SystemMessage(SystemMessageId.CANNOT_MAIL_GM_C1);
  197. sm.addString(_receiver);
  198. activeChar.sendPacket(sm);
  199. return;
  200. }
  201. if (MailManager.getInstance().getOutboxSize(activeChar.getObjectId()) >= OUTBOX_SIZE)
  202. {
  203. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_MAIL_LIMIT_EXCEEDED));
  204. return;
  205. }
  206. if (MailManager.getInstance().getInboxSize(receiverId) >= INBOX_SIZE)
  207. {
  208. activeChar.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_MAIL_LIMIT_EXCEEDED));
  209. return;
  210. }
  211. Message msg = new Message(activeChar.getObjectId(), receiverId, _isCod, _subject, _text, _reqAdena);
  212. if (removeItems(activeChar, msg))
  213. {
  214. MailManager.getInstance().sendMessage(msg);
  215. activeChar.sendPacket(new ExNoticePostSent(true));
  216. activeChar.sendPacket(new SystemMessage(SystemMessageId.MAIL_SUCCESSFULLY_SENT));
  217. }
  218. }
  219. private final boolean removeItems(L2PcInstance player, Message msg)
  220. {
  221. long currentAdena = player.getAdena();
  222. long fee = MESSAGE_FEE;
  223. if (_items != null)
  224. {
  225. for (AttachmentItem i : _items)
  226. {
  227. // Check validity of requested item
  228. L2ItemInstance item = player.checkItemManipulation(i.getObjectId(), i.getCount(), "attach");
  229. if (item == null || !item.isTradeable() || item.isEquipped())
  230. {
  231. player.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_BAD_ITEM));
  232. return false;
  233. }
  234. fee += MESSAGE_FEE_PER_SLOT;
  235. if (item.getItemId() == ADENA_ID)
  236. currentAdena -= i.getCount();
  237. }
  238. }
  239. // Check if enough adena and charge the fee
  240. if (currentAdena < fee || !player.reduceAdena("MailFee", fee, null, false))
  241. {
  242. player.sendPacket(new SystemMessage(SystemMessageId.CANT_FORWARD_NO_ADENA));
  243. return false;
  244. }
  245. if (_items == null)
  246. return true;
  247. Mail attachments = msg.createAttachments();
  248. // message already has attachments ? oO
  249. if (attachments == null)
  250. return false;
  251. // Proceed to the transfer
  252. InventoryUpdate playerIU = Config.FORCE_INVENTORY_UPDATE ? null : new InventoryUpdate();
  253. for (AttachmentItem i : _items)
  254. {
  255. // Check validity of requested item
  256. L2ItemInstance oldItem = player.checkItemManipulation(i.getObjectId(), i.getCount(), "attach");
  257. if (oldItem == null || !oldItem.isTradeable() || oldItem.isEquipped())
  258. {
  259. _log.warning("Error adding attachment for char "+player.getName()+" (olditem == null)");
  260. return false;
  261. }
  262. final L2ItemInstance newItem = player.getInventory().transferItem("SendMail", i.getObjectId(), i.getCount(), attachments, player, null);
  263. if (newItem == null)
  264. {
  265. _log.warning("Error adding attachment for char "+player.getName()+" (newitem == null)");
  266. continue;
  267. }
  268. newItem.setLocation(newItem.getLocation(), msg.getId());
  269. if (playerIU != null)
  270. {
  271. if (oldItem.getCount() > 0 && oldItem != newItem)
  272. playerIU.addModifiedItem(oldItem);
  273. else
  274. playerIU.addRemovedItem(oldItem);
  275. }
  276. }
  277. // Send updated item list to the player
  278. if (playerIU != null)
  279. player.sendPacket(playerIU);
  280. else
  281. player.sendPacket(new ItemList(player, false));
  282. // Update current load status on player
  283. StatusUpdate su = new StatusUpdate(player.getObjectId());
  284. su.addAttribute(StatusUpdate.CUR_LOAD, player.getCurrentLoad());
  285. player.sendPacket(su);
  286. return true;
  287. }
  288. private class AttachmentItem
  289. {
  290. private final int _objectId;
  291. private final long _count;
  292. public AttachmentItem(int id, long num)
  293. {
  294. _objectId = id;
  295. _count = num;
  296. }
  297. public int getObjectId()
  298. {
  299. return _objectId;
  300. }
  301. public long getCount()
  302. {
  303. return _count;
  304. }
  305. }
  306. @Override
  307. public String getType()
  308. {
  309. return _C__D0_66_REQUESTSENDPOST;
  310. }
  311. }