Search in sources :

Example 16 with Node

use of org.apache.jsieve.parser.generated.Node in project zm-mailbox by Zimbra.

the class RuleRewriter method traverse.

private void traverse(Node node) {
    int numChildren = node.jjtGetNumChildren();
    int nameIndex = 0;
    for (int i = 0; i < numChildren; i++) {
        Node childNode = node.jjtGetChild(i);
        String name = ((SieveNode) childNode).getName();
        if (childNode instanceof ASTcommand && ("if".equals(name) || "elsif".equals(name) || "disabled_if".equals(name))) {
            String ruleName = "";
            if (mRuleNames != null && nameIndex < mRuleNames.size()) {
                ruleName = mRuleNames.get(nameIndex);
                nameIndex++;
            }
            Element ruleElem = mRoot.addElement(MailConstants.E_RULE).addAttribute(MailConstants.A_NAME, ruleName);
            ruleElem.addAttribute(MailConstants.A_ACTIVE, !"disabled_if".equals(name));
            rule(ruleElem, childNode);
        } else {
            traverse(childNode);
        }
    }
}
Also used : SieveNode(org.apache.jsieve.parser.SieveNode) Node(org.apache.jsieve.parser.generated.Node) SieveNode(org.apache.jsieve.parser.SieveNode) ASTcommand(org.apache.jsieve.parser.generated.ASTcommand) Element(com.zimbra.common.soap.Element) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 17 with Node

use of org.apache.jsieve.parser.generated.Node in project zm-mailbox by Zimbra.

the class RuleRewriter method rule.

private void rule(Element elem, Node parent) {
    int numChildren = parent.jjtGetNumChildren();
    for (int i = 0; i < numChildren; i++) {
        Node node = parent.jjtGetChild(i);
        String name = ((SieveNode) node).getName();
        if (node instanceof ASTtest) {
            if ("anyof".equals(name) || "allof".equals(name)) {
                Element condsElem = elem.addElement(MailConstants.E_CONDITION_GROUP).addAttribute(MailConstants.A_OPERATION, name);
                rule(condsElem, node);
            } else if ("not".equals(name)) {
                mStack.push(name);
                rule(elem, node);
            } else {
                if ("exists".equals(name) && !mStack.isEmpty()) {
                    name = mStack.pop() + " " + name;
                }
                Element cElem = elem.addElement(MailConstants.E_CONDITION).addAttribute(MailConstants.A_NAME, name);
                x = 0;
                test(cElem, node);
            }
        } else if (node instanceof ASTcommand) {
            Element actionElem = elem.addElement(MailConstants.E_ACTION).addAttribute(MailConstants.A_NAME, ((SieveNode) node).getName());
            action(actionElem, node);
        } else {
            rule(elem, node);
        }
    }
}
Also used : SieveNode(org.apache.jsieve.parser.SieveNode) Node(org.apache.jsieve.parser.generated.Node) SieveNode(org.apache.jsieve.parser.SieveNode) Element(com.zimbra.common.soap.Element) ASTcommand(org.apache.jsieve.parser.generated.ASTcommand) ASTtest(org.apache.jsieve.parser.generated.ASTtest) Mountpoint(com.zimbra.cs.mailbox.Mountpoint)

Example 18 with Node

use of org.apache.jsieve.parser.generated.Node in project zm-mailbox by Zimbra.

the class SieveVisitor method accept.

private void accept(Node parent, RuleProperties props) throws ServiceException {
    visitNode(parent, VisitPhase.begin, props);
    int numChildren = parent.jjtGetNumChildren();
    for (int i = 0; i < numChildren; i++) {
        Node node = parent.jjtGetChild(i);
        if (isRuleNode(node)) {
            // New rule tree or Nested if. New RuleProperties is created for each nested if
            RuleProperties newProps = new RuleProperties();
            if ("disabled_if".equalsIgnoreCase(getNodeName(node))) {
                newProps.isEnabled = false;
            }
            visitIfControl(node, VisitPhase.begin, newProps);
            accept(node, newProps);
            visitIfControl(node, VisitPhase.end, newProps);
        } else if (node instanceof ASTtest) {
            acceptTest(node, props);
        } else if (node instanceof ASTcommand) {
            acceptAction(node, props);
        } else {
            accept(node, props);
        }
    }
    visitNode(parent, VisitPhase.end, props);
}
Also used : Node(org.apache.jsieve.parser.generated.Node) SieveNode(org.apache.jsieve.parser.SieveNode) ASTcommand(org.apache.jsieve.parser.generated.ASTcommand) ASTtest(org.apache.jsieve.parser.generated.ASTtest)

Example 19 with Node

use of org.apache.jsieve.parser.generated.Node in project zm-mailbox by Zimbra.

the class RuleManager method getRulesNode.

/**
     * Returns the parsed filter rules for the given domain.  If no cached
     * copy of the parsed rules exists, parses the script returned by
     * {@link #getRules(com.zimbra.cs.account.Entry, String)} and caches the result on the <tt>Account</tt>.
     *
     * @param entry the owner domain/cos/server of the filter rule
     * @param filterType <tt>FilterType.INCOMING</tt> or <tt>FilterType.OUTGOING</tt>
     * @param afType <tt>AdminFilterType.BEFORE</tt> or <tt>AdminFilterType.AFTER</tt>
     *
     * @see Entry#setCachedData(String, Object)
     * @throws ParseException if there was an error while parsing the Sieve script
     * @throws ServiceException 
     */
private static Node getRulesNode(Entry entry, FilterType filterType, AdminFilterType afType) throws ParseException, ServiceException {
    String rulesCacheKey = getAdminScriptCacheKey(filterType, afType);
    String adminRuleAttrName = getAdminScriptAttributeName(filterType, afType);
    Node node = (Node) entry.getCachedData(rulesCacheKey);
    if (node == null) {
        String script = null;
        String debugScript = null;
        StringBuilder requiresPart = new StringBuilder();
        String adminRule = entry.getAttr(adminRuleAttrName);
        String debugAdminRule = adminRule;
        if (adminRule == null) {
            debugAdminRule = "";
            adminRule = "";
        } else {
            List<String> splits = splitScript(adminRule);
            requiresPart.append(splits.get(0));
            debugAdminRule = splits.get(1);
        }
        /*
             * Since "require" is only allowed before other commands,
             * the "require" part at the beginning of each sieve rule
             * text will be cut, and pasted into the very top of the
             * combined rule. RFC-wise, it is okay to declare the
             * "require" commands multiple times.
             */
        script = requiresPart.toString() + adminRule;
        debugScript = requiresPart.toString() + "\n# " + adminRuleAttrName + " script\n" + debugAdminRule;
        ZimbraLog.filter.debug("filterType[%s] rule[%s]", filterType == FilterType.INCOMING ? "incoming" : "outgoing", debugScript);
        node = parse(script);
        entry.setCachedData(rulesCacheKey, node);
    }
    return node;
}
Also used : Node(org.apache.jsieve.parser.generated.Node)

Example 20 with Node

use of org.apache.jsieve.parser.generated.Node in project zm-mailbox by Zimbra.

the class RuleManager method getAdminRulesAsXML.

public static List<FilterRule> getAdminRulesAsXML(Entry entry, FilterType filterType, AdminFilterType afType) throws ServiceException {
    Node node;
    try {
        node = getRulesNode(entry, filterType, afType);
    } catch (ParseException | TokenMgrError e) {
        throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
    }
    String sieveScriptAttrName = getAdminScriptAttributeName(filterType, afType);
    SieveToSoap sieveToSoap = new SieveToSoap(getRuleNames(entry.getAttr(sieveScriptAttrName)));
    sieveToSoap.accept(node);
    return sieveToSoap.toFilterRules();
}
Also used : Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ParseException(org.apache.jsieve.parser.generated.ParseException)

Aggregations

Node (org.apache.jsieve.parser.generated.Node)22 ParseException (org.apache.jsieve.parser.generated.ParseException)10 SieveNode (org.apache.jsieve.parser.SieveNode)9 TokenMgrError (org.apache.jsieve.parser.generated.TokenMgrError)6 Mountpoint (com.zimbra.cs.mailbox.Mountpoint)5 ArrayList (java.util.ArrayList)5 Account (com.zimbra.cs.account.Account)4 ServiceException (com.zimbra.common.service.ServiceException)3 Element (com.zimbra.common.soap.Element)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SieveException (org.apache.jsieve.exception.SieveException)3 ASTcommand (org.apache.jsieve.parser.generated.ASTcommand)3 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)2 ErejectException (com.zimbra.cs.filter.jsieve.ErejectException)2 Message (com.zimbra.cs.mailbox.Message)2 ParsedMessage (com.zimbra.cs.mime.ParsedMessage)2 ItemId (com.zimbra.cs.service.util.ItemId)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 List (java.util.List)2