use of org.apache.jsieve.parser.generated.TokenMgrError in project zm-mailbox by Zimbra.
the class RuleManager method applyRulesToOutgoingMessage.
public static List<ItemId> applyRulesToOutgoingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int sentFolderId, boolean noICal, int flags, String[] tags, int convId) throws ServiceException {
List<ItemId> addedMessageIds = null;
OutgoingMessageHandler handler = new OutgoingMessageHandler(mailbox, pm, sentFolderId, noICal, flags, tags, convId, octxt);
ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
try {
Account account = mailbox.getAccount();
Node node = getRulesNode(account, FilterType.OUTGOING, true);
if (node != null) {
SIEVE_FACTORY.evaluate(mailAdapter, node);
// multiple fileinto may result in multiple copies of the messages in different folders
addedMessageIds = mailAdapter.getAddedMessageIds();
}
} catch (Exception e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
} catch (TokenMgrError e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
}
if (addedMessageIds == null) {
// Filter rules were not processed. File to the default folder.
Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
addedMessageIds = new ArrayList<ItemId>(1);
addedMessageIds.add(new ItemId(msg));
}
return addedMessageIds;
}
use of org.apache.jsieve.parser.generated.TokenMgrError in project zm-mailbox by Zimbra.
the class RuleManager method setRules.
/**
* Saves the filter rules.
*
* @param entry the account/domain/cos/server for which the rules are to be saved
* @param script the sieve script, or <code>null</code> or empty string if
* all rules should be deleted
* @param sieveScriptAttrName
* @param rulesCacheKey
* @throws ServiceException
*/
private static void setRules(Entry entry, String script, String sieveScriptAttrName, String rulesCacheKey) throws ServiceException {
if (entry instanceof Account) {
ZimbraLog.filter.debug("Setting filter rules for account %s:\n%s", ((Account) entry).getName(), script);
} else if (entry instanceof Domain) {
ZimbraLog.filter.debug("Setting filter rules for domian %s:\n%s", ((Domain) entry).getName(), script);
} else if (entry instanceof Cos) {
ZimbraLog.filter.debug("Setting filter rules for cos %s:\n%s", ((Cos) entry).getName(), script);
} else if (entry instanceof Server) {
ZimbraLog.filter.debug("Setting filter rules for server %s:\n%s", ((Server) entry).getName(), script);
}
if (script == null) {
script = "";
}
try {
Node node = parse(script);
// evaluate against dummy mail adapter to catch more errors
SIEVE_FACTORY.evaluate(new DummyMailAdapter(), node);
// save
Map<String, Object> attrs = new HashMap<String, Object>();
attrs.put(sieveScriptAttrName, script);
Provisioning.getInstance().modifyAttrs(entry, attrs);
entry.setCachedData(rulesCacheKey, node);
} catch (ParseException e) {
ZimbraLog.filter.error("Unable to parse script:\n" + script);
throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
} catch (TokenMgrError e) {
ZimbraLog.filter.error("Unable to parse script:\n" + script);
throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
} catch (SieveException e) {
ZimbraLog.filter.error("Unable to evaluate script:\n" + script);
throw ServiceException.PARSE_ERROR("evaluating Sieve script", e);
}
}
use of org.apache.jsieve.parser.generated.TokenMgrError in project zm-mailbox by Zimbra.
the class RuleManager method applyRulesToIncomingMessage.
/**
* Adds a message to a mailbox. If filter rules exist, processes
* the filter rules. Otherwise, files to <tt>Inbox</tt> or <tt>Spam</tt>.
*
* @param allowFilterToMountpoint if <tt>false</tt>, rules
* @return the list of message id's that were added, or an empty list.
*/
public static List<ItemId> applyRulesToIncomingMessage(OperationContext octxt, Mailbox mailbox, ParsedMessage pm, int size, String recipient, LmtpEnvelope env, DeliveryContext sharedDeliveryCtxt, int incomingFolderId, boolean noICal, boolean allowFilterToMountpoint) throws ServiceException {
List<ItemId> addedMessageIds = null;
IncomingMessageHandler handler = new IncomingMessageHandler(octxt, sharedDeliveryCtxt, mailbox, recipient, pm, size, incomingFolderId, noICal);
ZimbraMailAdapter mailAdapter = new ZimbraMailAdapter(mailbox, handler);
mailAdapter.setEnvelope(env);
mailAdapter.setAllowFilterToMountpoint(allowFilterToMountpoint);
try {
Account account = mailbox.getAccount();
Node node = getRulesNode(account, FilterType.INCOMING, true);
// Determine whether to apply rules
boolean applyRules = true;
if (node == null) {
applyRules = false;
}
if (SpamHandler.isSpam(handler.getMimeMessage()) && !account.getBooleanAttr(Provisioning.A_zimbraSpamApplyUserFilters, false)) {
// Don't apply user filters to spam by default
applyRules = false;
}
if (applyRules) {
SIEVE_FACTORY.evaluate(mailAdapter, node);
// multiple fileinto may result in multiple copies of the messages in different folders
addedMessageIds = mailAdapter.getAddedMessageIds();
}
} catch (ErejectException ex) {
throw DeliveryServiceException.DELIVERY_REJECTED(ex.getMessage(), ex);
} catch (Exception e) {
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
} catch (TokenMgrError e) {
// Workaround for bug 19576. Certain parse errors can cause JavaCC to throw an Error
// instead of an Exception. Woo.
ZimbraLog.filter.warn("An error occurred while processing filter rules. Filing message to %s.", handler.getDefaultFolderPath(), e);
}
if (addedMessageIds == null) {
// Filter rules were not processed. File to the default folder.
Message msg = mailAdapter.keep(KeepType.IMPLICIT_KEEP);
addedMessageIds = new ArrayList<ItemId>(1);
addedMessageIds.add(new ItemId(msg));
}
return addedMessageIds;
}
use of org.apache.jsieve.parser.generated.TokenMgrError in project zm-mailbox by Zimbra.
the class RuleManager method parse.
/**
* Parses the sieve script and returns the root to the resulting node tree.
*/
public static Node parse(String script) throws ParseException {
ByteArrayInputStream sin;
try {
sin = new ByteArrayInputStream(script.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new ParseException(e.getMessage());
}
Node node = null;
try {
node = SIEVE_FACTORY.parse(sin);
} catch (TokenMgrError e) {
// filter string without any undefined escape sequences.
try {
sin = new ByteArrayInputStream(ignoreBackslash(script).getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
throw new ParseException(uee.getMessage());
}
node = SIEVE_FACTORY.parse(sin);
}
return node;
}
use of org.apache.jsieve.parser.generated.TokenMgrError in project zm-mailbox by Zimbra.
the class RuleManager method getRulesAsXML.
private static List<FilterRule> getRulesAsXML(Account account, FilterType filterType) throws ServiceException {
Node node;
try {
node = getRulesNode(account, filterType, false);
} catch (ParseException e) {
throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
} catch (TokenMgrError e) {
throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
}
String sieveScriptAttrName = null;
if (filterType == FilterType.INCOMING) {
sieveScriptAttrName = Provisioning.A_zimbraMailSieveScript;
} else {
sieveScriptAttrName = Provisioning.A_zimbraMailOutgoingSieveScript;
}
SieveToSoap sieveToSoap = new SieveToSoap(getRuleNames(account.getAttr(sieveScriptAttrName)));
sieveToSoap.accept(node);
return sieveToSoap.toFilterRules();
}
Aggregations