Search in sources :

Example 36 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class EnvelopeTest method testFrom.

@Test
public void testFrom() {
    // RFC 5228 5.4. Test envelope example
    String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"from\" \"tim@example.com\" {\n" + "discard;\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        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(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(0, ids.size());
    } catch (Exception e) {
        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) 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) Test(org.junit.Test)

Example 37 with ItemId

use of com.zimbra.cs.service.util.ItemId 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 38 with ItemId

use of com.zimbra.cs.service.util.ItemId 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 39 with ItemId

use of com.zimbra.cs.service.util.ItemId 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)

Example 40 with ItemId

use of com.zimbra.cs.service.util.ItemId in project zm-mailbox by Zimbra.

the class HeaderTest method testBackslash.

/**
     * <ul>
     *  <li> a header with one backslash matches a filter with one escaped backslash (\\)
     *  <li> a header with two backslashes match a filter with two sets of escaped backslash (\\ and \\)
     *  <li> a header with three backslashes match a filter with three sets of escaped backslash (\\ x 3)
     *  <li> a header with four backslashes match a filter with four sets of escaped backslash (\\ x 4)
     *  <li> a header with five backslashes match a filter with five sets of escaped backslash (\\ x 5)
     *  <li> when the nested-if tests the same header (X-HeaderN), the same value matches both outer and inner 'if' condition.
     * </ul>
     * @throws Exception
     */
@Test
public void testBackslash() throws Exception {
    String script = "if header :matches \"X-Header1\" \"sample\\\\pattern\"             { tag \"01\"; }" + "if header :matches \"X-Header2\" \"sample\\\\\\\\pattern\"         { tag \"02\"; }" + "if header :matches \"X-Header3\" \"sample\\\\\\\\\\\\pattern\"     { tag \"03\"; }" + "if header :matches \"X-Header4\" \"sample\\\\\\\\\\\\\\\\pattern\" { tag \"04\"; }" + "if header :matches \"X-Header5\" \"sample\\\\\\\\\\\\\\\\\\\\\"    { tag \"05\"; }" + "if header :matches \"X-Header1\" \"*\" { set \"var1\" \"${1}\"; if header :matches \"X-Header1\" \"${var1}\" { tag \"11\"; }}" + "if header :matches \"X-Header2\" \"*\" { set \"var2\" \"${1}\"; if header :matches \"X-Header2\" \"${var2}\" { tag \"12\"; }}" + "if header :matches \"X-Header3\" \"*\" { set \"var3\" \"${1}\"; if header :matches \"X-Header3\" \"${var3}\" { tag \"13\"; }}" + "if header :matches \"X-Header4\" \"*\" { set \"var4\" \"${1}\"; if header :matches \"X-Header4\" \"${var4}\" { tag \"14\"; }}" + "if header :matches \"X-Header5\" \"*\" { set \"var5\" \"${1}\"; if header :matches \"X-Header5\" \"${var5}\" { tag \"15\"; }}" + "if header :comparator \"i;octet\" :matches \"X-Header1\" \"sample\\\\pattern\"             { tag \"21\"; }" + "if header :comparator \"i;octet\" :matches \"X-Header2\" \"sample\\\\\\\\pattern\"         { tag \"22\"; }" + "if header :comparator \"i;octet\" :matches \"X-Header3\" \"sample\\\\\\\\\\\\pattern\"     { tag \"23\"; }" + "if header :comparator \"i;octet\" :matches \"X-Header4\" \"sample\\\\\\\\\\\\\\\\pattern\" { tag \"24\"; }" + "if header :comparator \"i;octet\" :matches \"X-Header5\" \"sample\\\\\\\\\\\\\\\\\\\\\"    { tag \"25\"; }" + "if header :comparator \"i;octet\" :matches \"X-Header1\" \"*\" { set \"var1\" \"${1}\"; if header :comparator \"i;octet\" :matches \"X-Header1\" \"${var1}\" { tag \"31\"; }}" + "if header :comparator \"i;octet\" :matches \"X-Header2\" \"*\" { set \"var2\" \"${1}\"; if header :comparator \"i;octet\" :matches \"X-Header2\" \"${var2}\" { tag \"32\"; }}" + "if header :comparator \"i;octet\" :matches \"X-Header3\" \"*\" { set \"var3\" \"${1}\"; if header :comparator \"i;octet\" :matches \"X-Header3\" \"${var3}\" { tag \"33\"; }}" + "if header :comparator \"i;octet\" :matches \"X-Header4\" \"*\" { set \"var4\" \"${1}\"; if header :comparator \"i;octet\" :matches \"X-Header4\" \"${var4}\" { tag \"34\"; }}" + "if header :comparator \"i;octet\" :matches \"X-Header5\" \"*\" { set \"var5\" \"${1}\"; if header :comparator \"i;octet\" :matches \"X-Header5\" \"${var5}\" { tag \"35\"; }}";
    String sourceMsg = "X-Header1: sample\\pattern\n" + "X-Header2: sample\\\\pattern\n" + "X-Header3: sample\\\\\\pattern\n" + "X-Header4: sample\\\\\\\\pattern\n" + "X-Header5: sample\\\\\\\\\\\n";
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        account.setAdminSieveScriptBefore(script);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sourceMsg.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(20, msg.getTags().length);
        Assert.assertEquals("01", msg.getTags()[0]);
        Assert.assertEquals("02", msg.getTags()[1]);
        Assert.assertEquals("03", msg.getTags()[2]);
        Assert.assertEquals("04", msg.getTags()[3]);
        Assert.assertEquals("05", msg.getTags()[4]);
        Assert.assertEquals("11", msg.getTags()[5]);
        Assert.assertEquals("12", msg.getTags()[6]);
        Assert.assertEquals("13", msg.getTags()[7]);
        Assert.assertEquals("14", msg.getTags()[8]);
        Assert.assertEquals("15", msg.getTags()[9]);
        Assert.assertEquals("21", msg.getTags()[10]);
        Assert.assertEquals("22", msg.getTags()[11]);
        Assert.assertEquals("23", msg.getTags()[12]);
        Assert.assertEquals("24", msg.getTags()[13]);
        Assert.assertEquals("25", msg.getTags()[14]);
        Assert.assertEquals("31", msg.getTags()[15]);
        Assert.assertEquals("32", msg.getTags()[16]);
        Assert.assertEquals("33", msg.getTags()[17]);
        Assert.assertEquals("34", msg.getTags()[18]);
        Assert.assertEquals("35", msg.getTags()[19]);
    } 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) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Message(com.zimbra.cs.mailbox.Message) MimeMessage(javax.mail.internet.MimeMessage) 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)

Aggregations

ItemId (com.zimbra.cs.service.util.ItemId)327 Mailbox (com.zimbra.cs.mailbox.Mailbox)243 OperationContext (com.zimbra.cs.mailbox.OperationContext)231 Account (com.zimbra.cs.account.Account)219 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)189 Message (com.zimbra.cs.mailbox.Message)185 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)168 Test (org.junit.Test)161 Element (com.zimbra.common.soap.Element)81 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)80 MimeMessage (javax.mail.internet.MimeMessage)65 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)58 ServiceException (com.zimbra.common.service.ServiceException)52 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)47 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)45 ItemIdFormatter (com.zimbra.cs.service.util.ItemIdFormatter)44 SyntaxException (org.apache.jsieve.exception.SyntaxException)42 Folder (com.zimbra.cs.mailbox.Folder)33 ArrayList (java.util.ArrayList)30 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)29