Search in sources :

Example 6 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.

the class DateTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    String comparator = null;
    Date date = null;
    @SuppressWarnings("unchecked") ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a tag of ":before" or ":after"
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            if (tag.equals(BEFORE) || tag.equals(AFTER))
                comparator = tag;
            else
                throw new SyntaxException("Found unexpected: \"" + tag + "\"");
        }
    }
    if (null == comparator)
        throw new SyntaxException("Expecting \"" + BEFORE + "\" or \"" + AFTER + "\"");
    // Second argument MUST be a date
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            StringListArgument strList = (StringListArgument) argument;
            String datestr = (String) strList.getList().get(0);
            try {
                date = mShortDateFormat.parse(datestr);
            } catch (ParseException e) {
            }
        }
    }
    if (null == date)
        throw new SyntaxException("Expecting a valid date (yyyyMMdd)");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    return test(mail, comparator, date);
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 7 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.

the class MeTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
    assert (headers != null);
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    Mailbox mbox = ((ZimbraMailAdapter) mail).getMailbox();
    List<InternetAddress> addrs = new ArrayList<InternetAddress>();
    for (String header : headers) {
        for (String value : mail.getHeader(header)) {
            List<InternetAddress> inetAddrs = InternetAddress.parseHeader(value);
            if (inetAddrs != null) {
                addrs.addAll(inetAddrs);
            }
        }
    }
    try {
        Account account = mbox.getAccount();
        Set<String> me = AccountUtil.getEmailAddresses(account);
        me.addAll(AccountUtil.getImapPop3EmailAddresses(account));
        for (InternetAddress addr : addrs) {
            String email = addr.getAddress();
            if (email != null && me.contains(email.toLowerCase())) {
                return true;
            }
        }
    } catch (ServiceException e) {
        ZimbraLog.filter.error("Failed to lookup my addresses", e);
    }
    return false;
}
Also used : Account(com.zimbra.cs.account.Account) InternetAddress(com.zimbra.common.mime.InternetAddress) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 8 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.

the class MimeHeaderTest method match.

@SuppressWarnings("unchecked")
@Override
protected boolean match(MailAdapter mail, String comparator, String matchType, List headerNames, List keys, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
    // Iterate over the header names looking for a match
    boolean isMatched = false;
    Iterator<String> headerNamesIter = headerNames.iterator();
    while (!isMatched && headerNamesIter.hasNext()) {
        Set<String> values = zma.getMatchingHeaderFromAllParts(headerNamesIter.next());
        isMatched = match(comparator, matchType, new ArrayList<String>(values), keys, context);
    }
    return isMatched;
}
Also used : ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 9 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.

the class TwitterTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter adapter = (ZimbraMailAdapter) mail;
    ParsedAddress sender = adapter.getParsedMessage().getParsedSender();
    if (!Strings.isNullOrEmpty(sender.emailPart)) {
        String email = sender.emailPart.toLowerCase();
        if (email.equals("notify@twitter.com")) {
            return true;
        }
    }
    return false;
}
Also used : DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) ParsedAddress(com.zimbra.cs.mime.ParsedAddress)

Example 10 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter 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)19 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)19 ServiceException (com.zimbra.common.service.ServiceException)6 MessagingException (javax.mail.MessagingException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)4 ArrayList (java.util.ArrayList)4 Argument (org.apache.jsieve.Argument)4 StringListArgument (org.apache.jsieve.StringListArgument)4 TagArgument (org.apache.jsieve.TagArgument)4 SyntaxException (org.apache.jsieve.exception.SyntaxException)4 InternetAddress (com.zimbra.common.mime.InternetAddress)3 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)3 SieveException (org.apache.jsieve.exception.SieveException)3 Message (com.zimbra.cs.mailbox.Message)2 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2