Search in sources :

Example 76 with ItemId

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

the class SetVariableTest method testVariablesCombo.

@Test
public void testVariablesCombo() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        // set "company" "ACME";
        // set "a.b" "おしらせ"; (or any non-ascii characters [おしらせ])
        // set "c_d" "C";
        // set "1" "One"; ==> Should be ignored or error [Note 1]
        // set "23" "twenty three"; ==> Should be ignored or error [Note 1]
        // set "combination" "Hello ${company}!!";
        filterScript = "require [\"variables\"];\n" + "set \"company\" \"おしらせ\" ;\n" + "set  \"c_d\" \"C\";\n" + "set  \"combination\" \"Hello ${company}!!\"; " + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${combination}\";\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);
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("Hello おしらせ!!", 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) 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 77 with ItemId

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

the class SetVariableTest method testSetVarWithModifiersValid.

@Test
public void testSetVarWithModifiersValid() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "set \"var\" \"hello\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\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);
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("hello", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :length \"var\" \"hello\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("5", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :lower \"var\" \"heLLo\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("hello", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :upper \"var\" \"test\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("TEST", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :lowerfirst \"var\" \"WORLD\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("wORLD", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :UPPERFIRST \"var\" \"example\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("Example", ArrayUtil.getFirstElement(msg.getTags()));
        RuleManager.clearCachedRules(account);
        filterScript = "require [\"variables\"];\n" + "set :encodeurl :lower \"body_param\" \"Safe body&evil=evilbody\";\n" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${body_param}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("safe+body%26evil%3Devilbody", 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) 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 78 with ItemId

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

the class SetVariableTest method testSetMatchVarAndUseInHeaderForAddress.

@Test
public void testSetMatchVarAndUseInHeaderForAddress() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "if address :comparator \"i;ascii-casemap\" :matches \"To\" \"coyote@**.com\"{\n" + "  tag \"${2}\";\n" + "}";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: hello version 1.0 is out\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("ACME.Example", 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) 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 79 with ItemId

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

the class SocialcastTestTest method test.

@Test
public void test() throws Exception {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    account.setMailSieveScript("if socialcast { tag \"socialcast\"; }");
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    // bulk from socialcast
    List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage("From: do-not-reply@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.assertTrue(msg.getTags().length == 0);
    // notification from socialcast
    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());
    msg = mbox.getMessageById(null, ids.get(0).getId());
    Assert.assertEquals("socialcast", 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) SocialcastTest(com.zimbra.cs.filter.jsieve.SocialcastTest) Test(org.junit.Test)

Example 80 with ItemId

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

the class SetVariableTest method testNonExistingVarIndexWithLeadingZeroes.

@Ignore
public void testNonExistingVarIndexWithLeadingZeroes() {
    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\" \"t*t*\" { " + "addheader :last \"X-New-Header\" \"${009}\";}";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: test 123";
        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());
        String[] value = msg.getMimeMessage().getHeader("X-New-Header");
        Assert.assertEquals("", value[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) Ignore(org.junit.Ignore)

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