#!/usr/bin/perl
use lib '/home10/writch/cgi-bin/libs.pm';
#use libs;
$file="/home10/writch/.procmailrc-test";

$list=0;
$add=0;
$subtract=0;
@addlist;
@sublist;

foreach $a (@ARGV){
	if ($a =~ "^-" && ($a !~ "^(-a(s|t|f|)|-k|-l[fF]?|[-|/][h|H|\?])\$")){
		#must be an unknown option
		print STDERR "Usage: subproc [-a(f|t|s)] [-k] [-l] emailaddress(es) or subjects\n\n";
		exit;
	}
	if ($a =~ "^-[h|\?]\$"){
		print "\nspamproc help screen:\n";
		print "\nUsage: spamproc [-a(f|t|s)] [-k] [-l] emailaddress(es) or subjects\n\n";
		print "Spamproc is a .procmailrc processor which can add and subtract spamless\n";
		print "addresses and subjects for filtration.  \n\n";
		print "Switches\n\n";
		print "\t-af\tAdd From\n";
		print "\t-at\tAdd To\n";
		print "\t-as\tAdd Subject\n";
		print "\t-k\tKill Item (looks for exact match--takes grep)\n";
		print "\t-l\tList Items\n";
		print "\t-lf\tList Items (with identifiers)\n";
		print "\t-lF\tList All SPAMLESS configuration lines (whole)\n";
		print "\n";
		exit;
	}
	if ($a =~ "^-a"){ # add names
		$a=~ s/..//g;
		$add=(lc($a) =~ "t")?3:(lc($a)=~"s")?2:1;
		$subtract=0;
		next;
	}
	if ($a =~ "^-k"){ # subtract names
		$subtract=1;$add=0;
		next;
	}
	if($add==3){
		push @tolist,$a;
	}
	if($add==2){
		push @subjectlist,$a;
	}
	if($add==1){
		push @addlist,$a;
	}
	if($subtract==1){
		push @sublist,$a; 
	}
	if ($a =~ "^-l[fF]?\$"){
		$list=($a=~"^-l\$")?1:($a=~"^-lf\$")?2:3;
	}
}

open FILE, "< $file"  or die "Oops, can't open procmail file";
$doit=0;
$insert=0;
$after_insert=0;
while (<FILE>){
	
	if($_ =~ /^[\s]*$/ && $doit==1) {
		$doit=2;
	}
	if($_ =~ /^# spamless filter rules$/ ){
		$ready=1;
	}
 	if($doit==1){
		push @myfile,$_;
	} else { 
		push @wholefile,$_;
		if($doit == 0){
			$insert++;
		} else {
			$after_insert++;
		}
	}
	if($ready==1 && $_ =~ /^:0H$/){
		$doit=1;
	}
	if ($list >= 1 && $doit == 1 ){
		$a = $_;
                $a=~ s/\* \!\^([\w]+)://g;
		$save=$1;
                $a=~ s/\.\*//g;
                $a=~ s/\$//g;
                $a=~ s/^[\s]+//g;
                $a=~ s/(\(|\))/\\$1/g;
		if ( ($a !~ "^[\|] \/home") && ($a !~ "^:0H\$") ){
			if($list == 2) { $a = "$save: $a" ;}
			if($list == 3) { $a = $_; chomp($a); }
		}
		chomp($a);
		push @list, $a;
	}

}
$insert--;

foreach $a (sort {lc($a) cmp lc($b)} @list){
	if ( ($a !~ "^[\|] \/home") && ($a !~ "^:0H\$") ){
		print $a . "\n";
	}
}
if(@list){exit;}
foreach $b (@sublist){
	for ($i=0;$i<$#myfile;++$i){
		$a=$myfile[$i];
		$a=~ s/\* \!\^([\w]+)://g;
		$which=$1 . ": ";
		$a=~ s/\.\*//g;
		$a=~ s/\$//g;
		$a=~ s/^[\s]+//g;
		$a=~ s/(\(|\))/\\$1/g;
		if ($a =~ /^$b$/){
			print STDERR "-$which" . $a ;
			splice(@myfile,$i,1);
		}
	}
}

foreach $a (@tolist){
	print STDERR "+To: $a\n";	
	push @myfile, "* !^To:.*$a.*\n";
}

foreach $a (@subjectlist){
	print STDERR "+Subject: $a\n";
	push @myfile, "* !^Subject:.*$a.*\n";
}

foreach $a (@addlist){
	print STDERR "+From: $a\n";
	push @myfile, "* !^From:.*$a.*\n";
}

$i=0;
foreach $b (@wholefile) {
	print $b;
	if($i == $insert){
		foreach $a (sort {lc($a) cmp lc($b)} @myfile){
			if($a ne $lasta){
				print $a;
			}	
			$lasta=$a;
		}
	}
	$i++;
}
close STDERR;
