Browse Source

moved imap credentials from MailingList to ImapClient

drop_old_mail
Stephan Richter 3 years ago
parent
commit
d7668dadaa
  1. 85
      doc/data structure.dia
  2. 2
      pom.xml
  3. 2
      src/main/java/de/srsoftware/widerhall/Constants.java
  4. 34
      src/main/java/de/srsoftware/widerhall/data/MailingList.java
  5. 26
      src/main/java/de/srsoftware/widerhall/mail/Archiver.java
  6. 6
      src/main/java/de/srsoftware/widerhall/mail/Forwarder.java
  7. 53
      src/main/java/de/srsoftware/widerhall/mail/ImapClient.java
  8. 13
      src/main/java/de/srsoftware/widerhall/web/Web.java
  9. 4
      static/templates/add_list.st
  10. 1
      static/templates/js.st
  11. 3
      static/templates/listadminlist.st

85
doc/data structure.dia

@ -868,13 +868,13 @@ @@ -868,13 +868,13 @@
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O18">
<dia:attribute name="obj_pos">
<dia:point val="12,19"/>
<dia:point val="12,21"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,18.95;17.05,21.05"/>
<dia:rectangle val="11.95,20.95;17.05,23.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,19"/>
<dia:point val="12,21"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
@ -900,7 +900,7 @@ @@ -900,7 +900,7 @@
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,20.1941"/>
<dia:point val="14.5,22.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
@ -913,13 +913,13 @@ @@ -913,13 +913,13 @@
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O19">
<dia:attribute name="obj_pos">
<dia:point val="12,21"/>
<dia:point val="12,23"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,20.95;17.05,23.05"/>
<dia:rectangle val="11.95,22.95;17.05,25.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,21"/>
<dia:point val="12,23"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
@ -945,7 +945,7 @@ @@ -945,7 +945,7 @@
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,22.1941"/>
<dia:point val="14.5,24.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
@ -958,13 +958,13 @@ @@ -958,13 +958,13 @@
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O20">
<dia:attribute name="obj_pos">
<dia:point val="12,23"/>
<dia:point val="12,25"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,22.95;17.05,25.05"/>
<dia:rectangle val="11.95,24.95;17.05,27.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,23"/>
<dia:point val="12,25"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
@ -990,7 +990,7 @@ @@ -990,7 +990,7 @@
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,24.1941"/>
<dia:point val="14.5,26.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
@ -1003,13 +1003,13 @@ @@ -1003,13 +1003,13 @@
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O21">
<dia:attribute name="obj_pos">
<dia:point val="12,25"/>
<dia:point val="12,27"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,24.95;17.05,27.05"/>
<dia:rectangle val="11.95,26.95;17.05,29.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,25"/>
<dia:point val="12,27"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
@ -1035,7 +1035,7 @@ @@ -1035,7 +1035,7 @@
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,26.1941"/>
<dia:point val="14.5,28.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
@ -1562,13 +1562,13 @@ @@ -1562,13 +1562,13 @@
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O34">
<dia:attribute name="obj_pos">
<dia:point val="12,27"/>
<dia:point val="12,29"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,26.95;17.05,29.05"/>
<dia:rectangle val="11.95,28.95;17.05,31.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,27"/>
<dia:point val="12,29"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
@ -1594,7 +1594,7 @@ @@ -1594,7 +1594,7 @@
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,28.1941"/>
<dia:point val="14.5,30.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
@ -1650,5 +1650,50 @@ @@ -1650,5 +1650,50 @@
</dia:composite>
</dia:attribute>
</dia:object>
<dia:object type="Flowchart - Box" version="0" id="O36">
<dia:attribute name="obj_pos">
<dia:point val="12,19"/>
</dia:attribute>
<dia:attribute name="obj_bb">
<dia:rectangle val="11.95,18.95;17.05,21.05"/>
</dia:attribute>
<dia:attribute name="elem_corner">
<dia:point val="12,19"/>
</dia:attribute>
<dia:attribute name="elem_width">
<dia:real val="5"/>
</dia:attribute>
<dia:attribute name="elem_height">
<dia:real val="2"/>
</dia:attribute>
<dia:attribute name="show_background">
<dia:boolean val="true"/>
</dia:attribute>
<dia:attribute name="padding">
<dia:real val="0.5"/>
</dia:attribute>
<dia:attribute name="text">
<dia:composite type="text">
<dia:attribute name="string">
<dia:string>#inbox#</dia:string>
</dia:attribute>
<dia:attribute name="font">
<dia:font family="sans" style="0" name="Helvetica"/>
</dia:attribute>
<dia:attribute name="height">
<dia:real val="0.80000000000000004"/>
</dia:attribute>
<dia:attribute name="pos">
<dia:point val="14.5,20.1941"/>
</dia:attribute>
<dia:attribute name="color">
<dia:color val="#000000ff"/>
</dia:attribute>
<dia:attribute name="alignment">
<dia:enum val="1"/>
</dia:attribute>
</dia:composite>
</dia:attribute>
</dia:object>
</dia:layer>
</dia:diagram>

2
pom.xml

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
<groupId>org.example</groupId>
<artifactId>Widerhall</artifactId>
<version>0.0.13</version>
<version>0.0.14</version>
<build>
<plugins>
<plugin>

2
src/main/java/de/srsoftware/widerhall/Constants.java

@ -5,10 +5,10 @@ public class Constants { @@ -5,10 +5,10 @@ public class Constants {
public static final String BASE_URL = "base_url";
public static final String CONFIG = "configuration";
public static final String DB = "database";
public static final String DEFAULT_INBOX = "INBOX";
public static final String DOMAIN = "domain";
public static final String EMAIL = "email";
public static final String ERROR = "error";
public static final String HOST = "host";
public static final String IMAPS = "imaps";
public static final String INBOX = "inbox";
public static final String INDEX = "index";

34
src/main/java/de/srsoftware/widerhall/data/MailingList.java

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
package de.srsoftware.widerhall.data;
import de.srsoftware.widerhall.Configuration;
import de.srsoftware.widerhall.mail.ImapClient;
import de.srsoftware.widerhall.mail.SmtpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -34,10 +35,9 @@ public class MailingList { @@ -34,10 +35,9 @@ public class MailingList {
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 final ImapClient imap;
private static final HashMap<String,MailingList> lists = new HashMap<>();
@ -55,15 +55,12 @@ public class MailingList { @@ -55,15 +55,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 +78,8 @@ public class MailingList { @@ -81,8 +78,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 +96,7 @@ public class MailingList { @@ -99,6 +96,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(", ")
@ -168,6 +166,7 @@ public class MailingList { @@ -168,6 +166,7 @@ public class MailingList {
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),
@ -241,12 +240,14 @@ public class MailingList { @@ -241,12 +240,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,stateString(state));
return map;
}
@ -261,10 +262,11 @@ public class MailingList { @@ -261,10 +262,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())

26
src/main/java/de/srsoftware/widerhall/mail/Archiver.java

@ -0,0 +1,26 @@ @@ -0,0 +1,26 @@
package de.srsoftware.widerhall.mail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.mail.Message;
import javax.mail.MessagingException;
import java.io.UnsupportedEncodingException;
public class Archiver implements MessageHandler {
private static final Logger LOG = LoggerFactory.getLogger(Archiver.class);
private final SmtpClient smtp;
private final String receiver,sender;
public Archiver(String host, int port, String username, String password, String sender, String receiver) {
this.sender = sender;
this.receiver = receiver;
SmtpClient smtp = new SmtpClient(host,port,username,password);
this.smtp = smtp;
}
@Override
public void onMessageReceived(Message message) throws MessagingException {
LOG.debug("storing {}",message.getSubject());
}
}

6
src/main/java/de/srsoftware/widerhall/mail/Forwarder.java

@ -23,11 +23,5 @@ public class Forwarder implements MessageHandler { @@ -23,11 +23,5 @@ public class Forwarder implements MessageHandler {
@Override
public void onMessageReceived(Message message) throws MessagingException {
LOG.debug("forwarding {}",message.getSubject());
try {
smtp.send(sender,"Stephan Richter",receiver,"Info: "+message.getSubject(),"Neue Mail eingegangen!");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

53
src/main/java/de/srsoftware/widerhall/mail/ImapClient.java

@ -13,13 +13,12 @@ import java.util.Properties; @@ -13,13 +13,12 @@ import java.util.Properties;
public class ImapClient {
private static final Logger LOG = LoggerFactory.getLogger(ImapClient.class);
private final JSONObject config;
private boolean stopped = false;
private final int port;
private final String host, username, password, folderName;
private boolean stopped = true;
private IMAPFolder inbox;
private HashSet<MessageHandler> listeners = new HashSet<>();
private class ListeningThread extends Thread {
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
@ -41,10 +40,6 @@ public class ImapClient { @@ -41,10 +40,6 @@ public class ImapClient {
}
private void openInbox() throws MessagingException {
String host = (String) config.get(Constants.HOST);
String username = (String) config.get(Constants.USER);
String password = (String) config.get(Constants.PASSWORD);
String folderName = (String) config.get(Constants.INBOX);
LOG.debug("Connecting and logging in…");
Properties props = imapProps();
Session session = Session.getInstance(props);
@ -105,20 +100,50 @@ public class ImapClient { @@ -105,20 +100,50 @@ public class ImapClient {
}
}
}
public ImapClient(JSONObject config){
this.config = config;
public ImapClient(String host, int port, String username, String password, String folderName) {
this.host = host;
this.port = port;
this.username = username;
this.password = password;
this.folderName = folderName;
}
public ImapClient addListener(MessageHandler messageHandler) {
listeners.add(messageHandler);
return this;
}
public String host(){
return host;
}
public String username(){
return username;
}
public String password(){
return password;
}
public int port(){
return port;
}
public String folderName(){
return folderName;
}
public void start() {
stopped = false;
LOG.debug("Creating ListeningThread…");
new ListeningThread().start();
LOG.debug("Creating Heartbeat…");
new Heartbeat().start();
}
public ImapClient addListener(MessageHandler messageHandler) {
listeners.add(messageHandler);
return this;
public void stop(){
stopped = true;
}
}

13
src/main/java/de/srsoftware/widerhall/web/Web.java

@ -1,25 +1,16 @@ @@ -1,25 +1,16 @@
package de.srsoftware.widerhall.web;
import de.srsoftware.widerhall.Configuration;
import de.srsoftware.widerhall.Constants;
import de.srsoftware.widerhall.Util;
import de.srsoftware.widerhall.data.ListMember;
import de.srsoftware.widerhall.data.MailingList;
import de.srsoftware.widerhall.data.User;
import org.antlr.runtime.MismatchedTokenException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STRawGroupDir;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.security.InvalidKeyException;
import java.sql.SQLException;
import java.util.HashMap;
@ -76,6 +67,8 @@ public class Web extends TemplateServlet { @@ -76,6 +67,8 @@ public class Web extends TemplateServlet {
data.put(IMAP_USER, imapUser);
var imapPass = req.getParameter(IMAP_PASS);
var smtpHost = req.getParameter(SMTP_HOST);
var inbox = req.getParameter(INBOX);
if (inbox == null) inbox = DEFAULT_INBOX;
data.put(SMTP_HOST, smtpHost);
var smtpUser = req.getParameter(SMTP_USER);
data.put(SMTP_USER, smtpUser);
@ -125,7 +118,7 @@ public class Web extends TemplateServlet { @@ -125,7 +118,7 @@ public class Web extends TemplateServlet {
}
try {
var list = MailingList.create(email, name, imapHost, imapPort, imapUser, imapPass, smtpHost, smtpPort, smtpUser, smtpPass);
var list = MailingList.create(email, name, imapHost, imapPort, imapUser, imapPass, inbox, smtpHost, smtpPort, smtpUser, smtpPass);
ListMember.create(list, user, ListMember.STATE_OWNER);
return redirectTo(INDEX, resp);
} catch (SQLException e) {

4
static/templates/add_list.st

@ -43,6 +43,10 @@ @@ -43,6 +43,10 @@
<input type="password" name="imap_pass" value="«data.imap_pass»" id="imap_pass" />
Password
</label>
<label>
<input type="text" name="inbox" value="«data.inbox»" id="imap_inbox" />
Inbox name
</label>
</fieldset>
<fieldset>
<legend>SMTP protocol</legend>

1
static/templates/js.st

@ -78,6 +78,7 @@ function showListOfEditableLists(data){ @@ -78,6 +78,7 @@ function showListOfEditableLists(data){
$('<td/>').text(list.imap_host).appendTo(row);
$('<td/>').text(list.imap_port).appendTo(row);
$('<td/>').text(list.imap_user).appendTo(row);
$('<td/>').text(list.inbox).appendTo(row);
$('<td/>').text(list.smtp_host).appendTo(row);
$('<td/>').text(list.smtp_port).appendTo(row);
$('<td/>').text(list.smtp_user).appendTo(row);

3
static/templates/listadminlist.st

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
<table id="listlist">
<tr>
<th colspan="4">List</th>
<th colspan="3">IMAP</th>
<th colspan="4">IMAP</th>
<th colspan="3">SMTP</th>
</tr>
<tr>
@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
<th>Host</th>
<th>Port</th>
<th>User</th>
<th>Inbox</th>
<th>Host</th>
<th>Port</th>
<th>User</th>

Loading…
Cancel
Save