Search in sources :

Example 51 with DeliveryContext

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

the class FileIntoCopyTest method testCopyFileIntoPattern3Real.

/*
     * fileinto :copy foo; if header :contains "Subject" "Test" { discard; }
     * 
     * if message has "Subject: real" ==> it should be stored in "foo" and INBOX
     */
@Test
public void testCopyFileIntoPattern3Real() {
    try {
        String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";" + "if header :contains \"Subject\" \"Test\" {\n" + "discard; }";
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScriptPattern1);
        String rawReal = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Real\n" + "\n" + "Hello World";
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(rawReal.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // message should be stored in foo
        Integer item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "foo").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        Message msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
        // message should be stored in inbox
        item = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
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) Test(org.junit.Test)

Example 52 with DeliveryContext

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

the class FileIntoCopyTest method testPlainFileInto.

@Test
public void testPlainFileInto() {
    String filterPlainFileintoScript = "require [\"fileinto\"];\n" + "if header :contains \"Subject\" \"test\" { fileinto \"Junk\"; }";
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterPlainFileintoScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.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("Test", msg.getSubject());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
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) Test(org.junit.Test)

Example 53 with DeliveryContext

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

the class FileIntoCopyTest method doKeepAndFileIntoIncoming.

private void doKeepAndFileIntoIncoming(String filterScript) {
    String body = "doKeepAndFileIntoIncoming" + filterScript.hashCode();
    String sampleMsg = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + body;
    try {
        // Incoming
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScript);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sampleMsg.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(1, ids.size());
        List<Integer> searchedIds = TestUtil.search(mbox, "in:inbox " + body, MailItem.Type.MESSAGE);
        Assert.assertEquals(1, searchedIds.size());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
Also used : OperationContext(com.zimbra.cs.mailbox.OperationContext) Account(com.zimbra.cs.account.Account) Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId)

Example 54 with DeliveryContext

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

the class FileIntoCopyTest method testCopyFileIntoPattern1Test.

/*
     * fileinto :copy foo; if header :contains "Subject" "test" { fileinto bar;
     * }
     * 
     * if message has "Subject: Test" ==> it should be stored in "foo" and "bar"
     */
@Test
public void testCopyFileIntoPattern1Test() {
    try {
        String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";\n" + "if header :contains \"Subject\" \"Test\" {\n" + "fileinto \"bar\"; }";
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScriptPattern1);
        String rawTest = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World";
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(rawTest.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // message should not be stored in inbox
        Assert.assertNull(mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE));
        // message should be stored in foo
        Integer item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "foo").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        Message msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
        // message should be stored in bar
        item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "bar").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
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) Test(org.junit.Test)

Example 55 with DeliveryContext

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

the class FileIntoCopyTest method testPlainFileIntoNonExistingFolder.

@Test
public void testPlainFileIntoNonExistingFolder() {
    String filterPlainFileintoScript = "require [\"fileinto\"];\n" + "if header :contains \"Subject\" \"test\" { fileinto \"HelloWorld\"; }";
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterPlainFileintoScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.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("Test", msg.getSubject());
        com.zimbra.cs.mailbox.Folder folder = mbox.getFolderById(null, msg.getFolderId());
        Assert.assertEquals("HelloWorld", folder.getName());
    } catch (Exception e) {
        e.printStackTrace();
        fail("No exception should be thrown");
    }
}
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) ItemId(com.zimbra.cs.service.util.ItemId) Mailbox(com.zimbra.cs.mailbox.Mailbox) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) 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