Search in sources :

Example 1 with Flags

use of com.zimbra.cs.mailclient.imap.Flags 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 Flags

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

the class TestImap method appendInvalidFlag.

private void appendInvalidFlag(String flag) throws IOException {
    Literal msg = message(10);
    Flags flags = Flags.fromSpec("afs");
    flags.set(flag);
    Date date = new Date(System.currentTimeMillis());
    try {
        AppendResult res = connection.append("INBOX", flags, date, msg);
        fail("server allowed client to set system flag " + flag);
    } catch (CommandFailedException e) {
    //expected
    } finally {
        msg.dispose();
    }
    //do a no-op so we don't hit max consecutive error limit
    connection.noop();
}
Also used : Literal(com.zimbra.cs.mailclient.imap.Literal) AppendResult(com.zimbra.cs.mailclient.imap.AppendResult) Flags(com.zimbra.cs.mailclient.imap.Flags) Date(java.sql.Date) CommandFailedException(com.zimbra.cs.mailclient.CommandFailedException)

Example 3 with Flags

use of com.zimbra.cs.mailclient.imap.Flags 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 4 with Flags

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

the class ImapFolderSync method updateFlags.

// Updates flags for specified message.
private void updateFlags(ImapMessage msg, Flags flags) throws ServiceException, IOException {
    int id = msg.getItemId();
    int localFlags = msg.getItemFlags();
    int trackedFlags = msg.getFlags();
    int remoteFlags = SyncUtil.imapToZimbraFlags(flags);
    int newLocalFlags = mergeFlags(localFlags, trackedFlags, remoteFlags);
    int newRemoteFlags = SyncUtil.imapFlagsOnly(newLocalFlags);
    if (LOG.isDebugEnabled() && (newLocalFlags != localFlags || newRemoteFlags != remoteFlags || newRemoteFlags != trackedFlags)) {
        localFolder.debug("Updating flags for message with item id %d: " + "local=%s, tracked=%s, remote=%s, new_local=%s, new_remote=%s", id, Flag.toString(localFlags), Flag.toString(trackedFlags), Flag.toString(remoteFlags), Flag.toString(newLocalFlags), Flag.toString(newRemoteFlags));
    }
    if (newLocalFlags != localFlags) {
        localFolder.setMessageFlags(id, newLocalFlags);
        stats.flagsUpdatedLocally++;
    }
    if (newRemoteFlags != remoteFlags) {
        String uids = String.valueOf(msg.getUid());
        Flags toAdd = SyncUtil.getFlagsToAdd(flags, newRemoteFlags);
        Flags toRemove = SyncUtil.getFlagsToRemove(flags, newRemoteFlags);
        // UID STORE never fails even if message was deleted
        if (!toAdd.isEmpty()) {
            connection.uidStore(uids, "+FLAGS.SILENT", toAdd);
        }
        if (!toRemove.isEmpty()) {
            connection.uidStore(uids, "-FLAGS.SILENT", toRemove);
        }
        stats.flagsUpdatedRemotely++;
    }
    if (newRemoteFlags != trackedFlags) {
        msg.setFlags(newRemoteFlags);
        msg.update();
        stats.flagsUpdatedLocally++;
    }
}
Also used : Flags(com.zimbra.cs.mailclient.imap.Flags)

Example 5 with Flags

use of com.zimbra.cs.mailclient.imap.Flags 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)

Aggregations

Flags (com.zimbra.cs.mailclient.imap.Flags)44 Test (org.junit.Test)35 Literal (com.zimbra.cs.mailclient.imap.Literal)26 CommandFailedException (com.zimbra.cs.mailclient.CommandFailedException)21 MessageData (com.zimbra.cs.mailclient.imap.MessageData)16 Date (java.sql.Date)14 Date (java.util.Date)14 ZMailbox (com.zimbra.client.ZMailbox)12 MailboxInfo (com.zimbra.cs.mailclient.imap.MailboxInfo)11 ZTag (com.zimbra.client.ZTag)10 ZFolder (com.zimbra.client.ZFolder)8 ServiceException (com.zimbra.common.service.ServiceException)7 AppendResult (com.zimbra.cs.mailclient.imap.AppendResult)7 ImapResponse (com.zimbra.cs.mailclient.imap.ImapResponse)7 IOException (java.io.IOException)7 Atom (com.zimbra.cs.mailclient.imap.Atom)6 ImapRequest (com.zimbra.cs.mailclient.imap.ImapRequest)6 ResponseHandler (com.zimbra.cs.mailclient.imap.ResponseHandler)5 MessagingException (javax.mail.MessagingException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2