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