Search in sources :

Example 6 with ParsedMessage

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

the class ConversationTestTest method started.

@Test
public void started() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if conversation :where \"started\" { tag \"started\"; }");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    mbox.addMessage(new OperationContext(mbox), new ParsedMessage("From: test1@zimbra.com\nSubject: test".getBytes(), false), MailboxTest.STANDARD_DELIVERY_OPTIONS, new DeliveryContext());
    DeliveryOptions dopt = new DeliveryOptions();
    dopt.setFolderId(Mailbox.ID_FOLDER_SENT);
    dopt.setFlags(Flag.BITMASK_FROM_ME);
    mbox.addMessage(new OperationContext(mbox), new ParsedMessage("From: test@zimbra.com\nSubject: Re: test".getBytes(), false), dopt, new DeliveryContext());
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: test1@zimbra.com\nSubject: Re: test".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    Message msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals(0, msg.getTags().length);
    dopt = new DeliveryOptions();
    dopt.setFolderId(Mailbox.ID_FOLDER_SENT);
    dopt.setFlags(Flag.BITMASK_FROM_ME);
    mbox.addMessage(new OperationContext(mbox), new ParsedMessage("From: test@zimbra.com\nSubject: test1".getBytes(), false), dopt, new DeliveryContext());
    ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: test1@zimbra.com\nSubject: Re: test1".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals("started", ArrayUtil.getFirstElement(msg.getTags()));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 7 with ParsedMessage

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

the class ConversationTestTest method participated.

@Test
public void participated() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if conversation :where \"participated\" { tag \"participated\"; }");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    mbox.addMessage(new OperationContext(mbox), new ParsedMessage("From: test1@zimbra.com\nSubject: test".getBytes(), false), MailboxTest.STANDARD_DELIVERY_OPTIONS, new DeliveryContext());
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: test1@zimbra.com\nSubject: Re: test".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    Message msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals(0, msg.getTags().length);
    DeliveryOptions dopt = new DeliveryOptions();
    dopt.setFolderId(Mailbox.ID_FOLDER_SENT);
    dopt.setFlags(Flag.BITMASK_FROM_ME);
    mbox.addMessage(new OperationContext(mbox), new ParsedMessage("From: test@zimbra.com\nSubject: Re: test".getBytes(), false), dopt, new DeliveryContext());
    ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: test1@zimbra.com\nSubject: Re: test".getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    Assert.assertEquals(1, ids.size());
    msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals("participated", ArrayUtil.getFirstElement(msg.getTags()));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Test(org.junit.Test) MailboxTest(com.zimbra.cs.mailbox.MailboxTest)

Example 8 with ParsedMessage

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

the class DeleteHeaderTest method testDeleteHeaderXTestHeaderAll.

/*
     * Delete all X-Test-Header
     */
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderXTestHeaderAll() {
    String[] expected = { "Received: from edge01e.zimbra.com ([127.0.0.1])\r\n" + "\tby localhost (edge01e.zimbra.com [127.0.0.1]) (amavisd-new, port 10032)\r\n" + "\twith ESMTP id DN6rfD1RkHD7; Fri, 24 Jun 2016 01:45:31 -0400 (EDT)", "Received: from localhost (localhost [127.0.0.1])\r\n" + "\tby edge01e.zimbra.com (Postfix) with ESMTP id 9245B13575C;\r\n" + "\tFri, 24 Jun 2016 01:45:31 -0400 (EDT)", "X-Test-Header-non-ascii: =?utf-8?B?5pel5pys6Kqe44Gu5Lu25ZCN?=", "X-Numeric-Header: 2", "X-Numeric-Header: 3", "X-Numeric-Header: 4", "X-Dummy-Header: ABC", "X-Dummy-Header: 123", "X-Dummy-Header: abc", "X-Dummy-Header: \"\"", "X-Dummy-Header: xyz", "X-Dummy-Header: ", "X-Dummy-Header: test", "X-Dummy-Header: ''", "X-Dummy-Header: a1b2c3", "from: test2@zimbra.com", "Subject: example", "to: test@zimbra.com", "Content-Transfer-Encoding: 7bit", "MIME-Version: 1.0", "Message-ID:" };
    try {
        String filterScript = "require [\"editheader\"];\n" + " deleteheader \"X-Test-Header\" \r\n" + "  ;\n";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox1.getMessageById(null, itemId);
        boolean headerDeleted = true;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header temp = enumeration.nextElement();
            if ("X-Test-Header".equals(temp.getName())) {
                headerDeleted = false;
                break;
            }
        }
        Assert.assertTrue(headerDeleted);
        // Verify the order of the message header
        MimeMessage mm = message.getMimeMessage();
        List<MPartInfo> parts = Mime.getParts(mm);
        Set<MPartInfo> bodies = Mime.getBody(parts, false);
        Assert.assertEquals(1, bodies.size());
        for (MPartInfo body : bodies) {
            Enumeration e = body.getMimePart().getAllHeaderLines();
            int i = 0;
            while (e.hasMoreElements()) {
                String header = (String) e.nextElement();
                if (header.startsWith("Message-ID:")) {
                    header = "Message-ID:";
                }
                Assert.assertEquals(expected[i++], header);
            }
        }
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Enumeration(java.util.Enumeration) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) MPartInfo(com.zimbra.cs.mime.MPartInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 9 with ParsedMessage

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

the class DeleteHeaderTest method testDeleteHeaderUsingMatchesWildcardNonASCII.

/*
     * Delete header using matches wild-card
     */
@SuppressWarnings("unchecked")
@Test
public void testDeleteHeaderUsingMatchesWildcardNonASCII() {
    try {
        String filterScript = "require [\"editheader\"];\n" + "deleteheader :matches \"X-Test-Header-non-ascii\" \"*\";\n";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox1.getMessageById(null, itemId);
        boolean matchFound = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Test-Header-non-ascii".equals(header.getName())) {
                matchFound = true;
                break;
            }
        }
        Assert.assertFalse(matchFound);
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 10 with ParsedMessage

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

the class DeleteHeaderTest method testMatchVariables.

/*
     * Verify the Match Variables assigned by deleteheader's wild-card match
     */
@SuppressWarnings("unchecked")
@Test
public void testMatchVariables() {
    try {
        String filterScript = "require [\"editheader\"];\n" + "deleteheader :matches \"X-Dummy-Header\" \"*\";";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox1.getMessageById(null, itemId);
        String[] expectedTags = { "tag1-ABC", "tag2-123", "tag3-abc", "tag4-\"\"", "tag5-xyz", "tag6-", "tag7-tes", "tag8-\'\'", "tag9-a1b2c3" };
        String[] resultTags = message.getTags();
        for (String resultTag : resultTags) {
            String expectedTag = null;
            for (String testTag : expectedTags) {
                if (testTag.equalsIgnoreCase(resultTag)) {
                    expectedTag = testTag;
                    break;
                }
            }
            Assert.assertEquals(expectedTag, resultTag);
        }
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Aggregations

ParsedMessage (com.zimbra.cs.mime.ParsedMessage)388 Mailbox (com.zimbra.cs.mailbox.Mailbox)304 Test (org.junit.Test)300 Message (com.zimbra.cs.mailbox.Message)278 Account (com.zimbra.cs.account.Account)271 OperationContext (com.zimbra.cs.mailbox.OperationContext)264 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)252 ItemId (com.zimbra.cs.service.util.ItemId)186 MimeMessage (javax.mail.internet.MimeMessage)113 Header (javax.mail.Header)66 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)55 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)50 SyntaxException (org.apache.jsieve.exception.SyntaxException)49 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)45 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)33 ServiceException (com.zimbra.common.service.ServiceException)30 InputStream (java.io.InputStream)24 IOException (java.io.IOException)21 Element (com.zimbra.common.soap.Element)18