Search in sources :

Example 21 with LmtpAddress

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

the class EnvelopeTest method testTo_BccTo.

@Test
public void testTo_BccTo() {
    /*
         * RFC 5228 5.4.
         * ----
         * 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.
         * ----
         * The bcc recipient (who is specified by RCPT command but not on the
         * message header) should not be matched by the 'envelope' test.
         */
    String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"to\" \"bccTo@zimbra.com\" {\n" + "  tag \"Bcc To\";\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
    env.setSender(sender);
    // To address
    LmtpAddress recipient = new LmtpAddress("<testEnv@zimbra.com>", null, null);
    env.addLocalRecipient(recipient);
    // Bcc address
    recipient = new LmtpAddress("<bccTo@zimbra.com>", null, null);
    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(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals(null, 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) Test(org.junit.Test)

Example 22 with LmtpAddress

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

the class EnvelopeTest method testMailFromSingleQuote.

@Test
public void testMailFromSingleQuote() {
    String filterScript = "require \"envelope\";\n" + "if envelope :all :is \"from\" \"ti'm@example.com\" {\n" + "  tag \"From\";\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<ti'm@example.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<xyz@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(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals("From", 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) Test(org.junit.Test)

Example 23 with LmtpAddress

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

the class EnvelopeTest method testInvalidHeaderName.

@Test
public void testInvalidHeaderName() {
    String filterScript = "require  \"envelope\";\n" + "if anyof envelope :matches \"to123\" \"t1@zimbra.com\" {\n" + "    fileinto \"Junk\";\n" + "}\n";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
        account.setMail("xyz@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(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertEquals(Mailbox.ID_FOLDER_INBOX, msg.getFolderId());
    } 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) Provisioning(com.zimbra.cs.account.Provisioning) 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 24 with LmtpAddress

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

the class EnvelopeTest method testTo_Alias.

@Test
public void testTo_Alias() {
    String filterScript = "require [\"variables\", \"envelope\", \"relational\", \"comparator-i;ascii-numeric\"];\n" + "set \"rcptto\" \"unknown\";\n" + "if envelope :all :matches \"to\" \"*\" {\n" + "  set \"rcptto\" \"${1}\";\n" + "  tag \"${rcptto}\";\n" + "}\n" + "if envelope :all :matches \"to\" \"alias1*\" {\n" + "  tag \"${1}\";\n" + "}\n" + "if envelope :all :matches \"to\" \"alias2*\" {\n" + "  tag \"bad\";\n" + "}\n" + "if envelope :count \"eq\" :comparator \"i;ascii-numeric\" \"to\" \"1\" {" + "  tag \"1\";\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<tim@example.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<alias1@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = prov.createAccount("original1@zimbra.com", "secret", new HashMap<String, Object>());
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setAdminSieveScriptBefore(filterScript);
        account.setMail("original1@zimbra.com");
        String[] alias = { "alias1@zimbra.com", "alias2@zimbra.com" };
        account.setMailAlias(alias);
        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());
        String[] tags = msg.getTags();
        Assert.assertTrue(tags != null);
        Assert.assertEquals(3, tags.length);
        Assert.assertEquals("alias1@zimbra.com", tags[0]);
        Assert.assertEquals("@zimbra.com", tags[1]);
        Assert.assertEquals("1", tags[2]);
    } 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) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) 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 25 with LmtpAddress

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

the class EnvelopeTest method testAllDomainLocalIs.

@Test
public void testAllDomainLocalIs() {
    String filterScript = "require  [\"envelope\", \"tag\"];\n" + "if envelope :domain :is \"to\" \"zimbra.com\" {\n" + "    tag \"is-domain\";\n" + "}\n" + "if envelope :localpart :is \"to\" \"xyz\" {\n" + "    tag \"is-local\";\n" + "}\n" + "if envelope :all :is \"to\" \"xyz@zimbra.com\" {" + "    tag \"is-all\";\n" + "}";
    LmtpEnvelope env = new LmtpEnvelope();
    LmtpAddress sender = new LmtpAddress("<t1@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
    LmtpAddress recipient = new LmtpAddress("<xyz@zimbra.com>", null, null);
    env.setSender(sender);
    env.addLocalRecipient(recipient);
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
        account.setMail("xyz@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(1, ids.size());
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        String[] tags = msg.getTags();
        Assert.assertTrue(tags != null);
        Assert.assertEquals(3, tags.length);
        Assert.assertEquals("is-domain", tags[0]);
        Assert.assertEquals("is-local", tags[1]);
        Assert.assertEquals("is-all", tags[2]);
    } 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) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ItemId(com.zimbra.cs.service.util.ItemId) Provisioning(com.zimbra.cs.account.Provisioning) 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

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