MailManager.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (C) 2004-2015 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.instancemanager;
  20. import java.sql.Connection;
  21. import java.sql.PreparedStatement;
  22. import java.sql.ResultSet;
  23. import java.sql.SQLException;
  24. import java.sql.Statement;
  25. import java.util.ArrayList;
  26. import java.util.Collection;
  27. import java.util.List;
  28. import java.util.Map;
  29. import java.util.concurrent.ConcurrentHashMap;
  30. import java.util.logging.Level;
  31. import java.util.logging.Logger;
  32. import com.l2jserver.commons.database.pool.impl.ConnectionFactory;
  33. import com.l2jserver.gameserver.ThreadPoolManager;
  34. import com.l2jserver.gameserver.idfactory.IdFactory;
  35. import com.l2jserver.gameserver.instancemanager.tasks.MessageDeletionTask;
  36. import com.l2jserver.gameserver.model.L2World;
  37. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  38. import com.l2jserver.gameserver.model.entity.Message;
  39. import com.l2jserver.gameserver.network.serverpackets.ExNoticePostArrived;
  40. /**
  41. * @author Migi, DS
  42. */
  43. public final class MailManager
  44. {
  45. private static final Logger _log = Logger.getLogger(MailManager.class.getName());
  46. private final Map<Integer, Message> _messages = new ConcurrentHashMap<>();
  47. protected MailManager()
  48. {
  49. load();
  50. }
  51. private void load()
  52. {
  53. int count = 0;
  54. try (Connection con = ConnectionFactory.getInstance().getConnection();
  55. Statement ps = con.createStatement();
  56. ResultSet rs = ps.executeQuery("SELECT * FROM messages ORDER BY expiration"))
  57. {
  58. while (rs.next())
  59. {
  60. final Message msg = new Message(rs);
  61. int msgId = msg.getId();
  62. _messages.put(msgId, msg);
  63. count++;
  64. long expiration = msg.getExpiration();
  65. if (expiration < System.currentTimeMillis())
  66. {
  67. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), 10000);
  68. }
  69. else
  70. {
  71. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), expiration - System.currentTimeMillis());
  72. }
  73. }
  74. }
  75. catch (SQLException e)
  76. {
  77. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error loading from database:" + e.getMessage(), e);
  78. }
  79. _log.info(getClass().getSimpleName() + ": Successfully loaded " + count + " messages.");
  80. }
  81. public final Message getMessage(int msgId)
  82. {
  83. return _messages.get(msgId);
  84. }
  85. public final Collection<Message> getMessages()
  86. {
  87. return _messages.values();
  88. }
  89. public final boolean hasUnreadPost(L2PcInstance player)
  90. {
  91. final int objectId = player.getObjectId();
  92. for (Message msg : getMessages())
  93. {
  94. if ((msg != null) && (msg.getReceiverId() == objectId) && msg.isUnread())
  95. {
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. public final int getInboxSize(int objectId)
  102. {
  103. int size = 0;
  104. for (Message msg : getMessages())
  105. {
  106. if ((msg != null) && (msg.getReceiverId() == objectId) && !msg.isDeletedByReceiver())
  107. {
  108. size++;
  109. }
  110. }
  111. return size;
  112. }
  113. public final int getOutboxSize(int objectId)
  114. {
  115. int size = 0;
  116. for (Message msg : getMessages())
  117. {
  118. if ((msg != null) && (msg.getSenderId() == objectId) && !msg.isDeletedBySender())
  119. {
  120. size++;
  121. }
  122. }
  123. return size;
  124. }
  125. public final List<Message> getInbox(int objectId)
  126. {
  127. final List<Message> inbox = new ArrayList<>();
  128. for (Message msg : getMessages())
  129. {
  130. if ((msg != null) && (msg.getReceiverId() == objectId) && !msg.isDeletedByReceiver())
  131. {
  132. inbox.add(msg);
  133. }
  134. }
  135. return inbox;
  136. }
  137. public final List<Message> getOutbox(int objectId)
  138. {
  139. final List<Message> outbox = new ArrayList<>();
  140. for (Message msg : getMessages())
  141. {
  142. if ((msg != null) && (msg.getSenderId() == objectId) && !msg.isDeletedBySender())
  143. {
  144. outbox.add(msg);
  145. }
  146. }
  147. return outbox;
  148. }
  149. public void sendMessage(Message msg)
  150. {
  151. _messages.put(msg.getId(), msg);
  152. try (Connection con = ConnectionFactory.getInstance().getConnection();
  153. PreparedStatement ps = Message.getStatement(msg, con))
  154. {
  155. ps.execute();
  156. }
  157. catch (SQLException e)
  158. {
  159. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error saving message:" + e.getMessage(), e);
  160. }
  161. final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
  162. if (receiver != null)
  163. {
  164. receiver.sendPacket(ExNoticePostArrived.valueOf(true));
  165. }
  166. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msg.getId()), msg.getExpiration() - System.currentTimeMillis());
  167. }
  168. public final void markAsReadInDb(int msgId)
  169. {
  170. try (Connection con = ConnectionFactory.getInstance().getConnection();
  171. PreparedStatement ps = con.prepareStatement("UPDATE messages SET isUnread = 'false' WHERE messageId = ?"))
  172. {
  173. ps.setInt(1, msgId);
  174. ps.execute();
  175. }
  176. catch (SQLException e)
  177. {
  178. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as read message:" + e.getMessage(), e);
  179. }
  180. }
  181. public final void markAsDeletedBySenderInDb(int msgId)
  182. {
  183. try (Connection con = ConnectionFactory.getInstance().getConnection();
  184. PreparedStatement ps = con.prepareStatement("UPDATE messages SET isDeletedBySender = 'true' WHERE messageId = ?"))
  185. {
  186. ps.setInt(1, msgId);
  187. ps.execute();
  188. }
  189. catch (SQLException e)
  190. {
  191. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as deleted by sender message:" + e.getMessage(), e);
  192. }
  193. }
  194. public final void markAsDeletedByReceiverInDb(int msgId)
  195. {
  196. try (Connection con = ConnectionFactory.getInstance().getConnection();
  197. PreparedStatement ps = con.prepareStatement("UPDATE messages SET isDeletedByReceiver = 'true' WHERE messageId = ?"))
  198. {
  199. ps.setInt(1, msgId);
  200. ps.execute();
  201. }
  202. catch (SQLException e)
  203. {
  204. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error marking as deleted by receiver message:" + e.getMessage(), e);
  205. }
  206. }
  207. public final void removeAttachmentsInDb(int msgId)
  208. {
  209. try (Connection con = ConnectionFactory.getInstance().getConnection();
  210. PreparedStatement ps = con.prepareStatement("UPDATE messages SET hasAttachments = 'false' WHERE messageId = ?"))
  211. {
  212. ps.setInt(1, msgId);
  213. ps.execute();
  214. }
  215. catch (SQLException e)
  216. {
  217. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error removing attachments in message:" + e.getMessage(), e);
  218. }
  219. }
  220. public final void deleteMessageInDb(int msgId)
  221. {
  222. try (Connection con = ConnectionFactory.getInstance().getConnection();
  223. PreparedStatement ps = con.prepareStatement("DELETE FROM messages WHERE messageId = ?"))
  224. {
  225. ps.setInt(1, msgId);
  226. ps.execute();
  227. }
  228. catch (SQLException e)
  229. {
  230. _log.log(Level.WARNING, getClass().getSimpleName() + ": Error deleting message:" + e.getMessage(), e);
  231. }
  232. _messages.remove(msgId);
  233. IdFactory.getInstance().releaseId(msgId);
  234. }
  235. /**
  236. * Gets the single instance of {@code MailManager}.
  237. * @return single instance of {@code MailManager}
  238. */
  239. public static MailManager getInstance()
  240. {
  241. return SingletonHolder._instance;
  242. }
  243. private static class SingletonHolder
  244. {
  245. protected static final MailManager _instance = new MailManager();
  246. }
  247. }