#!/usr/local/bin/perl

#$Author: sf@ced.berkeley.edu $
#$Date: 94/06/10 13:17:23 $
#$Log:	create_list,v $
# Revision 1.1  94/06/13  10:15:13  sf
# Initial revision
# 
# To create a mailing list.
#
# Files changed after running this program:
#   ~server/config
#   ~server/owners
#   /ced/custom/aliases/mailhost/aliases.request, server must have w permission
#
# Usage:
# create_list [-t mro>] [-f filename] <list-name> <owner's email> <passwd> <comment>
#
# Example:
# ./create_list test-list sf@ced.berkeley.edu secret "TEST LIST"
#
# -t mro: The type of the list. Optional. 
#		m: morderated -- Article approval needed,
#               r: restrictive -- Sub/unsubscription approval needed,
#               o: open -- all people can send messages(but only members can
#                            get the messages)
#              The default is unmoderated, "public" and "closed" list. 
#                         
# -f filename:  The name of a file containing initial subscription. Optional.
#
#               Format of members file:
#               Emailname Fullname 
#
#               Example:  sf@ced.berkeley.edu  Susan Feng 
#               	  carrier@ced.berkeley.edu Stephen Carrier 
#                 
# list-name:     Name of the list. e.g admin-help
# owner's email: owner's email address. e.g. sf@ced.berkeley.edu
# passwd:        Passwd for list owner to manage the list. 
# Comment:       A short description for the list, should be quoted.
#
# 

#
# THINGS YOU MAY NEED TO CHANGE !!!
#
$server_home='/usr/server';
$archive='/usr/server/archives/mailing-lists';
$aliases='/ced/custom/aliases/mailhost/aliases.request';
$config_tmp="config.$$";
$subscribe_tmp="subscribe.$$";
$domain="ced.berkeley.edu";
$listserv="listserv@$domain";

die "Usage: $0 [-t mro] [-f filename] <list-name> <owner's email> <passwd> <comment> \n" unless ($#ARGV >= 3);

#
# check sanity
#
chdir ($server_home) || die "Cant' go to server's home. $!\n";
open(ALIASES,">> $aliases") || die "Can't open $aliases. $!\n";
open(CONFIG,">> config") || die "Can't open config file. $!\n";
open(OWNER,">>owners") || die "Can't open owners file. $!\n";

#
# Check options
#
while ( $ARGV[0] =~ /^-/ ) {
    $_ = shift;
    if ( /^-t$/ ) { 
	$type=shift; 
        die "Wrong type: $type.\nUsage: $0 [-t mro] [-f filename] <list-name> <owner's email> <passwd> <comment> \n" unless ( $type =~ /r|m|o/ );
    } else { 
       if ( /^-f$/ ) {
           $filename=shift;
           die "No such a file or file is empty: $filename.\n" unless ( -s $filename );
       } else {
           die "I don't recognize this option: $_\n"; 
       }
    }
}

($list_name=shift) =~y/A-Z/a-z/; #list name in lowercase
($LIST_NAME=$list_name) =~ y/a-z/A-Z/; #list name in upercase

$list_owner=shift;
die "Wrong email format for owner.\nUsage: $0 [<-t mro<days>] <list-name> <owner's email> <passwd> <comment> \n" unless ( $list_owner =~ /@/ );

$passwd=shift;
$comment=shift;


die "$LIST_NAME already exists.\n" unless ! -d "lists/" . $LIST_NAME;

if ( $type =~ /o/ ) { 
    # Open list, anybody can send message to the list, use -s option for list
    print CONFIG "list $list_name $list_name@$domain $list_owner $passwd -s -e -m 25 -P\n";
} else {
    # Closed list. only members can send messages to the list.
    print CONFIG "list $list_name $list_name@$domain $list_owner $passwd  -e -m 25 -P\n";
}

print CONFIG "archive $list_name $archive/$list_name %y-%m listserver/$list_name -\n";
print CONFIG "default $list_name {\n mail = ack\n } \n";
print CONFIG "comment $list_name # $comment\n";
close CONFIG;
print OWNER "$list_owner $list_name CCALL\n";

if ( $type =~ /r/ ) {
 # restrictive list, use -a option for listserv
  open(CONFIG,"config");
  open(CONFIG_TMP,">>$config_tmp");
  while (<CONFIG>) {
   s/^server(\s+\S*)(.*$)/server\1 -a $LIST_NAME \2/;
   print CONFIG_TMP $_;
  } 
  system("cp config config.bak");
  rename ("$config_tmp","config") || die "Could not rename $config_tmp to config. $!\n";
}

#
# change aliase file
#
if ( $type =~ /m/ ) {
    # moderated list
    print ALIASES "$list_name: \"|/usr/server/catmail -L $LIST_NAME -m -f\"\n"; 
} else {
    print ALIASES "$list_name: \"|/usr/server/catmail -L $LIST_NAME  -f\"\n"; 
};

#
# Generate a mail message to list owner who will subscribe initial users
#
# 	if the members file is not empty
#       then
#            send a message to list-owner to tell him how to subscribe
#            these members
#       else
#            Just send a message to notify him list has been created.
#            Also send a help file to him.
#       fi
#
#
if ( $filename ne "" ) {
    open(MEMBERS, $filename) || die "Can' open subscribers. $!\n";
    open(SUB,">$subscribe_tmp") || die "Can' create subscribers. $!\n";
    print SUB "Please send the following message to $listserv\n to make initial subscription for members of the list.\n";
    print SUB "---------Cut From Here-------------\n";
    while (<MEMBERS>) {
       ($email, $fn1, $fn2, $fn3) = split;
        if ( $email =~ /@/ ) { 
           $fullname= $fn1 . " " . $fn2 . " " . $fn3;
           print SUB "system $list_name $passwd $email #sub $list_name $fullname\n";
        } else {
           print "Wrong email name $email. \n\n";
        } 
    }
    close SUB;
    if ( -s "$subscribe_tmp" ) {
      system("/usr/ucb/Mail -s 'New list has been created.'  $list_owner < $subscribe_tmp");
      unlink $subscribe_tmp;
    }
}
system("/usr/ucb/Mail -s 'New list $list_name has been created.'  $list_owner < help/general");

print "New list \"$list_name\" has been created.\n";
print "Please as root, run whatever needed to update sendmail aliases file and restart sendmail.\n";
print "Please as server, run /usr/server/start to restart serverd.\n";
