modules/admin-full: rewrite wifi join wizzard
authorJo-Philipp Wich <jow@openwrt.org>
Sun, 8 Nov 2009 02:38:22 +0000 (02:38 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sun, 8 Nov 2009 02:38:22 +0000 (02:38 +0000)
modules/admin-full/luasrc/controller/admin/network.lua
modules/admin-full/luasrc/model/cbi/admin_network/iface_add.lua
modules/admin-full/luasrc/model/cbi/admin_network/wifi_add.lua [new file with mode: 0644]
modules/admin-full/luasrc/view/admin_network/wifi_join.htm
modules/admin-full/luasrc/view/admin_network/wifi_join_settings.htm [deleted file]

index a5cc2c2..590b8f0 100644 (file)
@@ -95,74 +95,20 @@ function wifi_join()
        local ssid = param("join")
 
        if dev and ssid then
-               local wep     = (tonumber(param("wep")) == 1)
-               local wpa     = tonumber(param("wpa_version")) or 0
-               local channel = tonumber(param("channel"))
-               local mode    = param("mode")
-               local bssid   = param("bssid")
+               local cancel  = (param("cancel") or param("cbi.cancel")) and true or false
 
-               local confirm = (param("confirm") == "1")
-               local cancel  = param("cancel") and true or false
-
-               if confirm and not cancel then
-                       local fixed_bssid = (param("fixed_bssid") == "1")
-                       local replace_net = (param("replace_net") == "1")
-                       local autoconnect = (param("autoconnect") == "1")
-                       local attach_intf = param("attach_intf")
-
-                       local uci = require "luci.model.uci".cursor()
-
-                       if replace_net then
-                               uci:delete_all("wireless", "wifi-iface")
-                       end
-
-                       local wificonf = {
-                               device  = dev,
-                               mode    = (mode == "Ad-Hoc" and "adhoc" or "sta"),
-                               ssid    = ssid
-                       }
-
-                       if attach_intf and uci:get("network", attach_intf) == "interface" then
-                               -- target network already has a interface, make it a bridge
-                               uci:set("network", attach_intf, "type", "bridge")
-                               uci:save("network")
-                               uci:commit("network")
-
-                               wificonf.network = attach_intf
-
-                               if autoconnect then
-                                       require "luci.sys".call("/sbin/ifup " .. attach_intf)
-                               end
-                       end
-
-                       if fixed_bssid then
-                               wificonf.bssid = bssid
-                       end
-
-                       if wep then
-                               wificonf.encryption = "wep"
-                               wificonf.key = param("key")
-                       elseif wpa > 0 then
-                               wificonf.encryption = param("wpa_suite")
-                               wificonf.key = param("key")
-                       end
-
-                       local s = uci:section("wireless", "wifi-iface", nil, wificonf)
-                       uci:delete("wireless", dev, "disabled")
-                       uci:set("wireless", dev, "channel", channel)
-
-                       uci:save("wireless")
-                       uci:commit("wireless")
-
-                       if autoconnect then
-                               require "luci.sys".call("/sbin/wifi")
-                       end
-
-                       luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless"))
-               elseif cancel then
+               if cancel then
                        luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless_join?device=" .. dev))
                else
-                       luci.template.render("admin_network/wifi_join_settings")
+                       local cbi = require "luci.cbi"
+                       local tpl = require "luci.template"
+                       local map = luci.cbi.load("admin_network/wifi_add")[1]
+
+                       if map:parse() ~= cbi.FORM_DONE then
+                               tpl.render("header")
+                               map:render()
+                               tpl.render("footer")
+                       end
                end
        else
                luci.template.render("admin_network/wifi_join")
index 3dc0ac4..f623cac 100644 (file)
@@ -35,7 +35,7 @@ newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
        ))
 
 newnet:depends("_attach", "")
-newnet.default = "net_" .. arg[1]:gsub("[^%w_]+", "_")
+newnet.default = arg[1] and "net_" .. arg[1]:gsub("[^%w_]+", "_")
 
 addnet = m:field(Value, "_netname_attach",
        translate("Network to attach interface to"))
@@ -52,7 +52,7 @@ fwzone = m:field(Value, "_fwzone",
 fwzone.template = "cbi/firewall_zonelist"
 addnet.widget = "radio"
 fwzone:depends("_attach", "")
-fwzone.default = "zone_" .. arg[1]:gsub("[^%w_]+", "_")
+fwzone.default = arg[1] and "zone_" .. arg[1]:gsub("[^%w_]+", "_")
 
 
 function attachnet.write(self, section, value)
diff --git a/modules/admin-full/luasrc/model/cbi/admin_network/wifi_add.lua b/modules/admin-full/luasrc/model/cbi/admin_network/wifi_add.lua
new file mode 100644 (file)
index 0000000..327f24e
--- /dev/null
@@ -0,0 +1,211 @@
+--[[
+LuCI - Lua Configuration Interface
+
+Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+$Id$
+]]--
+
+local nw   = require "luci.model.network"
+local fw   = require "luci.model.firewall"
+local wl   = require "luci.model.wireless"
+local uci  = require "luci.model.uci".cursor()
+local http = require "luci.http"
+
+local iw = luci.sys.wifi.getiwinfo(http.formvalue("device"))
+
+m = SimpleForm("network", translate("Join Network: Settings"))
+m.cancel = translate("Back to scan results")
+m.reset = false
+
+function m.on_cancel()
+       local dev = http.formvalue("device")
+       http.redirect(luci.dispatcher.build_url(
+               dev and "admin/network/wireless_join?device=" .. dev
+                       or "admin/network/wireless"
+       ))
+end
+
+nw.init(uci)
+fw.init(uci)
+wl.init(uci)
+
+m.hidden = {
+       device      = http.formvalue("device"),
+       join        = http.formvalue("join"),
+       channel     = http.formvalue("channel"),
+       mode        = http.formvalue("mode"),
+       bssid       = http.formvalue("bssid"),
+       wep         = http.formvalue("wep"),
+       wpa_suites      = http.formvalue("wpa_suites"),
+       wpa_version = http.formvalue("wpa_version")
+}
+
+if iw and iw.mbssid_support then
+       replace = m:field(Flag, "replace", translate("Replace wireless configuration"),
+               translate("An additional network will be created if you leave this unchecked."))
+else
+       replace = m:field(DummyValue, "replace", translate("Replace wireless configuration"))
+       replace.default = translate("The hardware is not multi-SSID capable and existing " ..
+               "configuration will be replaced if you proceed.")
+
+       function replace.formvalue() return "1" end
+end
+
+if http.formvalue("wep") == "1" then
+       key = m:field(Value, "key", translate("WEP passphrase"),
+               translate("Specify the secret encryption key here."))
+
+       key.password = true
+
+elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and m.hidden.wpa_suites == "PSK" then
+       key = m:field(Value, "key", translate("WPA passphrase"),
+               translate("Specify the secret encryption key here."))
+
+       key.password = true
+       --m.hidden.wpa_suite = (tonumber(http.formvalue("wpa_version")) or 0) >= 2 and "psk2" or "psk"
+end
+
+attachnet = m:field(Flag, "_attach", translate("Attach to existing network"),
+       translate("If the interface is attached to an existing network it will be <em>bridged</em> " ..
+               "to the existing interfaces and is covered by the firewall zone of the choosen network. " ..
+               "Uncheck this option to define a new standalone network."
+       ))
+
+attachnet.rmempty = false
+attachnet.default = http.formvalue("cbi.submit") and nil or "1"
+
+function attachnet.formvalue(self, section)
+       if not http.formvalue("cbi.submit") then
+               return m.hidden.mode == "Ad-Hoc" and "0" or "1"
+       else
+               return Value.formvalue(self, section) and "1" or "0"
+       end
+end
+
+attachnet.cfgvalue = attachnet.formvalue
+
+newnet = m:field(Value, "_netname_new", translate("Name of the new network"),
+       translate("The allowed characters are: <code>A-Z</code>, <code>a-z</code>, " ..
+               "<code>0-9</code> and <code>_</code>"
+       ))
+
+newnet:depends("_attach", "")
+newnet.default = m.hidden.mode == "Ad-Hoc" and "mesh"
+
+addnet = m:field(Value, "_netname_attach",
+       translate("Network to attach interface to"))
+
+addnet.template = "cbi/network_netlist"
+addnet.widget = "radio"
+addnet.default = "wan"
+addnet.nocreate = true
+addnet:depends("_attach", "1")
+
+fwzone = m:field(Value, "_fwzone",
+       translate("Create / Assign firewall-zone"),
+       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."))
+
+fwzone.template = "cbi/firewall_zonelist"
+fwzone:depends("_attach", "")
+fwzone.default = m.hidden.mode == "Ad-Hoc" and "mesh"
+
+function attachnet.parse(self, section)
+       Flag.parse(self, section)
+
+       if http.formvalue("cbi.submit") then
+               local net, zone
+               local value = self:formvalue(section)
+
+               if value == "1" then
+                       net = nw:get_network(addnet:formvalue(section))
+                       if net then
+                               net:type("bridge")
+                       end
+               else
+                       local zval = fwzone:formvalue(section)
+
+                       net = nw:add_network(newnet:formvalue(section), { proto = "dhcp" })
+                       zone = fw:get_zone(zval)
+
+                       if not zone and zval == '-' then
+                               zval = m:formvalue(fwzone:cbid(section) .. ".newzone")
+                               if zval and #zval > 0 then
+                                       zone = fw:add_zone(zval)
+                               end
+                       end
+               end
+
+               if not net then
+                       self.error = { [section] = "missing" }
+               else
+                       local wdev = wl:get_device(m.hidden.device)
+                       wdev:disabled(false)
+                       wdev:channel(m.hidden.channel)
+
+                       if replace:formvalue(section) then
+                               local n
+                               for _, n in ipairs(wdev:get_networks()) do
+                                       wl:del_network(n:name())
+                               end
+                       end
+
+                       local wconf = {
+                               device  = m.hidden.device,
+                               ssid    = m.hidden.join,
+                               mode    = (m.hidden.mode == "Ad-Hoc" and "adhoc" or "sta"),
+                               network = net:name()
+                       }
+
+                       if m.hidden.wep == "1" then
+                               wconf.encryption = "wep"
+                               wconf.key        = key and key:formvalue(section) or ""
+                       elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
+                               wconf.encryption = (tonumber(m.hidden.wpa_version) or 0) >= 2 and "psk2" or "psk"
+                               wconf.key        = key and key:formvalue(section) or ""
+                       else
+                               wconf.encryption = "none"
+                       end
+
+                       if wconf.mode == "adhoc" then
+                               wconf.bssid = m.hidden.bssid
+                       end
+
+                       local wnet = wl:add_network(wconf)
+
+                       if wnet then
+                               if zone then
+                                       fw:del_network(net:name())
+                                       zone:add_network(net:name())
+                               end
+
+                               uci:save("wireless")
+                               uci:save("network")
+                               uci:save("firewall")
+
+                               uci:commit("wireless")
+                               uci:commit("network")
+                               uci:commit("firewall")
+
+                               luci.http.redirect(luci.dispatcher.build_url("admin/network/wireless",
+                                       wdev:name(), wnet:name()))
+                       end
+               end
+       end
+end
+
+attachnet.remove = attachnet.write
+
+function fwzone.cfgvalue(self, section)
+       self.iface = section
+       local z = fw:get_zone_by_network(section)
+       return z and z:name()
+end
+
+return m
index 43ba40b..6ed4627 100644 (file)
@@ -71,7 +71,7 @@ $Id$
 
 <%+header%>
 
-<h2><a id="content" name="content"><%:Wireless Scan%></a></h2>
+<h2><a id="content" name="content"><%:Join Network: Wireless Scan%></a></h2>
 
 <div class="cbi-map">
        <fieldset class="cbi-section">
diff --git a/modules/admin-full/luasrc/view/admin_network/wifi_join_settings.htm b/modules/admin-full/luasrc/view/admin_network/wifi_join_settings.htm
deleted file mode 100644 (file)
index b29de61..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-<%#
-LuCI - Lua Configuration Interface
-Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-$Id$
-
--%>
-
-<%-
-
-       local sys = require "luci.sys"
-       local utl = require "luci.util"
-       local uci = require "luci.model.uci".cursor_state()
-
-       local ifaces = { }
-       uci:foreach("network", "interface", function(i)
-               if i.ifname ~= "lo" then
-                       ifaces[#ifaces+1] = { i['.name'], i.ifname, i.ipaddr }
-               end
-       end)
-
-       local dev = luci.http.formvalue("device")
-       local iw = luci.sys.wifi.getiwinfo(dev)
-
-       local requirement
-
-       if iwinfo.type(dev) == "broadcom" and not nixio.fs.access("/usr/sbin/nas") then
-               requirement = luci.i18n.translatef("You need to install the <a href='%s'>Broadcom <em>nas</em> supplicant</a> to use WPA!"
-                       % luci.dispatcher.build_url("admin/system/packages?url=nas&amp;submit=1"))
-       elseif not nixio.fs.access("/usr/sbin/wpa_supplicant") then
-               requirement = luci.i18n.translatef("You need to install <a href='%s'><em>wpa-supplicant</em></a> to use WPA!"
-                       % luci.dispatcher.build_url("admin/system/packages?url=wpa-supplicant&amp;submit=1"))
-       end
-
--%>
-
-<%+header%>
-
-<h2><a id="content" name="content"><%:Join Network%></a></h2>
-
-<form method="post" action="<%=REQUEST_URI%>">
-       <div class="cbi-map">
-               <div class="cbi-map-descr">
-                       <%=luci.i18n.translatef("You are about to join the wireless network <em><strong>%s</strong></em>. " ..
-                               "In order to complete the process, you need to provide some additional details.",
-                               utl.pcdata(luci.http.formvalue("join") or "(hidden)")
-                       )%>
-               </div>
-
-               <fieldset class="cbi-section">
-                       <input type="hidden" name="confirm" value="1" />
-                       <input type="hidden" name="join" value="<%=utl.pcdata(luci.http.formvalue("join"))%>" />
-                       <input type="hidden" name="device" value="<%=utl.pcdata(luci.http.formvalue("device"))%>" />
-                       <input type="hidden" name="mode" value="<%=luci.http.formvalue("mode")%>" />
-                       <input type="hidden" name="bssid" value="<%=luci.http.formvalue("bssid")%>" />
-                       <input type="hidden" name="channel" value="<%=luci.http.formvalue("channel")%>" />
-                       <input type="hidden" name="wep" value="<%=luci.http.formvalue("wep")%>" />
-                       <input type="hidden" name="wpa_version" value="<%=luci.http.formvalue("wpa_version")%>" />
-
-                       <% if luci.http.formvalue("wep") == "1" then %>
-                               <label for="pw_key">WEP passphrase</label><br />
-                               <input class="cbi-input-password" type="password" name="key" id="pw_key" />
-                               <br /><br />
-                       <% elseif tonumber(luci.http.formvalue("wpa_version") or 0) > 0 and luci.http.formvalue("wpa_suites") == "PSK" then %>
-                               <label for="pw_key">WPA passphrase</label><br />
-                               <input class="cbi-input-password" type="password" name="key" id="pw_key" />
-
-                               <% if tonumber(luci.http.formvalue("wpa_version") or 0) == 3 then %>
-                                       <select name="wpa_suite">
-                                               <option value="psk">WPA-PSK</option>
-                                               <option value="psk2" selected="selected">WPA2-PSK</option>
-                                               <option value="psk+psk2">WPA/WPA2-PSK mixed mode</option>
-                                       </select>
-                               <% else %>
-                                       <input type="hidden" name="wpa_suite" value="psk<%=tonumber(luci.http.formvalue("wpa_version") or 0) == 2 and 2%>" />
-                               <% end %>
-
-                               <% if requirement then %>
-                                       <strong class="error">&nbsp; <%=requirement%></strong>
-                               <% end %>
-
-                               <br /><br />
-                       <% end %>
-
-                       <label for="sel_attach_intf">Attach wireless to</label><br />
-                       <select name="attach_intf" id="sel_attach_intf">
-                               <% for _, i in ipairs(ifaces) do %>
-                                       <option<% if i[1] == "wan" then %> selected="selected"<% end %> value="<%=i[1]%>"><%=i[1]%> (<%=i[2]%><% if i[3] then %> - <%=i[3]%><% end %>)</option>
-                               <% end %>
-                               <option value="">-- no interface --</option>
-                       </select>
-
-                       <br/><br />
-                       <hr /><br />
-
-                       <% if luci.http.formvalue("mode") == "Ad-Hoc" then %>
-                       <input type="checkbox" name="fixed_bssid" value="1" id="cb_fixed_bssid" checked="checked" />
-                       <label for="cb_fixed_bssid">Lock BSSID to <%=luci.http.formvalue("bssid")%></label>
-                       <br />
-                       <% end %>
-
-                       <% if iw.mbssid_support then %>
-                               <input type="checkbox" name="replace_net" value="1" id="cb_replace_net" checked="checked" />
-                               <label for="cb_replace_net">Overwrite existing wireless configuration</label>
-                               <br />
-                       <% else %>
-                               <input type="hidden" name="replace_net" value="1" />
-                       <% end %>
-
-                       <input type="checkbox" name="autoconnect" value="1" id="cb_autoconnect" checked="checked" />
-                       <label for="cb_autoconnect">Automatically connect</label>
-               </fieldset>
-       </div>
-       <div class="cbi-page-actions">
-               <input class="cbi-button-apply" type="submit" value="<%:Join Network%>" />
-               <input class="cbi-button-reset" type="submit" name="cancel" value="<%:Back to scan results%>" />
-       </div>
-</form>
-
-<%+footer%>