Search in sources :

Example 6 with Argument

use of org.apache.jsieve.Argument 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(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(LAST)) {
                this.last = true;
            } else if (tag.is(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(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(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(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 7 with Argument

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

the class Notify method executeBasic.

@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    List<Argument> args = arguments.getArgumentList();
    if (args.size() < 3)
        throw new SyntaxException("Missing arguments");
    Argument nextArg = args.get(0);
    if (!(nextArg instanceof StringListArgument))
        throw new SyntaxException("Expected string");
    List<String> list = ((StringListArgument) nextArg).getList();
    if (list.size() != 1)
        throw new SyntaxException("Expected exactly one email address");
    String emailAddr = list.get(0);
    nextArg = args.get(1);
    if (!(nextArg instanceof StringListArgument))
        throw new SyntaxException("Expected string");
    list = ((StringListArgument) nextArg).getList();
    if (list.size() != 1)
        throw new SyntaxException("Expected exactly one subject");
    String subjectTemplate = list.get(0);
    nextArg = args.get(2);
    if (!(nextArg instanceof StringListArgument))
        throw new SyntaxException("Expected string");
    list = ((StringListArgument) nextArg).getList();
    if (list.size() != 1)
        throw new SyntaxException("Expected exactly one body");
    String bodyTemplate = list.get(0);
    int maxBodyBytes = -1;
    List<String> origHeaders = null;
    if (args.size() == 4) {
        nextArg = args.get(3);
        if (nextArg instanceof NumberArgument)
            maxBodyBytes = ((NumberArgument) nextArg).getInteger();
        else if (nextArg instanceof StringListArgument)
            origHeaders = ((StringListArgument) nextArg).getList();
        else
            throw new SyntaxException("Invalid argument");
    }
    if (args.size() == 5) {
        nextArg = args.get(3);
        if (!(nextArg instanceof NumberArgument))
            throw new SyntaxException("Expected int");
        maxBodyBytes = ((NumberArgument) nextArg).getInteger();
        nextArg = args.get(4);
        if (!(nextArg instanceof StringListArgument))
            throw new SyntaxException("Expected string list");
        origHeaders = ((StringListArgument) nextArg).getList();
    }
    mail.addAction(new ActionNotify(emailAddr, subjectTemplate, bodyTemplate, maxBodyBytes, origHeaders));
    return null;
}
Also used : NumberArgument(org.apache.jsieve.NumberArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) NumberArgument(org.apache.jsieve.NumberArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) StringListArgument(org.apache.jsieve.StringListArgument)

Example 8 with Argument

use of org.apache.jsieve.Argument 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 9 with Argument

use of org.apache.jsieve.Argument 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.clearValues();
            } 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)

Example 10 with Argument

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

the class FlaggedTest method validateArguments.

@Override
protected void validateArguments(Arguments args, SieveContext ctx) throws SieveException {
    Iterator<Argument> itr = args.getArgumentList().iterator();
    if (itr.hasNext()) {
        Argument arg = itr.next();
        if (arg instanceof StringListArgument) {
            ImmutableSet.Builder<ActionFlag> builder = ImmutableSet.builder();
            for (String name : ((StringListArgument) arg).getList()) {
                ActionFlag flag = ActionFlag.of(name);
                if (flag != null) {
                    builder.add(flag);
                } else {
                    throw ctx.getCoordinate().syntaxException("Invalid flag: " + name);
                }
            }
            flags = builder.build();
        } else {
            throw ctx.getCoordinate().syntaxException("Unexpected argument: " + arg.getValue());
        }
    } else {
        throw ctx.getCoordinate().syntaxException("No flag names specified");
    }
}
Also used : Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) ImmutableSet(com.google.common.collect.ImmutableSet) StringListArgument(org.apache.jsieve.StringListArgument)

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