Search in sources :

Example 31 with LmtpAddress

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

the class SetVariableTest method testSetMatchVarMultiLineWithEnvelope.

@Ignore
public void testSetMatchVarMultiLineWithEnvelope() {
    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);
        filterScript = "require [\"log\", \"variables\", \"envelope\" ];\n" + "if envelope :matches [\"To\"] \"*\" {" + "  set \"rcptto\" \"${1}\";\n" + "}\n" + "if header :matches [\"From\"] \"*\" {" + "  set \"fromheader\" \"${1}\";\n" + "}\n" + "if header :matches [\"Subject\"] \"*\" {" + "  set \"subjectheader\" \"${1}\";\n" + "}\n" + "set \"bodyparam\" text: # This is a comment\r\n" + "Message delivered to  ${rcptto}\n" + "Sent : ${fromheader}\n" + "Subject : ${subjectheader} \n" + ".\r\n" + ";\n" + "log \"${bodyparam}\"; \n";
        System.out.println(filterScript);
        account.setMailSieveScript(filterScript);
        account.setMail("test@zimbra.com");
        String raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.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(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Ignore(org.junit.Ignore)

Example 32 with LmtpAddress

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

the class SetVariableTest method testSetMatchVarOutOfRange.

/*
     * Verify that any unassigned Matched Variables should replaced by the "".
     */
@Test
public void testSetMatchVarOutOfRange() {
    try {
        filterScript = "require [\"variables\", \"editheader\"];\n" + "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"*C-*baa*123*\" { \r\n" + "  addheader \"X-New-Header1\" \"${0}\";\r\n" + "  addheader \"X-New-Header1\" \"${1}\";\r\n" + "  addheader \"X-New-Header1\" \"${2}\";\r\n" + "  addheader \"X-New-Header1\" \"${3}\";\r\n" + "  addheader \"X-New-Header1\" \"${4}\";\r\n" + "  addheader \"X-New-Header1\" \"${5}\";\r\n" + "  addheader \"X-New-Header1\" \"${6}\";\r\n" + "  addheader \"X-New-Header1\" \"${7}\";\r\n" + "  addheader \"X-New-Header1\" \"${8}\";\r\n" + "  addheader \"X-New-Header1\" \"${9}\";\r\n" + "}\n" + "if envelope :matches :comparator \"i;ascii-casemap\" [\"From\"] \"*yo*@*COM\" { \r\n" + "  addheader \"X-New-Header2\" \"${0}\";\r\n" + "  addheader \"X-New-Header2\" \"${1}\";\r\n" + "  addheader \"X-New-Header2\" \"${2}\";\r\n" + "  addheader \"X-New-Header2\" \"${3}\";\r\n" + "  addheader \"X-New-Header2\" \"${4}\";\r\n" + "  addheader \"X-New-Header2\" \"${5}\";\r\n" + "  addheader \"X-New-Header2\" \"${6}\";\r\n" + "  addheader \"X-New-Header2\" \"${7}\";\r\n" + "  addheader \"X-New-Header2\" \"${8}\";\r\n" + "  addheader \"X-New-Header2\" \"${9}\";\r\n" + "}\n" + "if address :matches :comparator \"i;ascii-casemap\" [\"To\"] \"*t@zimbra.com\" { \r\n" + "  addheader \"X-New-Header3\" \"${0}\";\r\n" + "  addheader \"X-New-Header3\" \"${1}\";\r\n" + "  addheader \"X-New-Header3\" \"${2}\";\r\n" + "  addheader \"X-New-Header3\" \"${3}\";\r\n" + "  addheader \"X-New-Header3\" \"${4}\";\r\n" + "  addheader \"X-New-Header3\" \"${5}\";\r\n" + "  addheader \"X-New-Header3\" \"${6}\";\r\n" + "  addheader \"X-New-Header3\" \"${7}\";\r\n" + "  addheader \"X-New-Header3\" \"${8}\";\r\n" + "  addheader \"X-New-Header\" \"${9}\";\r\n" + "}\n";
        String raw = "To: test@zimbra.com\n" + "From: coyote@ACME.Example.COM\n" + "Subject: test C-51 abc sample foo bar hoge piyo 123 456 789 sieve test\n" + "\n" + "Hello world.";
        String[] expectedHeaderValue1 = { "test C-51 abc sample foo bar hoge piyo 123 456 789 sieve test", "test", "51 abc sample foo", "hoge piyo", "456 789 sieve test" };
        String[] expectedHeaderValue2 = { "coyote@ACME.Example.COM", "co", "te", "ACME.Example." };
        String[] expectedHeaderValue3 = { "test@zimbra.com", "tes" };
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        Map<String, Object> attrs = Maps.newHashMap();
        attrs = Maps.newHashMap();
        Provisioning.getInstance().getServer(account).modify(attrs);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<coyote@ACME.Example.COM>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        RuleManager.clearCachedRules(account);
        account.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message mdnMsg = mbox.getMessageById(null, itemId);
        for (Enumeration<Header> e = mdnMsg.getMimeMessage().getAllHeaders(); e.hasMoreElements(); ) {
            Header temp = e.nextElement();
            check("X-New-Header1", temp, expectedHeaderValue1);
            check("X-New-Header2", temp, expectedHeaderValue2);
            check("X-New-Header3", temp, expectedHeaderValue3);
        }
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
}
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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) Header(javax.mail.Header) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 33 with LmtpAddress

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

the class NotifyMailtoTest method testNoMessageBody.

@Test
public void testNoMessageBody() {
    String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: test1@zimbra.com\n";
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
        Account acct3 = Provisioning.getInstance().get(Key.AccountBy.name, "test3@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
        Mailbox mbox3 = MailboxManager.getInstance().getMailboxByAccount(acct3);
        RuleManager.clearCachedRules(acct1);
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<xyz@example.com>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScript_NoBodyParameter);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // The triggered message should be delivered to the target mailbox
        Assert.assertEquals(1, ids.size());
        // Notification message should be delivered to mailto and to= addresses
        Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message notifyMsg = mbox2.getMessageById(null, item);
        Assert.assertEquals("", notifyMsg.getFragment());
        String[] headers = notifyMsg.getMimeMessage().getHeader("Auto-Submitted");
        Assert.assertTrue(headers.length == 1);
        Assert.assertEquals("auto-notified; owner-email=\"test1@zimbra.com\"", headers[0]);
        headers = notifyMsg.getMimeMessage().getHeader("to");
        Assert.assertTrue(headers.length == 1);
        Assert.assertEquals("test2@zimbra.com, test3@zimbra.com", headers[0]);
        headers = notifyMsg.getMimeMessage().getHeader("from");
        Assert.assertFalse(notifyMsg.getSender() == null);
        Assert.assertEquals("test1@zimbra.com", notifyMsg.getSender());
        notifyMsg = mbox3.getMessageById(null, item);
        Assert.assertEquals("おしらせ", notifyMsg.getSubject());
    } catch (Exception e) {
        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) MimeMessage(javax.mail.internet.MimeMessage) 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) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 34 with LmtpAddress

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

the class NotifyMailtoTest method testNotify_mimeVariables.

/**
 * Tests a sieve rule with mime variable parameters.
 */
@Test
public void testNotify_mimeVariables() {
    String filterScript = "require [\"enotify\", \"tag\", \"variables\", \"envelope\"];\n" + "if envelope :matches [\"To\"]     \"*\" {set \"rcptto\"        \"${1}\";}\n" + "if envelope :matches [\"From\"]   \"*\" {set \"mailfrom\"      \"${1}\";}\n" + "if anyof(not envelope :is [\"From\"] \"\") {\n" + "  set \"subjectparam\" \"Notification\";\n" + "  set \"bodyparam\" text:\r\n" + "Hello ${rcptto},\n" + "A new massage has arrived.\n" + "Sent: ${Date}\n" + "From: ${From}\n" + "Subject: ${Subject}\r\n" + ".\r\n" + ";\n" + "  notify :message \"${subjectparam}\"\n" + "         :from \"${rcptto}\"\n" + "         \"mailto:test2@zimbra.com?body=${bodyparam}\";\n" + "}";
    String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Date: Tue, 11 Oct 2016 12:01:37 +0900\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: foo@example.com, baz@example.com\n" + "cc: qux@example.com\n";
    String expectedNotifyMsg = "Hello test1@zimbra.com,\n" + "A new massage has arrived.\n" + "Sent: Tue, 11 Oct 2016 12:01:37  0900\n" + "From: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out";
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
        RuleManager.clearCachedRules(acct1);
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScript);
        acct1.setMail("test1@zimbra.com");
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // The triggered message should be delivered to the target mailbox
        Assert.assertEquals(1, ids.size());
        // Notification message should be delivered to mailto addresses
        Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message notifyMsg = mbox2.getMessageById(null, item);
        // Verify the subject line of the notification message
        Assert.assertEquals("Notification", notifyMsg.getSubject());
        // Verify the from header of the notification message
        String[] headers = notifyMsg.getMimeMessage().getHeader("from");
        Assert.assertTrue(headers.length == 1);
        Assert.assertEquals("test1@zimbra.com", headers[0]);
        // Verify the message body of the notification message
        MimeMessage mm = notifyMsg.getMimeMessage();
        List<MPartInfo> parts = Mime.getParts(mm);
        Set<MPartInfo> bodies = Mime.getBody(parts, false);
        Assert.assertEquals(1, bodies.size());
        for (MPartInfo body : bodies) {
            Object mimeContent = body.getMimePart().getContent();
            Assert.assertTrue(mimeContent instanceof String);
            String deliveredNotifyMsg = (String) mimeContent;
            Assert.assertEquals(expectedNotifyMsg, deliveredNotifyMsg);
        }
    } catch (Exception e) {
        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) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) MPartInfo(com.zimbra.cs.mime.MPartInfo) Mailbox(com.zimbra.cs.mailbox.Mailbox) MimeMessage(javax.mail.internet.MimeMessage) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 35 with LmtpAddress

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

the class NotifyMailtoTest method testNotifyMailtoWithSpaceInHeaderName.

@Test
public void testNotifyMailtoWithSpaceInHeaderName() {
    String sampleMsg = "Auto-Submitted: \"no\"\n" + "from: xyz@example.com\n" + "Subject: [acme-users] [fwd] version 1.0 is out\n" + "to: foo@example.com, baz@example.com\n" + "cc: qux@example.com\n";
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
        Account acct3 = Provisioning.getInstance().get(Key.AccountBy.name, "test3@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
        Mailbox mbox3 = MailboxManager.getInstance().getMailboxByAccount(acct3);
        RuleManager.clearCachedRules(acct1);
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScriptWithSpaceInHeaderName);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // The triggered message should be delivered to the target mailbox
        Assert.assertEquals(1, ids.size());
        // Notification message should be delivered to mailto and to= addresses
        Assert.assertTrue(mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).isEmpty());
    } 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)

Aggregations

LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)55 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)53 Account (com.zimbra.cs.account.Account)51 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)51 Mailbox (com.zimbra.cs.mailbox.Mailbox)51 OperationContext (com.zimbra.cs.mailbox.OperationContext)51 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)51 ItemId (com.zimbra.cs.service.util.ItemId)46 Message (com.zimbra.cs.mailbox.Message)43 Test (org.junit.Test)41 MimeMessage (javax.mail.internet.MimeMessage)9 Provisioning (com.zimbra.cs.account.Provisioning)8 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)8 SyntaxException (org.apache.jsieve.exception.SyntaxException)8 Ignore (org.junit.Ignore)5 ServiceException (com.zimbra.common.service.ServiceException)3 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)2 ErejectException (com.zimbra.cs.filter.jsieve.ErejectException)2 MPartInfo (com.zimbra.cs.mime.MPartInfo)2 List (java.util.List)2