Merge branch 'main' into lang_de

This commit is contained in:
2024-03-25 18:29:36 +01:00
13 changed files with 520 additions and 415 deletions

View File

@@ -33,44 +33,20 @@ public class ImapClient {
return this;
}
public ListeningThread doStop() {
stopped = true;
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("Verbindung-Problem:",e);
problemListener.onImapException(e);
}
}
}
private void openInbox() throws MessagingException {
LOG.debug("Verbinden und Einloggen…");
Properties props = imapProps();
session = Session.getInstance(props);
Store store = session.getStore(Constants.IMAPS);
store.connect(host,username,password);
LOG.debug("Verbunden. Öffne {}:",folderName);
inbox = (IMAPFolder)store.getFolder(folderName);
inbox.open(IMAPFolder.READ_WRITE);
while (!stopped){
handleMessages();
problemListener.clearProblems();
LOG.debug("Warte.");
inbox.idle(true);
}
private void handle(Message message) throws MessagingException {
LOG.debug("Handling {}",message.getSubject());
for (MessageHandler listener : listeners) listener.onMessageReceived(message);
}
private void handleMessages() throws MessagingException {
@@ -85,35 +61,64 @@ public class ImapClient {
}
}
private void handle(Message message) throws MessagingException {
LOG.debug("Verarbeite {}",message.getSubject());
for (MessageHandler listener : listeners) listener.onMessageReceived(message);
}
private Properties imapProps() {
Properties props = new Properties();
props.put(Constants.PROTOCOL,Constants.IMAPS);
return props;
}
public ListeningThread doStop() {
stopped = true;
return this;
private void openInbox() throws MessagingException {
LOG.debug("Connecting and logging in…");
Properties props = imapProps();
session = Session.getInstance(props);
Store store = session.getStore(Constants.IMAPS);
store.connect(host,username,password);
LOG.debug("Connected, opening {}:",folderName);
inbox = (IMAPFolder)store.getFolder(folderName);
inbox.open(IMAPFolder.READ_WRITE);
while (!stopped){
handleMessages();
problemListener.clearProblems();
LOG.debug("Idling.");
inbox.idle(true);
}
}
@Override
public void run() {
while (!stopped) {
try {
sleep(5000);
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
try {
openInbox();
} catch (MessagingException e){
LOG.warn("Connection problem:",e);
problemListener.onImapException(e);
}
}
}
}
private class Heartbeat extends Thread{
private static final Logger LOG = LoggerFactory.getLogger(Heartbeat.class);
private boolean stopped = false;
public Heartbeat doStop() {
stopped = true;
return this;
}
@Override
public void run() {
while (!stopped){
try {
sleep(300034);
if (inbox != null) continue;
LOG.debug("Sende NOOP");
LOG.debug("sending NOOP");
inbox.doCommand(protocol -> {
protocol.simpleCommand("NOOP",null);
return null;
@@ -123,11 +128,6 @@ public class ImapClient {
}
}
}
public Heartbeat doStop() {
stopped = true;
return this;
}
}
public ImapClient(String host, int port, String username, String password, String folderName,ProblemListener listener) {
@@ -148,7 +148,7 @@ public class ImapClient {
public void dropMailsOlderThan(Integer holdTime) throws MessagingException {
var now = new Date();
if (holdTime == null) return;
LOG.info("Removing mails older than {} days:",holdTime);
LOG.debug("Removing mails older than {} days:",holdTime);
if (!inbox.isOpen()) inbox.open(IMAPFolder.READ_WRITE);
for (Message message : inbox.getMessages()){
Date receivedDate = message.getReceivedDate();
@@ -165,27 +165,15 @@ public class ImapClient {
inbox.expunge();
}
public String folderName(){
return folderName;
}
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 move(Message message, String destinationFolder) throws MessagingException {
if (listeningThread == null || listeningThread.stopped) throw new IllegalStateException("IMAP client not connected!");
var source = message.getFolder();
@@ -200,14 +188,22 @@ public class ImapClient {
return this;
}
public String password(){
return password;
}
public int port(){
return port;
}
public ImapClient start() {
stop();
LOG.info("Erzeuge ListeningThread für {}…",username);
LOG.info("Creating ListeningThread for {}…",username);
(listeningThread = new ListeningThread()).start();
LOG.info("Erzeuge Heartbeat für {}…",username);
LOG.info("Creating Heartbeat for {}…",username);
(heartbeat = new Heartbeat()).start();
return this;
@@ -215,7 +211,7 @@ public class ImapClient {
public ImapClient stop(){
if (listeningThread != null) {
LOG.info("Stoppe alten ListeningThread für {}…",username);
LOG.info("Stopping old ListeningThread for {}…",username);
listeningThread.dropListeners().doStop();
listeningThread = null;
}
@@ -225,4 +221,8 @@ public class ImapClient {
}
return this;
}
public String username(){
return username;
}
}

View File

@@ -6,7 +6,6 @@ import org.slf4j.LoggerFactory;
import javax.mail.*;
import javax.mail.internet.*;
import javax.ws.rs.HEAD;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
@@ -63,7 +62,7 @@ public class SmtpClient {
MimeMultipart multipart = new MimeMultipart();
if (forwardAsAttachment){
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setText("Die weitergeleitete Nachricht findest du im Anhang dieser E-Mail!\n");
bodyPart.setText("Find the forwarded message in the attachment(s)!\n");
multipart.addBodyPart(bodyPart);
// create another body part to contain the message to be forwarded
@@ -82,6 +81,9 @@ public class SmtpClient {
send(forward);
}
public String host() {
return host;
}
public SmtpClient login(){
if (session == null) {
@@ -93,11 +95,25 @@ public class SmtpClient {
props.put(ENVELOPE_FROM,from);
session = Session.getInstance(props);
LOG.debug("Neue Session erzeugt: {}", session);
LOG.debug("Created new session: {}", session);
}
return this;
}
public String password() {
return password;
}
public int port() {
return port;
}
public void send(Message message) throws MessagingException {
LOG.debug("Versende Mail…");
Transport.send(message,username,password);
LOG.debug("…versendet");
}
public void send(String senderAdress, String senderName, String receivers, String subject, String content) throws MessagingException, UnsupportedEncodingException {
login();
MimeMessage message = new MimeMessage(session);
@@ -114,27 +130,7 @@ public class SmtpClient {
send(message);
}
public void send(Message message) throws MessagingException {
LOG.debug("Versende Mail…");
Transport.send(message,username,password);
LOG.debug("…versendet");
}
public String host() {
return host;
}
public int port() {
return port;
}
public String username() {
return username;
}
public String password() {
return password;
}
}