#!/usr/bin/perl -w

##### Requires bogofilter 0.15.0 or higher #####

# bogoTrain.perl is a wrapper for bogofilter that is specific to MH
# mail.  It labels as spam all e-mails in $spamFolder and everything
# else as ham (non-spam).  The database is built in
# $tmpDataDir/.bogofilter.  Once the database is built,
# $dataDir/.bogofilter is removed and the new database is moved there.

# Written by Jason D. M. Rennie, August 2003

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see file 'COPYING'); if not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA.

use FileHandle;
use DirHandle;
use IPC::Open3;
use strict;

## Customize these variables to suit your environment ##
my $spamFolder = "spam";
my $mailDir = "Mail";
my $tmpDataDir = "/tmp"; # this directory must exist
my $dataDir = "~"; # this directory must exist

my $verbose = 1;
my $bogoBin = "bogofilter";
my $homeDir = $ENV{"HOME"};
$homeDir =~ s/\/$//;
my $mailPath = "$homeDir/$mailDir";

# miscellaneous variables
my $fh;
my $s;

# Check bogofilter version number
my $pid = open3(undef, \*RDRFH, undef, "$bogoBin -V");
$_ = <RDRFH>;
m/version\s+([^.]+)\.([^.]+)\.([^.]+)/;
print "bogofilter version $1.$2.$3\n";
die "This program requires bogofilter 0.15.1 or later\n"
    if ($1 == 0 and ($2 < 15 or ($2 == 15 and $3 == 0)));
while (<RDRFH>) {}
waitpid $pid, 0;

my $profile = "$homeDir/.mh_profile";
$fh = new FileHandle($profile) or die "open $profile: $!";
while (<$fh>) {
    if (m/^path:\s*(\S+)/i) {
	$mailPath = $1;
	$mailPath =~ s/\/$//;
	$mailPath = "$homeDir/$mailPath" if ($mailPath !~ m/^\//);
    }
}
undef $fh;

my $foldersFile = "$mailPath/.folders";

$s = "rm -rf $tmpDataDir/.bogofilter";
print $s, "\n";
system $s;

my @spam;
my @ham;
$fh = new FileHandle($foldersFile) or die "open $foldersFile: $!\n";
foreach my $folder (<$fh>) {
    chomp $folder;
    my $path = "$mailPath/$folder";
    next if (-f "$path/.skip_me");
    push @spam, $path if ($folder eq $spamFolder);
    push @ham, $path if ($folder ne $spamFolder);
}

$s = "$bogoBin -d $tmpDataDir/.bogofilter -s -vt -B ".join(" ", @spam);
print "$s\n";
system $s;

$s = "$bogoBin -d $tmpDataDir/.bogofilter -n -vt -B ".join(" ", @ham);
print "$s\n";
system $s;

system "rm -rf $dataDir/.bogofilter";
system "mv $tmpDataDir/.bogofilter $dataDir";

# ChangeLog
# 09-02-2003 version 0.2 released
# 09-02-2003 pass folder paths directly with -B option
# 09-02-2003 requires bogofilter 0.15.1 or higher
# 08-28-2003 version 0.1 released
