#!/usr/bin/perl
# ^^^^^^^^^^^^^
# Make sure this is the location of your perl installation
# 'which perl' should do the trick.

########################################################
#                                                      #
#     Webex Mail Daemon 1.0 Beta                       #
#                                                      #
# (C) 1995, Matt Mankins (mmankins@mail.arc.miami.edu) #
#                                                      #
########################################################

## ---         Make sure these are correct       --- ###

#User Defines
#
#$webexpath = Webspace path for this script
#$IMGPATH   = Webspace path for webex images
#$imgpath   = *Absolute* path for webex images
#$machine   = URL for your machine -- ex. http://www.sony.com

$webexpath="/members/cgi-bin/uncgi/webex";            
$IMGPATH="/images/webex";
$imgpath="/var/lib/www/pub/images/webex";
$machine="http://www.knotwork.com"; 

### ---  You don't need to change anything else  --- ###
$keyserver = 'www-swiss.ai.mit.edu:80';
$keypath = '/htbin/pks-extract-key.pl?op=get&search=';
                                                                 
#Constants
$version = '1.03 Beta';

#Passed Variable Translation
($host, $user_name, $password, $folder) = split (/:/, $ENV{"WWW_data"});

require 'sock.pl';

# Format for Web output
if ($ARGV[0] ne "setup")
 {
  print "Content-type: text/html\n\n";
  print "<HTML>\n";
  print '<body background="',$IMGPATH,'/bg.gif" vlink="ff0000" alink="ffff00" text="ffffff">';
 }
&directional;
&footer;
exit;

#Directionals
sub directional
{
 local ($number, $passed);
 ($passed, $number,$extra) = split(/:/, $ENV{"WWW_passed"});
 if ($passed eq "gather")
  {
   &gather; #gather index from hosts
  }
 elsif ($passed eq "main")
  {
   &main;
  }
 elsif ($passed eq "get")
  {
   &get($number);
  }
 elsif ($passed eq "compose")
  {
   &compose;
  }
 elsif ($passed eq "compost")
  {
   &compost($number);
  }
 elsif ($passed eq "sendmessage")
  {
   &sendmessage;
  }
 elsif ($passed eq "del")
  {
   &del($number);
  }
 elsif ($passed eq "change")
  {
   &change;
  }
 elsif ($passed eq "changed")
  {
   &changed;
  }
 elsif ($passed eq "rep")
  {
   $comebackhere = $number;
   &rep($number);
  }
 elsif ($passed eq "repinc")
  {
   $comebackhere = $number;
   &repinc($number);
  }
 elsif ($passed eq "for")
  {
   $comebackhere = $number;
   &forward($number);
  }
 elsif ($passed eq "fol")
  {
   &fol($number);
  }
 elsif ($passed eq "postcard")
  {
   &postcard;
  }
 elsif ($passed eq "save")
  {
   &save($number);
  }
 elsif ($passed eq "first")
  {
   &first;
  }
 elsif ($passed eq "take")
  {
   &remember($number);
  }
 elsif ($passed eq "remember_addr")
  {
   &remember_addr;
  }
 else
  {
   &login; #login page
  }
}

sub connection
{
 #Open the connection
 local ($where, $port);
 ($where, $port) = split (/:/, "@_");
 if (!($port))
  {
   $port = 2222;
  }
 $s = &sock'open("$where",$port) || die $!;
}

sub passme
{
 $_  = '<input type="hidden" name="data" value="';
 $_ .= "$host:$user_name:$password:$folder";
 $_ .= '">';
}

sub authenticate
{
# Read in names and Encrypted Passwords
 local ($name, $pass, $junk);
  while (($name,$pass,$junk) = getpwent)
   {
    if ( ($name eq $ENV{"WWW_user_name"} ) &&
       (substr(crypt( $ENV{"WWW_password"}, substr($pass,0,2)),0,13) eq
        substr($pass,0,13)))
      {
       $validated = 1;
      }
   }
  endpwent;
}


sub gather
{
 local ($tmp, $index);
 print "<TITLE>Message Index</TITLE>";
 print '<CENTER><IMG SRC="',$IMGPATH,'/letters.gif"></CENTER><HR>';
     print "<B>",$user_name,"'s ";
     print 'messages from: <EM>',$host,'</EM>, in folder <EM>';
     if ($folder)
      {
       print "$folder</EM>\n";
      }
     else
      {
       print "INBOX</EM>\n";
      }
     &connection($host);
     print $s "login $user_name###$password###$folder\r\n";
     print $s "index $gmessages[($index)]\r\n";
     print $s "quit\r\n";

      while ($tmp = <$s>)
       {
        #says you're in gather mode
        #$z = 2;
        &passme;
        $tmp =~ s/<--PASS-->/$_/g;
        $_ = '<FORM METHOD="POST" ACTION="';
        $_ .= $webexpath;
        $_ .= '">';
        $tmp =~ s/<--FORM-->/$_/g;
        print $tmp;
       }
      &sock'close($s);
      print '<PRE>';
      print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
      print '<INPUT type="hidden" name="passed" value="compose">';
      print '<INPUT type="submit" value="Compose New Message">';

      print &passme;
      print '</FORM><FORM METHOD="POST" ACTION="',$webexpath,'">';
      print '<INPUT type="hidden" name="passed" value="gather">';
      print '<INPUT type="submit" value="Reload Message Index">';
    
      print &passme;

      print '</FORM><FORM METHOD="POST" ACTION="',$webexpath,'">';
      print '<INPUT type="hidden" name="passed" value="main">';
      print '<INPUT type="submit" value="Main Menu">';
      print &passme;
      print '</PRE>';
}

sub get
{
 local ($tmp);
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "get @_\r\n";
 print $s "quit\r\n";

 print "<pre>";
 while ($tmp = <$s>)
       {
        $z = 2;
        &passme; #says don't do a user_name
        $tmp =~ s/<--PASS-->/$_/g;
        $_ = '<FORM METHOD="POST" ACTION="';
        $_ .= $webexpath;
        $_ .= '">';
        $tmp =~ s/<--FORM-->/$_/g ;
        $tmp =~ s/<--src-->/$IMGPATH\//g;
        print $tmp;
       }
 print "</pre>";
 &sock'close($s);
}

sub del
{
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "del @_\r\n";
 print $s "quit\r\n";
 while ($tmp = <$s>)
  {
   &passme;
   $tmp =~ s/<--PASS-->/$_/g;
  }
 print $tmp;
 &sock'close($s);
 print "Message Deleted.<HR>";
 &get("@_");
} 

sub first
{
 local ($unencrypted);
 $user_name= $ENV{"WWW_user_name"};
 $host = $ENV{"WWW_host"};
 $password = $ENV{"WWW_password"};
 &main;
}

sub login
{
 print "<TITLE>Webex Mail Client Login</TITLE>\n";
 print "<CENTER><H2>Webex Mail Client Login</H2></CENTER><HR>\n";
 print '<font size=+2>W</font>elcome to <font size=+2>W</font>ebex<P>';
 print "  <font size=+2>T</font>o login to Webex, enter a hostname running the Webex Daemon,";
 print "  along with your username and password for that site.<P>";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print '<STRONG><font size=+2>W</font>ebex compliant hostname</STRONG> -- <EM>Example: mail.mymost.com</EM><BR>';
 print '<INPUT name= "host" size=30><P>';
 print '<STRONG><font size=+2>U</font>sername </STRONG><input name="user_name" size=9 value="';
 print $user_name.'"><BR>';
 print '<STRONG><font size=+2>P</font>assword </STRONG><input name="password" type="password" size=10><P>';
 print '<INPUT type="hidden" name="passed" value="first">';
 print '<INPUT type="submit" value="Enter">';
 print '</FORM>';
}

sub main
{
 print "<TITLE>Webex Main Menu</TITLE>\n";
 print "<CENTER><H2>Webex Main Menu</H2>\n";
 print '<IMG SRC="',$IMGPATH,'/webex.gif"></CENTER><HR>';
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 $z = 1; #sets up user/pass info for &passme
 print &passme;
 print '<input type="radio" name="passed" value="gather" checked>';
 print "View Message Index<BR>\n";
 print '<input type="radio" name="passed" value="compose">';
 print "Compose New Message<BR>\n";
 print '<input type="radio" name="passed" value="postcard">';
 print "Send Postcard<BR>\n";
 print '<input type="radio" name="passed" value="change">';
 print "Change Folder <EM>(current folder: ";
 if (!($folder))
  {
   print "INBOX";
  }
 else
  { 
   print $folder;
  }
 print ")</EM><BR>\n";
 print '<input type="radio" name="passed" value="login">';
 print 'Change Site <EM>(current site: ';
 print $host.")</EM><BR>\n";
 print '<input type="submit" value="Enter">';
}

sub compose
{
 print "<TITLE>Compose Message</TITLE><CENTER><H2>Compose Message</H2></CENTER><HR>\n";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print &passme;
 print '<INPUT type="hidden" name="passed" value="sendmessage">';
 print '<B>To:</B><BR>';
   print '<INPUT name="addresses" size="80" value="',$TO,'"><BR>';
 print '<BR><B>Subject:</B><BR>';
 print '<INPUT name="subject" size=80 value="'.$SUBJ.'"><P><B>Message Body:</B><BR>';
 print '<TEXTAREA name="body" rows=10 cols=80>';
 print @_;
 &signature;
 print "</TEXTAREA><P>\n";
 print '<INPUT type="checkbox" name="anon" value="anon">Send anonymously with a chain length of ';
print '<INPUT size=2 name="chain" value="3">.<BR>'; 
# print '<INPUT type="checkbox" name="pgp" value="pgp">Attempt PGP encryption<BR>'; 
# print '<INPUT type="checkbox" name="abort" value="abort">Cleartext on error<P>';  
 print '<INPUT type="submit" value="Mail Message">';
 print '</FORM>';
}

sub address
{
 local ($index);
 @addresses = split ( /,/, $ENV{"WWW_addresses"});
 foreach (@addresses)
  {
   if ( (!($addresses[$index] =~ /@/)) )
    {
     $addresses[$index] =~ y/a-zA-Z//cd;
     &connection($host);
     print $s "login $user_name###$password\r\n";
     print $s "address $addresses[$index]\r\n";
     print $s "quit\r\n";
     $addresses[$index] = <$s>;
     chop ($addresses[$index]);
     &sock'close($s);
    }
   if ($index != 0)
    {
     $to .= ", $addresses[$index]";
    }
   else
    {
     $to = $addresses[0];
    }
   $index++;
  }
 $to =~ s/\n//;
}

sub forwarded
{
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "body $number\r\n";
 print $s "quit\r\n";
 while (<$s>)
  {
   print;
  }
 &sock'close($s);
}

sub signature
{
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "signature\r\n";
 print $s "quit\r\n";
 print "\n\n";
 while (<$s>)
  {
   print;
  }
 &sock'close($s);
}

sub rep
{
 #Does a compose using ("repto $number") as the default name  
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "repto $number\r\n";
 print $s "quit\r\n";
 $TO = <$s>;
 chop ($TO);
 ($TO, $SUBJ) = split (/###/, $TO);
if (!($SUBJ =~ /^Re:/))
  {
   $SUBJ = "Re:$SUBJ";
  }
 &compose("");
 &sock'close($s);
}

sub repinc
{
 #Does a compose using ("repto $number") as the default name
 #includes original in reply  

 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "ondate $number\r\n";
 print $s "quit\r\n";
 $BODY = <$s>;
 &sock'close($s);
 $BODY .= "\n";
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "text $number\r\n";
 print $s "quit\r\n";

 while ($temp = <$s>)
  {
   $BODY .= ">";
   $BODY .= $temp;
  }
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "repto $number\r\n";
 print $s "quit\r\n";

 $TO = <$s>;
 chop ($TO);
 ($TO, $SUBJ) = split (/###/, $TO);
 if (!($SUBJ =~ /^Re:/))
  {
   $SUBJ = "Re:$SUBJ";
  }
 &compose($BODY);
}

sub sendmessage
{
 local ($mailcommand,$a,$b);
 $a = &date;
 &address;
 $_  = "To: $to\n";
 $_ .= "From: $user_name@$host\n";
 if (!($ENV{"WWW_anon"}))
  {
   $_ .= "Chain: ;\n";
  }
 else
  {
   $_ .= 'Chain: ';
   $_ .= $ENV{"WWW_chain"};
   $_ .= "\n";
  }
 $_ .= "Date: $a\n";
 $_ .= 'Subject: ';
 $_ .= $ENV{"WWW_subject"};
 $_ .= "\n";
 if ($ENV{"WWW_pgp"})
  {
   &getkey;
   $_ .= "Key: $to\n";
  }
 $_ .= "X-Mailer: Webex $version\n\n";
 if ($ENV{"WWW_imgfile"})
  {
   $_ .= "----Webex Encoded Postcard----<HR>\n";
   $_ .= '<WEBEX><CENTER><img src="';
   $_ .= "$machine$IMGPATH/";
   $_ .= $ENV{"WWW_imgfile"};
   $_ .= '"></CENTER><BR>';
   $_ .= "\n";
  }
 $_ .= $ENV{"WWW_body"};
 $_ .= "\n";
 if (!($abort))
  {
   $mailcommand = "$_\r\n</SENDMAIL>\r\n";
   &connection($host);
   $temp = "login $user_name###$password\r\n";
   $temp .= "sendmail\r\n";
   $temp .= $mailcommand;
   $temp .= "\r\nquit\r\n"; 
   print $s $temp;
   &sock'close($s);
  }
 else
  {
   print "<H2>Error: Could not find PGP key.  Message not sent.</H2>";
  }
 if (!($comebackhere))
  {
   &main;
  }
 else
  {
   &get($comebackhere);
  }
}

sub forward
{
 #$number = message to be forwarded
 print "<TITLE>Forward Message</TITLE><CENTER><H2>Forward Message</H2></CENTER><HR>\n";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print &passme;
 print '<INPUT type="hidden" name="passed" value="sendmessage">';
 print '<B>To:</B><BR>';
 print '<INPUT name="addresses" size="80"><BR>';
 #&address_select; #prints address book
 print '<BR><B>Subject:</B><BR>';
 print '<INPUT name="subject" value="" size=80><P><B>Message Body:</B><BR>';
 print '<TEXTAREA name="body" rows=10 cols=80>';
 &signature;
 &forwarded($number);
 print "</TEXTAREA><P>\n";
 print '<INPUT type="submit" value="Mail Message">';
}

sub date
{
 #returns a formated date
 local ($sec, $min, $hour,$mday, $mon, $year, $wday, $yday, $isdst);
 $sec = (localtime)[0];
 $min = (localtime)[1];
 $hour = (localtime)[2];
 $mday = (localtime)[3];
 $mon = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[(localtime)[4]];
 $year = (localtime)[5];
 $wday = (Sun,Mon,Tue,Wed,Thu,Fri,Sat)[(localtime)[6]];
 $_ = "$wday, $mday $mon 19$year $hour:$min:$sec";
}


sub fol
{
 local ($temp);
 $temp = "@_";
 print "<TITLE>Save Message to Folder</TITLE>\n";
 print "<H2>Select a folder to save message in:</H2>\n";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print '<B>Create a new folder<menu><input name="newf" size=15></menu>';
 print 'or select an existing folder';
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 print $s "folder\r\n";
 print $s "text @_\r\n";
 print $s "quit\r\n";
 print &passme;
 print '<input type="hidden" value="save:'.$temp.'" name="passed">';
 print "<PRE>\n";
 while (<$s>)
  {
   print;
  }
 print "\n</PRE></FORM></BODY>";
 &sock'close($s);
}

sub save
{
 &connection($host);
 print $s "login $user_name###$password###$folder\r\n";
 if (!($ENV{"WWW_newf"}))
  {
   print $s "append @_:".$ENV{"WWW_folder"}."\r\n";
  }
 else
  {
   print $s "append @_:".$ENV{"WWW_newf"}."\r\n";
  }
 print $s "quit\r\n";
 &sock'close($s);
 print "Message saved to folder ";
 if ($ENV{"WWW_folder"})
  {
   print $ENV{"WWW_folder"};
  }
 else
  {
   print $ENV{"WWW_newf"};
  }
 print '<HR>';
 &get(@_);
}

sub change
{
#Changes the WWW_box to be current folder
 print "<TITLE>Change Current Folder</TITLE>\n";
 print "<H2>Choose a folder</H2>\n";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print '<input type="checkbox" name="inbox" value="inbox">Inbox <EM>(Default)</EM><BR>'; 
 &connection($host);
 print $s "login $user_name###$password\r\n";
 print $s "folder c\r\n";
 print $s "quit\r\n";
 print &passme;
 print '<input type="hidden" value="changed" name="passed">';
 while (<$s>)
  {
   print;
  }
 print "\n</FORM></BODY>";
 &sock'close($s);
}

sub changed
{
 if ($ENV{"WWW_inbox"})
  {
   undef ($folder);
  }
 else
  {
   $folder = $ENV{"WWW_folder"};
  }
 &main;
}

sub footer
{
#cleans up HTML -- adds footer
 print '<HR><EM><A HREF="http://www.arc.miami.edu/matt/webex">Webex '.$version.'</A>';
 print ' -- <A HREF="http://www.arc.miami.edu/cgi-bin/matt">';
 print 'Matt Mankins</A>';
 print "</BODY></HTML>\n";
}

sub remember
{
 print "<TITLE>$user_name's Addressbook</TITLE>\n";
 print "<CENTER><H2>Addressbook</H2></CENTER><HR>\n";
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print &passme;
 print "To add an entry to the addressbook, enter the Internet address,\n";
 print "an easy to remember nickname <I>(which you can substitute for the\n";
 print "actual address)</I>, and a first/last name.<P>\n";
 print '<BR>Internet Address: ';
 print '<input name="inet" size=40 value="',$extra,'">';
 print '<BR>Nickname: ';
 print '<input name="nick" size=10>';
 print '<BR>First name: ';
 print '<input name="first" size=10>';
 print ' Last name: ';
 print '<input name="last" size=10>';
 print '<input type="hidden" name="passed" value="remember_addr">';
 print '<input type="hidden" name="number" value="',"@_",'">';
 print '<P><input type="submit" value="Add Address">';
 print '<HR>Current book:<P>';
 print '<pre>';
 &connection($host);
 print $s "login $user_name###$password\r\n";
 print $s "getbook\r\n";
 print $s "quit\r\n";
 while (<$s>)
  {
   print;
  }
 &sock'close($s);
 print '</pre></form>';
} 

sub remember_addr
{
 local ($addthis);
 $addthis = join(':', $ENV{"WWW_inet"},$ENV{"WWW_nick"},$ENV{"WWW_first"},
                 $ENV{"WWW_last"});
 &connection($host);
 print $s "login $user_name###$password\r\n";
 print $s "addbook $addthis\r\n";
 print $s "quit\r\n";
 $number = $ENV{"WWW_number"};
 &sock'close($s);
 &get($number);
}

sub getkey
{
#attempts to get a PGP key
 foreach (@addresses)
  {
   &connection($keyserver);
   print $s "GET $keypath$addresses[$index]\n";
   while ($raw = <$s>)
    {
     if ( ($yes != -1) && ($raw =~ /.*-----BEGIN PGP PUBLIC KEY BLOCK-----.*/) )
      {
       $pgpkey[$index] .= $raw;
       $yes = 1;
      }
     elsif (($yes == 1) && (!($raw =~ /.*-----END PGP PUBLIC KEY BLOCK-----.*/)))
      {
       $pgpkey[$index] .= $raw;
      }
     elsif (($yes) && ($raw =~ /.*-----END PGP PUBLIC KEY BLOCK-----.*/))
      {
       $pgpkey[$index] .= $raw;
       $yes = -1;
      }
    }
   $yes = 0;
   &sock'close($s);
   if (!($pgpkey[$index]))
    {
   ($finger, $fingerhost) = split (/@/, $addresses[$index]);
     &connection("$fingerhost:79");
     print $s "$finger\n";
     while ($raw = <$s>)
      {
       if ( ($yes != -1) && ($raw =~ /.*-----BEGIN PGP PUBLIC KEY BLOCK-----.*/) )
        {
         $pgpkey[$index] = $raw;
         $yes = 1;
        }
       elsif (($yes == 1) && (!($raw =~ /.*-----END PGP PUBLIC KEY BLOCK-----.*/)))
        {
         $pgpkey[$index] .= $raw;
        }
       elsif (($yes) && ($raw =~ /.*-----END PGP PUBLIC KEY BLOCK-----.*/))
        {
         $pgpkey[$index] .= $raw;
         $yes = -1;
        }
      }
     $yes = 0;
     &sock'close($s);
    }
   if ((!($pgpkey[$index])) && (!($ENV{"WWW_abort"})) )
    {
     $abort = 1;
    }
print $pgpkey[$index];
   $index++;
  }
}

sub postcard
{
 local (@card, $index);
 print "<TITLE>Webex Postcards</TITLE>\n";
 print "<H2><CENTER>Send a Postcard</CENTER></H2><HR>\n"; 
 print "<EM>Select the postcard you wish to send:</EM><P>";
 opendir (POSTCARD, $imgpath);
 @card = sort (grep (!/^\.|bg.gif|letters.gif/, readdir(POSTCARD)));
 $index = 0;
 while ($card[$index])
  {
   print "\n",'<FORM METHOD="POST" ACTION="',$webexpath,'">';
   print &passme;
   print '<input type="hidden" name="passed" value="compost:',$card[$index],'">'; 
   print '<IMG SRC="',"$IMGPATH/$card[$index]",'" width=100 height=60><BR>';
   print '<input type="submit" value="',$card[$index],'"></FORM>';
   $index++;
  }
 closedir(FOLDER);
}

sub compost
{
 $imgfile = "@_";
 undef @_;
 print "<TITLE>Compose Postcard</TITLE><CENTER><H2>Compose Postcard Text</H2>\n";
 print '<IMG SRC="',"$IMGPATH/$imgfile",'"><HR></CENTER>';
 print '<FORM METHOD="POST" ACTION="',$webexpath,'">';
 print &passme;
 print '<INPUT type="hidden" name="passed" value="sendmessage">';
 print '<B>To:</B><BR>';
 print '<INPUT name="addresses" size="80" value="',$TO,'"><BR>';
 print '<BR><B>Subject:</B><BR>';
 print '<INPUT name="subject" size=80 value="'.$SUBJ.'"><P><B>Message Body:</B><BR>';
 print '<TEXTAREA name="body" rows=10 cols=80>';
 &signature;
 print "</TEXTAREA><P>\n";
 print '<INPUT type="checkbox" name="anon" value="anon">Send anonymously with a chain length of ';
 print '<INPUT size=2 name="chain" value="3">.<BR>'; 
# print '<INPUT type="checkbox" name="pgp" value="pgp">Attempt PGP encryption<BR>'; 
# print '<INPUT type="checkbox" name="abort" value="abort">Cleartext on error<P>';  
 print '<INPUT type="hidden" value="',$imgfile,'" name="imgfile">';
 print '<INPUT type="submit" value="Mail Message">';
 print '</FORM>';
}

 
