Search in sources :

Example 11 with MPartInfo

use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.

the class DefangFilterTest method getHtmlBody.

/**
     * Utility method that gets the html body part from a mime message and returns its input stream
     * @param fileName The name of the email file to load from the unit test data dir
     * @return The input stream for the html body if successful
     * @throws Exception
     */
private InputStream getHtmlBody(String fileName) throws Exception {
    // Get an input stream of a test pdf to test with
    InputStream inputStream = new FileInputStream(EMAIL_BASE_DIR + fileName);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ByteUtil.copy(inputStream, true, baos, true);
    ParsedMessage msg = new ParsedMessage(baos.toByteArray(), false);
    Set<MPartInfo> bodyparts = Mime.getBody(msg.getMessageParts(), true);
    InputStream htmlStream = null;
    for (MPartInfo body : bodyparts) {
        if (body.getContentType().contains("html")) {
            htmlStream = body.getMimePart().getInputStream();
        }
    }
    return htmlStream;
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FileInputStream(java.io.FileInputStream)

Example 12 with MPartInfo

use of com.zimbra.cs.mime.MPartInfo 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)

Example 13 with MPartInfo

use of com.zimbra.cs.mime.MPartInfo 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 14 with MPartInfo

use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.

the class FeedManagerTest method socialcastAtomFeed.

@Test
public void socialcastAtomFeed() throws Exception {
    long lastModified = 0;
    String expectedCharset = MimeConstants.P_CHARSET_UTF8;
    BufferedInputStream content = new BufferedInputStream(getClass().getResourceAsStream("socialcastAtomFeed.xml"));
    RemoteDataInfo rdi = new RemoteDataInfo(HttpStatus.OK_200, 0, content, expectedCharset, lastModified);
    SubscriptionData<?> subsData = FeedManager.retrieveRemoteDatasource(null, rdi, null);
    List<?> subs = subsData.getItems();
    Assert.assertNotNull("List of subscriptions", subs);
    Assert.assertEquals("Number of items", 1, subs.size());
    for (Object obj : subs) {
        if (obj instanceof ParsedMessage) {
            ParsedMessage pm = (ParsedMessage) obj;
            List<MPartInfo> parts = pm.getMessageParts();
            Assert.assertEquals("Number of message parts", 1, parts.size());
            String msgContent = streamToString(parts.get(0).getMimePart().getInputStream(), Charsets.UTF_8);
            Assert.assertTrue("Text from inside <div>", msgContent.indexOf("Congratulations for passing!") > 0);
            Assert.assertTrue("Article reference", msgContent.indexOf("https://pink.socialcast.com/messages/15629747-active-learner-thanks-to-cccc") > 0);
        } else {
            Assert.fail("Expecting a ParsedMessage where is " + obj.getClass().getName());
        }
    }
}
Also used : MPartInfo(com.zimbra.cs.mime.MPartInfo) BufferedInputStream(java.io.BufferedInputStream) RemoteDataInfo(com.zimbra.cs.service.FeedManager.RemoteDataInfo) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Test(org.junit.Test)

Example 15 with MPartInfo

use of com.zimbra.cs.mime.MPartInfo in project zm-mailbox by Zimbra.

the class FilterUtil method getVarsMap.

@VisibleForTesting
static Map<String, String> getVarsMap(Mailbox mailbox, ParsedMessage parsedMessage, MimeMessage mimeMessage) throws MessagingException, ServiceException {
    Map<String, String> vars = new HashMap<String, String>() {

        @Override
        public String get(Object key) {
            return super.get(((String) key).toLowerCase());
        }
    };
    Enumeration enumeration = mimeMessage.getAllHeaders();
    while (enumeration.hasMoreElements()) {
        Header header = (Header) enumeration.nextElement();
        vars.put(header.getName().toLowerCase(), ZInternetHeader.decode(mimeMessage.getHeader(header.getName(), ",")));
    }
    // raw subject could be encoded, so get the parsed subject
    vars.put("subject", parsedMessage.getSubject());
    MPartInfo bodyPart = Mime.getTextBody(parsedMessage.getMessageParts(), false);
    if (bodyPart != null) {
        try {
            vars.put("body", Mime.getStringContent(bodyPart.getMimePart(), mailbox.getAccount().getPrefMailDefaultCharset()));
        } catch (IOException e) {
            ZimbraLog.filter.warn("Error in reading text body", e);
        }
    }
    return vars;
}
Also used : Enumeration(java.util.Enumeration) MPartInfo(com.zimbra.cs.mime.MPartInfo) ZInternetHeader(com.zimbra.common.zmime.ZInternetHeader) Header(javax.mail.Header) HashMap(java.util.HashMap) IOException(java.io.IOException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

MPartInfo (com.zimbra.cs.mime.MPartInfo)19 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)8 IOException (java.io.IOException)8 MimeMessage (javax.mail.internet.MimeMessage)8 MessagingException (javax.mail.MessagingException)6 Account (com.zimbra.cs.account.Account)5 Message (com.zimbra.cs.mailbox.Message)5 InputStream (java.io.InputStream)5 MimePart (javax.mail.internet.MimePart)5 Test (org.junit.Test)5 Element (com.zimbra.common.soap.Element)4 Mailbox (com.zimbra.cs.mailbox.Mailbox)4 OperationContext (com.zimbra.cs.mailbox.OperationContext)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ServiceException (com.zimbra.common.service.ServiceException)3 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)3 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)2 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)2 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)2 RemoteDataInfo (com.zimbra.cs.service.FeedManager.RemoteDataInfo)2