Search in sources :

Example 31 with DeliveryContext

use of com.zimbra.cs.mailbox.DeliveryContext 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 32 with DeliveryContext

use of com.zimbra.cs.mailbox.DeliveryContext 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 33 with DeliveryContext

use of com.zimbra.cs.mailbox.DeliveryContext 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 34 with DeliveryContext

use of com.zimbra.cs.mailbox.DeliveryContext 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)

Example 35 with DeliveryContext

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

the class ExistingMessageHandlerTest method existing.

@Test
public void existing() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    OperationContext octx = new OperationContext(mbox);
    Message msg = mbox.addMessage(octx, new ParsedMessage("From: sender@zimbra.com\nTo: test@zimbra.com\nSubject: test".getBytes(), false), new DeliveryOptions().setFolderId(Mailbox.ID_FOLDER_INBOX).setFlags(Flag.BITMASK_PRIORITY), new DeliveryContext());
    Folder f = mbox.createFolder(null, "test", new Folder.FolderOptions().setDefaultView(MailItem.Type.MESSAGE));
    ExistingMessageHandler handler = new ExistingMessageHandler(octx, mbox, msg.getId(), (int) msg.getSize());
    ItemId newMsgItemId = handler.fileInto("test", new ArrayList<ActionFlag>(), new String[0]);
    Message newMsg = mbox.getMessageById(octx, newMsgItemId.getId());
    Assert.assertEquals(msg.getFolderId(), Integer.parseInt(newMsg.getUnderlyingData().getPrevFolders().split(":")[1]));
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Folder(com.zimbra.cs.mailbox.Folder) ItemId(com.zimbra.cs.service.util.ItemId) ActionFlag(com.zimbra.cs.filter.jsieve.ActionFlag) Mailbox(com.zimbra.cs.mailbox.Mailbox) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) DeliveryOptions(com.zimbra.cs.mailbox.DeliveryOptions) Test(org.junit.Test)

Aggregations

DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)253 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)253 OperationContext (com.zimbra.cs.mailbox.OperationContext)249 Mailbox (com.zimbra.cs.mailbox.Mailbox)243 Account (com.zimbra.cs.account.Account)242 Message (com.zimbra.cs.mailbox.Message)235 Test (org.junit.Test)223 ItemId (com.zimbra.cs.service.util.ItemId)168 Header (javax.mail.Header)66 MimeMessage (javax.mail.internet.MimeMessage)64 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)50 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)49 SyntaxException (org.apache.jsieve.exception.SyntaxException)47 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)23 Ignore (org.junit.Ignore)11 InputStream (java.io.InputStream)10 MockProvisioning (com.zimbra.cs.account.MockProvisioning)9 Provisioning (com.zimbra.cs.account.Provisioning)9 BulkTest (com.zimbra.cs.filter.jsieve.BulkTest)6