Merge branch 'main' into lang_de
This commit is contained in:
@@ -1,33 +0,0 @@
|
||||
package de.srsoftware.widerhall.mail;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.Message;
|
||||
import javax.mail.MessagingException;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
|
||||
public class Forwarder implements MessageHandler {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Forwarder.class);
|
||||
private final SmtpClient smtp;
|
||||
private final String receiver,sender;
|
||||
|
||||
public Forwarder(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("forwarding {}",message.getSubject());
|
||||
|
||||
try {
|
||||
smtp.send(sender,"Stephan Richter",receiver,"Info: "+message.getSubject(),"Neue Mail eingegangen!");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,6 @@ package de.srsoftware.widerhall.mail;
|
||||
|
||||
import com.sun.mail.imap.IMAPFolder;
|
||||
import de.srsoftware.widerhall.Constants;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -13,56 +12,63 @@ 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 IMAPFolder inbox;
|
||||
private HashSet<MessageHandler> listeners = new HashSet<>();
|
||||
|
||||
|
||||
private ListeningThread listeningThread;
|
||||
private Heartbeat heartbeat;
|
||||
|
||||
private class ListeningThread extends Thread {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ListeningThread.class);
|
||||
private HashSet<MessageHandler> listeners = new HashSet<>();
|
||||
private boolean stopped = false;
|
||||
|
||||
public ListeningThread addListener(MessageHandler messageHandler) {
|
||||
listeners.add(messageHandler);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ListeningThread dropListeners() {
|
||||
listeners.clear();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped) {
|
||||
try {
|
||||
sleep(5000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
interruptedException.printStackTrace();
|
||||
}
|
||||
try {
|
||||
openInbox();
|
||||
} catch (MessagingException e){
|
||||
LOG.warn("Connection problem:",e);
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException interruptedException) {
|
||||
interruptedException.printStackTrace();
|
||||
}
|
||||
LOG.warn("Verbindung-Problem:",e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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…");
|
||||
LOG.debug("Verbinden und Einloggen…");
|
||||
Properties props = imapProps();
|
||||
Session session = Session.getInstance(props);
|
||||
Store store = session.getStore(Constants.IMAPS);
|
||||
store.connect(host,username,password);
|
||||
LOG.debug("Connected, opening {}:",folderName);
|
||||
LOG.debug("Verbunden. Öffne {}:",folderName);
|
||||
inbox = (IMAPFolder)store.getFolder(folderName);
|
||||
inbox.open(IMAPFolder.READ_WRITE);
|
||||
|
||||
while (!stopped){
|
||||
handleMessages();
|
||||
LOG.debug("Idling.");
|
||||
LOG.debug("Warte.");
|
||||
inbox.idle(true);
|
||||
}
|
||||
}
|
||||
|
||||
private void handleMessages() throws MessagingException {
|
||||
LOG.debug("reading email:");
|
||||
LOG.debug("Lese E-Mail:");
|
||||
if (!inbox.isOpen()){
|
||||
inbox.open(IMAPFolder.READ_WRITE);
|
||||
}
|
||||
@@ -74,7 +80,7 @@ public class ImapClient {
|
||||
}
|
||||
|
||||
private void handle(Message message) throws MessagingException {
|
||||
LOG.debug("Handling {}",message.getSubject());
|
||||
LOG.debug("Verarbeite {}",message.getSubject());
|
||||
for (MessageHandler listener : listeners) listener.onMessageReceived(message);
|
||||
}
|
||||
|
||||
@@ -83,18 +89,25 @@ public class ImapClient {
|
||||
props.put(Constants.PROTOCOL,Constants.IMAPS);
|
||||
return props;
|
||||
}
|
||||
|
||||
public ListeningThread doStop() {
|
||||
stopped = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
private class Heartbeat extends Thread{
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class);
|
||||
private boolean stopped = false;
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (!stopped){
|
||||
try {
|
||||
sleep(300034);
|
||||
if (inbox != null) continue;
|
||||
LOG.debug("sending NOOP");
|
||||
LOG.debug("Sende NOOP");
|
||||
inbox.doCommand(protocol -> {
|
||||
protocol.simpleCommand("NOOP",null);
|
||||
return null;
|
||||
@@ -104,21 +117,69 @@ public class ImapClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public ImapClient(JSONObject config){
|
||||
this.config = config;
|
||||
|
||||
public Heartbeat doStop() {
|
||||
stopped = true;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public void start() {
|
||||
LOG.debug("Creating ListeningThread…");
|
||||
new ListeningThread().start();
|
||||
LOG.debug("Creating Heartbeat…");
|
||||
new Heartbeat().start();
|
||||
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);
|
||||
if (listeningThread == null) listeningThread = new ListeningThread();
|
||||
listeningThread.addListener(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 ImapClient start() {
|
||||
stop();
|
||||
|
||||
LOG.debug("Erzeuge ListeningThread…");
|
||||
(listeningThread = new ListeningThread()).start();
|
||||
|
||||
LOG.debug("Erzeuge Heartbeat…");
|
||||
(heartbeat = new Heartbeat()).start();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public ImapClient stop(){
|
||||
if (listeningThread != null) {
|
||||
LOG.debug("Stoppe alten ListeningThread…");
|
||||
listeningThread.dropListeners().doStop();
|
||||
listeningThread = null;
|
||||
}
|
||||
if (heartbeat != null){
|
||||
heartbeat.doStop();
|
||||
heartbeat = null;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.*;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SmtpClient {
|
||||
@@ -19,6 +19,7 @@ public class SmtpClient {
|
||||
private static final String UTF8 = "UTF-8";
|
||||
private final String host,password,username;
|
||||
private final int port;
|
||||
private boolean forwardUsingListAddress = true;
|
||||
|
||||
private Session session;
|
||||
|
||||
@@ -29,6 +30,25 @@ public class SmtpClient {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void bccForward(String from, Message message, List<String> emails) throws MessagingException {
|
||||
if (session == null) login();
|
||||
MimeMessage forward = new MimeMessage(session);
|
||||
forward.setFrom(from);
|
||||
forward.setRecipients(Message.RecipientType.BCC,InternetAddress.parse(String.join(", ",emails)));
|
||||
forward.setSubject(message.getSubject());
|
||||
|
||||
MimeMultipart multipart = new MimeMultipart();
|
||||
MimeBodyPart messageBodyPart = new MimeBodyPart();
|
||||
|
||||
messageBodyPart.setDataHandler(message.getDataHandler());
|
||||
multipart.addBodyPart(messageBodyPart);
|
||||
|
||||
forward.setContent(multipart);
|
||||
|
||||
send(forward);
|
||||
}
|
||||
|
||||
|
||||
public SmtpClient login(){
|
||||
if (session == null) {
|
||||
Properties props = new Properties();
|
||||
@@ -38,7 +58,7 @@ public class SmtpClient {
|
||||
props.put(SSL, true);
|
||||
|
||||
session = Session.getInstance(props);
|
||||
LOG.debug("Created new session: {}", session);
|
||||
LOG.debug("Neue Session erzeugt: {}", session);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -55,10 +75,14 @@ public class SmtpClient {
|
||||
message.setText(content,UTF8);
|
||||
message.setSentDate(new Date());
|
||||
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receivers,false));
|
||||
send(message);
|
||||
}
|
||||
|
||||
public void send(Message message) throws MessagingException {
|
||||
LOG.debug("Versende Mail…");
|
||||
Transport.send(message,username,password);
|
||||
LOG.debug("…versendet");
|
||||
|
||||
}
|
||||
|
||||
public String host() {
|
||||
@@ -76,4 +100,5 @@ public class SmtpClient {
|
||||
public String password() {
|
||||
return password;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user