use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class AppendMessage method store.
private int store(Mailbox mbox, Folder folder) throws ImapSessionClosedException, ServiceException, IOException {
boolean idxAttach = mbox.attachmentsIndexingEnabled();
Long receivedDate = date != null ? date.getTime() : null;
ParsedMessage pm = new ParsedMessage(content, receivedDate, idxAttach);
try {
if (!pm.getSender().isEmpty()) {
InternetAddress ia = new JavaMailInternetAddress(pm.getSender());
if (AccountUtil.addressMatchesAccountOrSendAs(mbox.getAccount(), ia.getAddress())) {
flags |= Flag.BITMASK_FROM_ME;
}
}
} catch (Exception e) {
}
DeliveryOptions dopt = new DeliveryOptions().setFolderId(folder).setNoICal(true).setFlags(flags).setTags(tags);
Message msg = mbox.addMessage(handler.getContext(), pm, dopt, null);
if (msg != null && sflags != 0 && handler.getState() == ImapHandler.State.SELECTED) {
ImapFolder selectedFolder = handler.getSelectedFolder();
// (note that this leaves session flags unset on remote appended messages)
if (selectedFolder != null) {
ImapMessage i4msg = selectedFolder.getById(msg.getId());
if (i4msg != null) {
i4msg.setSessionFlags(sflags, selectedFolder);
}
}
}
return msg == null ? -1 : msg.getId();
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class ImapSession method handleModify.
private void handleModify(int changeId, Change chg, AddedItems added) {
if (chg.what instanceof Tag && (chg.why & Change.NAME) != 0) {
mFolder.handleTagRename(changeId, (Tag) chg.what, chg);
} else if (chg.what instanceof Folder && ((Folder) chg.what).getId() == mFolderId) {
Folder folder = (Folder) chg.what;
if ((chg.why & Change.FLAGS) != 0 && (folder.getFlagBitmask() & Flag.BITMASK_DELETED) != 0) {
// mailbox accessed by sending a untagged BYE response."
if (handler != null) {
handler.close();
}
} else if ((chg.why & (Change.FOLDER | Change.NAME)) != 0) {
mFolder.handleFolderRename(changeId, folder, chg);
}
} else if (chg.what instanceof Message || chg.what instanceof Contact) {
MailItem item = (MailItem) chg.what;
boolean inFolder = mIsVirtual || item.getFolderId() == mFolderId;
if (!inFolder && (chg.why & Change.FOLDER) == 0) {
return;
}
mFolder.handleItemUpdate(changeId, chg, added);
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class ImapHandlerTest method testDoSearch.
@Test
public void testDoSearch() throws Exception {
Account acct = Provisioning.getInstance().getAccount("12aa345b-2b47-44e6-8cb8-7fdfa18c1a9f");
acct.setFeatureAntispamEnabled(true);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
Message m1 = TestUtil.addMessage(mbox, "Message 1 blue");
Message m2 = TestUtil.addMessage(mbox, "Message 2 green red");
Message m3 = TestUtil.addMessage(mbox, "Message 3 green white");
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m1.getId()).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m2.getId()).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m3.getId()).getFolderId());
Thread.sleep(500);
ImapHandler handler = new MockImapHandler();
ImapCredentials creds = new ImapCredentials(acct, ImapCredentials.EnabledHack.NONE);
ImapPath pathInbox = new MockImapPath(null, mbox.getFolderById(null, Mailbox.ID_FOLDER_INBOX), creds);
handler.setCredentials(creds);
byte params = 0;
handler.setSelectedFolder(pathInbox, params);
Integer options = null;
boolean byUID = false;
ImapSearch.LogicalOperation i4srch = new ImapSearch.AndOperation();
ImapSearch child = new ImapSearch.AndOperation(new ImapSearch.FlagSearch("\\Recent"), new ImapSearch.NotOperation(new ImapSearch.FlagSearch("\\Seen")));
i4srch.addChild(child);
i4srch.addChild(new ImapSearch.ContentSearch("green"));
Assert.assertTrue(handler.doSEARCH("searchtag", i4srch, byUID, options));
ByteArrayOutputStream baos = (ByteArrayOutputStream) handler.output;
Assert.assertEquals("Output of SEARCH", "* SEARCH 2 3\r\nsearchtag OK SEARCH completed\r\n", baos.toString());
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class ImapHandlerTest method testDoCOPYByUID.
@Test
public void testDoCOPYByUID() {
try {
Account acct = Provisioning.getInstance().getAccount("12aa345b-2b47-44e6-8cb8-7fdfa18c1a9f");
acct.setFeatureAntispamEnabled(true);
Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(acct);
Message m1 = TestUtil.addMessage(mbox, "Message 1");
Message m2 = TestUtil.addMessage(mbox, "Message 2");
Message m3 = TestUtil.addMessage(mbox, "Message 3");
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m1.getId()).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m2.getId()).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m3.getId()).getFolderId());
ImapHandler handler = new MockImapHandler();
ImapCredentials creds = new ImapCredentials(acct, ImapCredentials.EnabledHack.NONE);
ImapPath pathSpam = new MockImapPath(null, mbox.getFolderById(null, Mailbox.ID_FOLDER_SPAM), creds);
ImapPath pathInbox = new MockImapPath(null, mbox.getFolderById(null, Mailbox.ID_FOLDER_INBOX), creds);
handler.setCredentials(creds);
byte params = 0;
handler.setSelectedFolder(pathSpam, params);
String sequenceSet = String.format("%d,%d,%d", m1.getId(), m2.getId(), m3.getId());
Assert.assertTrue(handler.doCOPY(null, sequenceSet, pathInbox, true));
List<Integer> newIds = TestUtil.search(mbox, "in:Inbox", MailItem.Type.MESSAGE);
assertEquals(3, newIds.size());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, newIds.get(0)).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, newIds.get(1)).getFolderId());
Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, newIds.get(2)).getFolderId());
/* Note, messages may not be returned in the original order */
Assert.assertTrue(String.format("message IDs should not have changed 1st ID=%s newIds=%s", m1.getId(), newIds), newIds.contains(m1.getId()));
Assert.assertTrue(String.format("message IDs should not have changed 2nd ID=%s newIds=%s", m2.getId(), newIds), newIds.contains(m2.getId()));
Assert.assertTrue(String.format("message IDs should not have changed 3rd ID=%s newIds=%s", m3.getId(), newIds), newIds.contains(m3.getId()));
handler.setSelectedFolder(pathInbox, params);
ImapFolder i4folder = handler.getSelectedFolder();
Assert.assertEquals(3, i4folder.getSize());
Assert.assertTrue(handler.doCOPY(null, sequenceSet, pathSpam, true));
newIds = TestUtil.search(mbox, "in:junk", MailItem.Type.MESSAGE);
assertEquals(3, newIds.size());
Assert.assertFalse("Message IDs should have changed", newIds.contains(m1.getId()));
Assert.assertFalse("Message IDs should have changed", newIds.contains(m3.getId()));
Assert.assertFalse("Message IDs should have changed", newIds.contains(m3.getId()));
Assert.assertEquals("Message should have been copied to Junk", Mailbox.ID_FOLDER_SPAM, mbox.getMessageById(null, newIds.get(0)).getFolderId());
Assert.assertEquals("Message should have been copied to Junk", Mailbox.ID_FOLDER_SPAM, mbox.getMessageById(null, newIds.get(1)).getFolderId());
Assert.assertEquals("Message should have been copied to Junk", Mailbox.ID_FOLDER_SPAM, mbox.getMessageById(null, newIds.get(2)).getFolderId());
Assert.assertEquals("original messages should have stayed in inbox", Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m1.getId()).getFolderId());
Assert.assertEquals("original messages should have stayed in inbox", Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m2.getId()).getFolderId());
Assert.assertEquals("original messages should have stayed in inbox", Mailbox.ID_FOLDER_INBOX, mbox.getMessageById(null, m3.getId()).getFolderId());
} catch (Exception e) {
fail("No error should be thrown");
e.printStackTrace();
}
}
use of com.zimbra.cs.mailbox.Message in project zm-mailbox by Zimbra.
the class ReplyTest method testReplyMimeVariables.
@Test
public void testReplyMimeVariables() {
try {
String sampleMsg = "from: test2@zimbra.com\n" + "Return-Path: test2@zimbra.com\n" + "Subject: Hello\n" + "to: test@zimbra.com\n";
Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
RuleManager.clearCachedRules(acct1);
String filterScript = "require \"variables\";\n" + "set \"var\" \"World\";\n" + "if anyof (true) { reply \"${Subject} ${var}\"" + " stop;" + "}";
acct1.setMailSieveScript(filterScript);
List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
Assert.assertEquals(1, ids.size());
Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
Message notifyMsg = mbox2.getMessageById(null, item);
Assert.assertEquals("Hello World", notifyMsg.getFragment());
} catch (Exception e) {
fail("No exception should be thrown");
}
}
Aggregations