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

###########################################################################
### $Id: //depot/personal/ryan/main/src/wham/cgi-bin/white.cgi#2 $
###########################################################################
###
###########################################################################

use FindBin '$RealBin';                             # locate libraries
BEGIN { unshift(@INC,"$RealBin/../lib", "/usr/local/wham/lib/") }

use strict;
use CGI;
use wham;

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

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

%config=LoadConfig();

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;}
</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

if( defined( $cgi->param('address')))
{
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='b'>\n";
	if( $cgi->param('address') !~ /(\S+\@\S+)/)
	{
		print "<font color='#ff0000' size=+1><b>You do not appear to have entered a valid email address: <tt>", $cgi->param('address'),"</tt></b></font>\n";
#		&PrintForm;
	} else {
		if( ($err = &Whitelist( $1)) eq "")
		{
			print "Your address <tt>" ,$cgi->param('address'), "</tt> has been added to the whitelist.<P>Any mail from this address has been released.\n";
print "</td></tr>\n";
print "</table>\n";
print "</td></tr></table>\n";
			exit;
		} else {
			print "<font color='#ff0000' size=+1><b>Unable to add your address: <tt>", $cgi->param('address'),"</tt> to the whitelist<p><pre>$err</pre></b></font>\n";
		}

	}
print "</td></tr>\n";
print "</table>\n";
print "</td></tr></table>\n";
} else {

}
	&PrintForm();

###########################################################################
### &Whitelist($addr);
###########################################################################
### add user to the whitelist
###########################################################################
sub Whitelist
{
	my( $addr) = @_;
	my( @out, $cmd, $line, $status);

	$cmd = "/home/ryan/src/wham/bin/add_white -m 'added via white.cgi' -a '$addr'";

	open( CMD, "$cmd 2>&1 |") || return "ERROR: could not execute '$cmd'\n";
#print "---\n";
	while( defined( $line = <CMD>))
	{
#		print "==$line<br>\n";
		push( @out, $line);
	}
#print "---\n";
	close( CMD);
	$status = ($? >> 8);

	if( $status != 0) {
		$line =join( "", @out);
		return "Exit Code:$status\n$line";
	}

	### 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.$$

EOF

	### Add in a section to delete specified message ids
	if( defined( $cgi->param('delete')))
	{
		print PROCRC ":0\n";
		foreach $line ($cgi->param('delete'))
		{
			print "Deleting: $line\n";
			print PROCRC "* 1^0 ^Message-ID: $line\n";
		}
		print PROCRC "/dev/null\n";

	}

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

} else

### Default will likely only work for me

print PROCRC <<EOF; 
### check for spam
:0:
* ? /home/ryan/src/wham/bin/iswhite
$config{spam}

### If it's not spam, and it's not trash, release it
:0:
$config{release}
EOF
}
	close( PROCRC);
exit;
	rename( $config{spam}, $config{spam} . ".tmp");

	$cmd = "/usr/bin/formail -s /usr/bin/procmail /tmp/procmailrc.wham.$$ < $config{spam}.tmp";
#	print $cmd;
	open( CMD, "$cmd|") || die "ERROR: could not execute '$cmd' : $!\n";
	while( defined( $line = <CMD>))
	{
		print "$line";
	}
	close( CMD);
	die "ERROR: '$cmd' exited with : $?\n" if( $? != 0);

#	rename( $config{spam} . ".tmp", $config{spam});
	unlink( $config{spam} . ".tmp");
#	unlink( "/tmp/procmailrc.wham.$$");
#	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;
}
