summaryrefslogtreecommitdiff
path: root/kernel/xpp/utils/zconf/Zaptel/Config/Gen/Users.pm
blob: 7c9e937bcede299b76a7835d7f2b709f2281fdb7 (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
package Zaptel::Config::Gen::Users;
use strict;

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

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;
}

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 = $gconfig->{'base_exten'} + $num;
	my $sig = $gconfig->{'zapata_signalling'}{$type};
	my $full_name = "$type $num";

	die "missing default_zapata_signalling for chan #$num type $type" unless $sig;
	print << "EOF";
[$exten]
callwaiting = yes
context = numberplan-custom-1
fullname = $full_name
cid_number = $exten
hasagent = no
hasdirectory = no
hasiax = no
hasmanager = no
hassip = no
hasvoicemail = yes
host = dynamic
mailbox = $exten
threewaycalling = yes
vmsecret = 1234
secret = 1234
signalling = $sig
zapchan = $num
registeriax = no
registersip = no
canreinvite = no
nat = no
dtmfmode = rfc2833
disallow = all
allow = all

EOF
}

# generate users.conf . The specific users.conf is strictly oriented
# towards using with the asterisk-gui .
#
# This code could have generated a much simpler and smaller
# configuration file, had there been minimal level of support for
# configuration templates in the asterisk configuration rewriting. Right
# now Asterisk's configuration rewriting simply freaks out in the face
# of templates: http://bugs.digium.com/11442 .
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)]}
;!
[general]
;
; Full name of a user
;
fullname = New User
;
; 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
	foreach my $span (@spans) {
		next unless grep { $_ eq $span->type} ( 'FXS', 'IN', 'OUT' );
		printf "; Span %d: %s %s\n", $span->num, $span->name, $span->description;
		foreach my $chan ($span->chans()) {
			$self->gen_channel($chan);
		}
		print "\n";
	}
	close F;
	select $old;
}

1;

__END__

=head1 NAME

users - Generate configuration for users.conf.

=head1 SYNOPSIS

 use Zaptel::Config::Gen::Users;

 my $cfg = new Zaptel::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.

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