# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Rust_Configs.pm,v 1.39 1997/04/16 20:18:37 ruefenac Exp $
#
# This is part of RUST-1.0 Beta 6
#
# Abstract: This library contains functions to create a hash for
#           global configuration variables for rust.
#
# 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 Inquire, 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.
#
# How this file works
#
#      This file contains all the configurable options of RUST.
#      To modify/change configuration variables, go through the
#      "get_globals" subroutine and change whatever you are looking
#      for. There are comments throughout the subroutine that
#      explains what config variables do what, so read carefully.
#
#      Some configs in here are arrays.  They are not a real array,
#      but have the format
#
#           <some_info>:<some_more_info>:<yet_some_more_info>....
# 
#      The above line is returned as a string.  The elements of the
#      "array" are seperated by the ':' character.  All arrays in the
#      global configs are related to each other.  The first element in
#      each "array" concerns RUST Queue #1, the second element concerns
#      RUST Queue #2, the third concerns RUST Queue #3, and so on. 

{
package Rust_Configs;

use Rust_Lib;
require Exporter;
@ISA = qw(Exporter);
@EXPORT =
    (
     # name                       brief description
     # --------------------------------------------------------     
     'get_globals',		# Create a hash table of global vars
     'extract_element'		# Extract an element from the hash
     );
 
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub get_globals {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Create a new "class" definition to hold global config
    #           information.

    $globals = {};
    my (@configs) = ();
    my ($top_level);
    my ($configs_file) = "/p/condor/home/rust/etc/rust_configs";
    $configs_file = Rust_Lib::untaint_obj($configs_file);

    # Need to make sure the PATH is zero'ed out because some perl
    # scripts run effective setgid, and perl doesn't like to have
    # paths then.  Scripts running real setgid don't have problems
    # with this.

    $ENV{'PATH'} = "";

    # ======================================================
    # ======================================================

    open(CONFIG,"<$configs_file");

    while (<CONFIG>) {
	chomp;
	push(@configs, $_);
    }

    close(CONFIG);

    $config_size = $#configs;
    $index = -1;
    $queue_configs = 0;
    $globals = {};
    @alias_name = ();
    @alias_email_addr = ();
    @alias_title = ();
    @alias_dist = ();
    @restrict_to = ();
    @do_initial_autoreply = ();
    @indluce_orig = ();
    @do_assign_autoreply = ();
    @send_to_humans = ();
    @send_on_assign = ();
    @send_from_username = ();
    @take_initial_mail = ();
    @no_unowned_resolved = ();
    @user_see_all = ();
    @tag_prefix = ();

    while ($index < $config_size) {

	$index++;
	next if ($configs[$index] =~ /^#/);
	next if ($configs[$index] =~ /^\s*$/);
		 
	@data = split('=',$configs[$index]);

	if (($data[0] ne 'queue_name') &&
	    ($queue_configs == 0)) {
	    $data[1] =~ s/\s*//;
	    $globals->{$data[0]} = $data[1];
	    if ($data[0] eq "top_level") {
		$top_level = $data[1];
	    }
	    next;
	}

	if ($data[0] eq 'queue_name') {

	    push(@alias_name, $data[1]);

	    $queue_configs = 1;

	    while (($index < $config_size) &&
		   ((split('=',$configs[$index + 1]))[0] ne 'queue_name')) {

		$index++;
		
		next if ($configs[$index] =~ /^#/);
		next if ($configs[$index] =~ /^\s*$/);
			 
		@data = split('=',$configs[$index]);
			 
		$data[1] =~ s/\s*//;

		if ($data[0] eq 'queue_email_addr') {
		    push(@alias_email_addr, $data[1]);
		} elsif ($data[0] eq 'queue_title') {
		    push(@alias_title, $data[1]);
		} elsif ($data[0] eq 'queue_dist') {
		    push(@alias_dist, $data[1]);
		} elsif ($data[0] eq 'local_domain') {
		    push(@local_domain, $data[1]);
		} elsif ($data[0] eq 'restrict_to') {
		    push(@restrict_to, $data[1]);
		} elsif ($data[0] eq 'do_initial_autoreply') {
		    push(@do_initial_autoreply, $data[1]);
		} elsif ($data[0] eq 'include_orig') {
		    push(@include_orig, $data[1]);
		} elsif ($data[0] eq 'do_assign_autoreply') {
		    push(@do_assign_autoreply, $data[1]);
		} elsif ($data[0] eq 'send_to_humans') {
		    push(@send_to_humans, $data[1]);
		} elsif ($data[0] eq 'send_on_assign') {
		    push(@send_on_assign, $data[1]);
		} elsif ($data[0] eq 'send_from_username') {
		    push(@send_from_username, $data[1]);
		} elsif ($data[0] eq 'take_initial_mail') {
		    push(@take_initial_mail, $data[1]);
		} elsif ($data[0] eq 'no_unowned_resolved') {
		    push(@no_unowned_resolved, $data[1]);
		} elsif ($data[0] eq 'tag_prefix') {
		    push(@tag_prefix, $data[1]);
		} elsif ($data[0] eq 'public_queue') {
		    push(@public_queue, $data[1]);
		} elsif ($data[0] eq 'user_see_all') {
		    push(@user_see_all, $data[1]);
		} elsif ($data[0] ne 'queue_name') {
		    print "ERROR PARSING line $configs[$index] !!!\n";
		}
	    }
	}
    }

    $globals->{'alias_name'} = join(':',@alias_name);
    $globals->{'alias_email_addr'} = join(':',@alias_email_addr);
    $globals->{"alias_title"} = join(':', @alias_title);
    $globals->{'alias_dist'} = join(':',@alias_dist);
    $globals->{'local_domain'} = join(':',@local_domain);
    $globals->{'restrict_to'} = join(':',@restrict_to);
    $globals->{'do_initial_autoreply'} = join(':',@do_initial_autoreply);
    $globals->{'include_orig'} = join(':',@include_orig);
    $globals->{'do_assign_autoreply'} =  join(':',@do_assign_autoreply);
    $globals->{'send_to_humans'} = join(':',@send_to_humans);
    $globals->{'send_on_assign'} = join(':',@send_on_assign);
    $globals->{'send_from_username'} = join(':',@send_from_username);
    $globals->{'take_initial_mail'} = join(':',@take_initial_mail);
    $globals->{'no_unowned_resolved'} = join(':',@no_unowned_resolved);
    $globals->{'valid_status'} = join(':','new','open','resolved','archived',
				      $globals->{'valid_status'});
    $globals->{'public_queue'} = join(':',@public_queue);
    $globals->{'user_see_all'} = join(':',@user_see_all);		 
    
    @temp = ();
    $tag_index = -1;
    foreach $queue (@alias_name) {
	$tag_index++;
	push(@temp, join('',"[", $tag_prefix[$tag_index], "$queue #%s] %s"));
	}
    $globals->{'tag'} = join(':',@temp);
    
    @temp = ();
    $tag_index = -1;
    foreach $queue (@alias_name) {
	$tag_index++;
	push(@temp, join('', "\\[", $tag_prefix[$tag_index], "$queue\\s*#(-?\\d+)\\]"));
    }
    $globals->{'tag_number'} = join(':',@temp);
    
    @temp = ();
    $tag_index = -1;
    foreach $queue (@alias_name) {
	$tag_index++;
	push(@temp, join('', "\\[", $tag_prefix[$tag_index], "$queue\\s*"));
    }
    $globals->{'tag_queue'} = join(':',@temp);
  
    # hostname returns the name of the host.  Unfortunatly, the
    # hostname program is in different places on different
    # machines.  If yours is different than the ones below,
    # then add yours here.  Have to untaint it too.  The
    # reason why you have to specify the path to it is because
    # under setgid, the ENV{path} has to be zero'ed out, otherwise
    # the setgid script will croak.

    if (-x "/bin/hostname") {
	$hostname_path = "/bin/hostname";
	$hostname_path = Rust_Lib::untaint_obj($hostname_path);
	$hostname = `$hostname_path`;
	chop $hostname;
    }
    elsif (-x "/usr/ucb/hostname") {
	$hostname_path = "/usr/ucb/hostname";
	$hostname_path = Rust_Lib::untaint_obj($hostname_path);
	$hostname = `$hostname_path`;
	chop $hostname;
    }
    elsif (-x "/usr/bin/hostname") {
	$hostname_path = "/usr/bin/hostname";
	$hostname_path = Rust_Lib::untaint_obj($hostname_path);
	$hostname = `$hostname_path`;
	chop $hostname;
    } 
    elsif (-x "/usr/bsd/hostname") {
	$hostname_path = "/usr/bsd/hostname";
	$hostname_path = Rust_Lib::untaint_obj($hostname_path);
	$hostname = `$hostname_path`;
	chop $hostname;
    } else {
	$hostname = "Unknown host";
    }
    $globals->{'hostname'} = $hostname;
    
    # name_file returns the path and filename of a file that
    # contains all the usernames who can work in the RUST queue.
    # Each RUST queue has its own file.  But, if the same people
    # take care of more than one RUST queue, they can share the
    # same file.  This file is used to determine whether or not
    # to create a "work" request whenever a new request is made.

    @temp = ();
    foreach $each (@alias_name) {
	push(@temp, "$top_level/etc/$each.names");
    }

    $globals->{'name_file'} = join(':',@temp);
    
    # signature_file returns the path and filename to a file
    # that is to be used as the "signature" file when the
    # RUST system sends mail out.  This is only a temp file
    # and is deleted once it is used.

    $globals->{'signature_file'} = "$top_level/tmp/signature.$$";

    # dont_make returns the path and filename to a file that contains
    # a list of usernames, of which if mail is sent from them,
    # you DO NOT want a request made, but rather just want the
    # mail forwarded to the alias-dist address.  Such users would
    # include MAILER-DAEMON and other stuff of that sort.

    @temp = ();
    foreach $each (@alias_name) {
	push(@temp, "$top_level/etc/$each.dont_make");
    }

    $globals->{'dont_make'} = join(':',@temp);

    # path_to_scripts is the path where RUST keeps its
    # various scripts.

    $globals->{'path_to_scripts'} = "$top_level/scripts";

    # path_to_tmp is the path where RUST keeps temp files.

    $globals->{'path_to_tmp'} = "$top_level/tmp";

    # path_to_etc is the path where RUST keeps miscallaneous
    # config files.

    $globals->{'path_to_etc'} = "$top_level/etc";

    # path_to_log is the path where RUST puts log files.

    $globals->{'path_to_log'} = "$top_level/logs";

    # path_to_debug is the path where RUST puts debug files.

    $globals->{'path_to_debug'} = "$top_level/debug";   
    
    # path_to_error is the path where RUST puts error files.  

    $globals->{'path_to_error'} = "$top_level/errors";

    # active_dirs is the path where RUST puts "active" requests.

    $globals->{'path_to_active'} = "$top_level/active";

    # resolved_dirs is the path where RUST puts "resolved" requests.

    $globals->{'path_to_resolved'} = "$top_level/resolved";

    # archived_dirs is the path where RUST puts "archived" requests.

    $globals->{'path_to_archived'} = "$top_level/archived";

    # path_to_headers is the path where RUST puts header files for
    # each RUST request.

    $globals->{'path_to_headers'} = "$top_level/headers";

    # path_to_mail_archives is the path where RUST puts its mail
    # archives.  The archives ARE NOT archived tickets.  The archives
    # are records of all mail sent to each queue.  When mail is sent
    # to a queue, the mail is appended to the end of the archive file.

    $globals->{'path_to_mail_archives'} = "$top_level/mail_archives";
  
    # path_to_auth is the path where RUST puts the auth files
    # which contain info about what users can work on each
    # queue.  This is new as of beta-6.

    $globals->{'path_to_auth'} = "$top_level/etc/auth";

    # path_to_dont_make is the path where RUST puts the dont_make
    # files which contain info about what user's email doesn't
    # get made into tickets.  This is new as of beta-6

    $globals->{'path_to_dont_make'} = "$top_level/etc/dont_make";

    return $globals;
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub extract_element {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Take in a element which is stored in the global
    #           hash and "split" it apart to extract which one
    #           is needed.  This is done here to remove these
    #           messy calls, because it is an ugly line of code

    my ($key, $index) = @_;

    my $line = $globals->{$key};
    return (split(':',$line))[$index];
}
}
1;

#EOF
