use of com.zimbra.cs.filter.ZimbraMailAdapter 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 com.zimbra.cs.filter.ZimbraMailAdapter 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.ZimbraMailAdapter in project zm-mailbox by Zimbra.
the class DeleteHeader method executeBasic.
/** (non-Javadoc)
* @see org.apache.jsieve.commands.AbstractCommand#executeBasic(org.apache.jsieve.mail.MailAdapter, org.apache.jsieve.Arguments, org.apache.jsieve.Block, org.apache.jsieve.SieveContext)
*/
@SuppressWarnings("unchecked")
@Override
protected Object executeBasic(MailAdapter mail, Arguments args, Block block, SieveContext sieveContext) throws SieveException {
if (!(mail instanceof ZimbraMailAdapter)) {
ZimbraLog.filter.info("deleteheader: Zimbra mail adapter not found.");
return null;
}
// make sure zcs do not delete immutable header
if (ehe.isImmutableHeaderKey()) {
ZimbraLog.filter.info("deleteheader: %s is immutable header, so exiting silently.", ehe.getKey());
return null;
}
ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
// replace sieve variables
ehe.replaceVariablesInValueList(mailAdapter);
ehe.replaceVariablesInKey(mailAdapter);
FilterUtil.headerNameHasSpace(ehe.getKey());
MimeMessage mm = mailAdapter.getMimeMessage();
Enumeration<Header> headers;
try {
headers = mm.getAllHeaders();
if (!headers.hasMoreElements()) {
ZimbraLog.filter.info("deleteheader: No headers found in mime.");
return null;
}
} catch (MessagingException e) {
throw new OperationException("deleteheader: Error occured while fetching all headers from mime.", e);
}
List<String> matchingeHeaderList = ehe.getMatchingHeaders(mm);
int headerCount = matchingeHeaderList.size();
if (headerCount < 1) {
ZimbraLog.filter.info("deleteheader: No headers found matching with \"%s\" in mime.", ehe.getKey());
return null;
}
ehe.setEffectiveIndex(headerCount);
int matchIndex = 0;
Set<String> removedHeaders = new HashSet<String>();
try {
while (headers.hasMoreElements()) {
Header header = headers.nextElement();
boolean deleteCurrentHeader = false;
if (header.getName().equalsIgnoreCase(ehe.getKey())) {
matchIndex++;
if (ehe.getIndex() == null || (ehe.getIndex() != null && ehe.getIndex() == matchIndex)) {
if (ehe.getValueList() == null || ehe.getValueList().isEmpty()) {
deleteCurrentHeader = true;
} else {
for (String value : ehe.getValueList()) {
deleteCurrentHeader = ehe.matchCondition(mailAdapter, header, matchingeHeaderList, value, sieveContext);
if (deleteCurrentHeader) {
break;
}
}
}
}
}
if (!(removedHeaders.contains(header.getName()))) {
mm.removeHeader(header.getName());
removedHeaders.add(header.getName());
}
// if deleteCurrentHeader is true, don't add header to mime
if (!deleteCurrentHeader) {
mm.addHeaderLine(header.getName() + ": " + header.getValue());
}
}
mm.saveChanges();
mailAdapter.updateIncomingBlob();
} catch (MessagingException me) {
throw new OperationException("deleteheader: Error occured while operating mime.", me);
}
return null;
}
use of com.zimbra.cs.filter.ZimbraMailAdapter 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.ZimbraMailAdapter 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;
}
Aggregations