#!/usr/bin/perl # # jmlc - a junk mail logfile corrector # # a program to mark records in our junkmail logfile as being mis-filed. # # Input is one line of stdin from the "From" line of a mail message, e.g., # From frank@MyCause.com Fri Apr 4 10:55:23 2003 # # it will find the corresponding entry in our mail file and add # an entry to the end of it. Note that we use the "From" (not the "From:") # line because the time in the "Date:" line is relative to the sender and # might be in another time zone, off, etc. The "From" line is when the # message was delivered locally. # # logfile format is: timestamp status [invalid] # and changes it to timestamp status invalid # # where timestamp is # of seconds since the Epoch # status is 0 = good, -1 = junkmail # invalid is 1 = entry was mis-classified, 0 or empty is properly # classified # use Time::Local; $fudgefactor = 5; # matches must be within +/- 5 seconds $logfile = $ENV{'HOME'} . "/bin/junkmail.log"; %months=(Jan => 0, Feb => 1, Mar => 2, Apr => 3, May => 4, Jun => 5, Jul => 6, Aug => 7, Sep => 8, Oct => 9, Nov =>10, Dec =>11); $_ = <>; chop; ($j, $j, $j, $mon, $day, $time, $year) = split; ($h, $m, $s) = split(/:/, $time); #print "Date is: $months{$mon} / $day / $year, $h:$m:$s\n"; $time = timelocal($s, $m, $h, $day, $months{$mon}, $year); if ($time == -1) { print "Error: Illegal format\n"; exit -1; } print "epoch time is: $time\n"; $flag = 0; open (INLOG, "$logfile") || die "can't open $logfile for read"; open (OUTLOG, ">$logfile.out") || die "can't open $logfile.out for write"; while () { # the pattern is num nnum[ nnum] # where nnum is a potentially negative number /(\d+) (-?\d+) ?(\d+)?/; #print "$1 $2 $3\n"; if ($1 == $time) { $flag++; print OUTLOG "$1 $2 1\n"; #printf "Found a match with a %s entry", $2?"junk":"good"; #if ($3 ne "") { # print " but it was marked invalid." #} #print "\n"; } elsif ($1 >= ($time-$fudgefactor) && $1 <= $time+$fudgefactor) { print "Found a close match within $fudgefactor ($1), using it\n"; $flag++; print OUTLOG "$1 $2 1\n"; } else { # just echo out the record print OUTLOG; } } close INLOG; close OUTLOG; if ($flag) { # make the temp copy real if something changed unlink $logfile; system "mv $logfile.out $logfile"; } else { # just get rid of the temp copy if nothing changed unlink "$logfile.out"; } printf ("Updated %s %s. \n", $flag, ($flag==1)?"entry":"entries"); exit 0;