Search in sources :

Example 26 with LmtpAddress

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

the class EnvelopeTest method testVariable.

public void testVariable(String filterScript) {
    /*
         * Checks if numeric variable works
         */
    String triggeringMsg = "from: message_header_from@example.com\n" + "to: message_header_to@zimbra.com\n" + "Subject: Example\n";
    String[] expectedTagName = { "env_envelope_from@example.com", "env_testEnv@zimbra.com", "adr_message_header_from@example.com", "adr_message_header_to@zimbra.com", "hdr_message_header_from@example.com", "hdr_message_header_to@zimbra.com" };
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<envelope_from@example.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<testEnv@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Account account = Provisioning.getInstance().getAccountByName("testEnv@zimbra.com");
        Map<String, Object> attrs = Maps.newHashMap();
        attrs = Maps.newHashMap();
        Provisioning.getInstance().getServer(account).modify(attrs);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScript);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(triggeringMsg.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());
        String[] tags = msg.getTags();
        Assert.assertTrue(tags != null);
        Assert.assertEquals(expectedTagName.length, tags.length);
        for (int i = 0; i < expectedTagName.length; i++) {
            Assert.assertEquals(expectedTagName[i], tags[i]);
        }
    } 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) 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)

Example 27 with LmtpAddress

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

the class EnvelopeTest method testFrom.

@Test
public void testFrom() {
    // RFC 5228 5.4. Test envelope example
    String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"from\" \"tim@example.com\" {\n" + "discard;\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<testEnv@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Account account = Provisioning.getInstance().getAccountByName("testEnv@zimbra.com");
        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(0, ids.size());
    } catch (Exception e) {
        e.printStackTrace();
        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) 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 28 with LmtpAddress

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

the class EscapeSequencesTest method doTestHeaderEscapePattern.

/*
     * MAIL FROM: <user123@zimbra.com>
     * RCPT TO: <test1@zimbra.com>
     * DATA
     * To: user123@zimbra.com
     * From: sender@zimbra.com
     * Subject: test\123
     * X-Header-0BackSlash:  testSample
     * X-Header-1BaskSlash:  test\Sample
     * X-Header-2BackSlash:  test\\Sample
     * X-Header-DoubleQuote: test"Sample
     */
public void doTestHeaderEscapePattern(String filterScript) {
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<user123@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<test1@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test1@zimbra.com");
        RuleManager.clearCachedRules(account);
        account.setMailSieveScript(filterScript);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(triggeringMsg.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(1, msg.getTags().length);
        Assert.assertEquals("list", msg.getTags()[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) 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)

Example 29 with LmtpAddress

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

the class ErejectTest method testThatSenderRcdUnDeliveredEmail.

/*
     * 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 testThatSenderRcdUnDeliveredEmail() {
    Account acct1 = null;
    Mailbox mbox1 = null;
    Account acct2 = null;
    Mailbox mbox2 = null;
    try {
        acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct1);
        RuleManager.clearCachedRules(acct1);
        acct2 = Provisioning.getInstance().get(Key.AccountBy.name, "test2@zimbra.com");
        mbox2 = MailboxManager.getInstance().getMailboxByAccount(acct2);
        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);
            } catch (Exception ex) {
                ex.printStackTrace();
                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());
    }
}
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)

Example 30 with LmtpAddress

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

the class SetVariableTest method testSetMatchVarEmptyMatch.

/*
     * Verify that the Matched Variabes keeps strings from the most
     * recently evaluated successful match of type ":matches".
     * In this test script, "the most recently evaluated successful" value
     * for the replaceheader command is the value of "Subject".  And if
     * the value of the Subject is empty, the ${1} should be replaced by
     * the empty string.
     */
@Ignore
public void testSetMatchVarEmptyMatch() {
    try {
        filterScript = "require [\"variables\", \"editheader\"];\n" + /* Replaceheader */
        "if header :matches \"Date\" \"*\" { \r\n" + "  set \"dateheader\" \"${1}\";\r\n" + "}\r\n" + "if exists \"Subject\" {\r\n" + "  replaceheader :newvalue \"[Replace Subject]${1}\" :matches \"Subject\" \"*\";\r\n" + "}\n" + /* Test header */
        "if header :matches \"Date\" \"*\" { \r\n" + "  set \"dateheader\" \"${1}\";\r\n" + "}\r\n" + "if header :matches :comparator \"i;ascii-casemap\" [\"X-Header\"] \"*\" {\r\n" + "  tag \"tag1${1}\";\n" + "}\n" + /* Test envelope */
        "if address :matches :comparator \"i;ascii-casemap\" [\"To\"] \"*t@zimbra.com\" { \r\n" + "  set \"toheader\" \"${1}\";\r\n" + "}\r\n" + "if envelope :matches :comparator \"i;ascii-casemap\" [\"From\"] \"*\" {\r\n" + "  tag \"tag1${1}\";\n" + "}\n" + /* Test string */
        "if header :matches \"Date\" \"*\" { \r\n" + "  set \"dateheader\" \"${1}\";\r\n" + "}\r\n" + "if string :matches :comparator \"i;ascii-casemap\" [\"X-String\"] \"*\" {\r\n" + "  tag \"tag2${1}\";\n" + "}\n";
        String raw = "From: \n" + "To: coyote@ACME.Example.COM\n" + "Date: Thu, 08 Dec 2016 07:10:48 +0900\n" + "Subject: \n" + "X-String: \n" + "X-Header: \n" + "\n" + "Hello world.";
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        Map<String, Object> attrs = Maps.newHashMap();
        attrs = Maps.newHashMap();
        Provisioning.getInstance().getServer(account).modify(attrs);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        RuleManager.clearCachedRules(account);
        account.setMailSieveScript(filterScript);
        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);
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), env, new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        Integer itemId = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        String value = "";
        int totalNewHeader = 0;
        Message msg = mbox.getMessageById(null, itemId);
        Assert.assertEquals("[Replace Subject]", msg.getSubject());
        String[] tags = msg.getTags();
        Assert.assertEquals(2, tags.length);
        Assert.assertEquals("tag1", tags[0]);
        Assert.assertEquals("tag2X-String", tags[1]);
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Mailbox(com.zimbra.cs.mailbox.Mailbox) LmtpAddress(com.zimbra.cs.lmtpserver.LmtpAddress) LmtpEnvelope(com.zimbra.cs.lmtpserver.LmtpEnvelope) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Ignore(org.junit.Ignore)

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