#!/usr/bin/perl
#
# written by oc2pus (c) 2004 GPL2
#
# Process mail from imap server shared folder 'SPAM' & 'NOSPAM' through
# spamassassin sa-learn
#
# Things to try if it doesn't work
# 1) Turn debug onto 1 and see if you connect to imap server ad get messages
# 2) Check your /etc/mail/spamassassin/local.cf for spamassassin bayes_path settings.
#
# changelog:
#
# 02.11.2004 deleteMsg flag inserted
# 28.10.2004 debug-total flag inserted
# 27.10.2004 --no-rebuild ==> --no-sync
# 27.10.2004 --rebuild ==> --sync
use Mail::IMAPClient;
print "========================\nmyIMAPClient.pl started\n";
my $debug=0;
my $debug_total=0;
my $deleteMsg=1;
my $salearn;
#####################
# login as user vscan
#####################
my $imap = Mail::IMAPClient->new( Server=> 'your-mail-server:143',
User => 'vscan',
Password => 'password-vscan',
Authmechanism => 'CRAM-MD5',
Debug => $debug);
if (!defined($imap)) {
die "IMAP Login Failed";
}
#############################################
# print out the total counts for each mailbox
#############################################
my $spamcount = $imap->message_count('user.SPAM');
print "-------\n", $spamcount, " SPAM to process\n";
my $nonspamcount = $imap->message_count('user.NOSPAM');
print $nonspamcount, " HAM to process\n-------\n";
##########################
# Process the spam mailbox
##########################
$imap->select('user.SPAM');
my @msgs = $imap->search("ALL");
for (my $i=0;$i <= $#msgs; $i++) {
$imap->message_to_file("/tmp/salearn",$msgs[$i]);
# execute sa-learn w/data
if ($debug) {
$salearn = `/usr/bin/sa-learn -D --no-sync --showdots --spam /tmp/salearn`;
}
else {
$salearn = `/usr/bin/sa-learn --no-sync --spam /tmp/salearn`;
}
print "-------\nSPAM: ", $salearn, "\n-------\n" if $debug;
# delete processed message
if ($deleteMsg) {
$imap->delete_message($msgs[$i]);
}
unlink("/tmp/salearn");
}
$imap->expunge();
$imap->close();
##############################
# Process the not-spam mailbox
##############################
$imap->select('user.NOSPAM');
my @msgs = $imap->search("ALL");
for (my $i=0;$i <= $#msgs; $i++) {
$imap->message_to_file("/tmp/salearn",$msgs[$i]);
# execute sa-learn w/data
if ($debug) {
$salearn = `/usr/bin/sa-learn -D --no-sync --showdots --ham /tmp/salearn`;
}
else {
$salearn = `/usr/bin/sa-learn --no-sync --ham /tmp/salearn`;
}
print "-------\nNOSPAM: ",$salearn,"\n-------\n" if $debug;
# delete processed message
$imap->delete_message($msgs[$i]);
unlink("/tmp/salearn");
}
$imap->expunge();
$imap->close();
###############
# close session
###############
$imap->logout();
#########################
# integrate learned stuff
#########################
if ($debug_total) {
my $sarebuild = `/usr/bin/sa-learn -D --sync`;
}
else {
my $sarebuild = `/usr/bin/sa-learn --sync`;
}
print "-------\nRebuild: ", $sarebuild, "\n-------\nmyIMAPClient.pl finished\n========================\n";