#!/bin/bash

### by jan: jbehrend@mpifr-bonn.mpg.de, Dec 2009

DEFAULT_RESPONDER_TEXT=/usr/local/etc/autorespond.txt
DB=/var/lib/autorespond/autorespond.db

logger="$(which logger) -t autorespond -i -p mail.error"
logger_info="$(which logger) -t autorespond -i -p mail.notice"
egrep=$(which egrep)
bc=$(which bc)
sed=$(which sed)
awk=$(which awk)
mv=$(which mv)
base64=$(which base64)
chmod=$(which chmod)
mail=$(which mail)
sudo=$(which sudo)
mktemp=$(which mktemp)
rm=$(which rm)
maildrop="$(which maildrop) -d"
postdrop=$(which postdrop)
ldapsearch="$(which ldapsearch) -x -ZZ -LLL -b ou=people,dc=mydomain,dc=de"

TARGET=$1
LOCAL_TARGET=${TARGET%%@*}
SENDER=$2
if [ $(echo "$TARGET" | $egrep -c '^.+@.+\..+$') -eq 0 ] ; then
   $logger "TARGET \"$TARGET\" is an invalid email address"
   exit 1
fi
ORIG_MSG=$($mktemp)
$chmod 600 $ORIG_MSG
while read ; do echo "$REPLY" >> $ORIG_MSG ; done
$sudo $maildrop $LOCAL_TARGET -f $SENDER < $ORIG_MSG
$logger_info "Sent original message from \"$SENDER\" to \"$LOCAL_TARGET\""

if [ $(echo "$SENDER" | $egrep -c '^.+@.+\..+$') -eq 0 ] ; then
   if [ "$SENDER" != "MAILER-DAEMON" ] ; then
      $logger "SENDER \"$SENDER\" is an invalid email address"
      $rm -f $ORIG_MSG
      exit 1
   fi
fi
if [ "$SENDER" == "MAILER-DAEMON" ] ; then
   $logger_info "System mail from MAILER-DAEMON: NOT sending autoresponse"
   $rm -f $ORIG_MSG
   exit 0
fi
if [ $($egrep -i '^Precedence:' $ORIG_MSG | $egrep -c -i 'bulk|list|junk') -gt 0 ] ; then
   $logger_info "Preventing autoresponses to mailing lists, bulk or junk addresses: NOT sending autoresponse to \"$SENDER\""
   $rm -f $ORIG_MSG
   exit 0
fi
if [ $($egrep -i -c '^Sender:' $ORIG_MSG) -gt 0 ] ; then
   $logger_info "Found \"Sender:\" header field."
   H_SENDER=$($egrep -i '^Sender:' $ORIG_MSG | $awk '{print $2}')
   if [ $(echo "$H_SENDER" | $egrep -c '^.+@.+\..+$') -eq 0 ] ; then
      $logger "Header \"Sender: $H_SENDER\" is an invalid email address, still using envelope sender: \"$SENDER\""
   else
      $logger_info "Using header \"Sender: $H_SENDER\" instead of envelope sender: \"$SENDER\""
      SENDER="$H_SENDER"
   fi
fi
if [ $(echo "$SENDER" | $egrep -c -i 'no(-|)reply@') -gt 0 ] ; then
   $logger_info "Preventing autoresponses to \"noreply\" addresses: NOT sending autoresponse from \"$TARGET\" to \"$SENDER\""
   $rm -f $ORIG_MSG
   exit 0
fi

LOCAL_ONLY="$($ldapsearch '(&(uid='$LOCAL_TARGET')(objectClass=mailUser))' autoResponderLocalOnly |\
   $egrep -v '^dn: uid=' | $sed 's/^.*\ //')"
if [ "$LOCAL_ONLY" == "TRUE" ] ; then
   $logger_info "User \"$LOCAL_TARGET\" allows autoresponses to local addresses only"
   if [ $(echo "$SENDER" | $egrep -c -i '@mydomain.de') -lt 1 ] ; then
      $logger_info "Preventing autoresponses to external address: NOT sending autoresponse from \"$TARGET\" to \"$SENDER\""
      $rm -f $ORIG_MSG
      exit 0
   fi
fi

$rm -f $ORIG_MSG

TODAY=$(echo $(date +%s)/60/60/24 | $bc)
unset ALREADY_SENT
for ((i=$TODAY;i>=(($TODAY-7));i=i-1)) ; do  
   if [ $($egrep -c "$i $TARGET $SENDER" $DB) -gt 0 ] ; then
      ALREADY_SENT=1
   fi
done

if [ -n "$ALREADY_SENT" ] ; then
   $logger_info "Already sent autoresponse from \"$TARGET\" to \"$SENDER\" this week, NOT sending again"
   $logger_info "Cleaning database"
   >$DB.clean
   for ((i=$TODAY;i>=(($TODAY-7));i=i-1)) ; do  
      $egrep "$i" $DB >>$DB.clean
   done
   $mv -f $DB.clean $DB
else
shopt -s nocasematch
   if [[ "$TARGET" == "$SENDER" ]] ; then
shopt -u nocasematch
      $logger_info "Preventing autoresponse loop from \"$TARGET\" to \"$SENDER\""      
   else
      TEXT="$($ldapsearch '(&(uid='$LOCAL_TARGET')(objectClass=mailUser))' autoResponderText |\
         $egrep -v '^dn: uid=' | $sed 's/^.*\ //' | $base64 -d)"
      if [ -z "$TEXT" ] ; then
         $logger_info "Autoresponder text for \"$LOCAL_TARGET\" empty, using default"
         TEXT="$(cat $DEFAULT_RESPONDER_TEXT)"
      fi
      SUBJECT="$($ldapsearch '(&(uid='$LOCAL_TARGET')(objectClass=mailUser))' autoResponderSubject |\
         $egrep -v '^dn: uid=' | $sed 's/^.*\ //' | $base64 -d)"
      if [ -z "$SUBJECT" ] ; then
         SUBJECT="Abwesendheitsnotiz / Away from my email"
      fi
      COMMON_NAME="$($ldapsearch '(&(uid='$LOCAL_TARGET')(objectClass=mailUser))' cn |\
         $egrep -v '^dn: uid=' | $sed 's/^cn:\+\ //')"
      echo "$TEXT" | $sed 's/TARGET/'"$COMMON_NAME"'/' |\
         $mail -a "Content-Type: text/plain; charset=utf-8" -a "From: $TARGET" -a "Sender: autorespond@mydomain.de" -s "$SUBJECT" $SENDER
      echo "$TODAY $TARGET $SENDER" >> $DB
      $logger_info "Sent autoresponse from \"$TARGET\" to \"$SENDER\""
   fi
fi
