Search in sources :

Example 1 with Argument

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

the class AddHeader method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    Iterator<Argument> itr = arguments.getArgumentList().iterator();
    if (arguments.getArgumentList().size() == 2 || arguments.getArgumentList().size() == 3) {
        Argument arg = itr.next();
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            if (tag.is(LAST)) {
                last = Boolean.TRUE;
                arg = itr.next();
            } else {
                throw new SyntaxException("Invalid argument with addheader.");
            }
        }
        if (arg instanceof StringListArgument) {
            StringListArgument sla = (StringListArgument) arg;
            headerName = sla.getList().get(0);
        } else {
            throw new SyntaxException("Invalid argument with addheader.");
        }
        if (itr.hasNext()) {
            arg = itr.next();
            if (arg instanceof StringListArgument) {
                StringListArgument sla = (StringListArgument) arg;
                headerValue = sla.getList().get(0);
            } else {
                throw new SyntaxException("Invalid argument with addheader.");
            }
        } else {
            throw new SyntaxException("Invalid Number of arguments with addheader.");
        }
    } else {
        throw new SyntaxException("Invalid Number of arguments with addheader.");
    }
    if (!StringUtil.isNullOrEmpty(headerName)) {
        if (!CharsetUtil.US_ASCII.equals(CharsetUtil.checkCharset(headerName, CharsetUtil.US_ASCII))) {
            throw new SyntaxException("AddHeader:Header name must be printable ASCII only.");
        }
    } else {
        throw new SyntaxException("AddHeader:Header name must be present.");
    }
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument)

Example 2 with Argument

use of org.apache.jsieve.Argument 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 3 with Argument

use of org.apache.jsieve.Argument 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 4 with Argument

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

the class ZimbraComparatorUtils method parseTestArguments.

/**
     * Parses the address test string in the 'if' part of the filter rule
     *
     * @see org.apache.jsieve.tests.AbstractComparatorTest#executeBasic(MailAdapter mail, Arguments arguments, SieveContext context)
     *
     * @param mail
     * @param arguments
     * @param context
     * @return TestParameters
     */
public static TestParameters parseTestArguments(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    String addressPart = null;
    String comparator = null;
    String matchType = null;
    String operator = null;
    List<String> headerNames = null;
    List<String> keys = null;
    boolean nextArgumentIsRelationalSign = false;
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    boolean stop = false;
    // Tag processing
    while (!stop && argumentsIter.hasNext()) {
        Argument argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            // [ADDRESS-PART]?
            if (addressPart == null && (LOCALPART_TAG.equalsIgnoreCase(tag) || DOMAIN_TAG.equalsIgnoreCase(tag) || ALL_TAG.equalsIgnoreCase(tag))) {
                addressPart = tag;
            } else // [COMPARATOR]?
            if (comparator == null && COMPARATOR_TAG.equalsIgnoreCase(tag)) {
                // The next argument must be a string list
                if (argumentsIter.hasNext()) {
                    argument = argumentsIter.next();
                    if (argument instanceof StringListArgument) {
                        List stringList = ((StringListArgument) argument).getList();
                        if (stringList.size() != 1) {
                            throw new SyntaxException("Expecting exactly one String");
                        }
                        comparator = (String) stringList.get(0);
                    } else {
                        throw new 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");
            }
        } 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()) {
        Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            headerNames = ((StringListArgument) argument).getList();
        }
    }
    if (headerNames == null) {
        throw context.getCoordinate().syntaxException("Expecting a StringList of header names");
    }
    // The next argument MUST be a string-list of keys
    if (argumentsIter.hasNext()) {
        Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            keys = ((StringListArgument) argument).getList();
        }
    }
    if (keys == null) {
        throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
    }
    if (argumentsIter.hasNext()) {
        throw context.getCoordinate().syntaxException("Found unexpected arguments");
    }
    TestParameters result = new TestParameters(mail, addressPart, comparator, matchType, operator, headerNames, keys);
    return result;
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) PatternSyntaxException(java.util.regex.PatternSyntaxException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) List(java.util.List) StringListArgument(org.apache.jsieve.StringListArgument)

Example 5 with Argument

use of org.apache.jsieve.Argument 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)

Aggregations

Argument (org.apache.jsieve.Argument)26 StringListArgument (org.apache.jsieve.StringListArgument)25 SyntaxException (org.apache.jsieve.exception.SyntaxException)20 TagArgument (org.apache.jsieve.TagArgument)17 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)8 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)4 ServiceException (com.zimbra.common.service.ServiceException)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)2 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 List (java.util.List)2 TimeZone (java.util.TimeZone)2 NumberArgument (org.apache.jsieve.NumberArgument)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1