Search in sources :

Example 31 with ZFilterRule

use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.

the class TestFolderFilterRules method testDeleteFolder.

/**
     * Confirms that when a folder is deleted, any rules that filed into that
     * folder or its subfolders are disabled (bug 17797).
     */
@Test
public void testDeleteFolder() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    mbox.deleteFolder(mFolder2.getId());
    // Deliver messages that used to match rules 2 and 3, and make sure
    // that they get delivered to inbox.
    TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
    TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
    // Confirm that rules for folders 2 and 3 are disabled.
    List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
    assertEquals(4, rules.size());
    for (ZFilterRule rule : rules) {
        if (rule.getName().equals("Folder 1")) {
            assertTrue(rule.isActive());
        } else if (rule.getName().equals("Folder 2")) {
            assertFalse(rule.isActive());
        } else if (rule.getName().equals("Folder 3")) {
            assertFalse(rule.isActive());
        } else if (rule.getName().equals("Folder 4")) {
            assertTrue(rule.isActive());
        } else {
            fail("Unexpected rule name: " + rule.getName());
        }
    }
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) ZFilterRule(com.zimbra.client.ZFilterRule) Test(org.junit.Test)

Example 32 with ZFilterRule

use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.

the class TestFolderFilterRules method testMoveFolderToTrash.

/**
     * Confirms that when a folder moved to the trash, any rules that filed into that
     * folder or its subfolders are disabled (bug 17797).
     */
@Test
public void testMoveFolderToTrash() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    mbox.trashFolder(mFolder2.getId());
    // Deliver messages that used to match rules 2 and 3, and make sure
    // that they get delivered to inbox.
    TestUtil.addMessageLmtp(SUBJECT2, USER_NAME, USER_NAME);
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT2 + "\"");
    TestUtil.addMessageLmtp(SUBJECT3, USER_NAME, USER_NAME);
    TestUtil.getMessage(mbox, "in:inbox subject:\"" + SUBJECT3 + "\"");
    // Confirm that rules for folders 2 and 3 are disabled.
    List<ZFilterRule> rules = mbox.getIncomingFilterRules(true).getRules();
    assertEquals(4, rules.size());
    for (ZFilterRule rule : rules) {
        if (rule.getName().equals("Folder 1")) {
            assertTrue(rule.isActive());
        } else if (rule.getName().equals("Folder 2")) {
            assertFalse(rule.isActive());
        } else if (rule.getName().equals("Folder 3")) {
            assertFalse(rule.isActive());
        } else if (rule.getName().equals("Folder 4")) {
            assertTrue(rule.isActive());
        } else {
            fail("Unexpected rule name: " + rule.getName());
        }
    }
}
Also used : ZMailbox(com.zimbra.client.ZMailbox) ZFilterRule(com.zimbra.client.ZFilterRule) Test(org.junit.Test)

Example 33 with ZFilterRule

use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.

the class TestFilter method testAddressTestPart.

@Test
public void testAddressTestPart() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    // use address test for address domain match
    conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.domain, HeaderOp.IS, false, "example.com"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTestPart1", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    String subject = NAME_PREFIX + " testAddressTestPart1";
    String mime = new MessageBuilder().withFrom("John Doe <JOHN.DOE@EXAMPLE.COM>").withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("Unexpected message flag state", msg.isFlagged());
    // use address test for address local-part match
    conditions.add(new ZFilterCondition.ZAddressCondition("From", Sieve.AddressPart.localpart, HeaderOp.MATCHES, true, "j*doe"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testAddressTestPart2", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    subject = NAME_PREFIX + " testAddressTestPart2";
    mime = new MessageBuilder().withFrom("John Doe <john.doe@example.com>").withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, mime);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("Unexpected message flag state", msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 34 with ZFilterRule

use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.

the class TestPop3Import method testFiltering.

/**
     * Confirms that messages pulled from a POP3 account are affected by
     * mail filtering (bug 13821).
     */
@Test
public void testFiltering() throws Exception {
    String folderPath = "/" + NAME_PREFIX + "-testFiltering";
    String filteredPath = "/" + NAME_PREFIX + "-testFiltering-filtered";
    // Create remote account
    prov.createAccount(TestUtil.getAddress(TEMP_USER_NAME), "test123", null);
    // Add message to remote mailbox
    ZMailbox remoteMbox = TestUtil.getZMailbox(TEMP_USER_NAME);
    TestUtil.addMessage(remoteMbox, NAME_PREFIX + " testFiltering");
    // Create local folders
    ZMailbox localMbox = TestUtil.getZMailbox(USER_NAME);
    localMbox.getFolderByPath("/Inbox");
    ZFolder dsFolder = TestUtil.createFolder(localMbox, folderPath);
    TestUtil.createFolder(localMbox, filteredPath);
    // Create filter rule that files to the local folder
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testFiltering"));
    actions.add(new ZFileIntoAction(filteredPath));
    rules.add(new ZFilterRule("testFiltering", true, false, conditions, actions));
    localMbox.saveIncomingFilterRules(new ZFilterRules(rules));
    // Set up data source and run import
    ZPop3DataSource ds = getZDataSource();
    ds.setUsername(TEMP_USER_NAME);
    ds.setFolderId(dsFolder.getId());
    ds.setEnabled(true);
    localMbox.modifyDataSource(ds);
    // Import data and make sure the message was filed to the folder
    TestUtil.importDataSource(ds, localMbox, remoteMbox);
    List<ZMessage> messages = TestUtil.search(localMbox, "in:" + folderPath);
    Assert.assertEquals("Found unexpected messages in " + folderPath, 0, messages.size());
    messages = TestUtil.search(localMbox, "in:" + filteredPath);
    Assert.assertEquals("Message not found in " + filteredPath, 1, messages.size());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZMailbox(com.zimbra.client.ZMailbox) ZPop3DataSource(com.zimbra.client.ZPop3DataSource) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZFolder(com.zimbra.client.ZFolder) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 35 with ZFilterRule

use of com.zimbra.client.ZFilterRule in project zm-mailbox by Zimbra.

the class ZMailboxUtil method doAddFilterRule.

private void doAddFilterRule(String[] args, ZFilterRules rules) throws ServiceException {
    ZFilterRule newRule = ZFilterRule.parseFilterRule(args);
    List<ZFilterRule> list = rules.getRules();
    if (firstOpt()) {
        list.add(0, newRule);
    } else if (afterOpt() != null) {
        boolean found = false;
        String name = afterOpt();
        for (int i = 0; i < list.size(); i++) {
            found = list.get(i).getName().equalsIgnoreCase(name);
            if (found) {
                if (i + 1 >= list.size())
                    list.add(newRule);
                else
                    list.add(i + 1, newRule);
                break;
            }
        }
        if (!found) {
            throw ZClientException.CLIENT_ERROR("can't find rule: " + name, null);
        }
    } else if (beforeOpt() != null) {
        String name = beforeOpt();
        boolean found = false;
        for (int i = 0; i < list.size(); i++) {
            found = list.get(i).getName().equalsIgnoreCase(name);
            if (found) {
                list.add(i, newRule);
                break;
            }
        }
        if (!found) {
            throw ZClientException.CLIENT_ERROR("can't find rule: " + name, null);
        }
    } else {
        // add to end
        list.add(newRule);
    }
}
Also used : ZFilterRule(com.zimbra.client.ZFilterRule) ZMountpoint(com.zimbra.client.ZMountpoint)

Aggregations

ZFilterRule (com.zimbra.client.ZFilterRule)43 ZFilterAction (com.zimbra.client.ZFilterAction)37 ZFilterCondition (com.zimbra.client.ZFilterCondition)37 ZFilterRules (com.zimbra.client.ZFilterRules)37 ArrayList (java.util.ArrayList)35 ZMessage (com.zimbra.client.ZMessage)29 Test (org.junit.Test)26 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)20 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)13 ZMailbox (com.zimbra.client.ZMailbox)12 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)10 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)9 HashSet (java.util.HashSet)5 ZBodyCondition (com.zimbra.client.ZFilterCondition.ZBodyCondition)4 ZTag (com.zimbra.client.ZTag)4 ZDiscardAction (com.zimbra.client.ZFilterAction.ZDiscardAction)3 ZRedirectAction (com.zimbra.client.ZFilterAction.ZRedirectAction)3 ZMountpoint (com.zimbra.client.ZMountpoint)3 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)2 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)2