use of org.apache.jsieve.StringListArgument 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("Invalid argument with addheader.");
}
}
if (arg instanceof StringListArgument) {
StringListArgument sla = (StringListArgument) arg;
headerName = sla.getList().get(0);
} else {
throw new SyntaxException("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("Invalid argument with addheader.");
}
} else {
throw new SyntaxException("Invalid Number of arguments with addheader.");
}
} else {
throw new SyntaxException("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.");
}
}
use of org.apache.jsieve.StringListArgument 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.StringListArgument 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());
}
use of org.apache.jsieve.StringListArgument in project zm-mailbox by Zimbra.
the class DummyCustomTag 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");
}
use of org.apache.jsieve.StringListArgument in project zm-mailbox by Zimbra.
the class ImportanceTest method executeBasic.
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
FilterTest.Importance importance;
if (argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
importance = FilterTest.Importance.valueOf(((StringListArgument) argument).getList().get(0));
} else {
throw new SyntaxException("Expecting a string");
}
} else {
throw new SyntaxException("Unexpected end of arguments");
}
// First check "Importance" header
List<String> headers = Arrays.asList("Importance");
List<String> values = null;
switch(importance) {
case high:
values = Arrays.asList("High");
break;
case low:
values = Arrays.asList("Low");
break;
case normal:
values = Arrays.asList("High", "Low");
}
boolean result1 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
// Now check "X-Priority" header
headers = Arrays.asList("X-Priority");
values = null;
switch(importance) {
case high:
values = Arrays.asList("1");
break;
case low:
values = Arrays.asList("5");
break;
case normal:
// normal is when it is neither high importance nor low importance
values = Arrays.asList("1", "5");
}
boolean result2 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
// normal is when it is neither high importance nor low importance
return importance == FilterTest.ImportanceTest.Importance.normal ? !(result1 || result2) : result1 || result2;
}
Aggregations