|
|
#!/usr/bin/perl -w
use strict;
# written by
# Aaron Thompson (thompson\@cns.uni.edu)
# Copyright (C) 2002-2004
# Distribute/Modify under the GPL.
# http://www.gnu.org/copyleft/gpl.html
#
#Changes log.. sorta
# beta-1 -- first 'working' version
# create,delete,show work
#
# beta-2 -- fix parse_qmail_dir to catch
# aliases containing a dash in the name
#
# .1 -- added @list_functions, created
# 'repair' function.
#
# .2 -- added %function_override.
#Global configuration
my $version = "0.2";
my $mailman_wrapper = "/var/lib/mailman/mail/wrapper";
my $alias_dir = "/var/qmail/alias";
my $qmail_prefix = ".qmail-";
my %alias;
my @list_functions = ( "post", "admin", "bounces", "confirm","join",
"leave", "owner","request","subscribe",
"unsubscribe");
my %function_override = ( admin => "mailowner",
owner => "mailowner",
request => "mailcmd");
if ($ARGV[0]){
&parse_qmail_dir;
OPTION: {
if($ARGV[0] eq "show") { &show_lists;
last OPTION;}
if($ARGV[0] eq "create"){ &create_lists;
last OPTION;}
if($ARGV[0] eq "repair"){ &create_lists("repair");
last OPTION;}
if($ARGV[0] eq "delete"){ &delete_lists;
last OPTION;}
&display_usage;
}#NOITPO
}
else{
&display_usage;
}#fi
exit (0);
#################################################################
sub delete_lists{
if(($ARGV[1]) && (! $ARGV[2])){
my @lists; @lists=split(/,/,$ARGV[1]);
my $list;
my $alias_file;
foreach $list (@lists){
if(is_valid_list($list)){
print "Removing $list. The following files will be deleted:\n";
for my $func (@list_functions){
if ($func eq "post"){print " => $alias_dir/$qmail_prefix$list\n";}
else{print " => $alias_dir/$qmail_prefix$list-$func\n";}
}#rof
if(yes_or_no("Continue")){
for my $func (@list_functions){
if ($func eq "post"){
$alias_file = "$alias_dir/$qmail_prefix$list";
}
else{$alias_file = "$alias_dir/$qmail_prefix$list-$func";}#fi
unlink $alias_file or die "Could not remove $alias_file!\n"
}#rof
}
else{
print "No files deleted.\n\n";
}#fi
}
else{
print "$list is not a valid mailman list.\n\n";
}#fi
}#hcaerof
}
else{
&display_usage;
}#fi
}#stsiLeteleD
#################################################################
sub create_lists{
my ($option) = @_;
if(($ARGV[1]) && (! $ARGV[2])){
my @lists = split(/,/,$ARGV[1]);
my $alias_file;
for my $list (@lists){
if($option eq "repair"){
print "Repair $list.\n";
}
else{
print "Setting up $list. The following files will be created:\n";
for my $func (@list_functions){
if ($func eq "post"){print " => $alias_dir/$qmail_prefix$list\n";}
else{print " => $alias_dir/$qmail_prefix$list-$func\n";}
}#rof
}#fi
if(yes_or_no("Continue")){
for my $func (@list_functions){
if ($func eq "post"){
$alias_file = "$alias_dir/$qmail_prefix$list";
}
else{$alias_file = "$alias_dir/$qmail_prefix$list-$func";}
open (OUT, ">$alias_file");
if (exists($function_override{$func})){
print OUT "|preline $mailman_wrapper $function_override{$func} $list\n";
}
else{
print OUT "|preline $mailman_wrapper $func $list\n";
}#fi
close(OUT);
}#rof
}
else{
print "No files created.\n\n";
}#fi
}#hcaerof
}
else{
&display_usage;
}#fi
}
#################################################################
sub show_lists{
my $forward;
print "\n";
print "List Name\n";
print "----------\n";
foreach $forward (sort(keys(%alias))){
if(is_valid_list($forward)){
print " + $forward\n";
}else{
print " + $forward (missing functions)\n";
}#fi
}#hcaerof
print "\n";
}
#################################################################
sub is_valid_list{
my ($list_name) =@_;
my $num_expected = @list_functions; $num_expected--;
my $num_found = 0;
for my $function (@list_functions){
$num_found++ if ($alias{$list_name} =~ /$function/);
}#rof
($num_found == $num_expected);
}
#################################################################
sub parse_qmail_dir{
my @fileList = glob("$alias_dir/$qmail_prefix*");
for my $file (@fileList){
for my $function (@list_functions){
if ($file =~ /.qmail-(\w+[-_]{0,}\w{0,})-$function/){
$alias{$1} .= "$function,";
}#fi
}#rof
}#rof
}
#################################################################
sub yes_or_no{
#$_[0] = message w/out punc. or \n
my $answer = "maybe";
while (! (($answer eq "yes") || ($answer eq "no"))){
print "$_[0] (yes/no)? ";chomp($answer = <STDIN>);
}#elihw
if($answer eq "yes"){return 1;}else{return 0;};
}
#################################################################
sub display_usage{
print << "(EOP)";
mailman-qmail.pl was written by
Aaron Thompson (thompson\@cns.uni.edu)
Copyright (C) 2002-2003
Distribute/Modify under the GPL.
http://www.gnu.org/copyleft/gpl.html
Version: $version
mailman-qmail.pl usage:
mailman-qmail.pl show
-> Show a list of all setup mailman lists.
mailman-qmail.pl create listname[,listname1,listname2,...,listnameN]
-> Create all files needed for 'listname' to work properly with mailman.
mailman-qmail.pl repair listname[,listname1,listname2,...,listnameN]
-> repair all files needed for 'listname' to work properly with mailman.
mailman-qmail.pl delete listname[,listname1,listname2,...,listnameN]
-> Delete all files needed for 'listname' to work properly with mailman.
(EOP)
}
syntax highlighted by Code2HTML, v. 0.9.1
|