Rework LuCI build system
[project/luci.git] / applications / luci-app-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", "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 sip_table.sectionhead = "Extension"
37
38 function sip_table.filter(self, s)
39         return s and (
40                 cbimap.uci:get("asterisk", s, "type") == nil or
41                 cbimap.uci:get_bool("asterisk", s, "provider")
42         )
43 end
44
45 function sip_table.create(self, section)
46         if TypedSection.create(self, section) then
47                 created = section
48         else
49                 self.invalid_cts = true
50         end
51 end
52
53 function sip_table.parse(self, ...)
54         TypedSection.parse(self, ...)
55         if created then
56                 cbimap.uci:tset("asterisk", created, {
57                         type     = "friend",
58                         qualify  = "yes",
59                         provider = "yes"
60                 })
61
62                 cbimap.uci:save("asterisk")
63                 luci.http.redirect(luci.dispatcher.build_url(
64                         "admin", "asterisk", "trunks", "sip", created
65                 ))
66         end
67 end
68
69
70 user = sip_table:option(DummyValue, "username", "Username")
71
72 context = sip_table:option(DummyValue, "context", "Dialplan")
73 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
74 function context.cfgvalue(...)
75         return AbstractValue.cfgvalue(...) or "(default)"
76 end
77
78 online = sip_table:option(DummyValue, "online", "Registered")
79 function online.cfgvalue(self, s)
80         if sip_peers[s] and sip_peers[s].info.online == nil then
81                 return "n/a"
82         else
83                 return sip_peers[s] and sip_peers[s].info.online
84                         and "yes" or "no (%s)" %{
85                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
86                         }
87         end
88 end
89
90 delay = sip_table:option(DummyValue, "delay", "Delay")
91 function delay.cfgvalue(self, s)
92         if sip_peers[s] and sip_peers[s].info.online then
93                 return "%i ms" % sip_peers[s].info.delay
94         else
95                 return "n/a"
96         end
97 end
98
99 info = sip_table:option(Button, "_info", "Info")
100 function info.write(self, s)
101         luci.http.redirect(luci.dispatcher.build_url(
102                 "admin", "asterisk", "trunks", "sip", s, "info"
103         ))
104 end
105
106 return cbimap