3bd369da3c8e0f5691c746056e088898e5eadf59
[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         for k, v in pairs(luci.http.formvaluetable("delvbox")) do
107                 local plan = ast.dialplan.plan(k)
108                 if #v > 0 and plan then
109                         uci:delete_all("asterisk", "dialplanvoice",
110                                 { extension=v, dialplan=plan.name })
111                         uci:save("asterisk")
112                 end
113         end
114
115         for k, v in pairs(luci.http.formvaluetable("addvbox")) do
116                 local plan = ast.dialplan.plan(k)
117                 local vbox = ast.voicemail.box(v)
118                 if plan and vbox then
119                         local vext = luci.http.formvalue("addvboxext.%s" % plan.name)
120                         vext = ( vext and #vext > 0 ) and vext or vbox.number
121                         uci:section("asterisk", "dialplanvoice", nil, {
122                                 dialplan                = plan.name,
123                                 extension               = vext,
124                                 voicebox                = vbox.number,
125                                 voicecontext    = vbox.context
126                         })
127                         uci:save("asterisk")
128                 end
129         end
130
131         local aname = luci.http.formvalue("addplan")
132         if aname and #aname > 0 then
133                 if aname:match("^[a-zA-Z0-9_]+$") then
134                         uci:section("asterisk", "dialplan", aname, { })
135                         uci:save("asterisk")
136                 else
137                         err = true
138                 end
139         end
140
141         local dname = luci.http.formvalue("delplan")
142         if dname and #dname > 0 then
143                 if uci:get("asterisk", dname) == "dialplan" then
144                         uci:delete("asterisk", dname)
145                         uci:save("asterisk")
146                 end
147         end
148
149         ast.uci_resync()
150         luci.template.render("asterisk/dialplans", { create_error = err })
151 end
152
153 function handle_dialzones()
154         local ast = require "luci.asterisk"
155         local uci = luci.model.uci.cursor()
156         local err = false
157
158         if luci.http.formvalue("newzone") then
159                 local name = luci.http.formvalue("newzone_name")
160                 if name and name:match("^[a-zA-Z0-9_]+$") then
161                         uci:section("asterisk", "dialzone", name, {
162                                 uses  = ast.tools.parse_list(luci.http.formvalue("newzone_uses") or {}),
163                                 match = ast.tools.parse_list(luci.http.formvalue("newzone_match") or {})
164                         })
165                         uci:save("asterisk")
166                 else
167                         err = true
168                 end
169         end
170
171         if luci.http.formvalue("delzone") then
172                 local name = luci.http.formvalue("delzone")
173                 if uci:get("asterisk", name) == "dialzone" then
174                         uci:delete("asterisk", name)
175                         uci:save("asterisk")
176                 end
177         end
178
179         luci.template.render("asterisk/dialzones", { create_error = err })
180 end