Search in sources :

Example 66 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderForMultilineValuedHeader.

/*
     * Replace header with multiline valued header
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderForMultilineValuedHeader() {
    try {
        String filterScript = "require [\"editheader\", \"variables\"];\n" + "        replaceheader :newvalue \"${1}[test]${2}\" :matches \"X-Test-Header\" \"*line2*\";";
        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(sampleBaseMsg2.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 headerValue = "";
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Test-Header".equals(header.getName())) {
                headerValue = header.getValue();
                break;
            }
        }
        Assert.assertEquals("line1 [test] line3", headerValue);
    } 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)

Example 67 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderValueNegative.

/*
     * Replace header with numeric comparator :value
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderValueNegative() {
    try {
        String filterScript = "require [\"editheader\", \"relational\", \"comparator-i;ascii-numeric\"];\n" + "replaceheader :newname \"X-Numeric2-Header\" :newvalue \"0\" :value \"lt\" :comparator \"i;ascii-numeric\" \"X-Numeric-Header\" \"-3\";\n";
        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);
        boolean matchFound = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Numeric2-Header".equals(header.getName())) {
                matchFound = true;
            }
        }
        Assert.assertFalse(matchFound);
        matchFound = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Numeric-Header".equals(header.getName())) {
                matchFound = true;
            }
        }
        Assert.assertTrue(matchFound);
    } 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)

Example 68 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderForEncodedHeaderValue.

/*
     * Replace encoded header value
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderForEncodedHeaderValue() {
    try {
        String filterScript = "require [\"editheader\", \"variables\"];\n" + "replaceheader :newvalue \"[test]${1}\" :matches \"X-Test-Header\" \"*\";" + "tag \"tag1-${1}\";" + "tag \"tag2-${2}\";" + "tag \"tag3-${3}\";" + "tag \"tag4-${4}\";" + "tag \"tag5-${5}\";" + "tag \"tag6-${6}\";" + "tag \"tag7-${7}\";" + "tag \"tag8-${8}\";" + "tag \"tag9-${9}\";";
        Account acct1 = Provisioning.getInstance().get(Key.AccountBy.name, "test4@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(sampleBaseMsg3.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 headerValue = "";
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            if ("X-Test-Header".equals(header.getName())) {
                headerValue = header.getValue();
                break;
            }
        }
        String matchValue = "=?UTF-8?Q?[test][SP?=\r\n" + " =?UTF-8?Q?AM]=E6=97=A5=E6=9C=AC=E8=AA=9E=E3=81=AE=E4=BB=B6=E5=90=8D?=";
        Assert.assertEquals(matchValue, headerValue);
        String[] expectedTags = { "tag1-[SPAM]日本語の件名", "tag2-ABC", "tag3-123", "tag4-abc", "tag5-\"\"", "tag6-XYZ", "tag7-", "tag8-xyz", "tag9-\'\'" };
        String[] resultTags = message.getTags();
        for (String resultTag : resultTags) {
            String expectedTag = null;
            for (String testTag : expectedTags) {
                if (testTag.equalsIgnoreCase(resultTag)) {
                    expectedTag = testTag;
                    break;
                }
            }
            Assert.assertEquals(expectedTag, resultTag);
        }
    } 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)

Example 69 with Message

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

the class ReplaceHeaderTest method testReplaceHeaderStartingWithSpacesAsHeaderName.

/*
     * Replace header with name starting with spaces as name, should not replace the original header
     */
@SuppressWarnings("unchecked")
@Test
public void testReplaceHeaderStartingWithSpacesAsHeaderName() {
    try {
        String filterScript = "require [\"editheader\"];\n" + " replaceheader :newname \"X-Test-New-Name\" :newvalue \"0\" :value \"lt\" :comparator \"i;ascii-casemap\" \" X-Test-Header\" \"test2\" \r\n" + "  ;\n";
        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);
        boolean found = false;
        for (Enumeration<Header> enumeration = message.getMimeMessage().getAllHeaders(); enumeration.hasMoreElements(); ) {
            Header header = enumeration.nextElement();
            Assert.assertFalse(header.getName().equals(""));
            if (header.getName().equals("X-Test-Header") && header.getValue().equals("test2")) {
                found = true;
            }
        }
        Assert.assertTrue(found);
    } 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)

Example 70 with Message

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

the class HeaderTest method testHeaderMatchWithItself.

@Test
public void testHeaderMatchWithItself() throws Exception {
    String script = "require [\"variables\"];\n" + "if header :matches \"X-Header1\" \"*\" {" + "    if header :matches \"X-Header1\" \"${1}\" {" + "        tag \"01\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    if header :is \"X-Header1\" \"${1}\" {" + "        tag \"02\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    set \"myvar1\" \"${1}\";" + "    if header :matches \"X-Header1\" \"${myvar1}\" {" + "        tag \"03\";" + "    }" + "}" + "if header :matches \"X-Header1\" \"*\" {" + "    set :quotewildcard \"myvar2\" \"${1}\";" + "    if string :matches \"sample\\\\\\\\\\\\\\\\pattern\" \"${myvar2}\" {" + "        tag \"04\";" + "    }" + "}";
    String sourceMsg = "X-Header1: sample\\\\pattern\n";
    try {
        Account account = Provisioning.getInstance().getAccountByName("testHdr@zimbra.com");
        RuleManager.clearCachedRules(account);
        account.setAdminSieveScriptBefore(script);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(sourceMsg.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(4, msg.getTags().length);
        Assert.assertEquals("01", msg.getTags()[0]);
        Assert.assertEquals("02", msg.getTags()[1]);
        Assert.assertEquals("03", msg.getTags()[2]);
        Assert.assertEquals("04", msg.getTags()[3]);
    } 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) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) 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)

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