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