Search in sources :

Example 6 with FilterVariables

use of com.zimbra.soap.mail.type.FilterVariables 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)

Example 7 with FilterVariables

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

the class SieveToSoap method setCurrentVariables.

private void setCurrentVariables() {
    if (currentVariables != null && !currentVariables.isEmpty()) {
        currentRule.setFilterVariables(new FilterVariables(currentVariables));
        currentVariables = null;
    }
}
Also used : FilterVariables(com.zimbra.soap.mail.type.FilterVariables)

Example 8 with FilterVariables

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

the class SieveToSoap method initCurrentRule.

private void initCurrentRule(RuleProperties props) {
    currentRule = new FilterRule(getCurrentRuleName(), props.isEnabled());
    setCurrentVariables();
    if (actionVariables != null && !actionVariables.isEmpty()) {
        currentRule.addFilterAction(new FilterVariables(actionVariables));
    }
    FilterTests tests = props.getCondition() != null ? new FilterTests(props.getCondition().toString()) : new FilterTests(null);
    currentRule.setFilterTests(tests);
    rules.add(currentRule);
    currentRuleIndex++;
    // When start working on the root rule, initialise the pointer to nested rule
    currentNestedRule = null;
}
Also used : FilterTests(com.zimbra.soap.mail.type.FilterTests) FilterRule(com.zimbra.soap.mail.type.FilterRule) FilterVariables(com.zimbra.soap.mail.type.FilterVariables)

Example 9 with FilterVariables

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

the class SoapToSieve method handleNest.

// Constructing nested rule block with base indents which is for entire block.
private String handleNest(String baseIndents, NestedRule currentNestedRule, boolean isAdminScript) throws ServiceException {
    StringBuilder nestedIfBlock = new StringBuilder();
    FilterVariables filterVariables = currentNestedRule.getFilterVariables();
    nestedIfBlock.append(handleVariables(filterVariables, baseIndents));
    Sieve.Condition childCondition = Sieve.Condition.fromString(currentNestedRule.getFilterTests().getCondition());
    if (childCondition == null) {
        childCondition = Sieve.Condition.allof;
    }
    // assuming no disabled_if for child tests so far
    nestedIfBlock.append(baseIndents).append("if ");
    nestedIfBlock.append(childCondition).append(" (");
    // Handle tests
    // sort by index
    Map<Integer, String> index2childTest = new TreeMap<Integer, String>();
    for (FilterTest childTest : currentNestedRule.getFilterTests().getTests()) {
        String childResult = handleTest(childTest);
        if (childResult != null) {
            FilterUtil.addToMap(index2childTest, childTest.getIndex(), childResult);
        }
    }
    Joiner.on(",\n  " + baseIndents).appendTo(nestedIfBlock, index2childTest.values());
    nestedIfBlock.append(") {\n");
    // Handle actions
    // sort by index
    Map<Integer, String> index2childAction = new TreeMap<Integer, String>();
    List<FilterAction> childActions = currentNestedRule.getFilterActions();
    String variables = "";
    if (childActions != null) {
        for (FilterAction childAction : childActions) {
            if (childAction instanceof FilterVariables) {
                FilterVariables var = (FilterVariables) childAction;
                variables = handleVariables(var, baseIndents + "    ");
            } else {
                String childResult = handleAction(childAction, isAdminScript);
                if (childResult != null) {
                    FilterUtil.addToMap(index2childAction, childAction.getIndex(), childResult);
                }
            }
        }
        for (String childAction : index2childAction.values()) {
            nestedIfBlock.append(baseIndents);
            nestedIfBlock.append("    ").append(childAction).append(END_OF_LINE);
        }
        if (!variables.isEmpty()) {
            nestedIfBlock.append(variables);
        }
    }
    // Handle nest
    if (currentNestedRule.getChild() != null) {
        nestedIfBlock.append(handleNest(baseIndents + "    ", currentNestedRule.getChild(), isAdminScript));
    }
    if (childActions == null && currentNestedRule.getChild() == null) {
        // If there is no more nested rule, there should be at least one action.
        throw ServiceException.INVALID_REQUEST("Missing action", null);
    }
    nestedIfBlock.append(baseIndents);
    nestedIfBlock.append("}\n");
    return nestedIfBlock.toString();
}
Also used : FilterTest(com.zimbra.soap.mail.type.FilterTest) Sieve(com.zimbra.common.filter.Sieve) TreeMap(java.util.TreeMap) FilterVariables(com.zimbra.soap.mail.type.FilterVariables) FilterAction(com.zimbra.soap.mail.type.FilterAction)

Aggregations

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