use of com.zimbra.cs.filter.DummyMailAdapter 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 com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.
the class MeTest method executeBasic.
@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
assert (headers != null);
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
Mailbox mbox = ((ZimbraMailAdapter) mail).getMailbox();
List<InternetAddress> addrs = new ArrayList<InternetAddress>();
for (String header : headers) {
for (String value : mail.getHeader(header)) {
List<InternetAddress> inetAddrs = InternetAddress.parseHeader(value);
if (inetAddrs != null) {
addrs.addAll(inetAddrs);
}
}
}
try {
Account account = mbox.getAccount();
Set<String> me = AccountUtil.getEmailAddresses(account);
me.addAll(AccountUtil.getImapPop3EmailAddresses(account));
for (InternetAddress addr : addrs) {
String email = addr.getAddress();
if (email != null && me.contains(email.toLowerCase())) {
return true;
}
}
} catch (ServiceException e) {
ZimbraLog.filter.error("Failed to lookup my addresses", e);
}
return false;
}
use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.
the class MimeHeaderTest method match.
@SuppressWarnings("unchecked")
@Override
protected boolean match(MailAdapter mail, String comparator, String matchType, List headerNames, List keys, SieveContext context) throws SieveException {
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
// Iterate over the header names looking for a match
boolean isMatched = false;
Iterator<String> headerNamesIter = headerNames.iterator();
while (!isMatched && headerNamesIter.hasNext()) {
Set<String> values = zma.getMatchingHeaderFromAllParts(headerNamesIter.next());
isMatched = match(comparator, matchType, new ArrayList<String>(values), keys, context);
}
return isMatched;
}
use of com.zimbra.cs.filter.DummyMailAdapter in project zm-mailbox by Zimbra.
the class TwitterTest method executeBasic.
@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
ZimbraMailAdapter adapter = (ZimbraMailAdapter) mail;
ParsedAddress sender = adapter.getParsedMessage().getParsedSender();
if (!Strings.isNullOrEmpty(sender.emailPart)) {
String email = sender.emailPart.toLowerCase();
if (email.equals("notify@twitter.com")) {
return true;
}
}
return false;
}
use of com.zimbra.cs.filter.DummyMailAdapter 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(COUNT_TAG)) {
// 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 (COUNT_TAG.equals(matchType)) {
for (final String key : keys) {
isMatched = ZimbraComparatorUtils.counts(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;
}
isMatched = match(comparator, matchType, operator, (String) headerValuesIter.next(), normalizedKeys, context);
}
}
return isMatched;
}
Aggregations