Search in sources :

Example 96 with Mailbox

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

the class SetVariableTest method testSetVarNameWithDigits.

@Test
public void testSetVarNameWithDigits() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "set \"var2\" \"hello\"\n;" + "set \"var_2\" \"hellovar_2\"\n;" + "set \"_var2\" \"hello_var2\"\n;" + "set \"_var2_ad\" \"hello_var2_ad\"\n;" + "set \"_var2_\" \"hello_var2_\"\n;" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${var2}\";\n" + "  tag \"${var_2}\";\n" + "  tag \"${_var2}\";\n" + "  tag \"${_var2_ad}\";\n" + "  tag \"${_var2_}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\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("hello", msg.getTags()[0]);
        Assert.assertEquals("hellovar_2", msg.getTags()[1]);
        Assert.assertEquals("hello_var2", msg.getTags()[2]);
        Assert.assertEquals("hello_var2_ad", msg.getTags()[3]);
        Assert.assertEquals("hello_var2_", msg.getTags()[4]);
    } 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) ItemId(com.zimbra.cs.service.util.ItemId) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Example 97 with Mailbox

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

the class SetVariableTest method testWildCardGreedyMatch.

@Test
public void testWildCardGreedyMatch() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"sample*test\" { " + "tag \"${1}\";}";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: sample abc test 123 test ABC test";
        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("abc test 123 test ABC", msg.getTags()[0]);
    } 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) ItemId(com.zimbra.cs.service.util.ItemId) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Example 98 with Mailbox

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

the class SetVariableTest method testDollar2.

/**
     *  When the match pattern ${dollar} was defined as '$' and used in the
     *  header/address/envelope test, this '$' should be treated as a string,
     *  not a part of the wild-card.
     */
@Test
public void testDollar2() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "set \"dollar\" \"$\";\n" + "set \"val\" \"xyz\";\n" + "if header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-matches\";\n" + "}\n" + "if header :is :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-is\";\n" + "}\n" + "if header :contains :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-contains\";\n" + "}\n" + "if address :all :matches :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"address-matches\";\n" + "}\n" + "if address :all :is :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"address-is\";\n" + "}\n" + "if address :all :contains :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}\" {\n" + "  tag \"address-contains\";\n" + "}" + "if envelope :all :matches :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"envelope-matches\";\n" + "}\n" + "if envelope :all :is :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"envelope-is\";\n" + "}\n" + "if envelope :all :contains :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}\" {\n" + "  tag \"envelope-contains\";\n" + "}" + "if not header :matches :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-not-matches\";\n" + "}\n" + "if not header :is :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-not-is\";\n" + "}\n" + "if not header :contains :comparator \"i;ascii-casemap\" \"Subject\" \"${dollar}${val}\" {\n" + "  tag \"header-not-contains\";\n" + "}\n" + "if not address :all :matches :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"address-not-matches\";\n" + "}\n" + "if not address :all :is :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"address-not-is\";\n" + "}\n" + "if not address :all :contains :comparator \"i;ascii-casemap\" \"To\" \"${dollar}${val}\" {\n" + "  tag \"address-not-contains\";\n" + "}" + "if not envelope :all :matches :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"envelope-not-matches\";\n" + "}\n" + "if not envelope :all :is :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}@zimbra.com\" {\n" + "  tag \"envelope-not-is\";\n" + "}\n" + "if not envelope :all :contains :comparator \"i;ascii-casemap\" \"From\" \"${dollar}${val}\" {\n" + "  tag \"envelope-not-contains\";\n" + "}\n" + "if header :contains \"X-Header1\" \"${dollar}\" {\n" + "  tag \"dollar\";\n" + "}\n" + "if header :contains \"X-Header2\" \"${dollar}{\" {\n" + "  tag \"dollar-opening-brace\";\n" + "}\n" + "if header :contains \"X-Header3\" \"${dollar}}\" {\n" + "  tag \"dollar-closing-brace\";\n" + "}\n" + "if header :contains \"X-Header4\" \"${dollar}\" {\n" + "  tag \"dollar-middle\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: \"$xyz\"@zimbra.com\n" + "Subject: $xyz\n" + "X-Header1: $\n" + "X-Header2: ${\n" + "X-Header3: $}\n" + "X-Header4: abc$def\n" + "\n" + "Hello World.";
        LmtpEnvelope env = new LmtpEnvelope();
        LmtpAddress sender = new LmtpAddress("<$xyz@zimbra.com>", new String[] { "BODY", "SIZE" }, null);
        LmtpAddress recipient = new LmtpAddress("<test@zimbra.com>", null, null);
        env.setSender(sender);
        env.addLocalRecipient(recipient);
        List<ItemId> ids = RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.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(13, msg.getTags().length);
        Assert.assertEquals("header-matches", msg.getTags()[0]);
        Assert.assertEquals("header-is", msg.getTags()[1]);
        Assert.assertEquals("header-contains", msg.getTags()[2]);
        Assert.assertEquals("address-matches", msg.getTags()[3]);
        Assert.assertEquals("address-is", msg.getTags()[4]);
        Assert.assertEquals("address-contains", msg.getTags()[5]);
        Assert.assertEquals("envelope-matches", msg.getTags()[6]);
        Assert.assertEquals("envelope-is", msg.getTags()[7]);
        Assert.assertEquals("envelope-contains", msg.getTags()[8]);
        Assert.assertEquals("dollar", msg.getTags()[9]);
        Assert.assertEquals("dollar-opening-brace", msg.getTags()[10]);
        Assert.assertEquals("dollar-closing-brace", msg.getTags()[11]);
        Assert.assertEquals("dollar-middle", msg.getTags()[12]);
    } 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 99 with Mailbox

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

the class SetVariableTest method testModifierSamePrecendenceInSingleSet2.

//    set :upperfirst lowerfirst "b" "${a}";            => "JuMBlEd lETteRS"
@Test
public void testModifierSamePrecendenceInSingleSet2() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        filterScript = "require [\"variables\"];\n" + "set \"a\" \"juMBlEd lETteRS\" ;\n" + "set :upperfirst :lower :lowerfirst :lower \"c\" \"${b}\";" + "if header :matches \"Subject\" \"*\" {\n" + "  tag \"${d}\";\n" + "}\n";
        account.setMailSieveScript(filterScript);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\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);
        Message msg = mbox.getMessageById(null, ids.get(0).getId());
        Assert.assertTrue(ArrayUtil.getFirstElement(msg.getTags()) == null);
    } 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 100 with Mailbox

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

the class SetVariableTest method testSetVarWithModifiersInValid.

@Test
public void testSetVarWithModifiersInValid() {
    try {
        Account account = Provisioning.getInstance().getAccount(MockProvisioning.DEFAULT_ACCOUNT_ID);
        RuleManager.clearCachedRules(account);
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        String raw = "From: sender@zimbra.com\n" + "To: test1@zimbra.com\n" + "Subject: Test\n" + "\n" + "Hello World.";
        try {
            filterScript = "require [\"variables\"];\n" + "set \"hello\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Atleast 2 argument are needed. Found Arguments: [[hello]]") > -1);
            }
        }
        try {
            filterScript = "require [\"variables\"];\n" + "set :lownner \"var\" \"hello\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Invalid variable modifier:") > -1);
            }
        }
        try {
            filterScript = "require [\"variables\"];\n" + "set :lower \"var\";\n";
            account.setMailSieveScript(filterScript);
            RuleManager.applyRulesToIncomingMessage(new OperationContext(mbox), mbox, new ParsedMessage(raw.getBytes(), false), 0, account.getName(), new DeliveryContext(), Mailbox.ID_FOLDER_INBOX, true);
        } catch (Exception e) {
            if (e instanceof SyntaxException) {
                SyntaxException se = (SyntaxException) e;
                assertTrue(se.getMessage().indexOf("Invalid variable modifier:") > -1);
            }
        }
    } 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) SyntaxException(org.apache.jsieve.exception.SyntaxException) DeliveryContext(com.zimbra.cs.mailbox.DeliveryContext) SyntaxException(org.apache.jsieve.exception.SyntaxException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Test(org.junit.Test)

Aggregations

Mailbox (com.zimbra.cs.mailbox.Mailbox)817 Account (com.zimbra.cs.account.Account)389 Test (org.junit.Test)376 OperationContext (com.zimbra.cs.mailbox.OperationContext)365 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)306 Message (com.zimbra.cs.mailbox.Message)303 DeliveryContext (com.zimbra.cs.mailbox.DeliveryContext)243 ItemId (com.zimbra.cs.service.util.ItemId)243 Element (com.zimbra.common.soap.Element)138 ServiceException (com.zimbra.common.service.ServiceException)127 MailServiceException (com.zimbra.cs.mailbox.MailServiceException)127 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)106 MimeMessage (javax.mail.internet.MimeMessage)104 Folder (com.zimbra.cs.mailbox.Folder)82 ArrayList (java.util.ArrayList)81 ZMailbox (com.zimbra.client.ZMailbox)73 DbConnection (com.zimbra.cs.db.DbPool.DbConnection)71 PreparedStatement (java.sql.PreparedStatement)67 SQLException (java.sql.SQLException)67 Header (javax.mail.Header)66