Search in sources :

Example 41 with LmtpAddress

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

the class NotifyMailtoTest method testNotify_variable.

/**
 * Tests a sieve rule with variable parameters.
 */
@Test
public void testNotify_variable() {
    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 header   :matches  \"Date\"    \"*\" {set \"dateheader\"    \"${1}\";}\n" + "if header   :matches  \"From\"    \"*\" {set \"fromheader\"    \"${1}\";}\n" + "if header   :matches  \"Subject\" \"*\" {set \"subjectheader\" \"${1}\";}\n" + "if header   :matches  \"X-Header-With-Control-Chars\" \"*\" {set \"xheader\" \"${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: ${dateheader}\n" + "From: ${fromheader}\n" + "Subject: ${subjectheader}\r\n" + "X-Header-With-Control-Chars: ${xheader}\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" + "X-Header-With-Control-Chars: =?utf-8?B?dGVzdCBIVAkgVlQLIEVUWAMgQkVMByBCUwggbnVsbAAgYWZ0ZXIgbnVsbA0K?=\n";
    String expectedNotifyMsg = "Hello test1@zimbra.com,\r\n" + "A new massage has arrived.\r\n" + "Sent: Tue, 11 Oct 2016 12:01:37  0900\r\n" + "From: xyz@example.com\r\n" + "Subject: [acme-users] [fwd] version 1.0 is out\r\n" + "X-Header-With-Control-Chars: test HT\t VT\u000b ETX\u0003 BEL\u0007 BS\u0008 null\u0000 after null";
    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 42 with LmtpAddress

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

the class RejectTest method testEmptyEnvelopeFrom.

/*
     * MDN should be sent to the return-path from (testRej2@zimbra.com)
     */
@Test
public void testEmptyEnvelopeFrom() {
    String sampleMsg = "Return-Path: testRej2@zimbra.com\n" + sampleBaseMsg;
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "testRej@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "testRej2@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("<testRej@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 43 with LmtpAddress

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

the class RejectTest method testEmptyEnvelopeFromAndEmptyReturnPath.

/*
     * MDN should not to be sent, and the message should be delivered to testRej@zimbra.com
     *
     * The following exception will be thrown:
     * javax.mail.MessagingException: Neither 'envelope from' nor 'Return-Path' specified. Can't locate the address to reject to (No MDN sent)
     */
@Test
public void testEmptyEnvelopeFromAndEmptyReturnPath() {
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "testRej@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "testRej2@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("<testRej@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScript);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(1, ids.size());
        List<Integer> items = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE);
        Assert.assertEquals(null, items);
    } 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) 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)

Example 44 with LmtpAddress

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

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

the class ErejectTest method test.

/*
     * applyRulesToIncomingMessage() should throw an exception to cancel the message delivery.
     * No message is delivered.
     *
     * The following error will be logged:
     * ERROR - Evaluation failed. Reason: 'ereject' action refuses delivery of a message. Sieve rule evaluation is cancelled
     */
@Test
public void test() {
    Account acct1 = null;
    Mailbox mbox1 = null;
    boolean isPassed = false;
    try {
        acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<test2@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        acct1.setMailSieveScript(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
    } catch (DeliveryServiceException e) {
        if (e.getCause() instanceof ErejectException) {
            try {
                List<Integer> items = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE);
                Assert.assertEquals(null, items);
                isPassed = true;
            } catch (Exception ex) {
                fail("No exception should be thrown: " + ex.getMessage());
            }
        } else {
            fail("No exception other than DeliveryServiceException/ErejectException should be thrown: " + e.getMessage());
        }
    } catch (Exception e) {
        fail("No exception should be thrown: " + e.getMessage());
    }
    if (!isPassed) {
        fail("DeliveryServiceException/ErejectException should have been thrown, but no exception is 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) List(java.util.List) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) ServiceException(com.zimbra.common.service.ServiceException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) 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