summaryrefslogtreecommitdiff
path: root/xpp/perl_modules/Dahdi/Config/Gen/Users.pm
blob: 05a345fad397e664dade189123c1edfdee48f147 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package Dahdi::Config::Gen::Users;
use strict;

use File::Basename;
use Dahdi::Config::Gen qw(is_true);

# Generate a complete users.conf for the asterisk-gui
# As the asterisk-gui provides no command-line interface of its own and
# no decent support of #include, we have no choice but to nuke users.conf
# if we're to provide a working system

sub new($$$) {
	my $pack = shift || die;
	my $gconfig = shift || die;
	my $genopts = shift || die;
	my $file = $ENV{USERS_FILE} || "/etc/asterisk/users.conf";
	my $self = {
			FILE	=> $file,
			GCONFIG	=> $gconfig,
			GENOPTS	=> $genopts,
		};
	bless $self, $pack;
	return $self;
}

# A single analog trunk for all the FXO channels
sub gen_analog_trunk {
	my @fxo_ports = @_;
	return unless (@fxo_ports); # no ports

	my $ports = join(',', @fxo_ports);

	print << "EOF"
[trunk_1]
trunkname = analog
hasexten = no
hasiax = no
hassip = no
hasregisteriax = no
hasregistersip = no
trunkstyle = analog
dahdichan = $ports

EOF
}

# A digital trunk for a single span.
# FIXME: how do I create the DID context?
sub gen_digital_trunk($) {
	my $span = shift;
	my $num = $span->num;
	my $sig = $span->signalling;
	my $type = $span->type;
	my $bchan_range = Dahdi::Config::Gen::bchan_range($span);

	print << "EOF";
[span_$num]
group = $num
hasexten = no
signalling = $sig
trunkname = Span $num $type
trunkstyle = digital  ; GUI metadata
hassip = no
hasiax = no
context = DID_span_$num
dahdichan = $bchan_range

EOF
}

my $ExtenNum;

# A single user for a FXS channel
sub gen_channel($$) {
	my $self = shift || die;
	my $chan = shift || die;
	my $gconfig = $self->{GCONFIG};
	my $type = $chan->type;
	my $num = $chan->num;
	die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
	my $exten = $ExtenNum++;
	my $sig = $gconfig->{'chan_dahdi_signalling'}{$type};
	my $full_name = "$type $num";

	die "missing default_chan_dahdi_signalling for chan #$num type $type" unless $sig;
	print << "EOF";
[$exten]
context = DLPN_DialPlan1
callwaiting = yes
fullname = $full_name
cid_number = $exten
hasagent = no
hasdirectory = no
hasiax = no
hasmanager = no
hassip = no
hasvoicemail = yes
mailbox = $exten
threewaycalling = yes
vmsecret = $exten
signalling = $sig
dahdichan = $num
registeriax = no
registersip = no
canreinvite = no

EOF
}

sub generate($) {
	my $self = shift || die;
	my $file = $self->{FILE};
	my $gconfig = $self->{GCONFIG};
	my $genopts = $self->{GENOPTS};
	#$gconfig->dump;
	my @spans = @_;
	warn "Empty configuration -- no spans\n" unless @spans;
	rename "$file", "$file.bak"
		or $! == 2	# ENOENT (No dependency on Errno.pm)
		or die "Failed to backup old config: $!\n";
	print "Generating $file\n" if $genopts->{verbose};
	open(F, ">$file") || die "$0: Failed to open $file: $!\n";
	my $old = select F;
	print <<"HEAD";
;!
;! Automatically generated configuration file
;! Filename: @{[basename($file)]} ($file)
;! Generator: $0
;! Creation Date: @{[scalar(localtime)]}
;! If you edit this file and execute $0 again,
;! your manual changes will be LOST.
;!
[general]
;
; Starting point of allocation of extensions
;
userbase = @{[$gconfig->{'base_exten'}+1]}
;
; Create voicemail mailbox and use use macro-stdexten
;
hasvoicemail = yes
;
; Set voicemail mailbox @{[$gconfig->{'base_exten'}+1]} password to 1234
;
vmsecret = 1234
;
; Create SIP Peer
;
hassip = no
;
; Create IAX friend
;
hasiax = no
;
; Create Agent friend
;
hasagent = no
;
; Create H.323 friend
;
;hash323 = yes
;
; Create manager entry
;
hasmanager = no
;
; Remaining options are not specific to users.conf entries but are general.
;
callwaiting = yes
threewaycalling = yes
callwaitingcallerid = yes
transfer = yes
canpark = yes
cancallforward = yes
callreturn = yes
callgroup = 1
pickupgroup = 1
localextenlength = @{[length($gconfig->{'base_exten'})]}


HEAD
	my @fxo_ports = ();
	$ExtenNum = $self->{GCONFIG}->{'base_exten'};
	foreach my $span (@spans) {
		printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description;
		if ($span->type =~ /^(BRI_(NT|TE)|E1|T1)$/) {
			gen_digital_trunk($span);
			next;
		}
		foreach my $chan ($span->chans()) {
			if (grep { $_ eq $span->type} ( 'FXS', 'IN', 'OUT' )) {
				$self->gen_channel($chan);
			} elsif ($chan->type eq 'FXO') {
				# TODO: "$first_chan-$last_chan"
				push @fxo_ports,($chan->num);
			}
		}
		print "\n";
	}
	gen_analog_trunk(@fxo_ports);
	close F;
	select $old;
}

1;

__END__

=head1 NAME

users - Generate configuration for users.conf.

=head1 SYNOPSIS

 use Dahdi::Config::Gen::Users;

 my $cfg = new Dahdi::Config::Gen::Users(\%global_config, \%genopts);
 $cfg->generate(@span_list);

=head1 DESCRIPTION

Generate the F</etc/asterisk/users.conf> which is used by asterisk(1) 
and AsteriskGUI. This will replace your entire configuration including
any SIP/IAX users and trunks you may have set. Thus it's probably only
appropriate for an initial setup.

Its location may be overriden via the environment variable F<USERS_FILE>.