#!/usr/local/bin/perl -w

###########################################################################
### $Id: //depot/personal/ryan/main/src/wham/cgi-bin/white.cgi#2 $
###########################################################################
###
###########################################################################
my( $BINDIR, $LIBDIR, $CGIDIR);

use FindBin '$RealBin';                             # locate libraries          
BEGIN {
$BINDIR="/usr/local/wham/bin";
$LIBDIR="/usr/local/wham/lib";
$CGIDIR="/usr/local/wham/cgi-bin";
}

use lib "$LIBDIR";
use strict;
use CGI;
use wham;

my( $cgi, $cmd, $line, $class, $count, $mid, $whoami, $err, %config, $addr);

open(STDERR, ">&STDOUT");
$|=1;
$cgi=new CGI;

%config=LoadConfig();

&PrintHTMLHeader();


if( defined( $cgi->param('address')))
{
  ### We've got an attempt to add an address to the whitelist
  print "<table cellpadding=0 cellspacing=0 border=0><tr>\n";
  print "<td bgcolor='#000000'>\n";
  print "<table cellpadding=2 cellspacing=1 border=0>\n";
  print "<tr><td class='b'>\n";

  ### Did we get a valid eddress
  if( $cgi->param('address') !~ /(\S+\@\S+)/) {
    print "<span class='error'>";
    print "You do not appear to have entered a valid email address: <tt>";
    print $cgi->param('address'),"</tt></span>\n";

  } else {

    ### Try to add the address
    if( ($err = &Whitelist( $1)) eq "") {
      print "Your address <tt>" ,$cgi->param('address');
      print "</tt> has been added to the whitelist.<P>";
      print "Any mail from this address has been released.\n";
    } else {
      print "<span class='error'><b>Unable to add your address: <tt>";
      print $cgi->param('address');
      print "</tt> to the whitelist</span><p><pre>$err</pre>\n";
    }
  }
  print "</td></tr>\n";
  print "</table>\n";
  print "</td></tr>\n";
  print "</table>\n";
} 

&PrintForm();

###########################################################################
### &Whitelist($addr);
###########################################################################
### add user to the whitelist
###########################################################################
sub Whitelist
{
  my( $addr) = @_;
  my( @WHITE, $err);

  @WHITE = &ReadList( $config{white});

  ### Check that it doesn't already match
  foreach my $filter (@WHITE) {
    if( $addr =~ /$filter/i) {
      return "$addr is covered by whitelist rule '$filter'\n";
    }
  }

  ### Rocking...  Add it to the list
  if( $err = &AddToList( "white", "added via white.cgi", ($addr)) ne "") {
    return "$err";
  }

  ### Let's create the procmail.rc file that we'll use to
  ### filter out messages
  open( PROCRC, ">/tmp/procmailrc.wham.$$");
  print PROCRC <<EOF ;
### Set up a logfile
LOGFILE         = /tmp/procmaillog.wham.$$

:0:
* ^From MAILER-DAEMON
* ^Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
$config{spam}

EOF

  ### Instert the template into the procmailrc if necessary
  if( defined( $config{template})) {
    open( TEMPLATE, $config{ template}) || 
      die "ERROR: could not open $config{template} : $!\n";
    while( defined( $line = <TEMPLATE>)) {
      print PROCRC $line;
    }
    close( TEMPLATE);
  }
  close( PROCRC);

  &ProcessMail( "/tmp/procmailrc.$$");
  
  unlink( "/tmp/procmaillog.wham.$$");
  return "";
}

###########################################################################
### &PrintForm;
###########################################################################
###
###########################################################################
sub PrintForm
{
	print $cgi->start_form;
	print "<table cellpadding=0 cellspacing=0 border=0><tr><td bgcolor='#000000'>\n";
	print "<table cellpadding=2 cellspacing=1 border=0>\n";
	print "<tr><td class='a'>\n";
	print "Address:";
	print "</td><td class='a'>\n";
	print $cgi->textfield('address'), "<br>";
	#print "</td></tr><tr><td class='b'>\n";
	#print "Passphrase:";
	#print "</td><td class='b'>\n";
	#print $cgi->textfield('passphrase'), "<br>";
	print "</td></tr>\n";
	print "</table>\n";
	print "</td></tr></table>\n";
	print $cgi->submit(-name=>'submit',value=>'Get Whitelisted');
	print $cgi->end_form;
}

###########################################################################
### PrintHTMLHeader();
###########################################################################
### Prints out all the necessary HTML/HTTP headerrs and whatnot
### to get the page happy
###########################################################################
sub PrintHTMLHeader() {
print $cgi->header;
print <<EOF ;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Whitelist Request</title>
<style type='text/css'>
th              {       background: #999999;}
td.a            {       background: #aaaaaa;}
td.b            {       background: #cccccc;}
td.menu         {       vertical-align: top;
                        text-align: center;
                        color: #ffffff;
                        background: #000066;}
A:link          {       color: red }
A:visited       {       color: maroon }
A:active        {       color: blue }
tt,pre		{       color: maroon;
			font-weight: bold;}

span.error      {       font-weight: bold;
                        color: #ff0000;
                        font-size: 14pt; }
</style>
</head>
<body>
<!--
<table cellpadding=0 cellspacing=0 border=0 width='80%'>
<tr>
	<td width='10%'>
		<img src='http://optimism.cc/~ryan/cgi-bin/wham/images/wham.gif' width=155 height=105>
	</td><td valign='bottom' align='left'>
		<h1>Whitelist Request</h1>
	</td>
</tr><tr>
	<td class='menu' width='100%' colspan=2>&nbsp;home</td></tr>
</tr><tr>
<td>
</table> -->
<br clear="all">

<center>
<!img src='images/wham.jpg'>
EOF
}
