Search in sources :

Example 1 with ZAttachmentExistsCondition

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

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

the class TestFilter method testAttachment.

@Test
public void testAttachment() throws Exception {
    List<ZFilterCondition> conditions = new ArrayList<ZFilterCondition>();
    List<ZFilterAction> actions = new ArrayList<ZFilterAction>();
    List<ZFilterRule> rules = new ArrayList<ZFilterRule>();
    // If attachment exists, set tag1.  If attachment doesn't exist, set tag2.
    ZTag tag1 = mMbox.createTag(NAME_PREFIX + " testAttachment1", null);
    ZTag tag2 = mMbox.createTag(NAME_PREFIX + " testAttachment2", null);
    conditions.add(new ZAttachmentExistsCondition(true));
    actions.add(new ZTagAction(tag1.getName()));
    rules.add(new ZFilterRule("testAttachment1", true, false, conditions, actions));
    conditions = new ArrayList<ZFilterCondition>();
    actions = new ArrayList<ZFilterAction>();
    conditions.add(new ZAttachmentExistsCondition(false));
    actions.add(new ZTagAction(tag2.getName()));
    rules.add(new ZFilterRule("testAttachment2", true, false, conditions, actions));
    ZFilterRules zRules = new ZFilterRules(rules);
    saveIncomingRules(mMbox, zRules);
    // Add a message with an attachment.
    String address = TestUtil.getAddress(USER_NAME);
    String subject = NAME_PREFIX + " testAttachment1";
    String msgContent = new MessageBuilder().withSubject(subject).withAttachment("attach this", "attach.txt", "text/plain").create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    ZMessage msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    Set<String> tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag1.getId()));
    // Add a message with no attachment.
    subject = NAME_PREFIX + " testAttachment2";
    msgContent = new MessageBuilder().withSubject(subject).create();
    TestUtil.addMessageLmtp(new String[] { address }, address, msgContent);
    msg = TestUtil.getMessage(mMbox, "in:inbox subject:\"" + subject + "\"");
    // Check the tag states.
    tagIds = getTagIds(msg);
    assertEquals(1, tagIds.size());
    assertTrue(tagIds.contains(tag2.getId()));
}
Also used : ZMessage(com.zimbra.client.ZMessage) ZFilterCondition(com.zimbra.client.ZFilterCondition) ArrayList(java.util.ArrayList) ZFilterAction(com.zimbra.client.ZFilterAction) ZFilterRule(com.zimbra.client.ZFilterRule) ZTag(com.zimbra.client.ZTag) ZTagAction(com.zimbra.client.ZFilterAction.ZTagAction) ZAttachmentExistsCondition(com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition) ZFilterRules(com.zimbra.client.ZFilterRules) Test(org.junit.Test)

Aggregations

ZTagAction (com.zimbra.client.ZFilterAction.ZTagAction)2 ZAttachmentExistsCondition (com.zimbra.client.ZFilterCondition.ZAttachmentExistsCondition)2 ArrayList (java.util.ArrayList)2 ZFilterAction (com.zimbra.client.ZFilterAction)1 ZDiscardAction (com.zimbra.client.ZFilterAction.ZDiscardAction)1 ZFileIntoAction (com.zimbra.client.ZFilterAction.ZFileIntoAction)1 ZKeepAction (com.zimbra.client.ZFilterAction.ZKeepAction)1 ZMarkAction (com.zimbra.client.ZFilterAction.ZMarkAction)1 ZNotifyAction (com.zimbra.client.ZFilterAction.ZNotifyAction)1 ZRFCCompliantNotifyAction (com.zimbra.client.ZFilterAction.ZRFCCompliantNotifyAction)1 ZRedirectAction (com.zimbra.client.ZFilterAction.ZRedirectAction)1 ZReplyAction (com.zimbra.client.ZFilterAction.ZReplyAction)1 ZStopAction (com.zimbra.client.ZFilterAction.ZStopAction)1 ZFilterCondition (com.zimbra.client.ZFilterCondition)1 SimpleOp (com.zimbra.client.ZFilterCondition.SimpleOp)1 ZAddressBookCondition (com.zimbra.client.ZFilterCondition.ZAddressBookCondition)1 ZAddressCondition (com.zimbra.client.ZFilterCondition.ZAddressCondition)1 ZBodyCondition (com.zimbra.client.ZFilterCondition.ZBodyCondition)1 ZContactRankingCondition (com.zimbra.client.ZFilterCondition.ZContactRankingCondition)1 ZCurrentDayOfWeekCondition (com.zimbra.client.ZFilterCondition.ZCurrentDayOfWeekCondition)1