|
|
|
@ -4,6 +4,8 @@ import org.slf4j.Logger;
@@ -4,6 +4,8 @@ import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import java.sql.SQLException; |
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
import static de.srsoftware.widerhall.Constants.*; |
|
|
|
@ -18,17 +20,29 @@ public class MailingList {
@@ -18,17 +20,29 @@ public class MailingList {
|
|
|
|
|
private static final String SMTP_PORT = "smtp_port"; |
|
|
|
|
private static final String SMTP_USER = "smtp_user"; |
|
|
|
|
private static final String SMTP_PASS = "smtp_pass"; |
|
|
|
|
private static final int ENABLED = 1; |
|
|
|
|
private final String name; |
|
|
|
|
private final String email; |
|
|
|
|
public static final String TABLE_NAME = "Lists"; |
|
|
|
|
private final String imapPass,smtpPass,imapHost,smtpHost,imapUser,smtpUser; |
|
|
|
|
private final int imapPort,smtpPort,state; |
|
|
|
|
|
|
|
|
|
public MailingList(String email, String name){ |
|
|
|
|
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.smtpHost = smtpHost; |
|
|
|
|
this.smtpPort = smtpPort; |
|
|
|
|
this.smtpUser = smtpUser; |
|
|
|
|
this.smtpPass = smtpPass; |
|
|
|
|
this.state = state; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static MailingList create(String email, String name) throws SQLException { |
|
|
|
|
return new MailingList(email,name).save(); |
|
|
|
|
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,ENABLED).save(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void createTable() throws SQLException { |
|
|
|
@ -36,22 +50,75 @@ public class MailingList {
@@ -36,22 +50,75 @@ public class MailingList {
|
|
|
|
|
.append("CREATE TABLE ").append(TABLE_NAME) |
|
|
|
|
.append(" (") |
|
|
|
|
.append(EMAIL).append(" ").append(VARCHAR).append(" NOT NULL PRIMARY KEY, ") |
|
|
|
|
.append(NAME).append(" ").append(VARCHAR) |
|
|
|
|
.append(IMAP_HOST).append(" ").append(VARCHAR) |
|
|
|
|
.append(IMAP_PORT).append(" ").append(INT) |
|
|
|
|
.append(IMAP_USER).append(" ").append(VARCHAR) |
|
|
|
|
.append(IMAP_PASS).append(" ").append(VARCHAR) |
|
|
|
|
.append(SMTP_HOST).append(" ").append(VARCHAR) |
|
|
|
|
.append(SMTP_PORT).append(" ").append(INT) |
|
|
|
|
.append(SMTP_USER).append(" ").append(VARCHAR) |
|
|
|
|
.append(SMTP_PASS).append(" ").append(VARCHAR) |
|
|
|
|
.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.toString()).run(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static List<MailingList> listsOf(User user) { |
|
|
|
|
var list = new ArrayList<MailingList>(); |
|
|
|
|
try { |
|
|
|
|
var rs = Database.open().query("SELECT * FROM "+TABLE_NAME).exec(); |
|
|
|
|
while (rs.next()){ |
|
|
|
|
var email = rs.getString(EMAIL); |
|
|
|
|
var name = rs.getString(NAME); |
|
|
|
|
var imapHost = rs.getString(IMAP_HOST); |
|
|
|
|
var imapPort = rs.getInt(IMAP_PORT); |
|
|
|
|
var imapUser = rs.getString(IMAP_USER); |
|
|
|
|
var imapPass = rs.getString(IMAP_PASS); |
|
|
|
|
var smtpHost = rs.getString(SMTP_HOST); |
|
|
|
|
var smtpPort = rs.getInt(SMTP_PORT); |
|
|
|
|
var smtpUser = rs.getString(SMTP_USER); |
|
|
|
|
var smtpPass = rs.getString(SMTP_PASS); |
|
|
|
|
var state = rs.getInt(STATE); |
|
|
|
|
list.add(new MailingList(email,name,imapHost,imapPort,imapUser,imapPass,smtpHost,smtpPort,smtpUser,smtpPass,state)); |
|
|
|
|
} |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.warn("Listing mailing lists failed: ",e); |
|
|
|
|
} |
|
|
|
|
return list; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Map<String,Object> safeMap() { |
|
|
|
|
return Map.of(EMAIL,email,NAME,name, |
|
|
|
|
IMAP_HOST,imapHost,IMAP_PORT,imapPort,IMAP_USER,imapUser, |
|
|
|
|
SMTP_HOST,smtpHost,SMTP_PORT,smtpPort,SMTP_USER,smtpUser, |
|
|
|
|
STATE,stateName(state)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static String stateName(int state) { |
|
|
|
|
switch (state){ |
|
|
|
|
case ENABLED: |
|
|
|
|
return "enabled"; |
|
|
|
|
default: |
|
|
|
|
return "disabled"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private MailingList save() throws SQLException { |
|
|
|
|
Database.open().insertInto(TABLE_NAME) |
|
|
|
|
.values(Map.of(EMAIL,email,NAME,name)) |
|
|
|
|
.values(Map.ofEntries( |
|
|
|
|
Map.entry(EMAIL,email), |
|
|
|
|
Map.entry(NAME,name), |
|
|
|
|
Map.entry(IMAP_HOST,imapHost), |
|
|
|
|
Map.entry(IMAP_PORT,imapPort), |
|
|
|
|
Map.entry(IMAP_USER,imapUser), |
|
|
|
|
Map.entry(IMAP_PASS,imapPass), |
|
|
|
|
Map.entry(SMTP_HOST,smtpHost), |
|
|
|
|
Map.entry(SMTP_PORT,smtpPort), |
|
|
|
|
Map.entry(SMTP_USER,smtpUser), |
|
|
|
|
Map.entry(SMTP_PASS,smtpPass), |
|
|
|
|
Map.entry(STATE,state))) |
|
|
|
|
.run(); |
|
|
|
|
return this; |
|
|
|
|
} |
|
|
|
|