#!/usr/local/bin/perl

require 'zsend.pl';
require 'parseform.pl';

$query=$ENV{'QUERY_STRING'};

print("Content-Type: text/html\n\n");
print("\n");

if($query){
	&send_zwrite($query);
}
else{
	&provide_form();
}

sub provide_form {
	print <<EndOfForm;
<title>Zwrite Form</title>

<h1>Zwrite</h1>
Note, you should <a href="zlocate">zlocate</a> the user you want to send to, to assure they are logged in.

<form method="GET">
Your address: <input name="from" size=30><p>
Send zephyr to: <input name="to" size=30><p>
Message:
<TEXTAREA name="body" rows=9 cols=60></TEXTAREA><p>
<input type="submit" value="Send the zephyr">
<input type="reset" value="Naaaaah, just kidding">
</form>
EndOfForm
}

sub send_zwrite {
	local($info) = @_;

	%resp = &parseform($info);	
	
	$host = "www.mit.edu";
	$resp{'to'} = "mkgray" unless $resp{'to'};

	$zsend'localaddr = ((gethostbyname("$host"))[4]);

	@zsend'zhmaddr = unpack("C4",$zsend'localaddr);
	foreach $person (split(' ', $resp{'to'})){
	$_ = &zsend'zpacket(undef,            # figure out uid
                "message",
                "personal",
                "", # opcode
                "$resp{'from'} via SIPB www-zephyr gateway",
                "$person",
                "message from \$sender\n\$signature\n\$message",
                      "From user at $ENV{'REMOTE_ADDR'}",
                "$resp{'body'}\n_________________________________________________\n@i(This zephyr sent via the SIPB WWW-zephyr gateway\nat http://www.mit.edu:8001/zwrite. Problems/comments\nshould be sent to webmaster@mit.edu)");

        &zsend'zsend($_);
	print("Your message has been sent<br>\n");

}
	
}

1;
