From d7668dadaa97caeed85f43fbe80d2428fed7fc93 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 19 Apr 2022 12:09:26 +0200 Subject: [PATCH 1/5] moved imap credentials from MailingList to ImapClient --- doc/data structure.dia | 85 ++++++++++++++----- pom.xml | 2 +- .../de/srsoftware/widerhall/Constants.java | 2 +- .../widerhall/data/MailingList.java | 34 ++++---- .../srsoftware/widerhall/mail/Archiver.java | 26 ++++++ .../srsoftware/widerhall/mail/Forwarder.java | 6 -- .../srsoftware/widerhall/mail/ImapClient.java | 53 +++++++++--- .../java/de/srsoftware/widerhall/web/Web.java | 13 +-- static/templates/add_list.st | 4 + static/templates/js.st | 1 + static/templates/listadminlist.st | 3 +- 11 files changed, 160 insertions(+), 69 deletions(-) create mode 100644 src/main/java/de/srsoftware/widerhall/mail/Archiver.java diff --git a/doc/data structure.dia b/doc/data structure.dia index ec54437..e887b44 100644 --- a/doc/data structure.dia +++ b/doc/data structure.dia @@ -868,13 +868,13 @@ - + - + - + @@ -900,7 +900,7 @@ - + @@ -913,13 +913,13 @@ - + - + - + @@ -945,7 +945,7 @@ - + @@ -958,13 +958,13 @@ - + - + - + @@ -990,7 +990,7 @@ - + @@ -1003,13 +1003,13 @@ - + - + - + @@ -1035,7 +1035,7 @@ - + @@ -1562,13 +1562,13 @@ - + - + - + @@ -1594,7 +1594,7 @@ - + @@ -1650,5 +1650,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + #inbox# + + + + + + + + + + + + + + + + + + + diff --git a/pom.xml b/pom.xml index 3f3cf2f..8d6f2ca 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.0.13 + 0.0.14 diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java index 1acd74d..22ea782 100644 --- a/src/main/java/de/srsoftware/widerhall/Constants.java +++ b/src/main/java/de/srsoftware/widerhall/Constants.java @@ -5,10 +5,10 @@ public class Constants { public static final String BASE_URL = "base_url"; public static final String CONFIG = "configuration"; public static final String DB = "database"; + public static final String DEFAULT_INBOX = "INBOX"; public static final String DOMAIN = "domain"; public static final String EMAIL = "email"; public static final String ERROR = "error"; - public static final String HOST = "host"; public static final String IMAPS = "imaps"; public static final String INBOX = "inbox"; public static final String INDEX = "index"; diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index a13a38b..c6845e4 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -1,6 +1,7 @@ package de.srsoftware.widerhall.data; import de.srsoftware.widerhall.Configuration; +import de.srsoftware.widerhall.mail.ImapClient; import de.srsoftware.widerhall.mail.SmtpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,10 +35,9 @@ public class MailingList { private static final int STATE_PUBLIC = 2; private final String name; private final String email; - private final String imapPass, imapHost, imapUser; - private final int imapPort; private int state; private final SmtpClient smtp; + private final ImapClient imap; private static final HashMap lists = new HashMap<>(); @@ -55,15 +55,12 @@ public class MailingList { * @param smtpPass * @param state */ - public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state) { + public MailingList(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String inbox, String smtpHost, int smtpPort, String smtpUser, String smtpPass, int state) { this.email = email; this.name = name; - this.imapHost = imapHost; - this.imapPort = imapPort; - this.imapUser = imapUser; - this.imapPass = imapPass; this.state = state; this.smtp = new SmtpClient(smtpHost,smtpPort,smtpUser,smtpPass); + this.imap = new ImapClient(imapHost,imapPort,imapUser,imapPass,inbox); } /** @@ -81,8 +78,8 @@ public class MailingList { * @return * @throws SQLException */ - public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException { - return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass, STATE_PENDING).save(); + public static MailingList create(String email, String name, String imapHost, int imapPort, String imapUser, String imapPass, String inbox, String smtpHost, int smtpPort, String smtpUser, String smtpPass) throws SQLException { + return new MailingList(email, name, imapHost, imapPort, imapUser, imapPass, inbox, smtpHost, smtpPort, smtpUser, smtpPass, STATE_PENDING).save(); } /** @@ -99,6 +96,7 @@ public class MailingList { .append(IMAP_PORT).append(" ").append(INT).append(", ") .append(IMAP_USER).append(" ").append(VARCHAR).append(", ") .append(IMAP_PASS).append(" ").append(VARCHAR).append(", ") + .append(INBOX).append(" ").append(VARCHAR).append(", ") .append(SMTP_HOST).append(" ").append(VARCHAR).append(", ") .append(SMTP_PORT).append(" ").append(INT).append(", ") .append(SMTP_USER).append(" ").append(VARCHAR).append(", ") @@ -168,6 +166,7 @@ public class MailingList { rs.getInt(IMAP_PORT), rs.getString(IMAP_USER), rs.getString(IMAP_PASS), + rs.getString(INBOX), rs.getString(SMTP_HOST), rs.getInt(SMTP_PORT), rs.getString(SMTP_USER), @@ -241,12 +240,14 @@ public class MailingList { */ public Map safeMap() { var map = minimalMap(); - if (imapHost != null) map.put(IMAP_HOST, imapHost); - if (imapPort != 0) map.put(IMAP_PORT, imapPort); - if (imapUser != null) map.put(IMAP_USER, imapUser); + if (imap.host() != null) map.put(IMAP_HOST, imap.host()); + if (imap.port() != 0) map.put(IMAP_PORT, imap.port()); + if (imap.username() != null) map.put(IMAP_USER, imap.username()); + if (imap.folderName() != null) map.put(INBOX,imap.folderName()); if (smtp.host() != null) map.put(SMTP_HOST, smtp.host()); if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port()); if (smtp.username() != null) map.put(SMTP_USER, smtp.username()); + map.put(STATE,stateString(state)); return map; } @@ -261,10 +262,11 @@ public class MailingList { .insertInto(TABLE_NAME) .set(EMAIL, email) .set(NAME, name) - .set(IMAP_HOST, imapHost) - .set(IMAP_PORT, imapPort) - .set(IMAP_USER, imapUser) - .set(IMAP_PASS, imapPass) + .set(IMAP_HOST, imap.host()) + .set(IMAP_PORT, imap.port()) + .set(IMAP_USER, imap.username()) + .set(IMAP_PASS, imap.password()) + .set(INBOX,imap.folderName()) .set(SMTP_HOST, smtp.host()) .set(SMTP_PORT, smtp.port()) .set(SMTP_USER, smtp.username()) diff --git a/src/main/java/de/srsoftware/widerhall/mail/Archiver.java b/src/main/java/de/srsoftware/widerhall/mail/Archiver.java new file mode 100644 index 0000000..0bb8c07 --- /dev/null +++ b/src/main/java/de/srsoftware/widerhall/mail/Archiver.java @@ -0,0 +1,26 @@ +package de.srsoftware.widerhall.mail; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.mail.Message; +import javax.mail.MessagingException; +import java.io.UnsupportedEncodingException; + +public class Archiver implements MessageHandler { + private static final Logger LOG = LoggerFactory.getLogger(Archiver.class); + private final SmtpClient smtp; + private final String receiver,sender; + + public Archiver(String host, int port, String username, String password, String sender, String receiver) { + this.sender = sender; + this.receiver = receiver; + SmtpClient smtp = new SmtpClient(host,port,username,password); + this.smtp = smtp; + } + + @Override + public void onMessageReceived(Message message) throws MessagingException { + LOG.debug("storing {}",message.getSubject()); + } +} diff --git a/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java b/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java index 0cca288..4dca6bd 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java +++ b/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java @@ -23,11 +23,5 @@ public class Forwarder implements MessageHandler { @Override public void onMessageReceived(Message message) throws MessagingException { LOG.debug("forwarding {}",message.getSubject()); - - try { - smtp.send(sender,"Stephan Richter",receiver,"Info: "+message.getSubject(),"Neue Mail eingegangen!"); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } } } diff --git a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java index 082402b..1a5c009 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java +++ b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java @@ -13,13 +13,12 @@ import java.util.Properties; public class ImapClient { private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class); - private final JSONObject config; - private boolean stopped = false; + private final int port; + private final String host, username, password, folderName; + private boolean stopped = true; private IMAPFolder inbox; private HashSet listeners = new HashSet<>(); - - private class ListeningThread extends Thread { private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class); @@ -41,10 +40,6 @@ public class ImapClient { } private void openInbox() throws MessagingException { - String host = (String) config.get(Constants.HOST); - String username = (String) config.get(Constants.USER); - String password = (String) config.get(Constants.PASSWORD); - String folderName = (String) config.get(Constants.INBOX); LOG.debug("Connecting and logging in…"); Properties props = imapProps(); Session session = Session.getInstance(props); @@ -105,20 +100,50 @@ public class ImapClient { } } } - public ImapClient(JSONObject config){ - this.config = config; + + public ImapClient(String host, int port, String username, String password, String folderName) { + this.host = host; + this.port = port; + this.username = username; + this.password = password; + this.folderName = folderName; + } + + + public ImapClient addListener(MessageHandler messageHandler) { + listeners.add(messageHandler); + return this; + } + + public String host(){ + return host; + } + + public String username(){ + return username; + } + + public String password(){ + return password; + } + + public int port(){ + return port; + } + + public String folderName(){ + return folderName; } public void start() { + stopped = false; LOG.debug("Creating ListeningThread…"); new ListeningThread().start(); LOG.debug("Creating Heartbeat…"); new Heartbeat().start(); } - - public ImapClient addListener(MessageHandler messageHandler) { - listeners.add(messageHandler); - return this; + public void stop(){ + stopped = true; } } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index 8d6b861..a976397 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -1,25 +1,16 @@ package de.srsoftware.widerhall.web; -import de.srsoftware.widerhall.Configuration; -import de.srsoftware.widerhall.Constants; import de.srsoftware.widerhall.Util; import de.srsoftware.widerhall.data.ListMember; import de.srsoftware.widerhall.data.MailingList; import de.srsoftware.widerhall.data.User; -import org.antlr.runtime.MismatchedTokenException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.stringtemplate.v4.STGroup; -import org.stringtemplate.v4.STRawGroupDir; import javax.mail.MessagingException; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.security.InvalidKeyException; import java.sql.SQLException; import java.util.HashMap; @@ -76,6 +67,8 @@ public class Web extends TemplateServlet { data.put(IMAP_USER, imapUser); var imapPass = req.getParameter(IMAP_PASS); var smtpHost = req.getParameter(SMTP_HOST); + var inbox = req.getParameter(INBOX); + if (inbox == null) inbox = DEFAULT_INBOX; data.put(SMTP_HOST, smtpHost); var smtpUser = req.getParameter(SMTP_USER); data.put(SMTP_USER, smtpUser); @@ -125,7 +118,7 @@ public class Web extends TemplateServlet { } try { - var list = MailingList.create(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass); + var list = MailingList.create(email, name, imapHost, imapPort, imapUser, imapPass, inbox, smtpHost, smtpPort, smtpUser, smtpPass); ListMember.create(list, user, ListMember.STATE_OWNER); return redirectTo(INDEX, resp); } catch (SQLException e) { diff --git a/static/templates/add_list.st b/static/templates/add_list.st index f855957..5b20816 100644 --- a/static/templates/add_list.st +++ b/static/templates/add_list.st @@ -43,6 +43,10 @@ Password +
SMTP protocol diff --git a/static/templates/js.st b/static/templates/js.st index 2f3a2b1..8f25513 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -78,6 +78,7 @@ function showListOfEditableLists(data){ $('').text(list.imap_host).appendTo(row); $('').text(list.imap_port).appendTo(row); $('').text(list.imap_user).appendTo(row); + $('').text(list.inbox).appendTo(row); $('').text(list.smtp_host).appendTo(row); $('').text(list.smtp_port).appendTo(row); $('').text(list.smtp_user).appendTo(row); diff --git a/static/templates/listadminlist.st b/static/templates/listadminlist.st index 3a687b6..edcc964 100644 --- a/static/templates/listadminlist.st +++ b/static/templates/listadminlist.st @@ -3,7 +3,7 @@ - + @@ -14,6 +14,7 @@ + From 7b5b6aa701ad0608bb8d7b7b66a0c0dd364035de Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Tue, 19 Apr 2022 13:46:20 +0200 Subject: [PATCH 2/5] first version doiing forward with attached original mail --- pom.xml | 2 +- .../java/de/srsoftware/widerhall/Util.java | 2 + .../widerhall/data/MailingList.java | 103 +++++++++++++----- .../srsoftware/widerhall/mail/Archiver.java | 26 ----- .../srsoftware/widerhall/mail/Forwarder.java | 27 ----- .../srsoftware/widerhall/mail/ImapClient.java | 68 +++++++++--- .../srsoftware/widerhall/mail/SmtpClient.java | 28 ++++- .../java/de/srsoftware/widerhall/web/Web.java | 6 +- static/templates/add_list.st | 2 +- static/templates/frontpage.st | 1 + 10 files changed, 162 insertions(+), 103 deletions(-) delete mode 100644 src/main/java/de/srsoftware/widerhall/mail/Archiver.java delete mode 100644 src/main/java/de/srsoftware/widerhall/mail/Forwarder.java diff --git a/pom.xml b/pom.xml index 8d6f2ca..f63bd10 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.0.14 + 0.0.15 diff --git a/src/main/java/de/srsoftware/widerhall/Util.java b/src/main/java/de/srsoftware/widerhall/Util.java index 829b278..b182ef4 100644 --- a/src/main/java/de/srsoftware/widerhall/Util.java +++ b/src/main/java/de/srsoftware/widerhall/Util.java @@ -2,6 +2,8 @@ package de.srsoftware.widerhall; import de.srsoftware.tools.translations.Translation; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index c6845e4..915f386 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -2,15 +2,22 @@ package de.srsoftware.widerhall.data; import de.srsoftware.widerhall.Configuration; import de.srsoftware.widerhall.mail.ImapClient; +import de.srsoftware.widerhall.mail.MessageHandler; import de.srsoftware.widerhall.mail.SmtpClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.mail.Address; +import javax.mail.Message; import javax.mail.MessagingException; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeMessage; import java.io.UnsupportedEncodingException; import java.sql.ResultSet; import java.sql.SQLException; import java.util.*; +import java.util.stream.Stream; import static de.srsoftware.widerhall.Constants.*; import static de.srsoftware.widerhall.Util.t; @@ -19,7 +26,7 @@ import static de.srsoftware.widerhall.data.User.PERMISSION_ADMIN; /** * this class encapsulates a MailingList db object */ -public class MailingList { +public class MailingList implements MessageHandler { private static final Logger LOG = LoggerFactory.getLogger(MailingList.class); private static final String IMAP_HOST = "imap_host"; private static final String IMAP_PORT = "imap_port"; @@ -106,6 +113,17 @@ public class MailingList { Database.open().query(sql).compile().run(); } + /** + * load the set of mailing lists a given user is allowed to edit + * @param user + * @return + */ + public static Set editableBy(User user) { + var list = new HashSet(); + for (String key : ListMember.listsOwnedBy(user)) list.add(load(key)); + return list; + } + public String email() { return email; } @@ -114,41 +132,23 @@ public class MailingList { public void enable(boolean enable) throws SQLException { state = enable ? state | STATE_ENABLED : state ^ (state & STATE_ENABLED); Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); - } - public void hide(boolean hide) throws SQLException { - state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC; - Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); + if (enable) { + imap.start().addListener(this); + } else { + imap.stop(); + } } - /** - * test, whether the current ML is subscribable by a given user - * @param user - * @return - */ - public boolean isOpenFor(User user) { - if ((state & STATE_PUBLIC) > 0) return true; // all users may subscribe public mailing lists - if (user == null) return false; + private void forward(Message message) throws MessagingException { try { - var member = ListMember.load(this,user); - return member.hasState(ListMember.STATE_OWNER|ListMember.STATE_SUBSCRIBER); // owners may subscribe their own mailing lists + var emails = members().stream().map(User::email).toList(); + smtp.bccForward(email(),message,emails); } catch (SQLException e) { - LOG.warn("Was not able to load ListMember: ",e); - return false; + LOG.error("Failed to read list members of {} from database. Cannot forward message!",email(),e); } } - /** - * load the set of mailing lists a given user is allowed to edit - * @param user - * @return - */ - public static Set editableBy(User user) { - var list = new HashSet(); - for (String key : ListMember.listsOwnedBy(user)) list.add(load(key)); - return list; - } - /** * Create a mailing list object from a ResultSet. * This is a cached method: if the ML has been loaded before, the already-loaded object will be returned. @@ -175,6 +175,28 @@ public class MailingList { return ml; } + public void hide(boolean hide) throws SQLException { + state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC; + Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); + } + + /** + * test, whether the current ML is subscribable by a given user + * @param user + * @return + */ + public boolean isOpenFor(User user) { + if ((state & STATE_PUBLIC) > 0) return true; // all users may subscribe public mailing lists + if (user == null) return false; + try { + var member = ListMember.load(this,user); + return member.hasState(ListMember.STATE_OWNER|ListMember.STATE_SUBSCRIBER); // owners may subscribe their own mailing lists + } catch (SQLException e) { + LOG.warn("Was not able to load ListMember: ",e); + return false; + } + } + /** * Load a ML object by it's identifying email address. * This is a cached method: if the ML has been loaded before, the already-loaded object will be returned. @@ -196,6 +218,10 @@ public class MailingList { return ml; } + private Set members() throws SQLException { + return ListMember.of(email()).keySet(); + } + /** * creates a map from the current ML object containing only email and name of the ML * @return @@ -213,6 +239,13 @@ public class MailingList { return name; } + @Override + public void onMessageReceived(Message message) throws MessagingException { + LOG.debug("Message received: {}",message.getFrom()); + storeMessage(message); + forward(message); + } + /** * provide the set of mailing lists that are publicy open to subscriptions * @return @@ -362,6 +395,10 @@ public class MailingList { } } + private void storeMessage(Message message){ + // TODO: implement + } + /** * send a test email to the provided user @@ -374,4 +411,14 @@ public class MailingList { var text = "If you received this mail, the SMTP settings of your mailing list are correct."; smtp.login().send(email(),name(),user.email(),subject,text); } + + public static Stream toAddress(String email) { + try { + return Arrays.asList(InternetAddress.parse(email)).stream(); + } catch (AddressException e) { + LOG.debug("Was not able to parse {}",email,e); + return new ArrayList().stream(); + } + } + } diff --git a/src/main/java/de/srsoftware/widerhall/mail/Archiver.java b/src/main/java/de/srsoftware/widerhall/mail/Archiver.java deleted file mode 100644 index 0bb8c07..0000000 --- a/src/main/java/de/srsoftware/widerhall/mail/Archiver.java +++ /dev/null @@ -1,26 +0,0 @@ -package de.srsoftware.widerhall.mail; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.mail.Message; -import javax.mail.MessagingException; -import java.io.UnsupportedEncodingException; - -public class Archiver implements MessageHandler { - private static final Logger LOG = LoggerFactory.getLogger(Archiver.class); - private final SmtpClient smtp; - private final String receiver,sender; - - public Archiver(String host, int port, String username, String password, String sender, String receiver) { - this.sender = sender; - this.receiver = receiver; - SmtpClient smtp = new SmtpClient(host,port,username,password); - this.smtp = smtp; - } - - @Override - public void onMessageReceived(Message message) throws MessagingException { - LOG.debug("storing {}",message.getSubject()); - } -} diff --git a/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java b/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java deleted file mode 100644 index 4dca6bd..0000000 --- a/src/main/java/de/srsoftware/widerhall/mail/Forwarder.java +++ /dev/null @@ -1,27 +0,0 @@ -package de.srsoftware.widerhall.mail; - -import org.json.simple.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import javax.mail.Message; -import javax.mail.MessagingException; -import java.io.UnsupportedEncodingException; - -public class Forwarder implements MessageHandler { - private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class); - private final SmtpClient smtp; - private final String receiver,sender; - - public Forwarder(String host, int port, String username, String password, String sender, String receiver) { - this.sender = sender; - this.receiver = receiver; - SmtpClient smtp = new SmtpClient(host,port,username,password); - this.smtp = smtp; - } - - @Override - public void onMessageReceived(Message message) throws MessagingException { - LOG.debug("forwarding {}",message.getSubject()); - } -} diff --git a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java index 1a5c009..0c0c2a7 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java +++ b/src/main/java/de/srsoftware/widerhall/mail/ImapClient.java @@ -2,7 +2,6 @@ package de.srsoftware.widerhall.mail; import com.sun.mail.imap.IMAPFolder; import de.srsoftware.widerhall.Constants; -import org.json.simple.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,26 +14,38 @@ public class ImapClient { private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class); private final int port; private final String host, username, password, folderName; - private boolean stopped = true; private IMAPFolder inbox; - private HashSet listeners = new HashSet<>(); + private ListeningThread listeningThread; + private Heartbeat heartbeat; private class ListeningThread extends Thread { private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class); + private HashSet listeners = new HashSet<>(); + private boolean stopped = false; + + public ListeningThread addListener(MessageHandler messageHandler) { + listeners.add(messageHandler); + return this; + } + + public ListeningThread dropListeners() { + listeners.clear(); + return this; + } @Override public void run() { while (!stopped) { + try { + sleep(5000); + } catch (InterruptedException interruptedException) { + interruptedException.printStackTrace(); + } try { openInbox(); } catch (MessagingException e){ LOG.warn("Connection problem:",e); - try { - sleep(1000); - } catch (InterruptedException interruptedException) { - interruptedException.printStackTrace(); - } } } } @@ -78,11 +89,18 @@ public class ImapClient { props.put(Constants.PROTOCOL,Constants.IMAPS); return props; } + + public ListeningThread doStop() { + stopped = true; + return this; + } } private class Heartbeat extends Thread{ private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class); + private boolean stopped = false; + @Override public void run() { while (!stopped){ @@ -99,6 +117,11 @@ public class ImapClient { } } } + + public Heartbeat doStop() { + stopped = true; + return this; + } } public ImapClient(String host, int port, String username, String password, String folderName) { @@ -109,9 +132,9 @@ public class ImapClient { this.folderName = folderName; } - public ImapClient addListener(MessageHandler messageHandler) { - listeners.add(messageHandler); + if (listeningThread == null) listeningThread = new ListeningThread(); + listeningThread.addListener(messageHandler); return this; } @@ -135,15 +158,28 @@ public class ImapClient { return folderName; } - public void start() { - stopped = false; + public ImapClient start() { + stop(); + LOG.debug("Creating ListeningThread…"); - new ListeningThread().start(); + (listeningThread = new ListeningThread()).start(); + LOG.debug("Creating Heartbeat…"); - new Heartbeat().start(); + (heartbeat = new Heartbeat()).start(); + + return this; } - public void stop(){ - stopped = true; + public ImapClient stop(){ + if (listeningThread != null) { + LOG.debug("Stopping old ListeningThread…"); + listeningThread.dropListeners().doStop(); + listeningThread = null; + } + if (heartbeat != null){ + heartbeat.doStop(); + heartbeat = null; + } + return this; } } diff --git a/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java b/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java index 526ef22..4033f0a 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java +++ b/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java @@ -4,10 +4,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.mail.*; -import javax.mail.internet.InternetAddress; -import javax.mail.internet.MimeMessage; +import javax.mail.internet.*; import java.io.UnsupportedEncodingException; import java.util.Date; +import java.util.List; import java.util.Properties; public class SmtpClient { @@ -29,6 +29,25 @@ public class SmtpClient { this.port = port; } + public void bccForward(String from, Message message, List emails) throws MessagingException { + if (session == null) login(); + MimeMessage forward = new MimeMessage(session); + var addresses = InternetAddress.parse(String.join(", ",emails)); + var senders = message.getFrom(); + forward.setFrom(senders[0]); + //forward.setFrom(InternetAddress.parse(from)[0]); + forward.setRecipients(Message.RecipientType.BCC,addresses); + forward.setSubject(message.getSubject()); + MimeBodyPart body = new MimeBodyPart(); + body.setContent(message,"message/rfc822"); + Multipart multipart = new MimeMultipart(); + multipart.addBodyPart(body); + forward.setContent(multipart); + forward.saveChanges(); + send(forward); + } + + public SmtpClient login(){ if (session == null) { Properties props = new Properties(); @@ -55,10 +74,14 @@ public class SmtpClient { message.setText(content,UTF8); message.setSentDate(new Date()); message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers,false)); + send(message); + } + public void send(Message message) throws MessagingException { LOG.debug("Versende Mail…"); Transport.send(message,username,password); LOG.debug("…versendet"); + } public String host() { @@ -76,4 +99,5 @@ public class SmtpClient { public String password() { return password; } + } diff --git a/src/main/java/de/srsoftware/widerhall/web/Web.java b/src/main/java/de/srsoftware/widerhall/web/Web.java index a976397..85c13f2 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Web.java +++ b/src/main/java/de/srsoftware/widerhall/web/Web.java @@ -66,9 +66,11 @@ public class Web extends TemplateServlet { var imapUser = req.getParameter(IMAP_USER); data.put(IMAP_USER, imapUser); var imapPass = req.getParameter(IMAP_PASS); - var smtpHost = req.getParameter(SMTP_HOST); var inbox = req.getParameter(INBOX); - if (inbox == null) inbox = DEFAULT_INBOX; + if (inbox == null || inbox.isBlank()) inbox = DEFAULT_INBOX; + data.put(INBOX, inbox); + + var smtpHost = req.getParameter(SMTP_HOST); data.put(SMTP_HOST, smtpHost); var smtpUser = req.getParameter(SMTP_USER); data.put(SMTP_USER, smtpUser); diff --git a/static/templates/add_list.st b/static/templates/add_list.st index 5b20816..8831334 100644 --- a/static/templates/add_list.st +++ b/static/templates/add_list.st @@ -44,7 +44,7 @@ Password diff --git a/static/templates/frontpage.st b/static/templates/frontpage.st index 2f7cbe6..741afb3 100644 --- a/static/templates/frontpage.st +++ b/static/templates/frontpage.st @@ -36,5 +36,6 @@ That's it.

+ «footer()» \ No newline at end of file From b38b40a677ce02df659cba09b718db32079ff9ab Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 20 Apr 2022 08:22:26 +0200 Subject: [PATCH 3/5] working on forwarding mail --- pom.xml | 2 +- .../widerhall/data/MailingList.java | 30 +++++++++++-------- .../srsoftware/widerhall/mail/SmtpClient.java | 21 ++++++------- .../de/srsoftware/widerhall/web/Rest.java | 14 +++++++++ static/templates/add_list.st | 2 +- static/templates/frontpage.st | 2 +- static/templates/index.st | 2 +- static/templates/inspect.st | 15 +++++++++- static/templates/js.st | 20 +++++++++++-- static/templates/login.st | 2 +- static/templates/register.st | 2 +- static/templates/subscribe.st | 2 +- static/templates/unsubscribe.st | 2 +- 13 files changed, 82 insertions(+), 34 deletions(-) diff --git a/pom.xml b/pom.xml index f63bd10..74d9804 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.example Widerhall - 0.0.15 + 0.0.16 diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java index 915f386..cdcda3d 100644 --- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java +++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java @@ -40,6 +40,9 @@ public class MailingList implements MessageHandler { private static final int STATE_PENDING = 0; private static final int STATE_ENABLED = 1; private static final int STATE_PUBLIC = 2; + public static final int STATE_FORWARD_FROM = 4; + private static final int VISIBLE = 1; + private static final int HIDDEN = 0; private final String name; private final String email; private int state; @@ -143,7 +146,8 @@ public class MailingList implements MessageHandler { private void forward(Message message) throws MessagingException { try { var emails = members().stream().map(User::email).toList(); - smtp.bccForward(email(),message,emails); + String sender = (state & STATE_FORWARD_FROM) > 0 ? message.getFrom()[0].toString() : email(); + smtp.bccForward(sender,message,emails); } catch (SQLException e) { LOG.error("Failed to read list members of {} from database. Cannot forward message!",email(),e); } @@ -175,6 +179,10 @@ public class MailingList implements MessageHandler { return ml; } + public boolean hasState(int test){ + return (state & test) > 0; + } + public void hide(boolean hide) throws SQLException { state = hide ? state ^ (state & STATE_PUBLIC) : state | STATE_PUBLIC; Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run(); @@ -231,7 +239,7 @@ public class MailingList implements MessageHandler { String[] parts = email.split("@", 2); map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1])); map.put(NAME, name); - map.put(STATE, stateString(state)); + map.put(STATE, stateMap()); return map; } @@ -280,7 +288,7 @@ public class MailingList implements MessageHandler { if (smtp.host() != null) map.put(SMTP_HOST, smtp.host()); if (smtp.port() != 0) map.put(SMTP_PORT, smtp.port()); if (smtp.username() != null) map.put(SMTP_USER, smtp.username()); - map.put(STATE,stateString(state)); + map.put(STATE,stateMap()); return map; } @@ -325,16 +333,12 @@ public class MailingList implements MessageHandler { smtp.login().send(email(),name(),user.email(),subject,text); } - /** - * convert state to readable string - * @param state - * @return - */ - private static String stateString(int state) { - var states = new ArrayList(); - states.add((state & STATE_ENABLED) == STATE_ENABLED ? "enabled" : "disabled"); - states.add((state & STATE_PUBLIC) == STATE_PUBLIC ? "public" : "hidden"); - return String.join(", ", states); + public Map stateMap(){ + var map = new HashMap(); + if (hasState(STATE_ENABLED)) map.put("enabled",VISIBLE); + if (hasState(STATE_PUBLIC)) map.put("public",VISIBLE); + if (hasState(STATE_FORWARD_FROM)) map.put("original_from",HIDDEN); + return map; } /** diff --git a/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java b/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java index 4033f0a..3531054 100644 --- a/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java +++ b/src/main/java/de/srsoftware/widerhall/mail/SmtpClient.java @@ -19,6 +19,7 @@ public class SmtpClient { private static final String UTF8 = "UTF-8"; private final String host,password,username; private final int port; + private boolean forwardUsingListAddress = true; private Session session; @@ -32,18 +33,18 @@ public class SmtpClient { public void bccForward(String from, Message message, List emails) throws MessagingException { if (session == null) login(); MimeMessage forward = new MimeMessage(session); - var addresses = InternetAddress.parse(String.join(", ",emails)); - var senders = message.getFrom(); - forward.setFrom(senders[0]); - //forward.setFrom(InternetAddress.parse(from)[0]); - forward.setRecipients(Message.RecipientType.BCC,addresses); + forward.setFrom(from); + forward.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(", ",emails))); forward.setSubject(message.getSubject()); - MimeBodyPart body = new MimeBodyPart(); - body.setContent(message,"message/rfc822"); - Multipart multipart = new MimeMultipart(); - multipart.addBodyPart(body); + + MimeMultipart multipart = new MimeMultipart(); + MimeBodyPart messageBodyPart = new MimeBodyPart(); + + messageBodyPart.setDataHandler(message.getDataHandler()); + multipart.addBodyPart(messageBodyPart); + forward.setContent(multipart); - forward.saveChanges(); + send(forward); } diff --git a/src/main/java/de/srsoftware/widerhall/web/Rest.java b/src/main/java/de/srsoftware/widerhall/web/Rest.java index 13b8f58..2d6d9ad 100644 --- a/src/main/java/de/srsoftware/widerhall/web/Rest.java +++ b/src/main/java/de/srsoftware/widerhall/web/Rest.java @@ -16,6 +16,7 @@ import java.io.IOException; import java.io.Serializable; import java.io.UnsupportedEncodingException; import java.sql.SQLException; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -27,6 +28,7 @@ public class Rest extends HttpServlet { private static final Logger LOG = LoggerFactory.getLogger(Rest.class); private static final String LIST_DISABLE = "list/disable"; private static final String LIST_EDITABLE = "list/editable"; + private static final String LIST_DETAIL = "list/detail"; private static final String LIST_ENABLE = "list/enable"; private static final String LIST_HIDE = "list/hide"; private static final String LIST_MEMBERS = "list/members"; @@ -104,6 +106,7 @@ public class Rest extends HttpServlet { break; case LIST_SUBSCRIBABLE: json.put("lists", MailingList.subscribable(user).stream().map(MailingList::minimalMap).toList()); + break; default: json.put(ERROR,t("No handler for path '{}'!",path)); break; @@ -139,6 +142,9 @@ public class Rest extends HttpServlet { var userEmail = req.getParameter(EMAIL); var permissions = req.getParameter(PERMISSIONS); switch (path) { + case LIST_DETAIL: + json.putAll(listDetail(listEmail,user)); + break; case LIST_DISABLE: json.putAll(enableList(listEmail,user,false)); break; @@ -183,6 +189,14 @@ public class Rest extends HttpServlet { } } + private Map listDetail(String listEmail, User user) { + var ml = MailingList.load(listEmail); + if (ml == null) return Map.of(ERROR,t("Mailinglist {} unknown",listEmail)); + var map = new HashMap<>(); + if (ml.hasState(MailingList.STATE_FORWARD_FROM)) map.put("forward_from",true); + return map; + } + private Map listMembers(String listEmail, User user) { if (listEmail == null || listEmail.isBlank()) return Map.of(ERROR,"no list email provided!"); if (user.hashPermission(User.PERMISSION_ADMIN) || ListMember.listsOwnedBy(user).contains(listEmail)) { diff --git a/static/templates/add_list.st b/static/templates/add_list.st index 8831334..00ba212 100644 --- a/static/templates/add_list.st +++ b/static/templates/add_list.st @@ -6,7 +6,7 @@ - + «navigation()» «userinfo()»

Widerhall List Creation

diff --git a/static/templates/frontpage.st b/static/templates/frontpage.st index 741afb3..3f9e8f0 100644 --- a/static/templates/frontpage.st +++ b/static/templates/frontpage.st @@ -4,7 +4,7 @@ - +

Widerhall front page

If you are looking for you mailing lists, Go to the /web page...
diff --git a/static/templates/index.st b/static/templates/index.st index 38f65a1..6ccc5df 100644 --- a/static/templates/index.st +++ b/static/templates/index.st @@ -6,7 +6,7 @@ - + «navigation()» «userinfo()» «messages()» diff --git a/static/templates/inspect.st b/static/templates/inspect.st index c4a3c82..fec45c3 100644 --- a/static/templates/inspect.st +++ b/static/templates/inspect.st @@ -6,11 +6,24 @@ - + «navigation()» «userinfo()» «messages()»

Widerhall '«data.list»' Details

«listmembers()» +
+
+ Options for «data.list» + + +
+ + \ No newline at end of file diff --git a/static/templates/js.st b/static/templates/js.st index 8f25513..b249e12 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -27,6 +27,10 @@ function hideList(listEmail){ $.post('/api/list/hide',{list:listEmail},showListResult,'json'); } +function loadListDetail(listEmail){ + $.post('/api/list/detail',{list:listEmail},showListDetail,'json'); +} + function loadListOfEditableLists(){ $.getJSON('/api/list/editable', showListOfEditableLists); } @@ -51,6 +55,10 @@ function showList(listEmail){ $.post('/api/list/show',{list:listEmail},showListResult,'json'); } +function showListDetail(data){ + console.log(data); +} + function showListOfEditableLists(data){ for (let i in data.lists){ let list = data.lists[i]; @@ -63,7 +71,11 @@ function showListOfEditableLists(data){ td = $('
ListIMAPIMAP SMTP
Host Port UserInbox Host Port User'); $('',{href:inspect}).text(addr).appendTo(td) td.appendTo(row); - $('').text(list.state).appendTo(row); + let states = []; + for (let state in list.state){ + if (list.state[state] > 0) states.push(state); + } + $('').text(states.toString()).appendTo(row); let select = $('',{class:'right'}).text(list.email.prefix).appendTo(row); $('',{class:'right'}).text('@').appendTo(row); $('').text(list.email.domain).appendTo(row); - $('').text(list.state).appendTo(row); + let states = []; + for (let state in list.state){ + if (list.state[state] > 0) states.push(state); + } + $('').text(states.toString()).appendTo(row); let td = $('',{class:'actions'}); $(' diff --git a/static/templates/js.st b/static/templates/js.st index b249e12..5300ef8 100644 --- a/static/templates/js.st +++ b/static/templates/js.st @@ -57,6 +57,8 @@ function showList(listEmail){ function showListDetail(data){ console.log(data); + if (data.forward_from) $('input[name="forward_from"]').prop('checked',true); + if (data.forward_attached) $('input[name="forward_attached"]').prop('checked',true); } function showListOfEditableLists(data){