# -*- Perl -*-
#
# Copyright (C) 1996 Craig Ruefenacht <ruefenac@cs.utah.edu> and the
#                    University of Utah Computer Science Department
#
# $Id: Rust_Debug.pm,v 1.25 1997/04/16 20:18:43 ruefenac Exp $
#
# This is part of RUST-1.0 Beta 6
#
# Abstract: This library contains the debugging functions 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 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_Debug;

use Rust_Configs;
use Rust_Exit;

require Exporter;
@ISA = qw(Exporter);
@EXPORT =
    (
     # name                       brief description
     # --------------------------------------------------------     
     'init_debug',		# Initialize this library
     'handle_error',		# Handle any error that was produced
     'start_debugging',		# Start a debug file
     'debug_globals',		# Write globals out
     'debug',			# Write a line out (debug = 1)
     'ddebug',			# Write a line out (debug = 2)
     'end_debugging'		# Close debug file
     );

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub init_debug {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Initialize this library by adding the $global
    #           hash to this object.

    $globals = &Rust_Configs::get_globals;

}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub handle_error {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Handle any errors that the modules may have
    #           produced

    my ($type, $error) = @_;

    &debug("The error I need to handle is $error\n");

    if ($type) {

	return "You don't have the authorization to do this.\n" if ($error == 1);
	return "Couldn't write to the ticket logfile.\n" if ($error == 2);
	return "Couldn't write to the system logfile.\n" if ($error == 3);
	return "Couldn't lock a new header file for the ticket.\n" if ($error == 4);
	return "Opps! A header file already existed for the new ticket.\n" if ($error == 5);
	return "Couldn't determine type of ticket (ie active/resolved)." if ($error == 6);
	return "The ticket is already owned by the specified user.\n" if ($error == 7);
	return "Couldn't find the ticket logfile to append to.\n" if ($error == 8);
	return "Wasn't able to increment the number file correctly.\n" if ($error == 9);
	return "Wasn't abel to determine what to do.\n" if ($error == 10);
	return "Couldn't open the number file for reading.\n" if ($error == 11);
	return "Can't give ownership to the specified user.\n" if ($error == 12);
	return "The specified ticket(s) didn't exist.\n" if ($error == 13);
	return "The ticket logfile for the ticket didn't exist.\n" if ($error == 14);
	return "The mail server got confused as to what to do.\n" if ($error == 15);
	return "The ticket is being taken care of by a different RUST queue.\n" if ($error == 16);
	return "There were no tickets found in the query.\n" if ($error == 17);
	return "Can't merge a ticket that isn't in the active queue.\n" if ($error == 18);
	return "Can't set that header field.\n" if ($error == 19);
	return "Can't assign a ticket to the queue it is already in.\n" if ($error == 20);
	return "Can't look at specified ticket because you don't own it.\n" if ($error == 21);
	return "The time specification wasn't in the proper HH:MM format.\n" if ($error == 22);
	return "Had problems parsing the \"To:\" header of the email.\n" if ($error == 23);
	return "Had problems parsing the \"From:\" header of the email.\n" if ($error == 24);
	return "RUST couldn't resolve any queues for the mail.  Sending to humans.\n" if ($error == 25);
	return "The user's mail shouldn't be made into a ticket.\n" if ($error == 26);
	return "The mail came from an Internet domain that can't make tickets.\n" if ($error == 27);
	return "Had problems locking a file.\n" if ($error == 28);
	return "The header file for the specified ticket was locked.  Couldn't update.\n" if ($error == 29);
	return "The ticketed section of the ticket file doesn't appear to exist\n" if ($error == 30);
	return "RUST had problems processing X-Rust headers that were found in email\n" if ($error == 31);
	return "RUST had problems sending mail to a RUST queue via sendmail\n" if ($error == 32);
	return "There were not enough tickets to archive!\n" if ($error == 33);
	return "Unknown system error.\n" if $error;
    }

    # The following three errors SHOULD NOT terminate the program because they really are not
    # errors.
    return "RUST couldn't resolve any queues for the mail.  Sending to humans.\n" if ($error == 25);
    return "The user's mail shouldn't be made into a ticket.\n" if ($error == 26);
    return "The mail came from an Internet domain that can't make tickets.\n" if ($error == 27);

    Rust_Exit::terminate("You don't have the authorization to do this!\n") if ($error == 1);
    Rust_Exit::terminate("Couldn't write to the ticket logfile!\n") if ($error == 2);
    Rust_Exit::terminate("Couldn't write to the system logfile!\n") if ($error == 3);
    Rust_Exit::terminate("Couldn't lock a new header file for the ticket!\n") if ($error == 4);
    Rust_Exit::terminate("Opps! A header file already existed for the new ticket!\n") if ($error == 5);
    Rust_Exit::terminate("Couldn't determine type of ticket (ie active/resolved)\n") if ($error == 6);
    Rust_Exit::terminate("The ticket is already owned by the specified user!\n") if ($error == 7);
    Rust_Exit::terminate("Couldn't find the ticket logfile to append to!\n") if ($error == 8);
    Rust_Exit::terminate("Wasn't able to increment the number file correctly!\n") if ($error == 9);
    Rust_Exit::terminate("Wasn't abel to determine what to do!\n") if ($error == 10);
    Rust_Exit::terminate("Couldn't open the number file for reading!\n") if ($error == 11);
    Rust_Exit::terminate("Can't give ownership to the specified user!\n") if ($error == 12);
    Rust_Exit::terminate("The specified reuqest(s) didn't exist!\n") if ($error == 13);
    Rust_Exit::terminate("The ticket logfile for the ticket didn't exist.\n") if ($error == 14);
    Rust_Exit::terminate("The mail server got confused as to what to do.\n") if ($error == 15);
    Rust_Exit::terminate("The ticket is being taken care of by a different RUST queue.\n") if ($error == 16);
    Rust_Exit::terminate("There were no tickets found in the query.\n") if ($error == 17);
    Rust_Exit::terminate("Can't merge a ticket that isn't in the active queue.\n") if ($error == 18);
    Rust_Exit::terminate("Can't set that header field.\n") if ($error == 19);
    Rust_Exit::terminate("Can't assign a ticket to the queue it is already in.\n") if ($error == 20);
    Rust_Exit::terminate("Can't look at specified ticket because you don't own it.\n") if ($error == 21);
    Rust_Exit::terminate("The time specification wasn't in the proper HH:MM format.\n") if ($error == 22);
    Rust_Exit::terminate("Had problems parsing the \"To:\" header of the email.\n") if ($error == 23);
    Rust_Exit::terminate("Had problems parsing the \"From:\" header of the email.\n") if ($error == 24);
    Rust_Exit::terminate("Had problems locking a file.\n") if ($error == 28);
    Rust_Exit::terminate("The header file for the specified ticket was locked.  Couldn't update.\n") if ($error == 29);
    Rust_Exit::terminate("The ticketed section of the ticket file doesn't appear to exist\n") if ($error == 30);
    Rust_Exit::terminate("RUST had problems processing X-Rust headers that were found in email\n") if ($error == 31);
    Rust_Exit::terminate("RUST had problems sending mail to a RUST queue via sendmail\n") if ($error == 32);
    Rust_Exit::terminate("There were not enough tickets to archive!\n") if ($error == 33);
    Rust_Exit::terminate("Unknown system error.  Aborting...\n") if $error;
}


#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub start_debugging {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Start a logfile that will hold debugging information
    #
    
    my ($program) = shift;
    my (@args) = @_;

    my ($date) = &main::ctime($^T);

    if($globals->{'do_debugging'}) {
 
	$debug_path = $globals->{'path_to_debug'};
	$hostname = $globals->{'hostname'};
	$debug_file = "$debug_path/$program.$hostname.$$";

	$debug_file = Rust_Lib::untaint_obj($debug_file);

	open(DEBUG, ">$debug_file") || return 0;

	$| = 1;
	print DEBUG "RUST script: $program\n";
	print DEBUG "  Arguments: $0 ", join(' ', @args), "\n" if (@args);
	print DEBUG "  Date/Time: $date\n";
	print DEBUG " Process ID: $$\n";
	print DEBUG "       Host: $hostname\n\n";
	return 1;
    }

    return 1
}


#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub debug_globals {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Print globals that RUST is using.  Don't do this sub
    #           until Rust knows which queue it is using (ie after
    #           the which_queue sub).
    #

    my ($index) = @_;

    if ($globals->{'do_debugging'}) {
	print DEBUG ("=" x 75, "\n",
		     "GLOBAL VARIABLE PRINTOUT\n\n".
		     'number_of_queues.....',$globals->{'number_of_queues'},"\n",
		     'alias_name...........',Rust_Configs::extract_element('alias_name',$index),"\n",
		     'alias_title..........',Rust_Configs::extract_element('alias_title',$index),"\n",
		     'alias_dist...........',Rust_Configs::extract_element('alias_dist',$index),"\n",
		     'alias_email..........',Rust_Configs::extract_element('alias_email_addr',$index),"\n",
		     'tag..................',Rust_Configs::extract_element('tag',$index),"\n",
		     'tag_number...........',Rust_Configs::extract_element('tag_number',$index),"\n",
		     'tag_queue............',Rust_Configs::extract_element('tag_queue',$index),"\n",
		     'name_file............',Rust_Configs::extract_element('name_file', $index),"\n",
		     'top_level............',$globals->{'top_level'},"\n",
		     'path_to_scripts......',$globals->{'path_to_scripts'},"\n",
		     'path_to_debug........',$globals->{'path_to_debug'},"\n",
		     'path_to_error........',$globals->{'path_to_error'},"\n",
		     'path_to_etc..........',$globals->{'path_to_etc'},"\n",
		     'path_to_tmp..........',$globals->{'path_to_tmp'},"\n",
		     'path_to_log..........',$globals->{'path_to_log'},"\n",
		     'path_to_headers......',$globals->{'path_to_headers'},"\n",
		     'path_to_active.......',$globals->{'path_to_active'},"\n",
		     'path_to_resolved.....',$globals->{'path_to_resolved'},"\n",
		     'path_to_archived.....',$globals->{'path_to_archived'},"\n",
                     'signature_file.......',$globals->{'signature_file'},"\n",
		     'hostname.............',$globals->{'hostname'},"\n",
		     'admin................',$globals->{'admin'},"\n",
		     'do_initial_autoreply.',Rust_Configs::extract_element('do_initial_autoreply', $index),"\n",
		     'do_logging...........',$globals->{'do_logging'},"\n",
		     'do_assign_autoreply..',Rust_Configs::extract_element('do_assign_autoreply', $index),"\n",
		     'do_debugging.........',$globals->{'do_debugging'},"\n",
		     'send_from_username...',Rust_Configs::extract_element('send_from_username', $index),"\n",
		     'take_initial_mail....',Rust_Configs::extract_element('take_initial_mail', $index),"\n",
		     'send_to_humans.......',Rust_Configs::extract_element('send_to_humans', $index),"\n",
		     'send_on_assign.......',Rust_Configs::extract_element('send_on_assign', $index),"\n",
		     'no_unowned_resolved..',Rust_Configs::extract_element('no_unowned_resolved', $index),"\n",
		     "=" x 75, "\n\n");
    }
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub debug {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Write to the debug file whatever is passed into this function,
    #           but only do it if we want debugging
    #

    print DEBUG @_;
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub ddebug {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Write to the debug file whatever is passed into this function,
    #           but only do it if we want full debugging (debug level = 2)
    #

    print DEBUG @_ if ($globals->{'do_debugging'} == 2);
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
sub end_debugging {
    #
    # Type: Can be called by external scripts
    #
    # Abstract: Close the debug file if we were doing debugging.
    #           Also put a timestamp on the bottom of the file
    #

    my ($date) = &main::ctime($^T);

    if($globals->{'do_debugging'}) {
	print DEBUG "\nRUST debugging: terminated at ", $date, "\n";
	close (DEBUG);
    }
}
} 
1;

# EOF
