#!/usr/athena/bin/perl
#
# Modified from the original SIPB Zwrite CGI, for HDML support
#     Rex Min, December 1999

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

$query=$ENV{'QUERY_STRING'};

$hdml= ($ENV{'HTTP_USER_AGENT'} =~ /UP\.Link/);
if ($hdml) {
  print("Content-Type: text/x-hdml\n\n");
} else {
  print("Content-Type: text/html\n\n");
}

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

sub provide_form {
if ($hdml) {
  print <<EndOfForm;
<HDML VERSION=3.0 PUBLIC=TRUE MARKABLE=TRUE>
  <CHOICE TITLE="Zwrite" BOOKMARK="http://stuff.mit.edu/cgi/zw" KEY=w>
     <ACTION TYPE=ACCEPT LABEL=Edit TASK=GO DEST=\#\$w>
     <ACTION TYPE=SOFT1 LABEL=Zwrit TASK=GO DEST="?from=\$(zf:e)\&to=\$(zt:e)\&body=\$(zb:e)">
     <CENTER> PCS Zephyr
     <CE VALUE="zf"> Fr: [\$zf]
     <CE VALUE="zt"> To: [\$zt]
     <CE VALUE="zb"> Msg: [\$zb]
     <CE TASK=GO LABEL=Zloc DEST="zl?\$zt"> Zlocate \$zt
  </CHOICE>

  <ENTRY NAME="zf" KEY=zf> Zephyr From: </ENTRY>
  <ENTRY NAME="zt" KEY=zt> Zephyr To: </ENTRY>
  <ENTRY NAME="zb" KEY=zb> Message: </ENTRY>
</HDML>
EndOfForm
} else {     #### original HTML CODE 
	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>
</body></html>
EndOfForm
}
}

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

	%resp = &parseform($info);	
	
	$host = "stuff.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://stuff.mit.edu/zwrite. Problems/comments\nshould be sent to stuffmaster\@mit.edu)");

          &zsend'zsend($_);
          if ($hdml) {
            print("<HDML VERSION=3.0><DISPLAY>Your message has been sent.</DISPLAY></HDML>");
          } else {
	  print("Your message has been sent<br>\n");
          }
	}
}

1;
