#!/usr/local/bin/perl -w


###
#
# bandito - a tool for stealing mailman configurations
#
# usage : bandito <path to a mailman list config db>
#
# (c)opyright 2003 - the siesta dev team
#
###

use strict;
use Siesta;
use Siesta::List;
use Siesta::User;
use Python::Serialise::Marshal;
use Data::Dumper;
use POSIX qw/strftime/;

## TODO
# move archives across
# no mail
# sig munging
# allowed posters


my $file = shift || die "You must pass a mailman config.db\n";
my $pr   = Python::Serialise::Marshal->new($file) || die "Couldn't open $file\n";


my $data = $pr->load();


(my $list_name   = $data->{private_archive_file_dir}) =~ s!.+/([^/]+).mbox$!$1!;
my $owner        =  Siesta::User->find_or_create({ email => $data->{owner}->[0] });
my $post_address = $list_name.'@'.$data->{host_name};
my $return_path  = $list_name.'-bounce@'.$data->{host_name};

print "Creating a new list '$list_name' ...\n";
print "\towner is '$owner'\n";
print "\tpost address is '$post_address'\n";
print "\treturn path is '$return_path'\n";
print "\n\n";

# create the new list
my $list = Siesta::List->new (
    name  => $list_name,
    owner => $owner,
    post_address => $post_address,
    return_path  => $return_path, 
) or die "Failed to create a new list\n";


# make the new users
print "Adding new users : \n";
foreach my $email (keys %{$data->{passwords}}) {
    print "$email ";
    $list->add_member( Siesta::User->find_or_create({ email => $email }) );    
}
print "\n\n";



print "Adding plugins : ";
my @plugins = qw(Debounce MembersOnly Moderated ListHeaders ReplyTo SubjectTag Send Archive);
print join ", ", @plugins;
print "\n\n";
$list->set_plugins( post => @plugins );

# now get them out again
my %plugins = map { $_->name => $_ } $list->plugins;

print "Setting options :\n";
# reply to 
my $reply_to      = $data->{'reply_goes_to_list'};
   $reply_to      = 0 unless defined $reply_to;
print "ReplyTo => $reply_to\n";
$plugins{'ReplyTo'}->pref('munge',$reply_to);


# members only
my $members_only  = $data->{'member_posting_only'};
   $members_only  = 0 unless defined $members_only;
print "MembersOnly => $members_only\n";
$plugins{'MembersOnly'}->pref('approve', $members_only);
$plugins{'MembersOnly'}->pref('tell_user', 1);

# set the subject line
my $subject_munge = $data->{'subject_prefix'};
   $subject_munge = "" unless defined $subject_munge;
print "SubjectTag => $subject_munge\n";

# whether or not it's moderated
my $moderated     = $data->{'moderated'};
   $moderated     = 0 unless defined $moderated;
print "Moderated => $moderated\n";
$plugins{'Moderated'}->pref('moderated', $moderated);
$plugins{'Moderated'}->pref('tell_user', 1);  



print "\n\nNow paste this into your aliases file :\n\n\n"; 
print $list->alias("bandito (the Siesta config stealing tool)");


