Search in sources :

Example 1 with StringListArgument

use of org.apache.jsieve.StringListArgument 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) || HeaderConstants.COUNT.equalsIgnoreCase(tag) || HeaderConstants.VALUE.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 && (HeaderConstants.GT_OP.equalsIgnoreCase(symbol) || HeaderConstants.GE_OP.equalsIgnoreCase(symbol) || HeaderConstants.LT_OP.equalsIgnoreCase(symbol) || HeaderConstants.LE_OP.equalsIgnoreCase(symbol) || HeaderConstants.EQ_OP.equalsIgnoreCase(symbol) || HeaderConstants.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 2 with StringListArgument

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

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

the class ZimbraVariablesCtrl method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    List<Argument> args = arguments.getArgumentList();
    if (args.size() > 1) {
        throw new SyntaxException("More than one argument found (" + args.size() + ")");
    }
    for (Argument arg : arguments.getArgumentList()) {
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            String tagValue = tag.getTag();
            if (!RESET.equalsIgnoreCase(tagValue)) {
                throw new SyntaxException("Invalid tag: [" + tagValue + "]");
            }
        } else {
            if (arg instanceof StringListArgument) {
                String argument = ((StringListArgument) arg).getList().get(0);
                throw new SyntaxException("Invalid argument: [" + argument + "]");
            } else {
                throw new SyntaxException("Invalid argument");
            }
        }
    }
}
Also used : TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument) Argument(org.apache.jsieve.Argument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument)

Example 4 with StringListArgument

use of org.apache.jsieve.StringListArgument 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;
    Require.checkCapability(mailAdapter, CAPABILITY_VARIABLES);
    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)) {
                checkCapability(mailAdapter, 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 5 with StringListArgument

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

the class EditHeaderExtension method setupEditHeaderData.

// Utility methods
/**
 * This method sets values provided with replaceheader or deleteheader in <b>EditHeaderExtension</b> object.
 * @param arguments
 * @param ac
 * @throws SyntaxException
 * @throws OperationException
 */
public void setupEditHeaderData(Arguments arguments, AbstractCommand ac) throws SyntaxException, OperationException {
    // set up class variables
    Iterator<Argument> itr = arguments.getArgumentList().iterator();
    while (itr.hasNext()) {
        Argument arg = itr.next();
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            if (tag.is(HeaderConstants.INDEX)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof NumberArgument) {
                        this.index = ((NumberArgument) arg).getInteger();
                    } else {
                        throw new SyntaxException("Invalid index provided with replaceheader : " + arg);
                    }
                }
            } else if (tag.is(HeaderConstants.LAST)) {
                this.last = true;
            } else if (tag.is(HeaderConstants.NEW_NAME)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        String origNewName = sla.getList().get(0);
                        if (StringUtil.isNullOrEmpty(origNewName)) {
                            throw new SyntaxException("New name must be present with :newname in replaceheader : " + arg);
                        }
                        this.newName = origNewName;
                    } else {
                        throw new SyntaxException("New name not provided with :newname in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(HeaderConstants.NEW_VALUE)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.newValue = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("New value not provided with :newValue in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(HeaderConstants.COUNT)) {
                if (this.valueTag) {
                    throw new SyntaxException(":count and :value both can not be used with replaceheader");
                }
                this.countTag = true;
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.relationalComparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Relational comparator not provided with :count in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(HeaderConstants.VALUE)) {
                if (this.countTag) {
                    throw new SyntaxException(":count and :value both can not be used with replaceheader");
                }
                this.valueTag = true;
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.relationalComparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Relational comparator not provided with :value in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(ComparatorTags.COMPARATOR_TAG)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.comparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Comparator not provided with :comparator in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(MatchTypeTags.CONTAINS_TAG)) {
                this.contains = true;
            } else if (tag.is(MatchTypeTags.IS_TAG)) {
                this.is = true;
            } else if (tag.is(MatchTypeTags.MATCHES_TAG)) {
                this.matches = true;
            } else {
                throw new SyntaxException("Invalid tag argument provided with replaceheader.");
            }
        } else if (arg instanceof StringListArgument) {
            if (ac instanceof ReplaceHeader) {
                StringListArgument sla = (StringListArgument) arg;
                this.key = sla.getList().get(0);
                if (itr.hasNext()) {
                    arg = itr.next();
                    sla = (StringListArgument) arg;
                    this.valueList = sla.getList();
                }
            } else if (ac instanceof DeleteHeader) {
                StringListArgument sla = (StringListArgument) arg;
                this.key = sla.getList().get(0);
                if (itr.hasNext()) {
                    arg = itr.next();
                    sla = (StringListArgument) arg;
                    this.valueList = sla.getList();
                } else {
                    ZimbraLog.filter.info("Value for " + this.key + " is not provided in deleteheader. So all headers with this key will be deleted.");
                }
            } else {
                throw new OperationException("Invalid instance of AbstractCommand is obtained.");
            }
        } else {
            ZimbraLog.filter.info("Unknown argument provided: " + arg.getValue());
        }
    }
    if (!(isIs() || isContains() || isMatches() || isCountTag() || isValueTag())) {
        this.is = true;
    }
}
Also used : NumberArgument(org.apache.jsieve.NumberArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) NumberArgument(org.apache.jsieve.NumberArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument) OperationException(org.apache.jsieve.exception.OperationException)

Aggregations

StringListArgument (org.apache.jsieve.StringListArgument)28 Argument (org.apache.jsieve.Argument)25 SyntaxException (org.apache.jsieve.exception.SyntaxException)17 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)14 TagArgument (org.apache.jsieve.TagArgument)14 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 SieveException (org.apache.jsieve.exception.SieveException)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Account (com.zimbra.cs.account.Account)1 FilterTest (com.zimbra.soap.mail.type.FilterTest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1