Search in sources :

Example 11 with SyntaxException

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

the class CurrentDayOfWeekTest 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 ":is" tag
    String comparator = null;
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof TagArgument)
            comparator = ((TagArgument) argument).getTag();
    }
    if (!":is".equals(comparator))
        throw new SyntaxException("Expecting \":is\"");
    // Second argument MUST be a list of day of week indices; 0=Sunday, 6=Saturday
    Set<Integer> daysToCheckAgainst = new HashSet<Integer>();
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            List<String> valList = ((StringListArgument) argument).getList();
            for (String val : valList) {
                int day;
                try {
                    day = Integer.valueOf(val);
                } catch (NumberFormatException e) {
                    throw new SyntaxException(e);
                }
                if (day < 0 || day > 6)
                    throw new SyntaxException("Expected values between 0 - 6");
                // In Java 1=Sunday, 7=Saturday
                daysToCheckAgainst.add(day + 1);
            }
        }
    }
    if (daysToCheckAgainst.isEmpty())
        throw new SyntaxException("Expecting at least one value");
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext())
        throw new SyntaxException("Found unexpected argument(s)");
    TimeZone accountTimeZone;
    try {
        accountTimeZone = Util.getAccountTimeZone(((ZimbraMailAdapter) mail).getMailbox().getAccount());
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
    Calendar rightNow = Calendar.getInstance(accountTimeZone);
    return daysToCheckAgainst.contains(rightNow.get(Calendar.DAY_OF_WEEK));
}
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) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) HashSet(java.util.HashSet)

Example 12 with SyntaxException

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

the class DateTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
    String comparator = null;
    Date date = null;
    @SuppressWarnings("unchecked") ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
    // First argument MUST be a tag of ":before" or ":after"
    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 (null == comparator) {
        throw new SyntaxException("Expecting \"" + BEFORE + "\" or \"" + AFTER + "\"");
    }
    // Second argument MUST be a date
    if (argumentsIter.hasNext()) {
        Object argument = argumentsIter.next();
        if (argument instanceof StringListArgument) {
            StringListArgument strList = (StringListArgument) argument;
            String datestr = (String) strList.getList().get(0);
            try {
                date = mShortDateFormat.parse(datestr);
            } catch (ParseException e) {
            }
        }
    }
    if (null == date) {
        throw new SyntaxException("Expecting a valid date (yyyyMMdd)");
    }
    // There MUST NOT be any further arguments
    if (argumentsIter.hasNext()) {
        throw new SyntaxException("Found unexpected argument(s)");
    }
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    return test(mail, comparator, date);
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 13 with SyntaxException

use of org.apache.jsieve.exception.SyntaxException 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 {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    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 = FilterUtil.replaceVariables(mailAdapter, 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 = FilterUtil.replaceVariables(mailAdapter, 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 = FilterUtil.replaceVariables(mailAdapter, 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) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 14 with SyntaxException

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

the class Redirect method validateArguments.

@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
    List<Argument> args = arguments.getArgumentList();
    if (args.size() < 1 || args.size() > 2) {
        throw new SyntaxException("Exactly 1 or 2 arguments permitted. Found " + args.size());
    }
    Argument argument;
    String copyArg;
    if (args.size() == 1) {
        // address list argument
        argument = (Argument) args.get(0);
    } else {
        copyArg = ((Argument) args.get(0)).getValue().toString();
        // if arguments size is 2; first argument should be :copy
        if (!Copy.COPY.equalsIgnoreCase(copyArg)) {
            throw new SyntaxException("Error in sieve redirect. Expecting argument :copy");
        }
        // address list argument
        argument = (Argument) args.get(1);
    }
    // address list argument should be a String list
    if (!(argument instanceof StringListArgument)) {
        throw new SyntaxException("Expecting a string-list");
    }
    // address list argument should contain exactly one address
    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 15 with SyntaxException

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

the class SetVariableTest method testSetVarWithModifiersInValid.

@Test
public void testSetVarWithModifiersInValid() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
        try {
            filterScript = "require [\"variables\"];\n" + "set \"hello\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Atleast 2 argument are needed. Found Arguments: [[hello]]") > -1);
            }
        }
        try {
            filterScript = "require [\"variables\"];\n" + "set :lownner \"var\" \"hello\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Invalid variable modifier:") > -1);
            }
        }
        try {
            filterScript = "require [\"variables\"];\n" + "set :lower \"var\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Invalid variable modifier:") > -1);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) SyntaxException(org.apache.jsieve.exception.SyntaxException) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

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