# This is a really basic mail sender script
# It uses exec and a telnet slave process
# You will probably need to hack it a bit since
# most mail servers will simply refuse to
# send so spam-looking mail :D

# Ah... remember that spamming creates bad karma.


# exec a telnet slave.
# Since we want to make sure that it does not run for too long,
# we give it 60 seconds to complete the operations

exec -x -k=60000 -t -e ("telnet my.mail.server.com 25")
{
	# We keep the state in the %:state extended scope variable

	if($0 == "started")
	{
		# This is the first event triggered
		# Initialize our variables...
		echo [process started]
		%:state = 0
		# This is the source address as specified in the MAIL command
		%:from = "<me@me.com>"
		%:rcpt = "<me@me.com>"
		# This is the From field
		%:sender = "\"Me\" <me@me.com>"
		%:receiver = "\"Me\" <me@me.com>"
		%:body = "Hello, this is a test mesage for the small KVIrc mail script$cr$lf"
		%:body .= "I hope that you have fun writing in kvs :)))"
		%:subject = "This is a test subject"
		# Returning an empty string does not write to stdin
		return
	}
	
	if($0 == "stderr")
	{
		# This is our stderr handler.. just in case something goes wrong
		echo "[stderr] $1"
		return
	}
	
	if($0 == "terminated")
	{
		echo "[process terminated]"
		return
	}

	# Ok.. this must be a stdout data event
	# Echo the line to the user
	echo "[stdout] $1"

	# And make our little state machine work
	switch(%:state)
	{
		case(0):
		{
			# We're waiting for 220 (ready)
			if($str.match("220*",$1))
			{
				# Send the HELO and go to next state
				%:state++
				echo "Sending HELO..."
				return "HELO myhostname$cr$lf";
			}
		}
		break
		case(1):
		{
			# Waiting for 250 (after the HELO)
			if($str.match("250*",$1))
			{
				# Send the MAIL command
				%:state++
				echo "Sending MAIL..."
				return "MAIL From: %:from$cr$lf"
			} else {
				# The server replied with something that is not a 250...
				# Fail :/
				echo "HELO command not accepted: $1"
				halt
			}
		}
		break;
		case(2):
		{
			# Waiting for another 250 (MAIL accepted)
			if($str.match("250*",$1))
			{
				# ...
				%:state++
				echo "Sending RCPT..."
				return "RCPT To: %:rcpt$cr$lf"
			} else {
				echo "MAIL command not accepted: $1"
				halt
			}
		}
		break;
		case(3):
		{
			# Waiting for another 250 (RCPT accepted)
			if($str.match("250*",$1))
			{
				%:state++
				echo "Sending DATA..."
				return "DATA$cr$lf"
			} else {
				echo "RCPT not accepted: $1"
				halt
			}
		}
		break;
		case(4):
		{
			# Waiting for 354 (ok, go on)
			if($str.match("354*",$1))
			{
				# You will probably need to hack in the Date: field :)
				%:state++
				echo "Sending body..."
				%x = "From: %:sender$cr$lf"
				%x .= "To: %:receiver$cr$lf"
				%x .= "Subject: %:subject$cr$lf"
				%x .= "Reply-To: %:sender$cr$lf"
				%x .= "Date: Thu, 8 Apr 2004 05:28:01 +0200$cr$lf"
				%x .= "X-Mailer: KVIrc funky KVS script$cr$lf"
				%x .= "$cr$lf"
				%x .= "%:body$cr$lf$cr$lf"
				%x .= ".$cr$lf"
				return %x
			} else {
				echo "DATA not accepted: $1"
				halt
			}
		}
		break;
		case(5):
		{
			# We don't wait anymore :)
			%:state++
			echo "Sending QUIT..."
			return "QUIT$cr$lf"
		}
		break;
		default:
		{
			# Usually the mail server closes the connection
			%:state++
			if(%:state > 10)
			{
				# But if it does not in few messages
				# Then force the process to die
				halt
			}
		}
	}
}