# forwardmail
# Written by SFC John Junod.  Updated/modified by CPT Joseph E. Hafeman
# Date:  7 November 1989.

# Forwardmail allows users to forward their mail to other users or to
# disable that feature.  If called with no arguments, mail forwarding
# is disabled.  If called with one argument, mail is forwarded to the
# addressee specified in the argument.

# 17 November 1989 - Modified so that forwardmail called with 0 arguments
#                    does nothing if .maildelivery file does not exist.
#                    Modified so that all prompted replies do not produce
#                    errors if user only enters <CR>.  Modified so that
#                    forwarding mail address is verified against the
#                    password and whipp files for validity.  CPT Joseph
#                    E. Hafeman
# 19 December 1989 - Move and save user's .maildelivery file, if it
#                    exists.  Restore user's .maildelivery file, when
#                    mail forwarding is cancelled.  CPT Joseph E. Hafeman
#  7 February 1990 - Changed to use mail.file instead of whipp.file for
#                    mailid verification.  CPT Joseph E. Hafeman

WHIPP=LIBDIR/mail.file

# Get your mail address

whoyou=$LOGNAME@`uname`
case $# in

# Forwardmail with no arguments disables mail forwarding.

# Check to see if mail is being forwarded.

  0)  if [ ! -f $HOME/.maildelivery ]
    then
      echo Your mail was not being forwarded.
      exit 1
    fi
    fname=`grep resend $HOME/.maildelivery | awk '{print $6}'`

# Delete user mail command file, mail forwarding log file, and
# mail forwarding message file.

    rm $HOME/.maildelivery >/dev/null 2>/dev/null
    rm $HOME/.fwdlog > /dev/null 2>/dev/null
    rm $HOME/.fwdmsg > /dev/null 2>/dev/null

# Restore the signature file, if appropriate.

    if [ -f $HOME/.signature.fwd ]
    then
      mv $HOME/.signature.fwd $HOME/.signature
    fi

# Restore the maildelivery file, if appropriate

    if [ -f $HOME/.old.maildel ]
    then
      mv $HOME/.old.maildel $HOME/.maildelivery
    fi

# Make an entry in the system forwarding log file.

    echo "$whoyou : OFF" >> /usr/acd/news/forward.log
    echo
    echo Your mail was being forwarded to $fname.
    echo;
    echo Mail forwarding to $fname is turned off.  To forward mail to another userid or
    echo another computer, enter \"forwardmail userid@computer\" replacing userid
    echo with the desired userid and computer with the host on which that userid
    echo is located.
    exit 1
    ;;

# If called with 1 argument, then mail is forwarded to the addressee specified
# in the argument.

  1)  fname=$1

# If forwardmail was called with ? as the argument, then the user is
# helped with whois.

    while [ $fname = "?" ]
    do
      echo "What name are you looking for?  \c"
      read usname
      if [ $usname ]
      then
        /usr/acd/bin/whois $usname | more
      fi
      echo "Enter forwarding userid (? for help/<CR> to quit):  \c"
      read fname
      if [ $fname ]
      then
        :
      else
        echo Mail not forwarded.
        exit 0
      fi
    done

# No pranksters trying to invoke cyclic mail sending.

    if [ $whoyou = $fname -o $LOGNAME = $fname ]
    then
      echo "You can't forward mail to yourself!!!"
      exit 1
    fi

# Check for valid forwarding mail address by searching for it in the
# password and whipp files.

    if [ `cut -f1 -d":" /etc/passwd | grep -c "^$fname$"` = "0" ]
    then
      if echo "$fname" | grep "@" > /dev/null 2>&1
      then
        if [ `awk '{print $1 "@" $2}' $WHIPP | grep -c "^$fname$"` = "0" ]
        then
          echo "$fname is an invalid mailid within our network."
          exit 1
        fi

      else
        if [ `awk '{print $1}' $WHIPP | grep -c "^$fname$"` = "0" ]
        then
          echo "$fname is an invalid mailid within our network."
          exit 1
        fi
      fi
    fi

# Move command and remove log and message files, if they exist.

    if [ -f $HOME/.maildelivery ]
    then
      mv $HOME/.maildelivery $HOME/.old.maildel
    fi
    rm $HOME/.fwdlog > /dev/null 2>/dev/null
    rm $HOME/.fwdmsg > /dev/null 2>/dev/null
    echo Forwarding mail for $whoyou to $fname;

# Create the .maildelivery file in the user's home directory.

    echo "# forward mail from $whoyou to "$fname >$HOME/.maildelivery
    echo "* - | A /usr/local/bin/resend "$fname >>$HOME/.maildelivery
    echo "* - | R BINDIR/notify \$(sender) $HOME $fname $whoyou" >> $HOME/.maildelivery

# Enter a message file for users to notify others about mail forwarding.

    echo "Enter a one line message to notify users that you are forwarding mail:"
    read response
    echo "$whoyou has forwarded mail to $fname" > $HOME/.fwdmsg
    echo "" >> $HOME/.fwdmsg
    echo "$response" >> $HOME/.fwdmsg

# Let the user who is having mail forwarded to them know about it.

    echo "FYI:  $whoyou has forwarded mail to you" | /usr/local/bin/v6mail "$fname" -s "$whoyou Forwarded Mail to You"
          echo "$whoyou : "$fname >> /usr/acd/news/forward.log
    chmod 644 $HOME/.fwdmsg
    chmod 644 $HOME/.maildelivery

# Mail forwarding does strange things if a .signature file exists in the
# user's home directory.  Move the .signature file until mail forwarding
# is terminated.

    if [ -f $HOME/.signature ]
    then
      mv $HOME/.signature $HOME/.signature.fwd
    fi
    echo "Your mail is set to be forwarded to $fname"
    ;;

# Give helpful usage message if user invokes with too many parameters.

  *)  echo "usage:  $0 [userid]"
    ;;
esac
