smeserver-unjunkmgr/root/usr/local/unjunkmgr/spamchanger.pl

128 lines
4.0 KiB
Perl
Executable File

#!/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=<unjunk 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 = <UNJUNK>;
# 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/<user>/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 "<HTML><HEAD><TITLE>UnJunk Manager</TITLE>\n";
printf
"<link rel=\"stylesheet\" type=\"text/css\" href=\"/unjunkmgr/unjunkmgr.css\">\n";
print "</HEAD>\n";
if ( not defined($user) or not defined($email) ) {
print "<H2>Incorrect UnJunk Link</H2><br>\n";
exit 0;
}
my $email_msg .= sprintf "<table border=\"1\" width=\"900\"><tr>";
$email_msg .= sprintf
'<td width="600" align="left" valign="top" bgcolor="#C0C0C0" colspan=3>';
$email_msg
.= sprintf
"<b><H2>The following email has been scheduled for UnJunk</b></H2></td></tr>",
$user;
$email_msg .= sprintf
"<td bgcolor=\"#C0C0C0\"><font size=\"2\" face=\"Verdana\">From</td><td bgcolor=\"#C0C0C0\"><font face=\"Verdana\" size=\"2\">Subject</td></tr>";
$email_msg
.= sprintf
"<td><font face=\"Verdana\" size=\"2\">%-30.30s</td><td><font face=\"Verdana\" size=\"2\">%-40.40s</td></tr>",
$from, $subject;
$email_msg .= sprintf "</table>";
printf $email_msg;
my $LogFile = "/tmp/unjunk.file";
if ( not open( WRITE, ">> $LogFile" ) ) {
printf('<H2>Unable to write to the UnJunk schedular file</b></H2>');
}
else {
printf WRITE "USER:%s:%s\n", $user, $email;
close(WRITE);
}
exit 0;