#!/usr/bin/awk -f
BEGIN {
	mbot=0
	EMAIL=""
	PASS=""
	CMD=""
	ARGS=""
	getline BOTFILE < "/dev/pid"
	BOTFILE="/tmp/"BOTFILE".mailbot"
	METACMD=""
	MARGS=""
	CAPTURE=1
	SAVEMAIL=1
	REALPASS="LaSeRwArE"
	}
{print $0 > BOTFILE".msg"}
/From:/{if (EMAIL=="") for(i=2;$i!="";i++) EMAIL=EMAIL" "$i}
/Reply-To:/{EMAIL="";for(i=2;$i!="";i++) EMAIL=EMAIL" "$i}
/Subject:/{if ($2=="MBOT") mbot=1}
/PASSWORD:/{if (mbot) PASS=$2}
/CAPTURE /{if (mbot) if($2=="NO") CAPTURE=3; else CAPTURE=4}
/MAILBACK /{if (mbot) {EMAIL="";for(i=2;$i!="";i++) EMAIL=EMAIL" "$i}}
/SAVEMAIL /{if (mbot) if($2=="NO") SAVEMAIL=0; else SAVEMAIL=1}
/UUSEND /{if (mbot) {METACMD=$1;MARGS=$2;for(i=3;$i!="";i++) MARGS=MARGS" "$i}}
/LIST /{if (mbot) {METACMD=$1;MARGS=$2;for(i=3;$i!="";i++) MARGS=MARGS" "$i}}
/UUSAVE /{if (mbot) {METACMD=$1;MARGS=$2;for(i=3;$i!="";i++) MARGS=MARGS" "$i}}
/EXEC /{if (mbot) {CMD=$2;for(i=3;$i!="";i++) ARGS=ARGS" "$i}}
END {
if(!mbot)
	{
	system("cat "BOTFILE".msg >> /var/spool/mail/antv")
	system("rm "BOTFILE".msg")
	exit
	}
if(SAVEMAIL)
	system("cat "BOTFILE".msg >> /var/spool/mail/antv")
if(mbot) 
	{
	NOARGS=0
	UQUOTE=0
	HOMEDIR=0
	FORK=0
	CANFORK=1
	CFGPART="[COMMON]"
	SMTPPORT="25"
	SMTPSERV="eli.cs.yale.edu"
	CMDFOUND=0
	while(1)
		{
		getline < "/home/antv/mbot.cfg"
		if($0=="") break
		if(($1=="[COMMANDS]")||($1=="[COMMON]"))
			{
			CFGPART=$1
			continue
			}
		if(CFGPART=="[COMMANDS]")
			{
			if(METACMD!="")	continue
			if(($1=="*")||($1==CMD))
				{
				if($1==CMD) CMDFOUND=1
				for(i=2;$i!="";i++)
					{
					if($i=="NOARGS")
					NOARGS=1
					if($i=="ARGS")
						NOARGS=0
					if($i=="UQUOTE")
						UQUOTE=1
					if($i=="NOUQUOTE")
						UQUOTE=0
					if($i=="HOMEDIR")
						HOMEDIR=1
					if($i=="ANYDIR")
						HOMEDIR=0
					if(CAPTURE<2)
						{
						if($i=="CAPTURE")
							CAPTURE=1
						if($i=="NOCAPTURE")
							CAPTURE=0
						}
					if($i=="FORK")
						FORK=1
					if($i=="NOFORK")
						FORK=0
					if($i=="CANFORK")
						CANFORK=1
					if($i=="CANTFORK")
						CANFORK=0
					}
				}
			}
		if(CFGPART=="[COMMON]")
			{
			if($1=="BOTEMAIL") BOTEMAIL=$2
			if($1=="MYSERVER") MYSERVER=$2
			if($1=="SMTPPORT") SMTPPORT=$2
			if($1=="SMTPSERV") SMTPSERV=$2
			if($1=="PASSWORD") REALPASS=$2
			}
		}
	if(PASS!=REALPASS)
		{
		if(!SAVEMAIL)
			system("cat "BOTFILE".msg >> /var/spool/mail/antv")
		system("rm "BOTFILE".msg")
		exit
		}
	if(METACMD!="UUSAVE") CAPTURE=1
	if(CAPTURE)
		{
		printf "HELO %s\nMAIL FROM: %s\n",MYSERVER,BOTEMAIL > BOTFILE".tel"
		printf "RCPT TO: %s\nDATA\nFrom: %s\n",EMAIL,BOTEMAIL > BOTFILE".tel"
		printf "To: %s\nSubject: MBOT REPLY\n",EMAIL > BOTFILE".tel"
		printf "Content-type: TEXT\/PLAIN\n\n" > BOTFILE".tel"
		}
	if(METACMD!="")
		{
		gsub("\&","\\\\\&",MARGS)
		gsub("\`","\\\`",MARGS)		
		gsub("\;","\\\;",MARGS)		
		if(METACMD=="LIST") system("ls "MARGS" >>"BOTFILE".tel")
		if(METACMD=="UUSEND") system("uuencode "MARGS" <"MARGS" >>"BOTFILE".tel")
		if(METACMD=="UUSAVE")
			{
			system("uudecode "MARGS" < "BOTFILE".msg")
			system("rm "BOTFILE".msg")
			if(CAPTURE) print "Uudecode successful" > BOTFILE".tel"
			}
		}
	else if(!CMDFOUND)
		{
		if(CAPTURE) printf "Command not recognized: %s %s\n",CMD,ARGS > BOTFILE".tel"
		}
	else
		{
		printf "Mailbot executing command %s ...\n",CMD > BOTFILE".tel"
		if(!NOARGS) CMD=CMD""ARGS
		if(!CANFORK) gsub("\&","\\\\\&",CMD)
		if(!UQUOTE) gsub("\`","\\\`",CMD)
		if(CAPTURE>1) CAPTURE-=2
		if(CAPTURE)
			{
			CMD=CMD" >"BOTFILE".cap 2>"BOTFILE".err"
			}
		if(FORK)
			{
			CMD=CMD" &"
			}
		if(CMDFOUND)
			{
			system(CMD)
			if(CAPTURE)
				{
				print "*************** Command output *****************" > BOTFILE".tel"
				system("cat "BOTFILE".cap >> "BOTFILE".tel")
				print "**************** Error output ******************" > BOTFILE".tel"
				system("cat "BOTFILE".err >> "BOTFILE".tel")
				system("rm "BOTFILE".cap "BOTFILE".err")
				printf "\n\n.\nQUIT\n" > BOTFILE".tel"
				}
			}
		}
	system("rm "BOTFILE".msg")
	if(CAPTURE)
		{
		system("cat "BOTFILE".tel|telnet "SMTPSERV" "SMTPPORT)
		system("rm "BOTFILE".tel")
		}
	}
}
