bcc085d132d7a5cf7647189ae9978112e5a74b56
[project/luci.git] / applications / luci-app-asterisk / luasrc / model / cbi / asterisk / dialplans.lua
1 -- Copyright 2008 Jo-Philipp Wich <xm@subsignal.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local ast = require("luci.asterisk")
5
6 cbimap = Map("asterisk", "Registered Trunks")
7 cbimap.pageaction = false
8
9 local sip_peers = { }
10 cbimap.uci:foreach("asterisk", "sip",
11         function(s)
12                 if s.type == "peer" then
13                         s.name = s['.name']
14                         s.info = ast.sip.peer(s.name)
15                         sip_peers[s.name] = s
16                 end
17         end)
18
19
20 sip_table = cbimap:section(TypedSection, "sip", "SIP Trunks")
21 sip_table.template    = "cbi/tblsection"
22 sip_table.extedit     = luci.dispatcher.build_url("admin", "asterisk", "trunks", "sip", "%s")
23 sip_table.addremove   = true
24 sip_table.sectionhead = "Extension"
25
26 function sip_table.filter(self, s)
27         return s and (
28                 cbimap.uci:get("asterisk", s, "type") == nil or
29                 cbimap.uci:get_bool("asterisk", s, "provider")
30         )
31 end
32
33 function sip_table.create(self, section)
34         if TypedSection.create(self, section) then
35                 created = section
36         else
37                 self.invalid_cts = true
38         end
39 end
40
41 function sip_table.parse(self, ...)
42         TypedSection.parse(self, ...)
43         if created then
44                 cbimap.uci:tset("asterisk", created, {
45                         type     = "friend",
46                         qualify  = "yes",
47                         provider = "yes"
48                 })
49
50                 cbimap.uci:save("asterisk")
51                 luci.http.redirect(luci.dispatcher.build_url(
52                         "admin", "asterisk", "trunks", "sip", created
53                 ))
54         end
55 end
56
57
58 user = sip_table:option(DummyValue, "username", "Username")
59
60 host = sip_table:option(DummyValue, "host", "Hostname")
61 function host.cfgvalue(self, s)
62         if sip_peers[s] and sip_peers[s].info.address then
63                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
64         else
65                 return "n/a"
66         end
67 end
68
69 context = sip_table:option(DummyValue, "context", "Dialplan")
70 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
71 function context.cfgvalue(...)
72         return AbstractValue.cfgvalue(...) or "(default)"
73 end
74
75 online = sip_table:option(DummyValue, "online", "Registered")
76 function online.cfgvalue(self, s)
77         if sip_peers[s] and sip_peers[s].info.online == nil then
78                 return "n/a"
79         else
80                 return sip_peers[s] and sip_peers[s].info.online
81                         and "yes" or "no (%s)" %{
82                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
83                         }
84         end
85 end
86
87 delay = sip_table:option(DummyValue, "delay", "Delay")
88 function delay.cfgvalue(self, s)
89         if sip_peers[s] and sip_peers[s].info.online then
90                 return "%i ms" % sip_peers[s].info.delay
91         else
92                 return "n/a"
93         end
94 end
95
96 info = sip_table:option(Button, "_info", "Info")
97 function info.write(self, s)
98         luci.http.redirect(luci.dispatcher.build_url(
99                 "admin", "asterisk", "trunks", "sip", s, "info"
100         ))
101 end
102
103 return cbimap