#! /bin/csh
# exceedingly user-friendly shell script
# for sending anonymous mail
# program will prompt user for everything it needs
# and build the SMTP file and send it all by itself.

#	The following is an easy to use shell script for sending
#	mail with a from field of your choice.  This script does
#	not guarantee total anonymity, but the average user won't
#	know how to trace it.  Thanks to Aimee for writing it.
#			--Bob

# put the scratch files where they won't bug anyone
# name they so that they won't overwrite anything
set pid = $$
set scratch = /usr/tmp/amail.$pid
set tousers = /usr/tmp/tousers.$pid
set message = /usr/tmp/message.$pid

# make files exist so cleanup won't encounter errors
touch $scratch
touch $tousers
touch $message
onintr cleanup

# ask for host, default athena 25
echo -n Enter sending host name \(athena.mit.edu\)' '
set reply = ( $< )
if ( $reply =~ '') then
     set machine=athena.mit.edu
else
     set machine = $reply
endif

# ask for From field, default real user
echo -n Enter return-path \($USER@`hostname`\)' '
set reply= ( $< )
if ( $reply =~ '') then
     set fromuser = $USER@`hostname`
else
     set fromuser = $reply
endif

# ask for To field, don't go on until it is specified
# allows for multiple recipients
# The last line in the tousers file will be RCPT To: .
# This will not cause a fatal error, nor is it noticeable 
# in the amail once it is received.
# Problem: if the user gives a dot as the first recipient,
# it will not realize that there have been no recipients specified.
set reply = ''
echo Enter recipients, one at a time.
echo Enter a dot on a line by itself when finished.
while (( $reply =~ '.' ) == 0 )
     set reply = ''
     while ( $reply =~ '' )
          echo -n Enter recipient ' '
          set reply = ( $< )
     end
     if (( $reply =~ '.' ) =~ 0 ) then
          echo RCPT To: $reply >> $tousers
          echo To: $reply >> $message
     endif
end
# ask for subject line
echo -n Subject\:' '
set reply = ( $< )
echo Subject: $reply >> $message

# ask for file where text of message is located
# defaults to letting user type message in by hand
# checks for existence of text file before trying to use it
set dummy = 1
while $dummy
     set dummy = 0
     echo Enter the name of the file containing the text,
     echo -n or hit return to type the message in by hand. ' '
     set reply = ( $< )
     if ( $reply =~ '' ) then
          echo Enter text of message.
          echo End with a dot on a line by itself.
          set input = ' '
          set foo = 1
          while $foo
               if (( $input =~ '.' ) =~ 0) then
                    set line = ( $< )
                    set input = ( $line ' ')
                    echo $line >> $message
               else set foo = 0
               endif
          end
     else if ( ! -e $reply ) then
          set dummy = 1
          echo File does not exist.
     else if ( ! -r $reply ) then
          set dummy = 1
          echo File is not readable.
     else
          cat $reply >> $message
     endif
end

# build smtp file
echo Building scratch file. . . .
echo 					>> $scratch
#send a blank line, for those mailers that require helo
echo HELO `hostname`			>> $scratch
echo MAIL From: $fromuser		>> $scratch
cat $tousers				>> $scratch
echo DATA				>> $scratch
cat $message				>> $scratch
echo \.					>> $scratch
# send two dots in case file doesn't end with a newline
echo \.					>> $scratch
echo QUIT				>> $scratch
# send lowercase quit in case connection is refused
echo quit				>> $scratch

# send message
echo Sending. . . .
telnet $machine 25 < $scratch

# get rid of scratch files
cleanup:
echo Removing scratch files. . . . 
rm $scratch
rm $tousers
rm $message
echo Done.
