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