Search in sources :

Example 31 with LmtpEnvelope

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

the class RejectTest method testEmptyEnvelopeFrom.

/*
     * MDN should be sent to the return-path from (test2@zimbra.com)
     */
@Test
public void testEmptyEnvelopeFrom() {
    String sampleMsg = "Return-Path: test2@zimbra.com\n" + sampleBaseMsg;
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@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("<>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScript);
        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);
        Assert.assertEquals(0, ids.size());
        Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message mdnMsg = mbox2.getMessageById(null, item);
        String ctStr = mdnMsg.getMimeMessage().getContentType().toLowerCase();
        boolean isReport = ctStr.startsWith("multipart/report;");
        boolean isMdn = ctStr.indexOf("report-type=disposition-notification") < 0 ? false : true;
        Assert.assertEquals(isReport & isMdn, true);
    } 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) 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 32 with LmtpEnvelope

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

the class RelationalExtensionTest 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) Message(com.zimbra.cs.mailbox.Message) 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 33 with LmtpEnvelope

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

the class RelationalExtensionTest method setEnvelopeInfo.

private LmtpEnvelope setEnvelopeInfo() {
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<abc@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient1 = new LmtpAddress("<xyz@zimbra.com>", null, null);
    LmtpAddress recipient2 = new LmtpAddress("<uvw@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient1);
    env.addLocalRecipient(recipient2);
    return env;
}
Also used : LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope)

Example 34 with LmtpEnvelope

use of com.zimbra.cs.lmtpserver.LmtpEnvelope 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 35 with LmtpEnvelope

use of com.zimbra.cs.lmtpserver.LmtpEnvelope 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\"];\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)

Aggregations

LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)54 Account (com.zimbra.cs.account.Account)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)52 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)52 Mailbox (com.zimbra.cs.mailbox.Mailbox)52 OperationContext (com.zimbra.cs.mailbox.OperationContext)52 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)52 ItemId (com.zimbra.cs.service.util.ItemId)47 Message (com.zimbra.cs.mailbox.Message)44 Test (org.junit.Test)41 MimeMessage (javax.mail.internet.MimeMessage)10 MockProvisioning (com.zimbra.cs.account.MockProvisioning)8 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)4 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)2 ServiceException (com.zimbra.common.service.ServiceException)2 ErejectException (com.zimbra.cs.filter.jsieve.ErejectException)2 MPartInfo (com.zimbra.cs.mime.MPartInfo)2