Search in sources :

Example 11 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class ImapFolderSync method handleFetch.

private void handleFetch(MessageData md, Map<Long, MessageData> flagsByUid, int folderId, boolean storeSync, boolean restorePurged) throws ServiceException, IOException {
    long uid = md.getUid();
    if (uid == -1) {
        throw new MailException("Missing UID in FETCH response");
    }
    MessageData flagsData = flagsByUid.get(uid);
    remoteFolder.debug("Found new IMAP message with uid %d", uid);
    // Parse the message data
    Date date = flagsData.getInternalDate();
    Long receivedDate = date != null ? date.getTime() : null;
    int zflags = SyncUtil.imapToZimbraFlags(flagsData.getFlags());
    MessageContent mc = getContent(md);
    Message msg;
    try {
        ParsedMessage pm = mc.getParsedMessage(receivedDate, mailbox.attachmentsIndexingEnabled());
        if (pm == null) {
            remoteFolder.warn("Empty message body for UID %d. Must be ignored.", uid);
            return;
        }
        pm.setDataSourceId(ds.getId());
        if (restorePurged) {
            try {
                refetchPurgedMsgsInConversation(pm);
            } catch (ServiceException e) {
                // refetching error should not halt import
                ZimbraLog.datasource.error("error refetching purged message", e);
            }
        }
        msg = imapSync.addMessage(null, pm, mc.getSize(), folderId, zflags, mc.getDeliveryContext());
    } finally {
        mc.cleanup();
    }
    if (msg != null && msg.getFolderId() == folderId) {
        storeImapMessage(uid, msg.getId(), zflags, storeSync);
        stats.msgsAddedLocally++;
    } else {
        // Message was filtered and discarded or moved to another folder.
        // This can only happen for messages fetched from INBOX which is
        // always imported first. Mark remote message for deletion and
        // do not create a local tracker. If message was moved to another
        // folder we will append it to the remote folder when we sync
        // that folder.
        addDeletedUid(uid);
    }
}
Also used : MessageContent(com.zimbra.cs.datasource.MessageContent) PurgedMessage(com.zimbra.cs.db.DbDataSource.PurgedMessage) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) RemoteServiceException(com.zimbra.common.service.RemoteServiceException) MessageData(com.zimbra.cs.mailclient.imap.MessageData) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MailException(com.zimbra.cs.mailclient.MailException) Date(java.util.Date)

Example 12 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class ImapFolderSync method fetchMessages.

private void fetchMessages(String seq) throws ServiceException, IOException {
    final Map<Long, MessageData> flagsByUid = connection.uidFetch(seq, "(FLAGS INTERNALDATE)");
    removeDeleted(flagsByUid);
    final Set<Long> uidSet = flagsByUid.keySet();
    if (uidSet.isEmpty())
        return;
    FetchResponseHandler handler = new FetchResponseHandler() {

        @Override
        public void handleFetchResponse(MessageData md) throws Exception {
            long uid = md.getUid();
            IOExceptionHandler.getInstance().trackSyncItem(mailbox, uid);
            try {
                handleFetch(md, flagsByUid, true);
                clearError(uid);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("Out of memory", e);
            } catch (Exception e) {
                if (!IOExceptionHandler.getInstance().isRecoverable(mailbox, uid, "Exception syncing UID " + uid + " in folder " + remoteFolder.getPath(), e)) {
                    syncFailed("Fetch failed for uid " + uid, e);
                    SyncErrorManager.incrementErrorCount(ds, remoteId(uid));
                }
            }
            uidSet.remove(uid);
        }
    };
    // Try fetching group of messages first
    LOG.debug("Fetching messages for sequence: " + seq);
    try {
        connection.uidFetch(getSequence(uidSet), "BODY.PEEK[]", handler);
    } catch (CommandFailedException e) {
        String msg = "UID FETCH failed: " + e.toString();
        checkCanContinue(msg, e);
        LOG.warn(msg, e);
    }
    if (uidSet.isEmpty())
        return;
    LOG.info("Fetching remaining messages one at a time for UIDs: " + uidSet);
    for (long uid : getOrderedUids(uidSet)) {
        try {
            LOG.info("Fetching message for uid: " + uid);
            MessageData md = connection.uidFetch(uid, "BODY.PEEK[]");
            if (md == null) {
                //FLAGS returned data for UID but BODY.PEEK[] is not; server error; provide more meaningful error than NPE
                throw ServiceException.FAILURE("Server returned no response for UID FETCH " + uid + " BODY.PEEK[]", null);
            }
            handler.handleFetchResponse(md);
        } catch (Exception e) {
            String msg = "Error while fetching message for UID " + uid;
            checkCanContinue(msg, e);
            LOG.warn(msg, e);
        }
    }
    if (!uidSet.isEmpty()) {
        LOG.error("Unable to fetch messages for uids: " + uidSet);
    }
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) FetchResponseHandler(com.zimbra.cs.mailclient.imap.FetchResponseHandler) MailException(com.zimbra.cs.mailclient.MailException) ServiceException(com.zimbra.common.service.ServiceException) SQLException(java.sql.SQLException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) RemoteServiceException(com.zimbra.common.service.RemoteServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException)

Example 13 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class ImapAppender method findUids.

private List<Long> findUids(String seq, final MessageInfo mi) throws IOException, MessagingException {
    final List<Long> uids = new ArrayList<Long>(1);
    Map<Long, MessageData> mds = connection.uidFetch(seq, "(RFC822.SIZE ENVELOPE)");
    for (MessageData md : mds.values()) {
        if (matches(mi, md)) {
            uids.add(md.getUid());
        }
    }
    return uids;
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) ArrayList(java.util.ArrayList)

Example 14 with MessageData

use of com.zimbra.cs.mailclient.imap.MessageData in project zm-mailbox by Zimbra.

the class RemoteFolder method getFlags.

/*
     * Fetch message flags for specific UID sequence. Exclude messages which
     * have been flagged \Deleted.
     */
public List<MessageData> getFlags(long startUid, long endUid) throws IOException {
    final List<MessageData> mds = new ArrayList<MessageData>();
    String end = endUid > 0 ? String.valueOf(endUid) : "*";
    connection.uidFetch(startUid + ":" + end, "FLAGS", new FetchResponseHandler() {

        public void handleFetchResponse(MessageData md) {
            Flags flags = md.getFlags();
            if (flags != null && !flags.isDeleted()) {
                mds.add(md);
            }
        }
    });
    // result.
    if (endUid <= 0 && mds.size() == 1 && mds.get(0).getUid() < startUid) {
        return Collections.emptyList();
    }
    return mds;
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) ArrayList(java.util.ArrayList) FetchResponseHandler(com.zimbra.cs.mailclient.imap.FetchResponseHandler) Flags(com.zimbra.cs.mailclient.imap.Flags)

Aggregations

MessageData (com.zimbra.cs.mailclient.imap.MessageData)14 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)6 Test (org.junit.Test)6 ServiceException (com.zimbra.common.service.ServiceException)5 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)5 MailException (com.zimbra.cs.mailclient.MailException)5 Flags (com.zimbra.cs.mailclient.imap.Flags)5 IOException (java.io.IOException)5 ZMailbox (com.zimbra.client.ZMailbox)4 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)4 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 ZFolder (com.zimbra.client.ZFolder)3 ZTag (com.zimbra.client.ZTag)3 FetchResponseHandler (com.zimbra.cs.mailclient.imap.FetchResponseHandler)3 SQLException (java.sql.SQLException)3 PurgedMessage (com.zimbra.cs.db.DbDataSource.PurgedMessage)2 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)2 Body (com.zimbra.cs.mailclient.imap.Body)2 ImapResponse (com.zimbra.cs.mailclient.imap.ImapResponse)2 Literal (com.zimbra.cs.mailclient.imap.Literal)2