#!/bin/sh
#
# abmailsreporters - an example of cron job for system use of abmailsreports
#
# Copyright (C) 2007-2008 Taiji Yamada <taiji@aihara.co.jp>
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#

#
# for users
#
getusershomes(){
  sed -ne 's|^\([^#:][^#:]*\).*:\([^:][^:]*\):\([^:]*\)$|\1 \2|p' /etc/passwd
}
getuser_sw_spamreport(){ # $0 usershome
  [ -f "$1/.procmailrc" ] &&
  [ "`sed -ne '/^[ 	]*SW_ANTISPAM_ADD_HEADER=.*$/{
s/.*=\(.*\)$/\1/p
q
}' \"$1/.procmailrc\"`" != "on" ] &&
  [ "`sed -ne '/^[ 	]*SW_SPAMREPORT=.*$/{
s/.*=\(.*\)$/\1/p
q
}' \"$1/.procmailrc\"`" = "on" ] &&
  sed -ne '/^[ 	]*SW_ANTISPAM=.*$/{
s/.*=\(.*\)$/\1/p
q
}' "$1/.procmailrc"
}
getusers_sw_spamreport_on(){
  getusershomes |
  while read user usershome; do
    [ "`getuser_sw_spamreport $usershome`" != "on" ] && continue
    echo $user
  done
}
#
# for groups and mailing lists
#
getaliases(){
  sed -ne 's|^\([^#:][^#:]*\):[ 	]*\(.*\)$|\1 \2|p' /etc/aliases
}
getaliases_spamreport_on(){
  getaliases |
  while read analias aliases; do
    case "$aliases" in
    */etc/procmailrcs/*) ;;
    *) continue;;
    esac
    echo $analias
  done
}
#
# abmailsreports for users, groups and mailing lists
#
/opt/local/bin/abmailsreports -- -b -g -d -r `getaliases_spamreport_on` `getusers_sw_spamreport_on`
