On Wed, Jul 21, 2004 at 12:07:34PM -0700, Justin Mason wrote:
> Johannes russek writes:
> > hi ye guys,
> > i'm sorry, i found DMZS-sa-learn right now :)
> > hm, but it calls sa-learn and therefore runs two instances of perl.
> > isnt that senseless?
> > what about making Mail::SpamAssassin::CmdLearn avaible as perl object?
> > (that requires only a small change in the class, so that the options will be
> > taken from a new constructor instead of GetOpt).
> > should i do that?
> > that way i could patch DMZS-sa-learn that it calls a
> > Mail::SpamAssassin::CmdLearn->new() construct instead of running sa-learn.
> 
> That certainly makes sense ;)
> 

Or just make calls directly into the API, that is what it is there for
afterall.

Something like this....

Michael

#!/usr/bin/perl -w

# Copyright 2004 Michael Parker
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

##
#
# Script: sa-learn-IMAP-spam.pl
# Description:
#  This script provides learning capabilities for IMAP mailboxes.  Specifically
#  this script learns spam from a given spam mailbox.  To use, update script
#  to point to common spam, processed spam and error IMAP mailboxes.  Then
#  invoke with the proper imapserver, username and password options.  You
#  can turn on debug to test.
#
# NOTE: This code requires the SA 3.0 API, the change to < 3.0 API is trivial,
#       but left as an exercise to the reader.
#
##

use strict;

use Getopt::Long;

use Mail::IMAPClient;
use Mail::SpamAssassin 3.0000;

my $debug = 0;

my %opt;

GetOptions("imapserver=s" => \$opt{imapserver},
           "username=s" => \$opt{username},
           "password=s" => \$opt{password},
           );


# Change these to something sane for your setup
my $spaminfolder = "INBOX.process.spam";
my $spamrptfolder = "INBOX.process.spam-reported";
my $spamerrfolder = "INBOX.process.spam-error";

my $imap = Mail::IMAPClient->new(
                                 Server         => $opt{imapserver}, 
                                 User           => $opt{username},
                                 Password       => $opt{password},
                                 Port           => "143",
                                 Peek           => "1",
                                 Debug          => $debug > 1,
                                 Uid            =>      '0', 
                                 Clear          =>      '5', 
                                 )
    || die ("Could not connect to server: $! $?\n");

my $spamass = Mail::SpamAssassin->new( { 'debug' => $debug } );
$spamass->init(1);

my $message_count = $imap->message_count($spaminfolder) || 0;

$imap->select($spaminfolder);

my @msgs = $imap->search("ALL");

my $learncount = 0;
my $errcount = 0;

foreach my $m (@msgs) {
    my $raw_message = $imap->message_string($m);
    $raw_message =~ s/\r\n/\n/g;
    my $mail = $spamass->parse($raw_message);

    my $status = $spamass->learn($mail, undef, 1);

    if ($status->did_learn()) {
        $imap->move($spamerrfolder,$m);
        $errcount++;
    }
    else {
        $imap->move($spamrptfolder,$m);
        $learncount++;
    }
}

$imap->expunge;

print "Processed $message_count Messages\n";
print "Learned: $learncount\tError: $errcount\n";

Reply via email to