summaryrefslogtreecommitdiff
path: root/kernel/xpp/utils/zconf/Zaptel/Config/Params.pm
blob: 7f6ae80d826e43aa586fa838a5e1c1b943194d54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package Zaptel::Config::Params;
#
# Written by Oron Peled <oron@actcom.co.il>
# Copyright (C) 2009, Xorcom
# This program is free software; you can redistribute and/or
# modify it under the same terms as Perl itself.
#
# $Id$
#
use strict;

=head1 NAME

Zaptel::Config::Params -- Object oriented representation of F<genconf_parameters> file.

=head1 SYNOPSIS

 use Zaptel::Config::Params;
 my $params = Zaptel::Config::Params->new('the-config-file');
 print $params->item{'some-key'};
 $params->dump;	# For debugging

=head1 DESCRIPTION

The constructor must be given a configuration file name:

=over 4

=item * Missing file is B<not> an error.

=item * Other opening errors cause a C<die> to be thrown.

=item * The file name is saved as the value of C<GENCONF_FILE> key.

=back

The access to config keys should only be done via the C<item()> method:

=over 4

=item * It contains all hard-coded defaults.

=item * All these values are overriden by directives in the config file.

=back

=cut

sub new($$) {
	my $pack = shift || die;
	my $cfg_file = shift || die;
	my $self = {
			GENCONF_FILE	=> $cfg_file,
		};
	bless $self, $pack;
	if(!open(F, $cfg_file)) {
		if(defined($!{ENOENT})) {
			#print STDERR "No $cfg_file. Assume empty config\n";
			return $self; # Empty configuration
		}
		die "$pack: Failed to open '$cfg_file': $!\n";
	}
	#print STDERR "$pack: $cfg_file\n";
	my $array_key;
	while(<F>) {
		my ($key, $val);
		chomp;
		s/#.*$//;
		s/\s+$//;	# trim tail whitespace
		next unless /\S/;
		if(defined $array_key && /^\s+/) {
			s/^\s+//;	# trim beginning whitespace
			push(@{$self->{$array_key}}, $_);
			next; 
		}
		undef $array_key;
		($key, $val) = split(/\s+/, $_, 2);
		$key = lc($key);
		if(! defined $val) {
			$array_key = $key;
			next;
		}
		die "$cfg_file:$.: Duplicate key '$key'\n", if exists $self->{$key};
		$self->{$key} = $val;
	}
	close F;
	return $self;
}

sub item($$) {
	my $self = shift || die;
	my $key = shift || die;
	my %defaults = (
			base_exten		=> '4000',
			freepbx			=> 'no',	# Better via -F command line
			fxs_immediate		=> 'no',
			fxs_default_start	=> 'ks',
			fxo_default_start	=> 'ks',
			lc_country		=> 'us',
			context_lines		=> 'from-pstn',
			context_phones		=> 'from-internal',
			context_input		=> 'astbank-input',
			context_output		=> 'astbank-output',
			group_phones		=> '5',
			group_lines		=> '0',
			brint_overlap		=> 'no',
			bri_sig_style		=> 'bri_ptmp',
			echo_can		=> 'mg2',
			bri_hardhdlc		=> 'no',
			pri_connection_type	=> 'PRI',
			r2_idle_bits		=> '1101',
			'pri_termtype'		=> [ 'SPAN/* TE' ],
		);

	return (exists($self->{$key})) ? $self->{$key} :$defaults{$key};
}

sub dump($) {
	my $self = shift || die;
	printf STDERR "%s dump:\n", ref $self;
	my $width = 30;
	foreach my $k (sort keys %$self) {
		my $val = $self->{$k};
		my $ref = ref $val;
		#print STDERR "DEBUG: '$k', '$ref', '$val'\n";
		if($ref eq '') {
			printf STDERR "%-${width}s %s\n", $k, $val;
		} elsif($ref eq 'SCALAR') {
			printf STDERR "%-${width}s %s\n", $k, ${$val};
		} elsif($ref eq 'ARRAY') {
			#printf STDERR "%s:\n", $k;
			my $i = 0;
			foreach my $v (@{$val}) {
				printf STDERR "%-${width}s %s\n", "$k\->[$i]", $v;
				$i++;
			}
		} elsif($ref eq 'HASH') {
			#printf STDERR "%s:\n", $k;
			foreach my $k1 (keys %{$val}) {
				printf STDERR "%-${width}s %s\n", "$k\->\{$k1\}", ${$val}{$k1};
			}
		} else {
			printf STDERR "%-${width}s (-> %s)\n", $k, $ref;
		}
	}
}

1;