libs/web: combine apply actions of all maps on a page, prevents concurrent XHR
[project/luci.git] / modules / admin-full / luasrc / model / cbi / admin_network / wifi_add.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2009 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 local nw   = require "luci.model.network"
16 local fw   = require "luci.model.firewall"
17 local uci  = require "luci.model.uci".cursor()
18 local http = require "luci.http"
19
20 local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
21
22 if not iw then
23         luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
24         return
25 end
26
27 m = SimpleForm("network", translate("Join Network: Settings"))
28 m.cancel = translate("Back to scan results")
29 m.reset = false
30
31 function m.on_cancel()
32         local dev = http.formvalue("device")
33         http.redirect(luci.dispatcher.build_url(
34                 dev and "admin/network/wireless_join?device=" .. dev
35                         or "admin/network/wireless"
36         ))
37 end
38
39 nw.init(uci)
40 fw.init(uci)
41
42 m.hidden = {
43         device      = http.formvalue("device"),
44         join        = http.formvalue("join"),
45         channel     = http.formvalue("channel"),
46         mode        = http.formvalue("mode"),
47         bssid       = http.formvalue("bssid"),
48         wep         = http.formvalue("wep"),
49         wpa_suites      = http.formvalue("wpa_suites"),
50         wpa_version = http.formvalue("wpa_version")
51 }
52
53 if iw and iw.mbssid_support then
54         replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
55                 translate("An additional network will be created if you leave this unchecked."))
56
57         function replace.cfgvalue() return "1" end
58 else
59         replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
60         replace.default = translate("The hardware is not multi-SSID capable and existing " ..
61                 "configuration will be replaced if you proceed.")
62
63         function replace.formvalue() return "1" end
64 end
65
66 if http.formvalue("wep") == "1" then
67         key = m:field(Value, "key", translate("WEP passphrase"),
68                 translate("Specify the secret encryption key here."))
69
70         key.password = true
71         key.datatype = "wepkey"
72
73 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
74         (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
75 then
76         key = m:field(Value, "key", translate("WPA passphrase"),
77                 translate("Specify the secret encryption key here."))
78
79         key.password = true
80         key.datatype = "wpakey"
81         --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
82 end
83
84 newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
85         translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
86                 "<code>0-9</code> and <code>_</code>"
87         ))
88
89 newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wwan"
90 newnet.datatype = "uciname"
91
92 fwzone = m:field(Value, "_fwzone",
93         translate("Create / Assign firewall-zone"),
94         translate("Choose the firewall zone you want to assign to this interface. Select <em>unspecified</em> to remove the interface from the associated zone or fill out the <em>create</em> field to define a new zone and attach the interface to it."))
95
96 fwzone.template = "cbi/firewall_zonelist"
97 fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh" or "wan"
98
99 function newnet.parse(self, section)
100         local net, zone
101         local value = self:formvalue(section)
102         local zval  = fwzone:formvalue(section)
103
104         net = nw:add_network(value, { proto = "dhcp" })
105         zone = fw:get_zone(zval)
106
107         if not zone and zval == '-' then
108                 zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
109                 if zval and #zval > 0 then
110                         zone = fw:add_zone(zval)
111                 end
112         end
113
114         if not net then
115                 self.error = { [section] = "missing" }
116         else
117                 local wdev = nw:get_wifidev(m.hidden.device)
118
119                 wdev:set("disabled", false)
120                 wdev:set("channel", m.hidden.channel)
121
122                 if replace:formvalue(section) then
123                         local n
124                         for _, n in ipairs(wdev:get_wifinets()) do
125                                 wdev:del_wifinet(n)
126                         end
127                 end
128
129                 local wconf = {
130                         device  = m.hidden.device,
131                         ssid    = m.hidden.join,
132                         mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
133                         network = net:name()
134                 }
135
136                 if m.hidden.wep == "1" then
137                         wconf.encryption = "wep"
138                         wconf.key        = key and key:formvalue(section) or ""
139                 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
140                         wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
141                         wconf.key        = key and key:formvalue(section) or ""
142                 else
143                         wconf.encryption = "none"
144                 end
145
146                 if wconf.mode == "adhoc" then
147                         wconf.bssid = m.hidden.bssid
148                 end
149
150                 local wnet = wdev:add_wifinet(wconf)
151                 if wnet then
152                         if zone then
153                                 fw:del_network(net:name())
154                                 zone:add_network(net:name())
155                         end
156
157                         uci:save("wireless")
158                         uci:save("network")
159                         uci:save("firewall")
160
161                         luci.http.redirect(luci.dispatcher.build_url(
162                                 "admin/network/wireless", wdev:name(), wnet:ifname()
163                         ))
164                 end
165         end
166 end
167
168 function fwzone.cfgvalue(self, section)
169         self.iface = section
170         local z = fw:get_zone_by_network(section)
171         return z and z:name()
172 end
173
174 return m