2
0

asterisk.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Licensed to the public under the Apache License 2.0.
  4. module("luci.controller.asterisk", package.seeall)
  5. function index()
  6. entry({"admin", "services", "asterisk"}, cbi("asterisk"), "Asterisk", 80)
  7. entry({"admin", "services", "asterisk", "voice"}, cbi("asterisk-voice"), "Voice Functions", 1)
  8. entry({"admin", "services", "asterisk", "meetme"}, cbi("asterisk-meetme"), "Meetme Conferences", 2)
  9. entry({"admin", "services", "asterisk", "iax-conns"}, cbi("asterisk-iax-connections"), "IAX Connections", 3)
  10. entry({"admin", "services", "asterisk", "sip-conns"}, cbi("asterisk-sip-connections"), "SIP Connections", 4)
  11. entry({"admin", "services", "asterisk", "dialplans"}, cbi("asterisk-dialplans"), "Dial Plans", 5)
  12. entry({"admin", "services", "asterisk", "mod"}, cbi("asterisk-mod-app"), "Modules", 4)
  13. entry({"admin", "services", "asterisk", "mod", "app"}, cbi("asterisk-mod-app"), "Applications", 1)
  14. entry({"admin", "services", "asterisk", "mod", "cdr"}, cbi("asterisk-mod-cdr"), "Call Detail Records", 2)
  15. entry({"admin", "services", "asterisk", "mod", "chan"}, cbi("asterisk-mod-chan"), "Channels", 3)
  16. entry({"admin", "services", "asterisk", "mod", "codec"}, cbi("asterisk-mod-codec"), "Codecs", 4)
  17. entry({"admin", "services", "asterisk", "mod", "format"}, cbi("asterisk-mod-format"), "Format", 5)
  18. entry({"admin", "services", "asterisk", "mod", "func"}, cbi("asterisk-mod-func"), "Functions", 6)
  19. entry({"admin", "services", "asterisk", "mod", "pbx"}, cbi("asterisk-mod-pbx"), "PBX", 7)
  20. entry({"admin", "services", "asterisk", "mod", "res"}, cbi("asterisk-mod-res"), "Resources", 8)
  21. entry({"admin", "services", "asterisk", "mod", "res", "feature"},
  22. cbi("asterisk-mod-res-feature"), "Feature Module Configuration", 9 )
  23. entry({"admin", "asterisk"}, cbi("asterisk/main"), "Asterisk", 99).i18n = "asterisk"
  24. entry({"admin", "asterisk", "phones"}, cbi("asterisk/phones"), "Phones", 1)
  25. entry({"admin", "asterisk", "phones", "sip"}, cbi("asterisk/phone_sip"), nil, 1).leaf = true
  26. --entry({"admin", "asterisk", "phones", "exten"}, cbi("asterisk/phone_exten"), "Extensions", 2).leaf = true
  27. entry({"admin", "asterisk", "trunks"}, cbi("asterisk/trunks"), "Trunks", 2)
  28. entry({"admin", "asterisk", "trunks", "sip"}, cbi("asterisk/trunk_sip"), nil, 1).leaf = true
  29. entry({"admin", "asterisk", "voicemail"}, cbi("asterisk/voicemail"), "Voicemail", 3)
  30. entry({"admin", "asterisk", "voicemail", "mailboxes"}, cbi("asterisk/voicemail"), "Mailboxes", 1)
  31. entry({"admin", "asterisk", "voicemail", "settings"}, cbi("asterisk/voicemail_settings"), "Settings", 2)
  32. entry({"admin", "asterisk", "meetme"}, cbi("asterisk/meetme"), "MeetMe", 4)
  33. entry({"admin", "asterisk", "meetme", "rooms"}, cbi("asterisk/meetme"), "Rooms", 1)
  34. entry({"admin", "asterisk", "meetme", "settings"}, cbi("asterisk/meetme_settings"), "Settings", 2)
  35. entry({"admin", "asterisk", "dialplans"}, call("handle_dialplan"), "Call Routing", 5)
  36. entry({"admin", "asterisk", "dialplans", "out"}, cbi("asterisk/dialplan_out"), nil, 1).leaf = true
  37. entry({"admin", "asterisk", "dialplans", "zones"}, call("handle_dialzones"), "Dial Zones", 2).leaf = true
  38. end
  39. function handle_dialplan()
  40. local uci = luci.model.uci.cursor()
  41. local ast = require "luci.asterisk"
  42. local err = false
  43. for k, v in pairs(luci.http.formvaluetable("delzone")) do
  44. local plan = ast.dialplan.plan(k)
  45. if #v > 0 and plan then
  46. local newinc = { }
  47. for _, z in ipairs(plan.zones) do
  48. if z.name ~= v then
  49. newinc[#newinc+1] = z.name
  50. end
  51. end
  52. uci:delete("asterisk", plan.name, "include")
  53. if #newinc > 0 then
  54. uci:set("asterisk", plan.name, "include", newinc)
  55. end
  56. end
  57. end
  58. for k, v in pairs(luci.http.formvaluetable("addzone")) do
  59. local plan = ast.dialplan.plan(k)
  60. local zone = ast.dialzone.zone(v)
  61. if #v > 0 and plan and zone then
  62. local newinc = { zone.name }
  63. for _, z in ipairs(plan.zones) do
  64. newinc[#newinc+1] = z.name
  65. end
  66. uci:delete("asterisk", plan.name, "include")
  67. if #newinc > 0 then
  68. uci:set("asterisk", plan.name, "include", newinc)
  69. end
  70. end
  71. end
  72. for k, v in pairs(luci.http.formvaluetable("delvbox")) do
  73. local plan = ast.dialplan.plan(k)
  74. if #v > 0 and plan then
  75. uci:delete_all("asterisk", "dialplanvoice",
  76. { extension=v, dialplan=plan.name })
  77. end
  78. end
  79. for k, v in pairs(luci.http.formvaluetable("addvbox")) do
  80. local plan = ast.dialplan.plan(k)
  81. local vbox = ast.voicemail.box(v)
  82. if plan and vbox then
  83. local vext = luci.http.formvalue("addvboxext.%s" % plan.name)
  84. vext = ( vext and #vext > 0 ) and vext or vbox.number
  85. uci:section("asterisk", "dialplanvoice", nil, {
  86. dialplan = plan.name,
  87. extension = vext,
  88. voicebox = vbox.number,
  89. voicecontext = vbox.context
  90. })
  91. end
  92. end
  93. for k, v in pairs(luci.http.formvaluetable("delmeetme")) do
  94. local plan = ast.dialplan.plan(k)
  95. if #v > 0 and plan then
  96. uci:delete_all("asterisk", "dialplanmeetme",
  97. { extension=v, dialplan=plan.name })
  98. end
  99. end
  100. for k, v in pairs(luci.http.formvaluetable("addmeetme")) do
  101. local plan = ast.dialplan.plan(k)
  102. local meetme = ast.meetme.room(v)
  103. if plan and meetme then
  104. local mext = luci.http.formvalue("addmeetmeext.%s" % plan.name)
  105. mext = ( mext and #mext > 0 ) and mext or meetme.room
  106. uci:section("asterisk", "dialplanmeetme", nil, {
  107. dialplan = plan.name,
  108. extension = mext,
  109. room = meetme.room
  110. })
  111. end
  112. end
  113. local aname = luci.http.formvalue("addplan")
  114. if aname and #aname > 0 then
  115. if aname:match("^[a-zA-Z0-9_]+$") then
  116. uci:section("asterisk", "dialplan", aname, { })
  117. else
  118. err = true
  119. end
  120. end
  121. local dname = luci.http.formvalue("delplan")
  122. if dname and #dname > 0 then
  123. if uci:get("asterisk", dname) == "dialplan" then
  124. uci:delete("asterisk", dname)
  125. uci:delete_all("asterisk", "dialplanvoice", { dialplan=dname })
  126. uci:delete_all("asterisk", "dialplanmeetme", { dialplan=dname })
  127. end
  128. end
  129. uci:save("asterisk")
  130. ast.uci_resync()
  131. luci.template.render("asterisk/dialplans", { create_error = err })
  132. end
  133. function handle_dialzones()
  134. local ast = require "luci.asterisk"
  135. local uci = luci.model.uci.cursor()
  136. local err = false
  137. if luci.http.formvalue("newzone") then
  138. local name = luci.http.formvalue("newzone_name")
  139. if name and name:match("^[a-zA-Z0-9_]+$") then
  140. uci:section("asterisk", "dialzone", name, {
  141. uses = ast.tools.parse_list(luci.http.formvalue("newzone_uses") or {}),
  142. match = ast.tools.parse_list(luci.http.formvalue("newzone_match") or {})
  143. })
  144. uci:save("asterisk")
  145. else
  146. err = true
  147. end
  148. end
  149. if luci.http.formvalue("delzone") then
  150. local name = luci.http.formvalue("delzone")
  151. if uci:get("asterisk", name) == "dialzone" then
  152. uci:delete("asterisk", name)
  153. uci:save("asterisk")
  154. end
  155. end
  156. luci.template.render("asterisk/dialzones", { create_error = err })
  157. end