Search in sources :

Example 1 with ZHeaderCondition

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

the class ZFilterRule method parseFilterRule.

public static ZFilterRule parseFilterRule(String[] args) throws ServiceException {
    String name = args[0];
    boolean all = true;
    boolean active = true;
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    int i = 1;
    while (i < args.length) {
        String a = args[i++];
        if (a.equals("active")) {
            active = true;
        } else if (a.equals("inactive")) {
            active = false;
        } else if (a.equals("any")) {
            all = false;
        } else if (a.equals("all")) {
            all = true;
        } else if (a.equals("addressbook")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String op = args[i++];
            String header = args[i++];
            conditions.add(new ZAddressBookCondition(AddressBookOp.fromString(op), header));
        } else if (a.equals("contact_ranking")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String op = args[i++];
            String header = args[i++];
            conditions.add(new ZContactRankingCondition(ContactRankingOp.fromString(op), header));
        } else if (a.equals("me")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String op = args[i++];
            String header = args[i++];
            conditions.add(new ZMeCondition(MeOp.fromString(op), header));
        } else if (a.equals("attachment")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing exists arg", null);
            }
            conditions.add(new ZAttachmentExistsCondition(args[i++].equals("exists")));
        } else if (a.equals("body")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String op = args[i++];
            String nextArg = args[i++];
            boolean caseSensitive = false;
            if (ZFilterCondition.C_CASE_SENSITIVE.equals(nextArg)) {
                caseSensitive = true;
                if (i + 1 > args.length) {
                    throw ZClientException.CLIENT_ERROR("missing args", null);
                }
                nextArg = args[i++];
            }
            conditions.add(new ZBodyCondition(BodyOp.fromString(op), caseSensitive, nextArg));
        } else if (a.equals("size")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            conditions.add(new ZSizeCondition(SizeOp.fromString(args[i++]), args[i++]));
        } else if (a.equals("date")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            conditions.add(new ZDateCondition(DateOp.fromString(args[i++]), args[i++]));
        } else if (a.equals("current_time")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            conditions.add(new ZCurrentTimeCondition(DateOp.fromString(args[i++]), args[i++]));
        } else if (a.equals("current_day_of_week")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            conditions.add(new ZCurrentDayOfWeekCondition(SimpleOp.fromString(args[i++]), args[i++]));
        } else if (a.equals("header")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String headerName = args[i++];
            String op = args[i++];
            if (op.equals("exists")) {
                conditions.add(new ZHeaderExistsCondition(headerName, true));
            } else if (op.equals("not_exists")) {
                conditions.add(new ZHeaderExistsCondition(headerName, false));
            } else {
                if (i + 1 > args.length) {
                    throw ZClientException.CLIENT_ERROR("missing args", null);
                }
                String nextArg = args[i++];
                boolean caseSensitive = false;
                if (ZFilterCondition.C_CASE_SENSITIVE.equals(nextArg)) {
                    caseSensitive = true;
                    if (i + 1 > args.length) {
                        throw ZClientException.CLIENT_ERROR("missing args", null);
                    }
                    nextArg = args[i++];
                }
                conditions.add(new ZHeaderCondition(headerName, HeaderOp.fromString(op), caseSensitive, nextArg));
            }
        } else if (a.equals("mime_header")) {
            if (i + 3 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String headerName = args[i++];
            String op = args[i++];
            String nextArg = args[i++];
            boolean caseSensitive = false;
            if (ZFilterCondition.C_CASE_SENSITIVE.equals(nextArg)) {
                caseSensitive = true;
                if (i + 1 > args.length) {
                    throw ZClientException.CLIENT_ERROR("missing args", null);
                }
                nextArg = args[i++];
            }
            conditions.add(new ZMimeHeaderCondition(headerName, HeaderOp.fromString(op), caseSensitive, nextArg));
        } else if (a.equals("address")) {
            if (i + 4 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String headerName = args[i++];
            String part = args[i++];
            String op = args[i++];
            String nextArg = args[i++];
            boolean caseSensitive = false;
            if (ZFilterCondition.C_CASE_SENSITIVE.equals(nextArg)) {
                caseSensitive = true;
                if (i + 1 > args.length) {
                    throw ZClientException.CLIENT_ERROR("missing args", null);
                }
                nextArg = args[i++];
            }
            conditions.add(new ZAddressCondition(headerName, Sieve.AddressPart.fromString(part), HeaderOp.fromString(op), caseSensitive, nextArg));
        } else if (a.equals("invite")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing exists arg", null);
            }
            ZInviteCondition cond = new ZInviteCondition(args[i++].equals("exists"));
            if (i + 1 < args.length && args[i].equalsIgnoreCase("method")) {
                // method
                i++;
                cond.setMethods(args[i++].split(","));
            }
            conditions.add(cond);
        } else if (a.equals("conversation")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing where arg", null);
            }
            conditions.add(new ZFilterCondition.ZConversationCondition(ZFilterCondition.ConversationOp.fromString(args[i++]), args[i++]));
        } else if (a.equals("facebook")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZFacebookCondition(op));
        } else if (a.equals("linkedin")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZLinkedInCondition(op));
        } else if (a.equals("socialcast")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZSocialcastCondition(op));
        } else if (a.equals("twitter")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZTwitterCondition(op));
        } else if (a.equals("list")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZListCondition(op));
        } else if (a.equals("bulk")) {
            ZFilterCondition.SimpleOp op;
            if (i + 1 < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            conditions.add(new ZFilterCondition.ZBulkCondition(op));
        } else if (a.equals("importance")) {
            if (i + 2 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            String op = args[i++];
            String importance = args[i++];
            conditions.add(new ZFilterCondition.ZImportanceCondition(SimpleOp.fromString(op), FilterTest.Importance.fromString(importance)));
        } else if (a.equals("flagged")) {
            ZFilterCondition.SimpleOp op;
            if (i < args.length && args[i].equalsIgnoreCase("not")) {
                // not
                i++;
                op = ZFilterCondition.SimpleOp.NOT_IS;
            } else {
                op = ZFilterCondition.SimpleOp.IS;
            }
            if (i >= args.length) {
                throw ZClientException.CLIENT_ERROR("missing a flag name after 'flagged'", null);
            }
            conditions.add(new ZFilterCondition.ZFlaggedCondition(op, Sieve.Flag.fromString(args[i++])));
        } else if (a.equals("keep")) {
            actions.add(new ZKeepAction());
        } else if (a.equals("discard")) {
            actions.add(new ZDiscardAction());
        } else if (a.equals("fileinto")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            actions.add(new ZFileIntoAction(args[i++]));
        } else if (a.equals("tag")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            actions.add(new ZTagAction(args[i++]));
        } else if (a.equals("mark")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            actions.add(new ZMarkAction(MarkOp.fromString(args[i++])));
        } else if (a.equals("redirect")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            actions.add(new ZRedirectAction(args[i++]));
        } else if (a.equals("reply")) {
            if (i + 1 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            actions.add(new ZReplyAction(args[i++]));
        } else if (a.equals("notify")) {
            if (i + 3 > args.length) {
                throw ZClientException.CLIENT_ERROR("missing args", null);
            }
            if (!"RFC".equalsIgnoreCase(args[i])) {
                String emailAddr = args[i++];
                String subjectTemplate = args[i++];
                String bodyTemplate = args[i++];
                int maxBodyBytes = -1;
                if (i + 1 <= args.length) {
                    try {
                        maxBodyBytes = Integer.valueOf(args[i]);
                        i++;
                    } catch (NumberFormatException ignored) {
                    }
                }
                actions.add(new ZNotifyAction(emailAddr, subjectTemplate, bodyTemplate, maxBodyBytes));
            } else {
                // "options".
                if (i + 4 > args.length) {
                    throw ZClientException.CLIENT_ERROR("missing args", null);
                }
                // skip string "RFC"
                i++;
                String from = args[i++];
                String importance = null;
                String options = null;
                if (i + 4 == args.length) {
                    // Now we check whether the notify string contains "importance" and "options" tokens
                    // If these two tokens are included, the remaining number of tokens at this point is 4,
                    // otherwise 2.
                    importance = args[i++];
                    options = args[i++];
                }
                String message = args[i++];
                String method = args[i++];
                if (importance != null && options != null) {
                    actions.add(new ZRFCCompliantNotifyAction(from, importance, options, message, method));
                } else {
                    actions.add(new ZRFCCompliantNotifyAction(from, message, method));
                }
            }
        } else if (a.equals("stop")) {
            actions.add(new ZStopAction());
        } else {
            throw ZClientException.CLIENT_ERROR("unknown keyword: " + a, null);
        }
    }
    if (name == null || name.length() == 0) {
        throw ZClientException.CLIENT_ERROR("missing filter name", null);
    }
    if (actions.isEmpty()) {
        throw ZClientException.CLIENT_ERROR("must have at least one action", null);
    }
    if (conditions.isEmpty()) {
        throw ZClientException.CLIENT_ERROR("must have at least one condition", null);
    }
    return new ZFilterRule(name, active, all, conditions, actions);
}
Also used : ZReplyAction(com.zimbra.client.ZFilterAction.ZReplyAction) ZSizeCondition(com.zimbra.client.ZFilterCondition.ZSizeCondition) ZDiscardAction(com.zimbra.client.ZFilterAction.ZDiscardAction) ZInviteCondition(com.zimbra.client.ZFilterCondition.ZInviteCondition) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ArrayList(java.util.ArrayList) ZNotifyAction(com.zimbra.client.ZFilterAction.ZNotifyAction) ZAddressBookCondition(com.zimbra.client.ZFilterCondition.ZAddressBookCondition) ZRFCCompliantNotifyAction(com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction) ZCurrentDayOfWeekCondition(com.zimbra.client.ZFilterCondition.ZCurrentDayOfWeekCondition) ZKeepAction(com.zimbra.client.ZFilterAction.ZKeepAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZHeaderExistsCondition(com.zimbra.client.ZFilterCondition.ZHeaderExistsCondition) ZDateCondition(com.zimbra.client.ZFilterCondition.ZDateCondition) ZAddressCondition(com.zimbra.client.ZFilterCondition.ZAddressCondition) SimpleOp(com.zimbra.client.ZFilterCondition.SimpleOp) ZMimeHeaderCondition(com.zimbra.client.ZFilterCondition.ZMimeHeaderCondition) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZBodyCondition(com.zimbra.client.ZFilterCondition.ZBodyCondition) ZCurrentTimeCondition(com.zimbra.client.ZFilterCondition.ZCurrentTimeCondition) ZContactRankingCondition(com.zimbra.client.ZFilterCondition.ZContactRankingCondition) SimpleOp(com.zimbra.client.ZFilterCondition.SimpleOp) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZMeCondition(com.zimbra.client.ZFilterCondition.ZMeCondition) ZAttachmentExistsCondition(com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition) ZStopAction(com.zimbra.client.ZFilterAction.ZStopAction)

Example 2 with ZHeaderCondition

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

the class TestFilterExisting method saveNewRules.

private void saveNewRules() throws Exception {
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // if subject contains "tag", tag
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "tag"));
    actions.add(new ZTagAction(TAG_NAME));
    rules.add(new ZFilterRule(TAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "flag", flag
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "flag"));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule(FLAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "mark read", mark read
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "mark read"));
    actions.add(new ZMarkAction(MarkOp.READ));
    rules.add(new ZFilterRule(MARK_READ_RULE_NAME, true, false, conditions, actions));
    // if subject contains "keep", keep
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "keep"));
    actions.add(new ZKeepAction());
    rules.add(new ZFilterRule(KEEP_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder1", file into folder1
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder1"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    rules.add(new ZFilterRule(FOLDER1_RULE_NAME, true, false, conditions, actions));
    // if the subject contains "folder1 and flag", file into folder1 and flag
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder1 and flag"));
    actions.add(new ZFileIntoAction(FOLDER1_PATH));
    actions.add(new ZMarkAction(MarkOp.FLAGGED));
    rules.add(new ZFilterRule(FOLDER1_AND_FLAG_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder2", file into folder2
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder2"));
    actions.add(new ZFileIntoAction(FOLDER2_PATH));
    rules.add(new ZFilterRule(FOLDER2_RULE_NAME, true, false, conditions, actions));
    // if subject contains "folder3", file into folder3.  This one uses the
    // folder name without the leading slash.
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "folder3"));
    actions.add(new ZFileIntoAction(FOLDER3_NAME));
    rules.add(new ZFilterRule(FOLDER3_RULE_NAME, true, false, conditions, actions));
    // if subject contains "discard", discard
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "discard"));
    actions.add(new ZDiscardAction());
    rules.add(new ZFilterRule(DISCARD_RULE_NAME, true, false, conditions, actions));
    // If subject contains "redirect", redirect to user2.  This rule should
    // be ignored when applied to existing messages.
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZHeaderCondition("subject", HeaderOp.CONTAINS, "redirect"));
    actions.add(new ZRedirectAction(TestUtil.getAddress(USER_NAME2)));
    rules.add(new ZFilterRule(REDIRECT_RULE_NAME, true, false, conditions, actions));
    // Save rules
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    mbox.saveIncomingFilterRules(new ZFilterRules(rules));
}
Also used : ZDiscardAction(com.zimbra.client.ZFilterAction.ZDiscardAction) 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) ZRedirectAction(com.zimbra.client.ZFilterAction.ZRedirectAction) ZMailbox(com.zimbra.client.ZMailbox) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZKeepAction(com.zimbra.client.ZFilterAction.ZKeepAction) ZMarkAction(com.zimbra.client.ZFilterAction.ZMarkAction) ZFilterRules(com.zimbra.client.ZFilterRules)

Example 3 with ZHeaderCondition

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

the class TestSpam method testSpamHandler.

@Test
public void testSpamHandler() throws Exception {
    TestUtil.createAccount(REMOTE_USER_NAME);
    TestUtil.createAccount(SPAM_NAME);
    TestUtil.createAccount(HAM_NAME);
    //check if AS is installed
    List<String> zimbraServiceInstalled = Arrays.asList(prov.getLocalServer().getServiceInstalled());
    if (zimbraServiceInstalled == null || zimbraServiceInstalled.isEmpty() || !zimbraServiceInstalled.contains("antispam")) {
        return;
    }
    Config config = prov.getConfig();
    config.setSpamIsSpamAccount(TestUtil.getAddress(SPAM_NAME));
    config.setSpamIsNotSpamAccount(TestUtil.getAddress(HAM_NAME));
    // Set filter rule.
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    ZFilterCondition cond = new ZHeaderCondition("Subject", ZFilterCondition.HeaderOp.CONTAINS, NAME_PREFIX);
    ZFolder spamFolder = mbox.getFolderById(Integer.toString(Mailbox.ID_FOLDER_SPAM));
    ZFolder inboxFolder = mbox.getFolderById(Integer.toString(Mailbox.ID_FOLDER_INBOX));
    ZFilterAction action = new ZFileIntoAction(spamFolder.getPath());
    ZFilterRule rule = new ZFilterRule(NAME_PREFIX + " testSpamHandler", true, true, Arrays.asList(cond), Arrays.asList(action));
    ZFilterRules rules = new ZFilterRules(Arrays.asList(rule));
    mbox.saveIncomingFilterRules(rules);
    // Confirm that the message was delivered to the Spam folder and that the report was sent.
    String subject = NAME_PREFIX + " testSpamHandler";
    TestUtil.addMessageLmtp(subject, USER_NAME, USER_NAME);
    ZMessage msg = TestUtil.getMessage(mbox, "in:" + spamFolder.getPath() + " subject:\"" + subject + "\"");
    ZMailbox spamMbox = TestUtil.getZMailbox(SPAM_NAME);
    ZMessage reportMsg = TestUtil.waitForMessage(spamMbox, "zimbra-spam-report spam");
    validateSpamReport(TestUtil.getContent(spamMbox, reportMsg.getId()), TestUtil.getAddress(USER_NAME), "spam", "filter", null, spamFolder.getPath(), null);
    spamMbox.deleteMessage(reportMsg.getId());
    // Move out of spam folder.
    mbox.moveMessage(msg.getId(), Integer.toString(Mailbox.ID_FOLDER_INBOX));
    ZMailbox hamMbox = TestUtil.getZMailbox(HAM_NAME);
    reportMsg = TestUtil.waitForMessage(hamMbox, "zimbra-spam-report ham");
    validateSpamReport(TestUtil.getContent(hamMbox, reportMsg.getId()), TestUtil.getAddress(USER_NAME), "ham", "move", spamFolder.getPath(), inboxFolder.getPath(), null);
    hamMbox.deleteMessage(reportMsg.getId());
    // Move back to spam folder.
    mbox.moveMessage(msg.getId(), Integer.toString(Mailbox.ID_FOLDER_SPAM));
    reportMsg = TestUtil.waitForMessage(spamMbox, "zimbra-spam-report spam");
    validateSpamReport(TestUtil.getContent(spamMbox, reportMsg.getId()), TestUtil.getAddress(USER_NAME), "spam", "move", inboxFolder.getPath(), spamFolder.getPath(), null);
    spamMbox.deleteMessage(reportMsg.getId());
    // Move to remote folder.
    ZMailbox remoteMbox = TestUtil.getZMailbox(REMOTE_USER_NAME);
    String mountpointPath = NAME_PREFIX + " remote";
    TestUtil.createMountpoint(remoteMbox, "/Inbox", mbox, mountpointPath);
    ZFolder mountpoint = mbox.getFolderByPath(mountpointPath);
    mbox.moveMessage(msg.getId(), mountpoint.getId());
    reportMsg = TestUtil.waitForMessage(hamMbox, "zimbra-spam-report ham");
    validateSpamReport(TestUtil.getContent(hamMbox, reportMsg.getId()), TestUtil.getAddress(USER_NAME), "ham", "remote move", spamFolder.getPath(), inboxFolder.getPath(), TestUtil.getAddress(REMOTE_USER_NAME));
    hamMbox.deleteMessage(reportMsg.getId());
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZMailbox(com.zimbra.client.ZMailbox) Config(com.zimbra.cs.account.Config) ZHeaderCondition(com.zimbra.client.ZFilterCondition.ZHeaderCondition) ZFilterCondition(com.zimbra.client.ZFilterCondition) ZFileIntoAction(com.zimbra.client.ZFilterAction.ZFileIntoAction) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZFolder(com.zimbra.client.ZFolder) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Example 4 with ZHeaderCondition

use of com.zimbra.client.ZFilterCondition.ZHeaderCondition 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 5 with ZHeaderCondition

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

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