Search in sources :

Example 1 with MailAdapter

use of org.apache.jsieve.mail.MailAdapter in project zm-mailbox by Zimbra.

the class AddressTest method match.

/**
     * Compares the address with operator
     */
private boolean match(MailAdapter mail, String addressPart, String comparator, String matchType, String operator, List<String> headerNames, List<String> keys, SieveContext context) throws SieveException {
    // Iterate over the address fields looking for a match
    boolean isMatched = false;
    Iterator headerNamesIter = headerNames.iterator();
    List<String> headerValues = new ArrayList<String>();
    while (headerNamesIter.hasNext()) {
        final MailAdapter.Address[] addresses = mail.parseAddresses((String) headerNamesIter.next());
        final int length = addresses.length;
        int i = 0;
        while (i < length) {
            MailAdapter.Address address = addresses[i++];
            final String localPart = address.getLocalPart();
            final String domain = address.getDomain();
            headerValues.add(getMatchAddress(addressPart, localPart, domain));
        }
    }
    if (COUNT_TAG.equals(matchType)) {
        for (final String key : keys) {
            isMatched = ZimbraComparatorUtils.counts(comparator, operator, headerValues, ZimbraComparatorUtils.getMatchKey(addressPart, key), context);
            if (isMatched) {
                break;
            }
        }
    } else {
        Iterator headerValuesIter = headerValues.iterator();
        while (!isMatched && headerValuesIter.hasNext()) {
            isMatched = match(comparator, matchType, operator, (String) headerValuesIter.next(), keys, context);
        }
    }
    return isMatched;
}
Also used : Address(org.apache.jsieve.tests.Address) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) MailAdapter(org.apache.jsieve.mail.MailAdapter)

Example 2 with MailAdapter

use of org.apache.jsieve.mail.MailAdapter 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(COUNT_TAG)) {
                    // 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 (COUNT_TAG.equals(matchType)) {
        for (final String key : keys) {
            isMatched = ZimbraComparatorUtils.counts(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;
            }
            isMatched = match(comparator, matchType, operator, (String) headerValuesIter.next(), normalizedKeys, context);
        }
    }
    return isMatched;
}
Also used : ASCII_NUMERIC_COMPARATOR(com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) VALUE_TAG(com.zimbra.cs.filter.jsieve.MatchTypeTags.VALUE_TAG) 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) 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) COUNT_TAG(com.zimbra.cs.filter.jsieve.MatchTypeTags.COUNT_TAG) 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

DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)2 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)2 Iterator (java.util.Iterator)2 MailAdapter (org.apache.jsieve.mail.MailAdapter)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 ServiceException (com.zimbra.common.service.ServiceException)1 StringUtil (com.zimbra.common.util.StringUtil)1 FilterUtil (com.zimbra.cs.filter.FilterUtil)1 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)1 ASCII_NUMERIC_COMPARATOR (com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR)1 COUNT_TAG (com.zimbra.cs.filter.jsieve.MatchTypeTags.COUNT_TAG)1 VALUE_TAG (com.zimbra.cs.filter.jsieve.MatchTypeTags.VALUE_TAG)1 ArrayList (java.util.ArrayList)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