applications/luci-asterisk:
[project/luci.git] / applications / luci-asterisk / luasrc / model / cbi / asterisk / phone_sip.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 local function find_outgoing_contexts(uci)
19         local c = { }
20         local h = { }
21
22         uci:foreach("asterisk", "dialplan",
23                 function(s)
24                         if not h[s['.name']] then
25                                 c[#c+1] = { s['.name'], "Dialplan: %s" % s['.name'] }
26                                 h[s['.name']] = true
27                         end
28                 end)
29
30         uci:foreach("asterisk", "dialzone",
31                 function(s)
32                         if not h[s['.name']] then
33                                 c[#c+1] = { s['.name'], "Dialzone: %s" % s['.name'] }
34                                 h[s['.name']] = true
35                         end
36                 end)
37
38         return c
39 end
40
41 local function find_incoming_contexts(uci)
42         local c = { }
43         local h = { }
44
45         uci:foreach("asterisk", "sip",
46                 function(s)
47                         if s.context and not h[s.context] and
48                            uci:get_bool("asterisk", s['.name'], "provider")
49                         then
50                                 c[#c+1] = { s.context, "Incoming: %s" % s['.name'] or s.context }
51                                 h[s.context] = true
52                         end
53                 end)
54
55         return c
56 end
57
58
59 --
60 -- SIP phone info
61 --
62 if arg[2] == "info" then
63         form = SimpleForm("asterisk", "SIP Phone Information")
64         form.reset  = false
65         form.submit = "Back to overview"
66
67         local info, keys = ast.sip.peer(arg[1])
68         local data = { }
69
70         for _, key in ipairs(keys) do
71                 data[#data+1] = {
72                         key = key,
73                         val = type(info[key]) == "boolean"
74                                 and ( info[key] and "yes" or "no" )
75                                 or  ( info[key] == nil or #info[key] == 0 )
76                                         and "(none)"
77                                         or  tostring(info[key])
78                 }
79         end
80
81         itbl = form:section(Table, data, "SIP Phone %q" % arg[1])
82         itbl:option(DummyValue, "key", "Key")
83         itbl:option(DummyValue, "val", "Value")
84
85         function itbl.parse(...)
86                 luci.http.redirect(
87                         luci.dispatcher.build_url("admin", "asterisk", "phones")
88                 )
89         end
90
91         return form
92
93 --
94 -- SIP phone configuration
95 --
96 elseif arg[1] then
97         cbimap = Map("asterisk", "Edit SIP Client")
98
99         peer = cbimap:section(NamedSection, arg[1])
100         peer.hidden = {
101                 type        = "friend",
102                 qualify     = "yes",
103                 host        = "dynamic",
104                 nat         = "no",
105                 canreinvite = "no"
106         }
107
108         back = peer:option(DummyValue, "_overview", "Back to phone overview")
109         back.value = ""
110         back.titleref = luci.dispatcher.build_url("admin", "asterisk", "phones")
111
112         active = peer:option(Flag, "disable", "Account enabled")
113         active.enabled  = "yes"
114         active.disabled = "no"
115         function active.cfgvalue(...)
116                 return AbstractValue.cfgvalue(...) or "yes"
117         end
118
119         exten = peer:option(Value, "extension", "Extension Number")
120         cbimap.uci:foreach("asterisk", "dialplanexten",
121                 function(s)
122                         exten:value(
123                                 s.extension,
124                                 "%s (via %s/%s)" %{ s.extension, s.type:upper(), s.target }
125                         )
126                 end)
127
128         display = peer:option(Value, "callerid", "Display Name")
129
130         username  = peer:option(Value, "username", "Authorization ID")
131         password  = peer:option(Value, "secret", "Authorization Password")
132         password.password = true
133
134         regtimeout = peer:option(Value, "registertimeout", "Registration Time Value")
135         function regtimeout.cfgvalue(...)
136                 return AbstractValue.cfgvalue(...) or "60"
137         end
138
139         sipport = peer:option(Value, "port", "SIP Port")
140         function sipport.cfgvalue(...)
141                 return AbstractValue.cfgvalue(...) or "5060"
142         end
143
144         linekey = peer:option(ListValue, "_linekey", "Linekey Mode (broken)")
145         linekey:value("", "Off")
146         linekey:value("trunk", "Trunk Appearance")
147         linekey:value("call", "Call Appearance")
148
149         dialplan = peer:option(ListValue, "context", "Dialplan Context")
150         for _, v in ipairs(find_outgoing_contexts(cbimap.uci)) do
151                 dialplan:value(unpack(v))
152         end
153
154         incoming = peer:option(StaticList, "incoming", "Receive incoming calls from")
155         for _, v in ipairs(find_incoming_contexts(cbimap.uci)) do
156                 incoming:value(unpack(v))
157         end
158
159         --function incoming.cfgvalue(...)
160                 --error(table.concat(MultiValue.cfgvalue(...),"."))
161         --end
162
163         return cbimap
164 end