Update my email addresses in the license headers
[project/luci.git] / applications / luci-app-asterisk / luasrc / model / cbi / asterisk / phones.lua
1 -- Copyright 2008 Jo-Philipp Wich <jow@openwrt.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 Phones")
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 Phones")
21 sip_table.template  = "cbi/tblsection"
22 sip_table.extedit   = luci.dispatcher.build_url("admin", "asterisk", "phones", "sip", "%s")
23 sip_table.addremove = true
24
25 function sip_table.filter(self, s)
26         return s and not cbimap.uci:get_bool("asterisk", s, "provider")
27 end
28
29 function sip_table.create(self, section)
30         if TypedSection.create(self, section) then
31                 created = section
32                 cbimap.uci:tset("asterisk", section, {
33                         type        = "friend",
34                         qualify     = "yes",
35                         provider    = "no",
36                         host        = "dynamic",
37                         nat         = "no",
38                         canreinvite = "no",
39                         extension   = section:match("^%d+$") and section or "",
40                         username    = section:match("^%d+$") and section or ""
41                 })
42         else
43                 self.invalid_cts = true
44         end
45 end
46
47 function sip_table.parse(self, ...)
48         TypedSection.parse(self, ...)
49         if created then
50                 cbimap.uci:save("asterisk")
51                 luci.http.redirect(luci.dispatcher.build_url(
52                         "admin", "asterisk", "phones", "sip", created
53                 ))
54         end
55 end
56
57
58 user = sip_table:option(DummyValue, "username", "Username")
59 function user.cfgvalue(self, s)
60         return sip_peers[s] and sip_peers[s].callerid or
61                 AbstractValue.cfgvalue(self, s)
62 end
63
64 host = sip_table:option(DummyValue, "host", "Hostname")
65 function host.cfgvalue(self, s)
66         if sip_peers[s] and sip_peers[s].info.address then
67                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
68         else
69                 return "n/a"
70         end
71 end
72
73 context = sip_table:option(DummyValue, "context", "Dialplan")
74 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
75
76 online = sip_table:option(DummyValue, "online", "Registered")
77 function online.cfgvalue(self, s)
78         if sip_peers[s] and sip_peers[s].info.online == nil then
79                 return "n/a"
80         else
81                 return sip_peers[s] and sip_peers[s].info.online
82                         and "yes" or "no (%s)" % {
83                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
84                         }
85         end
86 end
87
88 delay = sip_table:option(DummyValue, "delay", "Delay")
89 function delay.cfgvalue(self, s)
90         if sip_peers[s] and sip_peers[s].info.online then
91                 return "%i ms" % sip_peers[s].info.delay
92         else
93                 return "n/a"
94         end
95 end
96
97 info = sip_table:option(Button, "_info", "Info")
98 function info.write(self, s)
99         luci.http.redirect(luci.dispatcher.build_url(
100                 "admin", "asterisk", "phones", "sip", s, "info"
101         ))
102 end
103
104 return cbimap