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));
}
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);
}
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;
}
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");
}
}
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");
}
}
Aggregations