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