Search in sources :

Example 1 with Action

use of org.apache.jsieve.mail.Action in project zm-mailbox by Zimbra.

the class ZimbraMailAdapter method getTags.

private String[] getTags() {
    if (tags == null) {
        List<String> taglist = Lists.newArrayList();
        for (Action action : getTagActions()) {
            taglist.add(((ActionTag) action).getTagName());
        }
        tags = taglist.toArray(new String[taglist.size()]);
    }
    return tags;
}
Also used : Action(org.apache.jsieve.mail.Action)

Example 2 with Action

use of org.apache.jsieve.mail.Action in project zm-mailbox by Zimbra.

the class ZimbraMailAdapter method executeActions.

@Override
public void executeActions() throws SieveException {
    try {
        handler.beforeFiltering();
        String messageId = Mime.getMessageID(handler.getMimeMessage());
        // the script contains a single discard action, JSieve returns an empty list.
        if (getActions().size() == 0) {
            ZimbraLog.filter.info("Discarding message with Message-ID %s from %s", messageId, Mime.getSender(handler.getMimeMessage()));
            handler.discard();
            return;
        }
        List<Action> deliveryActions = getDeliveryActions();
        if (deliveryActions.isEmpty()) {
            // i.e. no keep/fileinto/redirect actions
            if (getReplyNotifyRejectActions().isEmpty()) {
                // if only flag/tag actions are present, we keep the message even if discard
                // action is present
                keep(KeepType.EXPLICIT_KEEP);
            } else if (!discardActionPresent) {
                // else if reply/notify/reject/ereject actions are present and there's no discard, do sort
                // of implicit keep
                keep(KeepType.EXPLICIT_KEEP);
            }
        } else {
            ListIterator<Action> li = deliveryActions.listIterator(deliveryActions.size());
            while (li.hasPrevious()) {
                Action lastDeliveryAction = li.previous();
                if (lastDeliveryAction instanceof ActionFileInto) {
                    ActionFileInto lastFileIntoAction = (ActionFileInto) lastDeliveryAction;
                    if (lastFileIntoAction.isCopy() && !discardActionPresent) {
                        keep(KeepType.EXPLICIT_KEEP);
                    }
                    break;
                } else if (lastDeliveryAction instanceof ActionRedirect) {
                    ActionRedirect lastRedirectAction = (ActionRedirect) lastDeliveryAction;
                    if (lastRedirectAction.isCopy() && !discardActionPresent) {
                        keep(KeepType.EXPLICIT_KEEP);
                    }
                    break;
                }
            }
        }
        for (Action action : actions) {
            if (action instanceof ActionKeep) {
                if (context == null) {
                    ZimbraLog.filter.warn("SieveContext has unexpectedly not been set");
                    keep(KeepType.IMPLICIT_KEEP);
                } else if (context.getCommandStateManager().isImplicitKeep()) {
                    // implicit keep: this means that none of the user's rules have been matched
                    // we need to check system spam filter to see if the mail is spam
                    keep(KeepType.IMPLICIT_KEEP);
                } else {
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionFileInto) {
                ActionFileInto fileinto = (ActionFileInto) action;
                String folderPath = fileinto.getDestination();
                folderPath = FilterUtil.replaceVariables(this, folderPath);
                try {
                    if (!allowFilterToMountpoint && isMountpoint(mailbox, folderPath)) {
                        ZimbraLog.filter.info("Filing to mountpoint \"%s\" is not allowed.  Filing to the default folder instead.", folderPath);
                        keep(KeepType.EXPLICIT_KEEP);
                    } else {
                        fileInto(folderPath);
                    }
                } catch (ServiceException e) {
                    ZimbraLog.filter.info("Unable to file message to %s.  Filing to %s instead.", folderPath, handler.getDefaultFolderPath(), e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionRedirect) {
                // redirect mail to another address
                ActionRedirect redirect = (ActionRedirect) action;
                String addr = redirect.getAddress();
                addr = FilterUtil.replaceVariables(this, addr);
                ZimbraLog.filter.info("Redirecting message to %s.", addr);
                try {
                    handler.redirect(addr);
                } catch (Exception e) {
                    ZimbraLog.filter.warn("Unable to redirect to %s.  Filing message to %s.", addr, handler.getDefaultFolderPath(), e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionReply) {
                // reply to mail
                ActionReply reply = (ActionReply) action;
                ZimbraLog.filter.debug("Replying to message");
                try {
                    String replyStrg = FilterUtil.replaceVariables(this, reply.getBodyTemplate());
                    handler.reply(replyStrg);
                } catch (Exception e) {
                    ZimbraLog.filter.warn("Unable to reply.", e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionNotify) {
                ActionNotify notify = (ActionNotify) action;
                ZimbraLog.filter.debug("Sending notification message to %s.", notify.getEmailAddr());
                try {
                    handler.notify(FilterUtil.replaceVariables(this, notify.getEmailAddr()), FilterUtil.replaceVariables(this, notify.getSubjectTemplate()), FilterUtil.replaceVariables(this, notify.getBodyTemplate()), notify.getMaxBodyBytes(), notify.getOrigHeaders());
                } catch (Exception e) {
                    ZimbraLog.filter.warn("Unable to notify.", e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionReject) {
                ActionReject reject = (ActionReject) action;
                ZimbraLog.filter.debug("Refusing delivery of a message: %s", reject.getMessage());
                try {
                    String msg = FilterUtil.replaceVariables(this, reject.getMessage());
                    handler.reject(msg, envelope);
                    handler.discard();
                } catch (Exception e) {
                    ZimbraLog.filter.info("Unable to reject.", e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            } else if (action instanceof ActionEreject) {
                ActionEreject ereject = (ActionEreject) action;
                ZimbraLog.filter.debug("Refusing delivery of a message at the protocol level");
                try {
                    handler.ereject(envelope);
                } catch (ErejectException e) {
                    // 'ereject' action executed
                    throw e;
                } catch (Exception e) {
                    ZimbraLog.filter.warn("Unable to ereject.", e);
                }
            } else if (action instanceof ActionNotifyMailto) {
                ActionNotifyMailto notifyMailto = (ActionNotifyMailto) action;
                ZimbraLog.filter.debug("Sending RFC 5435/5436 compliant notification message to %s.", notifyMailto.getMailto());
                try {
                    handler.notifyMailto(envelope, notifyMailto.getFrom(), notifyMailto.getImportance(), notifyMailto.getOptions(), notifyMailto.getMessage(), notifyMailto.getMailto(), notifyMailto.getMailtoParams());
                } catch (Exception e) {
                    ZimbraLog.filter.warn("Unable to notify (mailto).", e);
                    keep(KeepType.EXPLICIT_KEEP);
                }
            }
        }
        handler.afterFiltering();
    } catch (ServiceException e) {
        throw new ZimbraSieveException(e);
    }
}
Also used : Action(org.apache.jsieve.mail.Action) ActionReply(com.zimbra.cs.filter.jsieve.ActionReply) ActionRedirect(com.zimbra.cs.filter.jsieve.ActionRedirect) MessagingException(javax.mail.MessagingException) SieveMailException(org.apache.jsieve.mail.SieveMailException) SieveException(org.apache.jsieve.exception.SieveException) AddressException(javax.mail.internet.AddressException) ServiceException(com.zimbra.common.service.ServiceException) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) IOException(java.io.IOException) ActionEreject(com.zimbra.cs.filter.jsieve.ActionEreject) ActionNotifyMailto(com.zimbra.cs.filter.jsieve.ActionNotifyMailto) ServiceException(com.zimbra.common.service.ServiceException) ActionKeep(org.apache.jsieve.mail.ActionKeep) ActionReject(org.apache.jsieve.mail.ActionReject) ActionFileInto(com.zimbra.cs.filter.jsieve.ActionFileInto) ErejectException(com.zimbra.cs.filter.jsieve.ErejectException) ActionNotify(com.zimbra.cs.filter.jsieve.ActionNotify)

Example 3 with Action

use of org.apache.jsieve.mail.Action in project zm-mailbox by Zimbra.

the class FlaggedTest method executeBasic.

@Override
protected boolean executeBasic(MailAdapter mail, Arguments args, SieveContext ctx) throws SieveException {
    assert (flags != null);
    if (mail instanceof DummyMailAdapter) {
        return true;
    }
    if (!(mail instanceof ZimbraMailAdapter)) {
        return false;
    }
    ZimbraMailAdapter adapter = (ZimbraMailAdapter) mail;
    // check actions already taken by a previous filter
    for (Action action : adapter.getActions()) {
        if (action instanceof ActionFlag) {
            if (flags.contains(action)) {
                return true;
            }
        }
    }
    // check the message's flags if this filter is running against the existing messages
    Message msg = adapter.getMessage();
    if (msg != null) {
        int bitmask = msg.getFlagBitmask();
        return bitmask == FilterUtil.getFlagBitmask(flags, bitmask);
    }
    return false;
}
Also used : Action(org.apache.jsieve.mail.Action) Message(com.zimbra.cs.mailbox.Message) DummyMailAdapter(com.zimbra.cs.filter.DummyMailAdapter) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter)

Aggregations

Action (org.apache.jsieve.mail.Action)3 ServiceException (com.zimbra.common.service.ServiceException)1 DummyMailAdapter (com.zimbra.cs.filter.DummyMailAdapter)1 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)1 ActionEreject (com.zimbra.cs.filter.jsieve.ActionEreject)1 ActionFileInto (com.zimbra.cs.filter.jsieve.ActionFileInto)1 ActionNotify (com.zimbra.cs.filter.jsieve.ActionNotify)1 ActionNotifyMailto (com.zimbra.cs.filter.jsieve.ActionNotifyMailto)1 ActionRedirect (com.zimbra.cs.filter.jsieve.ActionRedirect)1 ActionReply (com.zimbra.cs.filter.jsieve.ActionReply)1 ErejectException (com.zimbra.cs.filter.jsieve.ErejectException)1 Message (com.zimbra.cs.mailbox.Message)1 IOException (java.io.IOException)1 MessagingException (javax.mail.MessagingException)1 AddressException (javax.mail.internet.AddressException)1 SieveException (org.apache.jsieve.exception.SieveException)1 ActionKeep (org.apache.jsieve.mail.ActionKeep)1 ActionReject (org.apache.jsieve.mail.ActionReject)1 SieveMailException (org.apache.jsieve.mail.SieveMailException)1