2
0

MailManager.java 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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.instancemanager;
  16. import java.sql.Connection;
  17. import java.sql.PreparedStatement;
  18. import java.sql.ResultSet;
  19. import java.sql.SQLException;
  20. import java.util.List;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javolution.util.FastList;
  24. import com.l2jserver.L2DatabaseFactory;
  25. import com.l2jserver.gameserver.ThreadPoolManager;
  26. import com.l2jserver.gameserver.idfactory.IdFactory;
  27. import com.l2jserver.gameserver.model.L2World;
  28. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  29. import com.l2jserver.gameserver.model.entity.Message;
  30. import com.l2jserver.gameserver.network.SystemMessageId;
  31. import com.l2jserver.gameserver.network.serverpackets.ExNoticePostArrived;
  32. import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
  33. import com.l2jserver.gameserver.util.L2TIntObjectHashMap;
  34. /**
  35. * @author Migi, DS<br>
  36. */
  37. public class MailManager
  38. {
  39. private static Logger _log = Logger.getLogger(MailManager.class.getName());
  40. private L2TIntObjectHashMap<Message> _messages = new L2TIntObjectHashMap<>();
  41. public static MailManager getInstance()
  42. {
  43. return SingletonHolder._instance;
  44. }
  45. protected MailManager()
  46. {
  47. load();
  48. }
  49. private void load()
  50. {
  51. int count = 0;
  52. Connection con = null;
  53. try
  54. {
  55. con = L2DatabaseFactory.getInstance().getConnection();
  56. PreparedStatement statement = con.prepareStatement("SELECT * FROM messages ORDER BY expiration");
  57. // stmt2 = con.prepareStatement("SELECT * FROM attachments WHERE messageId = ?");
  58. ResultSet rset1 = statement.executeQuery();
  59. while (rset1.next())
  60. {
  61. Message msg = new Message(rset1);
  62. int msgId = msg.getId();
  63. _messages.put(msgId, msg);
  64. count++;
  65. long expiration = msg.getExpiration();
  66. if (expiration < System.currentTimeMillis())
  67. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), 10000);
  68. else
  69. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msgId), expiration - System.currentTimeMillis());
  70. }
  71. rset1.close();
  72. statement.close();
  73. }
  74. catch (SQLException e)
  75. {
  76. _log.log(Level.WARNING, "Mail Manager: Error loading from database:" + e.getMessage(), e);
  77. }
  78. finally
  79. {
  80. L2DatabaseFactory.close(con);
  81. }
  82. _log.info("Mail Manager: Successfully loaded " + count + " messages.");
  83. }
  84. public final Message getMessage(int msgId)
  85. {
  86. return _messages.get(msgId);
  87. }
  88. public final Message[] getMessages()
  89. {
  90. return _messages.values(new Message[0]);
  91. }
  92. public final boolean hasUnreadPost(L2PcInstance player)
  93. {
  94. final int objectId = player.getObjectId();
  95. for (Message msg : getMessages())
  96. {
  97. if (msg != null
  98. && msg.getReceiverId() == objectId
  99. && msg.isUnread())
  100. return true;
  101. }
  102. return false;
  103. }
  104. public final int getInboxSize(int objectId)
  105. {
  106. int size = 0;
  107. for (Message msg : getMessages())
  108. {
  109. if (msg != null
  110. && msg.getReceiverId() == objectId
  111. && !msg.isDeletedByReceiver())
  112. size++;
  113. }
  114. return size;
  115. }
  116. public final int getOutboxSize(int objectId)
  117. {
  118. int size = 0;
  119. for (Message msg : getMessages())
  120. {
  121. if (msg != null
  122. && msg.getSenderId() == objectId
  123. && !msg.isDeletedBySender())
  124. size++;
  125. }
  126. return size;
  127. }
  128. public final List<Message> getInbox(int objectId)
  129. {
  130. List<Message> inbox = new FastList<>();
  131. for (Message msg : getMessages())
  132. {
  133. if (msg != null
  134. && msg.getReceiverId() == objectId
  135. && !msg.isDeletedByReceiver())
  136. inbox.add(msg);
  137. }
  138. return inbox;
  139. }
  140. public final List<Message> getOutbox(int objectId)
  141. {
  142. List<Message> outbox = new FastList<>();
  143. for (Message msg : getMessages())
  144. {
  145. if (msg != null
  146. && msg.getSenderId() == objectId
  147. && !msg.isDeletedBySender())
  148. outbox.add(msg);
  149. }
  150. return outbox;
  151. }
  152. public void sendMessage(Message msg)
  153. {
  154. _messages.put(msg.getId(), msg);
  155. Connection con = null;
  156. try
  157. {
  158. con = L2DatabaseFactory.getInstance().getConnection();
  159. PreparedStatement stmt = Message.getStatement(msg, con);
  160. stmt.execute();
  161. stmt.close();
  162. }
  163. catch (SQLException e)
  164. {
  165. _log.log(Level.WARNING, "Mail Manager: Error saving message:" + e.getMessage(), e);
  166. }
  167. finally
  168. {
  169. L2DatabaseFactory.close(con);
  170. }
  171. final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
  172. if (receiver != null)
  173. receiver.sendPacket(ExNoticePostArrived.valueOf(true));
  174. ThreadPoolManager.getInstance().scheduleGeneral(new MessageDeletionTask(msg.getId()), msg.getExpiration() - System.currentTimeMillis());
  175. }
  176. private class MessageDeletionTask implements Runnable
  177. {
  178. private final Logger _log = Logger.getLogger(MessageDeletionTask.class.getName());
  179. final int _msgId;
  180. public MessageDeletionTask(int msgId)
  181. {
  182. _msgId = msgId;
  183. }
  184. @Override
  185. public void run()
  186. {
  187. final Message msg = getMessage(_msgId);
  188. if (msg == null)
  189. return;
  190. if (msg.hasAttachments())
  191. {
  192. try
  193. {
  194. final L2PcInstance sender = L2World.getInstance().getPlayer(msg.getSenderId());
  195. if (sender != null)
  196. {
  197. msg.getAttachments().returnToWh(sender.getWarehouse());
  198. sender.sendPacket(SystemMessageId.MAIL_RETURNED);
  199. }
  200. else
  201. msg.getAttachments().returnToWh(null);
  202. msg.getAttachments().deleteMe();
  203. msg.removeAttachments();
  204. final L2PcInstance receiver = L2World.getInstance().getPlayer(msg.getReceiverId());
  205. if (receiver != null)
  206. {
  207. SystemMessage sm = SystemMessage.getSystemMessage(SystemMessageId.MAIL_RETURNED);
  208. //sm.addString(msg.getReceiverName());
  209. receiver.sendPacket(sm);
  210. }
  211. }
  212. catch (Exception e)
  213. {
  214. _log.log(Level.WARNING, "Mail Manager: Error returning items:" + e.getMessage(), e);
  215. }
  216. }
  217. deleteMessageInDb(msg.getId());
  218. }
  219. }
  220. public final void markAsReadInDb(int msgId)
  221. {
  222. Connection con = null;
  223. try
  224. {
  225. con = L2DatabaseFactory.getInstance().getConnection();
  226. PreparedStatement stmt = con.prepareStatement("UPDATE messages SET isUnread = 'false' WHERE messageId = ?");
  227. stmt.setInt(1, msgId);
  228. stmt.execute();
  229. stmt.close();
  230. }
  231. catch (SQLException e)
  232. {
  233. _log.log(Level.WARNING, "Mail Manager: Error marking as read message:" + e.getMessage(), e);
  234. }
  235. finally
  236. {
  237. L2DatabaseFactory.close(con);
  238. }
  239. }
  240. public final void markAsDeletedBySenderInDb(int msgId)
  241. {
  242. Connection con = null;
  243. PreparedStatement stmt = null;
  244. try
  245. {
  246. con = L2DatabaseFactory.getInstance().getConnection();
  247. stmt = con.prepareStatement("UPDATE messages SET isDeletedBySender = 'true' WHERE messageId = ?");
  248. stmt.setInt(1, msgId);
  249. stmt.execute();
  250. stmt.close();
  251. }
  252. catch (SQLException e)
  253. {
  254. _log.log(Level.WARNING, "Mail Manager: Error marking as deleted by sender message:" + e.getMessage(), e);
  255. }
  256. finally
  257. {
  258. L2DatabaseFactory.close(con);
  259. }
  260. }
  261. public final void markAsDeletedByReceiverInDb(int msgId)
  262. {
  263. Connection con = null;
  264. PreparedStatement stmt = null;
  265. try
  266. {
  267. con = L2DatabaseFactory.getInstance().getConnection();
  268. stmt = con.prepareStatement("UPDATE messages SET isDeletedByReceiver = 'true' WHERE messageId = ?");
  269. stmt.setInt(1, msgId);
  270. stmt.execute();
  271. stmt.close();
  272. }
  273. catch (SQLException e)
  274. {
  275. _log.log(Level.WARNING, "Mail Manager: Error marking as deleted by receiver message:" + e.getMessage(), e);
  276. }
  277. finally
  278. {
  279. L2DatabaseFactory.close(con);
  280. }
  281. }
  282. public final void removeAttachmentsInDb(int msgId)
  283. {
  284. Connection con = null;
  285. try
  286. {
  287. con = L2DatabaseFactory.getInstance().getConnection();
  288. PreparedStatement stmt = con.prepareStatement("UPDATE messages SET hasAttachments = 'false' WHERE messageId = ?");
  289. stmt.setInt(1, msgId);
  290. stmt.execute();
  291. stmt.close();
  292. }
  293. catch (SQLException e)
  294. {
  295. _log.log(Level.WARNING, "Mail Manager: Error removing attachments in message:" + e.getMessage(), e);
  296. }
  297. finally
  298. {
  299. L2DatabaseFactory.close(con);
  300. }
  301. }
  302. public final void deleteMessageInDb(int msgId)
  303. {
  304. Connection con = null;
  305. try
  306. {
  307. con = L2DatabaseFactory.getInstance().getConnection();
  308. PreparedStatement stmt = con.prepareStatement("DELETE FROM messages WHERE messageId = ?");
  309. stmt.setInt(1, msgId);
  310. stmt.execute();
  311. stmt.close();
  312. }
  313. catch (SQLException e)
  314. {
  315. _log.log(Level.WARNING, "Mail Manager: Error deleting message:" + e.getMessage(), e);
  316. }
  317. finally
  318. {
  319. L2DatabaseFactory.close(con);
  320. }
  321. _messages.remove(msgId);
  322. IdFactory.getInstance().releaseId(msgId);
  323. }
  324. private static class SingletonHolder
  325. {
  326. protected static final MailManager _instance = new MailManager();
  327. }
  328. }