#
# An attempt to render, as SIEVE, the actions of shouldRespond() from CMU
# sieve/bc_eval.c as distributed with cyrus-imapd 2.3.8
#
# This is not valid SIEVE; The following expansions must first be
# performed:
#
#  - %myaddrs%  list of valid recipient addresses
#  - %envrcpt%  the actual envelope recipient
#
# Example:
#
#  sed -e 's/%myaddrs%/["me1@example.org", "me2@example.org"]/' \
#      -e 's/%envrcpt%/"me1@example.org"/' < vacation.sieve.in
#
require ["envelope"];

if anyof ( exists "list-id",
           exists "list-help",
           exists "list-subscribe",
           exists "list-unsubscribe",
           exists "list-post",
           exists "list-owner",
           exists "list-archive",

           # XXX bc_eval.c also skips leading whitespace
           not header :is "auto-submitted" "no",

           # XXX bc_eval.c also skips leading whitespace
           header :is "precedence" ["junk", "bulk", "list"],

           # XXX does this really catch null sender?
           envelope :all :is "from" "",

           # envelope sender equals envelope recipient; redundant if we assume
           # %envrcpt% is an element in %myaddrs%
           envelope :all :is "from" %envrcpt%,
           envelope :all :is "from" %myaddrs%,

           envelope :localpart :is "from" ["mailer-daemon",
                                           "listserv",
                                           "majordomo"],
           envelope :comparator "i;octet"
                    :localpart :matches "from" ["*-request",
                                                "owner-*"],
           not address :all :is ["to",
                                 "cc",
                                 "bcc",
                                 "resent-to",
                                 "resent-cc",
                                 "resent-bcc"] %myaddrs%
) {
  discard;
}
