#!/bin/bash
#                                                                    #
# Copyright 2006, Michelle Konzack All rights reserved.              #
#                                                                    #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions #
# are met:                                                           #
#                                                                    #
# 1. Redistributions of source code  must retain the above copyright #
#    notice, this list of conditions and the following disclaimer.   #
#                                                                    #
# 2. Redistributions  in  binary  form   must  reproduce  the  above #
#    copyright  notice, this  list of  conditions and  the following #
#    disclaimer in the documentation and/or other materials provided #
#    with the distribution.                                          #
#                                                                    #
# 3. Neither  the name  of Michelle  Konzack  nor the  names of  its #
#    contributors may be used to endorse or promote products derived #
#    from this software without specific prior written permission.   #
#                                                                    #
# THIS  SOFTWARE IS  PROVIDED BY  MICHELLE KONZACK  AND CONTRIBUTORS #
# ``AS IS'  AND ANY  EXPRESS OR  IMPLIED WARRANTIES,  INCLUDING, BUT #
# NOT  LIMITED TO,  THE  IMPLIED WARRANTIES  OF MERCHANTABILITY  AND #
# FITNESS  FOR A  PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT #
# SHALL  JULIE  HAUGH OR  CONTRIBUTORS  BE  LIABLE FOR  ANY  DIRECT, #
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES #
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) #
# HOWEVER  CAUSED  AND  ON  ANY  THEORY  OF  LIABILITY,  WHETHER  IN #
# CONTRACT,  STRICT  LIABILITY,  OR TORT  (INCLUDING  NEGLIGENCE  OR #
# OTHERWISE) ARISING  IN ANY WAY  OUT OF  THE USE OF  THIS SOFTWARE, #
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                 #
#                                                                    #

VERSION=0.1.0

#. gettext.sh
#TEXTDOMAIN=tdmailcrforward
#TEXTDOMAINDIR=/usr/share/locale

####################################################################
#  A simpel help...
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "" ] ; then
echo "\
tdmailcrforward   $VERSION   Coded by Michelle Konzack <linux4michelle@freenet.de>
===============           This  BASH  script  is  under  GNU  GPL  version  2.0

Usage:  tdmailcrforward  --spool | --smtp | --help <mailfile

ATTENTION:      --smtp is the default.

Procmail recipe examples:

        :0fw
        |tdmailcrforward --spool
or
        :0fw
        |tdmailcrforward --smtp

to save the incoming CR\'s in a folder, use:

        :0
        * ^X-TDMailCRForward:.*
        .ATTENTION.Abuse/
"
  exit 0
fi

####################################################################
#  Check for the existence of the config directory
CONFDIR=$HOME/.tdmailtools
if [ ! -d $CONFDIR ] ; then
  mkdir -p $CONFDIR
  if [ $? -ne 0 ] ; then
    echo "tdmailcrforward: error: can not create the config directory »$CONFDIR«."
    exit 1
  fi
fi

####################################################################
#  Check for the existence of the config file
CONFIG=$CONFDIR/tdmailcrforward
if [ ! -f $CONFIG ] ; then
  echo "SPOOLDIR=$HOME/Maildir/.POSTPONED" >>$CONFIG
  echo "" >>$CONFIG
  echo "ABUSEDIR=$HOME/Maildir/.ATTENTION.Abuse" >>$CONFIG
  echo "" >>$CONFIG
  echo "LOGDIR=$HOME/log/tdmailcrforward" >>$CONFIG
  echo "" >>$CONFIG
  echo "MTAOPT=\"ssmtp -t\"" >>$CONFIG
  echo "tdmailcrforward: error: you have not configured yet.  please look at »$CONFDIR/tdmailcrforward«."
  exit 2
fi

####################################################################
#  Check for the existence of the LIST file
LIST=$CONFDIR/LIST_crforward
if [ ! -f "$LIST" ] ; then
#  echo "tdmailcrforward: error: the file which hold the blacklisted senders »$CONFDIR/LIST_crforward« does not exist."
#  touch $CONFDIR/LIST_crforward
  echo "tdmailcrforward: error: the file which hold the blacklisted senders »$CONFDIR/LIST_crforward« does not exist, but I will set them up to a default."
  echo "petsupermarket.sspam@uol.com.br: rhuol@uol.com.br, assineuol@uol.com.br, sos@uol.com.br, publicidade@uol.com.br, comercio.eletronico@uol.com.br, uol@uol.com.br" >$CONFDIR/LIST_crforward
fi

####################################################################
#  Check whether the LIST file has contents or not
STR=`cat $LIST`
if [ -z  "$STR" ] ; then
  echo "tdmailcrforward: error: the file which hold the blacklisted senders »$CONFDIR/LIST_crforward« is empty."
  exit 4
fi

####################################################################
#  Check for the existence of the spool directory
. $CONFDIR/tdmailcrforward

if [ ! -d $SPOOLDIR/ ] ; then
  mkdir -p $SPOOLDIR/{tmp,new,cur}
  if [ $? -ne 0 ] ; then
    echo "tdmailcrforward: error: can not create the spool directory »$SPOOLDIR«."
    exit 5
  fi
fi

####################################################################
#  Check for the existence of the abuse directory
if [ ! -d $ABUSEDIR ] ; then
  mkdir -p $ABUSEDIR/{tmp,new,cur}
  if [ $? -ne 0 ] ; then
    echo "tdmailcrforward: error: can not create the spool directory »$ABUSEDIR«."
    exit 6
  fi
fi

####################################################################
#  Check for the existence of the log directory
if [ ! -d $LOGDIR ] ; then
  mkdir -p $LOGDIR
  if [ $? -ne 0 ] ; then
    echo "tdmailcrforward: error: can not create the spool directory »$LOGDIR«."
    exit 7
  fi
fi

####################################################################
#  Create two tempfiles
TMP1=`mktemp -t tdmailcrforward.1.XXXXXXXX`
if [ $? -ne 0 ] ; then
  echo "tdmailcrforward: error: can not create temp files."
  exit 8
fi
TMP2=`mktemp -t tdmailcrforward.2.XXXXXXXX`
if [ $? -ne 0 ] ; then
  rm -f $TMP1
  echo "tdmailcrforward: error: can not create temp files."
  exit 8
fi

####################################################################
#  Now we catch the stdin
cat /dev/stdin >$TMP1

####################################################################
#  Our main progi starts here
       TO=`cat $TMP1 |formail -czx To:`
 FROMORIG=`cat $TMP1 |formail -czx From:`
MSGIDORIG=`cat $TMP1 |formail -czx Message-Id:`

LOGDATE=`date "+%Y-%m"`
DATELOG=`date "+%F %R:%S"`
echo "$DATELOG : $FROMORIG" >$LOGDIR/$LOGDATE.log

FROM=`echo "$FROMORIG" |sed -e "s/^.*<//;s/>*$//"`
RECP=`grep "^$FROM: " $LIST`
if [ $? -eq 0 ] ; then
  RECP=`echo "$RECP" |cut -d " " -f2-`
  DATE=`date -R`
  MSGID=`date +%s`
  HN=`hostname --fqdn`

  echo "\
From: $TO
To: $RECP
Date: $DATE
Subject: Recipient does not like Challange Reponses
Message-Id: <$MSGID@$HN>
In-Reply-To: $MSGIDORIG
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit

Who ever you are,

please keep in mind, that you receive this automated message after I
have received many others from the same address which bothering me.

You or your mail relay has send an unwanted challenge reponse to me
for a person I do not know.  This is SPAM !

If you do not want further forwarded messages in your mailbox, stop
SPAMING me with such stupid messages.  And then think about spamers
which forging From: adresses.  So your challenge reponse system harm
innocent peoples and internet users.

Friendliest
    Michelle Konzack
    Systemadministrator
    Tamay Dogan Network
    Debian GNU/Linux Consultant


######################################################################
#                                                                    #
#            Following a copy of the las SPAM you send me            #
#                                                                    #
######################################################################

" >>$TMP2
    
  cat $TMP1 >>$TMP2

  if [ "$1" == "--spool" ] ; then
    let X=1
    HNA=`hostname --alias`
    while [ -f $SPOOLDIR/$MSGID.$X.$HNA ] ; do
      let X=$X+1
    done   
    formail -f -I "X-TDMailCRForward: spool" <$TMP2 >>$SPOOLDIR/new/$MSGID.$X.$HNA
  else
    ################################################################
    #  We Try to send out the message
    let X=1
    HNA=`hostname --alias`
    $MTAOPT <$TMP2 >/dev/null 2>&1
    if [ $? -eq 0 ] ; then
      ##############################################################
      #  OK, it was correctly send and now we try
      #  to save the message with an unique name
      while [ -f $ABUSEDIR/cur/$MSGID.$X.$HNA:2,S ] ; do
        let X=$X+1
      done   
      formail -f -I "X-TDMailCRForward: smtp" < $TMP2 >$ABUSEDIR/cur/$MSGID.$X.$HNA:2,S
    else
      ##############################################################
      #  OK, there was an error while sending which mean, we
      #  should try to save the message with an unique name
      while [ -f $SPOOLDIR/new/$MSGID.$X.$HNA ] ; do
        let X=$X+1
      done   
      formail -f -I "X-TDMailCRForward: spool" < $TMP2 >$SPOOLDIR/new/$MSGID.$X.$HNA
    fi
  fi
  formail -f -I "X-TDMailCRForward: true"  < $TMP1
else
  cat $TMP1
fi

rm -f $TMP1 $TMP2

exit 0
