* ffluci/statistics: fixes for interface, netlink, dns and email plugin cbi models
[project/luci.git] / modules / freifunk / src / controller / admin / index / wizard.lua
1 module("ffluci.controller.admin.index.wizard", package.seeall)
2
3 function action()
4         if ffluci.http.formvalue("ip") then
5                 return configure_freifunk()
6         end
7         
8         local ifaces = {}
9         local wldevs = ffluci.model.uci.sections("wireless")
10         
11         if wldevs then
12                 for k, v in pairs(wldevs) 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         -- Load UCI
27         uci:t_load("network")
28         uci:t_load("dhcp")
29         uci:t_load("freifunk")
30         uci:t_load("luci_splash")
31         uci:t_load("olsr")
32         uci:t_load("wireless")
33         uci:t_load("luci_fw")
34         
35         
36         -- Configure FF-Interface
37         uci:t_del("network", "ff")
38         uci:t_del("network", "ffdhcp")
39         
40         uci:t_set("network", "ff", nil, "interface")
41         uci:t_set("network", "ff", "type", "bridge")
42         uci:t_set("network", "ff", "proto", "static")
43         uci:t_set("network", "ff", "ipaddr", ip)
44         uci:t_set("network", "ff", "netmask", uci:t_get("freifunk", "community", "mask")) 
45         uci:t_set("network", "ff", "dns", uci:t_get("freifunk", "community", "dns")) 
46         
47         -- Reset Routing
48         local routing = uci:t_sections("luci_fw")
49         if routing then
50                 for k, v in pairs(routing) do
51                         if v[".type"] == "routing" and (v.iface == "ff" or v.oface == "ff") then
52                                 uci:t_del("luci_fw", k)
53                         end
54                 end
55         
56                 local int = uci:t_add("luci_fw", "routing")
57                 uci:t_set("luci_fw", int, "iface", "ff")
58                 uci:t_set("luci_fw", int, "oface", "ff")
59                 uci:t_set("luci_fw", int, "fwd", "1")
60         end
61         
62         -- Routing from Internal
63         local iface = ffluci.http.formvalue("frominternal")
64         if iface and iface ~= "" then
65                 local routing = uci:t_sections("luci_fw")
66                 if routing then
67                         for k, v in pairs(routing) do
68                                 if v[".type"] == "routing" and (v.iface == iface and v.oface == "ff") then
69                                         uci:t_del("luci_fw", k)
70                                 end
71                         end
72                 
73                         local int = uci:t_add("luci_fw", "routing")
74                         uci:t_set("luci_fw", int, "iface", iface)
75                         uci:t_set("luci_fw", int, "oface", "ff")
76                         uci:t_set("luci_fw", int, "fwd", "1")
77                         uci:t_set("luci_fw", int, "nat", "1")
78                 end             
79         end     
80         
81         -- Routing to External
82         local iface = ffluci.http.formvalue("toexternal")
83         if iface and iface ~= "" then
84                 local routing = uci:t_sections("luci_fw")
85                 if routing then
86                         for k, v in pairs(routing) do
87                                 if v[".type"] == "routing" and (v.oface == iface and v.iface == "ff") then
88                                         uci:t_del("luci_fw", k)
89                                 end
90                         end
91                 
92                         local int = uci:t_add("luci_fw", "routing")
93                         uci:t_set("luci_fw", int, "iface", "ff")
94                         uci:t_set("luci_fw", int, "oface", iface)
95                         uci:t_set("luci_fw", int, "fwd", "1")
96                         uci:t_set("luci_fw", int, "nat", "1")
97                 end             
98         end     
99         
100         -- Configure DHCP
101         if ffluci.http.formvalue("dhcp") then
102                 local dhcpnet = uci:t_get("freifunk", "community", "dhcp"):match("^([0-9]+)")
103                 local dhcpip  = ip:gsub("^[0-9]+", dhcpnet)
104         
105                 uci:t_set("network", "ffdhcp", nil, "interface")
106                 uci:t_set("network", "ffdhcp", "proto", "static")
107                 uci:t_set("network", "ffdhcp", "ifname", "br-ff:dhcp")
108                 uci:t_set("network", "ffdhcp", "ipaddr", dhcpip)
109                 uci:t_set("network", "ffdhcp", "netmask", uci:t_get("freifunk", "community", "dhcpmask"))
110                 
111                 local dhcp = uci:t_sections("dhcp")
112                 if dhcp then
113                         for k, v in pairs(dhcp) do
114                                 if v[".type"] == "dhcp" and v.interface == "ffdhcp" then
115                                         uci:t_del("dhcp", k)
116                                 end
117                         end             
118                         
119                         local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4
120                         
121                         local sk = uci:t_add("dhcp", "dhcp")
122                         uci:t_set("dhcp", sk, "interface", "ffdhcp")
123                         uci:t_set("dhcp", sk, "start", dhcpbeg)
124                         uci:t_set("dhcp", sk, "limit", (dhcpbeg < 252) and 3 or 2)
125                         uci:t_set("dhcp", sk, "leasetime", "30m")
126                 end 
127                 
128                 local splash = uci:t_sections("luci_splash")
129                 if splash then
130                         for k, v in pairs(splash) do
131                                 if v[".type"] == "iface" then
132                                         uci:t_del("luci_splash", k)
133                                 end
134                         end             
135                         
136                         local sk = uci:t_add("luci_splash", "iface")
137                         uci:t_set("luci_splash", sk, "network", "ffdhcp")
138                 end     
139                 
140                 local routing = uci:t_sections("luci_fw")
141                 if routing then
142                         for k, v in pairs(routing) do
143                                 if v[".type"] == "routing" and (v.iface == "ffdhcp" or v.oface == "ffdhcp") then
144                                         uci:t_del("luci_fw", k)
145                                 end
146                         end
147                         
148                         local int = uci:t_add("luci_fw", "routing")
149                         uci:t_set("luci_fw", int, "iface", "ffdhcp")
150                         uci:t_set("luci_fw", int, "oface", "ff")
151                         uci:t_set("luci_fw", int, "nat", "1")                   
152                         
153                         local iface = ffluci.http.formvalue("toexternal")
154                         if iface and iface ~= "" then
155                                 local int = uci:t_add("luci_fw", "routing")
156                                 uci:t_set("luci_fw", int, "iface", "ffdhcp")
157                                 uci:t_set("luci_fw", int, "oface", iface)
158                                 uci:t_set("luci_fw", int, "nat", "1")                           
159                         end
160                 end     
161         end
162         
163         -- Configure OLSR
164         if ffluci.http.formvalue("olsr") and uci:t_sections("olsr") then
165                 for k, v in pairs(uci:t_sections("olsr")) do
166                         if v[".type"] == "Interface" or v[".type"] == "LoadPlugin" then
167                                 uci:t_del("olsr", k)
168                         end
169                 end
170                 
171                 if ffluci.http.formvalue("shareinet") then
172                         uci:t_set("olsr", "dyn_gw", nil, "LoadPlugin")
173                         uci:t_set("olsr", "dyn_gw", "Library", "olsrd_dyn_gw.so.0.4")
174                 end
175                 
176                 uci:t_set("olsr", "nameservice", nil, "LoadPlugin")
177                 uci:t_set("olsr", "nameservice", "Library", "olsrd_nameservice.so.0.3")
178                 uci:t_set("olsr", "nameservice", "name", ip:gsub("%.", "-"))
179                 uci:t_set("olsr", "nameservice", "hosts_file", "/var/etc/hosts")
180                 uci:t_set("olsr", "nameservice", "suffix", ".olsr")
181                 uci:t_set("olsr", "nameservice", "latlon_infile", "/tmp/latlon.txt")
182                 
183                 uci:t_set("olsr", "txtinfo", nil, "LoadPlugin")
184                 uci:t_set("olsr", "txtinfo", "Library", "olsrd_txtinfo.so.0.1")
185                 uci:t_set("olsr", "txtinfo", "Accept", "127.0.0.1")
186                 
187                 local oif = uci:t_add("olsr", "Interface")
188                 uci:t_set("olsr", oif, "Interface", "ff")
189                 uci:t_set("olsr", oif, "HelloInterval", "6.0")
190                 uci:t_set("olsr", oif, "HelloValidityTime", "108.0")
191                 uci:t_set("olsr", oif, "TcInterval", "4.0")
192                 uci:t_set("olsr", oif, "TcValidityTime", "324.0")
193                 uci:t_set("olsr", oif, "MidInterval", "18.0")
194                 uci:t_set("olsr", oif, "MidValidityTime", "324.0")
195                 uci:t_set("olsr", oif, "HnaInterval", "18.0")
196                 uci:t_set("olsr", oif, "HnaValidityTime", "108.0")
197         end
198         
199         -- Configure Wifi
200         local wcfg = uci:t_sections("wireless")
201         if wcfg then
202                 for iface, v in pairs(wcfg) do
203                         if v[".type"] == "wifi-device" and ffluci.http.formvalue("wifi."..iface) then
204                                 -- Cleanup
205                                 for k, j in pairs(wcfg) do
206                                         if j[".type"] == "wifi-iface" and j.device == iface then
207                                                 uci:t_del("wireless", k)
208                                         end
209                                 end
210                                 
211                                 uci:t_set("wireless", iface, "disabled", "0")
212                                 uci:t_set("wireless", iface, "mode", "11g")
213                                 uci:t_set("wireless", iface, "txantenna", 1)
214                                 uci:t_set("wireless", iface, "rxantenna", 1)
215                                 uci:t_set("wireless", iface, "channel", uci:t_get("freifunk", "community", "channel")) 
216                                 
217                                 local wif = uci:t_add("wireless", "wifi-iface")
218                                 uci:t_set("wireless", wif, "device", iface)
219                                 uci:t_set("wireless", wif, "network", "ff")
220                                 uci:t_set("wireless", wif, "mode", "adhoc")
221                                 uci:t_set("wireless", wif, "ssid", uci:t_get("freifunk", "community", "essid"))
222                                 uci:t_set("wireless", wif, "bssid", uci:t_get("freifunk", "community", "bssid"))
223                                 uci:t_set("wireless", wif, "txpower", 13)
224                         end
225                 end
226         end
227         
228         -- Save UCI
229         uci:t_save("network")
230         uci:t_save("dhcp")
231         uci:t_save("freifunk")
232         uci:t_save("luci_splash")
233         uci:t_save("olsr")
234         uci:t_save("wireless")
235         uci:t_save("luci_fw")
236
237         ffluci.http.redirect(ffluci.dispatcher.build_url("admin", "uci", "changes"))
238 end