Search in sources :

Example 16 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class SocialcastTest 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;
    ParsedMessage pm = adapter.getParsedMessage();
    ParsedAddress sender = pm.getParsedSender();
    if (!Strings.isNullOrEmpty(sender.emailPart) && sender.emailPart.endsWith("@socialcast.com")) {
        try {
            if (pm.getMimeMessage().getHeader("Reply-To", null) != null) {
                // test if Reply-To exists
                return true;
            }
        } catch (MessagingException ignore) {
        }
    }
    return false;
}
Also used : MessagingException(javax.mail.MessagingException) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) ParsedAddress(com.zimbra.cs.mime.ParsedAddress)

Example 17 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class InviteTest method executeBasic.

/**
     * Returns <tt>true</tt> if the message has a calendar part.
     */
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) {
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ParsedMessage pm = ((ZimbraMailAdapter) mail).getParsedMessage();
    if (pm == null) {
        return false;
    }
    CalendarPartInfo calPart = pm.getCalendarPartInfo();
    if (calPart != null) {
        if (mMethods.isEmpty()) {
            // Test matches any invite.
            return true;
        }
        if (calPart.method == null) {
            // Method not specified in the calendar part.
            return false;
        }
        if (mMethods.contains(calPart.method.toString())) {
            // Calendar part method matches one of the methods in the test.
            return true;
        }
        if (mMethods.contains("anyrequest") && isRequest(calPart.method)) {
            return true;
        }
        if (mMethods.contains("anyreply") && isReply(calPart.method)) {
            return true;
        }
    }
    return false;
}
Also used : ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) CalendarPartInfo(com.zimbra.cs.mime.ParsedMessage.CalendarPartInfo) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 18 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class AddHeader method executeBasic.

@SuppressWarnings("unchecked")
@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    headerName = FilterUtil.replaceVariables(mailAdapter, headerName);
    FilterUtil.headerNameHasSpace(headerName);
    headerValue = FilterUtil.replaceVariables(mailAdapter, headerValue);
    try {
        headerValue = MimeUtility.fold(headerName.length() + 2, MimeUtility.encodeText(headerValue));
    } catch (UnsupportedEncodingException uee) {
        throw new OperationException("addheader: Error occured while encoding header value.", uee);
    }
    MimeMessage mm = mailAdapter.getMimeMessage();
    if (headerName != null && headerValue != null) {
        try {
            if (last) {
                mm.addHeaderLine(headerName + ": " + headerValue);
            } else {
                List<Header> headerList = new ArrayList<Header>();
                Enumeration<Header> e = mm.getAllHeaders();
                // keep it at the first line.
                if (e.hasMoreElements()) {
                    Header temp = e.nextElement();
                    if ("Return-Path".equalsIgnoreCase(temp.getName())) {
                        mm.removeHeader(temp.getName());
                        headerList.add(temp);
                    } else {
                        // reset the iterator (push back the first item of the headers list)
                        e = mm.getAllHeaders();
                    }
                }
                headerList.add(new Header(headerName, headerValue));
                while (e.hasMoreElements()) {
                    Header temp = e.nextElement();
                    mm.removeHeader(temp.getName());
                    headerList.add(temp);
                }
                for (Header header : headerList) {
                    mm.addHeaderLine(header.getName() + ": " + header.getValue());
                }
            }
            mm.saveChanges();
            mailAdapter.updateIncomingBlob();
            ZimbraLog.filter.info("New header is added in mime with name: %s and value: %s", headerName, headerValue);
        } catch (MessagingException e) {
            throw new OperationException("Error occured while adding new header in mime.", e);
        }
        return null;
    }
    return null;
}
Also used : Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException)

Example 19 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class HeaderTest method replaceVariables.

/**
     * @param keys
     * @param mail
     * @return
     * @throws SyntaxException 
     */
public static List<String> replaceVariables(List<String> keys, MailAdapter mail) throws SyntaxException {
    List<String> replacedVariables = new ArrayList<String>();
    if (!(mail instanceof ZimbraMailAdapter)) {
        return replacedVariables;
    }
    ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
    for (String key : keys) {
        String temp = FilterUtil.replaceVariables(zma, key);
        replacedVariables.add(temp);
    }
    return replacedVariables;
}
Also used : ArrayList(java.util.ArrayList) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Example 20 with ZimbraMailAdapter

use of com.zimbra.cs.filter.ZimbraMailAdapter in project zm-mailbox by Zimbra.

the class ReplaceHeader 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 arguments, Block block, SieveContext sieveContext) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        ZimbraLog.filter.info("replaceheader: Zimbra mail adapter not found.");
        return null;
    }
    // make sure zcs do not edit immutable header
    if (ehe.isImmutableHeaderKey()) {
        ZimbraLog.filter.info("replaceheader: %s is immutable header, so exiting silently.", ehe.getKey());
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    // replace sieve variables
    ehe.replaceVariablesInValueList(mailAdapter);
    ehe.replaceVariablesInKey(mailAdapter);
    if (ehe.getValueList() == null || ehe.getValueList().isEmpty()) {
        ehe.setValueList(Arrays.asList("*"));
    }
    FilterUtil.headerNameHasSpace(ehe.getKey());
    MimeMessage mm = mailAdapter.getMimeMessage();
    Enumeration<Header> headers;
    try {
        headers = mm.getAllHeaders();
        if (!headers.hasMoreElements()) {
            ZimbraLog.filter.info("replaceheader: No headers found in mime.");
            return null;
        }
    } catch (MessagingException e) {
        throw new OperationException("replaceheader: 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("replaceheader: No headers found matching with \"%s\" in mime.", ehe.getKey());
        return null;
    }
    ehe.setEffectiveIndex(headerCount);
    int matchIndex = 0;
    List<Header> newHeaderList = new ArrayList<Header>();
    try {
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            String newHeaderName = null;
            String newHeaderValue = null;
            boolean replace = false;
            if (header.getName().equalsIgnoreCase(ehe.getKey())) {
                matchIndex++;
                if (ehe.getIndex() == null || (ehe.getIndex() != null && ehe.getIndex() == matchIndex)) {
                    ZimbraLog.filter.debug("replaceheader: header before processing\n%d  %s: %s", matchIndex, header.getName(), header.getValue());
                    for (String value : ehe.getValueList()) {
                        ZimbraLog.filter.debug("replaceheader: working with %s value", value);
                        replace = ehe.matchCondition(mailAdapter, header, matchingeHeaderList, value, sieveContext);
                        if (replace) {
                            if (ehe.getNewName() != null) {
                                newHeaderName = FilterUtil.replaceVariables(mailAdapter, ehe.getNewName());
                                FilterUtil.headerNameHasSpace(newHeaderName);
                            } else {
                                newHeaderName = header.getName();
                            }
                            if (ehe.getNewValue() != null) {
                                newHeaderValue = FilterUtil.replaceVariables(mailAdapter, ehe.getNewValue());
                                newHeaderValue = MimeUtility.fold(newHeaderName.length() + 2, MimeUtility.encodeText(newHeaderValue));
                            } else {
                                newHeaderValue = header.getValue();
                            }
                            ZimbraLog.filter.debug("replaceheader: header after processing\n%s: %s", newHeaderName, newHeaderValue);
                            header = new Header(newHeaderName, newHeaderValue);
                            break;
                        }
                    }
                }
            }
            newHeaderList.add(header);
        }
        // now remove all headers from mime and add from new list
        headers = mm.getAllHeaders();
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            mm.removeHeader(header.getName());
        }
        for (Header header : newHeaderList) {
            mm.addHeaderLine(header.getName() + ": " + header.getValue());
        }
        mm.saveChanges();
        mailAdapter.updateIncomingBlob();
    } catch (MessagingException me) {
        throw new OperationException("replaceheader: Error occured while operating mime.", me);
    } catch (UnsupportedEncodingException uee) {
        throw new OperationException("replaceheader: Error occured while encoding header value.", uee);
    }
    return null;
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException)

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