#!/usr/athena/bin/perl

unshift (@INC, '/var/www/cgi-bin/lib');
require 'zsend.pl';
require 'parseform.pl';

$query=$ENV{'QUERY_STRING'};

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

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

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

<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>

If you do not wish to receive messages from the zwrite gateway, edit your
<TT>.zwgc.desc</TT> file and insert the following three lines of code at
the top of that file

<PRE>
if(\$sender =~ "www-zephyr") then
	exit
endif
</PRE>
</body></html>
EndOfForm
}

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

	%resp = &parseform($info);		
	$host = "stuff.mit.edu";
	$resp{'to'} = "mkgray" unless $resp{'to'};
	return if ($resp{to} eq "frg93");
 	foreach $person (split(' ', $resp{'to'})){
	    $person =~ s/[^\d\w\@]*//g;
	    $resp{from} =~ s/[^\d\w\@]*//g;
	    next if $person eq "" || $person =~ /^@.*$/;
	    open(ZWRITE,"|-", qw(zwrite -d -q -l -n -s), "message from $resp{from} at $ENV{REMOTE_ADDR}", $person);
	    print ZWRITE "$resp{body}";
	    close ZWRITE;

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

# 	@zsend'zhmaddr = unpack("C4",$zsend'localaddr);

# 	  $_ = &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://stuff.mit.edu/zwrite. Problems/comments\nshould be sent to stuffmaster\@mit.edu)");

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

1;
