1) Added sounds needed for authentication app.
[project/luci.git] / applications / luci-pbx / root / etc / init.d / pbx-asterisk
1 #!/bin/sh /etc/rc.common
2 #
3 # Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
4 #
5 # This file is part of luci-pbx.
6 #
7 # luci-pbx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # luci-pbx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with luci-pbx. If not, see <http://www.gnu.org/licenses/>.
19
20 . /etc/functions.sh
21
22 START=60
23
24 # Some global variables
25 MODULENAME=pbx
26 USERAGENT="PBX"
27 HANGUPCNTXT=hangup-call-context
28 GTALKUNVL=unavailable
29
30 ASTUSER=nobody
31 ASTGROUP=nogroup
32 ASTDIRSRECURSIVE="/var/run/asterisk /var/log/asterisk /var/spool/asterisk"
33 ASTDIRS="/usr/lib/asterisk"
34 ASTSOUNDSDIR="/usr/lib/asterisk/sounds"
35
36 TEMPLATEDIR=/etc/${MODULENAME}-asterisk
37 PBXSOUNDSDIR=$TEMPLATEDIR/sounds
38 VMTEMPLATEDIR=/etc/${MODULENAME}-voicemail
39 VMSOUNDSDIR=$VMTEMPLATEDIR/sounds
40 ASTERISKDIR=/etc/asterisk
41 WORKDIR=/tmp/$MODULENAME.$$
42 MD5SUMSFILE=/tmp/$MODULENAME-sums.$$
43
44 TMPL_ASTERISK=$TEMPLATEDIR/asterisk.conf.TEMPLATE
45 TMPL_GTALK=$TEMPLATEDIR/gtalk.conf.TEMPLATE
46 TMPL_INDICATIONS=$TEMPLATEDIR/indications.conf.TEMPLATE
47 TMPL_LOGGER=$TEMPLATEDIR/logger.conf.TEMPLATE
48 TMPL_MANAGER=$TEMPLATEDIR/manager.conf.TEMPLATE
49 TMPL_MODULES=$TEMPLATEDIR/modules.conf.TEMPLATE
50 TMPL_RTP=$TEMPLATEDIR/rtp.conf.TEMPLATE
51
52 TMPL_EXTCTHRUCHECKHDR=$TEMPLATEDIR/extensions_disa-check_header.conf.TEMPLATE
53 TMPL_EXTCTHRUCHECK=$TEMPLATEDIR/extensions_disa-check.conf.TEMPLATE
54 TMPL_EXTCTHRUCHECKFTR=$TEMPLATEDIR/extensions_disa-check_footer.conf.TEMPLATE
55 TMPL_EXTCTHRUHDR=$TEMPLATEDIR/extensions_disa_header.conf.TEMPLATE
56 TMPL_EXTCTHRU=$TEMPLATEDIR/extensions_disa.conf.TEMPLATE
57
58 TMPL_EXTENSIONS=$TEMPLATEDIR/extensions.conf.TEMPLATE
59
60 TMPL_EXTVMDISABLED=$TEMPLATEDIR/extensions_voicemail_disabled.conf.TEMPLATE
61 TMPL_EXTVMENABLED=$TEMPLATEDIR/extensions_voicemail_enabled.conf.TEMPLATE
62
63 TMPL_EXTBLKLIST=$TEMPLATEDIR/extensions_blacklist.conf.TEMPLATE
64 TMPL_EXTBLKLISTFTR=$TEMPLATEDIR/extensions_blacklist_footer.conf.TEMPLATE
65 TMPL_EXTBLKLISTHDR=$TEMPLATEDIR/extensions_blacklist_header.conf.TEMPLATE
66
67 TMPL_EXTDEFAULT=$TEMPLATEDIR/extensions_default.conf.TEMPLATE
68 TMPL_EXTDEFAULTUSER=$TEMPLATEDIR/extensions_default_user.conf.TEMPLATE
69
70 TMPL_EXTINCNTXTSIP=$TEMPLATEDIR/extensions_incoming_context_sip.conf.TEMPLATE
71 TMPL_EXTINCNTXTGTALKHDR=$TEMPLATEDIR/extensions_incoming_context_gtalk_header.conf.TEMPLATE
72 TMPL_EXTINCNTXTGTALK=$TEMPLATEDIR/extensions_incoming_context_gtalk.conf.TEMPLATE
73
74 TMPL_EXTUSERCNTXT=$TEMPLATEDIR/extensions_user_context.conf.TEMPLATE
75 TMPL_EXTUSERCNTXTFTR=$TEMPLATEDIR/extensions_user_context_footer.conf.TEMPLATE
76 TMPL_EXTUSERCNTXTHDR=$TEMPLATEDIR/extensions_user_context_header.conf.TEMPLATE
77
78 TMPL_EXTOUTHDR=$TEMPLATEDIR/extensions_default_outgoing_header.conf.TEMPLATE
79 TMPL_EXTOUTGTALK=$TEMPLATEDIR/extensions_outgoing_gtalk.conf.TEMPLATE
80 TMPL_EXTOUTLOCAL=$TEMPLATEDIR/extensions_outgoing_dial_local_user.conf.TEMPLATE
81 TMPL_EXTOUTSIP=$TEMPLATEDIR/extensions_outgoing_sip.conf.TEMPLATE
82
83 TMPL_JABBER=$TEMPLATEDIR/jabber.conf.TEMPLATE
84 TMPL_JABBERUSER=$TEMPLATEDIR/jabber_users.conf.TEMPLATE
85 TMPL_SIP=$TEMPLATEDIR/sip.conf.TEMPLATE
86 TMPL_SIPPEER=$TEMPLATEDIR/sip_peer.TEMPLATE
87 TMPL_SIPREG=$TEMPLATEDIR/sip_registration.TEMPLATE
88 TMPL_SIPUSR=$TEMPLATEDIR/sip_user.TEMPLATE
89
90 TMPL_MSMTPDEFAULT=$VMTEMPLATEDIR/pbx-msmtprc-defaults.TEMPLATE
91 TMPL_MSMTPACCOUNT=$VMTEMPLATEDIR/pbx-msmtprc-account.TEMPLATE
92 TMPL_MSMTPAUTH=$VMTEMPLATEDIR/pbx-msmtprc-account-auth.TEMPLATE
93 TMPL_MSMTPACCTDFLT=$VMTEMPLATEDIR/pbx-msmtprc-account-default.TEMPLATE
94
95
96 INCLUDED_FILES="$WORKDIR/extensions_blacklist.conf $WORKDIR/extensions_callthrough.conf\
97 $WORKDIR/extensions_incoming.conf $WORKDIR/extensions_incoming_gtalk.conf\
98 $WORKDIR/extensions_user.conf $WORKDIR/jabber_users.conf\
99 $WORKDIR/sip_peers.conf $WORKDIR/sip_registrations.conf\
100 $WORKDIR/sip_users.conf $WORKDIR/extensions_voicemail.conf\
101 $WORKDIR/extensions_default.conf"
102
103
104 # In this string, we concatenate all local users enabled to receive calls
105 # readily formatted for the Dial command.
106 localusers_to_ring=""
107
108 # In this string, we keep a list of all users that are enabled for outgoing
109 # calls. It is used at the end to create the user contexts.
110 localusers_can_dial=""
111
112 # In this string, we put together a space-separated list of provider names
113 # (alphanumeric, with all non-alpha characters replaced with underscores),
114 # which will be used to dial out by default (whose outgoing contexts will
115 # be included in users' contexts by default.
116 outbound_providers=""
117
118 # Copies the template files which we don't edit.
119 copy_unedited_templates_over()
120 {
121 cp $TMPL_ASTERISK $WORKDIR/asterisk.conf
122 cp $TMPL_GTALK $WORKDIR/gtalk.conf
123 cp $TMPL_INDICATIONS $WORKDIR/indications.conf
124 cp $TMPL_LOGGER $WORKDIR/logger.conf
125 cp $TMPL_MANAGER $WORKDIR/manager.conf
126 cp $TMPL_MODULES $WORKDIR/modules.conf
127 # If this file isn't present at this stage, voicemail is disabled.
128 [ ! -f $WORKDIR/extensions_voicemail.conf ] && \
129 cp $TMPL_EXTVMDISABLED $WORKDIR/extensions_voicemail.conf
130 }
131
132 # Touches all the included files, to prevent asterisk from refusing to
133 # start if a config item is missing and an included config file isn't created.
134 create_included_files()
135 {
136 touch $INCLUDED_FILES
137 }
138
139 # Puts together all the extensions.conf related configuration.
140 pbx_create_extensions_config()
141 {
142 local ringtime
143 config_get ringtime advanced ringtime
144
145 sed "s/|RINGTIME|/$ringtime/" $TMPL_EXTENSIONS > $WORKDIR/extensions.conf
146 mv $WORKDIR/inext.TMP $WORKDIR/extensions_incoming.conf
147 cp $TMPL_EXTINCNTXTGTALKHDR $WORKDIR/extensions_incoming_gtalk.conf
148 cat $WORKDIR/outextgtalk.TMP >> $WORKDIR/extensions_incoming_gtalk.conf 2>/dev/null
149 rm -f $WORKDIR/outextgtalk.TMP
150 mv $WORKDIR/blacklist.TMP $WORKDIR/extensions_blacklist.conf
151 mv $WORKDIR/userext.TMP $WORKDIR/extensions_user.conf
152 cp $TMPL_EXTCTHRUHDR $WORKDIR/extensions_callthrough.conf
153 cat $WORKDIR/callthrough.TMP >> $WORKDIR/extensions_callthrough.conf 2>/dev/null
154 rm -f $WORKDIR/callthrough.TMP
155 cat $TMPL_EXTCTHRUCHECKHDR >> $WORKDIR/extensions_callthrough.conf 2>/dev/null
156 cat $WORKDIR/callthroughcheck.TMP >> $WORKDIR/extensions_callthrough.conf 2>/dev/null
157 rm -f $WORKDIR/callthroughcheck.TMP
158 cat $TMPL_EXTCTHRUCHECKFTR >> $WORKDIR/extensions_callthrough.conf 2>/dev/null
159 rm -f $WORKDIR/outext-*.TMP
160 rm -f $WORKDIR/localext.TMP
161 sed "s/|LOCALUSERS|/$localusers_to_ring/g" $TMPL_EXTDEFAULT \
162 > $WORKDIR/extensions_default.conf
163 cat $WORKDIR/inextuser.TMP >> $WORKDIR/extensions_default.conf
164 rm -f $WORKDIR/inextuser.TMP
165 }
166
167 # Puts together all the sip.conf related configuration.
168 pbx_create_sip_config()
169 {
170 mv $WORKDIR/sip_regs.TMP $WORKDIR/sip_registrations.conf
171 mv $WORKDIR/sip_peers.TMP $WORKDIR/sip_peers.conf
172 mv $WORKDIR/sip_users.TMP $WORKDIR/sip_users.conf
173 }
174
175 # Creates the jabber.conf related config
176 pbx_create_jabber_config()
177 {
178 cp $TMPL_JABBER $WORKDIR/jabber.conf
179 mv $WORKDIR/jabber.TMP $WORKDIR/jabber_users.conf
180 }
181
182 # Gets rid of any config files from $ASTERISKDIR not found in $WORKDIR.
183 clean_up_asterisk_config_dir()
184 {
185 for f in $ASTERISKDIR/* ; do
186 basef="`basename $f`"
187 if [ ! -e "$WORKDIR/$basef" ] ; then
188 rm -rf "$f"
189 fi
190 done
191 }
192
193 # Compares md5sums of the config files in $WORKDIR to those
194 # in $ASTERISKDIR, and copies only changed files over to reduce
195 # wear on flash in embedded devices.
196 compare_configs_and_copy_changed()
197 {
198 # First, compute md5sums of the config files in $WORKDIR.
199 cd $WORKDIR/
200 md5sum * > $MD5SUMSFILE
201
202 # Now, check the files in $ASTERISKDIR against the md5sums.
203 cd $ASTERISKDIR/
204 changed_files="`md5sum -c $MD5SUMSFILE 2>/dev/null | fgrep ": FAILED" | awk -F: '{print $1}'`"
205
206 rm -f $MD5SUMSFILE
207
208 [ -z "$changed_files" ] && return
209
210 # Now copy over the changed files.
211 for f in $changed_files ; do
212 cp "$WORKDIR/$f" "$ASTERISKDIR/$f"
213 done
214 }
215
216 # Calls the functions that create the final config files
217 # Calls the function which compares which files have changed
218 # Puts the final touches on $ASTERISKDIR
219 # Gets rid of $WORKDIR
220 pbx_assemble_and_copy_config()
221 {
222 mkdir -p $ASTERISKDIR
223
224 copy_unedited_templates_over
225 create_included_files
226 pbx_create_extensions_config
227 pbx_create_sip_config
228 pbx_create_jabber_config
229
230 touch $WORKDIR/features.conf
231
232 # At this point, $WORKDIR should contain a complete, working config.
233 clean_up_asterisk_config_dir
234
235 compare_configs_and_copy_changed
236
237 [ ! -d $ASTERISKDIR/manager.d ] && mkdir -p $ASTERISKDIR/manager.d/
238
239 # Get rid of the working directory
240 rm -rf $WORKDIR/
241 }
242
243 # Creates configuration for a user and adds it to the temporary file that holds
244 # all users configured so far.
245 pbx_add_user()
246 {
247 local fullname
248 local defaultuser
249 local secret
250 local ring
251 local can_call
252
253 config_get fullname $1 fullname
254 config_get defaultuser $1 defaultuser
255 config_get secret $1 secret
256 config_get ring $1 ring
257 config_get can_call $1 can_call
258
259 [ -z "$defaultuser" -o -z "$secret" ] && return
260 [ -z "$fullname" ] && fullname="$defaultuser"
261
262 sed "s/|DEFAULTUSER|/$defaultuser/g" $TMPL_SIPUSR > $WORKDIR/sip_user.tmp
263
264 if [ "$can_call" = "yes" ] ; then
265 # Add user to list of all users that are allowed to make calls.
266 localusers_can_dial="$localusers_can_dial $defaultuser"
267 sed -i "s/|CONTEXTNAME|/$defaultuser/g" $WORKDIR/sip_user.tmp
268 else
269 sed -i "s/|CONTEXTNAME|/$HANGUPCNTXT/g" $WORKDIR/sip_user.tmp
270 fi
271
272 # Add this user's configuration to the temp file containing all user configs.
273 sed "s/|FULLNAME|/$fullname/" $WORKDIR/sip_user.tmp |\
274 sed "s/|SECRET|/$secret/g" >> $WORKDIR/sip_users.TMP
275
276 if [ "$ring" = "yes" ] ; then
277 if [ -z "$localusers_to_ring" ] ; then
278 localusers_to_ring="SIP\/$defaultuser"
279 else
280 localusers_to_ring="$localusers_to_ring\&SIP\/$defaultuser"
281 fi
282 fi
283
284 # Add configuration which allows local users to call each other.
285 sed "s/|DEFAULTUSER|/$defaultuser/g" $TMPL_EXTOUTLOCAL >> $WORKDIR/localext.TMP
286
287 # Add configuration which puts calls to users through the default
288 # context, so that blacklists and voicemail take effect for this user.
289 sed "s/|DEFAULTUSER|/$defaultuser/g" $TMPL_EXTDEFAULTUSER >> $WORKDIR/inextuser.TMP
290
291 rm -f $WORKDIR/sip_user.tmp
292 }
293
294 # Creates configuration for a Google account, and adds it to the temporary file that holds
295 # all accounts configured so far.
296 # Also creates the outgoing extensions which are used in users' outgoing contexts.
297 pbx_add_jabber()
298 {
299 local username
300 local secret
301 local numprefix
302 local register
303 local make_outgoing_calls
304 local name
305 local users_to_ring
306 local status
307 local statusmessage
308
309 config_get username $1 username
310 config_get secret $1 secret
311 config_get numprefix $1 numprefix
312 config_get register $1 register
313 config_get make_outgoing_calls $1 make_outgoing_calls
314 config_get name $1 name
315 config_get status $1 status
316 config_get statusmessage $1 statusmessage
317
318 [ -z "$username" -o -z "$secret" ] && return
319
320 # Construct a jabber entry for this provider.
321 sed "s/|USERNAME|/$username/g" $TMPL_JABBERUSER |\
322 sed "s/|NAME|/$name/g" > $WORKDIR/jabber.tmp
323
324 if [ "$register" = yes ] ; then
325 # If this provider is enabled for incoming calls, we need to set the
326 # status of the user to something other than unavailable in order to receive calls.
327 sed -i "s/|STATUS|/$status/g" $WORKDIR/jabber.tmp
328 sed -i "s/|STATUSMESSAGE|/\"$statusmessage\"/g" $WORKDIR/jabber.tmp
329
330 users_to_ring="`uci -q get ${MODULENAME}-calls.incoming_calls.$name`"
331 # If no users have been specified to ring, we ring all users enabled for incoming calls.
332 if [ -z "$users_to_ring" ] ; then
333 users_to_ring=$localusers_to_ring
334 else
335 # Else, we cook up a string formatted for the Dial command
336 # with the specified users (SIP/user1&SIP/user2&...). We do it
337 # with set, shift and a loop in order to be more tolerant of ugly whitespace
338 # messes entered by users.
339 set $users_to_ring
340 users_to_ring="SIP\/$1" && shift
341 for u in $@ ; do users_to_ring=$users_to_ring\\\&SIP\\\/$u ; done
342 fi
343
344 # Now, we add this account to the gtalk incoming context.
345 sed "s/|USERNAME|/$username/g" $TMPL_EXTINCNTXTGTALK |\
346 sed "s/|LOCALUSERS|/$users_to_ring/g" >> $WORKDIR/outextgtalk.TMP
347 else
348 sed -i "s/|STATUS|/$GTALKUNVL/g" $WORKDIR/jabber.tmp
349 sed -i "s/|STATUSMESSAGE|/\"\"/g" $WORKDIR/jabber.tmp
350 fi
351
352 # Add this account's configuration to the temp file containing all account configs.
353 sed "s/|SECRET|/$secret/g" $WORKDIR/jabber.tmp >> $WORKDIR/jabber.TMP
354
355 # If this provider is enabled for outgoing calls.
356 if [ "$make_outgoing_calls" = "yes" ] ; then
357
358 numprefix="`uci -q get ${MODULENAME}-calls.outgoing_calls.$name`"
359
360 # If no prefixes are specified, then we use "X" which matches any prefix.
361 [ -z "$numprefix" ] && numprefix="X"
362
363 for p in $numprefix ; do
364 sed "s/|NUMPREFIX|/$p/g" $TMPL_EXTOUTGTALK |\
365 sed "s/|NAME|/$name/g" >> $WORKDIR/outext-$name.TMP
366 done
367
368 # Add this provider to the list of enabled outbound providers.
369 if [ -z "$outbound_providers" ] ; then
370 outbound_providers="$name"
371 else
372 outbound_providers="$outbound_providers $name"
373 fi
374 fi
375
376 rm -f $WORKDIR/jabber.tmp
377 }
378
379 # Creates configuration for a SIP provider account, and adds it to the temporary file that holds
380 # all accounts configured so far.
381 # Also creates the outgoing extensions which are used in users' outgoing contexts.
382 pbx_add_peer()
383 {
384 local defaultuser
385 local secret
386 local host
387 local fromdomain
388 local register
389 local numprefix
390 local make_outgoing_calls
391 local name
392 local users_to_ring
393 local port
394 local outboundproxy
395
396 config_get defaultuser $1 defaultuser
397 config_get secret $1 secret
398 config_get host $1 host
399 config_get port $1 port
400 config_get outbountproxy $1 outboundproxy
401 config_get fromdomain $1 fromdomain
402 config_get register $1 register
403 config_get numprefix $1 numprefix
404 config_get make_outgoing_calls $1 make_outgoing_calls
405 config_get name $1 name
406
407 [ -z "$defaultuser" -o -z "$secret" -o -z "$host" ] && return
408 [ -z "$fromdomain" ] && fromdomain=$host
409 [ -n "$port" ] && port="port=$port"
410 [ -n "$outboundproxy" ] && outboundproxy="outboundproxy=$outboundproxy"
411
412 # Construct a sip peer entry for this provider.
413 sed "s/|DEFAULTUSER|/$defaultuser/" $TMPL_SIPPEER > $WORKDIR/sip_peer.tmp
414 sed -i "s/|NAME|/$name/" $WORKDIR/sip_peer.tmp
415 sed -i "s/|FROMUSER|/$defaultuser/" $WORKDIR/sip_peer.tmp
416 sed -i "s/|SECRET|/$secret/" $WORKDIR/sip_peer.tmp
417 sed -i "s/|HOST|/$host/" $WORKDIR/sip_peer.tmp
418 sed -i "s/|PORT|/$port/" $WORKDIR/sip_peer.tmp
419 sed -i "s/|OUTBOUNDPROXY|/$outboundproxy/" $WORKDIR/sip_peer.tmp
420 # Add this account's configuration to the temp file containing all account configs.
421 sed "s/|FROMDOMAIN|/$host/" $WORKDIR/sip_peer.tmp >> $WORKDIR/sip_peers.TMP
422
423 # If this provider is enabled for incoming calls.
424 if [ "$register" = "yes" ] ; then
425 # Then we create a registration string for this provider.
426 sed "s/|DEFAULTUSER|/$defaultuser/g" $TMPL_SIPREG > $WORKDIR/sip_reg.tmp
427 sed -i "s/|SECRET|/$secret/g" $WORKDIR/sip_reg.tmp
428 sed "s/|NAME|/$name/g" $WORKDIR/sip_reg.tmp >> $WORKDIR/sip_regs.TMP
429
430 users_to_ring="`uci -q get ${MODULENAME}-calls.incoming_calls.$name`"
431 # If no users have been specified to ring, we ring all users enabled for incoming calls.
432 if [ -z "$users_to_ring" ] ; then
433 users_to_ring=$localusers_to_ring
434 else
435 # Else, we cook up a string formatted for the Dial command
436 # with the specified users (SIP/user1&SIP/user2&...). We do it
437 # with set, shift and a loop in order to be more tolerant of ugly whitespace
438 # messes entered by users.
439 set $users_to_ring
440 users_to_ring="SIP\/$1" && shift
441 for u in $@ ; do users_to_ring=$users_to_ring\\\&SIP\\\/$u ; done
442 fi
443
444 # And we create an incoming calls context for this provider.
445 sed "s/|NAME|/$name/g" $TMPL_EXTINCNTXTSIP |\
446 sed "s/|LOCALUSERS|/$users_to_ring/g" >> $WORKDIR/inext.TMP
447 fi
448
449 # If this provider is enabled for outgoing calls.
450 if [ "$make_outgoing_calls" = "yes" ] ; then
451
452 numprefix="`uci -q get ${MODULENAME}-calls.outgoing_calls.$name`"
453 # If no prefixes are specified, then we use "X" which matches any prefix.
454 [ -z "$numprefix" ] && numprefix="X"
455 for p in $numprefix ; do
456 sed "s/|NUMPREFIX|/$p/g" $TMPL_EXTOUTSIP |\
457 sed "s/|NAME|/$name/g" >> $WORKDIR/outext-$name.TMP
458 done
459
460 # Add this provider to the list of enabled outbound providers.
461 if [ -z "$outbound_providers" ] ; then
462 outbound_providers="$name"
463 else
464 outbound_providers="$outbound_providers $name"
465 fi
466 fi
467
468 rm -f $WORKDIR/sip_peer.tmp
469 rm -f $WORKDIR/sip_reg.tmp
470 }
471
472 # For all local users enabled for outbound calls, creates a context
473 # containing the extensions for Google and SIP accounts this user is
474 # allowed to use.
475 pbx_create_user_contexts()
476 {
477 local providers
478
479 for u in $localusers_can_dial ; do
480 sed "s/|DEFAULTUSER|/$u/g" $TMPL_EXTUSERCNTXTHDR >> $WORKDIR/userext.TMP
481 cat $WORKDIR/localext.TMP >> $WORKDIR/userext.TMP
482 providers="`uci -q get ${MODULENAME}-calls.providers_user_can_use.$u`"
483 [ -z "$providers" ] && providers="$outbound_providers"
484
485 # For each provider, cat the contents of outext-$name.TMP into the user's outgoing calls extension
486 for p in $providers ; do
487 [ -f $WORKDIR/outext-$p.TMP ] && cat $WORKDIR/outext-$p.TMP >> $WORKDIR/userext.TMP
488 done
489 cat $TMPL_EXTUSERCNTXTFTR >> $WORKDIR/userext.TMP
490 done
491 }
492
493 # Creates the blacklist context which hangs up on blacklisted numbers.
494 pbx_add_blacklist()
495 {
496 local blacklist1
497 local blacklist2
498
499 config_get blacklist1 blacklisting blacklist1
500 config_get blacklist2 blacklisting blacklist2
501
502 # We create the blacklist context no matter whether the blacklist
503 # actually contains entries or not, since the PBX will send calls
504 # to the context for a check against the list anyway.
505 cp $TMPL_EXTBLKLISTHDR $WORKDIR/blacklist.TMP
506 for n in $blacklist1 $blacklist2 ; do
507 sed "s/|BLACKLISTITEM|/$n/g" $TMPL_EXTBLKLIST >> $WORKDIR/blacklist.TMP
508 done
509 cat $TMPL_EXTBLKLISTFTR >> $WORKDIR/blacklist.TMP
510 }
511
512 # Creates the callthrough context which allows specified numbers to get
513 # into the PBX and dial out as the configured user.
514 pbx_add_callthrough()
515 {
516 local callthrough_number_list
517 local defaultuser
518 local pin
519 local enabled
520
521 config_get callthrough_number_list $1 callthrough_number_list
522 config_get defaultuser $1 defaultuser
523 config_get pin $1 pin
524 config_get enabled $1 enabled
525
526 [ "$enabled" = "no" ] && return
527 [ "$defaultuser" = "" ] && return
528
529 for callthrough_number in $callthrough_number_list ; do
530 sed "s/|NUMBER|/$callthrough_number/g" $TMPL_EXTCTHRUCHECK >> $WORKDIR/callthroughcheck.TMP
531
532 sed "s/|NUMBER|/$callthrough_number/g" $TMPL_EXTCTHRU |\
533 sed "s/|DEFAULTUSER|/$defaultuser/" |\
534 sed "s/|PIN|/$pin/" >> $WORKDIR/callthrough.TMP
535 done
536 }
537
538 # Creates sip.conf from its template.
539 pbx_cook_sip_template()
540 {
541 local useragent
542 local externhost
543 local bindport
544
545 config_get useragent advanced useragent
546 config_get externhost advanced externhost
547 config_get bindport advanced bindport
548
549 [ -z "$useragent" ] && useragent="$USERAGENT"
550
551 sed "s/|USERAGENT|/$useragent/g" $TMPL_SIP > $WORKDIR/sip.conf
552
553 if [ -z "$externhost" ] ; then
554 sed -i "s/externhost=|EXTERNHOST|//g" $WORKDIR/sip.conf
555 else
556 sed -i "s/|EXTERNHOST|/$externhost/g" $WORKDIR/sip.conf
557 fi
558
559 if [ -z "$bindport" ] ; then
560 sed -i "s/bindport=|BINDPORT|//g" $WORKDIR/sip.conf
561 else
562 sed -i "s/|BINDPORT|/$bindport/g" $WORKDIR/sip.conf
563 fi
564
565
566 }
567
568 # Creates rtp.conf from its template.
569 pbx_cook_rtp_template()
570 {
571 local rtpstart
572 local rtpend
573
574 config_get rtpstart advanced rtpstart
575 config_get rtpend advanced rtpend
576
577 sed "s/|RTPSTART|/$rtpstart/" $TMPL_RTP |\
578 sed "s/|RTPEND|/$rtpend/" > $WORKDIR/rtp.conf
579 }
580
581 # Links any sound files found in $PBXSOUNDSDIR and $VMSOUNDSDIR
582 # into $ASTSOUNDSDIR for use by Asterisk. Does not overwrite files.
583 pbx_link_sounds()
584 {
585 mkdir -p $ASTSOUNDSDIR
586
587 for dir in $PBXSOUNDSDIR $VMSOUNDSDIR ; do
588 if [ -d $dir ] ; then
589 for f in $dir/* ; do
590 ln -s $f $ASTSOUNDSDIR 2>/dev/null
591 done
592 fi
593 done
594 }
595
596
597 # Makes sure the ownership of specified directories is proper.
598 pbx_fix_ownership()
599 {
600 chown $ASTUSER:$ASTGROUP $ASTDIRS
601 chown $ASTUSER:$ASTGROUP -R $ASTDIRSRECURSIVE
602 }
603
604
605 # Creates voicemail config if installed and enabled.
606 pbx_configure_voicemail()
607 {
608 local enabled
609 local global_timeout
610 local global_email_addresses
611
612 local smtp_tls
613 local smtp_server
614 local smtp_port
615 local smtp_auth
616 local smtp_user
617 local smtp_password
618
619 config_get enabled global_voicemail enabled
620
621 # First check if voicemail is enabled.
622 [ "$enabled" != "yes" ] && return
623
624 config_get global_timeout global_voicemail global_timeout
625 #config_get global_email_addresses global_voicemail global_email_addresses
626 config_get smtp_auth voicemail_smtp smtp_auth
627 config_get smtp_tls voicemail_smtp smtp_tls
628 config_get smtp_server voicemail_smtp smtp_server
629 config_get smtp_port voicemail_smtp smtp_port
630 config_get smtp_user voicemail_smtp smtp_user
631 config_get smtp_password voicemail_smtp smtp_password
632
633 sed "s/|AUTH|/$smtp_auth/" $TMPL_MSMTPDEFAULT |\
634 sed "s/|TLS|/$smtp_tls/" > $WORKDIR/pbx-msmtprc
635
636 sed "s/|HOST|/$smtp_server/" $TMPL_MSMTPACCOUNT |\
637 sed "s/|PORT|/$smtp_port/" >> $WORKDIR/pbx-msmtprc
638
639 if [ "$smtp_auth" = "on" ] ; then
640 sed "s/|USER|/$smtp_user/" $TMPL_MSMTPAUTH |\
641 sed "s/|PASSWORD|/$smtp_password/" >> $WORKDIR/pbx-msmtprc
642 fi
643
644 cat $TMPL_MSMTPACCTDFLT >> $WORKDIR/pbx-msmtprc
645
646 [ ! -f /etc/pbx-msmtprc ] && cp $WORKDIR/pbx-msmtprc /etc/pbx-msmtprc
647 diff -q $WORKDIR/pbx-msmtprc /etc/pbx-msmtprc 1>/dev/null \
648 || mv $WORKDIR/pbx-msmtprc /etc/pbx-msmtprc
649 chmod 600 /etc/pbx-msmtprc
650
651 # Copy over the extensions file which has voicemail enabled.
652 cp $TMPL_EXTVMENABLED $WORKDIR/extensions_voicemail.conf
653 }
654
655
656 start() {
657 mkdir -p $WORKDIR
658
659 # Create the users.
660 config_load ${MODULENAME}-users
661 config_foreach pbx_add_user local_user
662
663 # Create configuration for each google account.
664 config_unset
665 config_load ${MODULENAME}-google
666 config_foreach pbx_add_jabber gtalk_jabber
667
668 # Create configuration for each voip provider.
669 config_unset
670 config_load ${MODULENAME}-voip
671 config_foreach pbx_add_peer voip_provider
672
673 # Create the user contexts, and phone blacklist.
674 config_unset
675 config_load ${MODULENAME}-calls
676 pbx_create_user_contexts
677 pbx_add_blacklist
678 config_foreach pbx_add_callthrough callthrough_numbers
679
680 # Prepare sip.conf using settings from the "advanced" section.
681 config_unset
682 config_load ${MODULENAME}-advanced
683 pbx_cook_sip_template
684 pbx_cook_rtp_template
685
686 # Prepare voicemail config.
687 config_unset
688 config_load ${MODULENAME}-voicemail
689 pbx_configure_voicemail
690
691 # Assemble the configuration, and copy changed files over.
692 config_unset
693 config_load ${MODULENAME}-advanced
694 pbx_assemble_and_copy_config
695
696 # Link sound files
697 pbx_link_sounds
698
699 # Enforce ownership of specified files and directories.
700 pbx_fix_ownership
701 }