Search in sources :

Example 1 with NestedRule

use of com.zimbra.soap.mail.type.NestedRule in project zm-mailbox by Zimbra.

the class SieveToSoap method initRule.

private void initRule(RuleProperties props) {
    if (!isNestedRule()) {
        initCurrentRule(props);
    } else {
        // set new Nested Rule instance as child of current one.
        FilterTests tests = props.getCondition() != null ? new FilterTests(props.getCondition().toString()) : new FilterTests(null);
        NestedRule nestedRule = new NestedRule(tests);
        if (currentNestedRule != null) {
            // some nested rule has been already processed
            // set it as child of previous one
            currentNestedRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentNestedRule.addFilterAction(new FilterVariables(actionVariables));
            }
        } else {
            // set it as child of root rule
            if (null == currentRule) {
                initCurrentRule(props);
            }
            currentRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentRule.addFilterAction(new FilterVariables(actionVariables));
            }
        }
        currentNestedRule = nestedRule;
        actionVariables = null;
    }
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) NestedRule(com.zimbra.soap.mail.type.NestedRule) FilterVariables(com.zimbra.soap.mail.type.FilterVariables)

Example 2 with NestedRule

use of com.zimbra.soap.mail.type.NestedRule in project zm-mailbox by Zimbra.

the class SoapToSieve method handleRule.

private void handleRule(FilterRule rule, boolean isAdminScript) throws ServiceException {
    String name = rule.getName();
    boolean active = rule.isActive();
    // Rule name
    buffer.append("# ").append(name).append('\n');
    FilterVariables filterVariables = rule.getFilterVariables();
    buffer.append(handleVariables(filterVariables, null));
    FilterTests tests = rule.getFilterTests();
    if (hasTest(tests)) {
        Sieve.Condition condition = Sieve.Condition.fromString(tests.getCondition());
        if (condition == null) {
            condition = Sieve.Condition.allof;
        }
        if (active) {
            buffer.append("if ");
        } else {
            buffer.append("disabled_if ");
        }
        buffer.append(condition).append(" (");
        // Handle tests
        // sort by index
        Map<Integer, String> index2test = new TreeMap<Integer, String>();
        for (FilterTest test : tests.getTests()) {
            String result = handleTest(test);
            if (result != null) {
                FilterUtil.addToMap(index2test, test.getIndex(), result);
            }
        }
        Joiner.on(",\n  ").appendTo(buffer, index2test.values());
        buffer.append(") {\n");
    }
    // Handle actions
    // sort by index
    Map<Integer, String> index2action = new TreeMap<Integer, String>();
    List<FilterAction> filterActions = rule.getFilterActions();
    String variables = "";
    if (filterActions != null) {
        for (FilterAction action : filterActions) {
            if (action instanceof FilterVariables) {
                FilterVariables var = (FilterVariables) action;
                variables = handleVariables(var, "    ");
            } else {
                String result = handleAction(action, isAdminScript);
                if (result != null) {
                    FilterUtil.addToMap(index2action, action.getIndex(), result);
                }
            }
        }
        for (String action : index2action.values()) {
            if (hasTest(tests)) {
                buffer.append("    ");
            }
            buffer.append(action).append(END_OF_LINE);
        }
        if (!variables.isEmpty()) {
            buffer.append(variables);
        }
    }
    NestedRule child = rule.getChild();
    // Handle nested rule
    if (child != null) {
        // first nested block's indent is "    "
        String nestedRuleBlock = handleNest("    ", child, isAdminScript);
        buffer.append(nestedRuleBlock);
    }
    if (filterActions == null && child == null) {
        // If there is no more nested rule, there should be at least one action.
        throw ServiceException.INVALID_REQUEST("Missing action", null);
    }
    if (hasTest(tests)) {
        buffer.append("}\n");
    }
}
Also used : NestedRule(com.zimbra.soap.mail.type.NestedRule) TreeMap(java.util.TreeMap) FilterVariables(com.zimbra.soap.mail.type.FilterVariables) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) Sieve(com.zimbra.common.filter.Sieve) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 3 with NestedRule

use of com.zimbra.soap.mail.type.NestedRule in project zm-mailbox by Zimbra.

the class SoapToSieve method handleRule.

private void handleRule(FilterRule rule) throws ServiceException {
    String name = rule.getName();
    boolean active = rule.isActive();
    // Rule name
    buffer.append("# ").append(name).append('\n');
    FilterVariables filterVariables = rule.getFilterVariables();
    buffer.append(handleVariables(filterVariables, null));
    FilterTests tests = rule.getFilterTests();
    Sieve.Condition condition = Sieve.Condition.fromString(tests.getCondition());
    if (condition == null) {
        condition = Sieve.Condition.allof;
    }
    if (active) {
        buffer.append("if ");
    } else {
        buffer.append("disabled_if ");
    }
    buffer.append(condition).append(" (");
    // Handle tests
    // sort by index
    Map<Integer, String> index2test = new TreeMap<Integer, String>();
    for (FilterTest test : tests.getTests()) {
        String result = handleTest(test);
        if (result != null) {
            FilterUtil.addToMap(index2test, test.getIndex(), result);
        }
    }
    Joiner.on(",\n  ").appendTo(buffer, index2test.values());
    buffer.append(") {\n");
    // Handle actions
    // sort by index
    Map<Integer, String> index2action = new TreeMap<Integer, String>();
    List<FilterAction> filterActions = rule.getFilterActions();
    String variables = "";
    if (filterActions != null) {
        for (FilterAction action : filterActions) {
            if (action instanceof FilterVariables) {
                FilterVariables var = (FilterVariables) action;
                variables = handleVariables(var, "    ");
            } else {
                String result = handleAction(action);
                if (result != null) {
                    FilterUtil.addToMap(index2action, action.getIndex(), result);
                }
            }
        }
        for (String action : index2action.values()) {
            buffer.append("    ").append(action).append(END_OF_LINE);
        }
        if (!variables.isEmpty()) {
            buffer.append(variables);
        }
    }
    NestedRule child = rule.getChild();
    // Handle nested rule
    if (child != null) {
        // first nested block's indent is "    "
        String nestedRuleBlock = handleNest("    ", child);
        buffer.append(nestedRuleBlock);
    }
    if (filterActions == null && child == null) {
        // If there is no more nested rule, there should be at least one action.
        throw ServiceException.INVALID_REQUEST("Missing action", null);
    }
    buffer.append("}\n");
}
Also used : NestedRule(com.zimbra.soap.mail.type.NestedRule) TreeMap(java.util.TreeMap) FilterVariables(com.zimbra.soap.mail.type.FilterVariables) FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterTest(com.zimbra.soap.mail.type.FilterTest) Sieve(com.zimbra.common.filter.Sieve) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Example 4 with NestedRule

use of com.zimbra.soap.mail.type.NestedRule in project zm-mailbox by Zimbra.

the class SieveToSoap method visitRule.

@Override
protected void visitRule(Node ruleNode, VisitPhase phase, RuleProperties props) {
    if (phase == VisitPhase.end) {
        return;
    }
    if (!isNestedRule()) {
        currentRule = new FilterRule(getCurrentRuleName(), props.isEnabled);
        setCurrentVariables();
        if (actionVariables != null && !actionVariables.isEmpty()) {
            currentRule.addFilterAction(new FilterVariables(actionVariables));
        }
        if (actionVariables != null && !actionVariables.isEmpty()) {
            currentRule.addFilterAction(new FilterVariables(actionVariables));
        }
        currentRule.setFilterTests(new FilterTests(props.condition.toString()));
        rules.add(currentRule);
        currentRuleIndex++;
        // When start working on the root rule, initialise the pointer to nested rule
        currentNestedRule = null;
    } else {
        // set new Nested Rule instance as child of current one.
        NestedRule nestedRule = new NestedRule(new FilterTests(props.condition.toString()));
        if (currentNestedRule != null) {
            // some nested rule has been already processed
            // set it as child of previous one
            currentNestedRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentNestedRule.addFilterAction(new FilterVariables(actionVariables));
            }
        } else {
            // first nested rule
            // set it as child of root rule
            currentRule.setChild(nestedRule);
            setCurrentVariables();
            if (actionVariables != null && !actionVariables.isEmpty()) {
                currentRule.addFilterAction(new FilterVariables(actionVariables));
            }
        }
        currentNestedRule = nestedRule;
        actionVariables = null;
    }
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) NestedRule(com.zimbra.soap.mail.type.NestedRule) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterVariables(com.zimbra.soap.mail.type.FilterVariables)

Aggregations

FilterTests (com.zimbra.soap.mail.type.FilterTests)4 FilterVariables (com.zimbra.soap.mail.type.FilterVariables)4 NestedRule (com.zimbra.soap.mail.type.NestedRule)4 Sieve (com.zimbra.common.filter.Sieve)2 FilterAction (com.zimbra.soap.mail.type.FilterAction)2 FilterTest (com.zimbra.soap.mail.type.FilterTest)2 TreeMap (java.util.TreeMap)2 FilterRule (com.zimbra.soap.mail.type.FilterRule)1