protected boolean sieveMessage(MailAddress recipient, Mail aMail) throws MessagingException { try { ResourceLocator.UserSieveInformation userSieveInformation = resourceLocator.get(recipient); sieveMessageEvaluate(recipient, aMail, userSieveInformation); return true; } catch (ScriptNotFoundException e) { LOGGER.info("Can not locate SIEVE script for user {}", recipient.asPrettyString()); return false; } catch (Exception ex) { LOGGER.error("Cannot evaluate Sieve script for user {}", recipient.asPrettyString(), ex); return false; } }
private String computeUsername(MailAddress recipient) { try { return usersRepository.getUser(recipient); } catch (UsersRepositoryException e) { LOGGER.warn("Unable to retrieve username for {}", recipient.asPrettyString(), e); return recipient.toString(); } } }
private Optional<MessageId> findMessageIdForRFC822MessageId(String messageId, MailAddress recipient) { LOGGER.debug("Searching message {} for recipient {}", messageId, recipient.asPrettyString()); try { MailboxSession session = mailboxManager.createSystemSession(recipient.asString()); int limit = 1; MultimailboxesSearchQuery searchByRFC822MessageId = MultimailboxesSearchQuery .from(new SearchQuery(SearchQuery.mimeMessageID(messageId))) .build(); return mailboxManager.search(searchByRFC822MessageId, session, limit).stream().findFirst(); } catch (MailboxException e) { LOGGER.error("unable to find message with Message-Id: " + messageId, e); } return Optional.empty(); }
@Override public void storeMail(MailAddress recipient, Mail mail) throws MessagingException { String username = computeUsername(recipient); String locatedFolder = locateFolder(username, mail); ComposedMessageId composedMessageId = mailboxAppender.append(mail.getMessage(), username, locatedFolder); metric.increment(); LOGGER.info("Local delivered mail {} successfully from {} to {} in folder {} with composedMessageId {}", mail.getName(), mail.getMaybeSender().asString(), recipient.asPrettyString(), locatedFolder, composedMessageId); }
private void sieveMessageEvaluate(MailAddress recipient, Mail aMail, ResourceLocator.UserSieveInformation userSieveInformation) throws MessagingException, IOException { try { SieveMailAdapter aMailAdapter = new SieveMailAdapter(aMail, mailetContext, actionDispatcher, sievePoster, userSieveInformation.getScriptActivationDate(), userSieveInformation.getScriptInterpretationDate(), recipient); if (LOGGER.isDebugEnabled()) { // This logging operation is potentially costly LOGGER.debug("Evaluating " + aMailAdapter.toString() + " against \"" + recipient.asPrettyString() + "\""); } factory.evaluate(aMailAdapter, factory.parse(userSieveInformation.getScriptContent())); } catch (SieveException | ParseException ex) { handleFailure(recipient, aMail, ex); } catch (TokenMgrError ex) { handleFailure(recipient, aMail, new SieveException(ex)); } }