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.
633 lines
25 KiB
633 lines
25 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.SmtpClient; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
|
|
import javax.mail.Address; |
|
import javax.mail.Flags; |
|
import javax.mail.Message; |
|
import javax.mail.MessagingException; |
|
import javax.mail.internet.AddressException; |
|
import javax.mail.internet.InternetAddress; |
|
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 { |
|
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"; |
|
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_CREATE_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 final String name; |
|
private final String email; |
|
private int state; |
|
private final SmtpClient smtp; |
|
private final 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); |
|
} |
|
|
|
public MailingList archive(boolean enabled) throws SQLException { |
|
return setFlag(STATE_PUBLIC_ARCHIVE,enabled); |
|
} |
|
|
|
/** |
|
* 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(");"); |
|
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 MailingList enable(boolean enable) throws SQLException { |
|
if (!enable) imap.stop(); |
|
setFlag(STATE_ENABLED,enable); |
|
if (enable) imap.start().addListener(this); |
|
return this; |
|
} |
|
|
|
private void forward(Message message) throws MessagingException { |
|
try { |
|
String newSender = !hasState(STATE_FORWARD_FROM) ? email() : null; |
|
var receivers = members() |
|
.stream() |
|
.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); |
|
} catch (SQLException e) { |
|
LOG.error("Failed to read list members of {} from database. Cannot forward message!",email(),e); |
|
} |
|
} |
|
|
|
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; |
|
} |
|
|
|
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; |
|
} |
|
|
|
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); |
|
} |
|
|
|
public boolean isAllowedSender(String senderEmail){ |
|
if (senderEmail == null || senderEmail.isBlank()) return false; |
|
try { |
|
var user = User.load(senderEmail); |
|
if (user == null) return this.isOpenForGuests(); |
|
var member = ListMember.load(this,user); |
|
if (member == null) return this.isOpenForGuests(); |
|
if (member.isModerator()) return true; |
|
if (member.isSubscriber()) return this.isOpenForSubscribers(); |
|
} catch (SQLException e) { |
|
LOG.warn("Failed to load User for {}",senderEmail,e); |
|
} |
|
return this.isOpenForGuests(); |
|
} |
|
/** |
|
* 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("Was not able to load ListMember: ",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); |
|
if (ml == null) try { |
|
var rs = Database.open() |
|
.select(TABLE_NAME) |
|
.where(EMAIL,listEmail) |
|
.compile().exec(); |
|
if (rs.next()) ml = MailingList.from(rs); |
|
} catch (SQLException e) { |
|
LOG.debug("Failed to load MailingList: ",e); |
|
} |
|
return ml; |
|
} |
|
|
|
public boolean mayBeAlteredBy(User user) { |
|
if (user.hashPermission(PERMISSION_ADMIN)) return true; |
|
try { |
|
if (ListMember.load(this,user).isOwner()) return true; |
|
} catch (SQLException e) { |
|
LOG.debug("Error loading list member for ({}, {})",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("Error loading list member for ({}, {})",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).isOwner()) return true; |
|
} catch (SQLException e) { |
|
LOG.debug("Error loading list member for ({}, {})",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()); |
|
return map; |
|
} |
|
|
|
public String name(){ |
|
return name; |
|
} |
|
|
|
@Override |
|
public void onMessageReceived(Message message) throws MessagingException { |
|
LOG.info("Message received: {}",message.getFrom()); |
|
String subject = message.getSubject(); |
|
if (subject.toLowerCase().contains("undelivered")){ |
|
try { |
|
var receivers = members().stream().filter(ListMember::isOwner).map(ListMember::user).map(User::email).toList(); |
|
smtp.forward(email(), receivers, message, message.getSubject(), false,false,null); |
|
} catch (SQLException e){ |
|
LOG.error("Was not able to load members of {}; Non-Delivery notification dropped!",this.email(),e); |
|
} |
|
return; |
|
} |
|
|
|
Address from = message.getFrom()[0]; |
|
if (from instanceof InternetAddress internetAddress){ |
|
var senderEmail = ((InternetAddress) from).getAddress(); |
|
if (!isAllowedSender(senderEmail)) { |
|
retainMessage(message); |
|
sentRetentionNotification(senderEmail); |
|
return; |
|
} |
|
} |
|
if (hasState(STATE_PUBLIC_ARCHIVE)) storeMessage(message); |
|
forward(message); |
|
} |
|
|
|
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("Listing mailing lists failed: ", e); |
|
} |
|
return list; |
|
} |
|
|
|
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()); |
|
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; |
|
} |
|
|
|
/** |
|
* send an email to a potential subscriber asking for confirmation |
|
* @param user |
|
* @param token |
|
* @throws MessagingException |
|
* @throws UnsupportedEncodingException |
|
*/ |
|
private void sendConfirmationRequest(User user, String token) throws MessagingException, UnsupportedEncodingException { |
|
var subject = t("Please confirm your list subscription"); |
|
var config = Configuration.instance(); |
|
var url = new StringBuilder(config.baseUrl()).append("/confirm?token=").append(token); |
|
var text = t("Please go to {} in order to complete your list subscription!",url); |
|
smtp.send(email(),name(),user.email(),subject,text); |
|
} |
|
|
|
private void sentRetentionNotification(String senderEmail) { |
|
try { |
|
var receivers = members() |
|
.stream() |
|
.filter(ListMember::isOwner) |
|
.map(ListMember::user) |
|
.map(User::email) |
|
.collect(Collectors.joining(", ")); |
|
var subject = t("List '{}' requires attention!",name()); |
|
var text = t("This list received an email from {}, who is not member of the list.\nThe email has been moved to the '{}' folder.\nYou may manually forward this message or drop it.",senderEmail,RETAINED_FOLDER); |
|
smtp.send(email(), name(), receivers,subject,text); |
|
|
|
subject = t("Your message to {} was rejected!",email()); |
|
text = t("You have tried to send a message to the list '{}', which failed. This is because you are not a member of this list.\n",name()); |
|
if (hasState(STATE_PUBLIC)) text += t("You may go to {} and subscribe to the list, then try again.",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; |
|
} |
|
|
|
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("Failed to read subscribable mailinglists for {}",user,e); |
|
return Set.of(); |
|
} |
|
} |
|
|
|
/** |
|
* 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) throws SQLException, MessagingException { |
|
var state = skipConfirmation ? ListMember.STATE_SUBSCRIBER : ListMember.STATE_AWAITING_CONFIRMATION; |
|
var member = ListMember.create(this,user,state); |
|
if (skipConfirmation) return; |
|
try { |
|
sendConfirmationRequest(user, member.token()); |
|
} catch (UnsupportedEncodingException e) { |
|
throw new MessagingException(t("Failed to send email to {}",user.email()),e); |
|
} |
|
} |
|
|
|
|
|
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 = "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<InternetAddress> 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<InternetAddress>().stream(); |
|
} |
|
} |
|
}
|
|
|