#!/usr/bin/perl # use strict; use warnings; use File::Copy; my $choice = 999; # SOMETHING WAY OFF...JUST IN CASE # DEFINE THE FUNCTIONS #################################################################################################### sub getlists { # GRAB THE LATEST LISTS ON THE SERVER my @locknloadlist = (); open(LISTLISTS, "/usr/lib/mailman/bin/list_lists -b|") || die "Can't run program: $!\n"; while () { chomp($_); # THE NEWLINE AT THE END JACKS UP THE MAILMAN PYTHON TOOLS push(@locknloadlist,$_); } return @locknloadlist; } sub exportconfig { # GRAB THE CONFIG FROM THE LIST SPECIFIED (my $currentlist, my $dirname) = @_; system("/usr/lib/mailman/bin/config_list -o ./${dirname}/${currentlist}.config ${currentlist}"); print "\n\t$currentlist config saved...\n"; } sub exportmembers { # GRAB THE MEMBER ADDRESSES FROM THE LIST SPECIFIED (my $currentlist, my $dirname) = @_; # my $memberfilepath = $dirname . $currentlist . "\.emails"; my $memberlist = `/usr/lib/mailman/bin/list_members ${currentlist}`; open(MEMBERFILE, ">./${dirname}/${currentlist}.emails") || die "\n\tUnable to write member file: $!\n"; print MEMBERFILE $memberlist; close(MEMBERFILE); print "\n\t$currentlist member addresses saved...\n"; } sub exportmods { # GRAB THE ADMIN ADDRESSES FROM THE LIST SPECIFIED (my $currentlist, my $dirname) = @_; my $modlist = `/usr/lib/mailman/bin/list_owners $currentlist`; open(ADMINFILE, ">./${dirname}/${currentlist}.admins") || die "\n\tUnable to write admin file: $!\n"; print ADMINFILE $modlist; close(MEMBERFILE); print "\n\t$currentlist admin addresses saved...\n"; } sub importconfigs { # GRAB list, directory, config file & IMPORT (my $currentlist, my $dirname, my $configfile) = @_; system("/usr/lib/mailman/bin/config_list -i ${dirname}${configfile} $currentlist"); print "\n\t$currentlist config loaded...\n"; } sub importmembers { # GRAB list, directory, member list & IMPORT (my $currentlist, my $dirname, my $memberFile) = @_; system("/usr/lib/mailman/bin/add_members --regular-members-file=${dirname}${memberFile} --welcome-msg=n --admin-notify=n $currentlist"); print "\n\t$currentlist member addresses added...\n"; } sub importadmins { # GRAB list, directory, admin list & IMPORT (my $currentlist, my $dirname, my $adminFile) = @_; my @adminaddr = (); open(ADMINFILE, "<${dirname}${adminFile}") || print "\n\tCouldn't open $adminFile: $!\n"; while () { # GRAB THE ADDRESSES chomp($_); push(@adminaddr,$_); } close(ADMINFILE); open(NEWADMINFILE, ">${dirname}${currentlist}.tmp") || print "\n\tCouldn't create ${dirname}${currentlist}.tmp: $!\n"; foreach my $address (@adminaddr) { # BREAK ADDRESSES UP INTO "USERNAME[0] DOMAIN[1]" FOR NEXT STEP my @pieces = split('@', $address); print NEWADMINFILE "mlist.owner.append('$pieces[0] at $pieces[1]')" # FUNKY PYTHON INPUT } close(NEWADMINFILE); system("/usr/lib/mailman/bin/config_list -i ${dirname}${currentlist}.tmp $currentlist"); unlink("${dirname}${currentlist}.tmp"); # WE DONT NEED THIS ANYMORE print "\n\t$currentlist admin addresses added...\n"; } sub breakEmailAddress { # JUST IN CASE WE NEED IT my ($address) = shift(@_); my @components = split('@', $address); return @components; # @output = &breakEmailAddress('john@some.domain.com'); # print "Username is ", $output[0], "\nDomain is ", $output[1]; } #################################################################################################### # CLEAR THE SCREEN system $^O eq 'MSWin32' ? 'cls' : 'clear'; # READY STEADY GO! #################################################################################################### while ($choice != 0) { my @lists = getlists(); print "\n\n"; print "\tWhat would you like to do?:\n"; print "\t" . "-" x 50 . "\n"; print "\t 1) Export list(s) (config, members, mods)\n"; print "\t 2) Import list(s) (config, members, mods)\n"; print "\t 0) Exit\n" . "\n"; print "\t\>"; chomp($choice = <>); if ($choice == 0) { # JUST IN CASE ;-p print "Bye!\n\n"; exit; } if ($choice == 1) { # EXPORT print "\n\tGetting current lists...\n"; @lists = getlists(); print "\tEnter directory name to create and dump files into (eg BACKUP): "; chomp(my $exportdirname = <>); mkdir($exportdirname, 0777) || die "\n\tDIR make failed: $!\n"; foreach (@lists) { exportconfig($_, $exportdirname); exportmembers($_, $exportdirname); exportmods($_, $exportdirname); } } if ($choice == 2) { # IMPORT print "\n\tGetting current lists...\n"; @lists = getlists(); print "\tEnter the PATH (with trailing \'/\')where I can find exported files (eg ./backup/):\n\t\> "; chomp(my $importdirname = <>); # GET IMPORT FILE LOCATION opendir(IMPORTDIR, $importdirname); my @importfiles = readdir(IMPORTDIR); closedir(IMPORTDIR); my @listconfigs = (); my @listmembers = (); my @listadmins = (); foreach my $file (@importfiles) { if ($file =~ /config/) { push(@listconfigs,$file); print "\n\tFound $file...\n"; } elsif ($file =~ /emails/) { push(@listmembers,$file); print "\n\tFound $file...\n"; } elsif ($file =~ /admins/) { push(@listadmins,$file); print "\n\tFound $file...\n"; } else { print "\n\t$file is not a valid file to IMPORT.\n\n";} } foreach (@lists) { my $nextInLine = $_; foreach my $configFile (@listconfigs) { while ($configFile =~ /$nextInLine/) { importconfigs($nextInLine, $importdirname,$configFile); } } foreach my $memberFile (@listmembers) { while ($memberFile =~ /$nextInLine/) { importmembers($nextInLine, $importdirname, $memberFile); } } foreach my $adminFile (@listadmins) { while ($adminFile =~ /$nextInLine/) { importadmins($nextInLine, $importdirname, $_); } } } } } print "Bye!\n\n"; exit;