Search in sources :

Example 6 with ZimbraMailAdapter

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());
}
Also used : ZimbraSieveException(com.zimbra.cs.filter.ZimbraSieveException) TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) Calendar(java.util.Calendar) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) TimeZone(java.util.TimeZone) ServiceException(com.zimbra.common.service.ServiceException) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 7 with ZimbraMailAdapter

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);
}
Also used : TagArgument(org.apache.jsieve.TagArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ParseException(java.text.ParseException) StringListArgument(org.apache.jsieve.StringListArgument) Date(java.util.Date) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 8 with ZimbraMailAdapter

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;
}
Also used : Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException) HashSet(java.util.HashSet)

Example 9 with ZimbraMailAdapter

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;
}
Also used : Account(com.zimbra.cs.account.Account) InternetAddress(com.zimbra.common.mime.InternetAddress) Mailbox(com.zimbra.cs.mailbox.Mailbox) ServiceException(com.zimbra.common.service.ServiceException) ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 10 with ZimbraMailAdapter

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;
}
Also used : ArrayList(java.util.ArrayList) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)29 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)19 ServiceException (com.zimbra.common.service.ServiceException)9 ArrayList (java.util.ArrayList)8 MessagingException (javax.mail.MessagingException)8 Argument (org.apache.jsieve.Argument)8 StringListArgument (org.apache.jsieve.StringListArgument)8 TagArgument (org.apache.jsieve.TagArgument)8 SyntaxException (org.apache.jsieve.exception.SyntaxException)8 SieveException (org.apache.jsieve.exception.SieveException)5 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 ParsedAddress (com.zimbra.cs.mime.ParsedAddress)4 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)4 InternetAddress (com.zimbra.common.mime.InternetAddress)3 Account (com.zimbra.cs.account.Account)3 ZimbraComparatorUtils (com.zimbra.cs.filter.ZimbraComparatorUtils)3 ZimbraSieveException (com.zimbra.cs.filter.ZimbraSieveException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Message (com.zimbra.cs.mailbox.Message)2 IOException (java.io.IOException)2