Search in sources :

Example 56 with Message

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

the class NotifyMailtoTest method testPercentEncodingVariable_WithoutEncodeurl_BadEncodedString.

@Test
public void testPercentEncodingVariable_WithoutEncodeurl_BadEncodedString() {
    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";
    String filterScript_WithoutPercentEncodingVariable = "require [\"enotify\", \"variables\"];\n" + "set \"body_param\" \"Bad%body&evil=evilbody\";\n" + "notify \"mailto:test2@zimbra.com?body=${body_param}\";";
    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);
        acct1.setMail("test1@zimbra.com");
        RuleManager.clearCachedRules(acct1);
        acct1.setMailSieveScript(filterScript_WithoutPercentEncodingVariable);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.getBytes(), false), 0, acct1.getName(), 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);
        // Since the message body was originally incorrectly percent-encoded,
        // URLDecoder is failed to decode the message contents. Keep using the
        // original message string.
        Assert.assertEquals("Bad%body", notifyMsg.getFragment());
        String[] headers = notifyMsg.getMimeMessage().getHeader("evil");
        Assert.assertTrue(headers.length == 1);
        Assert.assertEquals("evilbody", headers[0]);
        RuleManager.clearCachedRules(acct1);
    } 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) MimeMessage(javax.mail.internet.MimeMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test)

Example 57 with Message

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

the class NotifyMailtoTest method test.

/**
 * Tests 'notify' filter rule:
 *  - Set :message (Subject field), :from (From field) and mechanism (mailto:...)
 *  - Body of the notification message contains non-ascii characters
 *  - Additional header fields are specified via mechanism (maito:) parameter
 */
@Test
public void test() {
    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(filterScript1);
        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("新しいメールが届きました。 You've got a mail. Chao!", 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());
        boolean header1 = false;
        boolean header2 = false;
        boolean header3 = false;
        boolean header4 = false;
        for (Enumeration<Header> e = notifyMsg.getMimeMessage().getAllHeaders(); e.hasMoreElements(); ) {
            Header temp = e.nextElement();
            if ("X-HEADER1".equals(temp.getName())) {
                header1 = true;
            }
            if ("X-header2".equals(temp.getName())) {
                header2 = true;
            }
            if ("X-HeAdEr3".equals(temp.getName())) {
                header3 = true;
            }
            if ("X-hEader4".equals(temp.getName())) {
                header4 = true;
            }
        }
        Assert.assertTrue(header1);
        Assert.assertTrue(header2);
        Assert.assertTrue(header3);
        Assert.assertTrue(header4);
        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) 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 58 with Message

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

the class RedirectCopyTest method testCopyRedirect.

@Test
public void testCopyRedirect() {
    String filterScript = "require [\"copy\", \"redirect\"];\n" + "if header :contains \"Subject\" \"Test\" { redirect :copy \"test3@zimbra.com\"; }";
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
        Account account2 = Provisioning.getInstance().get(Key.AccountBy.name, "test3@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        Mailbox mbox2 = MailboxManager.getInstance().getMailboxByAccount(account2);
        account.setMailSieveScript(filterScript);
        String raw = "From: test1@zimbra.com\n" + "To: test2@zimbra.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(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(1, ids.size());
        Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message notifyMsg = mbox2.getMessageById(null, item);
        Assert.assertEquals("Hello World", notifyMsg.getFragment());
        Assert.assertEquals(2, notifyMsg.getFolderId());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.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) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test)

Example 59 with Message

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

the class NotifyTest method filterValidToField.

@Test
public void filterValidToField() {
    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);
        String filterScript = "require [\"enotify\"];if anyof (true) { notify \"test2@zimbra.com\" \"\" \"Hello World\"" + "[\"*\"];" + "    keep;" + "}";
        acct1.setMailSieveScript(filterScript);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage("To: test@zimbra.com".getBytes(), false), 0, acct1.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Assert.assertEquals(1, ids.size());
        Integer item = mbox2.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message notifyMsg = mbox2.getMessageById(null, item);
        Assert.assertEquals("Hello World", notifyMsg.getFragment());
        Assert.assertEquals("text/plain; charset=us-ascii", notifyMsg.getMimeMessage().getContentType());
    } 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) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId) Test(org.junit.Test)

Example 60 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderWithXSpamScore.

/*
     * Replace header with numeric comparator :count
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderWithXSpamScore() {
    try {
        String filterScript = "require [\"editheader\", \"variables\", \"relational\", \"comparator-i;ascii-numeric\"];\n" + "if anyof(header :value \"ge\" :comparator \"i;ascii-numeric\" [\"X-Spam-Score\"] [\"80\"]) {" + "      if exists \"Subject\" {" + "        replaceheader :newvalue \"[SPAM]${1}\" :matches \"Subject\" \"*\";" + "      } else {" + "        addheader :last \"Subject\" \"[SPAM]\";" + "      }" + "    }";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setSieveEditHeaderEnabled(true);
        acct1.setAdminSieveScriptBefore(filterScript);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleBaseMsg.getBytes(), false), 0, acct1.getName(), null, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox1.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        Message message = mbox1.getMessageById(null, itemId);
        String subjectValue = "";
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("Subject".equals(header.getName())) {
                subjectValue = header.getValue();
            }
        }
        Assert.assertEquals("[SPAM]example", subjectValue);
    } 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) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Header(javax.mail.Header) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) SyntaxException(org.apache.jsieve.exception.SyntaxException) Test(org.junit.Test)

Aggregations

Message (com.zimbra.cs.mailbox.Message)442 Mailbox (com.zimbra.cs.mailbox.Mailbox)375 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)358 Test (org.junit.Test)335 Account (com.zimbra.cs.account.Account)315 OperationContext (com.zimbra.cs.mailbox.OperationContext)302 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)281 ItemId (com.zimbra.cs.service.util.ItemId)211 MimeMessage (javax.mail.internet.MimeMessage)123 SyntaxException (org.apache.jsieve.exception.SyntaxException)87 Header (javax.mail.Header)83 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)63 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)53 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)46 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)43 ServiceException (com.zimbra.common.service.ServiceException)41 Element (com.zimbra.common.soap.Element)41 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)36 Folder (com.zimbra.cs.mailbox.Folder)32 ZMailbox (com.zimbra.client.ZMailbox)26