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