Search in sources :

Example 56 with DeliveryContext

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

Example 57 with DeliveryContext

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

the class LinkedInTestTest method test.

@Test
public void test() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if linkedin { tag \"linkedin\"; }");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    // connections@linkedin.com
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(("Sender: messages-noreply@bounce.linkedin.com\n" + "From: LinkedIn Connections <connections@linkedin.com>").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("linkedin", ArrayUtil.getFirstElement(msg.getTags()));
    // deals
    ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(("Sender: messages-noreply@bounce.linkedin.com\n" + "From: Yuichi Sasaki via LinkedIn <member@linkedin.com>").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("linkedin", 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) LinkedInTest(com.zimbra.cs.filter.jsieve.LinkedInTest) Test(org.junit.Test)

Example 58 with DeliveryContext

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

the class HeaderTest method testHeaderMatchWithItself.

@Test
public void testHeaderMatchWithItself() throws Exception {
    String script = "if header :matches \"X-Header1\" \"*\" {" + "    if header :matches \"X-Header1\" \"${1}\" {" + "        tag \"01\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    if header :is \"X-Header1\" \"${1}\" {" + "        tag \"02\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    set \"myvar1\" \"${1}\";" + "    if header :matches \"X-Header1\" \"${myvar1}\" {" + "        tag \"03\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    set :quotewildcard \"myvar2\" \"${1}\";" + "    if string :matches \"sample\\\\\\\\\\\\\\\\pattern\" \"${myvar2}\" {" + "        tag \"04\";" + "    }" + "}";
    String sourceMsg = "X-Header1: sample\\\\pattern\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(4, 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]);
    } 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)

Example 59 with DeliveryContext

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

the class HeaderTest method doTest.

private void doTest(String filterScript, String expectedResult) {
    try {
        LmtpEnvelope env = setEnvelopeInfo();
        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(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals(expectedResult, ArrayUtil.getFirstElement(msg.getTags()));
    } 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) 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) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId)

Example 60 with DeliveryContext

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

the class MeTestTest method quotedMultiRecipientTo.

@Test
public void quotedMultiRecipientTo() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if me :in \"To\" { tag \"Priority\"; }");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("To: \"bar, foo\" <foo@zimbra.com>, \"user, test\" <test@zimbra.com>, \"aaa bbb\" <aaabbb@test.com>".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("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) MeTest(com.zimbra.cs.filter.jsieve.MeTest)

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