Hi, anithri.
Thanks for this script. It proved to be VERY useful to me and some co-workers. I took the freedom to adapt your script a little bit... Tested under Debian 'Woody' with Perl v5.6.1, IMAPClient v2.1.4, SpamAssassin v2.60 and Exchange 2000. Primary changes include message rewriting, commandline switches and a check whether the mail was checked earlier...
#!/usr/bin/perl -w
### Modules
use strict;
use Mail::IMAPClient;
use Mail::SpamAssassin;
use Proc::Daemon;
use Term::ReadKey;
use Getopt::Long;
### Variables
my ($spamtest, $imap, $status);
my ($flag, $password, $msg, $msgtxt);
my ($opt_user, $opt_host, $opt_help, $opt_interval);
my @messages;
### Initial values
my $inbox = "INBOX"; # Name of mailbox to check for SPAM in
my $junk = "INBOX/SPAM"; # Name of mailbox to move SPAM to
### Main program
print <<_EoT_;
IMAP interface to SpamAssassin V1.0
(c)2003 Alexander J. Trentini (thanks to anithri of perlmonks.org)
_EoT_
# Check commandline parameters
GetOptions("user=s" => \$opt_user, # IMAP user name
"host=s" => \$opt_host, # IMAP host name or IP add
+res
"interval=s" => \$opt_interval, # Check interval
"help" => \$opt_help); # Usage text
# Help text?
if (defined($opt_help)) {
print <<_EoT_;
Usage: $0 [--user --host --interval] [--help]
If no parameters are given on the commandline, the script enter
+s
interactive mode.
Parameters:
--help Prints this usage text
--user IMAP user name to use
--host IMAP host name or IP address to use
--interval Check interval
Note: The password will ALWAYS be read via interactive mode, du
+e to
security reasons!
_EoT_
exit(1);
}
# Interactive mode?
unless ($opt_host and $opt_user and $opt_interval) {
print("Interactive mode.\n\n");
print("Check interval (seconds): ");
$opt_interval = <>;
chomp($opt_interval);
print("IMAP host name or IP address: ");
$opt_host = <>;
chomp($opt_host);
print("IMAP user name: ");
$opt_user = <>;
chomp($opt_user);
}
# Get password (always interactive)
print "Password for $opt_user: ";
ReadMode(2);
$password = <>;
chomp($password);
ReadMode(0);
print("\n");
# Interval ok? #TODO
if ($opt_interval < 60) {
die("Are you crazy? I'm just a little CELERON (60s interval min.)!
+\n");
}
# Get rid of zombie problems
$SIG{CHLD} = 'IGNORE';
# Run as daemon
Proc::Daemon::Init;
# Initialize SpamAssassin
$spamtest = Mail::SpamAssassin->new();
# Loop endlessly
while (1) {
# Connect to IMAP server
$imap = Mail::IMAPClient->new(User => $opt_user,
Password => $password,
Server => $opt_host,
UID => 1,
Debug => 0,
Peek => 1) or die("Couldn't conn
+ect" .
" to $opt_host using $opt_user (Incorrect password?).\n");
$imap->select($inbox);
# Create $junk folder if not existant
unless ($imap->exists($junk)) {
$imap->create($junk);
}
# Omit already checked messages and messages within
# the IXOS mail archive (compressed, would result in
# garbage output) #TODO
@messages = $imap->search('NOT HEADER X-Spam-Checker-Version "Spam
+Assassin" NOT SUBJECT "IXOS"');
# Traverse messages
foreach $msg (@messages) {
# Only check if mail is smaller than 150KB (performance)
next if ($imap->size($msg) > 153600);
# Get message and pass to SpamAssassin
$msgtxt = $imap->message_string($msg);
$status = $spamtest->check_message_text($msgtxt);
$status->rewrite_mail();
$msgtxt = $status->get_full_message_as_text();
if ($status->is_spam()) {
$flag = $imap->append_string($junk, $msgtxt);
}
else {
$flag = $imap->append_string($inbox, $msgtxt);
}
# If successfully created, delete original from inbox
$imap->delete_message($msg) if ($flag);
}
# Close IMAP session
$imap->close();
sleep $opt_interval;
}
| [reply] [d/l] |
Hey -- thanks for this!
I hacked up a copy for a co-worker to work with modern Mail::SpamAssassin, require less non-core CPAN modules, track its state by adding a custom header to the processed messages, and generally do less stuff I wouldn't need:
http://taint.org/x/2009/reassassinate
| [reply] |