#!/usr/bin/perl
#
#
# filter.pl derived from the original .filter.pl but much modified to:-
#       Filter mailing list messages to appropriate mailboxes
#
#
#
# Notes:
#    Delivery is to maildir format mailboxes (path ends with /)
#
use Mail::Audit qw/KillDups PGP/;
use strict;
#
#
# File and directory names
#
my $log = ">>/home/isbd/tmp/filter.log";
my $LISTS = "/home/isbd/.mutt/lists";      
my $lipath = "/home/isbd/Mail/Li/";
my $inpath = "/home/isbd/Mail/In/";
my $inbox = "/home/isbd/Mail/In/inbox/";

my $i = 0;
my (@listalias, @list, @strip);
#
#
# read the mutt list aliases file and create an array of the aliases
#
open(LISTS, $LISTS) or die "Couldn't open " . $LISTS;

while (<LISTS>)      
{
    next if /^#/;         
    ($listalias[$i], $list[$i], $strip[$i]) = split(" ");
    $i++;
}
#
#
# Open log file for writing
#
open(LOGFILE, $log)    or die "Can't open logfile: $!";
print LOGFILE "default destination is: " . $inbox . "\n";
#
#
# set up to kill duplicate messages
#
$Mail::Audit::KillDups::dupfile = "/home/isbd/.msgid-cache";
# 
# 
# Get the mail message to process, the 'emergency' parameter is the
# delivery path used if a message isn't delivered normally.
#
my $mail = new Mail::Audit
    (nomime=>1,
    emergency=>"/home/isbd/emrgMail/",
    log=>"/home/isbd/tmp/mail.log");
# 
# 
# Fix PGP headers, this adds PGP headers for the MUA to use 
#
$mail->fix_pgp_headers;
# 
# 
# Put the junk/spam in its own folder, Spamassassin has already been
# run and adds the X-Spam-Flag header.
#
foreach ($mail->get("X-Spam-Flag"))
{
        chomp;
        $mail->accept($inpath . "junk/") if (/YES/);
        # $mail->ignore() if (/YES/);
}
# 
# 
# Filter out failed messages to ben@isbd.biz as spam as well
#
foreach ($mail->get("X-Failed-Recipients"))
{
    chomp;
    $mail->accept($inpath . "ben/") if (/ben\@isbd.biz/);
}
#
# 
# 
# Now send mailing list mail to list mailboxes, check for mailling list
# pattern (in @listalias) in both Cc: and To: headers.
# 
foreach ($mail->get("Cc:"))
{
        chomp;
        filterLists($_);
}
foreach ($mail->get("To:"))
{
        chomp;
        filterLists($_);
}
# 
#
# Then send everything that's left to the default mailbox
#
$mail->accept($inbox);

#
#
# subroutine to send list mail to appropriate mailbox
#
sub filterLists
{
    my $a;
    my $i = 0;
    #
    #
    # return straight away if the header is empty
    #
    return if (length(@_[0]) == 0);
    #
    #
    # check to see if any list is in the header
    #
    foreach $a (@listalias)
    {
        if (@_[0] =~ /\Q$a\E/i)
        {
            #
            #
            # list found, strip list name from Subject: line if necessary
            #
            my $hdr = $mail->get("Subject:");
            my $str = "\\[" . $strip[$i] . "\\] ";
            $hdr =~ s/$str//;
            $mail->replace_header("Subject:", $hdr);
            if ($strip[$i] eq "spam")
            #
            #
            # and put the mail into the list mailbox
            #
            {
                $mail->accept($inpath . $a . "/");
            }
            else
            {
                $mail->accept($lipath . $a . "/" );
            }
        }
        $i++;
    }
}
