Browse Source

preparing message store

drop_old_mail
Stephan Richter 3 years ago
parent
commit
06324a7e96
  1. 2
      pom.xml
  2. 8
      src/main/java/de/srsoftware/widerhall/Application.java
  3. 2
      src/main/java/de/srsoftware/widerhall/Constants.java
  4. 40
      src/main/java/de/srsoftware/widerhall/Util.java
  5. 61
      src/main/java/de/srsoftware/widerhall/data/MailingList.java

2
pom.xml

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
<groupId>org.example</groupId>
<artifactId>Widerhall</artifactId>
<version>0.1.3</version>
<version>0.1.4</version>
<build>
<plugins>
<plugin>

8
src/main/java/de/srsoftware/widerhall/Application.java

@ -1,5 +1,6 @@ @@ -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 { @@ -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 {

2
src/main/java/de/srsoftware/widerhall/Constants.java

@ -23,6 +23,8 @@ public class Constants { @@ -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)";

40
src/main/java/de/srsoftware/widerhall/Util.java

@ -4,9 +4,14 @@ import de.srsoftware.tools.translations.Translation; @@ -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 { @@ -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;
}
}

61
src/main/java/de/srsoftware/widerhall/data/MailingList.java

@ -1,9 +1,11 @@ @@ -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; @@ -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 { @@ -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 { @@ -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 { @@ -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();
}
}

Loading…
Cancel
Save