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);
}
}
}
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);
}
}
}
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);
}
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;
}
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();
}
Aggregations