Search in sources :

Example 71 with Message

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

the class HeaderTest method testMissingComparatorNumericDeclaration.

/*
     * The ascii-numeric comparator should be looked up in the list of the "require".
     */
@Test
public void testMissingComparatorNumericDeclaration() throws Exception {
    // Default match type :is is used.
    // No "comparator-i;ascii-numeric" capability text in the require command
    String filterScript = "require [\"tag\"];" + "if header :comparator \"i;ascii-numeric\" \"Subject\" \"こんにちは\" {\n" + "  tag \"is\";\n" + "} else {\n" + "  tag \"not is\";\n" + "}";
    try {
        LmtpEnvelope env = setEnvelopeInfo();
        Account account = Provisioning.getInstance().getAccountByName("testHdr@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.unsetAdminSieveScriptBefore();
        account.unsetMailSieveScript();
        account.unsetAdminSieveScriptAfter();
        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(null, ArrayUtil.getFirstElement(msg.getTags()));
    } catch (Exception e) {
        fail("No exception should be thrown" + e);
    }
}
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) Test(org.junit.Test)

Example 72 with Message

use of com.zimbra.cs.mailbox.Message 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 = "require [\"variables\"];\n" + "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().getAccountByName("testHdr@zimbra.com");
        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 73 with Message

use of com.zimbra.cs.mailbox.Message 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 74 with Message

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

Example 75 with Message

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

the class MeTestTest method meInToMultiRecipient.

@Test
public void meInToMultiRecipient() 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: foo@zimbra.com, test@zimbra.com, bar@zimbra.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

Message (com.zimbra.cs.mailbox.Message)442 Mailbox (com.zimbra.cs.mailbox.Mailbox)375 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)358 Test (org.junit.Test)335 Account (com.zimbra.cs.account.Account)315 OperationContext (com.zimbra.cs.mailbox.OperationContext)302 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)281 ItemId (com.zimbra.cs.service.util.ItemId)211 MimeMessage (javax.mail.internet.MimeMessage)123 SyntaxException (org.apache.jsieve.exception.SyntaxException)87 Header (javax.mail.Header)83 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)63 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)53 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)46 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)43 ServiceException (com.zimbra.common.service.ServiceException)41 Element (com.zimbra.common.soap.Element)41 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)36 Folder (com.zimbra.cs.mailbox.Folder)32 ZMailbox (com.zimbra.client.ZMailbox)26