Search in sources :

Example 6 with ZHeaderCondition

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

the class TestFilter method testHeaderMatches.

/**
     * Tests matching a header against a wildcard expression.  See bug 21701.
     */
@Test
public void testHeaderMatches() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // If subject matches the first for characters + *, mark as flagged.
    String pattern = NAME_PREFIX.substring(0, 4) + "*";
    conditions.add(new ZHeaderCondition("subject", HeaderOp.MATCHES, pattern));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule("testHeaderMatches", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message and confirm it is flagged
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testHeaderMatches";
    TestUtil.addMessageLmtp(subject, address, address);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:testHeaderMatches");
    assertTrue("Message was not flagged", msg.isFlagged());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 7 with ZHeaderCondition

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

the class TestFilter method testManyAsterisks.

/**
     * Make sure we disallow more than four asterisks in a :matches condition (bug 35983).
     */
@Test
public void testManyAsterisks() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    ZFilterCondition condition = new ZHeaderCondition("from", HeaderOp.MATCHES, "*****address@yahoo.com");
    ZFilterAction action = new ZKeepAction();
    conditions.add(condition);
    actions.add(action);
    rules.add(new ZFilterRule("test many asterisks", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    try {
        mMbox.saveIncomingFilterRules(zRules);
        fail("Saving filter rules with quotes should not have succeeded");
    } catch (SoapFaultException e) {
        assertTrue("Unexpected exception: " + e, e.getMessage().contains("four asterisks"));
    }
}
Also used : ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZKeepAction(com.zimbra.client.ZFilterAction.ZKeepAction) ZFilterRules(com.zimbra.client.ZFilterRules) SoapFaultException(com.zimbra.common.soap.SoapFaultException) Test(org.junit.Test)

Example 8 with ZHeaderCondition

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

the class TestFilter method testToOrCc.

public void testToOrCc() throws Exception {
    // Create filter rules
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("to,cc", HeaderOp.CONTAINS, "checkthis.com"));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testToOrCc", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message 1 and make sure that tag 1 was applied
    String subject = NAME_PREFIX + " testToOrCc 1";
    String content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withCcRecipient("cc@checkthis.com").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    Set<String> tagIds = new HashSet<String>();
    assertNotNull(msg.getTagIds());
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(mTag1.getId()));
    // Deliver message 2 and make sure that tag 1 was applied
    subject = NAME_PREFIX + " testToOrCc 2";
    content = new MessageBuilder().withSubject(subject).withToRecipient("to@checkthis.com").withCcRecipient(USER_NAME).create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    tagIds = new HashSet<String>();
    assertNotNull(msg.getTagIds());
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(mTag1.getId()));
}
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) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet)

Example 9 with ZHeaderCondition

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

the class TestFilter method testCaseSensitiveComparison.

public void testCaseSensitiveComparison() throws Exception {
    // Create case sensitive header and body filter rules
    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, true, "CHECK THIS"));
    conditions.add(new ZBodyCondition(BodyOp.CONTAINS, true, "CHECK THIS"));
    actions.add(new ZTagAction(mTag1.getName()));
    rules.add(new ZFilterRule("testCaseSensitiveComparison", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    // Deliver message having lower case subject substring and make sure that tag1 was not applied
    String subject = NAME_PREFIX + " testCaseSensitiveComparison1 check this";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("message should not have been tagged", msg.getTagIds() == null || msg.getTagIds().isEmpty());
    // Deliver message having upper case subject substring and make sure that tag1 was applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison2 CHECK THIS";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertNotNull("message should have been tagged", msg.getTagIds());
    Set<String> tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertTrue("message should have been tagged with tag1", tagIds.contains(mTag1.getId()));
    // Deliver message having lower case body content substring and make sure that tag1 was not applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison3";
    String content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withBody("Hi check this").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertTrue("message should not have been tagged", msg.getTagIds() == null || msg.getTagIds().isEmpty());
    // Deliver message having upper case body content substring and make sure that tag1 was applied
    subject = NAME_PREFIX + " testCaseSensitiveComparison4";
    content = new MessageBuilder().withSubject(subject).withToRecipient(USER_NAME).withBody("Hi CHECK THIS").create();
    TestUtil.addMessageLmtp(new String[] { USER_NAME }, USER_NAME, content);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    assertNotNull("message should have been tagged", msg.getTagIds());
    tagIds = new HashSet<String>();
    tagIds.addAll(Arrays.asList(msg.getTagIds().split(",")));
    assertTrue("message should have been tagged with tag1", tagIds.contains(mTag1.getId()));
}
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) ZBodyCondition(com.zimbra.client.ZFilterCondition.ZBodyCondition) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZFilterRules(com.zimbra.client.ZFilterRules) HashSet(java.util.HashSet)

Example 10 with ZHeaderCondition

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

the class TestFilter method testSpam.

/**
     * Confirms that spam filtering takes a higher precedence than
     * custom user filtering (bug 23886).
     */
public void testSpam() throws Exception {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String sender = TestUtil.getAddress(USER_NAME);
    String[] recipients = new String[] { sender };
    String message = TestUtil.getTestMessage(NAME_PREFIX + " testSpam", USER_NAME, USER_NAME, null);
    Config config = Provisioning.getInstance().getConfig();
    message = config.getAttr(Provisioning.A_zimbraSpamHeader) + ": " + config.getAttr(Provisioning.A_zimbraSpamHeaderValue) + "\r\n" + message;
    // Make sure spam message doesn't already exist
    assertEquals(0, TestUtil.search(mbox, "in:junk subject:testSpam").size());
    // Deliver spam message without matching rule, make sure it gets delivered
    // to the junk folder, and delete.
    TestUtil.addMessageLmtp(recipients, sender, message);
    ZMessage msg = TestUtil.getMessage(mbox, "in:junk subject:testSpam");
    mbox.deleteMessage(msg.getId());
    // Add matching filter rule: if subject contains "testSpam", file into folder1
    ZFilterRules rules = getTestIncomingRules();
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testSpam"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    rules.getRules().add(new ZFilterRule("testBug5455", true, false, conditions, actions));
    saveIncomingRules(mMbox, rules);
    // Set "apply user rules" attribute to TRUE and make sure the message gets filed into folder1
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraSpamApplyUserFilters, LdapConstants.LDAP_TRUE);
    TestUtil.addMessageLmtp(recipients, sender, message);
    msg = TestUtil.waitForMessage(mbox, "in:" + FOLDER1_PATH + " subject:testSpam");
    mbox.deleteMessage(msg.getId());
    // Set "apply user rules" attribute to FALSE and make sure the message gets filed into junk
    TestUtil.setAccountAttr(USER_NAME, Provisioning.A_zimbraSpamApplyUserFilters, LdapConstants.LDAP_FALSE);
    TestUtil.addMessageLmtp(recipients, sender, message);
    TestUtil.waitForMessage(mbox, "in:junk subject:testSpam");
}
Also used : ZMessage(com.zimbra.client.ZMessage) Config(com.zimbra.cs.account.Config) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZMailbox(com.zimbra.client.ZMailbox) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZFilterRules(com.zimbra.client.ZFilterRules)

Aggregations

ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)20 ZFilterAction (com.zimbra.client.ZFilterAction)19 ZFilterCondition (com.zimbra.client.ZFilterCondition)19 ZFilterRule (com.zimbra.client.ZFilterRule)19 ZFilterRules (com.zimbra.client.ZFilterRules)19 ArrayList (java.util.ArrayList)19 ZMessage (com.zimbra.client.ZMessage)11 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)10 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)8 Test (org.junit.Test)8 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)6 ZMailbox (com.zimbra.client.ZMailbox)5 ZRedirectAction (com.zimbra.client.ZFilterAction.ZRedirectAction)4 ZDiscardAction (com.zimbra.client.ZFilterAction.ZDiscardAction)3 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)3 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)2 ZBodyCondition (com.zimbra.client.ZFilterCondition.ZBodyCondition)2 ZFolder (com.zimbra.client.ZFolder)2 MimeMessage (com.zimbra.common.mime.MimeMessage)2 Config (com.zimbra.cs.account.Config)2