3a993da456b0f26818d73dfffe8b52b364d38323
[project/luci.git] / applications / luci-app-asterisk / luasrc / model / cbi / asterisk / dialplans.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 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 host = sip_table:option(DummyValue, "host", "Hostname")
73 function host.cfgvalue(self, s)
74         if sip_peers[s] and sip_peers[s].info.address then
75                 return "%s:%i" %{ sip_peers[s].info.address, sip_peers[s].info.port }
76         else
77                 return "n/a"
78         end
79 end
80
81 context = sip_table:option(DummyValue, "context", "Dialplan")
82 context.href = luci.dispatcher.build_url("admin", "asterisk", "dialplan")
83 function context.cfgvalue(...)
84         return AbstractValue.cfgvalue(...) or "(default)"
85 end
86
87 online = sip_table:option(DummyValue, "online", "Registered")
88 function online.cfgvalue(self, s)
89         if sip_peers[s] and sip_peers[s].info.online == nil then
90                 return "n/a"
91         else
92                 return sip_peers[s] and sip_peers[s].info.online
93                         and "yes" or "no (%s)" %{
94                                 sip_peers[s] and sip_peers[s].info.Status:lower() or "unknown"
95                         }
96         end
97 end
98
99 delay = sip_table:option(DummyValue, "delay", "Delay")
100 function delay.cfgvalue(self, s)
101         if sip_peers[s] and sip_peers[s].info.online then
102                 return "%i ms" % sip_peers[s].info.delay
103         else
104                 return "n/a"
105         end
106 end
107
108 info = sip_table:option(Button, "_info", "Info")
109 function info.write(self, s)
110         luci.http.redirect(luci.dispatcher.build_url(
111                 "admin", "asterisk", "trunks", "sip", s, "info"
112         ))
113 end
114
115 return cbimap