Debian Codegraph @

xpp/perl_modules/Dahdi/Config/Gen/Users.pm

raw
1 package Dahdi::Config::Gen::Users;
2 use strict;
3
4 use File::Basename;
5 use Dahdi::Config::Gen qw(is_true);
6
7 # Generate a complete users.conf for the asterisk-gui
8 # As the asterisk-gui provides no command-line interface of its own and
9 # no decent support of #include, we have no choice but to nuke users.conf
10 # if we're to provide a working system
11
12 sub new($$$) {
13 my $pack = shift || die;
14 my $gconfig = shift || die;
15 my $genopts = shift || die;
16 my $file = $ENV{USERS_FILE} || "/etc/asterisk/users.conf";
17 my $self = {
18 FILE => $file,
19 GCONFIG => $gconfig,
20 GENOPTS => $genopts,
21 };
22 bless $self, $pack;
23 return $self;
24 }
25
26 # A single analog trunk for all the FXO channels
27 sub gen_analog_trunk {
28 my @fxo_ports = @_;
29 return unless (@fxo_ports); # no ports
30
31 my $ports = join(',', @fxo_ports);
32
33 print << "EOF"
34 [trunk_1]
35 trunkname = analog
36 hasexten = no
37 hasiax = no
38 hassip = no
39 hasregisteriax = no
40 hasregistersip = no
41 trunkstyle = analog
42 dahdichan = $ports
43
44 EOF
45 }
46
47 # A digital trunk for a single span.
48 # FIXME: how do I create the DID context?
49 sub gen_digital_trunk($) {
50 my $span = shift;
51 my $num = $span->num;
52 my $sig = $span->signalling;
53 my $type = $span->type;
54 my $bchan_range = Dahdi::Config::Gen::bchan_range($span);
55
56 print << "EOF";
57 [span_$num]
58 group = $num
59 hasexten = no
60 signalling = $sig
61 trunkname = Span $num $type
62 trunkstyle = digital ; GUI metadata
63 hassip = no
64 hasiax = no
65 context = DID_span_$num
66 dahdichan = $bchan_range
67
68 EOF
69 }
70
71 my $ExtenNum;
72
73 # A single user for a FXS channel
74 sub gen_channel($$) {
75 my $self = shift || die;
76 my $chan = shift || die;
77 my $gconfig = $self->{GCONFIG};
78 my $type = $chan->type;
79 my $num = $chan->num;
80 die "channel $num type $type is not an analog channel\n" if $chan->span->is_digital();
81 my $exten = $ExtenNum++;
82 my $sig = $gconfig->{'chan_dahdi_signalling'}{$type};
83 my $full_name = "$type $num";
84
85 die "missing default_chan_dahdi_signalling for chan #$num type $type" unless $sig;
86 print << "EOF";
87 [$exten]
88 context = DLPN_DialPlan1
89 callwaiting = yes
90 fullname = $full_name
91 cid_number = $exten
92 hasagent = no
93 hasdirectory = no
94 hasiax = no
95 hasmanager = no
96 hassip = no
97 hasvoicemail = yes
98 mailbox = $exten
99 threewaycalling = yes
100 vmsecret = $exten
101 signalling = $sig
102 dahdichan = $num
103 registeriax = no
104 registersip = no
105 canreinvite = no
106
107 EOF
108 }
109
110 sub generate($) {
111 my $self = shift || die;
112 my $file = $self->{FILE};
113 my $gconfig = $self->{GCONFIG};
114 my $genopts = $self->{GENOPTS};
115 #$gconfig->dump;
116 my @spans = @_;
117 warn "Empty configuration -- no spans\n" unless @spans;
118 rename "$file", "$file.bak"
119 or $! == 2 # ENOENT (No dependency on Errno.pm)
120 or die "Failed to backup old config: $!\n";
121 print "Generating $file\n" if $genopts->{verbose};
122 open(F, ">$file") || die "$0: Failed to open $file: $!\n";
123 my $old = select F;
124 print <<"HEAD";
125 ;!
126 ;! Automatically generated configuration file
127 ;! Filename: @{[basename($file)]} ($file)
128 ;! Generator: $0
129 ;! Creation Date: @{[scalar(localtime)]}
130 ;! If you edit this file and execute $0 again,
131 ;! your manual changes will be LOST.
132 ;!
133 [general]
134 ;
135 ; Starting point of allocation of extensions
136 ;
137 userbase = @{[$gconfig->{'base_exten'}+1]}
138 ;
139 ; Create voicemail mailbox and use use macro-stdexten
140 ;
141 hasvoicemail = yes
142 ;
143 ; Set voicemail mailbox @{[$gconfig->{'base_exten'}+1]} password to 1234
144 ;
145 vmsecret = 1234
146 ;
147 ; Create SIP Peer
148 ;
149 hassip = no
150 ;
151 ; Create IAX friend
152 ;
153 hasiax = no
154 ;
155 ; Create Agent friend
156 ;
157 hasagent = no
158 ;
159 ; Create H.323 friend
160 ;
161 ;hash323 = yes
162 ;
163 ; Create manager entry
164 ;
165 hasmanager = no
166 ;
167 ; Remaining options are not specific to users.conf entries but are general.
168 ;
169 callwaiting = yes
170 threewaycalling = yes
171 callwaitingcallerid = yes
172 transfer = yes
173 canpark = yes
174 cancallforward = yes
175 callreturn = yes
176 callgroup = 1
177 pickupgroup = 1
178 localextenlength = @{[length($gconfig->{'base_exten'})]}
179
180
181 HEAD
182 my @fxo_ports = ();
183 $ExtenNum = $self->{GCONFIG}->{'base_exten'};
184 foreach my $span (@spans) {
185 printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description;
186 if ($span->type =~ /^(BRI_(NT|TE)|E1|T1)$/) {
187 gen_digital_trunk($span);
188 next;
189 }
190 foreach my $chan ($span->chans()) {
191 if (grep { $_ eq $span->type} ( 'FXS', 'IN', 'OUT' )) {
192 $self->gen_channel($chan);
193 } elsif ($chan->type eq 'FXO') {
194 # TODO: "$first_chan-$last_chan"
195 push @fxo_ports,($chan->num);
196 }
197 }
198 print "\n";
199 }
200 gen_analog_trunk(@fxo_ports);
201 close F;
202 select $old;
203 }
204
205 1;
206
207 __END__
208
209 =head1 NAME
210
211 users - Generate configuration for users.conf.
212
213 =head1 SYNOPSIS
214
215 use Dahdi::Config::Gen::Users;
216
217 my $cfg = new Dahdi::Config::Gen::Users(\%global_config, \%genopts);
218 $cfg->generate(@span_list);
219
220 =head1 DESCRIPTION
221
222 Generate the F</etc/asterisk/users.conf> which is used by asterisk(1)
223 and AsteriskGUI. This will replace your entire configuration including
224 any SIP/IAX users and trunks you may have set. Thus it's probably only
225 appropriate for an initial setup.
226
227 Its location may be overriden via the environment variable F<USERS_FILE>.