#!/usr/bin/perl
# Script to pull apart a majordomo .config file and find
# required information for a mailman list
# Written by Brad Marshall <b.marshall@cqu.edu.au>, Dec 2003

# List aliases extract with majordomo aliases
$aliases = "/path/to/alias";
$configdir = "/path/to/config/";
$outputdir = "/path/to/output/";
# Debug
$debug = 0;

# Pull listname from arguments
$listname = $ARGV[0];
$owneralias = "$listname" . "-owner:";
$listconfig = "$listname" . ".config";

open(CONF, ">$outputdir/$listname.config") or die "Can't open $listname.config: $!\n";

# Get listname-owner from aliases
	# set owner = ['email1','email2']
$ownertmp = `grep ^$owneralias $aliases`;
chomp($ownertmp);
($owner, $owneremail) = split(/\s*:\s*/, $ownertmp);

@owners = split(/\s*,\s*/, $owneremail);
print CONF "owner = ['";
$own = join("','", @owners);
print CONF $own, "']\n";

#foreach $own (@owners) {
#	print "Owner is $own\n" if $debug;
#}

# Set description
$description = &getconfig("description");
print "Description is -$description-\n" if $debug;
print CONF "description = '$description'\n";

# Set subject_prefix
$subjectprefix = &getconfig("subject_prefix");

if ($subjectprefix =~ /\$LIST/) {
	$subjectprefix =~ s/\$LIST/$listname/;
}

print "Subject prefix is -$subjectprefix-\n" if $debug;
print CONF "subject_prefix = '$subjectprefix '\n";
	
# If welcome = yes
	# if listname.info exists, use that
$welcome = &getconfig("welcome");
print "Welcome is -$welcome-\n" if $debug;

if ($welcome eq "yes") {
	# include welcome message
	print "Yes, sending welcome message..." if $debug;
	$listinfo = $listname . ".info";
	print CONF "send_welcome_msg = 1\n";
	if (-f $listinfo) {
		print "including .info\n" if $debug;
		open(INFO, "$listinfo") or die "Can't open $listinfo: $!\n";
		$info = "";
		while(<INFO>) {
			$info .= $_;
		}
		close(INFO);
		chomp($info);
		print CONF "welcome_msg = \"\"\"$info\"\"\"\n";
	} else {
		print "not including .info\n" if $debug;
	}
} else {
	print CONF "send_welcome_msg = 0\n";
}

# if subscribe_policy = open*: subscribe_policy = 1
# if subscribe_policy = closed: subscribe_policy = 2
# if subscribe_policy = closed+confirm: subscribe_policy = 3
$subscribepolicy = &getconfig("subscribe_policy");
print "Subscribe policy is -$subscribepolicy-\n" if $debug;
if ($subscribepolicy =~ /^open/) {
	print "subscribe_policy should be 1\n" if $debug;
	print CONF "subscribe_policy = 1\n";
} elsif ($subscribepolicy eq "closed") {
	print "subscribe_policy should be 2\n" if $debug;
	print CONF "subscribe_policy = 2\n";
} elsif ($subscribepolicy eq "closed+confirm") {
	print "subscribe_policy should be 3\n" if $debug;
	print CONF "subscribe_policy = 3\n";
}

# if unsubscribe_policy = open*: unsubscribe_policy = 0
# if unsubscribe_policy = closed*: unsubscribe_policy = 1
$unsubscribepolicy = &getconfig("unsubscribe_policy");
print "Unsubscribe policy is -$unsubscribepolicy-\n" if $debug;
if ($unsubscribepolicy =~ /^open/) {
	print "unsubscribe_policy should be 0\n" if $debug;
	print CONF "unsubscribe_policy = 0\n";
} elsif ($unsubscribepolicy =~ /^closed/) {
	print "unsubscribe_policy should be 1\n" if $debug;
	print CONF "unsubscribe_policy = 1\n";
}

# if moderate = yes: default_member_moderation = 1
# if moderator is filled out
	# if moderator value != owner, set moderator
$moderate = &getconfig("moderate");
print "Moderate is -$moderate-\n" if $debug;
$moderator = &getconfig("moderator");
print "Moderator is -$moderator-\n" if $debug;

if ($moderate eq "yes") {
	print "need default_member_moderation = 1\n" if $debug;
	print CONF "default_member_moderation = 1\n";
} else {
	print "need default_member_moderation = 0\n" if $debug;
	print CONF "default_member_moderation = 0\n";
}

if ($moderator eq "") {
	print "moderator empty\n" if $debug;
} elsif ($moderator eq $oweremail) {
	print "Moderator same as owner\n" if $debug;
} else {
	print "Need to set moderator in mailman\n" if $debug;
	print CONF "moderator = ['$moderator']\n";
}

# if restrict_post = listname, set generic_nonmember_action = 1 (hold)
# if restrict_post has listname.special-post
	# Add contents to accept_these_nonmembers
$restrictpost = &getconfig("restrict_post");
print "Restrict post is -$restrictpost-\n" if $debug;
@restrictent = split(/ /, $restrictpost);
foreach $ent (@restrictent) {
	if ($ent eq $listname) {
		print "Need to set generic_nonmember_action to 1\n" if $debug;
		print CONF "generic_nonmember_action = 1\n";
	}
	if ($ent eq "$listname.special-post") {
		print "Need to set accept_these_nonmembers\n" if $debug;
		open(SPECIAL,"$listname.special-post") or die "Can't open special post: $!\n";
		@specials = ();
		while(<SPECIAL>) {
			chomp;
			push(@specials, $_);
		}
		print CONF "accept_these_nonmembers = ['";
		$spec = join("','", @specials);
		print CONF $spec, "']\n";
	}
}

# if reply_to = $SENDER, reply_goes_to_list = 0
# if reply_to = listname, reply_goes_to_list = 1
# if reply_to has values, reply_goes_to_list = 2, reply_to_address = value
$replyto = &getconfig("reply_to");
print "Reply to is -$replyto-\n" if $debug;
if ($replyto eq "") {
	print "reply_goes_to_list = 0\n" if $debug;
	print CONF "reply_goes_to_list = 0\n";
} elsif ($replyto eq "\$SENDER") {
	print "reply_goes_to_list = 0\n" if $debug;
	print CONF "reply_goes_to_list = 0\n";
} elsif ($replyto =~ /^$listname/) {
	print "reply_goes_to_list = 1\n" if $debug;
	print CONF "reply_goes_to_list = 1\n";
} elsif (defined $replyto) {
	print "reply_goes_to_list = 2, reply_to_address = \$replyto\n" if $debug;
	print CONF "reply_goes_to_list = 2\n";
	print CONF "reply_to_address = $replyto\n";
}

# if who_access = open, private_roster = 0
# if who_access = list, private_roster = 1
# if who_access = closed, private_roster = 2
$whoaccess = &getconfig("who_access");
print "Who access is -$whoaccess-\n" if $debug;
if ($whoaccess =~ /^open/) {
	print "private_roster should be 0\n" if $debug;
	print CONF "private_roster = 0\n";
} elsif ($whoaccess =~ /^list/) {
	print "private_roster should be 1\n" if $debug;
	print CONF "private_roster = 1\n";
} elsif ($whoaccess =~ /^closed/) {
	print "private_roster should be 2\n" if $debug;
	print CONF "private_roster = 2\n";
}

# if maxlength exists, set max_message_size
$maxlength = &getconfig("maxlength");
print "maxlength is -$maxlength-\n" if $debug;
if ($maxlength ne "") {
	print "max_message_size should be $maxlength\n" if $debug;
	print CONF "max_message_size = $maxlength\n";
} else {
	print "max_message_size should be 0\n" if $debug;
	print CONF "max_message_size = 0\n";
}

# if message_footer, msg_footer
$footer = &getconfig("message_footer");
print "footer is -$footer-\n" if $debug;
if ($footer ne "") {
	print CONF "msg_footer = \"\"\"$footer\"\"\"\n";
} else {
	print CONF "msg_footer = ''\n";
}

# if message_fronter, set msg_header
$header = &getconfig("message_fronter");
print "header is -$header-\n" if $debug;
if ($header ne "") {
	print CONF "msg_header = \"\"\"$header\"\"\"\n";
} else {
	print CONF "msg_header = ''\n";
}
close(CONF);

# Get admin_passwd from listname.config, call change_pw
$password = &getconfig("admin_passwd");
print "admin password is -$password-\n" if $debug;
# now call change_pw
#print "system change_pw -q -l $listname -p $password\n";

# Create the lists
system("$mailmandir/newlist -q $listname $own $password");

# if listname-digest exists, add as digest members
$digestmembers = $listname . "-digest";
if (-e $digestmembers) {
	print "Added $digestmembers as digest members\n" if $debug;
	system("$mailmandir/add_members -d $configdir/$digestmembers $listname");
}

# add listname contents as regular members
if (-e $listname) {
	print "Adding regular members\n" if $debug;
	system("$mailmandir/add_members -r $configdir/$listname $listname");
}

#### SUBROUTINES ####
sub getconfig {
	my ($key) = @_;
	open(LISTCONFIG, "$configdir/$listconfig") or die "Can't open listconfig: $!\n";
	while(<LISTCONFIG>) {
		if ($key eq "message_footer") {
			if (/message_footer\s*<<\s*END/) {
				$footer = "";
				while(<LISTCONFIG>) {
					if (/^END/) {
						last;
					} else {
						$footer .= $_;
					}
				}
				chomp $footer;
				#print "-$footer-\n" if $debug;
				return $footer;
			}
		} elsif ($key eq "message_fronter") {
			if (/message_fronter\s*<<\s*END/) {
				$header = "";
				while(<LISTCONFIG>) {
					if (/^END/) {
						last;
					} else {
						$header .= $_;
					}
				}
				chomp $header;
				#print "-$header-\n" if $debug;
				return $header;
			}
		} elsif (/^$key\s*=\s*/) {
			($tmp,$value) = split(/\s*=\s*/);
			chomp($value);
			#print "value = $value\n" if $debug;
			close(LISTCONFIG);
			return $value;
		}
	}
}
