Don't know if this is of interest to others, but I've modified a script used 
for reporting spam to Spamassassins sa-learn. The script was originally done by 
Nick Burch for updating the Bayesian db by fetching mail for a public folders 
on MS Exchange servers over IMAP. (http://tirian.magd.ox.ac.uk/~nick/code/)

However, I could not find any similar tools for reporting mail from IMAP 
folders to the razor-report tool, so I just modified the existing script a bit 
to call the razor-report tool. I have not tested this extensively, but it seems 
to work ok.

Basically, what we do is that all users report spam not tagged by SA by moving 
the original email into this SPAM folder. Then our mail server runs the 
following script to fetch the emails from Public Folders and report them, in 
this case to razor-report.

This version contains a lot of the old SA code which is not needed...  the HAM 
folder setting is not used, but it could be used to call razor-revoke..

Any input appreciated...

Regards,
Thomas Nilsen

----------------------- START OF SCRIPT -------------------------

#!/usr/bin/perl

# Imap Interface to razor-report    v0.01
# -----------------------------     -----
#
# Connects to an imap server, and filters the messages from the INBOX
# and SpamTrap
#
#  usage:
#    razor-report.pl <-hamfolder HAM> <-spamfolder SPAM>
#
#  Other options:
#    -skips nnn         skips over the first nnn messages in the folder(s)
#    -deletespam        after learning from a spam message, delete it
#    -delete-spam       (as above)
#    -dangerous-delete-ham      after learning from a ham (real email),
#                               delete it. Most people don't want this...
#    -dangerous-delete-all      after learning from any message, delete
#                               it, spam or ham
#
# Uses Mail::IMAPClient and Razor Agent (razor-report)
#
#
#      Original code by Nick Burch <[EMAIL PROTECTED]>
#           25/06/2003
#      Adopted for Razor by Thomas Nilsen
#           24/01/2005

use Mail::IMAPClient;

# Define our server and credentials here
# * Really ought to able to have several accounts defined
#
#  ** fix me **    your details go below
my $username = 'user';
my $password = 'password';
my $server = 'imapserver';

# Define where to find messages
my $defspamfolder = 'Public Folders/Spam/SPAM-Test';
my $defhamfolder = 'Public Folders/Spam/NON_SPAM';
my $deletespam = 0;
my $deleteham = 0;
my $default = 1;

my $skips = 0;

my @spams;
my @hams;
while(my $arg = shift) {
   if($arg eq "-spamfolder") {
     my $spam = shift;
     push @spams,$spam;
     print "Using spam folder $spam\n";
     $default = 0;
   }
   if($arg eq "-hamfolder") {
     my $ham = shift;
     push @hams,$ham;
     print "Using normal (ham) folder $ham\n";
     $default = 0;
   }
   if($arg eq "-deletespam" || $arg eq "-deletespams" || $arg eq "-delete-spam" 
|| $arg eq "-delete-spams") {
     $deletespam = 1;
   }
   if($arg eq "-dangerous-delete-ham" || $arg eq "-dangerous-delete-hams") {
     $deleteham = 1;
   }
   if($arg eq "-dangerous-delete-all") {
     $deletespam = 1;
     $deleteham = 1;
   }
   if($arg eq "-skips" || $arg eq "-skip") {
     $skips = shift;
   }
   if($arg eq "-?" || $arg eq "-h") {
     print "Usage:\n";
     print "  imap-sa-learn.pl [-spamfolder f]* [-hamfolder f]*\n\n";
     print "with no argumnets, uses default folders\n";
     print "(a few other options exist, see the header of the program)\n";
     exit;
   }
}

if($default) {
   push @hams,$defhamfolder;
   push @spams,$defspamfolder;
}

my %folders;
$folders{'spam'} = [EMAIL PROTECTED];
#$folders{'ham'} = [EMAIL PROTECTED];


# Normal (1), Debugging (2), or silent(0)?

# Connect to the IMAP server in peek (i.e. don't set read flag) mode
my $imap = Mail::IMAPClient->new(Server   => $server,
                                 User     => $username,
                                 Password => $password,
                                 Peek     => 1);

foreach my $type(keys %folders) {
   foreach my $folder (@{$folders{$type}}) {
      print "\nLooking in $type folder $folder\n";

      # Pick the folder
      $imap->select($folder);

      # Enable peek mode
      $imap->Peek(1);

      # Fetch messages
      my @mails = ($imap->seen(),$imap->unseen);

      my $count = 0;

      foreach my $id (@mails) {
         $count++;
         if($count < $skips) { next; }

         print " Learning on $type message $id\n";
         my $mail = $imap->message_string($id);
         open SA, "| razor-report";
           print SA $mail;
         close SA;

         if($type eq "spam" && $deletespam) {
            # If you want to move the message rather than deleting it,
            # uncomment the line below, change the folder, but _don't_
            # remove the delete line!
            #$imap->append('TrashBin', $mail );

            print "Deleting Spam Message $id\n";
            $imap->delete_message($id);
         }
         if($type eq "ham" && $deleteham) {
            print "Deleting Ham (normal email) Message $id\n";
            $imap->delete_message($id);
         }
      }
      if($deleteham || $deletespam) {
         # Only expunge now, rather than on every message
         $imap->expunge();
      }
   }
}

$imap->close;
exit;

DISCLAIMER:
This message contains information that may be privileged or confidential and is 
the property of the Roxar Group. It is intended only for the person to whom it 
is addressed. If you are not the intended recipient, you are not authorised to 
read, print, retain, copy, disseminate, distribute, or use this message or any 
part thereof. If you receive this message in error, please notify the sender 
immediately and delete all copies of this message.


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
Razor-users mailing list
Razor-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/razor-users

Reply via email to