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