diff --git a/pom.xml b/pom.xml
index a7a89ac..99031ba 100644
--- a/pom.xml
+++ b/pom.xml
@@ -6,7 +6,7 @@
org.example
Widerhall
- 0.1.3
+ 0.1.4
diff --git a/src/main/java/de/srsoftware/widerhall/Application.java b/src/main/java/de/srsoftware/widerhall/Application.java
index 59a5454..79ef706 100644
--- a/src/main/java/de/srsoftware/widerhall/Application.java
+++ b/src/main/java/de/srsoftware/widerhall/Application.java
@@ -1,5 +1,6 @@
package de.srsoftware.widerhall;
+import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.web.Front;
import de.srsoftware.widerhall.web.Rest;
import de.srsoftware.widerhall.web.Web;
@@ -20,8 +21,13 @@ public class Application {
while (!config.configFile().equals(config.file())) config.load(config.configFile());
if (!config.configFile().exists()) config.save();
- //startMailSystem(json);
+
startWebserver();
+ startMailsystem();
+ }
+
+ private static void startMailsystem() {
+ MailingList.startEnabled();
}
private static void startWebserver() throws Exception {
diff --git a/src/main/java/de/srsoftware/widerhall/Constants.java b/src/main/java/de/srsoftware/widerhall/Constants.java
index 22ea782..be0229a 100644
--- a/src/main/java/de/srsoftware/widerhall/Constants.java
+++ b/src/main/java/de/srsoftware/widerhall/Constants.java
@@ -23,6 +23,8 @@ public class Constants {
public static final String PREFIX = "prefix";
public static final String PROTOCOL = "mail.store.protocol";
public static final String STATE = "state";
+ public static final String SUBJECT = "subject";
+ public static final String TEXT = "text";
public static final String TOKEN = "token";
public static final String USER = "user";
public static final String VARCHAR = "VARCHAR(255)";
diff --git a/src/main/java/de/srsoftware/widerhall/Util.java b/src/main/java/de/srsoftware/widerhall/Util.java
index 8dbee4e..b3781e4 100644
--- a/src/main/java/de/srsoftware/widerhall/Util.java
+++ b/src/main/java/de/srsoftware/widerhall/Util.java
@@ -4,9 +4,14 @@ import de.srsoftware.tools.translations.Translation;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.Part;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
@@ -118,4 +123,39 @@ public class Util {
public static boolean getCheckbox(HttpServletRequest req, String key) {
return "on".equals(req.getParameter(key));
}
+
+ /**
+ * Return the primary text content of the message.
+ */
+ public static String getText(Part p) throws MessagingException, IOException {
+ // https://javaee.github.io/javamail/FAQ
+ if (p.isMimeType("text/*")) return (String)p.getContent();
+
+ if (p.isMimeType("multipart/alternative")) {
+ // prefer html text over plain text
+ Multipart mp = (Multipart)p.getContent();
+ String text = null;
+ for (int i = 0; i < mp.getCount(); i++) {
+ Part bp = mp.getBodyPart(i);
+ if (bp.isMimeType("text/plain")) {
+ if (text == null) text = getText(bp);
+ continue;
+ } else if (bp.isMimeType("text/html")) {
+ String s = getText(bp);
+ if (s != null) return s;
+ } else {
+ return getText(bp);
+ }
+ }
+ return text;
+ } else if (p.isMimeType("multipart/*")) {
+ Multipart mp = (Multipart)p.getContent();
+ for (int i = 0; i < mp.getCount(); i++) {
+ String s = getText(mp.getBodyPart(i));
+ if (s != null) return s;
+ }
+ }
+
+ return null;
+ }
}
diff --git a/src/main/java/de/srsoftware/widerhall/data/MailingList.java b/src/main/java/de/srsoftware/widerhall/data/MailingList.java
index 7cc28ff..fc450e7 100644
--- a/src/main/java/de/srsoftware/widerhall/data/MailingList.java
+++ b/src/main/java/de/srsoftware/widerhall/data/MailingList.java
@@ -1,9 +1,11 @@
package de.srsoftware.widerhall.data;
import de.srsoftware.widerhall.Configuration;
+import de.srsoftware.widerhall.Util;
import de.srsoftware.widerhall.mail.ImapClient;
import de.srsoftware.widerhall.mail.MessageHandler;
import de.srsoftware.widerhall.mail.SmtpClient;
+import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -13,7 +15,7 @@ import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
-import java.io.UnsupportedEncodingException;
+import java.io.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
@@ -199,6 +201,16 @@ public class MailingList implements MessageHandler {
return ml;
}
+ private boolean hashMember(String senderEmail) {
+ if (senderEmail == null) return false;
+ try {
+ return members().stream().map(ListMember::user).map(User::email).anyMatch(senderEmail::equals);
+ } catch (SQLException e) {
+ LOG.warn("hasMember() failded for {}",email(),e);
+ }
+ return false;
+ }
+
public boolean hasState(int test){
return (state & test) > 0;
}
@@ -331,15 +343,7 @@ public class MailingList implements MessageHandler {
- private boolean hashMember(String senderEmail) {
- if (senderEmail == null) return false;
- try {
- return members().stream().map(ListMember::user).map(User::email).anyMatch(senderEmail::equals);
- } catch (SQLException e) {
- LOG.warn("hasMember() failded for {}",email(),e);
- }
- return false;
- }
+
public MailingList open(boolean open) throws SQLException {
return setFlag(STATE_OPEN,open);
@@ -554,8 +558,41 @@ public class MailingList implements MessageHandler {
return "["+name+"]";
}
- private void storeMessage(Message message){
- // TODO: implement
+ public static void startEnabled() {
+ try {
+ var rs = Database.open().select(TABLE_NAME).compile().exec();
+ while (rs.next()) {
+ var list = MailingList.from(rs);
+ if (list.hasState(STATE_ENABLED)) list.enable(true);
+ }
+ } catch (SQLException e) {
+ LOG.debug("Failed to load MailingLists.");
+ }
+ }
+
+ private void storeMessage(Message message) {
+ try {
+ var id = message.getHeader("Message-ID")[0].replace("<","").replace(">","");
+ var addr = ((InternetAddress)message.getFrom()[0]);
+ var fromEmail = addr.getAddress();
+ var fromName = addr.getPersonal();
+ if (fromName == null || fromName.isBlank()) fromName = fromEmail.split("@")[0]+"@xxxxxx";
+ var subject = message.getSubject();
+ var text = Util.getText(message);
+ JSONObject json = new JSONObject();
+ json.put("id",id);
+ json.put("from",Map.of(EMAIL,fromEmail,NAME,fromName));
+ json.put(SUBJECT,subject);
+ json.put(TEXT,text);
+
+ File file = new File("/tmp/"+id+".json");
+ try (var fw = new FileWriter(file)) {
+ json.writeJSONString(fw);
+ fw.flush();
+ }
+ } catch (MessagingException | IOException e) {
+ e.printStackTrace();
+ }
}