Search in sources :

Example 1 with MessageData

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

the class TestImap method testAppendTags.

@Test
public void testAppendTags() throws Exception {
    Flags flags = Flags.fromSpec("afs");
    //new tag; does not exist in mbox
    String tag1 = "APPENDTAG1";
    flags.set(tag1);
    Date date = new Date(System.currentTimeMillis());
    Literal msg = message(10);
    try {
        AppendResult res = connection.append("INBOX", flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag1));
    } finally {
        msg.dispose();
    }
    //should not have created a visible tag
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue("APPEND created new visible tag", tags == null || tags.size() == 0);
    //now create a visible tag, add it to a message in inbox then try append to message in different folder
    String tag2 = "APPENDTAG2";
    ZTag tag = mbox.getTag(tag2);
    if (tag == null) {
        tag = mbox.createTag(tag2, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals(tag2, tags.get(0).getName());
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tag2, info.getFlags().isSet(tag2));
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    info = connection.select(folderName);
    assertFalse("new tag unexpectedly set in new folder", info.getFlags().isSet(tag2));
    msg = message(10);
    flags = Flags.fromSpec("afs");
    flags.set(tag2);
    try {
        AppendResult res = connection.append(folderName, flags, date, msg);
        MessageData data = connection.uidFetch(res.getUid(), "FLAGS");
        assertTrue(data.getFlags().isSet(tag2));
    } finally {
        msg.dispose();
    }
    info = connection.select(folderName);
    assertTrue("new tag not set in new folder", info.getFlags().isSet(tag2));
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) MessageData(com.zimbra.cs.mailclient.imap.MessageData) Literal(com.zimbra.cs.mailclient.imap.Literal) AppendResult(com.zimbra.cs.mailclient.imap.AppendResult) ZTag(com.zimbra.client.ZTag) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) ZFolder(com.zimbra.client.ZFolder) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) Test(org.junit.Test)

Example 2 with MessageData

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

the class TestImap method testStoreTagsDirty.

@Test
public void testStoreTagsDirty() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER);
    List<ZTag> tags = mbox.getAllTags();
    assertTrue(tags == null || tags.size() == 0);
    String tagName = "T1";
    final String tagName2 = "T2";
    ZTag tag = mbox.getTag(tagName);
    if (tag == null) {
        tag = mbox.createTag(tagName, Color.blue);
    }
    tags = mbox.getAllTags();
    assertTrue(tags != null && tags.size() == 1);
    assertEquals("T1", tags.get(0).getName());
    String folderName = "newfolder1";
    ZFolder folder = mbox.createFolder(Mailbox.ID_FOLDER_USER_ROOT + "", folderName, ZFolder.View.message, ZFolder.Color.DEFAULTCOLOR, null, null);
    mbox.addMessage(Mailbox.ID_FOLDER_INBOX + "", "u", tag.getId(), System.currentTimeMillis(), simpleMessage("foo1"), true);
    MailboxInfo info = connection.select("INBOX");
    assertTrue("INBOX does not contain expected flag " + tagName, info.getFlags().isSet(tagName));
    assertFalse("INBOX contain unexpected flag " + tagName2, info.getFlags().isSet(tagName2));
    Map<Long, MessageData> data = connection.fetch("1:*", "FLAGS");
    assertEquals(1, data.size());
    Iterator<Long> it = data.keySet().iterator();
    Long seq = it.next();
    assertTrue("flag not set on first message", data.get(seq).getFlags().isSet(tagName));
    ImapRequest req = connection.newRequest("STORE", seq + "", "+FLAGS", tagName2);
    req.setResponseHandler(new ResponseHandler() {

        @Override
        public void handleResponse(ImapResponse res) throws Exception {
            if (res.isUntagged() && res.getCCode() == CAtom.FLAGS) {
                Flags flags = (Flags) res.getData();
                assertTrue(flags.isSet(tagName2));
            }
        }
    });
    req.sendCheckStatus();
}
Also used : ResponseHandler(com.zimbra.cs.mailclient.imap.ResponseHandler) MessageData(com.zimbra.cs.mailclient.imap.MessageData) ImapRequest(com.zimbra.cs.mailclient.imap.ImapRequest) ZTag(com.zimbra.client.ZTag) ImapResponse(com.zimbra.cs.mailclient.imap.ImapResponse) Flags(com.zimbra.cs.mailclient.imap.Flags) ServiceException(com.zimbra.common.service.ServiceException) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZMailbox(com.zimbra.client.ZMailbox) ZFolder(com.zimbra.client.ZFolder) MailboxInfo(com.zimbra.cs.mailclient.imap.MailboxInfo) Test(org.junit.Test)

Example 3 with MessageData

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

the class ImapFolderSync method refetchPurgedMsgsInConversation.

protected void refetchPurgedMsgsInConversation(ParsedMessage pm) throws ServiceException, IOException {
    if (!mailbox.getAccount().isFeatureDataSourcePurgingEnabled()) {
        return;
    }
    Threader threader = pm.getThreader(ds.getMailbox());
    List<PurgedConversation> purgedConvs = threader.lookupPurgedConversations(ds);
    if (purgedConvs.size() == 0) {
        return;
    } else {
        for (PurgedConversation conv : purgedConvs) {
            ImapConnection conn;
            try {
                conn = getRefetchConnection();
            } catch (ServiceException e) {
                ZimbraLog.datasource.warn("could not establish IMAP connection to refetch purged messages");
                return;
            }
            for (PurgedMessage msg : conv.getMessages()) {
                String remoteFolderId = msg.getRemoteFolder();
                String msgUid = msg.getUid();
                final Integer folderId = msg.getLocalFolderId();
                ZimbraLog.datasource.info("restoring message " + msgUid + " in remote folder " + remoteFolderId);
                conn.select(remoteFolderId);
                final Map<Long, MessageData> msgFlags = conn.uidFetch(msgUid, "(FLAGS INTERNALDATE)");
                FetchResponseHandler handler = new FetchResponseHandler() {

                    @Override
                    public void handleFetchResponse(MessageData md) throws Exception {
                        long uid = md.getUid();
                        IOExceptionHandler.getInstance().trackSyncItem(mailbox, uid);
                        try {
                            handleFetch(md, msgFlags, folderId, false, false);
                            clearError(uid);
                        } catch (OutOfMemoryError e) {
                            Zimbra.halt("Out of memory", e);
                        } catch (Exception e) {
                            if (!IOExceptionHandler.getInstance().isRecoverable(mailbox, uid, "Exception re-fetching UID " + uid, e)) {
                                syncFailed("re-fetch failed for uid " + uid, e);
                                SyncErrorManager.incrementErrorCount(ds, remoteId(uid));
                            }
                        }
                    }
                };
                conn.uidFetch(msgUid, "BODY.PEEK[]", handler);
            }
            conv.unpurge();
        }
    }
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) Threader(com.zimbra.cs.mailbox.Threader) ImapConnection(com.zimbra.cs.mailclient.imap.ImapConnection) 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) PurgedConversation(com.zimbra.cs.db.DbDataSource.PurgedConversation) PurgedMessage(com.zimbra.cs.db.DbDataSource.PurgedMessage) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) RemoteServiceException(com.zimbra.common.service.RemoteServiceException) FetchResponseHandler(com.zimbra.cs.mailclient.imap.FetchResponseHandler)

Example 4 with MessageData

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

the class ImapFolderSync method removeDeleted.

// Discard messages that have been flagged \Deleted
private void removeDeleted(Map<Long, MessageData> mds) {
    Iterator<MessageData> it = mds.values().iterator();
    while (it.hasNext()) {
        MessageData md = it.next();
        Flags flags = md.getFlags();
        if (flags != null && flags.isDeleted()) {
            remoteFolder.debug("Remote message with uid %d is flagged \\Deleted", md.getUid());
            it.remove();
        }
    }
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) Flags(com.zimbra.cs.mailclient.imap.Flags)

Example 5 with MessageData

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

the class ImapFolderSync method fetchFlags.

private void fetchFlags(String seq) throws ServiceException, IOException {
    Map<Long, MessageData> mds = connection.uidFetch(seq, "FLAGS");
    // Remove messages that have been flagged \Deleted
    removeDeleted(mds);
    for (MessageData md : mds.values()) {
        long uid = md.getUid();
        ImapMessage trackedMsg = trackedMsgs.getByUid(uid);
        if (trackedMsg != null) {
            int msgId = trackedMsg.getItemId();
            if (localMsgIds.contains(msgId)) {
                localMsgIds.remove(msgId);
                try {
                    updateFlags(trackedMsg, md.getFlags());
                    clearError(msgId);
                } catch (MailServiceException.NoSuchItemException e) {
                    // Message was deleted locally
                    addDeletedUid(uid);
                    clearError(msgId);
                } catch (Exception e) {
                    syncMessageFailed(msgId, "Unable to update message flags", e);
                }
            } else {
                addDeletedUid(uid);
                clearError(msgId);
            }
        } else {
            remoteFolder.debug("Adding new message with UID %d detected while syncing flags", uid);
            addedUids.add(uid);
        }
    }
}
Also used : MessageData(com.zimbra.cs.mailclient.imap.MessageData) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) 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)

Aggregations

MessageData (com.zimbra.cs.mailclient.imap.MessageData)47 Test (org.junit.Test)33 ZMailbox (com.zimbra.client.ZMailbox)28 ZFolder (com.zimbra.client.ZFolder)19 Flags (com.zimbra.cs.mailclient.imap.Flags)16 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)14 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)11 ZTag (com.zimbra.client.ZTag)10 Body (com.zimbra.cs.mailclient.imap.Body)9 BodyStructure (com.zimbra.cs.mailclient.imap.BodyStructure)8 Envelope (com.zimbra.cs.mailclient.imap.Envelope)8 Literal (com.zimbra.cs.mailclient.imap.Literal)8 IOException (java.io.IOException)8 Date (java.util.Date)8 ServiceException (com.zimbra.common.service.ServiceException)7 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)6 MailException (com.zimbra.cs.mailclient.MailException)5 RemoteServiceException (com.zimbra.common.service.RemoteServiceException)4 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)4 Atom (com.zimbra.cs.mailclient.imap.Atom)4