#!/usr/bin/perl ############################################################################# # # This script provides a weekly overview of email stored in the junkmail # folder and allows for unjunking. When an email is being unjunked the # bayesian filter in SpamAssassin is trained as ham. # # This script has been developed # by Jesper Knudsen at http://sme.swerts-knudsen.dk # # Revision History: # # August 24, 2008: Initial version ############################################################################# use Cwd "realpath"; use File::Basename; use English; use strict; use CGI qw(:standard); use File::Copy; use File::Spec; ## If we are called as Perl Script then we are in "Move files" mode. The commandline then has the path ## to the unjunk file in the option file= if ( defined param('-file') ) { printf( "UnJunk File = %s\n", param('-file') ) if ( defined( param('-verbose') ) ); my $LogFile = param('-file'); if ( not open( UNJUNK, "+< $LogFile" ) ) { exit 0; } my @logfile = ; # Now truncate the file... truncate( UNJUNK, 0 ); close(UNJUNK); foreach my $email (@logfile) { my ( $user, $file ) = $email =~ m/^USER:([^:]+):(.*)$/; if ( defined($user) and defined($email) ) { printf( "UnJunking file %s for user %s\n", $file, $user ) if ( defined( param('-verbose') ) ); # Now Move the file to /home/e-smith/files/users//Maildir/cur my ( $name, $path, $suffix ) = File::Basename::fileparse( $file, '\..*' ); my $new_location = sprintf( "/home/e-smith/files/users/%s/Maildir/cur/%s%s", $user, $name, $suffix ); # If UnJunking a file lets learn as ham my $result = `su - root -c "/usr/bin/sa-learn --ham $file"`; if ( defined( param('-verbose') ) ) { printf( "Result of sa-learn: %s\n", $result ); } printf( "New location = %s\n", $new_location ) if ( defined( param('-verbose') ) ); if ( not rename( $file, $new_location ) ) { printf( "Move was not successfull : %s\n", $! ) if ( defined( param('-verbose') ) ); } } else { printf("Incorrect UnJunk file!!\n") if ( defined( param('-verbose') ) ); } } exit 0; } #################################################################### ## If we end here we are in the web mode and should output HTML data #################################################################### print header; #<-- prints the http header using the CGI module my $user = param('user'); my $from = param('from'); my $subject = param('subject'); my $email = param('email'); # Print header info print "UnJunk Manager\n"; printf "\n"; print "\n"; if ( not defined($user) or not defined($email) ) { print "

Incorrect UnJunk Link


\n"; exit 0; } my $email_msg .= sprintf ""; $email_msg .= sprintf '", $user; $email_msg .= sprintf ""; $email_msg .= sprintf "", $from, $subject; $email_msg .= sprintf "
'; $email_msg .= sprintf "

The following email has been scheduled for UnJunk

FromSubject
%-30.30s%-40.40s
"; printf $email_msg; my $LogFile = "/tmp/unjunk.file"; if ( not open( WRITE, ">> $LogFile" ) ) { printf('

Unable to write to the UnJunk schedular file

'); } else { printf WRITE "USER:%s:%s\n", $user, $email; close(WRITE); } exit 0;