Search in sources :

Example 1 with TokenMgrError

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;
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ItemId(com.zimbra.cs.service.util.ItemId) ServiceException(com.zimbra.common.service.ServiceException) ParseException(org.apache.jsieve.parser.generated.ParseException) SieveException(org.apache.jsieve.exception.SieveException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 2 with TokenMgrError

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);
    }
}
Also used : Account(com.zimbra.cs.account.Account) Server(com.zimbra.cs.account.Server) HashMap(java.util.HashMap) Cos(com.zimbra.cs.account.Cos) Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) SieveException(org.apache.jsieve.exception.SieveException) ParseException(org.apache.jsieve.parser.generated.ParseException) Domain(com.zimbra.cs.account.Domain)

Example 3 with TokenMgrError

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;
}
Also used : Account(com.zimbra.cs.account.Account) Message(com.zimbra.cs.mailbox.Message) ParsedMessage(com.zimbra.cs.mime.ParsedMessage) Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) ItemId(com.zimbra.cs.service.util.ItemId) ServiceException(com.zimbra.common.service.ServiceException) ParseException(org.apache.jsieve.parser.generated.ParseException) SieveException(org.apache.jsieve.exception.SieveException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) DeliveryServiceException(com.zimbra.common.service.DeliveryServiceException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with TokenMgrError

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;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.apache.jsieve.parser.generated.Node) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ParseException(org.apache.jsieve.parser.generated.ParseException)

Example 5 with TokenMgrError

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();
}
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)6 ParseException (org.apache.jsieve.parser.generated.ParseException)6 TokenMgrError (org.apache.jsieve.parser.generated.TokenMgrError)6 Account (com.zimbra.cs.account.Account)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SieveException (org.apache.jsieve.exception.SieveException)3 DeliveryServiceException (com.zimbra.common.service.DeliveryServiceException)2 ServiceException (com.zimbra.common.service.ServiceException)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 IOException (java.io.IOException)2 Cos (com.zimbra.cs.account.Cos)1 Domain (com.zimbra.cs.account.Domain)1 Server (com.zimbra.cs.account.Server)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 HashMap (java.util.HashMap)1