Search in sources :

Example 31 with ZimbraMailAdapter

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

the class AddressTest method executeBasic.

/**
 * <p>
 * From RFC 5228, Section 5.1...
 * </p>
 * <code>
 * Syntax: address [ADDRESS-PART] [COMPARATOR] [MATCH-TYPE]
 *         &lt;header-list: string-list&gt; &lt;key-list: string-list&gt;
 * </code>
 *
 * @see org.apache.jsieve.tests.AbstractComparatorTest#executeBasic(MailAdapter,
 *      Arguments, SieveContext)
 */
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraComparatorUtils.TestParameters params = ZimbraComparatorUtils.parseTestArguments(mail, arguments, context);
    params.setKeys(HeaderTest.replaceVariables(params.getKeys(), mail));
    params.setHeaderNames(HeaderTest.replaceVariables(params.getHeaderNames(), mail));
    for (String headerName : params.getHeaderNames()) {
        FilterUtil.headerNameHasSpace(headerName);
    }
    if (MatchTypeTags.MATCHES_TAG.equals(params.getMatchType())) {
        ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
        try {
            HeaderTest.evaluateVarExp(zma, params.getHeaderNames(), HeaderTest.SourceType.HEADER, params.getKeys());
        } catch (MessagingException e) {
            throw new SieveException("Exception occured while evaluating variable expression.", e);
        }
    }
    if (params.getMatchType() == null) {
        params.setMatchType(IS_TAG);
    }
    params.setComparator(ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()));
    if (ASCII_NUMERIC_COMPARATOR.equalsIgnoreCase(params.getComparator())) {
        Require.checkCapability((ZimbraMailAdapter) mail, ASCII_NUMERIC_COMPARATOR);
    }
    if (HeaderConstants.COUNT.equals(params.getMatchType()) || HeaderConstants.VALUE.equals(params.getMatchType()) || IS_TAG.equalsIgnoreCase(params.getMatchType())) {
        return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getOperator(), params.getHeaderNames(), params.getKeys(), context);
    } else {
        return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getHeaderNames(), params.getKeys(), context);
    }
}
Also used : SieveException(org.apache.jsieve.exception.SieveException) MessagingException(javax.mail.MessagingException) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraComparatorUtils(com.zimbra.cs.filter.ZimbraComparatorUtils) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 32 with ZimbraMailAdapter

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

the class BodyTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    String comparison = null;
    boolean caseSensitive = false;
    String key = null;
    @SuppressWarnings("unchecked") ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // TODO: handles ":matches" with * and ?
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            if (tag.equals(CONTAINS))
                comparison = tag;
            else
                throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
        }
    }
    if (null == comparison)
        throw new SyntaxException("Expecting \"" + CONTAINS + "\"");
    // Second argument could be :comparator tag or else the value (string)
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            if (tag.equals(COMPARATOR)) {
                if (argumentsIter.hasNext()) {
                    argument = argumentsIter.next();
                    if (argument instanceof StringListArgument) {
                        StringListArgument strList = (StringListArgument) argument;
                        try {
                            String comparator = strList.getList().get(0);
                            if (ASCII_NUMERIC_COMPARATOR.equalsIgnoreCase(comparator) && mail instanceof ZimbraMailAdapter) {
                                Require.checkCapability((ZimbraMailAdapter) mail, ASCII_NUMERIC_COMPARATOR);
                            }
                            caseSensitive = Sieve.Comparator.ioctet == Sieve.Comparator.fromString(comparator);
                        } catch (ServiceException e) {
                            throw new SyntaxException(e.getMessage());
                        }
                        // Move to the last argument
                        if (argumentsIter.hasNext())
                            argument = argumentsIter.next();
                    } else {
                        throw new SyntaxException("Found unexpected argument after :comparator");
                    }
                } else {
                    throw new SyntaxException("Unexpected end of arguments");
                }
            } else {
                throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
            }
        }
        if (argument instanceof StringListArgument) {
            StringListArgument strList = (StringListArgument) argument;
            key = strList.getList().get(0);
        }
    }
    if (null == key)
        throw new SyntaxException("Expecting a string");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    return mail instanceof ZimbraMailAdapter && test(mail, caseSensitive, key);
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 33 with ZimbraMailAdapter

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

the class CommunityAbstractTest method checkHeaderValue.

protected boolean checkHeaderValue(MailAdapter mail, Set<String> values) {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter adapter = (ZimbraMailAdapter) mail;
    List<String> header = adapter.getHeader(notificationTypeHeaderName);
    if (!header.isEmpty()) {
        for (String id : header) {
            if (values.contains(id)) {
                return true;
            }
        }
    }
    return false;
}
Also used : DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 34 with ZimbraMailAdapter

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

the class EnvelopeTest method executeBasic.

/**
 * <p>
 * From RFC 5228, Section 5.4...
 * </p>
 * <code>
 * Syntax: envelope [COMPARATOR] [ADDRESS-PART] [MATCH-TYPE]
 *         &lt;envelope-part: string-list&gt; &lt;key-list: string-list&gt;
 * </code>
 */
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    Require.checkCapability(mailAdapter, CAPABILITY_ENVELOPE);
    ZimbraComparatorUtils.TestParameters params = ZimbraComparatorUtils.parseTestArguments(mail, arguments, context);
    params.setKeys(HeaderTest.replaceVariables(params.getKeys(), mail));
    params.setHeaderNames(HeaderTest.replaceVariables(params.getHeaderNames(), mail));
    for (String headerName : params.getHeaderNames()) {
        FilterUtil.headerNameHasSpace(headerName);
    }
    if (MatchTypeTags.MATCHES_TAG.equals(params.getMatchType())) {
        try {
            HeaderTest.evaluateVarExp(mailAdapter, params.getHeaderNames(), HeaderTest.SourceType.ENVELOPE, params.getKeys());
        } catch (MessagingException e) {
            throw new SieveException("Exception occured while evaluating variable expression.", e);
        }
    }
    if (params.getMatchType() == null) {
        params.setMatchType(IS_TAG);
    }
    params.setComparator(ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()));
    if (ASCII_NUMERIC_COMPARATOR.equalsIgnoreCase(params.getComparator())) {
        Require.checkCapability(mailAdapter, ASCII_NUMERIC_COMPARATOR);
    }
    if (HeaderConstants.COUNT.equals(params.getMatchType()) || HeaderConstants.VALUE.equals(params.getMatchType()) || IS_TAG.equals(params.getMatchType())) {
        return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getOperator(), params.getHeaderNames(), params.getKeys(), context);
    } else {
        return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getHeaderNames(), params.getKeys(), context);
    }
}
Also used : SieveException(org.apache.jsieve.exception.SieveException) MessagingException(javax.mail.MessagingException) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraComparatorUtils(com.zimbra.cs.filter.ZimbraComparatorUtils) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 35 with ZimbraMailAdapter

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

the class FacebookTest 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.endsWith("@facebookmail.com") && email.startsWith("notification+")) {
            return true;
        }
    }
    return false;
}
Also used : DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) ParsedAddress(com.zimbra.cs.mime.ParsedAddress)

Aggregations

ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)39 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)20 StringListArgument (org.apache.jsieve.StringListArgument)15 Argument (org.apache.jsieve.Argument)12 MessagingException (javax.mail.MessagingException)9 TagArgument (org.apache.jsieve.TagArgument)9 SyntaxException (org.apache.jsieve.exception.SyntaxException)9 ServiceException (com.zimbra.common.service.ServiceException)8 ArrayList (java.util.ArrayList)8 SieveException (org.apache.jsieve.exception.SieveException)7 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 InternetAddress (com.zimbra.common.mime.InternetAddress)3 Account (com.zimbra.cs.account.Account)3 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Message (com.zimbra.cs.mailbox.Message)2 IOException (java.io.IOException)2