2
0

MailSystem.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.loginserver.mail;
  16. import java.io.BufferedInputStream;
  17. import java.io.File;
  18. import java.io.FileInputStream;
  19. import java.io.IOException;
  20. import java.util.Map;
  21. import java.util.logging.Level;
  22. import java.util.logging.Logger;
  23. import javax.xml.parsers.DocumentBuilderFactory;
  24. import javolution.util.FastMap;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Node;
  27. import com.l2jserver.Config;
  28. /**
  29. * @author mrTJO
  30. */
  31. public class MailSystem
  32. {
  33. private static final Logger _log = Logger.getLogger(MailSystem.class.getName());
  34. private final Map<String, MailContent> _mailData = new FastMap<String, MailContent>();
  35. public static MailSystem getInstance()
  36. {
  37. return SingletonHolder._instance;
  38. }
  39. public MailSystem()
  40. {
  41. loadMails();
  42. }
  43. public void sendMail(String account, String messageId, String... args)
  44. {
  45. BaseMail mail = new BaseMail(account, messageId, args);
  46. mail.run();
  47. }
  48. private void loadMails()
  49. {
  50. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  51. factory.setValidating(false);
  52. factory.setIgnoringComments(true);
  53. File file = new File(Config.DATAPACK_ROOT, "data/mail/MailList.xml");
  54. Document doc = null;
  55. if (file.exists())
  56. {
  57. try
  58. {
  59. doc = factory.newDocumentBuilder().parse(file);
  60. }
  61. catch (Exception e)
  62. {
  63. _log.log(Level.WARNING, "Could not parse MailList.xml file: " + e.getMessage(), e);
  64. return;
  65. }
  66. Node n = doc.getFirstChild();
  67. File mailFile;
  68. for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
  69. {
  70. if (d.getNodeName().equals("mail"))
  71. {
  72. String mailId = d.getAttributes().getNamedItem("id").getNodeValue();
  73. String subject = d.getAttributes().getNamedItem("subject").getNodeValue();
  74. String maFile = d.getAttributes().getNamedItem("file").getNodeValue();
  75. mailFile = new File(Config.DATAPACK_ROOT, "data/mail/" + maFile);
  76. try (FileInputStream fis = new FileInputStream(mailFile); BufferedInputStream bis = new BufferedInputStream(fis);)
  77. {
  78. int bytes = bis.available();
  79. byte[] raw = new byte[bytes];
  80. bis.read(raw);
  81. String html = new String(raw, "UTF-8");
  82. html = html.replaceAll("\r\n", "\n");
  83. html = html.replace("%servermail%", Config.EMAIL_SERVERINFO_ADDRESS);
  84. html = html.replace("%servername%", Config.EMAIL_SERVERINFO_NAME);
  85. _mailData.put(mailId, new MailContent(subject, html));
  86. }
  87. catch (IOException e)
  88. {
  89. _log.warning("IOException while reading " + maFile);
  90. }
  91. }
  92. }
  93. _log.info("eMail System Loaded");
  94. }
  95. else
  96. {
  97. _log.warning("Cannot load eMail System - Missing file MailList.xml");
  98. }
  99. }
  100. public class MailContent
  101. {
  102. private final String _subject;
  103. private final String _text;
  104. /**
  105. * @param subject
  106. * @param text
  107. */
  108. public MailContent(String subject, String text)
  109. {
  110. _subject = subject;
  111. _text = text;
  112. }
  113. public String getSubject()
  114. {
  115. return _subject;
  116. }
  117. public String getText()
  118. {
  119. return _text;
  120. }
  121. }
  122. public MailContent getMailContent(String mailId)
  123. {
  124. return _mailData.get(mailId);
  125. }
  126. private static class SingletonHolder
  127. {
  128. protected static final MailSystem _instance = new MailSystem();
  129. }
  130. }