#!/bin/sh
#
# abmails* - do something only abmail files in spam folders
#
# 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.
#
#
# usage:
#   $ abmails* [[-c preexec] -- abmails-*_args]
#   $ abmailsreport [[-R] [-Q] [[-a] -r] | [[-c preexec] -- abmails-*_args]]
#
mydomain=aihara.co.jp
dirranges(){ # -d=nday -p=prefix
  perl -s -e 'use POSIX; $tm=time(); for ($i=$d; $i>0; $i--) { print strftime("$p%Y%m%d\n",localtime($tm-$i*60*60*24)); }' -- "$@"
}
abid(){
  perl -e 'printf("%08X\n", rand(2**32));'
  #perl -e 'printf("%08X\n", unpack("I", `openssl rand 8`));'
}
abmsgid(){
  echo "<`/bin/date +%Y%m%d.%H%M%S`.`abid`.abmsg.${LOGNAME}@${mydomain}>"
}
ABMSGID_CACHE="${MAILDIR}"/abmsgid.cache
register(){
  formail -D 8192 "${ABMSGID_CACHE}"
}
registered(){
  cp -p "${ABMSGID_CACHE}" "${ABMSGID_CACHE}".bak
  formail -D 8192 "${ABMSGID_CACHE}".bak
}
inquiry(){
  cp -p "${ABMSGID_CACHE}" "${ABMSGID_CACHE}".bak
  formail -R "Message-Id:" "-Message-Id:" |
  formail -R "In-Reply-To:" "Message-Id:" |
  formail -D 8192 "${ABMSGID_CACHE}".bak
}
prep_msg=cat
antiabmail(){
  formail -R "Received:" "-Received:"
}
substmaildir() {
  echo "$1" | sed -e "s|/new/\([^/][^/]*\)|$2|"
}
ismaildir() {
  [ -d "$1/cur" -a -d "$1/tmp" -a -d "$1/new" ]
}
receiver(){
  abmailc - |
  sed -ne 's|^File: \(\.*spam-[0-9][0-9]*/.*\)|\1|p' | while read msg; do
    $prep_msg < "$MAILDIR/$msg" | procmail -Yf- SW_ANTISPAM=off ${PROCMAILARGS} &&
    case "$msg" in
    */new/*[!/]*)
      ismaildir "`substmaildir \"$MAILDIR/$msg\"`" &&
      cp -p "$MAILDIR/$msg" "`substmaildir \"$MAILDIR/$msg#\" \"/cur/\1\"`" ||
      cp -p "$MAILDIR/$msg" "$MAILDIR/$msg#"
      ;;
    *)
      cp -p "$MAILDIR/$msg" "$MAILDIR/$msg#"
      ;;
    esac
  done
}
preexec()
{
  eval ${PREEXEC}
}

case "`basename \"$0\"`" in
*report)
  REPORT="${SENDMAIL:-sendmail} -oi -t"
  #REPORT='formail -s procmail'
  while [ $# != 0 ]; do
    case "$1" in
    -c) shift; PREEXEC="$1";;
    --) shift; ABMAIL=abmails-rescue; break;;
    -R)	registered;	exit $?;;
    -Q)	inquiry;	exit $?;;
    -a)	prep_msg="antiabmail";;
    -r)	receiver;	exit $?;;
    *) break;;
    esac
    shift
  done
  ;;
*)
  REPORT=${PAGER:-less}
  while [ $# != 0 ]; do
    case "$1" in
    -c) shift; PREEXEC="$1";;
    --) shift; ABMAIL=abmails-echo; break;;
    *) break;;
    esac
    shift
  done
  ;;
esac
if [ "$ABMAIL" != "" ]; then
  ABMAILFLAGS=""
  while true; do
    case "$1" in
    -?*) ABMAILFLAGS="${ABMAILFLAGS} $1";;
    *) break;;
    esac
    shift
  done
  "${ABMAIL}" ${ABMAILFLAGS}
fi

SUBJECT="[abmails report for ${ANALIAS:-${LOGNAME}}] `/bin/date +%Y/%m/%d`"
RECIPIENT="${OWNER:-${ANALIAS:-${LOGNAME}}}"
SENDER="${ANALIAS:-${LOGNAME}}"

{ [ "${MAILDIR}" != "" ] && echo "${MAILDIR}" || cat <<EOF
$HOME/Mail
$HOME/Maildir
EOF
} | while read mailpath; do
if [ -d "${mailpath}" ] && cd "${mailpath}"; then
ABMSGID_CACHE="${mailpath}"/abmsgid.cache
MESSAGE_ID="`abmsgid`"
DATE="`env LC_ALL=C date '+%a, %d %b %Y %X %z (%Z)'`"
ANTISPAMDIRS="`dirranges -d=1 -p=\"spam-\"`"
ABMAILC_ARGS='-s -S -w 70 -c 70'

if [ "${PREEXEC}" != "" ]; then
  [ -d ${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}} ] &&
  preexec ${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}}
  [ -d .${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}} ] &&
  preexec .${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}}
fi

if abmailc -E ${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}} .${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}}; then
cat <<EOF | register ||
Message-Id: ${MESSAGE_ID}

EOF
(cat <<EOF
Subject: ${SUBJECT}
From: ${SENDER}
To: ${RECIPIENT}
Content-Type: text/plain; charset="utf-8"
Message-Id: ${MESSAGE_ID}
Date: ${DATE}

EOF
abmailc ${ABMAILC_ARGS} ${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}} .${DEFAULT_ANTISPAMDIRS:-${ANTISPAMDIRS}}
) | ${REPORT}
fi
fi
done
