02f07a5faa70151f474223c885a8b11dbe1587b9
[project/luci.git] / applications / luci-app-asterisk / luasrc / model / cbi / asterisk / trunks.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", "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 context = sip_table:option(DummyValue, "context", "Dialplan")
61 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
62 function context.cfgvalue(...)
63         return AbstractValue.cfgvalue(...) or "(default)"
64 end
65
66 online = sip_table:option(DummyValue, "online", "Registered")
67 function online.cfgvalue(self, s)
68         if sip_peers[s] and sip_peers[s].info.online == nil then
69                 return "n/a"
70         else
71                 return sip_peers[s] and sip_peers[s].info.online
72                         and "yes" or "no (%s)" %{
73                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
74                         }
75         end
76 end
77
78 delay = sip_table:option(DummyValue, "delay", "Delay")
79 function delay.cfgvalue(self, s)
80         if sip_peers[s] and sip_peers[s].info.online then
81                 return "%i ms" % sip_peers[s].info.delay
82         else
83                 return "n/a"
84         end
85 end
86
87 info = sip_table:option(Button, "_info", "Info")
88 function info.write(self, s)
89         luci.http.redirect(luci.dispatcher.build_url(
90                 "admin", "asterisk", "trunks", "sip", s, "info"
91         ))
92 end
93
94 return cbimap