From d1ee31e5e6f5cb50e94b5e7fcacd339d5eaa5f2d Mon Sep 17 00:00:00 2001 From: boavenn Date: Wed, 20 Nov 2024 12:29:09 +0100 Subject: [PATCH] Fix not working mail receiver (#257) --- readme.md | 1 - .../urlopia/config/mail/receive/MailDecider.java | 3 +-- .../urlopia/config/mail/receive/MailParser.java | 6 +++++- .../urlopia/config/mail/receive/MailReceiver.java | 14 +++++++++----- src/main/resources/application.properties | 1 - 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/readme.md b/readme.md index fd9edd1f..9bb4aa41 100644 --- a/readme.md +++ b/readme.md @@ -160,7 +160,6 @@ mails.bot= (Mail address of mailing bot) mails.storage= (Mail address of emails storage) mail.title.prefix = (Prefix for mail title - can be empty) mail.sender.enabled= (Boolean flag to set sending mail) -mail.receiver.enabled= (Boolean flag to set observing incoming mail) # slack slack.bot-token= slack.signing-secret= diff --git a/src/main/java/info/fingo/urlopia/config/mail/receive/MailDecider.java b/src/main/java/info/fingo/urlopia/config/mail/receive/MailDecider.java index 7263e980..5e562a7e 100644 --- a/src/main/java/info/fingo/urlopia/config/mail/receive/MailDecider.java +++ b/src/main/java/info/fingo/urlopia/config/mail/receive/MailDecider.java @@ -86,8 +86,7 @@ private void parse(Mail mail){ private void updateAcceptance(Long deciderId) { var acceptanceId = mailParser.getId(); var decision = mailParser.getReply().toLowerCase(); - var loggerInfo = "Decision for acceptance with id: %d is: %s" - .formatted(acceptanceId, decision); + var loggerInfo = "Decision for acceptance with id: %d is: %s".formatted(acceptanceId, decision); log.info(loggerInfo); if (mailParser.isAcceptedByMail(decision)) { acceptanceService.accept(acceptanceId, deciderId); diff --git a/src/main/java/info/fingo/urlopia/config/mail/receive/MailParser.java b/src/main/java/info/fingo/urlopia/config/mail/receive/MailParser.java index dae57243..2b261726 100644 --- a/src/main/java/info/fingo/urlopia/config/mail/receive/MailParser.java +++ b/src/main/java/info/fingo/urlopia/config/mail/receive/MailParser.java @@ -81,7 +81,11 @@ public void parseSubject(Mail mail) { public void parseReply(Mail mail) { var emailLines = splitByLines(mail.getContent()); - reply = emailLines[0]; + reply = Arrays.stream(emailLines) + .map(String::trim) + .filter(line -> !line.isBlank() && !line.startsWith(">")) // Remove empty lines and quotes + .reduce((first, second) -> second) // Get last line + .orElse(""); } private void convertDate() { diff --git a/src/main/java/info/fingo/urlopia/config/mail/receive/MailReceiver.java b/src/main/java/info/fingo/urlopia/config/mail/receive/MailReceiver.java index 57631042..801ffbab 100644 --- a/src/main/java/info/fingo/urlopia/config/mail/receive/MailReceiver.java +++ b/src/main/java/info/fingo/urlopia/config/mail/receive/MailReceiver.java @@ -9,7 +9,10 @@ import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; -import javax.mail.*; +import javax.mail.Folder; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.Store; import javax.mail.event.MessageCountEvent; import javax.mail.event.MessageCountListener; import java.util.Properties; @@ -40,9 +43,6 @@ public class MailReceiver extends Thread { @Value("${mail.receiver.idle.time}") private int keepAliveFreq; //time unit: milliseconds - @Value("${mail.receiver.enabled}") - private boolean isEnabled; - private Store store; private IMAPFolder inbox; @@ -133,7 +133,9 @@ private void keepInboxIdle() { @Override public void run() { - if (isEnabled){ + if (!host.isBlank()){ + log.info("Initializing MailReceiver"); + // Configuring the inbox performConfiguration(); @@ -159,6 +161,8 @@ public void run() { } catch (MessagingException e) { log.error("MessagingException during closing the store", e); } + } else { + log.warn("MailReceiver has been disabled"); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 214a59cb..9c78bde5 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -45,7 +45,6 @@ mail.receiver.idle.time=300000 mails.bot= mails.storage= mail.sender.enabled=false -mail.receiver.enabled=false # slack slack.bot-token= slack.signing-secret=