I found this perl script on the net for using emails to add whitelisted or 
blacklisted email addresses. 

Has anyone implemented this successfully. The instructions provided by the 
author are minimal but I would like to implement it.

Any assistance would be appreciated.

- ken

[SCRIPT]

#!/usr/local/bin/perl
#
# xmsactl.pl v0.1
#
# XMail Filter that adds/removes config entries in SpamAssassin
# control files based on commands in emails sent to a dedicated
# user.
#
# Copyright (C) 2004-01 Hagen Mayer, http://www.hagen-mayer.de/
#
# 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA  02111-1307, USA
#

use Getopt::Std;
use vars qw($opt_f $opt_s $opt_g $opt_u $opt_x);

my $lcfname="/etc/mail/spamassassin/local.cf";  # -g on cmdline
my $upfname="";                    # -u on cmdline
my $xmfname="";                    # -f on cmdline
my $passcode="HMYXMSACTL";            # -p on cmdline
my $retcode=0;                    # -x on cmdline
my $whitelisted=0;
my $blacklisted=0;
my $splhead="";
my $msghead="";
my $msgbody="";
my @body=();
my $bl="";
my $b=0;
my $w=0;
my $pcode="";
my $cmd="";
my $addr="";


# short usage
sub usage() {
    print "Usage:\n";
    print "  xmsactl.pl -f xmspoolfile  # name of XMail spoolfile\n";
    print "             -g local.cf     # SpamAssassin local.cf file\n";
    print "                             # Defaults to 
/etc/mail/spamassassin/local.cf\n";
    print "             -u user_prefs   # usually ~/.spamassassin/user_prefs\n";
    print "                             # if omitted, user prefs are not 
used\n";
    print "             -p passcode     # used in email body to identify 
command\n";
    print "                             # Defaults to HMYXMSACTL\n";
    print "             -x retcode      # returncode to XMail.\n";
    print "                             # Defaults to 0\n";
    print "\n";
    print "Commands accepted:\n";
    print "  - passcode ADDBLFROM address\n";
    print "  - passcode ADDWLFROM address\n";
    print "  commands will modify local.cf or if given on cmdline 
user_prefs.\n";
    print "  commands will match against local.cf and if given against 
user_prefs.\n";
    print "\n";
    print "ToDo:\n";
    print " - cfglist of valid users/email addresses allowed to do changes\n";
    print " - read cfg files into arrays instead processing them for each 
command\n";
}


# convert file globbing expression of SpamAssassin
# to perl regex
sub fp2regex($) {
    my $val = shift;
    $val =~ s/^\s//g;
    $val =~ s/\s$//g;
    $val =~ s/\*/\.\*/g;
    $val =~ s/\?/\.\?/g;
    $val =~ s/\@/\\\@/g;
    $val =~ s/\./\\\./g;
    return $val;
}


# remove some whitespaces
sub cleankey($) {
    my $key = shift;
    $key =~ s/^\s//g;
    $key =~ s/\s$//g;
    return $key;
}


# check if email address is either
# blacklisted or whitelisted
# returns on very first match
sub chkbwlist($$$$) {
    my $addr = shift;
    my $fname = shift;
    my $bl = shift;
    my $wl = shift;
    my $fd;
    
    return ($bl, $wl) if $fname eq "";
    return ($bl, $wl) if $bl || $wl;
    
    open($fd, "<", $fname) or die "cannot open $fname\n";
    while (<$fd>) {
        if (/^whitelist_from\s/) {
            ($key, $val) = split /\s/, $_, 2;
            $val = fp2regex($val);
            #$key = cleankey($key);
            if ($addr =~ /$val/) {
                $wl++;
                close $fd;
                return $bl, $wl;
            }
        } elsif (/^blacklist_from\s/) {
            ($key, $val) = split /\s/, $_, 2;
            $val = fp2regex($val);
            #$key = cleankey($key);
            if ($addr =~ /$val/) {
                $bl++;
                close $fd;
                return $bl, $wl;
            }
        }
    }
    close $fd;
    return $bl, $wl;
}


# splits xmail spool file into spoolhead, mailhead and mailbody
sub splitsplfile($) {
    my ($fname)=...@_;
    my $fd;
    my $splhead="";
    my $mailhead="";
    my $mailbody="";
    my $t=0;
    my $s;
    
    open $fd, $fname or die "cannot open $fname\n";
    while (<$fd>) {
        #$splhead .= $_ if 1..m#^<<MAIL-DATA>>$#;
        #$mailhead .= $_ if m#^<<MAIL-DATA>>$#..m#^$#;
        #$mailbody .= $_ if m#^$#..eof;

        chomp ($s=$_);

        if ($s =~ /^\<\<MAIL\-DATA\>\>$/i && $t==0) {
            $t=1;
            $splhead.=$_;
            next;
        }
        
        if ($s =~ /^$/ && $t==1) {
            $t=2;
            $mailhead.=$_;
            next;
        }
        $splhead.=$_ if $t==0;
        $mailhead.=$_ if $t==1;
        $mailbody.=$_ if $t==2;
    }
    close $fd;
    return $splhead, $mailhead, $mailbody;
}


# adds new config entry to either local.cf
# or, if not empty, to user_pref
sub addcfg($$$$$) {
    my $key = shift;
    my $val = shift;
    my $lcf = shift;
    my $ucf = shift;
    my $who = shift;
    my $now = scalar localtime(time);
    my $fd;
    
    if ($ucf ne "") {
        open $fd, ">>", $ucf or die "cannot open  $ucf for writing!\n";
    } else {
        open $fd, ">>", $lcf or die "cannot open $lcf for writing!\n";
    }
    print $fd $key."\t".$val."\t".'# '.$who." via xmsactl on ".$now."\n";
    close $fd;
}


###
### main
###

getopt("f:g:u:p:");
die (usage()) if ($opt_f eq "");
$xmfname=$opt_f;

$lcfname=$opt_g if ($opt_g ne "");
$upfname=$opt_u if ($opt_u ne "");
$passcode=$opt_p if ($opt_p ne "");
$retcode=$opt_x if ($opt_x ne "");

($splhead, $msghead, $msgbody) = splitsplfile($xmfname);
@body = split /\n/xms, $msgbody;
foreach $bl (@body) {
    $bl=~s/\s\s/ /g;
    $bl=cleankey($bl);
    ($pcode, $cmd, $addr) = split / /, $bl;
    
    $pcode=cleankey($pcode);
    next if !($pcode =~ /$passcode/);

    $addr=cleankey($addr);
    next if !($addr =~ /^(.+)@(.+)$/);
    
    $cmd=cleankey($cmd);
    
    # the rest of this loop is designed to work
    # just with ADDBLFROM and ADDWLFROM.
    # For other commands, logic needs to be reworked!
    
    ($b, $w) = chkbwlist($addr, $lcfname, 0, 0);
    ($b, $w) = chkbwlist($addr, $upfname, $b, $w);
    next if $b || $w;
        
    addcfg("blacklist_from", $addr, $lcfname, $upfname, $origin) if $cmd =~ 
/ADDBLFROM/;
    addcfg("whitelist_from", $addr, $lcfname, $upfname, $origin) if $cmd =~ 
/ADDWLFROM/;
}

exit $retcode;
# EOF

[/SCRIPT]



      
_______________________________________________
xmail mailing list
xmail@xmailserver.org
http://xmailserver.org/mailman/listinfo/xmail

Reply via email to