Search in sources :

Example 1 with Envelope

use of org.apache.jsieve.tests.optional.Envelope in project zm-mailbox by Zimbra.

the class EnvelopeTest method match.

/**
 * Compares the address with operator
 *
 * @param operator "gt" / "ge" / "lt" / "le" / "eq" / "ne"
 */
private boolean match(MailAdapter mail, String addressPart, String comparator, String matchType, String operator, List<String> headerNames, List<String> keys, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    // Iterate over the address fields looking for a match
    boolean isMatched = false;
    List<String> headerValues = Lists.newArrayListWithExpectedSize(2);
    for (final String headerName : headerNames) {
        if ("to".equalsIgnoreCase(headerName)) {
            // RFC 5231 4.2. ... The envelope "to" will always have only one
            // entry, which is the address of the user for whom the Sieve script
            // is running.
            String recipient = null;
            try {
                recipient = ((ZimbraMailAdapter) mail).getMailbox().getAccount().getMail();
            } catch (ServiceException e) {
                recipient = "";
            }
            headerValues.add(getMatchAddressPart(addressPart, recipient));
        } else if ("from".equalsIgnoreCase(headerName)) {
            List<String> values = getMatchingValues(mail, headerName);
            if (values != null) {
                if (matchType.equalsIgnoreCase(HeaderConstants.COUNT)) {
                    // RFC 5231 Section 4.2 Match Type COUNT says:
                    // | The envelope "from" will be 0 if the MAIL FROM is empty, or 1 if MAIL
                    // | FROM is not empty.
                    // This method could be called for other match type, such as :value or :is,
                    // remove the empty element only if the match type is :count.
                    values.removeIf(s -> Strings.isNullOrEmpty(s));
                }
                for (String value : values) {
                    headerValues.add(getMatchAddressPart(addressPart, value));
                }
            }
        } else {
            throw new SyntaxException("Unexpected header name as a value for <envelope-part>: '" + headerName + "'");
        }
    }
    if (HeaderConstants.COUNT.equals(matchType)) {
        for (final String key : keys) {
            isMatched = ZimbraComparatorUtils.counts(mail, comparator, operator, headerValues, ZimbraComparatorUtils.getMatchKey(addressPart, key), context);
            if (isMatched) {
                break;
            }
        }
    } else {
        Iterator headerValuesIter = headerValues.iterator();
        while (!isMatched && headerValuesIter.hasNext()) {
            List<String> normalizedKeys = Lists.newArrayListWithExpectedSize(keys.size());
            if (DOMAIN_TAG.equalsIgnoreCase(addressPart)) {
                for (String key : keys) {
                    normalizedKeys.add(key.toLowerCase());
                }
            } else {
                normalizedKeys = keys;
            }
            String headerValue = (String) headerValuesIter.next();
            // Iterate over the keys looking for a match
            for (final String key : keys) {
                isMatched = ZimbraComparatorUtils.match(mail, comparator, matchType, operator, headerValue, key, context);
                if (isMatched) {
                    break;
                }
            }
        }
    }
    return isMatched;
}
Also used : CAPABILITY_ENVELOPE(com.zimbra.cs.filter.JsieveConfigMapHandler.CAPABILITY_ENVELOPE) ASCII_NUMERIC_COMPARATOR(com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) Strings(com.google.common.base.Strings) ZimbraComparatorUtils(com.zimbra.cs.filter.ZimbraComparatorUtils) Lists(com.google.common.collect.Lists) FilterUtil(com.zimbra.cs.filter.FilterUtil) SieveException(org.apache.jsieve.exception.SieveException) MatchTypeTags(org.apache.jsieve.comparators.MatchTypeTags) HeaderConstants(com.zimbra.common.soap.HeaderConstants) DOMAIN_TAG(org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) Iterator(java.util.Iterator) ASCII_CASEMAP_COMPARATOR(org.apache.jsieve.comparators.ComparatorNames.ASCII_CASEMAP_COMPARATOR) Arguments(org.apache.jsieve.Arguments) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) MailAdapter(org.apache.jsieve.mail.MailAdapter) ALL_TAG(org.apache.jsieve.tests.AddressPartTags.ALL_TAG) Envelope(org.apache.jsieve.tests.optional.Envelope) List(java.util.List) SyntaxException(org.apache.jsieve.exception.SyntaxException) LOCALPART_TAG(org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG) SieveContext(org.apache.jsieve.SieveContext) StringUtil(com.zimbra.common.util.StringUtil) IS_TAG(org.apache.jsieve.comparators.MatchTypeTags.IS_TAG) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) Iterator(java.util.Iterator) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) List(java.util.List) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 ServiceException (com.zimbra.common.service.ServiceException)1 HeaderConstants (com.zimbra.common.soap.HeaderConstants)1 StringUtil (com.zimbra.common.util.StringUtil)1 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)1 FilterUtil (com.zimbra.cs.filter.FilterUtil)1 CAPABILITY_ENVELOPE (com.zimbra.cs.filter.JsieveConfigMapHandler.CAPABILITY_ENVELOPE)1 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)1 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)1 ASCII_NUMERIC_COMPARATOR (com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR)1 Iterator (java.util.Iterator)1 List (java.util.List)1 MessagingException (javax.mail.MessagingException)1 Arguments (org.apache.jsieve.Arguments)1 SieveContext (org.apache.jsieve.SieveContext)1 ASCII_CASEMAP_COMPARATOR (org.apache.jsieve.comparators.ComparatorNames.ASCII_CASEMAP_COMPARATOR)1 MatchTypeTags (org.apache.jsieve.comparators.MatchTypeTags)1 IS_TAG (org.apache.jsieve.comparators.MatchTypeTags.IS_TAG)1 SieveException (org.apache.jsieve.exception.SieveException)1