Search in sources :

Example 86 with Message

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

the class UrlNamespace method getResourceFromMailItem.

/* Returns DavResource for the MailItem. */
public static DavResource getResourceFromMailItem(DavContext ctxt, MailItem item) throws DavException {
    DavResource resource = null;
    if (item == null) {
        return resource;
    }
    MailItem.Type itemType = item.getType();
    try {
        MailItem.Type viewType;
        switch(itemType) {
            case MOUNTPOINT:
                Mountpoint mp = (Mountpoint) item;
                viewType = mp.getDefaultView();
                // don't expose mounted calendars when using iCal style delegation model.
                if (!ctxt.useIcalDelegation() && (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK)) {
                    resource = new RemoteCalendarCollection(ctxt, mp);
                } else if (viewType == MailItem.Type.CONTACT) {
                    resource = new RemoteAddressbookCollection(ctxt, mp);
                } else {
                    resource = new RemoteCollection(ctxt, mp);
                }
                break;
            case FOLDER:
                Folder f = (Folder) item;
                viewType = f.getDefaultView();
                if (f.getId() == Mailbox.ID_FOLDER_INBOX && DavResource.isSchedulingEnabled()) {
                    resource = new ScheduleInbox(ctxt, f);
                } else if (f.getId() == Mailbox.ID_FOLDER_SENT && DavResource.isSchedulingEnabled()) {
                    resource = new ScheduleOutbox(ctxt, f);
                } else if (viewType == MailItem.Type.APPOINTMENT || viewType == MailItem.Type.TASK) {
                    resource = getCalendarCollection(ctxt, f);
                } else if (viewType == MailItem.Type.CONTACT) {
                    resource = new AddressbookCollection(ctxt, f);
                } else {
                    resource = new Collection(ctxt, f);
                }
                break;
            case DOCUMENT:
                resource = new Notebook(ctxt, (Document) item);
                break;
            case APPOINTMENT:
            case TASK:
                resource = new CalendarObject.LocalCalendarObject(ctxt, (CalendarItem) item);
                break;
            case MESSAGE:
                resource = getCalendarItemForMessage(ctxt, (Message) item);
                break;
            case CONTACT:
                resource = new AddressObject(ctxt, (Contact) item);
                break;
            default:
                break;
        }
    } catch (ServiceException e) {
        ZimbraLog.dav.info("cannot create DavResource", e);
    }
    return resource;
}
Also used : Message(com.zimbra.cs.mailbox.Message) Folder(com.zimbra.cs.mailbox.Folder) Document(com.zimbra.cs.mailbox.Document) Contact(com.zimbra.cs.mailbox.Contact) CalendarItem(com.zimbra.cs.mailbox.CalendarItem) MailItem(com.zimbra.cs.mailbox.MailItem) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 87 with Message

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

the class FilterUtil method addMessage.

/**
 * Adds a message to the given folder.  Handles both local folders and mountpoints.
 * @return the id of the new message, or <tt>null</tt> if it was a duplicate
 */
public static ItemId addMessage(DeliveryContext context, Mailbox mbox, ParsedMessage pm, String recipient, String folderPath, boolean noICal, int flags, String[] tags, int convId, OperationContext octxt) throws ServiceException {
    // Do initial lookup.
    Pair<Folder, String> folderAndPath = mbox.getFolderByPathLongestMatch(octxt, Mailbox.ID_FOLDER_USER_ROOT, folderPath);
    Folder folder = folderAndPath.getFirst();
    String remainingPath = folderAndPath.getSecond();
    ZimbraLog.filter.debug("Attempting to file to %s, remainingPath=%s.", folder, remainingPath);
    if (folder instanceof Mountpoint) {
        Mountpoint mountpoint = (Mountpoint) folder;
        ZMailbox remoteMbox = getRemoteZMailbox(mbox, mountpoint);
        // Look up remote folder.
        String remoteAccountId = mountpoint.getOwnerId();
        ItemId id = mountpoint.getTarget();
        ZFolder remoteFolder = remoteMbox.getFolderById(id.toString());
        if (remoteFolder != null) {
            if (remainingPath != null) {
                remoteFolder = remoteFolder.getSubFolderByPath(remainingPath);
                if (remoteFolder == null) {
                    String msg = String.format("Subfolder %s of mountpoint %s does not exist.", remainingPath, mountpoint.getName());
                    throw ServiceException.FAILURE(msg, null);
                }
            }
        }
        // File to remote folder.
        if (remoteFolder != null) {
            byte[] content;
            try {
                content = pm.getRawData();
            } catch (Exception e) {
                throw ServiceException.FAILURE("Unable to get message content", e);
            }
            String msgId = remoteMbox.addMessage(remoteFolder.getId(), com.zimbra.cs.mailbox.Flag.toString(flags), null, 0, content, false);
            return new ItemId(msgId, remoteAccountId);
        } else {
            String msg = String.format("Unable to find remote folder %s for mountpoint %s.", remainingPath, mountpoint.getName());
            throw ServiceException.FAILURE(msg, null);
        }
    } else {
        if (!StringUtil.isNullOrEmpty(remainingPath)) {
            // Only part of the folder path matched.  Auto-create the remaining path.
            ZimbraLog.filter.info("Could not find folder %s.  Automatically creating it.", folderPath);
            folder = mbox.createFolder(octxt, folderPath, new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
        }
        try {
            DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(noICal);
            dopt.setFlags(flags).setTags(tags).setConversationId(convId).setRecipientEmail(recipient);
            Message msg = mbox.addMessage(octxt, pm, dopt, context);
            if (msg == null) {
                return null;
            } else {
                return new ItemId(msg);
            }
        } catch (IOException e) {
            throw ServiceException.FAILURE("Unable to add message", e);
        }
    }
}
Also used : ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Message(com.zimbra.cs.mailbox.Message) SMTPMessage(com.sun.mail.smtp.SMTPMessage) MimeMessage(javax.mail.internet.MimeMessage) IOException(java.io.IOException) Folder(com.zimbra.cs.mailbox.Folder) ZFolder(com.zimbra.client.ZFolder) ItemId(com.zimbra.cs.service.util.ItemId) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) IOException(java.io.IOException) SyntaxException(org.apache.jsieve.exception.SyntaxException) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions)

Example 88 with Message

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

the class RuleManager method applyRulesToOutgoingMessage.

public static List<ItemId> applyRulesToOutgoingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int sentFolderId, boolean noICal, int flags, String[] tags, int convId) throws ServiceException {
    List<ItemId> addedMessageIds = null;
    OutgoingMessageHandler handler = new OutgoingMessageHandler(mailbox, pm, sentFolderId, noICal, flags, tags, convId, octxt);
    ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
    String[] filters = { ADMIN_OUTGOING_FILTER_RULES_BEFORE_CACHE_KEY, OUTGOING_FILTER_RULES_CACHE_KEY, ADMIN_OUTGOING_FILTER_RULES_AFTER_CACHE_KEY };
    try {
        Account account = mailbox.getAccount();
        for (String filter : filters) {
            Node node = getRulesNode(account, filter);
            if (null != node) {
                if (filter.equals(OUTGOING_FILTER_RULES_CACHE_KEY)) {
                    mailAdapter.setUserScriptExecuting(true);
                }
                boolean proceed = evaluateScript(mailAdapter, node);
                if (!proceed) {
                    continue;
                }
                if (mailAdapter.isStop()) {
                    break;
                }
                mailAdapter.resetValues();
                mailAdapter.resetCapabilities();
            }
        }
        mailAdapter.executeAllActions();
        // multiple fileinto may result in multiple copies of the messages in different folders
        addedMessageIds = mailAdapter.getAddedMessageIds();
    } catch (Exception e) {
        ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
    } catch (TokenMgrError e) {
        ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
    }
    if (addedMessageIds == null) {
        // Filter rules were not processed.  File to the default folder.
        Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
        addedMessageIds = new ArrayList<ItemId>(1);
        addedMessageIds.add(new ItemId(msg));
    }
    return addedMessageIds;
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ItemId(com.zimbra.cs.service.util.ItemId) ServiceException(com.zimbra.common.service.ServiceException) ParseException(org.apache.jsieve.parser.generated.ParseException) SieveException(org.apache.jsieve.exception.SieveException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 89 with Message

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

the class RuleManager method applyRulesToExistingMessage.

public static boolean applyRulesToExistingMessage(OperationContext octxt, Mailbox mbox, int messageId, Node node) throws ServiceException {
    Message msg = mbox.getMessageById(octxt, messageId);
    ExistingMessageHandler handler = new ExistingMessageHandler(octxt, mbox, messageId, (int) msg.getSize());
    ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mbox, handler);
    try {
        SIEVE_FACTORY.evaluate(mailAdapter, node);
        mailAdapter.executeAllActions();
    } catch (SieveException e) {
        throw ServiceException.FAILURE("Unable to evaluate script", e);
    }
    return handler.filtered();
}
Also used : SieveException(org.apache.jsieve.exception.SieveException) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage)

Example 90 with Message

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

the class RuleManager method applyRulesToIncomingMessage.

/**
 * Adds a message to a mailbox.  If filter rules exist, processes
 * the filter rules.  Otherwise, files to <tt>Inbox</tt> or <tt>Spam</tt>.
 *
 * @param allowFilterToMountpoint if <tt>false</tt>, rules
 * @return the list of message id's that were added, or an empty list.
 */
public static List<ItemId> applyRulesToIncomingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int size, String recipient, LmtpEnvelope env, DeliveryContext sharedDeliveryCtxt, int incomingFolderId, boolean noICal, boolean allowFilterToMountpoint) throws ServiceException {
    List<ItemId> addedMessageIds = null;
    IncomingMessageHandler handler = new IncomingMessageHandler(octxt, sharedDeliveryCtxt, mailbox, recipient, pm, size, incomingFolderId, noICal);
    ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
    mailAdapter.setEnvelope(env);
    mailAdapter.setAllowFilterToMountpoint(allowFilterToMountpoint);
    String[] filters = { ADMIN_FILTER_RULES_BEFORE_CACHE_KEY, FILTER_RULES_CACHE_KEY, ADMIN_FILTER_RULES_AFTER_CACHE_KEY };
    try {
        boolean applyRules = true;
        Account account = mailbox.getAccount();
        for (String filter : filters) {
            // Determine whether to apply rules
            Node node = getRulesNode(account, filter);
            if (null == node) {
                applyRules = false;
            }
            if (SpamHandler.isSpam(handler.getMimeMessage()) && !account.getBooleanAttr(Provisioning.A_zimbraSpamApplyUserFilters, false)) {
                // Don't apply user filters to spam by default
                applyRules = false;
                break;
            }
            if (applyRules) {
                if (filter.equals(FILTER_RULES_CACHE_KEY)) {
                    mailAdapter.setUserScriptExecuting(true);
                }
                boolean proceed = evaluateScript(mailAdapter, node);
                if (!proceed) {
                    continue;
                }
                if (mailAdapter.isStop()) {
                    break;
                }
                mailAdapter.resetValues();
                mailAdapter.resetCapabilities();
            }
        }
        if (applyRules) {
            mailAdapter.executeAllActions();
            addedMessageIds = mailAdapter.getAddedMessageIds();
        }
    } catch (ErejectException ex) {
        throw DeliveryServiceException.DELIVERY_REJECTED(ex.getMessage(), ex);
    } catch (Exception e) {
        ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
    } catch (TokenMgrError e) {
        // Workaround for bug 19576.  Certain parse errors can cause JavaCC to throw an Error
        // instead of an Exception.  Woo.
        ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
    }
    if (addedMessageIds == null) {
        // Filter rules were not processed.  File to the default folder.
        Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
        addedMessageIds = new ArrayList<ItemId>(1);
        addedMessageIds.add(new ItemId(msg));
    }
    return addedMessageIds;
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ItemId(com.zimbra.cs.service.util.ItemId) ServiceException(com.zimbra.common.service.ServiceException) ParseException(org.apache.jsieve.parser.generated.ParseException) SieveException(org.apache.jsieve.exception.SieveException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException)

Aggregations

Message (com.zimbra.cs.mailbox.Message)442 Mailbox (com.zimbra.cs.mailbox.Mailbox)375 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)358 Test (org.junit.Test)335 Account (com.zimbra.cs.account.Account)315 OperationContext (com.zimbra.cs.mailbox.OperationContext)302 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)281 ItemId (com.zimbra.cs.service.util.ItemId)211 MimeMessage (javax.mail.internet.MimeMessage)123 SyntaxException (org.apache.jsieve.exception.SyntaxException)87 Header (javax.mail.Header)83 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)63 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)53 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)46 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)43 ServiceException (com.zimbra.common.service.ServiceException)41 Element (com.zimbra.common.soap.Element)41 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)36 Folder (com.zimbra.cs.mailbox.Folder)32 ZMailbox (com.zimbra.client.ZMailbox)26