application/luci-asterisk:
[project/luci.git] / applications / luci-asterisk / luasrc / model / cbi / asterisk / trunks.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Jo-Philipp Wich <xm@subsignal.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 ]]--
15
16 local ast = require("luci.asterisk")
17
18 cbimap = Map("asterisk", "Registered Trunks")
19 cbimap.pageaction = false
20
21 local sip_peers = { }
22 cbimap.uci:foreach("asterisk", "sip",
23         function(s)
24                 if s.type == "peer" then
25                         s.name = s['.name']
26                         s.info = ast.sip.peer(s.name)
27                         sip_peers[s.name] = s
28                 end
29         end)
30
31
32 sip_table = cbimap:section(TypedSection, "sip", "SIP Trunks")
33 sip_table.template  = "cbi/tblsection"
34 sip_table.extedit   = luci.dispatcher.build_url("admin", "asterisk", "trunks", "sip", "%s")
35 sip_table.addremove = true
36
37 sip_table.hidden = {
38         type    = "peer",
39         qualify = "yes"
40 }
41
42 function sip_table.filter(self, s)
43         return s and (
44                 cbimap.uci:get("asterisk", s, "type") == "peer" or
45                 cbimap.uci:get("asterisk", s, "type") == nil
46         )
47 end
48
49 function sip_table.create(self, section)
50         if TypedSection.create(self, section) then
51                 created = section
52         else
53                 self.invalid_cts = true
54         end
55 end
56
57 function sip_table.parse(self, ...)
58         TypedSection.parse(self, ...)
59         if created then
60                 cbimap.uci:save("asterisk")
61                 luci.http.redirect(luci.dispatcher.build_url(
62                         "admin", "asterisk", "trunks", "sip", created
63                 ))
64         end
65 end
66
67
68 user = sip_table:option(DummyValue, "username")
69
70 host = sip_table:option(DummyValue, "host")
71 function host.cfgvalue(self, s)
72         if sip_peers[s] and sip_peers[s].info.address then
73                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
74         else
75                 return "n/a"
76         end
77 end
78
79 context = sip_table:option(DummyValue, "context")
80 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
81 function context.cfgvalue(...)
82         return AbstractValue.cfgvalue(...) or "(default)"
83 end
84
85 nat = sip_table:option(DummyValue, "nat")
86 function nat.cfgvalue(self, s)
87         return sip_peers[s] and sip_peers[s].info.Nat or "none"
88 end
89
90 online = sip_table:option(DummyValue, "online")
91 function online.cfgvalue(self, s)
92         if sip_peers[s] and sip_peers[s].info.online == nil then
93                 return "n/a"
94         else
95                 return sip_peers[s] and sip_peers[s].info.online
96                         and "yes" or "no (%s)" %{
97                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
98                         }
99         end
100 end
101
102 delay = sip_table:option(DummyValue, "delay")
103 function delay.cfgvalue(self, s)
104         if sip_peers[s] and sip_peers[s].info.online then
105                 return "%i ms" % sip_peers[s].info.delay
106         else
107                 return "n/a"
108         end
109 end
110
111 info = sip_table:option(Button, "_info", "Info")
112 function info.write(self, s)
113         luci.http.redirect(luci.dispatcher.build_url(
114                 "admin", "asterisk", "trunks", "sip", s, "info"
115         ))
116 end
117
118 return cbimap