Here's the script any comments would be appreciated.

To get going just change

$imapserver = "YOUR IMAP SERVER";

and create a spam and ham directory from your home account (or change
the learn_mail calls to point to shared directories).

calling convention is 

./learn-spam.pl -uid="USERNAME" -pwd="PASSWORD"

Note that going to microsoft exchange, users can be specified in two
different ways

NT 4 Domain  : -uid="\"ASGARD/John Smith\"" 
2000 Domain  : e.g -uid="easysoft.com/johns"

There are a few useful examples that come with the MAIL::ImapClient perl
module (2.2.7) in its examples area.

Jason

<<<<< CUT HERE >>>>>

#!/usr/bin/perl -w
use strict;
use Mail::IMAPClient;
use Shell;
use Env qw(HOME);
use Getopt::Long;

use File::Temp qw/ tempfile tempdir /; 

my $imapserver = "YOUR IMAP SERVER";

# set to 1 to enable imapclient debugging
my $debug = 0; 

# set to 1 if running under cron (disables output)
my $cron = 0;

my $filename;
my $fh;

my %options = 
(
 uid => undef,
 pwd => undef
);

my $cmdsts = GetOptions ("uid=s" => \$options{uid}, "pwd=s" =>
\$options{pwd});

if (!$options {uid}) { die "[SPAMASSASSIN] uid not set
(-uid=username)\n"; }
if (!$options {pwd}) { die "[SPAMASSASSIN] pwd not set
(-pwd=password)\n"; }

my $uid = $options{uid};
my $pwd = $options{pwd};

# login to imap server
my $imap = Mail::IMAPClient->new (Server=>$imapserver, User=>$uid,
Password=>$pwd, Debug=>$debug)
        or die "Can't connect to [EMAIL PROTECTED]: $@ $\n";

if ($imap)
{
  my $count;

  # Deal with spam first
  learn_mail ($HOME."/spam/", ".spam", "spam", 0, "--spam --showdots");

  # Now deal with ham
  learn_mail ($HOME."/ham/", ".ham", "ham", 0, "--ham --showdots");

}
else
{
  die "[SPAMASSASSIN] Unable to logon to IMAP mail account!
$options{uid}\n";
}

exit;

#
# read and learn mail from imap server
# 
# arguments
#  $dir         directory to place retrieved messages in
#  $ext         file extension to use on retrieved messages
#  $folder      imap folder name on server
#  $shared      0 if imap folder is in users mailbox
#               1 if imap folder is in shared name space or
#  $sa_args     additional arguments to specify to sa-learn
#               (e.g. --spam or --ham)
#
sub learn_mail {
  my $dir = shift (@_);
  my $ext = shift (@_);
  my $folder = shift (@_);
  my $shared = shift (@_);
  my $sa_args = shift (@_);
 
  my $count = 0;

  # tidy up directory before run
  clear_directory ($dir, $ext);

  # read mail from server
  $count = read_mail ($dir, $ext, $folder, $shared);
  if ($count > 0) 
  { 
    # learn about mail
    sa_learn ($dir, $ext, $sa_args); 

    # tidy up files after sa-learn is called
    clear_directory ($dir, $ext);
  }
}


#
# reads mail from an imap folder and saves in a local directory
#
# arguments
#  $dir         directory to place retrieved messages in
#  $ext         file extension to use on retrieved messages
#  $folder      imap folder name on server
#  $shared      0 if imap folder is in users mailbox
#               1 if imap folder is in shared name space or
sub read_mail {
  my $dir = shift (@_);
  my $ext = shift (@_);
  my $folder = shift (@_);
  my $shared = shift (@_);
  my $count = 0;
  my $target = "";

  if ($shared)
  {
    # use a shared public folder instead
    my ($prefix, $sep) = @{$imap->namespace->[2][0]} 
       or die "Can't get shared folder namespace or seperator: [EMAIL PROTECTED]";

    $target = $prefix.
       ($prefix =~ /\Q$sep\E$/ || $folder =~ /^\Q$sep/ ? "" : $sep).
       $folder;
  }
  else { $target = $folder; }

  $imap->select ($target) or die "Cannot select $target: [EMAIL PROTECTED]";

  # If a shared public folder is required uncomment the following
  # lines and comment out the previous $imap->select line

  # read through all messages
  my @msgs = $imap->search("ALL");
  foreach my $msg (@msgs)
  {
    ($fh, $filename) = tempfile (SUFFIX => $ext, DIR => $dir);
    $imap->message_to_file ($fh, $msg);
    close $fh;
    $count++;
  }

  if ($cron == 0) { print "Retrieved $count messages from $target\n"; }

  return $count;
}

#
# Removes files in directory $dir with extension $ext
#
sub clear_directory{
  my $dir = shift (@_);
  my $ext = shift (@_);

  opendir (DIR, $dir) or die "Couldn't open dir: $dir\n";
  my @files = readdir (DIR);
  close (DIR);

  for (my $i = 0; $i <= $#files; $i++ ) 
  { 
    if ($files[$i] =~ /.*?$ext$/) { unlink ($dir.$files[$i]); }
  } 
}


#
# execute sa-learn command
#
sub sa_learn {
  my $dir = shift (@_);
  my $ext = shift (@_);
  my $type = shift (@_);
  my $learncmd = "/usr/local/bin/sa-learn ".$type." --dir ".$dir; 

  if ($cron == 0) { $learncmd .= " --showdots"; }
  else { $learncmd .= " > /dev/null 2>&1"; }

  #
  # Run sa-learn script on spam directory
  #
  my $sh = Shell->new;
  my @args = ($learncmd);

  system (@args) == 0 or die "system @args failed: $?";
}
<<<<< CUT HERE >>>>>



-----Original Message-----
From: Louis LeBlanc [mailto:[EMAIL PROTECTED]
Sent: 18 March 2003 17:39
To: [EMAIL PROTECTED]
Subject: Re: [SAtalk] using bayes learning from an imap folder


Yes, I remember now.  A perl script would be perfect.  Please post it,
and if I find anything to rework, I'll let you know what my changes
are.

Thanks!
Lou
On 03/18/03 05:19 PM, Jason Crummack sat at the `puter and typed:
> I've got a script which I use to do this (and did post this fact a
> couple of weeks ago on the list), It's a simple perl script that uses
> the perl Mail::IMAPClient to connect to a microsoft exchange server,
> pulls down the contents of a imap folder and then runs sa-learn on the
> resultant directory. I'll post it to the list if you want it (p.s my
> perl isn't that great so if anyone wants to rework anything please let
> me know).
> 
> Jason Crummack
> Easysoft Limited
> 
> 
> 
> -----Original Message-----
> From: Louis LeBlanc [mailto:[EMAIL PROTECTED]
> Sent: 17 March 2003 17:47
> To: SpamAssassin Talk
> Subject: [SAtalk] using bayes learning from an imap folder
> 
> 
> Hey folks.  I'm trying to get 2.5 tweaked out, and the only thing left
> to do is have it look at false negatives to learn them as spam.  I
> haven't had any false positives yet - never really had any with 2.44,
> either, but I have had a few false negatives.  I am running off an
> imap server, so I will need a way to have this done from there,
> preferably automatically.
> 
> Someone said he had a script that would pull an imap folder down to an
> mbox or mail directory and apply bayes learning to that, but I can't
> remember the message or sender.  Does anyone know of such a tool, or
> have any pointers where I could start looking for details on how to
> accomplish this?
> 
> TIA
> Lou
> -- 
> Louis LeBlanc               [EMAIL PROTECTED]
> Fully Funded Hobbyist, KeySlapper Extrordinaire :)
> http://www.keyslapper.org                     ԿԬ
> 
> Science and religion are in full accord but science and faith are in
> complete
> discord.
> 
> 
> -------------------------------------------------------
> This SF.net email is sponsored by:Crypto Challenge is now open! 
> Get cracking and register here for some mind boggling fun and 
> the chance of winning an Apple iPod:
> http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
> _______________________________________________
> Spamassassin-talk mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/spamassassin-talk
> 
> 
> 

-- 
Louis LeBlanc               [EMAIL PROTECTED]
Fully Funded Hobbyist, KeySlapper Extrordinaire :)
http://www.keyslapper.org                     ԿԬ

Confirmed bachelor:
  A man who goes through life without a hitch.


-------------------------------------------------------
This SF.net email is sponsored by: Does your code think in ink? 
You could win a Tablet PC. Get a free Tablet PC hat just for playing. 
What are you waiting for?
http://ads.sourceforge.net/cgi-bin/redirect.pl?micr5043en
_______________________________________________
Spamassassin-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/spamassassin-talk




-------------------------------------------------------
This SF.net email is sponsored by: Does your code think in ink? 
You could win a Tablet PC. Get a free Tablet PC hat just for playing. 
What are you waiting for?
http://ads.sourceforge.net/cgi-bin/redirect.pl?micr5043en
_______________________________________________
Spamassassin-talk mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/spamassassin-talk

Reply via email to