376 lines
14 KiB
Java
376 lines
14 KiB
Java
package de.srsoftware.widerhall.data;
|
|
|
|
import de.srsoftware.widerhall.Configuration;
|
|
import de.srsoftware.widerhall.mail.SmtpClient;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import javax.mail.MessagingException;
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.*;
|
|
|
|
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 {
|
|
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;
|
|
private static final int STATE_PUBLIC = 2;
|
|
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 static final HashMap<String,MailingList> lists = 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 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);
|
|
}
|
|
|
|
/**
|
|
* 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 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();
|
|
}
|
|
|
|
/**
|
|
* 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(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();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
/**
|
|
* test, whether the current ML is subscribable by a given user
|
|
* @param user
|
|
* @return
|
|
*/
|
|
public boolean isOpenFor(User user) {
|
|
if ((state & STATE_PUBLIC) > 0) 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 ListeMember 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.
|
|
* @param listEmail
|
|
* @return
|
|
*/
|
|
public static MailingList load(String listEmail) {
|
|
if (listEmail == null) return null;
|
|
var ml = lists.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("Konnte MailingList nicht laden: ",e);
|
|
}
|
|
return ml;
|
|
}
|
|
|
|
/**
|
|
* 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, stateString(state));
|
|
return map;
|
|
}
|
|
|
|
public String name(){
|
|
return name;
|
|
}
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
|
|
/**
|
|
* creates a map of the current ML containing all fields but passwords.
|
|
* @return
|
|
*/
|
|
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 (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());
|
|
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, imapHost)
|
|
.set(IMAP_PORT, imapPort)
|
|
.set(IMAP_USER, imapUser)
|
|
.set(IMAP_PASS, imapPass)
|
|
.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("Bitte bestätigen Sie ihr Listen-Abonnement");
|
|
var config = Configuration.instance();
|
|
var url = new StringBuilder(config.baseUrl()).append("/confirm?token=").append(token);
|
|
var text = t("Botte gehen Sie zu {} um das Abonnieren der Liste abzuschließen!",url);
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 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 für {} abbonnierbaren Mailinglisten fehlgeschlagen.",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);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
}
|