Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

No Format
#!/usr/bin/perl -I/usr/local/lib
#
#       run sa-learn on is_spam and not_spam to update spamassassin
#
#       brj     01/27/04

use strict;
use Cwd;
require "splitmail.pl";


my $spamfile    = "/var/mail/is_spam";
my $hamfile     = "/var/mail/not_spam";
my $tmpdir      = "/var/tmp/split";

#my $learn_spam = "sa-learn --spam -C /etc/mail/spamassassin --showdots --dir $tmpdir";
#my $learn_ham  = "sa-learn --ham -C /etc/mail/spamassassin --showdots --dir $tmpdir";

my $learn_spam  = "sa-learn --spam -C /etc/mail/spamassassin --dir $tmpdir";
my $learn_ham   = "sa-learn --ham -C /etc/mail/spamassassin --dir $tmpdir";

my $startdir = cwd();

sub init {
        if ( ! -d $tmpdir ) {
                mkdir $tmpdir;
        } else {
                if ( chdir($tmpdir) ) {
                        unlink <*>;
                }
                chdir($startdir);
        }
}

sub learn {
        my $infile  = shift;
        my $command = shift;
        if ( -r $infile ) {
                splitmail($infile,$tmpdir);
                system("$command");
                if ( chdir($tmpdir) ) {
                        unlink <*>;
                }
                chdir($startdir);
        }
}

sub cleanup {
        unlink $spamfile, $hamfile;
        rmdir $tmpdir;
}

init();
learn( $spamfile, $learn_spam );
learn( $hamfile,  $learn_ham  );
cleanup();

...

No Format
#!/usr/bin/perl
#
#       splits a file containing multiple messages into individual files
#
use strict;

sub splitmail {
        my $infile = shift;
        my $outdir = shift;
        my $count = 0;

        open(INFILE, "< $infile") or die "Can't open $infile: $!\n";

        while(<INFILE>) {
                /^From / and do {
                        close(OUTFILE) if $count;
                        open(OUTFILE, "> $outdir/$count") or die "Can't open $outdir/$count: $!\n";
                        $count++;
                };
                print OUTFILE $_;
        }
        close(OUTFILE);
}

1;

Alternately, you can use this wrapper for sa-learn and call it from a .qmail file for on-the-fly split-and-learn-via-forward.

/usr/bin/learn_spam:

No Format

#!/usr/bin/perl
#
#      run sa-learn on STDIN ... easy to use with .qmail files:
#
#  .qmail-spamtrap:  
#      | learn_spam --spam --username=alias | cat - > /dev/null
#  .qmail-qqqhamreport:
#      | learn_spam --ham --username=alias | cat - > /dev/null
#
#  3/16/2005 -- cgg007 at yahoo.com
#

use strict;

sub learn {
  my $message = shift;
  my $pipe = shift;
  open LEARN, $pipe;
  print LEARN $message;
  close LEARN;
}

my $learn_cmd =  "| bayes_fixup.pl | sa-learn " . join(" ",@ARGV);
my $count = 0;
my $message = '';

while (<STDIN>) {

  /^From/ and do {
    if ($count) {
      learn($message,$learn_cmd);
      $message = '';
    }
    $count++;
  };

  $message .= $_;

}

learn($message,$learn_cmd);

...