Search in sources :

Example 11 with Argument

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

the class Redirect method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    List<Argument> args = arguments.getArgumentList();
    if (args.size() < 1 || args.size() > 2) {
        throw new SyntaxException("Exactly 1 or 2 arguments permitted. Found " + args.size());
    }
    Argument argument;
    String copyArg;
    if (args.size() == 1) {
        // address list argument
        argument = (Argument) args.get(0);
    } else {
        copyArg = ((Argument) args.get(0)).getValue().toString();
        // if arguments size is 2; first argument should be :copy
        if (!Copy.COPY.equalsIgnoreCase(copyArg)) {
            throw new SyntaxException("Error in sieve redirect. Expecting argument :copy");
        }
        // address list argument
        argument = (Argument) args.get(1);
    }
    // address list argument should be a String list
    if (!(argument instanceof StringListArgument)) {
        throw new SyntaxException("Expecting a string-list");
    }
    // address list argument should contain exactly one address  
    if (1 != ((StringListArgument) argument).getList().size()) {
        throw new SyntaxException("Expecting exactly one argument");
    }
}
Also used : StringListArgument(org.apache.jsieve.StringListArgument) Argument(org.apache.jsieve.Argument) SyntaxException(org.apache.jsieve.exception.SyntaxException) StringListArgument(org.apache.jsieve.StringListArgument)

Example 12 with Argument

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

the class SetVariable method executeBasic.

@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    this.validateArguments(arguments, context);
    Map<String, String> existingVars = mailAdapter.getVariables();
    List<String> matchedValues = mailAdapter.getMatchedValues();
    String[] operations = new String[OPERATIONS_IDX];
    String key = null;
    String value = null;
    int index = 0;
    for (Argument a : arguments.getArgumentList()) {
        if (a instanceof TagArgument) {
            TagArgument tag = (TagArgument) a;
            String tagValue = tag.getTag();
            if (isValidModifier(tagValue)) {
                operations[getIndex(tagValue)] = tagValue.toLowerCase();
            }
        } else {
            String argument = ((StringListArgument) a).getList().get(0);
            if (index == 0) {
                key = FilterUtil.handleQuotedAndEncodedVar(argument);
            } else {
                if (argument.contains("${")) {
                    value = FilterUtil.replaceVariables(mailAdapter, argument);
                } else {
                    value = argument;
                }
            }
            ++index;
        }
    }
    value = applyModifiers(value, operations);
    mailAdapter.addVariable(key, value);
    return null;
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 13 with Argument

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

the class Tag method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    @SuppressWarnings("unchecked") List<Argument> args = arguments.getArgumentList();
    if (args.size() != 1)
        throw new SyntaxException("Exactly 1 argument permitted. Found " + args.size());
    Object argument = args.get(0);
    if (!(argument instanceof StringListArgument))
        throw new SyntaxException("Expecting a string-list");
    if (1 != ((StringListArgument) argument).getList().size())
        throw new SyntaxException("Expecting exactly one argument");
}
Also used : StringListArgument(org.apache.jsieve.StringListArgument) Argument(org.apache.jsieve.Argument) SyntaxException(org.apache.jsieve.exception.SyntaxException) StringListArgument(org.apache.jsieve.StringListArgument)

Example 14 with Argument

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

the class HeaderTest method executeBasic.

/**
     * <p>
     * From RFC 5228, Section 5.7...
     * </p>
     * <code>
     * Syntax: header [COMPARATOR] [MATCH-TYPE]
     *       &lt;header-names: string-list&gt; &lt;key-list: string-list&gt;
     *
     * @see org.apache.jsieve.tests.Header#executeBasic(MailAdapter,
     *      Arguments, SieveContext)
     */
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    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) {
            final String tag = ((TagArgument) argument).getTag();
            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) {
            headerNames = ((StringListArgument) argument).getList();
        }
    }
    if (null == headerNames) {
        throw context.getCoordinate().syntaxException("Expecting a StringListof header names");
    }
    headerNames = replaceVariables(headerNames, mail);
    for (String headerName : headerNames) {
        if (headerName != null) {
            FilterUtil.headerNameHasSpace(headerName);
        }
    }
    // The next argument MUST be a string-list of keys
    if (argumentsIter.hasNext()) {
        final Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            keys = ((StringListArgument) argument).getList();
        }
    }
    keys = replaceVariables(keys, mail);
    if (null == keys) {
        throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
    }
    if (argumentsIter.hasNext()) {
        throw context.getCoordinate().syntaxException("Found unexpected arguments");
    }
    if (matchType != null && (COUNT_TAG.equalsIgnoreCase(matchType) || VALUE_TAG.equalsIgnoreCase(matchType) || IS_TAG.equalsIgnoreCase(matchType))) {
        return match(mail, ZimbraComparatorUtils.getComparator(comparator, matchType), matchType, operator, headerNames, keys, context);
    } else {
        return match(mail, ZimbraComparatorUtils.getComparator(comparator, matchType), (matchType == null ? IS_TAG : matchType), headerNames, keys, context);
    }
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument)

Example 15 with Argument

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

the class DummyCustomTag method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    @SuppressWarnings("unchecked") List<Argument> args = arguments.getArgumentList();
    if (args.size() != 1)
        throw new SyntaxException("Exactly 1 argument permitted. Found " + args.size());
    Object argument = args.get(0);
    if (!(argument instanceof StringListArgument))
        throw new SyntaxException("Expecting a string-list");
    if (1 != ((StringListArgument) argument).getList().size())
        throw new SyntaxException("Expecting exactly one argument");
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) StringListArgument(org.apache.jsieve.StringListArgument)

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