pbx-asterisk 31 KB

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