Search in sources :

Example 51 with ParsedMessage

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

the class FileIntoCopyTest method testCopyFileIntoPattern1Real.

/*
     * fileinto :copy foo; if header :contains "Subject" "test" { fileinto bar;
     * }
     * 
     * if message has "Subject: real" ==> it should be stored in "foo" and INBOX
     */
@Test
public void testCopyFileIntoPattern1Real() {
    try {
        String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";\n" + "if header :contains \"Subject\" \"Test\" {\n" + "fileinto \"bar\"; }";
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScriptPattern1);
        String rawReal = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Real\n" + "\n" + "Hello World";
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(rawReal.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // message should be stored in foo
        Integer item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "foo").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        Message msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
        // message should be stored in inbox
        item = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
    } 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) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 52 with ParsedMessage

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

the class FileIntoCopyTest method testCopyFileIntoPattern2Test.

/*
     * fileinto :copy foo; if header :contains "Subject" "Test" { fileinto :copy
     * bar; }
     * 
     * if message has "Subject: test" ==> it should be stored in "foo", "bar"
     * and INBOX
     */
@Test
public void testCopyFileIntoPattern2Test() {
    try {
        String filterScriptPattern1 = "require [\"copy\", \"fileinto\"];\n" + "fileinto :copy \"foo\";" + "if header :contains \"Subject\" \"Test\" {\n" + "fileinto :copy \"bar\"; }";
        Account account = Provisioning.getInstance().get(Key.AccountBy.name, "test@zimbra.com");
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        account.setMailSieveScript(filterScriptPattern1);
        String rawReal = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World";
        RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(rawReal.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        // message should be stored in bar
        Integer item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "bar").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        Message msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
        // message should be stored in foo
        item = mbox.getItemIds(null, mbox.getFolderByName(null, Mailbox.ID_FOLDER_USER_ROOT, "foo").getId()).getIds(MailItem.Type.MESSAGE).get(0);
        msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
        // message should be stored in inbox
        item = mbox.getItemIds(null, Mailbox.ID_FOLDER_INBOX).getIds(MailItem.Type.MESSAGE).get(0);
        msg = mbox.getMessageById(null, item);
        Assert.assertEquals("Hello World", msg.getFragment());
    } 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) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) Test(org.junit.Test)

Example 53 with ParsedMessage

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

the class FilterUtilTest method initZimbraMailAdapter.

/*
     * Create and initialize the ZimbraMailAdapter object 
     */
private ZimbraMailAdapter initZimbraMailAdapter() throws ServiceException {
    Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
    RuleManager.clearCachedRules(account);
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
    IncomingMessageHandler handler = new IncomingMessageHandler(new OperationContext(mbox), new DeliveryContext(), mbox, "test@zimbra.com", new ParsedMessage("From: test1@zimbra.com".getBytes(), false), 0, Mailbox.ID_FOLDER_INBOX, true);
    ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mbox, handler);
    // Set various variables
    mailAdapter.addVariable("var", "hello");
    List<String> matchedValues = new ArrayList<String>();
    matchedValues.add("test1");
    matchedValues.add("test2");
    mailAdapter.setMatchedValues(matchedValues);
    return mailAdapter;
}
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) ArrayList(java.util.ArrayList) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext)

Example 54 with ParsedMessage

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

the class FilterUtilTest method noHeaders.

@Test
public void noHeaders() throws Exception {
    Mailbox mbox = MailboxManager.getInstance().getMailboxByAccountId(MockProvisioning.DEFAULT_ACCOUNT_ID);
    String content = "just some content";
    ParsedMessage parsedMessage = new ParsedMessage(content.getBytes(), false);
    Map<String, String> vars = FilterUtil.getVarsMap(mbox, parsedMessage, parsedMessage.getMimeMessage());
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Test(org.junit.Test)

Example 55 with ParsedMessage

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

the class EnvelopeTest method testCountForEmptyFromHeader.

@Test
public void testCountForEmptyFromHeader() {
    String filterScript = "require \"envelope\";\n" + "if envelope :count \"eq\" :comparator \"i;ascii-numeric\" :all \"FROM\" \"0\" {\n" + "tag \"0\";\n" + "}\n" + "if envelope :all :matches \"from\" \"\" {\n" + "  tag \"empty\";\n" + "}\n" + "if envelope :count \"eq\" :comparator \"i;ascii-numeric\" :all \"to\" \"1\" {\n" + "tag \"1\";\n" + "}";
    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);
    try {
        Provisioning prov = Provisioning.getInstance();
        Account account = prov.createAccount("xyz@zimbra.com", "secret", new HashMap<String, Object>());
        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("0", ArrayUtil.getFirstElement(msg.getTags()));
        Assert.assertEquals("empty", msg.getTags()[1]);
        Assert.assertEquals("1", msg.getTags()[2]);
    } 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) MockProvisioning(com.zimbra.cs.account.MockProvisioning) 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

ParsedMessage (com.zimbra.cs.mime.ParsedMessage)388 Mailbox (com.zimbra.cs.mailbox.Mailbox)304 Test (org.junit.Test)300 Message (com.zimbra.cs.mailbox.Message)278 Account (com.zimbra.cs.account.Account)271 OperationContext (com.zimbra.cs.mailbox.OperationContext)264 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)252 ItemId (com.zimbra.cs.service.util.ItemId)186 MimeMessage (javax.mail.internet.MimeMessage)113 Header (javax.mail.Header)66 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)55 LmtpEnvelope (com.zimbra.cs.lmtpserver.LmtpEnvelope)52 LmtpAddress (com.zimbra.cs.lmtpserver.LmtpAddress)50 SyntaxException (org.apache.jsieve.exception.SyntaxException)49 DeliveryOptions (com.zimbra.cs.mailbox.DeliveryOptions)45 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)33 ServiceException (com.zimbra.common.service.ServiceException)30 InputStream (java.io.InputStream)24 IOException (java.io.IOException)21 Element (com.zimbra.common.soap.Element)18