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