use of org.apache.jsieve.tests.optional.Envelope 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;
}
Aggregations