ba582cf5c541055f62a37f604f166f092e04dee3
[project/luci.git] / module / admin-core / src / controller / admin / index.lua
1 module("ffluci.controller.admin.index", package.seeall)
2
3 function action_wizard()
4         if ffluci.http.formvalue("ip") then
5                 return configure_freifunk()
6         end
7         
8         local ifaces = {}
9         local wldevs = ffluci.model.uci.show("wireless")
10         
11         if wldevs then
12                 for k, v in pairs(wldevs.wireless) do
13                         if v[".type"] == "wifi-device" then
14                                 table.insert(ifaces, k)
15                         end
16                 end
17         end
18         
19         ffluci.template.render("admin_index/wizard", {ifaces=ifaces})
20 end
21
22 function configure_freifunk()
23         local ip  = ffluci.http.formvalue("ip")
24         local uci = ffluci.model.uci.Session()
25         
26         -- Configure FF-Interface
27         uci:del("network", "ff")
28         uci:del("network", "ffdhcp")
29         
30         uci:set("network", "ff", nil, "interface")
31         uci:set("network", "ff", "type", "bridge")
32         uci:set("network", "ff", "proto", "static")
33         uci:set("network", "ff", "ipaddr", ip)
34         uci:set("network", "ff", "netmask", uci:get("freifunk", "community", "mask")) 
35         uci:set("network", "ff", "dns", uci:get("freifunk", "community", "dns")) 
36         
37         -- Enable internal routing
38         uci:set("freifunk", "routing", "internal", "1")
39         
40         -- Enable internet routing
41         if ffluci.http.formvalue("shareinet") then
42                 uci:set("freifunk", "routing", "internet", "1")
43         else
44                 uci:set("freifunk", "routing", "internet", "0")
45         end
46         
47         -- Configure DHCP
48         if ffluci.http.formvalue("dhcp") then
49                 local dhcpnet = uci:get("freifunk", "community", "dhcp"):match("^([0-9]+)")
50                 local dhcpip  = ip:gsub("^[0-9]+", dhcpnet)
51         
52                 uci:set("network", "ffdhcp", nil, "interface")
53                 uci:set("network", "ffdhcp", "proto", "static")
54                 uci:set("network", "ffdhcp", "ifname", "br-ff:dhcp")
55                 uci:set("network", "ffdhcp", "ipaddr", dhcpip)
56                 uci:set("network", "ffdhcp", "netmask", uci:get("freifunk", "community", "dhcpmask"))
57                 
58                 local splash = uci:show("luci_splash")
59                 if splash then
60                         for k, v in pairs(splash.luci_splash) do
61                                 if v[".type"] == "iface" then
62                                         uci:del("luci_splash", k)
63                                 end
64                         end             
65                         
66                         local sk = uci:add("luci_splash", "iface")
67                         uci:set("luci_splash", sk, "network", "ffdhcp")
68                 end             
69         end
70         
71         -- Configure OLSR
72         if ffluci.http.formvalue("olsr") and uci:show("olsr") then
73                 for k, v in pairs(uci:show("olsr").olsr) do
74                         if v[".type"] == "Interface" or v[".type"] == "LoadPlugin" then
75                                 uci:del("olsr", k)
76                         end
77                 end
78                 
79                 if ffluci.http.formvalue("shareinet") then
80                         uci:set("olsr", "dyn_gw", nil, "LoadPlugin")
81                         uci:set("olsr", "dyn_gw", "Library", "olsrd_dyn_gw.so.0.4")
82                 end
83                 
84                 uci:set("olsr", "nameservice", nil, "LoadPlugin")
85                 uci:set("olsr", "nameservice", "Library", "olsrd_nameservice.so.0.3")
86                 uci:set("olsr", "nameservice", "name", ip:gsub("%.", "-"))
87                 uci:set("olsr", "nameservice", "hosts_file", "/var/etc/hosts")
88                 uci:set("olsr", "nameservice", "suffix", ".olsr")
89                 uci:set("olsr", "nameservice", "latlon_infile", "/tmp/latlon.txt")
90                 
91                 uci:set("olsr", "txtinfo", nil, "LoadPlugin")
92                 uci:set("olsr", "txtinfo", "Library", "olsrd_txtinfo.so.0.1")
93                 uci:set("olsr", "txtinfo", "Accept", "127.0.0.1")
94                 
95                 local oif = uci:add("olsr", "Interface")
96                 uci:set("olsr", oif, "Interface", "ff")
97                 uci:set("olsr", oif, "HelloInterval", "6.0")
98                 uci:set("olsr", oif, "HelloValidityTime", "108.0")
99                 uci:set("olsr", oif, "TcInterval", "4.0")
100                 uci:set("olsr", oif, "TcValidityTime", "324.0")
101                 uci:set("olsr", oif, "MidInterval", "18.0")
102                 uci:set("olsr", oif, "MidValidityTime", "324.0")
103                 uci:set("olsr", oif, "HnaInterval", "18.0")
104                 uci:set("olsr", oif, "HnaValidityTime", "108.0")
105         end
106         
107         -- Configure Wifi
108         local wifi = ffluci.http.formvalue("wifi")
109         local wcfg = uci:show("wireless")
110         if type(wifi) == "table" and wcfg then
111                 for iface, v in pairs(wifi) do
112                         if wcfg[iface] then
113                                 -- Cleanup
114                                 for k, v in pairs(wcfg.wireless) do
115                                         if v[".type"] == "wifi-iface" and v.device == iface then
116                                                 uci:del("wireless", k)
117                                         end
118                                 end
119                                 
120                                 uci:set("wireless", iface, "disabled", "0")
121                                 uci:set("wireless", iface, "mode", "11g")
122                                 uci:set("wireless", iface, "txantenna", 1)
123                                 uci:set("wireless", iface, "rxantenna", 1)
124                                 uci:set("wireless", iface, "channel", uci:get("freifunk", "community", "channel")) 
125                                 
126                                 local wif = uci:add("wireless", "wifi-iface")
127                                 uci:set("wireless", wif, "device", iface)
128                                 uci:set("wireless", wif, "network", "ff")
129                                 uci:set("wireless", wif, "mode", "adhoc")
130                                 uci:set("wireless", wif, "ssid", uci:get("freifunk", "community", "essid"))
131                                 uci:set("wireless", wif, "bssid", uci:get("freifunk", "community", "bssid"))
132                                 uci:set("wireless", wif, "txpower", 13)
133                         end
134                 end
135         end
136                 
137
138         ffluci.http.request_redirect("admin", "uci", "changes")
139 end