Search in sources :

Example 41 with ParsedMessage

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

the class AddressTest method testAddressContainingDot.

@Test
public void testAddressContainingDot() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        String filterScript = "if address :comparator \"i;ascii-casemap\" :matches \"to\" \"user.1@cosmonaut.zimbra.com\" {" + "  tag \"TestDot\";" + "}";
        account.setMailSieveScript(filterScript);
        InputStream is = getClass().getResourceAsStream("TestFilter-testBackslashDotInAddress.msg");
        MimeMessage mm = new ZMimeMessage(JMSession.getSession(), is);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(mm, 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("TestDot", ArrayUtil.getFirstElement(msg.getTags()));
    } catch (Exception e) {
        fail("No exception should be thrown" + e);
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) InputStream(java.io.InputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 42 with ParsedMessage

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

the class AddHeaderTest method testAddHeaderNameAsMultipleSpace.

/*
     * Try adding new header with multiple spaces as header name, which should fail
     */
@Test
public void testAddHeaderNameAsMultipleSpace() {
    try {
        String filterScript = "require [\"editheader\"];\n" + " addheader \"    \" \"my-new-header-value\" \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 mdnMsg = mbox1.getMessageById(null, itemId);
        for (Enumeration<Header> e = mdnMsg.getMimeMessage().getAllHeaders(); e.hasMoreElements(); ) {
            Header temp = e.nextElement();
            Assert.assertFalse(temp.getValue().equals("my-new-header-value"));
        }
    } 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) 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 43 with ParsedMessage

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

the class AddHeaderTest method testAddHeader.

/*
     * Adding new header
     */
@Test
public void testAddHeader() {
    try {
        String filterScript = "require [\"editheader\"];\n" + "if header :contains \"Subject\" \"example\" {\n" + " addheader \"my-new-header\" \"my-new-header-value\" \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);
        int index = 0;
        Message mdnMsg = mbox1.getMessageById(null, itemId);
        for (Enumeration<Header> e = mdnMsg.getMimeMessage().getAllHeaders(); e.hasMoreElements(); ) {
            Header temp = e.nextElement();
            index++;
            if ("my-new-header".equals(temp.getName())) {
                break;
            }
        }
        // the header field is inserted at the beginning of the existing message header.
        Assert.assertEquals(1, index);
    } 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) 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 44 with ParsedMessage

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

the class EscapeSequencesTest method doTestHeaderEscapePattern.

/*
     * MAIL FROM: <user123@zimbra.com>
     * RCPT TO: <test1@zimbra.com>
     * DATA
     * To: user123@zimbra.com
     * From: sender@zimbra.com
     * Subject: test\123
     * X-Header-0BackSlash:  testSample
     * X-Header-1BaskSlash:  test\Sample
     * X-Header-2BackSlash:  test\\Sample
     * X-Header-DoubleQuote: test"Sample
     */
public void doTestHeaderEscapePattern(String filterScript) {
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<user123@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        RuleManager.clearCachedRules(account);
        account.setMailSieveScript(filterScript);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(triggeringMsg.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals(1, msg.getTags().length);
        Assert.assertEquals("list", msg.getTags()[0]);
    } catch (Exception e) {
        fail("No exception should be thrown " + e);
    }
}
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) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId)

Example 45 with ParsedMessage

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

the class EscapeSequencesTest method doTestReplaceheaderEscapePattern.

private void doTestReplaceheaderEscapePattern(String filterScript) {
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(triggeringMsg.getBytes(), false), 0, account.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox.getMessageById(null, itemId);
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("Subject".equals(header.getName())) {
                Assert.assertEquals("[replaced]", header.getValue());
                break;
            }
        }
    } 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) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext)

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