RequestSendPost.java 11 KB

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