application/luci-asterisk:
[project/luci.git] / applications / luci-asterisk / luasrc / model / cbi / asterisk / phones.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 Phones")
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 Phones")
33 sip_table.template  = "cbi/tblsection"
34 sip_table.extedit   = luci.dispatcher.build_url("admin", "asterisk", "phones", "sip", "%s")
35 sip_table.addremove = true
36
37 sip_table.hidden = {
38         type        = "friend",
39         qualify     = "yes",
40         host        = "dynamic",
41         nat         = "no",
42         canreinvite = "no"
43 }
44
45 function sip_table.filter(self, s)
46         return s and cbimap.uci:get("asterisk", s, "type") ~= "peer"
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", "phones", "sip", created
63                 ))
64         end
65 end
66
67
68 user = sip_table:option(DummyValue, "username")
69 function user.cfgvalue(self, s)
70         return sip_peers[s] and sip_peers[s].callerid or
71                 AbstractValue.cfgvalue(self, s)
72 end
73
74 host = sip_table:option(DummyValue, "host")
75 function host.cfgvalue(self, s)
76         if sip_peers[s] and sip_peers[s].info.address then
77                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
78         else
79                 return "n/a"
80         end
81 end
82
83 context = sip_table:option(DummyValue, "context")
84 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
85
86 nat = sip_table:option(DummyValue, "nat")
87 function nat.cfgvalue(self, s)
88         return sip_peers[s] and sip_peers[s].info.Nat or "none"
89 end
90
91 online = sip_table:option(DummyValue, "online")
92 function online.cfgvalue(self, s)
93         if sip_peers[s] and sip_peers[s].info.online == nil then
94                 return "n/a"
95         else
96                 return sip_peers[s] and sip_peers[s].info.online
97                         and "yes" or "no (%s)" % {
98                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
99                         }
100         end
101 end
102
103 delay = sip_table:option(DummyValue, "delay")
104 function delay.cfgvalue(self, s)
105         if sip_peers[s] and sip_peers[s].info.online then
106                 return "%i ms" % sip_peers[s].info.delay
107         else
108                 return "n/a"
109         end
110 end
111
112 info = sip_table:option(Button, "_info", "Info")
113 function info.write(self, s)
114         luci.http.redirect(luci.dispatcher.build_url(
115                 "admin", "asterisk", "phones", "sip", s, "info"
116         ))
117 end
118
119 return cbimap