pbx-calls.lua 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. --[[
  2. Copyright 2011 Iordan Iordanov <iiordanov (AT) gmail.com>
  3. This file is part of luci-pbx.
  4. luci-pbx is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. luci-pbx is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with luci-pbx. If not, see <http://www.gnu.org/licenses/>.
  14. ]]--
  15. if nixio.fs.access("/etc/init.d/asterisk") then
  16. server = "asterisk"
  17. elseif nixio.fs.access("/etc/init.d/freeswitch") then
  18. server = "freeswitch"
  19. else
  20. server = ""
  21. end
  22. modulename = "pbx-calls"
  23. voipmodulename = "pbx-voip"
  24. googlemodulename = "pbx-google"
  25. usersmodulename = "pbx-users"
  26. allvalidaccounts = {}
  27. nallvalidaccounts = 0
  28. validoutaccounts = {}
  29. nvalidoutaccounts = 0
  30. validinaccounts = {}
  31. nvalidinaccounts = 0
  32. allvalidusers = {}
  33. nallvalidusers = 0
  34. validoutusers = {}
  35. nvalidoutusers = 0
  36. -- Checks whether the entered extension is valid syntactically.
  37. function is_valid_extension(exten)
  38. return (exten:match("[#*+0-9NXZ]+$") ~= nil)
  39. end
  40. m = Map (modulename, translate("Call Routing"),
  41. translate("This is where you indicate which Google/SIP accounts are used to call what \
  42. country/area codes, which users can use what SIP/Google accounts, how incoming \
  43. calls are routed, what numbers can get into this PBX with a password, and what \
  44. numbers are blacklisted."))
  45. -- Recreate the config, and restart services after changes are commited to the configuration.
  46. function m.on_after_commit(self)
  47. luci.sys.call("/etc/init.d/pbx-" .. server .. " restart 1\>/dev/null 2\>/dev/null")
  48. luci.sys.call("/etc/init.d/" .. server .. " restart 1\>/dev/null 2\>/dev/null")
  49. end
  50. -- Add Google accounts to all valid accounts, and accounts valid for incoming and outgoing calls.
  51. m.uci:foreach(googlemodulename, "gtalk_jabber",
  52. function(s1)
  53. -- Add this provider to list of valid accounts.
  54. if s1.username ~= nil and s1.name ~= nil then
  55. allvalidaccounts[s1.name] = s1.username
  56. nallvalidaccounts = nallvalidaccounts + 1
  57. if s1.make_outgoing_calls == "yes" then
  58. -- Add provider to the associative array of valid outgoing accounts.
  59. validoutaccounts[s1.name] = s1.username
  60. nvalidoutaccounts = nvalidoutaccounts + 1
  61. end
  62. if s1.register == "yes" then
  63. -- Add provider to the associative array of valid outgoing accounts.
  64. validinaccounts[s1.name] = s1.username
  65. nvalidinaccounts = nvalidinaccounts + 1
  66. end
  67. end
  68. end)
  69. -- Add SIP accounts to all valid accounts, and accounts valid for incoming and outgoing calls.
  70. m.uci:foreach(voipmodulename, "voip_provider",
  71. function(s1)
  72. -- Add this provider to list of valid accounts.
  73. if s1.defaultuser ~= nil and s1.host ~= nil and s1.name ~= nil then
  74. allvalidaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
  75. nallvalidaccounts = nallvalidaccounts + 1
  76. if s1.make_outgoing_calls == "yes" then
  77. -- Add provider to the associative array of valid outgoing accounts.
  78. validoutaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
  79. nvalidoutaccounts = nvalidoutaccounts + 1
  80. end
  81. if s1.register == "yes" then
  82. -- Add provider to the associative array of valid outgoing accounts.
  83. validinaccounts[s1.name] = s1.defaultuser .. "@" .. s1.host
  84. nvalidinaccounts = nvalidinaccounts + 1
  85. end
  86. end
  87. end)
  88. -- Add Local User accounts to all valid users, and users allowed to make outgoing calls.
  89. m.uci:foreach(usersmodulename, "local_user",
  90. function(s1)
  91. -- Add user to list of all valid users.
  92. if s1.defaultuser ~= nil then
  93. allvalidusers[s1.defaultuser] = true
  94. nallvalidusers = nallvalidusers + 1
  95. if s1.can_call == "yes" then
  96. validoutusers[s1.defaultuser] = true
  97. nvalidoutusers = nvalidoutusers + 1
  98. end
  99. end
  100. end)
  101. ----------------------------------------------------------------------------------------------------
  102. -- If there are no accounts configured, or no accounts enabled for outgoing calls, display a warning.
  103. -- Otherwise, display the usual help text within the section.
  104. if nallvalidaccounts == 0 then
  105. text = translate("NOTE: There are no Google or SIP provider accounts configured.")
  106. elseif nvalidoutaccounts == 0 then
  107. text = translate("NOTE: There are no Google or SIP provider accounts enabled for outgoing calls.")
  108. else
  109. text = translate("If you have more than one account that can make outgoing calls, you \
  110. should enter a list of phone numbers and/or prefixes in the following fields for each \
  111. provider listed. Invalid prefixes are removed silently, and only 0-9, X, Z, N, #, *, \
  112. and + are valid characters. The letter X matches 0-9, Z matches 1-9, and N matches 2-9. \
  113. For example to make calls to Germany through a provider, you can enter 49. To make calls \
  114. to North America, you can enter 1NXXNXXXXXX. If one of your providers can make \"local\" \
  115. calls to an area code like New York's 646, you can enter 646NXXXXXX for that \
  116. provider. You should leave one account with an empty list to make calls with \
  117. it by default, if no other provider's prefixes match. The system will automatically \
  118. replace an empty list with a message that the provider dials all numbers not matched by another \
  119. provider's prefixes. Be as specific as possible (i.e. 1NXXNXXXXXX is better than 1). Please note \
  120. all international dial codes are discarded (e.g. 00, 011, 010, 0011). Entries can be made in a \
  121. space-separated list, and/or one per line by hitting enter after every one.")
  122. end
  123. s = m:section(NamedSection, "outgoing_calls", "call_routing", translate("Outgoing Calls"), text)
  124. s.anonymous = true
  125. for k,v in pairs(validoutaccounts) do
  126. patterns = s:option(DynamicList, k, v)
  127. -- If the saved field is empty, we return a string
  128. -- telling the user that this provider would dial any exten.
  129. function patterns.cfgvalue(self, section)
  130. value = self.map:get(section, self.option)
  131. if value == nil then
  132. return {translate("Dials numbers unmatched elsewhere")}
  133. else
  134. return value
  135. end
  136. end
  137. -- Write only valid extensions into the config file.
  138. function patterns.write(self, section, value)
  139. newvalue = {}
  140. nindex = 1
  141. for index, field in ipairs(value) do
  142. val = luci.util.trim(value[index])
  143. if is_valid_extension(val) == true then
  144. newvalue[nindex] = val
  145. nindex = nindex + 1
  146. end
  147. end
  148. DynamicList.write(self, section, newvalue)
  149. end
  150. end
  151. ----------------------------------------------------------------------------------------------------
  152. -- If there are no accounts configured, or no accounts enabled for incoming calls, display a warning.
  153. -- Otherwise, display the usual help text within the section.
  154. if nallvalidaccounts == 0 then
  155. text = translate("NOTE: There are no Google or SIP provider accounts configured.")
  156. elseif nvalidinaccounts == 0 then
  157. text = translate("NOTE: There are no Google or SIP provider accounts enabled for incoming calls.")
  158. else
  159. text = translate("For each provider enabled for incoming calls, here you can restrict which users to\
  160. ring on incoming calls. If the list is empty, the system will indicate that all users \
  161. enabled for incoming calls will ring. Invalid usernames will be rejected \
  162. silently. Also, entering a username here overrides the user's setting to not receive \
  163. incoming calls. This way, you can make certain users ring only for specific providers. \
  164. Entries can be made in a space-separated list, and/or one per line by hitting enter after \
  165. every one.")
  166. end
  167. s = m:section(NamedSection, "incoming_calls", "call_routing", translate("Incoming Calls"), text)
  168. s.anonymous = true
  169. for k,v in pairs(validinaccounts) do
  170. users = s:option(DynamicList, k, v)
  171. -- If the saved field is empty, we return a string telling the user that
  172. -- this provider would ring all users configured for incoming calls.
  173. function users.cfgvalue(self, section)
  174. value = self.map:get(section, self.option)
  175. if value == nil then
  176. return {translate("Rings users enabled for incoming calls")}
  177. else
  178. return value
  179. end
  180. end
  181. -- Write only valid user names.
  182. function users.write(self, section, value)
  183. newvalue = {}
  184. nindex = 1
  185. for index, field in ipairs(value) do
  186. trimuser = luci.util.trim(value[index])
  187. if allvalidusers[trimuser] == true then
  188. newvalue[nindex] = trimuser
  189. nindex = nindex + 1
  190. end
  191. end
  192. DynamicList.write(self, section, newvalue)
  193. end
  194. end
  195. ----------------------------------------------------------------------------------------------------
  196. -- If there are no user accounts configured, no user accounts enabled for outgoing calls,
  197. -- display a warning. Otherwise, display the usual help text within the section.
  198. if nallvalidusers == 0 then
  199. text = translate("NOTE: There are no local user accounts configured.")
  200. elseif nvalidoutusers == 0 then
  201. text = translate("NOTE: There are no local user accounts enabled for outgoing calls.")
  202. else
  203. text = translate("For each user enabled for outgoing calls you can restrict what providers the user \
  204. can use for outgoing calls. By default all users can use all providers. To show up in the list \
  205. below the user should be allowed to make outgoing calls in the \"User Accounts\" page. Enter VoIP \
  206. providers in the format username@some.host.name, as listed in \"Outgoing Calls\" above. It's \
  207. easiest to copy and paste the providers from above. Invalid entries, including providers not \
  208. enabled for outgoing calls, will be rejected silently. Entries can be made in a space-separated \
  209. list, and/or one per line by hitting enter after every one.")
  210. end
  211. s = m:section(NamedSection, "providers_user_can_use", "call_routing",
  212. translate("Providers Used for Outgoing Calls"), text)
  213. s.anonymous = true
  214. for k,v in pairs(validoutusers) do
  215. providers = s:option(DynamicList, k, k)
  216. -- If the saved field is empty, we return a string telling the user
  217. -- that this user uses all providers enavled for outgoing calls.
  218. function providers.cfgvalue(self, section)
  219. value = self.map:get(section, self.option)
  220. if value == nil then
  221. return {translate("Uses providers enabled for outgoing calls")}
  222. else
  223. newvalue = {}
  224. -- Convert internal names to user@host values.
  225. for i,v in ipairs(value) do
  226. newvalue[i] = validoutaccounts[v]
  227. end
  228. return newvalue
  229. end
  230. end
  231. -- Cook the new values prior to entering them into the config file.
  232. -- Also, enter them only if they are valid.
  233. function providers.write(self, section, value)
  234. cookedvalue = {}
  235. cindex = 1
  236. for index, field in ipairs(value) do
  237. cooked = string.gsub(luci.util.trim(value[index]), "%W", "_")
  238. if validoutaccounts[cooked] ~= nil then
  239. cookedvalue[cindex] = cooked
  240. cindex = cindex + 1
  241. end
  242. end
  243. DynamicList.write(self, section, cookedvalue)
  244. end
  245. end
  246. ----------------------------------------------------------------------------------------------------
  247. s = m:section(TypedSection, "callthrough_numbers", translate("Call-through Numbers"),
  248. translate("Designate numbers that are allowed to call through this system and which user's \
  249. privileges they will have."))
  250. s.anonymous = true
  251. s.addremove = true
  252. num = s:option(DynamicList, "callthrough_number_list", translate("Call-through Numbers"),
  253. translate("Specify numbers individually here. Press enter to add more numbers. \
  254. You will have to experiment with what country and area codes you need to add \
  255. to the number."))
  256. num.datatype = "uinteger"
  257. p = s:option(ListValue, "enabled", translate("Enabled"))
  258. p:value("yes", translate("Yes"))
  259. p:value("no", translate("No"))
  260. p.default = "yes"
  261. user = s:option(Value, "defaultuser", translate("User Name"),
  262. translate("The number(s) specified above will be able to dial out with this user's providers. \
  263. Invalid usernames, including users not enabled for outgoing calls, are dropped silently. \
  264. Please verify that the entry was accepted."))
  265. function user.write(self, section, value)
  266. trimuser = luci.util.trim(value)
  267. if allvalidusers[trimuser] == true then
  268. Value.write(self, section, trimuser)
  269. end
  270. end
  271. pwd = s:option(Value, "pin", translate("PIN"),
  272. translate("Your PIN disappears when saved for your protection. It will be changed \
  273. only when you enter a value different from the saved one. Leaving the PIN \
  274. empty is possible, but please beware of the security implications."))
  275. pwd.password = true
  276. pwd.rmempty = false
  277. -- We skip reading off the saved value and return nothing.
  278. function pwd.cfgvalue(self, section)
  279. return ""
  280. end
  281. -- We check the entered value against the saved one, and only write if the entered value is
  282. -- something other than the empty string, and it differes from the saved value.
  283. function pwd.write(self, section, value)
  284. local orig_pwd = m:get(section, self.option)
  285. if value and #value > 0 and orig_pwd ~= value then
  286. Value.write(self, section, value)
  287. end
  288. end
  289. ----------------------------------------------------------------------------------------------------
  290. s = m:section(TypedSection, "callback_numbers", translate("Call-back Numbers"),
  291. translate("Designate numbers to whom the system will hang up and call back, which provider will \
  292. be used to call them, and which user's privileges will be granted to them."))
  293. s.anonymous = true
  294. s.addremove = true
  295. num = s:option(DynamicList, "callback_number_list", translate("Call-back Numbers"),
  296. translate("Specify numbers individually here. Press enter to add more numbers. \
  297. You will have to experiment with what country and area codes you need to add \
  298. to the number."))
  299. num.datatype = "uinteger"
  300. p = s:option(ListValue, "enabled", translate("Enabled"))
  301. p:value("yes", translate("Yes"))
  302. p:value("no", translate("No"))
  303. p.default = "yes"
  304. delay = s:option(Value, "callback_hangup_delay", translate("Hang-up Delay"),
  305. translate("How long to wait before hanging up. If the provider you use to dial automatically forwards \
  306. to voicemail, you can set this value to a delay that will allow you to hang up before your call gets \
  307. forwarded and you get billed for it."))
  308. delay.datatype = "uinteger"
  309. delay.default = 0
  310. user = s:option(Value, "defaultuser", translate("User Name"),
  311. translate("The number(s) specified above will be able to dial out with this user's providers. \
  312. Invalid usernames, including users not enabled for outgoing calls, are dropped silently. \
  313. Please verify that the entry was accepted."))
  314. function user.write(self, section, value)
  315. trimuser = luci.util.trim(value)
  316. if allvalidusers[trimuser] == true then
  317. Value.write(self, section, trimuser)
  318. end
  319. end
  320. pwd = s:option(Value, "pin", translate("PIN"),
  321. translate("Your PIN disappears when saved for your protection. It will be changed \
  322. only when you enter a value different from the saved one. Leaving the PIN \
  323. empty is possible, but please beware of the security implications."))
  324. pwd.password = true
  325. pwd.rmempty = false
  326. -- We skip reading off the saved value and return nothing.
  327. function pwd.cfgvalue(self, section)
  328. return ""
  329. end
  330. -- We check the entered value against the saved one, and only write if the entered value is
  331. -- something other than the empty string, and it differes from the saved value.
  332. function pwd.write(self, section, value)
  333. local orig_pwd = m:get(section, self.option)
  334. if value and #value > 0 and orig_pwd ~= value then
  335. Value.write(self, section, value)
  336. end
  337. end
  338. provider = s:option(Value, "callback_provider", translate("Call-back Provider"),
  339. translate("Enter a VoIP provider to use for call-back in the format username@some.host.name, as listed in \
  340. \"Outgoing Calls\" above. It's easiest to copy and paste the providers from above. Invalid entries, including \
  341. providers not enabled for outgoing calls, will be rejected silently."))
  342. function provider.write(self, section, value)
  343. cooked = string.gsub(luci.util.trim(value), "%W", "_")
  344. if validoutaccounts[cooked] ~= nil then
  345. Value.write(self, section, value)
  346. end
  347. end
  348. ----------------------------------------------------------------------------------------------------
  349. s = m:section(NamedSection, "blacklisting", "call_routing", translate("Blacklisted Numbers"),
  350. translate("Enter phone numbers that you want to decline calls from automatically. \
  351. You should probably omit the country code and any leading zeroes, but please \
  352. experiment to make sure you are blocking numbers from your desired area successfully."))
  353. s.anonymous = true
  354. b = s:option(DynamicList, "blacklist1", translate("Dynamic List of Blacklisted Numbers"),
  355. translate("Specify numbers individually here. Press enter to add more numbers."))
  356. b.cast = "string"
  357. b.datatype = "uinteger"
  358. b = s:option(Value, "blacklist2", translate("Space-Separated List of Blacklisted Numbers"),
  359. translate("Copy-paste large lists of numbers here."))
  360. b.template = "cbi/tvalue"
  361. b.rows = 3
  362. return m