Skip to content

Commit

Permalink
JAMES-1854 Only discard when Sieve script was executed successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
chibenwa committed Nov 23, 2016
1 parent 296bb64 commit 9251503
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Expand Up @@ -84,29 +84,34 @@ public void init() throws MessagingException {


@Override @Override
public void service(Mail mail) throws MessagingException { public void service(Mail mail) throws MessagingException {
for(MailAddress recipient: mail.getRecipients()) { List<MailAddress> recipientsWithSuccessfulSieveExecution = executeRetrieveSuccess(mail);
executeSieveScript(mail, recipient); mail.setRecipients(keepNonDiscardedRecipients(mail, recipientsWithSuccessfulSieveExecution));
}
mail.setRecipients(keepNonDiscardedRecipients(mail));
} }


private void executeSieveScript(Mail mail, MailAddress recipient) { private List<MailAddress> executeRetrieveSuccess(Mail mail) throws MessagingException {
try { ImmutableList.Builder<MailAddress> recipientsWithSuccessfulSieveExecution = ImmutableList.builder();
sieveExecutor.execute(recipient, mail); for(MailAddress recipient: mail.getRecipients()) {
} catch (Exception e) { if (sieveExecutor.execute(recipient, mail)) {
getMailetContext().getLogger().warn("Failed to execute Sieve script for user " + recipient.asPrettyString(), e); recipientsWithSuccessfulSieveExecution.add(recipient);
}
} }
return recipientsWithSuccessfulSieveExecution.build();
} }



private ImmutableList<MailAddress> keepNonDiscardedRecipients(Mail mail, final List<MailAddress> recipientsWithSuccessfulSieveExecution) {
private ImmutableList<MailAddress> keepNonDiscardedRecipients(Mail mail) {
final List<MailAddress> discardedRecipients = retrieveDiscardedRecipients(mail); final List<MailAddress> discardedRecipients = retrieveDiscardedRecipients(mail);
return FluentIterable.from(mail.getRecipients()).filter(new Predicate<MailAddress>() { return FluentIterable.from(mail.getRecipients())
@Override .filter(discardPredicate(discardedRecipients, recipientsWithSuccessfulSieveExecution))
public boolean apply(MailAddress input) { .toList();
return !discardedRecipients.contains(input); }
}
}).toList(); private Predicate<MailAddress> discardPredicate(final List<MailAddress> discardedAddressList, final List<MailAddress> discardeableAddressList) {
return new Predicate<MailAddress>() {
@Override
public boolean apply(MailAddress input) {
return !discardeableAddressList.contains(input) || !discardedAddressList.contains(input);
}
};
} }


private List<MailAddress> retrieveDiscardedRecipients(Mail mail) { private List<MailAddress> retrieveDiscardedRecipients(Mail mail) {
Expand Down
Expand Up @@ -110,21 +110,24 @@ private SieveFactory createFactory(Log log) throws MessagingException {
} }
} }


public void execute(MailAddress recipient, Mail mail) throws MessagingException { public boolean execute(MailAddress recipient, Mail mail) throws MessagingException {
Preconditions.checkNotNull(recipient, "Recipient for mail to be spooled cannot be null."); Preconditions.checkNotNull(recipient, "Recipient for mail to be spooled cannot be null.");
Preconditions.checkNotNull(mail.getMessage(), "Mail message to be spooled cannot be null."); Preconditions.checkNotNull(mail.getMessage(), "Mail message to be spooled cannot be null.");


sieveMessage(recipient, mail, log); return sieveMessage(recipient, mail, log);
} }


protected void sieveMessage(MailAddress recipient, Mail aMail, Log log) throws MessagingException { protected boolean sieveMessage(MailAddress recipient, Mail aMail, Log log) throws MessagingException {
try { try {
ResourceLocator.UserSieveInformation userSieveInformation = resourceLocator.get(recipient); ResourceLocator.UserSieveInformation userSieveInformation = resourceLocator.get(recipient);
sieveMessageEvaluate(recipient, aMail, userSieveInformation, log); sieveMessageEvaluate(recipient, aMail, userSieveInformation, log);
return true;
} catch (ScriptNotFoundException e) { } catch (ScriptNotFoundException e) {
log.info("Can not locate SIEVE script for user " + recipient.asPrettyString()); log.info("Can not locate SIEVE script for user " + recipient.asPrettyString());
return false;
} catch (Exception ex) { } catch (Exception ex) {
log.error("Cannot evaluate Sieve script for user " + recipient.asPrettyString(), ex); log.error("Cannot evaluate Sieve script for user " + recipient.asPrettyString(), ex);
return false;
} }
} }


Expand Down

0 comments on commit 9251503

Please sign in to comment.