Merge pull request #278 from nmav/ocserv
[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 function sip_table.filter(self, s)
38         return s and not cbimap.uci:get_bool("asterisk", s, "provider")
39 end
40
41 function sip_table.create(self, section)
42         if TypedSection.create(self, section) then
43                 created = section
44                 cbimap.uci:tset("asterisk", section, {
45                         type        = "friend",
46                         qualify     = "yes",
47                         provider    = "no",
48                         host        = "dynamic",
49                         nat         = "no",
50                         canreinvite = "no",
51                         extension   = section:match("^%d+$") and section or "",
52                         username    = section:match("^%d+$") and section or ""
53                 })
54         else
55                 self.invalid_cts = true
56         end
57 end
58
59 function sip_table.parse(self, ...)
60         TypedSection.parse(self, ...)
61         if created then
62                 cbimap.uci:save("asterisk")
63                 luci.http.redirect(luci.dispatcher.build_url(
64                         "admin", "asterisk", "phones", "sip", created
65                 ))
66         end
67 end
68
69
70 user = sip_table:option(DummyValue, "username", "Username")
71 function user.cfgvalue(self, s)
72         return sip_peers[s] and sip_peers[s].callerid or
73                 AbstractValue.cfgvalue(self, s)
74 end
75
76 host = sip_table:option(DummyValue, "host", "Hostname")
77 function host.cfgvalue(self, s)
78         if sip_peers[s] and sip_peers[s].info.address then
79                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
80         else
81                 return "n/a"
82         end
83 end
84
85 context = sip_table:option(DummyValue, "context", "Dialplan")
86 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
87
88 online = sip_table:option(DummyValue, "online", "Registered")
89 function online.cfgvalue(self, s)
90         if sip_peers[s] and sip_peers[s].info.online == nil then
91                 return "n/a"
92         else
93                 return sip_peers[s] and sip_peers[s].info.online
94                         and "yes" or "no (%s)" % {
95                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
96                         }
97         end
98 end
99
100 delay = sip_table:option(DummyValue, "delay", "Delay")
101 function delay.cfgvalue(self, s)
102         if sip_peers[s] and sip_peers[s].info.online then
103                 return "%i ms" % sip_peers[s].info.delay
104         else
105                 return "n/a"
106         end
107 end
108
109 info = sip_table:option(Button, "_info", "Info")
110 function info.write(self, s)
111         luci.http.redirect(luci.dispatcher.build_url(
112                 "admin", "asterisk", "phones", "sip", s, "info"
113         ))
114 end
115
116 return cbimap