Search in sources :

Example 16 with Argument

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

the class ImportanceTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    FilterTest.Importance importance;
    if (argumentsIter.hasNext()) {
        Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            importance = FilterTest.Importance.valueOf(((StringListArgument) argument).getList().get(0));
        } else {
            throw new SyntaxException("Expecting a string");
        }
    } else {
        throw new SyntaxException("Unexpected end of arguments");
    }
    // First check "Importance" header
    List<String> headers = Arrays.asList("Importance");
    List<String> values = null;
    switch(importance) {
        case high:
            values = Arrays.asList("High");
            break;
        case low:
            values = Arrays.asList("Low");
            break;
        case normal:
            values = Arrays.asList("High", "Low");
    }
    boolean result1 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
    // Now check "X-Priority" header
    headers = Arrays.asList("X-Priority");
    values = null;
    switch(importance) {
        case high:
            values = Arrays.asList("1");
            break;
        case low:
            values = Arrays.asList("5");
            break;
        case normal:
            // normal is when it is neither high importance nor low importance
            values = Arrays.asList("1", "5");
    }
    boolean result2 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
    // normal is when it is neither high importance nor low importance
    return importance == FilterTest.ImportanceTest.Importance.normal ? !(result1 || result2) : result1 || result2;
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) FilterTest(com.zimbra.soap.mail.type.FilterTest) SyntaxException(org.apache.jsieve.exception.SyntaxException) StringListArgument(org.apache.jsieve.StringListArgument)

Example 17 with Argument

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

the class NotifyMethodCapabilityTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    String comparator = ASCII_CASEMAP_COMPARATOR;
    String matchType = IS_TAG;
    String uri = null;
    String capability = null;
    List<String> keys = null;
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    boolean stop = false;
    /*
         * Test notify_method_capability
         * Usage:  notify_method_capability [COMPARATOR] [MATCH-TYPE]
         *         <notification-uri: string>
         *         <notification-capability: string>
         *         <key-list: string-list>
         */
    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;
            } else {
                throw context.getCoordinate().syntaxException("Found unexpected TagArgument: \"" + tag + "\"");
            }
        } else {
            // Stop when a non-tag argument is encountered
            argumentsIter.previous();
            stop = true;
        }
    }
    // The next argument MUST be a string of notification-uri
    if (argumentsIter.hasNext()) {
        final Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            uri = ((StringListArgument) argument).getList().get(0);
        }
    }
    if (null == uri) {
        throw context.getCoordinate().syntaxException("Expecting a String of uri");
    }
    // The next argument MUST be a string of notification-capability
    if (argumentsIter.hasNext()) {
        final Argument argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            capability = ((StringListArgument) argument).getList().get(0);
        }
    }
    if (null == capability) {
        throw context.getCoordinate().syntaxException("Expecting a String of capability");
    }
    // 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();
        }
    }
    if (null == keys) {
        throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
    } else {
        for (String key : keys) {
            if (!CAPABILITY_YES.equalsIgnoreCase(key) && !CAPABILITY_NO.equalsIgnoreCase(key) && !CAPABILITY_MAYBE.equalsIgnoreCase(key)) {
                throw context.getCoordinate().syntaxException("Invalid key value: [" + key + "]");
            }
        }
    }
    if (argumentsIter.hasNext()) {
        throw context.getCoordinate().syntaxException("Found unexpected arguments");
    }
    return test(comparator, matchType, uri, capability, keys);
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument)

Example 18 with Argument

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

the class Reply method validateArguments.

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

Example 19 with Argument

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

the class SetVariable method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    List<Argument> args = arguments.getArgumentList();
    // RFC 5229
    //  ":lower" / ":upper" / ":lowerfirst" / ":upperfirst" /
    //  ":quotewildcard" / ":length"
    //    set "name" "Ethelbert"
    //    set "a" "juMBlEd lETteRS";             => "juMBlEd lETteRS"
    //    set :length "b" "${a}";                => "15"
    //    set :lower "b" "${a}";                 => "jumbled letters"
    //    set :upperfirst "b" "${a}";            => "JuMBlEd lETteRS"
    //    set :upperfirst :lower "b" "${a}";     => "Jumbled letters"
    //    set :quotewildcard "b" "Rock*";        => "Rock\*"
    // RFC 5435
    //  ":encodeurl"
    //    set :encodeurl "body_param" "Safe body&evil=evilbody"; => "safe+body%26evil%3Devilbody"
    int varArgCount = 0;
    String key = null;
    String[] operations = new String[OPERATIONS_IDX];
    if (args.size() >= 2) {
        for (Argument arg : arguments.getArgumentList()) {
            if (arg instanceof TagArgument) {
                TagArgument tag = (TagArgument) arg;
                String tagValue = tag.getTag();
                if (!isValidModifier(tagValue)) {
                    throw new SyntaxException("Invalid variable modifier:" + tagValue);
                } else {
                    int index = getIndex(tagValue);
                    if (StringUtil.isNullOrEmpty(operations[index])) {
                        operations[index] = tagValue.toLowerCase();
                    } else {
                        throw new SyntaxException("Cannot use two or more modifiers of the same" + " precedence in a single \"set\" action. Modifiers used: " + tagValue + " and " + operations[index]);
                    }
                }
            } else {
                String argument = ((StringListArgument) arg).getList().get(0);
                ZimbraLog.filter.debug("set variable argument: " + argument);
                if (varArgCount == 0) {
                    key = argument;
                }
                ++varArgCount;
            }
        }
        if (varArgCount != 2) {
            throw new SyntaxException("Exactly 2 arguments permitted. Found " + varArgCount);
        } else {
            if (!(isValidIdentifier(key))) {
                throw new SyntaxException("Variable identifier is invalid, got identifier " + key);
            }
        }
    } else {
        throw new SyntaxException("Minimum 2 arguments are needed. Usage: set :upperfirst \"b\" \"hello\";. Arguments found: " + arguments);
    }
}
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)

Example 20 with Argument

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

the class StringTest method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    if (arguments.getArgumentList().size() < 3) {
        throw new SyntaxException("At least 3 argument are needed. Found " + arguments);
    }
    for (Argument a : arguments.getArgumentList()) {
        System.out.println(a);
        ZimbraLog.filter.debug(a);
    }
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException)

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