Moderators: ScalixSupport, admin
aniewels wrote:Would you be so kind? :)
Code: Select all
#!/bin/sh
for user in `/opt/scalix/bin/omshowu -m all -i`
do
date
echo "Running spamlearn for $user"
/usr/local/bin/spamlearner.pl localhost $user Spam INBOX
done 2>>/var/log/spamlearn.log 1>>/var/log/spamlearn.log
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
my $usage =
"ARGS must be :
\targv1 : imap host
\targv2 : imap user
\targv3 : spam mailbox on imap server
\targv4 : ham mailbox on imap server\n";
die($usage) if(@ARGV != 4);
my ($host,$user,$spam,$ham)=@ARGV;
my $imap = new Mail::IMAPClient( 'Server' => $host , 'User' => 'mboxadmin:adminuser:' . $user , 'Password' => 'password') or die "Unable to login to IMAP $@";
foreach my $folder ($imap->folders) {
$imap->select($folder) or next;
if ($folder eq $spam) {
#For spam fetch all messages because delete them each day
print "Processing $folder folder\n";
my @list = $imap->messages or next;
print "Found " . @list . " messages in the $folder folder\n";
foreach my $mess (@list){
open (MBOX_SPAM, "|spamassassin -d | sa-learn --spam") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822')) or die "Unable to fetch $@";
print MBOX_SPAM "$output[1]" if(defined($output[1]));
close (MBOX_SPAM);
}
### Remove seen spam messages, because we don't need them anymore
my $nrDeleted = $imap->delete_message( scalar($imap->seen) ) or warn "Could not delete_message: $@\n";
print "$nrDeleted messages deleted\n";
### Ok, the messages are deleted, but in fact they aren't (welcome to IMAP ;-))
### So, we should expunge the folder to actually delete the messages
$imap->expunge($folder) or die "Could not expunge: $@\n";
}
elsif ($folder eq $ham) {
print "Processing $folder folder\n";
#This process will affect the status of recent and unseen flags, so take copy before and restore afterwards
print "Checking for Unseen and Recent messages:\n";
my @recent = $imap->recent or warn "No recent msgs: $@\n";
my @unseen = $imap->unseen or warn "No unseen msgs: $@\n";
print "There are " . @recent . " recent messages, and " . @unseen . " unseen messages in the $folder folder\n";
print "The status of these messages will be retained.\n";
#For ham we only fetch a day's worth of messages otherwise we would be continually re-learning same messages
my $yesterday = time()-86400;
my @list = $imap->since($yesterday) or warn "search: No emails found since yesterday\n";
if ($@) {
warn "Error in search: $@\n";
}
print "Found " . @list . " messages in $folder folder\n";
foreach my $mess (@list){
open (MBOX_HAM,"|spamassassin -d | sa-learn --ham") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822')) or die "Unable to fetch $@";
print MBOX_HAM "$output[1]" if(defined($output[1]));
close (MBOX_HAM);
}
#Restore the Unseen and Recent flags after first checking whether there were any in that state
if (@unseen > 0 ) {
print "Restoring the Unseen status for " . @unseen . " messages in the $folder folder\n";
$imap->unset_flag("Seen",@unseen) or warn "Could not reset flag for Unseen messages: $@\n";
}
if (@recent > 0 ) {
print "Restoring the Recent status for " . @recent . " messages in the $folder folder\n";
$imap->set_flag("Recent",@recent) or warn "Could not set flag for Recent messages: $@\n";
}
}
}
$imap->disconnect() or die "Unable to disconnect\n";
#Run a final run with --sync option
system("sa-learn --sync") == 0 or die "Could not sync bayes database: $?\n";
print "Spamassassin learning of $ham and $spam folders for user $user finished\n";
print "\n";
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
my $usage =
"ARGS must be :
\targv1 : imap user (password will be prompted)\n";
die($usage) if(@ARGV != 1);
my ($user) = @ARGV;
my $host = "localhost";
my $adminuser = "sxadmin";
my $password = 'password';
my $spam = "Junk-E-Mail";
my $ham = "INBOX";
my $imap = new Mail::IMAPClient( 'Server' => $host , 'User' => "mboxadmin:$adminuser:$user" , 'Password' => $password ) or die "Unable to connect to imap server\n";
print "Scanning user $user\n";
foreach my $folder ($imap->folders) {
$imap->select($folder) or next;
if ($folder eq $spam) {
#For spam fetch all messages older than one week because we want to delete them each day
print "Processing $folder folder\n";
my $lastweek = time()-604800;
my @all = $imap->messages or next;
$imap->unset_flag("Seen",@all) or warn "Could not reset all messages to unseen: $@\n";
my @list = $imap->before($lastweek) or next;
print "Found " . @list . " messages in the $folder folder older than 1 week\n";
foreach my $mess (@list){
open (MBOX_SPAM, "|spamassassin -d | sa-learn --spam -u $user ") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822')) or die "Unable to fetch $@";
print MBOX_SPAM "$output[1]" if(defined($output[1]));
close (MBOX_SPAM);
}
### Remove seen (=checked) spam messages, because we don't need them anymore
my $nrDeleted = $imap->delete_message( scalar($imap->seen) ) or warn "Could not delete_message: $@\n";
print "$nrDeleted messages deleted\n";
### Ok, the messages are deleted, but in fact they aren't (welcome to IMAP ;-))
### So, we should expunge the folder to actually delete the messages
$imap->expunge($folder) or die "Could not expunge: $@\n";
}
elsif ($folder eq $ham) {
print "Processing $folder folder\n";
#For ham we only fetch a day's worth of messages otherwise we would be continually re-learning same messages
my $yesterday = time()-86400;
my @list = $imap->since($yesterday) or warn "search: No emails found since yesterday\n";
if ($@) {
warn "Error in search: $@\n";
}
print "Found " . @list . " messages in $folder folder\n";
foreach my $mess (@list){
open (MBOX_HAM,"|spamassassin -d | sa-learn --ham -u $user ") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822.PEEK')) or die "Unable to fetch $@";
print MBOX_HAM "$output[1]" if(defined($output[1]));
close (MBOX_HAM);
}
}
}
$imap->disconnect() or die "Unable to disconnect\n";
print "Spamassassin learning of imap folders $ham and $spam finished\n";
Code: Select all
#!/bin/sh
scalixusers=`/opt/scalix/bin/omsearch -e ' ! RESOURCE-TYPE=1' -s -m UL-AUTHID|awk -F "=" '{ print $2 }'|grep -v admin`;
for i in $scalixusers ; do
/usr/local/sbin/spamlearner.pl $i >> /var/log/spamlearner.log 2>> /var/log/spamlearner.log
sa-learn --sync -u $i 2> /dev/null
done
Code: Select all
No recent msgs:
search: No emails found since yesterday
Processing Spam folder
Processing Ham folder
Checking for Unseen and Recent messages:
There are 0 recent messages, and 38 unseen messages in the Ham folder
The status of these messages will be retained.
Found 0 messages in Ham folder
Restoring the Unseen status for 38 messages in the Ham folder
bayes: synced databases from journal in 0 seconds: 192 unique entries (1374 total entries)
Spamassassin learning of Ham and Spam folders for user mschmitt finished
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
my ($host,$user,$spam,$ham)=("localhost", "mschmitt", "Spam", "Ham");
my $imap = new Mail::IMAPClient( 'Server' => $host , 'User' => $user , 'Password' => 'PaSsWoRd') or die "Unable to login to IMAP $@";
foreach my $folder ($imap->folders) {
$imap->select($folder) or next;
if ($folder eq $spam) {
#For spam fetch all messages because delete them each day
print "Processing $folder folder\n";
my @list = $imap->messages or next;
print "Found " . @list . " messages in the $folder folder\n";
foreach my $mess (@list){
open (MBOX_SPAM, "|spamassassin -d | sa-learn --spam") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822')) or die "Unable to fetch $@";
print MBOX_SPAM "$output[1]" if(defined($output[1]));
close (MBOX_SPAM);
}
### Remove seen spam messages, because we don't need them anymore
my $nrDeleted = $imap->delete_message( scalar($imap->seen) ) or warn "Could not delete_message: $@\n";
print "$nrDeleted messages deleted\n";
### Ok, the messages are deleted, but in fact they aren't (welcome to IMAP ;-))
### So, we should expunge the folder to actually delete the messages
$imap->expunge($folder) or die "Could not expunge: $@\n";
}
elsif ($folder eq $ham) {
print "Processing $folder folder\n";
#This process will affect the status of recent and unseen flags, so take copy before and restore afterwards
print "Checking for Unseen and Recent messages:\n";
my @recent = $imap->recent or warn "No recent msgs: $@\n";
my @unseen = $imap->unseen or warn "No unseen msgs: $@\n";
print "There are " . @recent . " recent messages, and " . @unseen . " unseen messages in the $folder folder\n";
print "The status of these messages will be retained.\n";
#For ham we only fetch a day's worth of messages otherwise we would be continually re-learning same messages
my $yesterday = time()-86400;
my @list = $imap->since($yesterday) or warn "search: No emails found since yesterday\n";
if ($@) {
warn "Error in search: $@\n";
}
print "Found " . @list . " messages in $folder folder\n";
foreach my $mess (@list){
open (MBOX_HAM,"|spamassassin -d | sa-learn --ham") or die "Can't open pipe: $!";
my @output = $imap->fetch(($mess,'RFC822')) or die "Unable to fetch $@";
print MBOX_HAM "$output[1]" if(defined($output[1]));
close (MBOX_HAM);
}
#Restore the Unseen and Recent flags after first checking whether there were any in that state
if (@unseen > 0 ) {
print "Restoring the Unseen status for " . @unseen . " messages in the $folder folder\n";
$imap->unset_flag("Seen",@unseen) or warn "Could not reset flag for Unseen messages: $@\n";
}
if (@recent > 0 ) {
print "Restoring the Recent status for " . @recent . " messages in the $folder folder\n";
$imap->set_flag("Recent",@recent) or warn "Could not set flag for Recent messages: $@\n";
}
}
}
$imap->disconnect() or die "Unable to disconnect\n";
#Run a final run with --sync option
system("sa-learn --sync") == 0 or die "Could not sync bayes database: $?\n";
print "Spamassassin learning of $ham and $spam folders for user $user finished\n";
print "\n";
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use Mail::IMAPClient;
my ( $host, $user, $spam, $ham ) = ( "localhost", "mschmitt", "Spam", "Ham" );
my $imap = new Mail::IMAPClient(
'Server' => $host,
'User' => $user,
'Password' => 'PaSsWoRd'
) or die "Unable to login to IMAP $@";
foreach my $folder ( $imap->folders ) {
# Only the $spam and $ham folder are of interest
next unless ( $folder eq $spam || $folder eq $ham);
$imap->select($folder) or next;
# Set the mode for sa-learn
# --spam for the $spam folder, --ham for the $ham folder
my $mode = ( $folder eq $spam ) ? '--spam' : '--ham';
print "Processing $folder folder\n";
my @list = $imap->messages or next;
print "Found " . @list . " messages in the $folder folder\n";
# Process each message in folder and send it to sa-learn
foreach my $mess (@list) {
open( MBOX_SPAM, "|spamassassin -d | sa-learn $mode" )
or die "Can't open pipe: $!";
my @output = $imap->fetch( ( $mess, 'RFC822' ) )
or die "Unable to fetch $@";
print MBOX_SPAM "$output[1]"
if ( defined( $output[1] ) );
close(MBOX_SPAM);
}
# As spam folder is no longer needed and the ham folder only contains copies
# delete every mail processed
my $nrDeleted = $imap->delete_message( scalar( $imap->seen ) )
or warn "Could not delete_message: $@\n";
print "$nrDeleted messages deleted\n";
### Ok, the messages are deleted, but in fact they aren't (welcome to IMAP ;-))
### So, we should expunge the folder to actually delete the messages
$imap->expunge($folder) or die "Could not expunge: $@\n";
}
$imap->disconnect() or die "Unable to disconnect\n";
#Run a final run with --sync option
system("sa-learn --sync") == 0 or die "Could not sync bayes database: $?\n";
print
"Spamassassin learning of $ham and $spam folders for user $user finished\n";
print "\n";
Users browsing this forum: No registered users and 7 guests