Search in sources :

Example 51 with Message

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

the class SetVariableTest method testSetMatchVarAndUseInHeaderForAddress.

@Test
public void testSetMatchVarAndUseInHeaderForAddress() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "if address :comparator \"i;ascii-casemap\" :matches \"To\" \"coyote@**.com\"{\n" + "  tag \"${2}\";\n" + "}";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@in.telligent.com\n" + "To: coyote@ACME.Example.COM\n" + "Subject: hello version 1.0 is out\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());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("ACME.Example", 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) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) ItemId(com.zimbra.cs.service.util.ItemId) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Example 52 with Message

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

the class SetVariableTest method testVariables_Header_HeaderNames.

@Test
public void testVariables_Header_HeaderNames() {
    try {
        String sampleMsg = "Received: from edge01e.zimbra.com ([127.0.0.1])\n" + "\tby localhost (edge01e.zimbra.com [127.0.0.1]) (amavisd-new, port 10032)\n" + "\twith ESMTP id DN6rfD1RkHD7; Fri, 24 Jun 2016 01:45:31 -0400 (EDT)\n" + "Received: from localhost (localhost [127.0.0.1])\n" + "\tby edge01e.zimbra.com (Postfix) with ESMTP id 9245B13575C;\n" + "\tFri, 24 Jun 2016 01:45:31 -0400 (EDT)\n" + "x-priority: 1\n" + "from: tim@zimbra.com\n" + "Subject: =?ISO-2022-JP?B?GyRCJDMkcyRLJEEkTxsoQg==?=\n" + "to: test1@zimbra.com, test2@zimbra.com\n";
        String filterScript = "require [\"variables\", \"tag\"];\n" + "set \"header_name\" \"x-priority\";" + "if header :contains [\"${header_name}\"] [\"\"] { tag \"zimbra\"; }";
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<tim@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient1 = new LmtpAddress("<test1@zimbra.com>", null, null);
        LmtpAddress recipient2 = new LmtpAddress("<test2@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient1);
        env.addLocalRecipient(recipient2);
        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("zimbra", 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) 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Example 53 with Message

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

the class NotifyMailtoTest method testBackslashEscapeSequence.

@Test
public void testBackslashEscapeSequence() {
    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 = "require [\"enotify\"];\n" + "notify :from \"\\\"tes\\\\\\\\t1\\\"@zimbra.com\"" + ":message \"sample me\\\\ssa\\\"ge4\" " + "\"mailto:\\\"tes\\\\\\\\t2\\\"@zimbra.com?body=sample_message\";";
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "\"tes\\\\t2\"@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);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage(sampleMsg.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("sample me\\ssa\"ge4", notifyMsg.getSubject());
        Assert.assertEquals("<\"tes\\\\t1\"@zimbra.com>", notifyMsg.getSender());
        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 54 with Message

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

the class NotifyMailtoTest method testNotifyMethodCapability_OnlineMaybe.

/**
 * Tests the notify_method_capability ("online"/"maybe")
 */
@Test
public void testNotifyMethodCapability_OnlineMaybe() {
    String filterScript = "require [\"enotify\", \"tag\"];\n" + "if notify_method_capability\n" + "     \"mailto:test2@zimbra.com\"\n" + "     \"Online\"\n" + "     \"maybe\" { \n" + "  tag \"notify_method_capability\";\n" + "}";
    try {
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct1.setMailSieveScript(filterScript);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox1), mbox1, new ParsedMessage("From: test1@zimbra.com".getBytes(), false), 0, acct1.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // ZCS implements the RFC 5436 so that it returns true when 'notify_method_capability'
        // checkes whether the "Online" status is "maybe".
        Assert.assertEquals(1, ids.size());
        Message msg = mbox1.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("notify_method_capability", 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) 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 55 with Message

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

the class NotifyMailtoTest method testEmptyMessageBody.

@Test
public void testEmptyMessageBody() {
    String sampleMsg = "Auto-Submitted: \"no\"\n" + "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_EmptyBodyParameter);
        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("", 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());
        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) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) 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