Merge branch 'main' into lang_de
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
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.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
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;
|
||||
@@ -18,7 +24,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";
|
||||
@@ -32,14 +38,17 @@ public class MailingList {
|
||||
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;
|
||||
public static final int STATE_FORWARD_ATTACHED = 8;
|
||||
private static final int VISIBLE = 1;
|
||||
private static final int HIDDEN = 0;
|
||||
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<String,MailingList> lists = new HashMap<>();
|
||||
private static final HashMap<String,MailingList> cache = new HashMap<>();
|
||||
|
||||
/**
|
||||
* create a new ML object
|
||||
@@ -55,15 +64,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 +87,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 +105,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(", ")
|
||||
@@ -108,19 +115,83 @@ 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<MailingList> editableBy(User user) {
|
||||
var list = new HashSet<MailingList>();
|
||||
for (String key : ListMember.listsOwnedBy(user)) list.add(load(key));
|
||||
return list;
|
||||
}
|
||||
|
||||
public String email() {
|
||||
return email;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
setFlag(STATE_ENABLED,enable);
|
||||
|
||||
if (enable) {
|
||||
imap.start().addListener(this);
|
||||
} else {
|
||||
imap.stop();
|
||||
}
|
||||
}
|
||||
|
||||
private void forward(Message message) throws MessagingException {
|
||||
try {
|
||||
var emails = members().stream().map(ListMember::user).map(User::email).toList();
|
||||
String sender = (state & STATE_FORWARD_FROM) > 0 ? message.getFrom()[0].toString() : email();
|
||||
smtp.bccForward(sender,message,emails);
|
||||
} catch (SQLException e) {
|
||||
LOG.error("Laden der Listen-Mitglieder von {} fehlgeschlagen. Nachricht kann nicht weitergeleitet werden!",email(),e);
|
||||
}
|
||||
}
|
||||
|
||||
public void forwardAttached(boolean forward) throws SQLException {
|
||||
setFlag(STATE_FORWARD_ATTACHED,forward);
|
||||
}
|
||||
|
||||
public void forwardFrom(boolean forward) throws SQLException {
|
||||
setFlag(STATE_FORWARD_FROM,forward);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param rs
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
private static MailingList from(ResultSet rs) throws SQLException {
|
||||
String email = rs.getString(EMAIL);
|
||||
var ml = cache.get(email);
|
||||
if (ml == null) cache.put(email,ml = new MailingList(rs.getString(EMAIL),
|
||||
rs.getString(NAME),
|
||||
rs.getString(IMAP_HOST),
|
||||
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),
|
||||
rs.getString(SMTP_PASS),
|
||||
rs.getInt(STATE)));
|
||||
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();
|
||||
setFlag(STATE_PUBLIC,!hide);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,47 +206,11 @@ public class MailingList {
|
||||
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("Konnte ListeMember nicht laden: ",e);
|
||||
LOG.warn("Konnte Listen-Mitglied nicht laden: ",e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* load the set of mailing lists a given user is allowed to edit
|
||||
* @param user
|
||||
* @return
|
||||
*/
|
||||
public static Set<MailingList> editableBy(User user) {
|
||||
var list = new HashSet<MailingList>();
|
||||
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.
|
||||
*
|
||||
* @param rs
|
||||
* @return
|
||||
* @throws SQLException
|
||||
*/
|
||||
private static MailingList from(ResultSet rs) throws SQLException {
|
||||
String email = rs.getString(EMAIL);
|
||||
var ml = lists.get(email);
|
||||
if (ml == null) lists.put(email,ml = new MailingList(rs.getString(EMAIL),
|
||||
rs.getString(NAME),
|
||||
rs.getString(IMAP_HOST),
|
||||
rs.getInt(IMAP_PORT),
|
||||
rs.getString(IMAP_USER),
|
||||
rs.getString(IMAP_PASS),
|
||||
rs.getString(SMTP_HOST),
|
||||
rs.getInt(SMTP_PORT),
|
||||
rs.getString(SMTP_USER),
|
||||
rs.getString(SMTP_PASS),
|
||||
rs.getInt(STATE)));
|
||||
return ml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
@@ -184,7 +219,7 @@ public class MailingList {
|
||||
*/
|
||||
public static MailingList load(String listEmail) {
|
||||
if (listEmail == null) return null;
|
||||
var ml = lists.get(listEmail);
|
||||
var ml = cache.get(listEmail);
|
||||
if (ml == null) try {
|
||||
var rs = Database.open()
|
||||
.select(TABLE_NAME)
|
||||
@@ -192,11 +227,46 @@ public class MailingList {
|
||||
.compile().exec();
|
||||
if (rs.next()) ml = MailingList.from(rs);
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Konnte MailingList nicht laden: ",e);
|
||||
LOG.debug("Konnte Mailing-Liste nicht laden: ",e);
|
||||
}
|
||||
return ml;
|
||||
}
|
||||
|
||||
public boolean mayBeAlteredBy(User user) {
|
||||
if (user.hashPermission(PERMISSION_ADMIN)) return true;
|
||||
try {
|
||||
if (ListMember.load(this,user).hasState(ListMember.STATE_OWNER)) return true;
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Fehler beim Laden des Listenmitglieds für ({}, {})",user.email(),email());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean mayBeTestedBy(User user) {
|
||||
if (user.hashPermission(PERMISSION_ADMIN)) return true;
|
||||
try {
|
||||
if (ListMember.load(this,user).hasState(ListMember.STATE_OWNER)) return true;
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Fehler beim Laden des Listenmitglieds für ({}, {})",user.email(),email());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public Set<ListMember> members() throws SQLException {
|
||||
return ListMember.of(this);
|
||||
}
|
||||
|
||||
public boolean membersMayBeListedBy(User user) {
|
||||
if (user.hashPermission(PERMISSION_ADMIN)) return true;
|
||||
try {
|
||||
if (ListMember.load(this,user).hasState(ListMember.STATE_OWNER)) return true;
|
||||
} catch (SQLException e) {
|
||||
LOG.debug("Fehler beim Laden des Listenmitglieds für ({}, {})",user.email(),email());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* creates a map from the current ML object containing only email and name of the ML
|
||||
* @return
|
||||
@@ -206,7 +276,7 @@ public class MailingList {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -214,6 +284,13 @@ public class MailingList {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessageReceived(Message message) throws MessagingException {
|
||||
LOG.debug("Nachricht empfangen: {}",message.getFrom());
|
||||
storeMessage(message);
|
||||
forward(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* provide the set of mailing lists that are publicy open to subscriptions
|
||||
* @return
|
||||
@@ -241,12 +318,14 @@ public class MailingList {
|
||||
*/
|
||||
public Map<String, Object> 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,stateMap());
|
||||
return map;
|
||||
}
|
||||
|
||||
@@ -261,10 +340,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())
|
||||
@@ -290,16 +370,17 @@ public class MailingList {
|
||||
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<String>();
|
||||
states.add((state & STATE_ENABLED) == STATE_ENABLED ? "enabled" : "disabled");
|
||||
states.add((state & STATE_PUBLIC) == STATE_PUBLIC ? "public" : "hidden");
|
||||
return String.join(", ", states);
|
||||
private void setFlag(int flag, boolean on) throws SQLException {
|
||||
state = on ? state | flag : state ^ (state & flag);
|
||||
Database.open().update(TABLE_NAME).set(STATE,state).where(EMAIL, email()).compile().run();
|
||||
}
|
||||
|
||||
public Map<String,Integer> stateMap(){
|
||||
var map = new HashMap<String,Integer>();
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -335,7 +416,7 @@ public class MailingList {
|
||||
rs.close();
|
||||
return result;
|
||||
} catch (SQLException e) {
|
||||
LOG.warn("Lesen der für {} abbonnierbaren Mailinglisten fehlgeschlagen.",user,e);
|
||||
LOG.warn("Lesen der abbonnierbaren Mailinglisten von {} fehlgeschlagen.",user,e);
|
||||
return Set.of();
|
||||
}
|
||||
}
|
||||
@@ -360,6 +441,10 @@ public class MailingList {
|
||||
}
|
||||
}
|
||||
|
||||
private void storeMessage(Message message){
|
||||
// TODO: implement
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* send a test email to the provided user
|
||||
@@ -372,4 +457,13 @@ public class MailingList {
|
||||
var text = "Wenn Sie diese Nachricht empfangen haben, sind die SMTP-Einstellungen Ihrer Mailing-Liste korrekt.";
|
||||
smtp.login().send(email(),name(),user.email(),subject,text);
|
||||
}
|
||||
|
||||
public static Stream<InternetAddress> toAddress(String email) {
|
||||
try {
|
||||
return Arrays.asList(InternetAddress.parse(email)).stream();
|
||||
} catch (AddressException e) {
|
||||
LOG.debug("Parsen von {} fehlgeschlagen",email,e);
|
||||
return new ArrayList<InternetAddress>().stream();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user