Search in sources :

Example 11 with DummyMailAdapter

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

the class CurrentDayOfWeekTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a ":is" tag
    String comparator = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument)
            comparator = ((TagArgument) argument).getTag();
    }
    if (!":is".equals(comparator))
        throw new SyntaxException("Expecting \":is\"");
    // Second argument MUST be a list of day of week indices; 0=Sunday, 6=Saturday
    Set<Integer> daysToCheckAgainst = new HashSet<Integer>();
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            for (String val : valList) {
                int day;
                try {
                    day = Integer.valueOf(val);
                } catch (NumberFormatException e) {
                    throw new SyntaxException(e);
                }
                if (day < 0 || day > 6)
                    throw new SyntaxException("Expected values between 0 - 6");
                // In Java 1=Sunday, 7=Saturday
                daysToCheckAgainst.add(day + 1);
            }
        }
    }
    if (daysToCheckAgainst.isEmpty())
        throw new SyntaxException("Expecting at least one value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    return daysToCheckAgainst.contains(rightNow.get(Calendar.DAY_OF_WEEK));
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) HashSet(java.util.HashSet)

Example 12 with DummyMailAdapter

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

the class CurrentTimeTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a tag of ":before" or ":after"
    String comparator = null;
    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 (comparator == null)
        throw new SyntaxException("Expecting \"" + BEFORE + "\" or \"" + AFTER + "\"");
    // Second argument MUST be a time in "HHmm" format
    DateFormat timeFormat = new SimpleDateFormat("HHmm");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    timeFormat.setTimeZone(accountTimeZone);
    Date timeArg = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            if (valList.size() != 1)
                throw new SyntaxException("Expecting exactly one time value");
            String timeStr = valList.get(0);
            try {
                timeArg = timeFormat.parse(timeStr);
            } catch (ParseException e) {
                throw new SyntaxException(e);
            }
        }
    }
    if (timeArg == null)
        throw new SyntaxException("Expecting a time value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    Calendar timeToCompareWith = Calendar.getInstance(accountTimeZone);
    // set the time part
    timeToCompareWith.setTime(timeArg);
    // now set the right date
    timeToCompareWith.set(rightNow.get(Calendar.YEAR), rightNow.get(Calendar.MONTH), rightNow.get(Calendar.DAY_OF_MONTH));
    return BEFORE.equals(comparator) ? rightNow.getTime().before(timeToCompareWith.getTime()) : rightNow.getTime().after(timeToCompareWith.getTime());
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 13 with DummyMailAdapter

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

the class StringTest method executeBasic.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.jsieve.tests.AbstractTest#executeBasic(org.apache.jsieve.mail.
     * MailAdapter, org.apache.jsieve.Arguments, org.apache.jsieve.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;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    String matchType = null;
    String comparator = null;
    String operator = null;
    List<String> sourceValues = null;
    List<String> keyValues = null;
    boolean nextArgumentIsRelationalSign = false;
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    boolean stop = false;
    //         <source: string-list> <key-list: string-list>
    while (!stop && argumentsIter.hasNext()) {
        Argument argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            final String tag = ((TagArgument) argument).getTag();
            // [COMPARATOR]?
            if (comparator == null && COMPARATOR_TAG.equalsIgnoreCase(tag)) {
                // The next argument must be a stringlist
                if (argumentsIter.hasNext()) {
                    argument = argumentsIter.next();
                    if (argument instanceof StringListArgument) {
                        List<String> stringList = ((StringListArgument) argument).getList();
                        if (stringList.size() != 1) {
                            throw context.getCoordinate().syntaxException("Expecting exactly one String");
                        }
                        comparator = stringList.get(0);
                    } else {
                        throw context.getCoordinate().syntaxException("Expecting a StringList");
                    }
                }
            } else // [MATCH-TYPE]?
            if (matchType == null && (IS_TAG.equalsIgnoreCase(tag) || CONTAINS_TAG.equalsIgnoreCase(tag) || MATCHES_TAG.equalsIgnoreCase(tag) || COUNT_TAG.equalsIgnoreCase(tag) || VALUE_TAG.equalsIgnoreCase(tag))) {
                matchType = tag;
                nextArgumentIsRelationalSign = true;
            } else {
                throw context.getCoordinate().syntaxException("Found unexpected TagArgument: \"" + tag + "\"");
            }
        } else {
            if (nextArgumentIsRelationalSign && argument instanceof StringListArgument) {
                String symbol = ((StringListArgument) argument).getList().get(0);
                if (matchType != null && (GT_OP.equalsIgnoreCase(symbol) || GE_OP.equalsIgnoreCase(symbol) || LT_OP.equalsIgnoreCase(symbol) || LE_OP.equalsIgnoreCase(symbol) || EQ_OP.equalsIgnoreCase(symbol) || NE_OP.equalsIgnoreCase(symbol))) {
                    operator = symbol;
                } else {
                    argumentsIter.previous();
                    stop = true;
                }
                nextArgumentIsRelationalSign = false;
            } else {
                // Stop when a non-tag argument is encountered
                argumentsIter.previous();
                stop = true;
            }
        }
    }
    // The next argument MUST be a string-list of header names
    if (argumentsIter.hasNext()) {
        final Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> sourceStringList = ((StringListArgument) argument).getList();
            if (null == sourceStringList || 0 == sourceStringList.size()) {
                throw context.getCoordinate().syntaxException("Expecting a StringListof header names");
            }
            sourceValues = new ArrayList<String>();
            for (String source : sourceStringList) {
                sourceValues.add(FilterUtil.replaceVariables(mailAdapter, source));
            }
        }
    }
    // The next argument MUST be a string-list of keys
    if (argumentsIter.hasNext()) {
        final Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> keyStringList = ((StringListArgument) argument).getList();
            if (null == keyStringList || 0 == keyStringList.size()) {
                throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
            }
            keyValues = new ArrayList<String>();
            for (String key : keyStringList) {
                keyValues.add(FilterUtil.replaceVariables(mailAdapter, key));
            }
        }
    }
    if (argumentsIter.hasNext()) {
        throw context.getCoordinate().syntaxException("Found unexpected arguments");
    }
    if (null == matchType) {
        matchType = IS_TAG;
    }
    if (null == comparator) {
        if (matchType.equalsIgnoreCase(VALUE_TAG) || matchType.equalsIgnoreCase(COUNT_TAG)) {
            comparator = ASCII_NUMERIC_COMPARATOR;
        } else {
            comparator = ASCII_CASEMAP_COMPARATOR;
        }
    }
    boolean result = match(mail, comparator, matchType, operator, sourceValues, keyValues, context);
    if (result) {
        if (matchType.equals(MatchTypeTags.MATCHES_TAG)) {
            try {
                HeaderTest.evaluateVarExp(mailAdapter, sourceValues, HeaderTest.SourceType.LITERAL, keyValues);
            } catch (MessagingException e) {
                throw new SieveException("Exception occured while evaluating variable expression.", e);
            }
        }
    }
    return result;
}
Also used : SieveException(org.apache.jsieve.exception.SieveException) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) MessagingException(javax.mail.MessagingException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) StringListArgument(org.apache.jsieve.StringListArgument) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 14 with DummyMailAdapter

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

the class LinkedInTest 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) && ADDRESSES.contains(sender.emailPart.toLowerCase())) {
        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 15 with DummyMailAdapter

use of com.zimbra.cs.filter.DummyMailAdapter 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;
    }
    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.ENVELOPE, params.getKeys());
        } catch (MessagingException e) {
            throw new SieveException("Exception occured while evaluating variable expression.", e);
        }
    }
    if (COUNT_TAG.equals(params.getMatchType()) || VALUE_TAG.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() == null ? IS_TAG : 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)

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