Search in sources :

Example 91 with DeliveryContext

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

the class RuleManagerAdminFilterTest method requireText.

// Verification for the ZCS-272
/*  @Test
    public void deleteHeaderInAdminBefore() throws Exception {
        String adminBefore = "require [\"editheader\",\"log\"];\n"
                           + "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";

        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        RuleManager.clearCachedRules(account);

        account.unsetAdminSieveScriptBefore();
        account.unsetMailSieveScript();
        account.unsetAdminSieveScriptAfter();

        account.setAdminSieveScriptBefore(adminBefore);

        String rawTest = "From: sender@zimbra.com\n"
                       + "To: test1@zimbra.com\n"
                       + "Subject: Test\n"
                       + "X-Test-Header: Random\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);

        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox.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);
    }

    // Verification for the ZCS-272
    @Test
    public void deleteHeaderInUser() throws Exception {
        String endUser = "require [\"editheader\",\"log\"];\n"
                       + "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";

        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        RuleManager.clearCachedRules(account);

        account.unsetAdminSieveScriptBefore();
        account.unsetMailSieveScript();
        account.unsetAdminSieveScriptAfter();

        account.setMailSieveScript(endUser);

        String rawTest = "From: sender@zimbra.com\n"
                       + "To: test1@zimbra.com\n"
                       + "Subject: Test\n"
                       + "X-Test-Header: Random\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);

        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox.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);
    }

    // Verification for the ZCS-272
    @Test
    public void deleteHeaderInAdminAfter() throws Exception {
        String adminAfter = "require [\"editheader\",\"log\"];\n"
                          + "deleteheader :matches \"X-Test-Header\" \"Ran*\";\n";

        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        RuleManager.clearCachedRules(account);

        account.unsetAdminSieveScriptBefore();
        account.unsetMailSieveScript();
        account.unsetAdminSieveScriptAfter();

        account.setAdminSieveScriptAfter(adminAfter);

        String rawTest = "From: sender@zimbra.com\n"
                       + "To: test1@zimbra.com\n"
                       + "Subject: Test\n"
                       + "X-Test-Header: Random\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);

        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox.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);
    }
*/
/* Verification for the ZCS-611
     */
@Test
public void requireText() throws Exception {
    String adminAfter = "require [\"log\", \"fileinto\"];\n" + "require \"tag\";\n" + "if  header :contains [\"Subject\"] \"require abc def\" {\n" + "  tag \"--require--\";" + "  tag \"123require789\";\n" + "}";
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    RuleManager.clearCachedRules(account);
    account.unsetAdminSieveScriptBefore();
    account.unsetMailSieveScript();
    account.unsetAdminSieveScriptAfter();
    account.setAdminSieveScriptAfter(adminAfter);
    RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("Subject: require abc def\n".getBytes(), false), 0, account.getName(), 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);
    String[] tags = message.getTags();
    Assert.assertTrue(tags != null);
    Assert.assertEquals(2, tags.length);
    Assert.assertEquals("--require--", tags[0]);
    Assert.assertEquals("123require789", tags[1]);
}
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 92 with DeliveryContext

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

the class RuleManagerTest method tagAndFileinto.

@Test
public void tagAndFileinto() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"priority\"; fileinto \"socialcast\"; }");
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: 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("socialcast", mbox.getFolderById(null, msg.getFolderId()).getName());
    Assert.assertEquals("priority", ArrayUtil.getFirstElement(msg.getTags()));
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"priority\"; }\n" + "if header :contains [\"Subject\"] [\"Zimbra\"] { fileinto \"zimbra\"; }");
    ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: Zimbra".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("zimbra", mbox.getFolderById(null, msg.getFolderId()).getName());
    Assert.assertEquals("priority", 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) Test(org.junit.Test)

Example 93 with DeliveryContext

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

the class RuleManagerTest method tagAndTag.

@Test
public void tagAndTag() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"priority\"; tag \"socialcast\"; }");
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: 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.assertArrayEquals(new String[] { "priority", "socialcast" }, msg.getTags());
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"priority\"; }\n" + "if header :contains [\"Subject\"] [\"Zimbra\"] { tag \"zimbra\"; }");
    ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@socialcast.com\nReply-To: share@socialcast.com\nSubject: Zimbra".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.assertArrayEquals(new String[] { "priority", "zimbra" }, 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) Test(org.junit.Test)

Example 94 with DeliveryContext

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

the class SetVariableTest method testSetVarNameWithDigits.

@Test
public void testSetVarNameWithDigits() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "set \"var2\" \"hello\"\n;" + "set \"var_2\" \"hellovar_2\"\n;" + "set \"_var2\" \"hello_var2\"\n;" + "set \"_var2_ad\" \"hello_var2_ad\"\n;" + "set \"_var2_\" \"hello_var2_\"\n;" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var2}\";\n" + "  tag \"${var_2}\";\n" + "  tag \"${_var2}\";\n" + "  tag \"${_var2_ad}\";\n" + "  tag \"${_var2_}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        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("hello", msg.getTags()[0]);
        Assert.assertEquals("hellovar_2", msg.getTags()[1]);
        Assert.assertEquals("hello_var2", msg.getTags()[2]);
        Assert.assertEquals("hello_var2_ad", msg.getTags()[3]);
        Assert.assertEquals("hello_var2_", msg.getTags()[4]);
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Example 95 with DeliveryContext

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

the class SetVariableTest method testWildCardGreedyMatch.

@Test
public void testWildCardGreedyMatch() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"sample*test\" { " + "tag \"${1}\";}";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: sample abc test 123 test ABC test";
        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("abc test 123 test ABC", msg.getTags()[0]);
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) 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