Search in sources :

Example 6 with SyntaxException

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

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

the class CurrentTimeTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a tag of ":before" or ":after"
    String comparator = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument) {
            String tag = ((TagArgument) argument).getTag();
            if (tag.equals(BEFORE) || tag.equals(AFTER))
                comparator = tag;
            else
                throw new SyntaxException("Found unexpected: \"" + tag + "\"");
        }
    }
    if (comparator == null)
        throw new SyntaxException("Expecting \"" + BEFORE + "\" or \"" + AFTER + "\"");
    // Second argument MUST be a time in "HHmm" format
    DateFormat timeFormat = new SimpleDateFormat("HHmm");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    timeFormat.setTimeZone(accountTimeZone);
    Date timeArg = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            if (valList.size() != 1)
                throw new SyntaxException("Expecting exactly one time value");
            String timeStr = valList.get(0);
            try {
                timeArg = timeFormat.parse(timeStr);
            } catch (ParseException e) {
                throw new SyntaxException(e);
            }
        }
    }
    if (timeArg == null)
        throw new SyntaxException("Expecting a time value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    Calendar timeToCompareWith = Calendar.getInstance(accountTimeZone);
    // set the time part
    timeToCompareWith.setTime(timeArg);
    // now set the right date
    timeToCompareWith.set(rightNow.get(Calendar.YEAR), rightNow.get(Calendar.MONTH), rightNow.get(Calendar.DAY_OF_MONTH));
    return BEFORE.equals(comparator) ? rightNow.getTime().before(timeToCompareWith.getTime()) : rightNow.getTime().after(timeToCompareWith.getTime());
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 8 with SyntaxException

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

Example 9 with SyntaxException

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

the class EnvelopeTest method match.

/**
 * Compares the address with operator
 *
 * @param operator "gt" / "ge" / "lt" / "le" / "eq" / "ne"
 */
private boolean match(MailAdapter mail, String addressPart, String comparator, String matchType, String operator, List<String> headerNames, List<String> keys, SieveContext context) throws SieveException {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    // Iterate over the address fields looking for a match
    boolean isMatched = false;
    List<String> headerValues = Lists.newArrayListWithExpectedSize(2);
    for (final String headerName : headerNames) {
        if ("to".equalsIgnoreCase(headerName)) {
            // RFC 5231 4.2. ... The envelope "to" will always have only one
            // entry, which is the address of the user for whom the Sieve script
            // is running.
            String recipient = null;
            try {
                recipient = ((ZimbraMailAdapter) mail).getMailbox().getAccount().getMail();
            } catch (ServiceException e) {
                recipient = "";
            }
            headerValues.add(getMatchAddressPart(addressPart, recipient));
        } else if ("from".equalsIgnoreCase(headerName)) {
            List<String> values = getMatchingValues(mail, headerName);
            if (values != null) {
                if (matchType.equalsIgnoreCase(HeaderConstants.COUNT)) {
                    // RFC 5231 Section 4.2 Match Type COUNT says:
                    // | The envelope "from" will be 0 if the MAIL FROM is empty, or 1 if MAIL
                    // | FROM is not empty.
                    // This method could be called for other match type, such as :value or :is,
                    // remove the empty element only if the match type is :count.
                    values.removeIf(s -> Strings.isNullOrEmpty(s));
                }
                for (String value : values) {
                    headerValues.add(getMatchAddressPart(addressPart, value));
                }
            }
        } else {
            throw new SyntaxException("Unexpected header name as a value for <envelope-part>: '" + headerName + "'");
        }
    }
    if (HeaderConstants.COUNT.equals(matchType)) {
        for (final String key : keys) {
            isMatched = ZimbraComparatorUtils.counts(mail, comparator, operator, headerValues, ZimbraComparatorUtils.getMatchKey(addressPart, key), context);
            if (isMatched) {
                break;
            }
        }
    } else {
        Iterator headerValuesIter = headerValues.iterator();
        while (!isMatched && headerValuesIter.hasNext()) {
            List<String> normalizedKeys = Lists.newArrayListWithExpectedSize(keys.size());
            if (DOMAIN_TAG.equalsIgnoreCase(addressPart)) {
                for (String key : keys) {
                    normalizedKeys.add(key.toLowerCase());
                }
            } else {
                normalizedKeys = keys;
            }
            String headerValue = (String) headerValuesIter.next();
            // Iterate over the keys looking for a match
            for (final String key : keys) {
                isMatched = ZimbraComparatorUtils.match(mail, comparator, matchType, operator, headerValue, key, context);
                if (isMatched) {
                    break;
                }
            }
        }
    }
    return isMatched;
}
Also used : CAPABILITY_ENVELOPE(com.zimbra.cs.filter.JsieveConfigMapHandler.CAPABILITY_ENVELOPE) ASCII_NUMERIC_COMPARATOR(com.zimbra.cs.filter.jsieve.ComparatorName.ASCII_NUMERIC_COMPARATOR) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) Strings(com.google.common.base.Strings) ZimbraComparatorUtils(com.zimbra.cs.filter.ZimbraComparatorUtils) Lists(com.google.common.collect.Lists) FilterUtil(com.zimbra.cs.filter.FilterUtil) SieveException(org.apache.jsieve.exception.SieveException) MatchTypeTags(org.apache.jsieve.comparators.MatchTypeTags) HeaderConstants(com.zimbra.common.soap.HeaderConstants) DOMAIN_TAG(org.apache.jsieve.tests.AddressPartTags.DOMAIN_TAG) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) Iterator(java.util.Iterator) ASCII_CASEMAP_COMPARATOR(org.apache.jsieve.comparators.ComparatorNames.ASCII_CASEMAP_COMPARATOR) Arguments(org.apache.jsieve.Arguments) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) MailAdapter(org.apache.jsieve.mail.MailAdapter) ALL_TAG(org.apache.jsieve.tests.AddressPartTags.ALL_TAG) Envelope(org.apache.jsieve.tests.optional.Envelope) List(java.util.List) SyntaxException(org.apache.jsieve.exception.SyntaxException) LOCALPART_TAG(org.apache.jsieve.tests.AddressPartTags.LOCALPART_TAG) SieveContext(org.apache.jsieve.SieveContext) StringUtil(com.zimbra.common.util.StringUtil) IS_TAG(org.apache.jsieve.comparators.MatchTypeTags.IS_TAG) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) Iterator(java.util.Iterator) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) List(java.util.List) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 10 with SyntaxException

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

the class AddHeader method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    Iterator<Argument> itr = arguments.getArgumentList().iterator();
    if (arguments.getArgumentList().size() == 2 || arguments.getArgumentList().size() == 3) {
        Argument arg = itr.next();
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            if (tag.is(LAST)) {
                last = Boolean.TRUE;
                arg = itr.next();
            } else {
                throw new SyntaxException("addheader: Invalid argument with addheader.");
            }
        }
        if (arg instanceof StringListArgument) {
            StringListArgument sla = (StringListArgument) arg;
            headerName = sla.getList().get(0);
        } else {
            throw new SyntaxException("addheader: Invalid argument with addheader.");
        }
        if (itr.hasNext()) {
            arg = itr.next();
            if (arg instanceof StringListArgument) {
                StringListArgument sla = (StringListArgument) arg;
                headerValue = sla.getList().get(0);
            } else {
                throw new SyntaxException("addheader: Invalid argument with addheader.");
            }
        } else {
            throw new SyntaxException("addheader: Invalid Number of arguments with addheader.");
        }
    } else {
        throw new SyntaxException("addheader: Invalid Number of arguments with addheader.");
    }
    if (!StringUtil.isNullOrEmpty(headerName)) {
        if (!CharsetUtil.US_ASCII.equals(CharsetUtil.checkCharset(headerName, CharsetUtil.US_ASCII))) {
            throw new SyntaxException("addheader: Header name must be printable ASCII only.");
        }
    } else {
        throw new SyntaxException("addheader: Header name must be present.");
    }
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument)

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