Search in sources :

Example 6 with ZFilterRules

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

the class TestTagFilterRules method getRules.

private ZFilterRules getRules() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "TestTagFilterRules", tag TestTagFilterRules
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, SUBJECT_PREFIX));
    actions.add(new ZTagAction(TAG_NAME));
    rules.add(new ZFilterRule(TAG_NAME, true, false, conditions, actions));
    // if subject contains "TestTagFilterRules", tag TestTagFilterRules2
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, SUBJECT_PREFIX));
    actions.add(new ZTagAction(TAG2_NAME));
    rules.add(new ZFilterRule(TAG2_NAME, true, false, conditions, actions));
    return new ZFilterRules(rules);
}
Also used : 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)

Example 7 with ZFilterRules

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

the class TestFilter method testMultipleMultilineText.

/**
     * Tests fix for bug 57890 (https://issues.apache.org/jira/browse/JSIEVE-75).
     */
@Test
public void testMultipleMultilineText() throws Exception {
    if (mAvailableRFCCompliantNotify) {
        fail("Unable to test because 'zimbraSieveNotifyActionRFCCompliant' is set to TRUE");
        return;
    }
    List<ZFilterRule> rules;
    List<ZFilterCondition> conditions;
    List<ZFilterAction> actions;
    rules = new ArrayList<ZFilterRule>();
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZFilterCondition.ZTrueCondition());
    String notifyMsg1 = "From: ${FROM}\nMessage: ${BODY}\n";
    actions.add(new ZFilterAction.ZNotifyAction("abc@xyz.com", "${SUBJECT}", notifyMsg1));
    String notifyMsg2 = "you've got mail";
    actions.add(new ZFilterAction.ZNotifyAction("abc@xyz.com", "subject", notifyMsg2));
    rules.add(new ZFilterRule("testMultipleMultilineText 1", true, false, conditions, actions));
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZFilterCondition.ZTrueCondition());
    String replyMsg = "Replying to:\n${BODY}";
    actions.add(new ZFilterAction.ZReplyAction(replyMsg));
    rules.add(new ZFilterRule("testMultipleMultilineText 2", true, false, conditions, actions));
    saveIncomingRules(mMbox, new ZFilterRules(rules));
    rules = mMbox.getIncomingFilterRules().getRules();
    assertEquals("There should be 2 rules", rules.size(), 2);
    assertEquals("Rule 1 should have 2 actions", rules.get(0).getActions().size(), 2);
    assertEquals(((ZFilterAction.ZNotifyAction) rules.get(0).getActions().get(0)).getBodyTemplate(), notifyMsg1);
    assertEquals(((ZFilterAction.ZNotifyAction) rules.get(0).getActions().get(1)).getBodyTemplate(), notifyMsg2);
    assertEquals("Rule 2 should have 1 action", rules.get(1).getActions().size(), 1);
    assertEquals(((ZFilterAction.ZReplyAction) rules.get(1).getActions().get(0)).getBodyTemplate(), replyMsg);
}
Also used : ZFilterCondition(com.zimbra.client.ZFilterCondition) ZFilterRule(com.zimbra.client.ZFilterRule) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 8 with ZFilterRules

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

the class TestFilter method testRedirect.

/**
     * Tests the redirect filter action and confirms that the X-ZimbraForwarded
     * header is set on the redirected message.
     */
@Test
public void testRedirect() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "testRedirect", redirect to user2
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "testRedirect"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(REMOTE_USER_NAME)));
    rules.add(new ZFilterRule("testRedirect", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message.  Set the From header to something bogus to make
    // sure we're not rewriting it
    String from = "joebob@mycompany.com";
    String subject = NAME_PREFIX + " testRedirect 1";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    // Confirm that user1 did not receive it.
    List<ZMessage> messages = TestUtil.search(mMbox, "subject:\"" + subject + "\"");
    assertEquals(0, messages.size());
    // Confirm that user2 received it, and make sure X-ZimbraForwarded is set.
    ZMailbox remoteMbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    ZMessage msg = TestUtil.waitForMessage(remoteMbox, "in:inbox subject:\"" + subject + "\"");
    byte[] content = TestUtil.getContent(remoteMbox, msg.getId()).getBytes();
    MimeMessage mimeMsg = new MimeMessage(new ByteArrayInputStream(content));
    assertEquals(user1.getName(), mimeMsg.getHeader(FilterUtil.HEADER_FORWARDED));
    assertEquals(from, mimeMsg.getHeader("From"));
    // Check zimbraMailRedirectSetEnvelopeSender=FALSE.
    int port = 6025;
    DummySmtpServer smtp = startSmtpServer(port);
    localServer.setSmtpPort(port);
    localServer.setMailRedirectSetEnvelopeSender(false);
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    assertEquals(from, smtp.getMailFrom());
    // Check zimbraMailRedirectSetEnvelopeSender=TRUE.
    smtp = startSmtpServer(port);
    localServer.setMailRedirectSetEnvelopeSender(true);
    subject = NAME_PREFIX + " testRedirect 2";
    TestUtil.addMessageLmtp(subject, USER_NAME, from);
    String userAddress = Strings.nullToEmpty(TestUtil.getAddress(USER_NAME)).toLowerCase();
    assertEquals("testRedirect 2 mail from", userAddress, smtp.getMailFrom());
    // Check empty envelope sender.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 3";
    String msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    String[] recipients = new String[] { USER_NAME };
    TestUtil.addMessageLmtp(recipients, null, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=yes.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 4";
    msgContent = "Auto-Submitted: yes\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
    // Check Auto-Submitted=no.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 5";
    msgContent = "Auto-Submitted: no\r\n" + TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertEquals("testRedirect 5 mail from", userAddress, smtp.getMailFrom());
    // Check Content-Type=multipart/report.
    smtp = startSmtpServer(port);
    subject = NAME_PREFIX + " testRedirect 6";
    msgContent = TestUtil.getTestMessage(subject, USER_NAME, USER_NAME, null);
    msgContent = msgContent.replace("text/plain", "multipart/report");
    TestUtil.addMessageLmtp(recipients, USER_NAME, msgContent);
    assertTrue(smtp.getMailFrom(), StringUtil.isNullOrEmpty(smtp.getMailFrom()));
}
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) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZMailbox(com.zimbra.client.ZMailbox) MimeMessage(com.zimbra.common.mime.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 9 with ZFilterRules

use of com.zimbra.client.ZFilterRules 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 10 with ZFilterRules

use of com.zimbra.client.ZFilterRules 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)

Aggregations

ZFilterRules (com.zimbra.client.ZFilterRules)48 ZFilterAction (com.zimbra.client.ZFilterAction)37 ZFilterCondition (com.zimbra.client.ZFilterCondition)37 ZFilterRule (com.zimbra.client.ZFilterRule)37 ArrayList (java.util.ArrayList)35 ZMessage (com.zimbra.client.ZMessage)28 Test (org.junit.Test)23 ZHeaderCondition (com.zimbra.client.ZFilterCondition.ZHeaderCondition)20 ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)13 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)10 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)9 ZMailbox (com.zimbra.client.ZMailbox)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 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)2 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)2 ZFolder (com.zimbra.client.ZFolder)2