Search in sources :

Example 16 with TagArgument

use of org.apache.jsieve.TagArgument 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 {
                            caseSensitive = Sieve.Comparator.ioctet == Sieve.Comparator.fromString(strList.getList().get(0));
                        } 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 17 with TagArgument

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

the class VariableLog method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    if (arguments.getArgumentList().size() > 2) {
        throw new SyntaxException("Log: maximum 2 parameters allowed with Log");
    }
    boolean foundTagArg = false;
    int index = 0;
    Iterator<Argument> itr = arguments.getArgumentList().iterator();
    while (itr.hasNext()) {
        Argument arg = itr.next();
        index++;
        if (arg instanceof TagArgument) {
            if (foundTagArg) {
                throw new SyntaxException("Log: Multiple log levels are not allowed.");
            }
            if (index > 1) {
                throw new SyntaxException("Log: Log level must be mentioned before log message.");
            }
            TagArgument tag = (TagArgument) arg;
            if (!(tag.is(":" + FilterAction.LogAction.LogLevel.fatal.toString()) || tag.is(":" + FilterAction.LogAction.LogLevel.error.toString()) || tag.is(":" + FilterAction.LogAction.LogLevel.warn.toString()) || tag.is(":" + FilterAction.LogAction.LogLevel.info.toString()) || tag.is(":" + FilterAction.LogAction.LogLevel.debug.toString()) || tag.is(":" + FilterAction.LogAction.LogLevel.trace.toString()))) {
                throw new SyntaxException("Log: Invalid log level provided - " + tag.getTag());
            }
            foundTagArg = true;
        }
        if (index > 1 && !foundTagArg) {
            throw new SyntaxException("Log: Only 1 text message allowed with log statement.");
        }
    }
    ZimbraLog.filter.debug("Log: Validation successfful");
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument)

Aggregations

TagArgument (org.apache.jsieve.TagArgument)17 Argument (org.apache.jsieve.Argument)16 StringListArgument (org.apache.jsieve.StringListArgument)15 SyntaxException (org.apache.jsieve.exception.SyntaxException)12 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)8 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)4 ServiceException (com.zimbra.common.service.ServiceException)3 Date (java.util.Date)3 List (java.util.List)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)2 ParseException (java.text.ParseException)2 ArrayList (java.util.ArrayList)2 Calendar (java.util.Calendar)2 TimeZone (java.util.TimeZone)2 Sieve (com.zimbra.common.filter.Sieve)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