# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Rust_Admin.pm,v 1.2 1997/05/21 15:13:04 ruefenac Exp $
#
# This is part of RUST-1.0 Beta 6
#
# Abstract: This library contains library functions to administer
#           RUST queues and their config files
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with the source distribution of RUST, in the license
# directory. If you don't have a copy of it, write to: 
#
# Free Software Foundation, Inc.
# 675 Mass Ave
# Cambridge, MA 02139, USA.

{
package Rust_Admin;

use Rust_Configs;
use Rust_Debug;

require Exporter;
@ISA = qw(Exporter);
@EXPORT =
    (
     # name                       brief description
     # --------------------------------------------------------     
     'init_admin',		# Initialize this library
     'create_queue_files',	# Create files for a new queue
     'create_template_file',	# Create template file to add queues
     'generate_configs',	# Auto-generate Rust_Configs.pm
     'convert_names'		# Convert old name files to new format
     );

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub init_admin {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Initialize this library by adding the $global
    #           hash to this object.
    
    $globals = &Rust_Configs::get_globals;
    $new_configs = {};

}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub create_template_file {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Create a template datafile to use for
    #           creating new RUST queues.
    #
    #           This assumes that you are *NOT* running setgid,
    #           as it will give you an error message if you are.

   Rust_Debug::ddebug("Entering Rust_Create::make_template_file\n");

     my ($count) = @_;
     my ($index) = 1;

     unlink("create.datafile") if (-f "create.datafile");
     if (open(TEMPLATE,">create.datafile")) {
	  
	  print TEMPLATE ("# AUTO-GENERATED FILE FOR RUST QUEUE CREATION\n",
			  "#\n",
			  "# To add a queue to the Rust Mail System, fill out the\n",
			  "# information below.  Samples are provided.  Things you\n",
			  "# put into this file needs to be in the exact same format\n",
			  "# as the samples\n\n",
			  "# NUMBER is the number of queues you are adding to RUST using\n",
			  "# this datafile.  This is an integer, and it needs to be\n",
			  "# greater than 0.\n",
			  "#\n",
			  "# Example\n",
			  "#\n",
			  "#NUMBER=2\n\n",
			  "NUMBER=$count\n\n");
	  
	  while ($index <= $count) {
	      print TEMPLATE ("##########################################################\n",
			      "# Queue number $index information\n",
			      "#\n",
			      "# QUEUE_NAME is the name of the queue you are adding.\n",
			      "# If it is a mailing list (which it usually is), then\n",
			      "# it should be the name of the mailing list, minus the\n",
			      "# \@<machine_name>.\n",
			      "#\n",
			      "# Example\n",
			      "#\n",
			      "# QUEUE_NAME=problems\n\n",
			      "QUEUE_NAME=\n\n",
			      "# QUEUE_EMAIL_ADDR is the email address of the mailing alias\n",
			      "# of the queue you are adding to RUST.  This includes the\n",
			      "# \@<machine_name>.\n",
			      "#\n",
			      "# Example\n",
			      "# QUEUE_EMAIL_ADDR=problems\@cs.foo.edu\n\n",
			      "QUEUE_EMAIL_ADDR=\n\n",
			      "# QUEUE_TITLE is a \"title\" that would best fit as a title\n",
			      "# for the queue being added.\n",
			      "#\n",
			      "# Examples\n",
			      "# QUEUE_TITLE=System Administrators\n",
			      "# QUEUE_TITLE=System Operators\n",
			      "# QUEUE_TITLE=RUST bug mailing list\n\n",
			      "QUEUE_TITLE=\n\n",
			      "# QUEUE_DIST is a email alias which contains the humans\n",
			      "# who normally get mail being sent to the queue being added\n",
			      "# to RUST.  ITS IMPORTANT TO HAVE ONE FOR EACH QUEUE, EVEN\n",
			      "# IF YOU ARE NOT HAVING RUST FORWARD MAIL TO THE HUMANS.\n",
			      "# THERE ARE TIMES THAT RUST NEEDS THIS EMAIL ALIAS.\n",
			      "#\n",
			      "# Example\n",
			      "# QUEUE_DIST=problems-dist\@cs.foobar.edu\n\n",
			      "QUEUE_DIST=\n\n",
			      "# LOCAL_DOMAIN is a list of Internet domains that are\n",
			      "# considered local to the RUST queue.  This is currently\n",
			      "# not used, but will be in the future.  What it will be\n",
			      "# used for is to determine whether email that is being made\n",
			      "# into a new ticket was sent from the local domain or from\n",
			      "# a remote domain.  People on the local domain can run the\n",
			      "# provided user interfaces to RUST.  People on the remote\n",
			      "# domain have to use the RUST Mail Server to obtain\n",
			      "# information on tickets they have made.\n",
			      "#\n",
			      "# Example\n",
			      "# LOCAL_DOMAIN=cs.utah.edu,eng.utah.edu\n\n",
			      "LOCAL_DOMAIN=\n\n",
			      "# RESTRICT_TO contains a list of Internet domains that\n",
			      "# are allowed to create tickets via email.  If you don\'t\n",
			      "# want this queue to be restricted - ie if you want\n",
			      "# any Internet domain to be able to make requests via\n",
			      "# email, then LEAVE THIS LIST BLANK.\n",
			      "#\n",
			      "# This list is matched with the whole \@<machine_name>\n",
			      "# from which the email was sent from, so it can be as\n",
			      "# restricted as you want.\n",
			      "#\n",
			      "# Examples\n",
			      "#\n",
			      "# The following allows any .edu domain to create tickets\n",
			      "# via email\n",
			      "# RESTRICT_TO=edu\n",
			      "#\n",
			      "# The following only allows eng.utah.edu domain to create\n",
			      "# tickets via email\n",
			      "# RESTRICT_TO=eng.utah.edu\n\n",
			      "RESTRICT_TO=\n\n",
			      "# DO_INITIAL_AUTOREPLY is a bool value.  It is 1 if you\n",
			      "# want RUST to acknowledge the sender of email when the\n",
			      "# email is made into a new ticket.  It is 0 otherwise\n\n",
			      "DO_INITIAL_AUTOREPLY=0\n\n",
			      "# INCLUDE_ORIG is a bool value.  It is 1 if you have\n",
			      "# DO_INTITIAL_AUTOREPLY set to 1 and you want the original\n",
			      "# email included in the initial autoreply that is sent to\n",
			      "# users.  It is 0 otherwise.\n\n",
			      "INCLUDE_ORIG=0\n\n",
			      "# DO_ASSIGN_AUTOREPLY is set to 1 if you want the user\n",
			      "# of a ticket notified whenever the owner of the ticket\n",
			      "# changes.  This is done via a auto-generated email message.\n",
			      "# It is 0 otherwise.\n\n",
			      "DO_ASSIGN_AUTOREPLY=0\n\n",
			      "# SEND_TO_HUMANS is a bool value.  It is 1 if you want RUST\n",
			      "# to send the humans (the QUEUE_DIST) all email that it\n",
			      "# receives.  It is 0 otherwise.\n\n",
			      "SEND_TO_HUMANS=1\n\n",
			      "# SEND_ON_ASSIGN is a bool value.  It is 1 if you have\n",
			      "# SEND_TO_HUMANS set to 0 and you want a copy of the\n",
			      "# ticket logfile emailed to the person to whome the ticket\n",
			      "# gets assigned to.  This way, whoever is assigned to take\n",
			      "# care of the ticket has a copy of the logfile in their\n",
			      "# email mailbox.  It is 0 otherwise.\n\n",
			      "SEND_ON_ASSIGN=0\n\n",
			      "# SEND_FROM_USERNAME is a bool value.  It is 1 of you\n",
			      "# want RUST outgoing email to have a From: header like\n",
			      "# this:\n",
			      "#\n",
			      "#             John Doe <QUEUE_EMAIL_ADDR>\n",
			      "#\n",
			      "# Otherwise, RUST outgoing email will have a From: header\n",
			      "# like this:\n",
			      "#\n",
			      "#              QUEUE_TITLE <QUEUE_EMAIL_ADDR>\n\n",
			      "SEND_FROM_USERNAME=1\n\n",
			      "# USER_SEE_ALL is a bool value.  It is 1 if you want\n",
			      "# users using RUST user interfaces to be able to see\n",
			      "# all tickets in the queue.  It is 0 if you want\n",
			      "# users using RUST user interfaces to be able to see\n",
			      "# only the tickets in the queue which they made.\n\n",
			      "USER_SEE_ALL=1\n\n",
			      "# TAKE_INITIAL_MAIL is a bool value.  It is 1 if you\n",
			      "# want unassigned tickets to be assigned to the first\n",
			      "# staff member who sends a reply back to the user of a\n",
			      "# ticket.  It is 0 otherwise.\n\n",
			      "TAKE_INITIAL_MAIL=1\n\n",
			      "# NO_UNOWNED_RESOLVED is a bool value.  It is 1 if you\n",
			      "# want unassigned tickets being resolved to be owned\n",
			      "# by someone.  This is so that there are no unowned\n",
			      "# resolved tickets.  It is 0 otherwise.\n\n",
			      "NO_UNOWNED_RESOLVED=1\n\n",
			      "# PUBLIC_QUEUE is a bool value.  It is 1 if you\n",
			      "# want the queue to be publicly accessable, 0 if you\n",
			      "# do not.  Users can look at tickets in a public\n",
			      "# queue.\n\n",
			      "PUBLIC_QUEUE=1\n\n",
			      "# STAFF_LIST is a list of usernames, real names, and a RUST\n",
			      "# password for each user who will be allowed to work on RUST\n",
			      "# tickets in the queue being created.\n",
			      "#\n",
			      "# IMPORTANT: The RUST pseudo user and your mailer UID NEEDS\n",
			      "# TO BE INCLUDED HERE.  If you forget to put these to logins\n",
			      "# here, then RUST will not work properly!!!!!\n",
			      "#\n",
			      "# The format of this data is one username:real_name:password\n",
			      "# per line.  STAFF_LIST needs to proceed the list and\n",
			      "# END needs to come after the list\n",
			      "#\n",
			      "# Example\n",
			      "#\n",
			      "# STAFF_LIST\n",
			      "# ruefenac:Craig R. Ruefenacht:asdfjkl\n",
			      "# jdoe:John Doe:waCkeY\n",
			      "# janed:Jane Doe:YekCaw\n",
			      "# END\n\n",
			      "STAFF_LIST\n",
			      "END\n\n",
			      "# DONT_MAKE is a list of usernames, who if RUST gets mail from\n",
			      "# them, their email WILL NOT be made into a ticket. This list\n",
			      "# should include MAILER-DAEMON.\n",
			      "#\n",
			      "# The format of this data is one username per line.\n",
			      "# DONT_LIST needs to proceed the list and END\n",
			      "# needs to come after the list\n",
			      "#\n",
			      "# Example\n",
			      "#\n",
			      "# DONT_LIST \n",
			      "# MAILER-DAEMON\n",
			      "# root\n",
			      "# postmaster\n",
			      "# END\n\n",
			      "DONT_LIST\n",
			      "MAILER-DAEMON\n",
			      "END\n\n",
			      "# WORK_LIST is a list of usernames, who if RUST gets mail from\n",
			      "# them, their email should be made into a WORK ticket.  Otherwise\n",
			      "# the email will be made into a USER ticket.\n",
			      "#\n",
			      "# The format for this is username:real_name.\n",
			      "#\n",
			      "# Example\n",
			      "#\n",
			      "# WORK_LIST\n",
			      "# ruefenac:Craig R. Ruefenacht\n",
			      "# miller:Jack Miller\n",
			      "# smith:Bill Smith\n",
			      "# END\n\n",
			      "WORK_LIST\n",
			      "END\n\n");
	      $index++;
	  }
			
	  print TEMPLATE "# EOF\n";
	  close(TEMPLATE);
	  }

   Rust_Debug::ddebug("Leaving Rust_Lib::create_template_file\n");
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub create_queue_files {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Create the necessary files for a new queue
    #
    #           This assumes that you are *NOT* running setgid,
    #           as it will give you an error message if you are.

  Rust_Debug::ddebug("Entering Rust_Create::make_queue_files\n");
    
    my (@lines, $error, $user_response, $okay, @user_list);
    my (@create_data, $index);
    my ($path_to_headers) = $globals->{'path_to_headers'};
    my ($path_to_resolved) = $globals->{'path_to_resolved'};
    my ($path_to_active) = $globals->{'path_to_active'};
    my ($path_to_archived) = $globals->{'path_to_archived'};
    my ($path_to_etc) = $globals->{'path_to_etc'};

    # Not running setgid, so we can get away with this path setup here
    #
    # Also, direct most debugging to stdout since this script should
    # always be run in an interactive shell...
  
    $ENV{'PATH'} = "/bin";

    umask(017);

    @create_data = ();
    print "Reading create.datafile...\n";
    open(INPUT,"<create.datafile");
    while (<INPUT>) {
	next if /^#/;
	next if /^\s*$/;
	chomp;
	push(@create_data, $_);
    }
    close(INPUT);

    $index = 0;
    if ($create_data[$index] =~ /(NUMBER)/){ 
	$number_to_add = (split('=',$create_data[$index]))[1];
	print "There will be $number_to_add queues added to RUST...\n";

	$index++;
	$new_configs->{'number_of_queues'} = $number_to_add;
	
	$current_number = 0;

	while ($current_number < $number_to_add) {
	    $current_number++;

	    $found = 0;
	    while ($found < 19) {

		if ($create_data[$index] =~ /(QUEUE_NAME)/){ 
		    $alias_name = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(QUEUE_EMAIL_ADDR)/) {
		    $alias_email_addr = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(QUEUE_TITLE)/) {
		    $alias_title = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(QUEUE_DIST)/) {
		    $alias_dist = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(LOCAL_DOMAIN)/) {
		    $local_domain = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(RESTRICT_TO)/) {
		    $restrict_to = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(DO_INITIAL_AUTOREPLY)/) {
		    $do_initial_autoreply = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(INCLUDE_ORIG)/) {
		    $include_orig = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(DO_ASSIGN_AUTOREPLY)/) {
		    $do_assign_autoreply = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(SEND_TO_HUMANS)/) {
		    $send_to_humans = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(SEND_ON_ASSIGN)/) {
		    $send_on_assign = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(SEND_FROM_USERNAME)/) {
		    $send_from_username = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(USER_SEE_ALL)/) {
		    $user_see_all = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(TAKE_INITIAL_MAIL)/) {
		    $take_initial_mail = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(NO_UNOWNED_RESOLVED)/) {
		    $no_unowned_resolved = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(PUBLIC_QUEUE)/) {
		    $public_queue = (split('=',$create_data[$index]))[1];
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(STAFF_LIST)/) {
		    $index++;
		    @staff_list = ();
		    while (!($create_data[$index] =~ /(END)/)) {
			push(@staff_list, $create_data[$index]);
			$index++;
		    }
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(DONT_LIST)/) {
		    $index++;
		    @dont_list = ();
		    while (!($create_data[$index] =~ /(END)/)) {
			push(@dont_list, $create_data[$index]);
			$index++;
		    }
		    $index++;
		    $found++;
		}

		if ($create_data[$index] =~ /(WORK_LIST)/) {
		    $index++;
		    @work_list = ();
		    while (!($create_data[$index] =~ /(END)/)) {
			push(@work_list, $create_data[$index]);
			$index++;
		    }
		    $index++;
		    $found++;
		}
	    }

	    # Create the necessary directories

	    print "\nProcessing the $alias_name queue...\n\n";
	    print "Creating database directories\n";
	    umask(017);
      
	    print "     Creating $path_to_headers/$alias_name\n";
	    `mkdir $path_to_headers/$alias_name`;
	    print "     Creating $path_to_resolved/$alias_name\n";
	    `mkdir $path_to_resolved/$alias_name`;
	    print "     Creating $path_to_active/$alias_name\n";
	    `mkdir $path_to_active/$alias_name`;
	    print "     Creating $path_to_archived/$alias_name\n";
	    `mkdir $path_to_archived/$alias_name`;
	    `chmod 2770 $path_to_headers/$alias_name`;
	    `chmod 2770 $path_to_resolved/$alias_name`;
	    `chmod 2770 $path_to_active/$alias_name`;
	    `chmod 2770 $path_to_archived/$alias_name`;

	    # Create the numbers file

	    print "Creating $path_to_etc/numbers/number-$alias_name\n";

	    $error = Rust_Lib::number_funcs($caller, 'create', $alias_name, 0);

	    Rust_Exit::terminate("Exit: Had problems creating $alias_name numbers file!\n")
		if $error;

	    # First see if we need to create a <top_level/etc/auth/<login> file
	    # for each user on a work or auth list.
	    # Also see if we need to create a <top_level/etc/dont_make/<login>
	    # file for each user on a dont_make list.

	    umask(037);

	    print "Creating user authorization files\n";

	    print "     Updating the STAFF listings in $path_to_etc/auth\n";
	    foreach $each_name (@work_list) {
		($user_name, $real_name, $xrust_password) = split(':',$each_name);
		$auth_file = "$path_to_etc/auth/$user_name";
		if (!(-f "$auth_file")) {
		    &create_auth_name_file($each_name);
		}

		&add_to_auth_file($alias_name,$auth_file,"STAFF");
		print "          $user_name updated\n";
	    }

	    print "     Updating the QUEUES listings in $path_to_etc/auth\n"; 
	    foreach $each_name (@staff_list) {
		($user_name, $real_name, $xrust_password) = split(':',$each_name);
		$auth_file = "$path_to_etc/auth/$user_name";
		if (!(-f "$auth_file")) {
		    &create_auth_name_file($each_name);
		}

		&add_to_auth_file($alias_name,$auth_file,"QUEUES");
		print "          $user_name updated\n";
	    }

	    print "     Updating the DONT_MAKE listings in $path_to_etc/dont_make\n";
	    foreach $each_name (@dont_list) {
		$auth_file = "$path_to_etc/dont_make/$each_name";
		if (!(-f "$auth_file")) {
		    &create_dont_make_file($each_name);
		}

		&add_to_auth_file($alias_name,$auth_file,"DONT_MAKE");
		print "          $each_name updated\n";
	    }

	    print "     Updating $path_to_etc/RUST.names\n";
	    @user_list = ();
	    if (-f "$path_to_etc/RUST.names") {
		open(RUST_NAMES, "<$path_to_etc/RUST.names");
		while (<RUST_NAMES>) {
		    next if /^#/;
		    next if /^\s*$/;
		    chop;
		    push (@user_list, $_);
		}
		close(RUST_NAMES);

		unlink("$path_to_etc/RUST.names");
	
		if (open(RUST_NAMES, ">$path_to_etc/RUST.names")) {
		    print RUST_NAMES ("# This file contains a list of usernames who can work on\n",
				      "# at least *one* RUST queue.\n",
				      "#\n",
				      "# The format of this file is one username per line\n",
				      "\n");
		    
		    foreach $each_input (@staff_list) {
			$username = (split(':',$each_input))[0];
			$found = 0;
			foreach $each_user (@user_list) {
			    $found = 1 if ($each_user eq $username);
			}

			print RUST_NAMES "$username\n" if (!$found);
		    }

		    foreach $each (@user_list) {
			print RUST_NAMES "$each\n";
		    }
		    print RUST_NAMES "\n# EOF\n";
		    close(RUST_NAMES);

		} else {
		  Rust_Exit::terminate("Exit: Had problems creating the RUST.names file!\n");
		}
	    } else {
		if (open(RUST_NAMES, ">$path_to_etc/RUST.names")) {
		    print RUST_NAMES ("# This file contains a list of usernames who can work on\n",
				      "# at least *one* RUST queue.\n",
				      "#\n",
				      "# The format of this file is one username per line\n",
				      "\n");
		    
		    foreach $each (@staff_list) {
			print RUST_NAMES ((split(':',$each))[0], "\n");
		    }
		    print RUST_NAMES "\n# EOF\n";	
		    close(RUST_NAMES);
		} else {
		  Rust_Exit::terminate("Exit: Had problems creating the RUST.names file!\n");
		}
	    }

	    # Now for the last step.  Generate a new rust_configs file
	    
	    print "Updating the RUST config file $path_to_etc/rust_configs\n";

	    open(CONF_IN,"<$path_to_etc/rust_configs");
	    while (<CONF_IN>) {
		chomp;
		push(@configs_contents, $_);
	    }
	    close(CONF_IN);

	    unlink("$path_to_etc/rust_configs");
	    open(CONF_OUT,">$path_to_etc/rust_configs");
	    
	    $index = 0;
	    while(!((split('=',$configs_contents[$index]))[0] eq "number_of_queues")) {
		print CONF_OUT "$configs_contents[$index]\n";
		$index++;
	    }
	
	    $original_number = (split('=',$configs_contents[$index]))[1];
	    $new_number = $original_number + 1;
	    print CONF_OUT "number_of_queues=$new_number\n";
	    $index++;

	    while ($index < $#configs_contents) {
		print CONF_OUT "$configs_contents[$index]\n";
		$index++;
	    }

	    print CONF_OUT ("#\n",
			    "# ======================================================\n",
			    "# $alias_title QUEUE\n",
			    "queue_name=$alias_name\n",
			    "queue_email_addr=$alias_email_addr\n",
			    "queue_title=$alias_title\n",
			    "queue_dist=$alias_dist\n",
			    "restrict_to=$restrict_to\n",
			    "do_initial_autoreply=$do_initial_autoreply\n",
			    "include_orig=$include_orig\n",
			    "do_assign_autoreply=$do_assign_autoreply\n",
			    "send_to_humans=$send_to_humans\n",
			    "send_on_assign=$send_on_assign\n",
			    "send_from_username=$send_from_username\n",
			    "take_initial_mail=$take_initial_mail\n",
			    "no_unowned_resolved=$no_unowned_resolved\n",
			    "public_queue=$public_queue\n",
			    "tag_prefix=$tag_prefix\n\n");
		close(CONF_OUT);

	    print "\nDone adding $alias_name queue to the RUST Mail System.\n";
	}

    } else {
      Rust_Exit::terminate("Exit: Couldn't determine how many queues to add!\n");
    }
    
    print ("Done adding queues to the RUST Mail System.\n",
	   "Do not forget to update your system mail aliases file.\n\n");

  Rust_Debug::ddebug("Leaving Rust_Create::create_queue_files\n");

}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub create_auth_name_file {
    #
    # Type: Not to be called by external scripts
    #
    # Abstract: Generate a new username file structure used
    #           for RUST authentication.  This creates a
    #           file for the given user because it does not
    #           exist yet and we need one.
    #

    my ($user_info) = @_;
    my ($user_name, $real_name, $xrust_password) = split(':',$user_info);
    my ($path_to_etc) = $globals->{'path_to_etc'};

    # Only create a new one if it does not exist.  It
    # should not exist if we got here, but just in case...

    if (!(-f "$path_to_etc/auth/$user_name")) {

	open(OUTPUT,">$path_to_etc/auth/$user_name");
	print OUTPUT ("# Auto-generated file.  Edit with caution\n",
		      "#\n",
		      "# This file contains information about the user $user_name\n",
		      "#\n",
		      "#\n",
		      "# REAL_NAME is the real name of the user as they use it\n",
		      "REAL_NAME=$real_name\n",
		      "#\n",
		      "# XRUST PASSWORD is the password $user_name wants to use\n",
		      "# when doing commands vial email using X-Rust headers\n",
		      "#\n",
		      "PASSWORD=$xrust_password\n",
		      "#\n",
		      "# QUEUES is the list of queues $user_name is on\n",
		      "QUEUES\n",
		      "END\n",
		      "#\n",
		      "# STAFF is the list of queues that $user_name is considered\n",
		      "# staff as apposed to just a normal user\n",
		      "STAFF\n",
		      "END\n",
		      "#\n",
		      "#EOF\n");
	
	close(OUTPUT);
    } else {
	&debug("An auth file for $user_name already exists at $path_to_etc/auth/$user_name\n",
	       "Not making a new one.\n\n");
    }
}


#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub create_dont_make_file {
    #
    # Type: Not to be called by external scripts
    #
    # Abstract: Generate a new dont_make file for RUST
    #           to use to determine whether email from
    #           the user in question should be made into
    #           a ticket or not.
    #

    my ($user_name) = @_;
    my ($path_to_etc) = $globals->{'path_to_etc'};

    # Only create a new one if it does not exist.  It
    # should not exist if we got here, but just in case...

    if (!(-f "$path_to_etc/dont_make/$user_name")) {

	open(OUTPUT,">$path_to_etc/dont_make/$user_name");
	print OUTPUT ("# Auto-generated file.  Edit with caution\n",
		      "#\n",
		      "# This file contains information about $user_name\n",
		      "#\n",
		      "# Listed in this file are the queues which if they\n",
		      "# recieve email from $user_name, the email WILL NOT be\n",
		      "# made into a ticket.\n",
		      #"\n",
		      "DONT_MAKE\n",
		      "END\n",
		      "#\n",
		      "# EOF\n");
    } else {
	print ("An dont_make file for $user_name already exists at\n",
	       "$path_to_etc/dont_make/$user_name.  Not making a new one.\n\n");
    }
}


#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub add_to_auth_file {
    #
    # Type: Not to be called by external scripts
    #
    # Abstract: Add a queue name to either the STAFF or
    #           QUEUE section of a auth file of a user.
    #

    my ($alias_name, $auth_file, $queue_type) = @_;
    my(@user_file_contents, $new_queue, $index);

    open(INPUT,"<$auth_file");
    $new_queue = 1;       
    while (<INPUT>) {
	chomp;
	push(@user_file_contents, $_);
    }
    close(INPUT);
    
    unlink($auth_file);
    
    open(OUTPUT,">$auth_file");
    
    $index = -1;
    while ($index < $#user_file_contents) {
	
	$index++;
	    
	    if ($user_file_contents[$index] eq $queue_type) {
		print OUTPUT "$user_file_contents[$index]\n";
		$index++;

		while ($user_file_contents[$index] ne "END") {
		    if ($user_file_contents[$index] eq $alias_name) {
			$new_queue = 0;
		    }
		    print OUTPUT "$user_file_contents[$index]\n";
		    $index++;
		}
		
		if ($new_queue) {
		    print OUTPUT "$alias_name\n";
		}
		
		print OUTPUT "$user_file_contents[$index]\n";
		
	    } else {
		print OUTPUT "$user_file_contents[$index]\n";
	    }
    }
    
    close(OUTPUT);
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub convert_names {
    #
    # Type: Not to be called by external scripts
    #
    # Abstract: Generate a new username file structure used
    #           for RUST authentication.  It takes what was
    #           used in beta-5 and earlier versions of RUST
    #           and puts them into the beta-6 format.
    #
    # THIS IS ONLY TO BE USED WHEN UPGRADING FROM beta-5 
    # (or previous version) to beta-6.  THIS IS A TEMPORARY
    # FUNCTION WHICH WILL BE REMOVED IN FUTURE RELEASES
    #

    my %AUTH_LIST;
    my %STAFF_LIST;
    my %DONT_LIST;
    my %PASSWORDS;
    my (@keys, $key, $each, @name_data, @auth_queues, $name_file,
	$path_to_auth, $path_to_dont_make, $path_to_etc);

    $path_to_etc = $globals->{'path_to_etc'};

    foreach $each (split(':',$globals->{'alias_name'})) {

	print "Converting the $each queue names...\n";

	open(INPUT,"<$path_to_etc/.$each");
	
	while (<INPUT>) {
	    
	    chomp;
	    next if /^#/;
	    next if /^\s*$/;
	    
	    @name_data = split(':',$_);
	    
	    next if ($name_data[0] eq "root");
	    next if ($name_data[0] eq "rustsys");
	    next if ($name_data[0] eq "nobody");

	    
	    chomp($name_data[0]);
	    if (!$AUTH_LIST{$name_data[0]}) {
		$AUTH_LIST{$name_data[0]} = $each;
	    } else {
		$AUTH_LIST{$name_data[0]} = join(',',$AUTH_LIST{$name_data[0]},
						 $each);
	    }
	}
	
	
	close(INPUT);
	
	open(INPUT,"<$path_to_etc/$each.dont_make");
	
	while (<INPUT>) {
	    
	    chomp;
	    next if /^#/;
	    next if /^\s*$/;
	    
	    
	    if (!$DONT_LIST{$_}) {
		$DONT_LIST{$_} = $each;
	    } else {
		$DONT_LIST{$_} = join(',',$DONT_LIST{$_}, $each);
	    }
	}
	close(INPUT);
	
	
	open(INPUT,"</$path_to_etc/$each.names");
	
	while (<INPUT>) {
	    
	    chomp;
	    next if /^#/;
	    next if /^\s*$/;
	    
	    if (!$STAFF_LIST{$_}) {
		$STAFF_LIST{$_} = $each;
	    } else {
		$STAFF_LIST{$_} = join(',',$STAFF_LIST{$_}, $each);
	    }
	}
	close(INPUT);
	
    }
    
    open(INPUT,"<$path_to_etc/.passwords");
    
    while (<INPUT>) {
	
	chomp;
	next if /^#/;
	next if /^\s*$/;

	@password_info = split(':',$_);

	$PASSWORDS{$password_info[2]} = $_;
    }
    close(INPUT);

    @keys = keys %AUTH_LIST;
    
    foreach $key (@keys) {
	@auth_queues = split(',',$AUTH_LIST{$key});

	$path_to_auth = $globals->{'path_to_auth'};
	$name_file = join('/',$globals->{'path_to_auth'},,$key);

	if (!(-d "$path_to_auth")) {
	    `$globals->{'mkdir_prog'} $path_to_auth`;
	}

	if (-f "$name_file") {
	    `rm $name_file`;
	}

	open(NAME_FILE,">$name_file");

	print NAME_FILE ("# Auto-generated file.  Edit with caution\n",
			 "#\n",
			 "# This file contains information about the user $key\n",
			 "#\n\n");

	print NAME_FILE "# REAL_NAME is the real name of the user as they use it\n";
	print NAME_FILE ("REAL_NAME=",(split(':',$PASSWORDS{$key}))[0],"\n\n");

	print NAME_FILE ("# XRUST PASSWORD is the password the user wants to use\n",
			 "# when doing commands vial email using X-Rust headers\n");
	print NAME_FILE ("PASSWORD=",(split(':',$PASSWORDS{$key}))[1],"\n\n");

	print NAME_FILE ("# QUEUES is the list of queues $key is on\n",
			 "QUEUES\n");
	foreach $each_queue (@auth_queues) {
	    print NAME_FILE "$each_queue\n";
	}
	print NAME_FILE "END\n\n\n";
	close(NAME_FILE);
    }

    @keys = keys %STAFF_LIST;
    
    foreach $key (@keys) {
	@auth_queues = split(',',$STAFF_LIST{$key});

	$path_to_auth = $globals->{'path_to_auth'};
	$name_file = join('/',$globals->{'path_to_auth'},,$key);

	if (!(-d "$path_to_auth")) {
	    `$globals->{'mkdir_prog'} $path_to_auth`;
	}

	open(NAME_FILE,">>$name_file");

	print NAME_FILE ("#\n",
			 "# STAFF is the list of queues that $key is considered\n",
			 "# staff as apposed to just a normal user\n",
			 "STAFF\n");
	foreach $each_queue (@auth_queues) {
	    print NAME_FILE "$each_queue\n";
	}
	print NAME_FILE "END\n\n\n";
	close(NAME_FILE);
    }

    @keys = keys %DONT_LIST;

    foreach $key (@keys) {
	@auth_queues = split(',',$DONT_LIST{$key});

	$path_to_dont_make = $globals->{'path_to_dont_make'};
	$name_file = join('/',$globals->{'path_to_dont_make'},,$key);

	if (!(-d "$path_to_dont_make")) {
	    `$globals->{'mkdir_prog'} $path_to_dont_make`;
	}

	open(NAME_FILE,">>$name_file");

	print NAME_FILE ("# Auto-generated file.  Edit with caution\n",
			 "#\n",
			 "# This file contains information about $key\n",
			 "#\n",
			 "# Listed in this file are the queues which if they\n",
			 "# recieve email from $key, the email WILL NOT be\n",
			 "# made into a ticket.\n\n",
			 "DONT_MAKE\n");
	foreach $each_queue (@auth_queues) {
	    print NAME_FILE "$each_queue\n";
	}
	print NAME_FILE "END\n\n\n";
	close(NAME_FILE);
    }

}

}

1;

# EOF
	    
	
