Search in sources :

Example 91 with OperationContext

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

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

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

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

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

OperationContext (com.zimbra.cs.mailbox.OperationContext)396 Mailbox (com.zimbra.cs.mailbox.Mailbox)365 Account (com.zimbra.cs.account.Account)292 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)266 Message (com.zimbra.cs.mailbox.Message)256 Test (org.junit.Test)255 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)249 ItemId (com.zimbra.cs.service.util.ItemId)231 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)82 MimeMessage (javax.mail.internet.MimeMessage)82 Element (com.zimbra.common.soap.Element)80 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)72 Header (javax.mail.Header)66 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)50 SyntaxException (org.apache.jsieve.exception.SyntaxException)47 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)46 ServiceException (com.zimbra.common.service.ServiceException)36 Folder (com.zimbra.cs.mailbox.Folder)27 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)26