#!/bin/bash
set -e
# ETRN tickle
#
# 26/04/2003
# Ferdinand Hagethorn <scripts -at- precompiled -dot- org>
#

# set debug to true if you want to see what the server said about our
# responses
debug=false

function printhelp () {
	echo "$(basename $0)"
	echo "It will send the ETRN <domain> to the MTA there"
	echo "Usage: $(basename $0) etrnhost domain.ext"
	echo "example: $(basename $0) mail.xs4all.com etrntrigger.com"
	exit 1
}

if [ x$1 == xchatscriptmode1231412 ];then
	while read line;do
		case "$line" in
			220*)
				echo "ETRN $2"
			;;
			250*)
				echo "quit"
			;;
			221*)
				exit 0
			;;
		esac	
	done
	exit 0
fi


if [ x$1 == x ];then
	printhelp
fi

if [ x$2 == x ];then
	printhelp
fi

FIFO="/tmp/$RANDOM.$RANDOM"

if [ ! -x "$FIFO" ];then
	mkfifo "$FIFO"
else
	echo tampering found with $FIFO
	exit 1
fi
if [ ! -f "$FIFO.log" ];then
	touch $FIFO.log
else
	echo tampering found with $FIFO.log
	echo 1
fi

MAILHOST=$1
HOST=$2

cat $FIFO | telnet $MAILHOST 25 2>/dev/null|tee -a $FIFO.log|$0 chatscriptmode1231412 $HOST >$FIFO
if $debug;then echo "/----DEBUG MODE----/";cat $FIFO.log;echo "\\----DEBUG MODE----\\";fi
rm $FIFO
rm $FIFO.log

