package ot_const;
use strict;
use vars qw( @ISA @EXPORT_OK $VERSION $testmode %ot %valid %r %f );
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw( %ot %r %f &is &res &is_err $testmode );
#push( @INC, '/data/httpd/lib/perl' );

=head1 NAME

ot_const - Constants for all modules

=head1 SYNOPSIS

This package includes constants for all modules (e.g. error) and some
functions operating on these constants.

=head1 DESCRIPTION

The constants are packed using hashes depending on their use. Functions are
used in order to check, whether a constant property is set or not.

=head2 %ot - general constants

These are general open theory constants. The switch I<$testmode> switches between
test and production. The general constants are:
S<
domain      : domain name
fqdomain    : full qualified name of web domain
maildom     : mail domain name
listmaster  : ot listmaster (email /wo domain)
bb_addr     : identifier for body based mode
sqlhost     : hostname
sqluser     : mysql username
sqlpw       : mysql users password
sqldb       : mysql database name
logpath     : path to logs>

=cut

$testmode = 0;					# switch between test and production

if( $testmode ){				# run on test system
  %ot = (
 	domain => "opentheory.de",		# domain name
	fqdomain => "www.opentheory.de",	# full qualified name of web domain
	maildom => "opentheory.de",		# mail domain name
	listmaster => "listmaster",		# ot listmaster (email /wo domain)
	bb_addr => "ot",			# identifier for body based mode
	sqlhost => "localhost",			# hostname
	sqluser => "otdb",			# mysql username
	sqlpw => "password",			# mysql users password
	sqldb => "otdb",			# mysql database name
	logpath => "/usr/local/httpd/otlog/"	# path to logs
	);
}
else {						# run on production system
  %ot = (
	domain => "opentheory.org",		# domain name
	fqdomain => "www.opentheory.org",	# full qualified name of web domain
	maildom => "opentheory.org",		# mail domain name
	listmaster => "listmaster",		# ot listmaster (email /wo domain)
	bb_addr => "ot",			# identifier for body based mode
	sqlhost => "localhost",			# hostname
	sqluser => "otdb",			# mysql username
	sqlpw => "password",			# mysql users password
	sqldb => "otdb",			# mysql database name
	logpath => "/data/httpd/htdocs_8/otlog/"	# path to logs
	);
}

=head2 %valid - command properties, B<%f> - flags for I<%valid> hash

The I<%valid> hash codes the properties of all commands. Properties are
bitwise coded using the flag hash I<%f>:
S<
00000000 00000000 = 0     : invalid command
00000000 00000001 = 1     : comes with projectname
00000000 00000010 = 2     : cmd for ot-members only
00000000 00000100 = 4     : cmd for pj-members only
00000000 00001000 = 8     : cmd for pj-maintainer only
00000000 00010000 = 16    : has no argument
00000000 00100000 = 32    : has one argument
00000000 01000000 = 64    : has two arguments
00000000 10000000 = 128   : has three arguments
00000001 00000000 = 256   : needs confirmation
00000010 00000000 = 512   : cmd for ml-members only
00000100 00000000 = 1024  : unused
00001000 00000000 = 2048  : command not implemented
00010000 00000000 = 4096  : unused
00100000 00000000 = 8192  : unused
01000000 00000000 = 16384 : unused
10000000 00000000 = 32768 : unused
11111111 11111111 = 65535 : valid command>

NOTE: the I<%valid> hash is an internal structure and can only be accessed
via the funtion I<&is> described below.

The following table shows the coding of the command properties using the
flag hash I<%f>:
S<
Name       | 1 | 2 | 4 | 8 | 16| 32| 64|128|256|512|024|048|
-----------+---+---+---+---+---+---+---+---+---+---+---+---+
which      |   | x |   |   | x |   |   |   |   |   |   |   |
who        | x | x | x |   | x |   |   |   |   |   |   |   |
subscribe  | x |   |   |   | x |   |   |   | x |   |   |   |
unsubscribe| x | x | x |   | x |   |   |   | x |   |   |   |
gettree    | x |   |   |   | x |   |   |   |   |   |   | x |
getlists   | x |   |   |   | x |   |   |   |   |   |   | x |
confirm    |   |   |   |   |   | x |   |   |   |   |   |   |
help       |   |   |   |   |   | x |   |   |   |   |   | x |
submit     | x | x | x | x |   | x |   |   | x |   |   | x |
give       | x | x | x | x |   | x |   |   | x |   |   | x |
addsub     | x | x | x | x |   | x |   |   |   |   |   | x |
uselist    | x | x | x | x |   | x |   |   |   |   |   | x |
keywords   | x | x | x | x |   | x |   |   |   |   |   | x |
title      | x | x | x | x |   | x |   |   |   |   |   | x |
description| x | x | x | x |   | x |   |   |   |   |   | x |
setoption  |   | x |   |   |   |   | x |   |   |   |   | x |
comment    | x |   |   |   |   |   | x |   |   |   |   | x |
setname    |   | x |   |   |   |   | x |   |   |   |   | x |
newversion | x | x | x | x |   |   | x |   | x |   |   | x |
newproject |   | x | x |   |   |   |   | x | x |   |   | x |
(maillist) | x | x |   |   |   |   | x |   |   | x |   |   |

The command 'maillist' is not set by user, but set by parse_a.>

=cut

%f = (
	invalid_command	=> 0,
	has_pj_name	=> 1,
	ot_members_only	=> 2,
	pj_members_only	=> 4,
	pj_maint_only	=> 8,
	no_arg		=> 16,
	one_arg		=> 32,
	two_args	=> 64,
	three_args	=> 128,
	to_confirm	=> 256,
	ml_members_only	=> 512,
	not_implemented	=> 2048,
	valid_command	=> 65535,
);

%valid = (
	addsub		=> 47+2048,
	comment 	=> 65+2048,
	confirm		=> 32,
	description	=> 47+2048,
	getlists	=> 17+2048,
	gettree		=> 17+2048,
	give		=> 303+2048,
	help		=> 32+2048,
	keywords	=> 47+2048,
	newproject	=> 390+2048,
	newversion	=> 335+2048,
	setname		=> 66+2048,
	setoption	=> 66+2048,
	submit		=> 303+2048,
	subscribe	=> 273,
	title		=> 47+2048,
	unsubscribe	=> 279,
	uselist		=> 47,
	which		=> 18,
	who		=> 23,
	maillist	=> 579
);

=head2 &is - checks if flag is set

The function I<&is> checks, whether in a command a flag described by I<%f> is set or
not. This means the question, if a command has a property or not.

S<in-param:	$cmd - command
in-param:	$flag - flag coding the command property
returns:	true (flag is set) / false (flag is not set)>

=cut

sub is {
	my( $cmd, $flag ) = @_;
	return((( $valid{$cmd} || $f{invalid_command} ) & $flag ) > 0 );
}

=head2 %r - result codes

All result codes used in all packages are defined by the I<%r> hash. Zero or
negative values indicate errors, positive values indicate normal processing.
However, values can be handled opaque and evaluated by functions I<&is>,
I<&res> and I&<is_err>.

	ok_finished	: processing finished
	ok_confirmed	: normal processing, user confirmed cmd
	ok_bb_mode	: normal processing, body based mode
	ok_ab_mode	: normal processing, address based mode
	ok_nothing_done	: normal processing, but nothing done
	ok		: normal processing
	ok_requested	: normal processing, confirmation is requested
	ok_to_confirm	: normal processing, user has to confirm
	errors		: from here error section starts
	bcc_not_allowed	: bbc-mailing is not allowed
	mixed_mode	: mail mixes ab- and bb-mode
	not_a_command	: addr is not a command (no '*', no 'ot')
	invalid_command	: invalid command
	missing_pjname	: missing project name
	invalid_pjname	: invalid project name
	sh_never_happen	: this error should never happen ;-)
	not_ot_member	: user is not ot-member
	not_pj_member	: user is not project member
	not_maintainer	: user is not project maintainer
	yet_maintainer	: user is yet project maintainer
	new_not_pj_memb	: new maintainer is not project member
	hasn_maillist	: user has not given mailinglist
	nothing_confirm	: there is nothing to confirm
	already_ot_memb	: user is already ot member
	already_pj_memb	: user is already pj member
	not_implemented	: this command is not yet implemented
	error		: general error, not specified

=cut

%r = (				# list of result codes
	ok_finished	=> 99,	# processing finished
	ok_confirmed	=> 7,	# normal processing, user confirmed cmd
	ok_bb_mode	=> 6,	# normal processing, body based mode
	ok_ab_mode	=> 5,	# normal processing, address based mode
	ok_nothing_done	=> 4,	# normal processing, but nothing done
	ok		=> 3,	# normal processing, evaluation done
	ok_requested	=> 2,	# normal processing, confirmation is requested
	ok_to_confirm	=> 1,	# normal processing, user has to confirm
	errors		=> 0,	# from here error section starts
	bcc_not_allowed	=> -1,	# bbc-mailing is not allowed
	mixed_mode	=> -2,	# mail mixes ab- and bb-mode
	not_a_command	=> -3,	# addr is not a command (no '*', no 'ot')
	invalid_command	=> -4,	# invalid command
	missing_pjname	=> -5,	# missing project name
	invalid_pjname	=> -6,	# invalid project name
	sh_never_happen	=> -7,	# this error should never happen ;-)
	not_ot_member	=> -8,	# user is not ot-member
	not_pj_member	=> -9,	# user is not project member
	not_maintainer	=> -10,	# user is not project maintainer
	yet_maintainer	=> -11,	# user is yet project maintainer
	yet_pj_member	=> -12,	# user is yet project member
	new_not_pj_memb	=> -13,	# new maintainer is not project member
	hasnt_maillist	=> -14,	# user has not given mailinglist
	nothing_confirm	=> -15,	# there is nothing to confirm
	already_ot_memb	=> -16,	# user is already ot member
	already_pj_memb	=> -17,	# user is already pj member
	not_implemented	=> -98,	# this command is not yet implemented
	error		=> -99	# general error, not specified
);

=head2 &is_err - checks, if an error occured

in-param:	$r_val	= result code
returns:	true (an error occured) / false (normal processing)

=cut

sub is_err {
	my( $r_val ) = @_;
	return( $r_val < $r{errors} );
}

=head2 &res - result function, returns key for I<%r> code

in-param:	$r_val	- result code
returns:	$r_key	_ result key

=cut

sub res {
	my( $r_val ) = @_;
	my $r_key = 'key_not_found';	# should never happen;-)
	foreach my $key ( keys %r ){
		if( $r{$key} eq $r_val ) {
			$r_key = $key;
			last;
		}
	}
	return( $r_key );
}

1;
