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