### autoreply_deferentially.rc - a considerate procmailrc autoresponder
### ===================================================================
#
# - (c) Copyright Andreas Schamanek, version 2.03 * 2010-02-03
# - http://www.fam.tuwien.ac.at/~schamane/_/autoreply_deferentially
# - Free for personal, non-commercial use! Else please ask first.
# - Use at your own risk! Tested only on Debian 4 + 5.
#
### Notes ----------------------------------------------------------------
#
# - These recipes assume that spam _has already_ been filtered.
# - Before activating autoreply make sure you removed $AR_VACATION_CACHE.
# - After activating autoreply test it by asking a friend to write to you.
# - Don't forget to deactivate autoreply when not needed anymore, either
#   by setting AR_AUTOREPLY=no or by removing $AR_MSGFILE.
#
### Settings/Variables ---------------------------------------------------
#
# - Set these variables here or before including this script.
# - The following comments show the default values.

### AR_AUTOREPLY ... acticates and deacticates autoreplies
# AR_AUTOREPLY=yes       # yes/no, defaults to yes

### File/message to be included into autoreply, make sure it is readable
# AR_MSGFILE=$HOME/autoreply_message.txt

### List of addresses which received an autoreply (avoids sending msg twice)
# - This file needs to be removed before the autoresponder is activated.
# AR_VACATION_CACHE=$HOME/autoreply_vacation.cache

### Your address: The address people use to write to you
# - Example: AR_MYADDRESS="firstname.surname@example.com"
# AR_MYADDRESS=$LOGNAME@   # $LOGNAME is set to your loginname by procmail

### Address used as "From:" (sender's address) in the autoreplies
# - It is recommended to not use your own address.
# - Examples:
# AR_FROMADDRESS="Mail Delivery Subsystem <MAILER-DAEMON@example.com>"
# AR_FROMADDRESS="AutoReply of Your Name <secondary_address@example.com>"
# - Default:
# AR_FROMADDRESS="Mail Delivery Subsystem <MAILER-DAEMON@$AR_HOSTNAME>"

### Log file where sent autoreplies are logged
# - To disable set this to "/dev/null" or ""
# AR_LOGFILE="$HOME/autoreply_sent_autoreplies.log"

# ------------------------------------------------------------------------
# Setting defaults of variables

AR_AUTOREPLY=${AR_AUTOREPLY:-"yes"}
AR_MSGFILE=${AR_MSGFILE:-"$HOME/autoreply_message.txt"}
AR_VACATION_CACHE=${AR_VACATION_CACHE:-"$HOME/autoreply_vacation.cache"}
AR_MYADDRESS=${AR_MYADDRESS:-"$LOGNAME@"}

AR_HOSTNAME=${AR_HOSTNAME:-`hostname -f`} # used in $AR_FROMADDRESS
AR_FROMADDRESS=${AR_FROMADDRESS:-"Mail Delivery Subsystem <MAILER-DAEMON@$AR_HOSTNAME>"}

AR_FORMAIL=${AR_FORMAIL:-"/usr/bin/formail"}
AR_SENDMAIL=${AR_SENDMAIL:-"/usr/sbin/sendmail"}

AR_LOGFILE=${AR_LOGFILE:-"$HOME/autoreply_sent_autoreplies.log"}
AR_NL="
" # newline to be used with LOG="text to log$AR_NL"
# ------------------------------------------------------------------------

# --- send autoreply to senders (in case you received a _personal_ message)
:0
* AR_AUTOREPLY ?? yes
* $!^X-Loop: $AR_MYADDRESS
{
  :0 Whc: autoreply_deferentially.lock
  # Check to see if the mail was addressed to us
  * $^(To|CC): (.*\<)?$AR_MYADDRESS
  # Don't reply to daemons and mailinglists and junk
  * !^FROM_DAEMON
  * !(^List|mail(ing|(ing)?[- _]?list)|-l@|newsletter)
  * !^Precedence: (list|junk|bulk)
  * !^(From|Reply-To): .*noreply
  # Never reply to ourselves
  * $!^From: .*$AR_MYADDRESS
  # Miscellaneous criteria
  * !^From: .*((www|web)-?(data|master|admin)|robot@)
  # RFC 3834 suggests to accept only "Auto-Submitted: no"
  * !^Auto-Submitted: ...
  # Don't reply to receipts (delivery notifications, out of office, ...)
  # Get a filter list e.g. from http://wox.at/as/_/regex_mail_receipts
  ###* !? grep -Ef /etc/procmailrc_receipts  ### uncomment if available
  # Do not reply to some selected addresses (local blacklist)
  * !^From: .*(@example.com|wf-batch@brz\.gv\.at)
  # Make sure the autoreply text exists
  * ? test -r $AR_MSGFILE
  # Mail loops are evil
  | $AR_FORMAIL -rtD 65532 "$AR_VACATION_CACHE"

  :0 ec         # if the senders address was not in the cache
  {
    # Get address to send reply to and write a line to $AR_LOGFILE
    :0
    * ^From:.*\/(<?[a-zA-Z0-9_][-a-zA-Z0-9_+.=]*@[-a-zA-Z0-9_+.=]+\.[-a-zA-Z0-9_+.=]+[a-zA-Z0-9_]>?)
    { # Logging the sent autoreplies
      AR_NOW=`date +%Y-%m-%d\ %H:%M`
      AR_LOGFILEBAK="$LOGFILE"
      LOGFILE="$AR_LOGFILE"
      LOG="$MATCH $AR_NOW$AR_NL"
      LOGFILE="$AR_LOGFILEBAK"
    }
    # Write a message to the default procmail log
    LOG="   >>> sending autoreply to $MATCH $AR_NL"
    # Send autoreply message
    :0 h
    | ($AR_FORMAIL -rtA"Precedence: junk" \
      -A"Auto-Submitted: auto-replied" \
      -A"X-Loop: $AR_MYADDRESS" \
      -A"From: $AR_FROMADDRESS" ;\
      cat $AR_MSGFILE \
      ) | $AR_SENDMAIL -i -t
  }
}

# History:
#
# 2005-04-08  Added flag h when checking with $FORMAIL -rD
# 2007-09-02  Added !? grep -Ef /etc/procmailrc_receipts for Autoreply
# 2008-05-06  Added further exceptions for auto replies
# 2008-07-08  Made auto-replies a bit more RFC 3834 friendly
# 2009-05-23  Avoiding auto-replies to ourselves
# 2009-07-14  Several fixes including X-Greylist bug
# 2009-07-14  Added support for logging auto-replies
# 2009-11-15  Code re-written for first public release
# 2010-01-31  Added -t to $AR_FORMAIL, removed < requirement for From:
# 2010-02-03  Added robot@ to miscellaneous stuff, added "local blacklist"

#/ autoreply_deferentially.rc --------------------------------------------

