Search in sources :

Example 1 with SyntaxException

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

the class FilterUtil method replaceVariables.

/**
 * Look-up the variable table to get the set value.
 * If the 'sourceStr' contains a variable-ref (formatted with ${variable-name}), this method looks up the
 * variable table with its variable-name and replaces the variable with the text value assigned by
 * the 'set' command.
 * According to the RFC 5229 Section 4., "Variable names are case insensitive."
 *
 * @param variables a list of variable name/value
 * @param matchedVariables a list of Matched Variables
 * @param sourceStr text string that may contain "variable-ref" (RFC 5229 Section 3.)
 * @return Replaced text string
 * @throws SyntaxException
 */
public static String replaceVariables(ZimbraMailAdapter mailAdapter, String sourceStr) throws SyntaxException {
    if (null == mailAdapter) {
        return sourceStr;
    }
    if (sourceStr.indexOf("${") == -1) {
        return sourceStr;
    }
    validateVariableIndex(sourceStr);
    try {
        Require.checkCapability(mailAdapter, CAPABILITY_VARIABLES);
    } catch (SyntaxException e) {
        ZimbraLog.filter.info("\"variables\" capability is not declared. No variables will be replaced");
        return sourceStr;
    }
    Map<String, String> variables = mailAdapter.getVariables();
    List<String> matchedVariables = mailAdapter.getMatchedValues();
    ZimbraLog.filter.debug("Variable: %s ", sourceStr);
    ZimbraLog.filter.debug("Variable available: %s : %s", variables, matchedVariables);
    String resultStr = sourceStr;
    // (1) Resolve the Matched Variables (numeric variables; "${N}" (N=0,1,...9)
    int i = 0;
    for (; i < matchedVariables.size() && i < 10; i++) {
        String keyName = "(?i)" + "\\$\\{0*" + String.valueOf(i) + "\\}";
        resultStr = resultStr.replaceAll(keyName, Matcher.quoteReplacement(matchedVariables.get(i)));
    }
    // (2) Replace the empty string to Matched Variables whose index is out of range
    for (; i < 10; i++) {
        String keyName = "(?i)" + "\\$\\{0*" + String.valueOf(i) + "\\}";
        resultStr = resultStr.replaceAll(keyName, Matcher.quoteReplacement(""));
    }
    // (3) Resolve the named variables ("${xxx}")
    resultStr = leastGreedyReplace(variables, resultStr, mailAdapter.getMimeVariables());
    ZimbraLog.filter.debug("Sieve: variable value is: %s", resultStr);
    return resultStr;
}
Also used : SyntaxException(org.apache.jsieve.exception.SyntaxException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 2 with SyntaxException

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

the class FilterUtil method validateVariableIndex.

private static void validateVariableIndex(String srcStr) throws SyntaxException {
    boolean match = false;
    String negativeIndexPattern = "\\$\\{-\\d*\\}";
    String exceedsIndexPattern = "\\$\\{0*[1-9]{1,}\\d{1,}\\}";
    Pattern pattern = Pattern.compile(negativeIndexPattern);
    Matcher matcher = pattern.matcher(srcStr);
    match = matcher.find();
    if (!match) {
        pattern = Pattern.compile(exceedsIndexPattern);
        matcher = pattern.matcher(srcStr);
        match = matcher.find();
    }
    if (match) {
        ZimbraLog.filter.debug("Invalid variable index %s ", srcStr);
        throw new SyntaxException("Invalid variable index " + srcStr);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) SyntaxException(org.apache.jsieve.exception.SyntaxException)

Example 3 with SyntaxException

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

use of org.apache.jsieve.exception.SyntaxException 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 5 with SyntaxException

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

the class ZimbraVariablesCtrl 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;
    for (Argument arg : arguments.getArgumentList()) {
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            String tagValue = tag.getTag();
            if (RESET.equalsIgnoreCase(tagValue)) {
                mailAdapter.resetValues();
            } else {
                throw new SyntaxException("Invalid tag: [" + tagValue + "]");
            }
        }
    }
    return null;
}
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) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

SyntaxException (org.apache.jsieve.exception.SyntaxException)26 Argument (org.apache.jsieve.Argument)20 StringListArgument (org.apache.jsieve.StringListArgument)19 TagArgument (org.apache.jsieve.TagArgument)13 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)8 ServiceException (com.zimbra.common.service.ServiceException)4 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)4 List (java.util.List)3 Test (org.junit.Test)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)2 EditHeaderExtension (com.zimbra.cs.filter.jsieve.EditHeaderExtension)2 ParseException (java.text.ParseException)2 Calendar (java.util.Calendar)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 NumberArgument (org.apache.jsieve.NumberArgument)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1 HeaderConstants (com.zimbra.common.soap.HeaderConstants)1 StringUtil (com.zimbra.common.util.StringUtil)1