You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
729 lines
28 KiB
729 lines
28 KiB
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.ProblemListener; |
|
import de.srsoftware.widerhall.mail.SmtpClient; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
import org.stringtemplate.v4.ST; |
|
|
|
import javax.mail.*; |
|
import javax.mail.internet.AddressException; |
|
import javax.mail.internet.InternetAddress; |
|
import javax.ws.rs.HEAD; |
|
import java.io.*; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.util.*; |
|
import java.util.stream.Collectors; |
|
import java.util.stream.Stream; |
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
import static de.srsoftware.widerhall.Util.t; |
|
import static de.srsoftware.widerhall.data.User.PERMISSION_ADMIN; |
|
|
|
/** |
|
* this class encapsulates a MailingList db object |
|
*/ |
|
public class MailingList implements MessageHandler, ProblemListener { |
|
public static final String KEY_FORWARD_FROM = "forward_from"; |
|
public static final String KEY_FORWARD_ATTACHED = "forward_attached"; |
|
public static final String KEY_HIDE_RECEIVERS = "hide_receivers"; |
|
public static final String KEY_REPLY_TO_LIST = "reply_to_list"; |
|
public static final String KEY_OPEN_FOR_GUESTS = "open_for_guests"; |
|
public static final String KEY_OPEN_FOR_SUBSCRIBERS = "open_for_subscribers"; |
|
public static final String KEY_ARCHIVE = "archive"; |
|
public static final String KEY_MODS_CAN_EDIT_MODS = "edit_mods"; |
|
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"; |
|
private static final String IMAP_USER = "imap_user"; |
|
private static final String IMAP_PASS = "imap_pass"; |
|
private static final String SMTP_HOST = "smtp_host"; |
|
private static final String SMTP_PORT = "smtp_port"; |
|
private static final String SMTP_USER = "smtp_user"; |
|
private static final String SMTP_PASS = "smtp_pass"; |
|
public static final String TABLE_NAME = "Lists"; |
|
private static final int STATE_PENDING = 0; |
|
private static final int STATE_ENABLED = 1; // do we process incoming messages? |
|
private static final int STATE_PUBLIC = 2; // can guests see this ML? |
|
public static final int STATE_FORWARD_FROM = 4; // set original sender as FROM when forwarding? |
|
public static final int STATE_FORWARD_ATTACHED = 8; // forward messages as attachment? |
|
public static final int STATE_HIDE_RECEIVERS = 16; // send using BCC receivers |
|
public static final int STATE_REPLY_TO_LIST = 32; // set REPLY TO field to list address? |
|
public static final int STATE_OPEN_FOR_GUESTS = 64; // allow anyone to send via this list? |
|
public static final int STATE_PUBLIC_ARCHIVE = 128; // save received messages in archive? |
|
public static final int STATE_OPEN_FOR_SUBSCRIBERS = 256; // allow mods to send via this list? |
|
public static final int STATE_MODS_CAN_EDIT_MODS = 512; // allow mods to make subscribers to mods? |
|
private static final int VISIBLE = 1; |
|
private static final int HIDDEN = 0; |
|
private static final int DEFAULT_STATE = STATE_PENDING|STATE_HIDE_RECEIVERS|STATE_PUBLIC_ARCHIVE; |
|
private static final String RETAINED_FOLDER = "retained"; |
|
private static final String LAST_ERROR = "last_error"; |
|
private static final String LIST_NAME = "list_name"; |
|
private String email, name, lastError; |
|
private int state; |
|
private SmtpClient smtp; |
|
private ImapClient imap; |
|
|
|
private static final HashMap<String,MailingList> cache = new HashMap<>(); |
|
|
|
/** |
|
* create a new ML object |
|
* @param email |
|
* @param name |
|
* @param imapHost |
|
* @param imapPort |
|
* @param imapUser |
|
* @param imapPass |
|
* @param smtpHost |
|
* @param smtpPort |
|
* @param smtpUser |
|
* @param smtpPass |
|
* @param 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.state = state; |
|
this.smtp = new SmtpClient(smtpHost,smtpPort,smtpUser,smtpPass); |
|
this.imap = new ImapClient(imapHost,imapPort,imapUser,imapPass,inbox,this); |
|
} |
|
|
|
public MailingList archive(boolean enabled) throws SQLException { |
|
return setFlag(STATE_PUBLIC_ARCHIVE,enabled); |
|
} |
|
|
|
@Override |
|
public void clearProblems() { |
|
try { |
|
setLastError(null); |
|
} catch (SQLException e) { |
|
LOG.warn("setLastError(null) failed."); |
|
} |
|
} |
|
|
|
/** |
|
* create a new ML object int the database |
|
* @param email |
|
* @param name |
|
* @param imapHost |
|
* @param imapPort |
|
* @param imapUser |
|
* @param imapPass |
|
* @param smtpHost |
|
* @param smtpPort |
|
* @param smtpUser |
|
* @param smtpPass |
|
* @return |
|
* @throws SQLException |
|
*/ |
|
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, DEFAULT_STATE).save(); |
|
} |
|
|
|
/** |
|
* create underlying db table |
|
* @throws SQLException |
|
*/ |
|
public static void createTable() throws SQLException { |
|
var sql = new StringBuilder() |
|
.append("CREATE TABLE ").append(TABLE_NAME) |
|
.append(" (") |
|
.append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") |
|
.append(NAME).append(" ").append(VARCHAR).append(", ") |
|
.append(IMAP_HOST).append(" ").append(VARCHAR).append(", ") |
|
.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(", ") |
|
.append(SMTP_PASS).append(" ").append(VARCHAR).append(", ") |
|
.append(STATE).append(" ").append(INT).append(", ") |
|
.append(LAST_ERROR).append(" ").append(TEXT) |
|
.append(");"); |
|
Database.open().query(sql).compile().run(); |
|
} |
|
|
|
public String email() { |
|
return email; |
|
} |
|
|
|
|
|
public MailingList enable(boolean enable) throws SQLException { |
|
if (!enable) imap.stop(); |
|
setFlag(STATE_ENABLED,enable); |
|
if (enable) { |
|
setLastError(null); |
|
imap.start().addListener(this); |
|
} |
|
return this; |
|
} |
|
|
|
private void forward(Message message, Stream<ListMember> members) throws MessagingException { |
|
if (hasState(STATE_PUBLIC_ARCHIVE)) storeMessage(message); |
|
String newSender = !hasState(STATE_FORWARD_FROM) ? email() : null; |
|
var receivers = members |
|
.map(ListMember::user) |
|
.map(User::email) |
|
.toList(); |
|
var subject = message.getSubject(); |
|
|
|
if (!subject.contains(stamp())) subject = stamp()+" "+subject; |
|
var replyTo = (newSender == null && hasState(STATE_REPLY_TO_LIST)) ? email() : null; |
|
smtp.forward(newSender,receivers,message,subject,hasState(STATE_FORWARD_ATTACHED),hasState(STATE_HIDE_RECEIVERS),replyTo); |
|
} |
|
|
|
|
|
|
|
public MailingList forwardAttached(boolean forward) throws SQLException { |
|
return setFlag(STATE_FORWARD_ATTACHED,forward); |
|
} |
|
|
|
public MailingList forwardFrom(boolean forward) throws SQLException { |
|
return 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 MailingList hide(boolean hide) throws SQLException { |
|
return setFlag(STATE_PUBLIC,!hide); |
|
} |
|
|
|
public MailingList hideReceivers(boolean hide) throws SQLException { |
|
return setFlag(STATE_HIDE_RECEIVERS,hide); |
|
} |
|
|
|
/** |
|
* test, whether the current ML is subscribable by a given user |
|
* @param user |
|
* @return |
|
*/ |
|
public boolean isOpenFor(User user) { |
|
if (hasState(STATE_PUBLIC)) 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("Konnte Listen-Mitglied nicht laden: ",e); |
|
return false; |
|
} |
|
} |
|
|
|
public boolean isOpenForGuests(){ |
|
return hasState(STATE_OPEN_FOR_GUESTS); |
|
} |
|
|
|
public boolean isOpenForSubscribers(){ |
|
return hasState(STATE_OPEN_FOR_GUESTS|STATE_OPEN_FOR_SUBSCRIBERS); |
|
} |
|
|
|
/** |
|
* 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. |
|
* @param listEmail |
|
* @return |
|
*/ |
|
public static MailingList load(String listEmail) { |
|
if (listEmail == null) return null; |
|
var ml = cache.get(listEmail); |
|
try { |
|
var rs = Database.open() |
|
.select(TABLE_NAME) |
|
.where(EMAIL,listEmail) |
|
.compile().exec(); |
|
if (rs.next()) { |
|
if (ml == null ) ml = MailingList.from(rs); |
|
ml.lastError = rs.getString(LAST_ERROR); |
|
} |
|
} catch (SQLException e) { |
|
LOG.debug("Konnte Mailing-Liste nicht laden: ",e); |
|
} |
|
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. |
|
* @param listEmails |
|
* @return |
|
*/ |
|
public static Set<MailingList> loadAll(Collection<String> listEmails) { |
|
if (listEmails == null) return null; |
|
if (listEmails.isEmpty()) return Set.of(); |
|
var list = new HashSet<MailingList>(); |
|
try { |
|
var rs = Database.open() |
|
.select(TABLE_NAME) |
|
.where(EMAIL,listEmails) |
|
.compile().exec(); |
|
while (rs.next()) list.add(MailingList.from(rs)); |
|
} catch (SQLException e) { |
|
LOG.debug("Konnte Mailing-Listen nicht laden: ",e); |
|
} |
|
return list; |
|
} |
|
|
|
public boolean mayBeAlteredBy(User user) { |
|
if (user.hashPermission(PERMISSION_ADMIN)) return true; |
|
try { |
|
if (ListMember.load(this,user).isModerator()) 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).isOwner()) return true; |
|
} catch (SQLException e) { |
|
LOG.debug("Fehler beim Laden des Listenmitglieds für ({}, {})",user.email(),email()); |
|
} |
|
return false; |
|
} |
|
|
|
public Stream<ListMember> members() throws SQLException { |
|
return ListMember.of(this).stream(); |
|
} |
|
|
|
public boolean membersMayBeListedBy(User user) { |
|
if (user.hashPermission(PERMISSION_ADMIN)) return true; |
|
try { |
|
if (ListMember.load(this,user).isOwner()) 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 |
|
*/ |
|
public HashMap<String, Object> minimalMap() { |
|
var map = new HashMap<String,Object>(); |
|
String[] parts = email.split("@", 2); |
|
map.put(EMAIL,Map.of(PREFIX,parts[0],DOMAIN,parts[1])); |
|
map.put(NAME, name); |
|
map.put(STATE, stateMap()); |
|
map.put(LAST_ERROR, lastError); |
|
return map; |
|
} |
|
|
|
/** |
|
* load the set of mailing lists a given user is allowed to edit |
|
* @param user |
|
* @return |
|
*/ |
|
public static List<MailingList> moderatedBy(User user) { |
|
return ListMember.listsOf(user) |
|
.stream() |
|
.filter(listMember -> listMember.isModerator()) |
|
.map(ListMember::list) |
|
.toList(); |
|
|
|
} |
|
|
|
public Stream<ListMember> moderators() throws SQLException { |
|
return members().filter(ListMember::isModerator); |
|
} |
|
|
|
public boolean modsMayEditMods(){ |
|
return hasState(STATE_MODS_CAN_EDIT_MODS); |
|
} |
|
|
|
public MailingList modsMayNominateMods(boolean allowed) throws SQLException { |
|
return setFlag(STATE_MODS_CAN_EDIT_MODS,allowed); |
|
} |
|
|
|
public String name(){ |
|
return name; |
|
} |
|
|
|
@Override |
|
public void onImapException(MessagingException e) { |
|
try { |
|
if (e instanceof AuthenticationFailedException afe) this.enable(false); |
|
setLastError(e.getMessage()); |
|
} catch (SQLException sqle) { |
|
LOG.warn("Database error during onImapException ({})",e.getMessage(),sqle); |
|
} |
|
} |
|
|
|
@Override |
|
public void onMessageReceived(Message message) throws MessagingException { |
|
LOG.info("Nachricht empfangen: {}",message.getFrom()); |
|
String subject = message.getSubject(); |
|
|
|
try { |
|
if (subject.toLowerCase().contains("undelivered")){ |
|
forward(message,moderators()); |
|
return; |
|
} |
|
|
|
Address from = message.getFrom()[0]; |
|
if (from instanceof InternetAddress internetAddress) { |
|
var senderEmail = internetAddress.getAddress(); |
|
|
|
var user = User.load(senderEmail); |
|
if (user == null) { // no subscription |
|
if (this.isOpenForGuests()) { |
|
forward(message,subscribers()); |
|
} else { |
|
retainMessage(message); |
|
sentRetentionNotification(senderEmail); |
|
|
|
} |
|
return; |
|
} |
|
|
|
var member = ListMember.load(this, user); |
|
if (member == null || member.isAwaiting()) { // no subscription |
|
if (this.isOpenForGuests()) { |
|
forward(message,subscribers()); |
|
} else { |
|
retainMessage(message); |
|
sentRetentionNotification(senderEmail); |
|
} |
|
return; |
|
} |
|
|
|
// at this point the member is at least a subscriber! |
|
|
|
if (member.isModerator() || this.isOpenForSubscribers()) { |
|
forward(message,subscribers()); |
|
} else { |
|
forward(message,moderators()); |
|
} |
|
} |
|
} catch (SQLException e){ |
|
LOG.warn("Failed to process message '{}'",subject,e); |
|
} |
|
|
|
} |
|
|
|
public MailingList openForGuests(boolean open) throws SQLException { |
|
return setFlag(STATE_OPEN_FOR_GUESTS,open); |
|
} |
|
|
|
public MailingList openForSubscribers(boolean open) throws SQLException { |
|
return setFlag(STATE_OPEN_FOR_SUBSCRIBERS,open); |
|
} |
|
|
|
/** |
|
* provide the set of mailing lists that are publicy open to subscriptions |
|
* @return |
|
*/ |
|
public static Set<MailingList> openLists() { |
|
var list = new HashSet<MailingList>(); |
|
try { |
|
var rs = Database.open() |
|
.select(TABLE_NAME,"*", "(" + STATE + " & " + STATE_PUBLIC + ") as test") |
|
.where("test", STATE_PUBLIC) |
|
.compile() |
|
.exec(); |
|
while (rs.next()) list.add(MailingList.from(rs)); |
|
rs.close(); |
|
} catch (SQLException e) { |
|
LOG.warn("Auflisten der Mailinglisten fehlgeschlagen: ", e); |
|
} |
|
return list; |
|
} |
|
|
|
public Stream<ListMember> owners() throws SQLException { |
|
return members().filter(ListMember::isOwner); |
|
} |
|
|
|
public MailingList replyToList(boolean on) throws SQLException { |
|
return setFlag(STATE_REPLY_TO_LIST,on); |
|
} |
|
|
|
private void retainMessage(Message message) { |
|
String subject = "unknown mail"; |
|
try { |
|
subject = message.getSubject(); |
|
imap.move(message, RETAINED_FOLDER); |
|
return; |
|
} catch (MessagingException e){ |
|
LOG.warn("Retaining message {} failed!",subject,e); |
|
} |
|
try { |
|
message.setFlag(Flags.Flag.SEEN, true); |
|
return; |
|
} catch (MessagingException e) { |
|
LOG.warn("Failed to flag message {} as SEEN!",subject,e); |
|
} |
|
|
|
try { |
|
LOG.error("Retaining message {} failed. To avoid dead loop, the MailingList '{}' will be stopped!",subject,email()); |
|
enable(false); |
|
} catch (SQLException sqle) { |
|
LOG.debug("Failed to update list state in database:",sqle); |
|
} |
|
} |
|
/** |
|
* creates a map of the current ML containing all fields but passwords. |
|
* @return |
|
*/ |
|
public Map<String, Object> safeMap() { |
|
var map = minimalMap(); |
|
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()); |
|
if (lastError != null) map.put(LAST_ERROR, lastError); |
|
return map; |
|
} |
|
|
|
|
|
/** |
|
* save the current ML object to the database |
|
* @return |
|
* @throws SQLException |
|
*/ |
|
private MailingList save() throws SQLException { |
|
Database.open() |
|
.insertInto(TABLE_NAME) |
|
.set(EMAIL, email) |
|
.set(NAME, name) |
|
.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()) |
|
.set(SMTP_PASS, smtp.password()) |
|
.set(STATE, state) |
|
.compile() |
|
.run(); |
|
return this; |
|
} |
|
|
|
private void sentRetentionNotification(String senderEmail) { |
|
try { |
|
var receivers = moderators() |
|
.map(ListMember::user) |
|
.map(User::email) |
|
.collect(Collectors.joining(", ")); |
|
var subject = t("Liste '{}' erfordert Aufmerksamkeit!",name()); |
|
var text = t("Diese Liste hat eine E-Mail von {} empfangen. Der Absender ist nicht Mitglied der Liste.\nDie Email wurde in den '{}'-Ordner verschoben.\nSie können die Nachricht manuell weiterleiten oder verwerfen.",senderEmail,RETAINED_FOLDER); |
|
smtp.send(email(), name(), receivers,subject,text); |
|
|
|
subject = t("Ihre Nachricht an {} wurde zurückgewiesen!",email()); |
|
text = t("Sie haben versucht, eine Nachricht an die Liste '{}' zu senden. Das wurde verweigert, da Sie kein Mitglied der Liste (mit entsprechenden Berechtigungen) sind.\n",name()); |
|
if (hasState(STATE_PUBLIC)) text += t("Sie können zu {} gehen und die Liste abonnieren. Versuchen Sie es danach erneut.",Configuration.instance().baseUrl()); |
|
smtp.send(email(), name(), senderEmail,subject,text); |
|
} catch (SQLException e){ |
|
LOG.error("Failed to load list of owners of mailing list. Retention notification was not sent to owners of {}",email(),e); |
|
} catch (MessagingException | UnsupportedEncodingException e){ |
|
LOG.error("Failed to send retention notification to owners of {}",email(),e); |
|
} |
|
} |
|
|
|
private MailingList 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(); |
|
return this; |
|
} |
|
|
|
private void setLastError(String message) throws SQLException { |
|
Database.open().update(TABLE_NAME).set(LAST_ERROR,message).where(EMAIL,email()).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); |
|
if (hasState(STATE_FORWARD_ATTACHED)) map.put("forward_attached",HIDDEN); |
|
if (hasState(STATE_HIDE_RECEIVERS)) map.put("hide_receivers",HIDDEN); |
|
if (hasState(STATE_REPLY_TO_LIST)) map.put("reply_to_list",HIDDEN); |
|
if (hasState(STATE_OPEN_FOR_GUESTS)) map.put("open_for_guests",HIDDEN); |
|
if (hasState(STATE_OPEN_FOR_SUBSCRIBERS)) map.put("open_for_subscribers",HIDDEN); |
|
if (hasState(STATE_PUBLIC_ARCHIVE)) map.put("archive",VISIBLE); |
|
return map; |
|
} |
|
|
|
/** |
|
* Provides the set of MailingLists subscribable to visitors. Returns the same value as subscribable(null); |
|
* @return |
|
*/ |
|
public static Set<MailingList> subscribable() { |
|
return subscribable(null); |
|
} |
|
|
|
/** |
|
* Provides the set of MailingLists subscribable to a given user. |
|
* If the user is null, the set of openLists is returned. |
|
* If the user is an admin, the set of all MLs is returned. |
|
* Otherwise the set of MLs owned by the given user + the set of openLists is returned |
|
* @param user |
|
* @return |
|
*/ |
|
public static Set<MailingList> subscribable(User user) { |
|
try { |
|
if (user == null) return openLists(); |
|
if (user.hashPermission(PERMISSION_ADMIN)) { |
|
var rs = Database.open().select(TABLE_NAME).compile().exec(); |
|
var result = new HashSet<MailingList>(); |
|
while (rs.next()) result.add(MailingList.from(rs)); |
|
rs.close(); |
|
return result; |
|
} |
|
var listEmails = ListMember.listsOwnedBy(user); |
|
var rs = Database.open().select(TABLE_NAME).where(EMAIL, listEmails).compile().exec(); |
|
var result = openLists(); |
|
while (rs.next()) result.add(MailingList.from(rs)); |
|
rs.close(); |
|
return result; |
|
} catch (SQLException e) { |
|
LOG.warn("Lesen der abbonnierbaren Mailinglisten von {} fehlgeschlagen.",user,e); |
|
return Set.of(); |
|
} |
|
} |
|
|
|
private Stream<ListMember> subscribers() throws SQLException { |
|
return members().filter(ListMember::isSubscriber); |
|
} |
|
|
|
/** |
|
* Request list subscription for the given user. |
|
* Usually creates a ListMember entry with AWAITING_CONFIRMATION state is created and a confirmation request email is sent. |
|
* However, if skipConfirmation is set to true, no email is sent and the ListMember's state is set to SUBSCRIBER immediately. |
|
* @param user |
|
* @param skipConfirmation |
|
* @throws SQLException |
|
* @throws MessagingException |
|
*/ |
|
public void requestSubscription(User user, boolean skipConfirmation, ST template) throws SQLException, MessagingException { |
|
var state = skipConfirmation ? ListMember.STATE_SUBSCRIBER : ListMember.STATE_AWAITING_CONFIRMATION; |
|
var member = ListMember.create(this,user,state); |
|
if (skipConfirmation) return; |
|
try { |
|
var config = Configuration.instance(); |
|
var url = new StringBuilder(config.baseUrl()).append("/web/confirm?token=").append(member.token()).toString(); |
|
var subject = t("[{}] Bitte bestätigen Sie Ihr Listen-Abonnement",name()); |
|
var text = template.add(URL,url).add(LIST_NAME,name()).render(); |
|
smtp.send(email(),name(),user.email(),subject,text); |
|
} catch (UnsupportedEncodingException e) { |
|
throw new MessagingException(t("Senden der Email an {} fehlgeschlagen",user.email()),e); |
|
} |
|
} |
|
|
|
protected SmtpClient smtp(){ |
|
return smtp; |
|
} |
|
|
|
private String stamp() { |
|
return "["+name+"]"; |
|
} |
|
|
|
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) { |
|
Post.create(this,message); |
|
} |
|
|
|
|
|
/** |
|
* send a test email to the provided user |
|
* @param user |
|
* @throws MessagingException |
|
* @throws UnsupportedEncodingException |
|
*/ |
|
public void test(User user) throws MessagingException, UnsupportedEncodingException { |
|
var subject = t("{}: Test-Mail",name()); |
|
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(); |
|
} |
|
} |
|
|
|
public void update(String name, String email, String imapHost, Integer imapPort, String imapUser, String imapPass, String inbox, String smtpHost, Integer smtpPort, String smtpUser, String smtpPass) throws SQLException { |
|
imap.stop(); |
|
Database.open() |
|
.update(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(INBOX,inbox) |
|
.set(SMTP_HOST, smtpHost) |
|
.set(SMTP_PORT, smtpPort) |
|
.set(SMTP_USER, smtpUser) |
|
.set(SMTP_PASS, smtpPass) |
|
.where(EMAIL,email()) |
|
.compile() |
|
.run(); |
|
if (!this.email.equals(email)){ |
|
ListMember.updateListEmail(this.email,email); |
|
this.email = email; |
|
} |
|
this.name = name; |
|
smtp = new SmtpClient(smtpHost,smtpPort,smtpUser,smtpPass); |
|
imap = new ImapClient(imapHost,imapPort,imapUser,imapPass,inbox,this); |
|
if (this.hasState(STATE_ENABLED)) imap.start().addListener(this); |
|
} |
|
}
|
|
|