#!/usr/local/bin/perl
$ControlAddress = "listproc\@lists.acusd.edu";
$Testing = "Off";
$MaxDisplay = 4;
$SearchPage = $ENV{'HTTP_REFERER'};
$TitleAlign = "center";
#if you need to specify a "base" directory for your 'ListBase's
$HTMLBase = "";
#Send the HTTP Headers
print "Content-type: text/html\n\n";
#Get the form data
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
#Get the values for each form item
@pairs = split(/&/, $buffer);
#Get any URL data
if ($ENV{'QUERY_STRING'}) {
@pairs = (split(/&/, $ENV{'QUERY_STRING'}),@pairs);
}
#Get any command-line data
if (@ARGV) {
@pairs = (@ARGV,@pairs);
}
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
#De-Htmlify %symbols and spaces
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
if ($name =~ /owner/i) {
$Owner = $value;
} elsif ($name =~ /OwnerName/i) {
$OwnerName = $value;
} elsif ($name =~ /HomePage/i) {
$HomePage = $value;
} elsif ($name =~ /SearchPage/i) {
$SearchPage = $value;
} elsif ($name =~ /ListBase/i) {
if ($value =~ /\/$/) {
chop($value);
}
if ($value =~ /^(\~[^\/]*)(.*)$/) {
$BasePath = glob($1 . ($PublicHTML ne "off" ? "/public_html" : "") . $2);
} else {
$BasePath = glob($value);
}
print "Setting Base path to $BasePath \n" if &Testing;
@ListBases[$#ListBases+1] = $BasePath;
$URLs{$BasePath} = $value;
} elsif ($name =~ /ListAddress/i) {
$ListAddress = $value;
if ($value =~ /^([^@]*)@.*$/) {
$ListName = uc($1) if ! $ListName;
}
} elsif ($name =~ /ListName/i) {
$ListName = $value;
} elsif ($name =~ /ListDescription/i) {
$ListDescription = $value;
} elsif ($name =~ /ControlAddress/i) {
$ControlAddress = $value;
} elsif ($name =~ /MaxDisplay/i) {
$MaxDisplay = $value;
} elsif ($name =~ /Term/i) {
$SearchTerm = $value;
} elsif ($name =~ /TitleAlignment/i) {
$TitleAlign = $value;
} elsif ($name =~ /publichtml/i) {
$PublicHTML=$value;
} elsif ($name =~ /Testing/i) {
$Testing = $value;
} else {
$FORM{$name} = $value;
}
#While testing, print the value of the variables as we get them.
print "Setting $name to $value \n" if &Testing;
}
#Send the HTML Headers
print "
";
print "" . ($ListName ? $ListName : "Mailing List") . " Search Results ";
print "\n";
#Send the HTML Body
print "\n";
print "$ListName \n" if $ListName;
print "$ListDescription \n" if $ListDescription;
print "Search Results \n";
#Make sure that the important entries are there
&blank_entry("mailing list folder to search") unless @ListBases;
&blank_entry("word to search for") unless $SearchTerm;
study $SearchTerm;
#Send the results to the reader
foreach $ListBase (@ListBases) {
chdir $HTMLBase . $ListBase;
print "Searching $ListBase \n" if &Testing;
#get the directory listing
opendir THEDIR,".";
@FileListing = readdir(THEDIR);
closedir THEDIR;
foreach $THEFILE (@FileListing) {
next if $THEFILE !~ /^msg.*\.html$/;
print "Looking in file $THEFILE \n" if &Testing;
open THEFILE;
while () {
last if /\<\!--X-MsgBody--\>/;
if (/\(.*)\<\/TITLE\>/i) { $Subject = $1;}
if (/\ /i) { $Poster = $1;}
}
@Lines = grep(/$SearchTerm/i,);
$HitCount = $#Lines +1;
close THEFILE;
if ($HitCount) {
@Lines = &StripHTML(@Lines);
$HitList{$THEFILE} = $HitCount;
$PosterList{$THEFILE} = $Poster;
$SubjectList{$THEFILE} = $Subject;
$Max = $MaxDisplay < $#Lines ? $MaxDisplay-1 : $#Lines;
$LineList{$THEFILE} = join("",@Lines[0..$Max]);
}
undef $HitCount;
undef $Poster;
undef $Subject;
undef @Lines;
}
@TheHits = sort Compare_Hit_Count keys(%HitList);
if ($#TheHits >= 0) {
$HitCount = $#TheHits+1;
print "We found $HitCount post", $HitCount > 1 ? "s" : "";
print " in ", ($ListAddress ? "$ListName " : $ListName) if $ListName;
print " matching $SearchTerm .
\n";
foreach $TheHit (@TheHits) {
print "$SubjectList{$TheHit} ";
print "$HitList{$TheHit} mention", $HitList{$TheHit} > 1 ? "s" : "", " in the text, by $PosterList{$TheHit} \n";
print "$LineList{$TheHit} \n";
}
print " \n";
} else {
print "No matches for $SearchTerm in $ListAddress .\n";
}
undef %HitList;
undef %PosterList;
undef %SubjectList;
undef %LineList;
undef @TheHits;
undef $HitCount;
}
#Send the End
&send_end;
#Additional subroutines
sub blank_entry
{
print "You forgot something truly important! ";
print "\nYou forgot to enter your @_. Please go back and try again.\n";
&send_end;
exit;
}
sub send_end
{
print " ";
print "If you have further comments, please contact ", $OwnerName ? $OwnerName : $Owner, " ." if $Owner;
print " ";
print "";
}
sub Compare_Hit_Count {
$HitList{$b} <=> $HitList{$a};
}
sub StripHTML {
for (@_) {
$_ =~ s/<[^>]*>//g;
}
return @_;
}
sub Testing {
return ($Testing =~ /On/i);
}