-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle Multipart/mixed Bounces and Auto-submitted Messages (#1490)
* Multipart/mixed messages may also contain a delivery failure report * Detect auto-generated responses and delete them, do not attempt to parse them as Whois Update messages * Correct Auto-Submitted value * Fix formatting, updated TODO * Added TODO refactor BouncedMessageParser
- Loading branch information
Showing
6 changed files
with
273 additions
and
25 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
whois-api/src/main/java/net/ripe/db/whois/api/mail/dequeue/AutoSubmittedMessageParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package net.ripe.db.whois.api.mail.dequeue; | ||
|
||
import jakarta.mail.MessagingException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import net.ripe.db.whois.api.mail.EmailMessageInfo; | ||
import net.ripe.db.whois.api.mail.exception.MailParsingException; | ||
import org.elasticsearch.common.Strings; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.Collections; | ||
|
||
// Detect automated responses and mark them for deletion. Do not try to parse them as Whois updates (for now). | ||
// TODO: attempt to find the outgoing message-id and failed recipient if possible by parsing the plaintext body | ||
@Component | ||
public class AutoSubmittedMessageParser { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(AutoSubmittedMessageParser.class); | ||
|
||
private final boolean enabled; | ||
|
||
@Autowired | ||
public AutoSubmittedMessageParser(@Value("${mail.smtp.from:}") final String smtpFrom) { | ||
this.enabled = !Strings.isNullOrEmpty(smtpFrom); | ||
} | ||
|
||
@Nullable | ||
public EmailMessageInfo parse(final MimeMessage message) throws MessagingException, MailParsingException { | ||
if (!enabled) { | ||
return null; | ||
} | ||
|
||
final String autoSubmitted = getHeader(message, "Auto-Submitted"); | ||
if (autoSubmitted != null) { | ||
if (autoSubmitted.contains("auto-generated") || autoSubmitted.contains("auto-replied")) { | ||
return new EmailMessageInfo(Collections.emptyList(), null); | ||
} else { | ||
LOGGER.info("Unexpected Auto-Submitted value {}", autoSubmitted); | ||
} | ||
} | ||
|
||
final String from = getHeader(message, "From"); | ||
if (from != null) { | ||
if (from.toUpperCase().contains("MAILER-DAEMON")) { | ||
return new EmailMessageInfo(Collections.emptyList(), null); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
@Nullable | ||
private String getHeader(final MimeMessage message, final String name) throws MessagingException { | ||
final String[] headers = message.getHeader(name); | ||
if (headers != null && headers.length > 0) { | ||
return headers[0]; | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.