Search in sources :

Example 51 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\"];\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 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" + ".\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 52 with LmtpAddress

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

the class NotifyMailtoTest method testNotOverridableParams.

/**
     * Verify the "header fields of the notification message
     * that are normally related to an individual new message
     * (such as "Message-ID" and "Date") are generated for the
     * notification message in the normal manner, and MUST NOT
     * be copied from the triggering message" (RFC 5436-2.7)
     */
@Test
public void testNotOverridableParams() {
    String sampleMsg = "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_MsgIDDate);
        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("notifybody", notifyMsg.getFragment());
        String[] headers = notifyMsg.getMimeMessage().getHeader("Message-ID");
        Assert.assertTrue(headers.length == 1);
        Assert.assertNotSame("dummymessageid", headers[0]);
        headers = notifyMsg.getMimeMessage().getHeader("Date");
        Assert.assertTrue(headers.length == 1);
        Assert.assertNotSame("dummydate", headers[0]);
    } catch (Exception e) {
        fail("No exception should be thrown:" + e);
    }
}
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 53 with LmtpAddress

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

the class HeaderTest 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 54 with LmtpAddress

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

the class ZimbraMailAdapter method getMatchingEnvelope.

@Override
public List<String> getMatchingEnvelope(String name) throws SieveMailException {
    List<String> result = Lists.newArrayListWithExpectedSize(2);
    if (envelope == null) {
        return result;
    }
    switch(name.toLowerCase()) {
        case "to":
            /* RFC 5228 5.4. Test envelope
             * ---
             * If the SMTP transaction involved several RCPT commands, only the data
             * from the RCPT command that caused delivery to this user is available
             * in the "to" part of the envelope.
             * ---
             * Return only the address (primary and alias) who is currently being processed.
             */
            List<LmtpAddress> recipients = envelope.getRecipients();
            try {
                String myaddress = mailbox.getAccount().getMail();
                if (null != myaddress && !myaddress.isEmpty()) {
                    for (LmtpAddress recipient : recipients) {
                        if (myaddress.toUpperCase().startsWith(recipient.getEmailAddress().toUpperCase())) {
                            result.add(recipient.getEmailAddress());
                        }
                    }
                }
                String[] myaliases = mailbox.getAccount().getMailAlias();
                if (myaliases.length > 0) {
                    for (String alias : myaliases) {
                        for (LmtpAddress recipient : recipients) {
                            if (alias.toUpperCase().startsWith(recipient.getEmailAddress().toUpperCase())) {
                                result.add(recipient.getEmailAddress());
                            }
                        }
                    }
                }
            } catch (ServiceException e) {
            // nothing to do with this exception. Just return an empty list
            }
            break;
        case "from":
            LmtpAddress sender = envelope.getSender();
            result.add(sender.getEmailAddress());
            break;
    }
    return result;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress)

Aggregations

LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)54 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 Account (com.zimbra.cs.account.Account)50 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)50 Mailbox (com.zimbra.cs.mailbox.Mailbox)50 OperationContext (com.zimbra.cs.mailbox.OperationContext)50 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)50 ItemId (com.zimbra.cs.service.util.ItemId)45 Message (com.zimbra.cs.mailbox.Message)42 Test (org.junit.Test)41 MimeMessage (javax.mail.internet.MimeMessage)9 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 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