* luci/app/ffwizard: add more communities to Freifunk selection list
[project/luci.git] / applications / luci-ffwizard-leipzig / luasrc / model / cbi / ffwizard.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14
15 ]]--
16
17
18 local uci = require "luci.model.uci".cursor()
19 local tools = require "luci.tools.ffwizard"
20
21
22 -------------------- View --------------------
23 f = SimpleForm("ffwizward", "Freifunkassistent",
24  "Dieser Assistent unterstüzt bei der Einrichtung des Routers für das Freifunknetz.")
25
26
27 dev = f:field(ListValue, "device", "WLAN-Gerät")
28 uci:foreach("wireless", "wifi-device",
29         function(section)
30                 dev:value(section[".name"])
31         end)
32
33
34 main = f:field(Flag, "wifi", "Freifunkzugang einrichten")
35
36 net = f:field(Value, "net", "Freifunknetz")
37 net.rmempty = true
38 net:depends("wifi", "1")
39 net:value("104.0", "Berlin (104.0)")
40 net:value("104.59", "Leisnig/Gadow/div. (104.59)")
41 net:value("104.61", "Leipzig (104.61)")
42 net:value("104.62", "Halle (104.62)")
43 net:value("191.161", "Augsburg (191.161)")
44
45 function net.cfgvalue(self, section)
46         return uci:get("freifunk", "wizard", "net")
47 end
48 function net.write(self, section, value)
49         uci:set("freifunk", "wizard", "net", value)
50         uci:save("freifunk")
51 end
52
53
54 subnet = f:field(ListValue, "subnet", "Subnetz (Projekt)")
55 subnet.rmempty = true
56 subnet:depends("wifi", "1")
57 for i=0, 255 do
58         subnet:value(i)
59 end
60 function subnet.cfgvalue(self, section)
61         return uci:get("freifunk", "wizard", "subnet")
62 end
63 function subnet.write(self, section, value)
64         uci:set("freifunk", "wizard", "subnet", value)
65         uci:save("freifunk")
66 end
67
68 node = f:field(Value, "node", "Knoten")
69 node.rmempty = true
70 node:depends("wifi", "1")
71 for i=1, 51 do
72         node:value(i)
73 end
74 function node.cfgvalue(self, section)
75         return uci:get("freifunk", "wizard", "node")
76 end
77 function node.write(self, section, value)
78         uci:set("freifunk", "wizard", "node", value)
79         uci:save("freifunk")
80 end
81
82 client = f:field(Flag, "client", "WLAN-DHCP anbieten")
83 client:depends("wifi", "1")
84
85
86 olsr = f:field(Flag, "olsr", "OLSR einrichten")
87
88 share = f:field(ListValue, "sharenet", "Eigenen Internetzugang freigeben")
89 share:value("maybe", "-- keine Aktion --")
90 share:value("yes", "einschalten")
91 share:value("no", "ausschalten")
92
93
94
95 -------------------- Control --------------------
96 function f.handle(self, state, data)
97         if state == FORM_VALID then
98                 luci.http.redirect(luci.dispatcher.build_url("admin", "uci", "changes"))
99                 return false
100         elseif state == FORM_INVALID then
101                 self.errmessage = "Ungültige Eingabe: Bitte die Formularfelder auf Fehler prüfen."
102         end
103         return true
104 end
105
106 local function _strip_internals(tbl)
107         tbl = tbl or {}
108         for k, v in pairs(tbl) do
109                 if k:sub(1, 1) == "." then
110                         tbl[k] = nil
111                 end
112         end
113         return tbl
114 end
115
116 -- Configure Freifunk checked
117 function main.write(self, section, value)
118         if value == "0" then
119                 return
120         end
121
122         local device = dev:formvalue(section)
123
124         -- Collect IP-Address
125         local inet = net:formvalue(section)
126         local isubnet = subnet:formvalue(section)
127         local inode = node:formvalue(section)
128
129         -- Invalidate fields
130         if not inet then
131                 net.tag_missing[section] = true
132         end
133         if not isubnet then
134                 subnet.tag_missing[section] = true
135         end
136         if not inode then
137                 node.tag_missing[section] = true
138         end
139
140         if not inet or not isubnet or not inode then
141                 return
142         end
143
144         local ip = "%s.%s.%s" % {inet, isubnet, inode}
145
146
147         -- Cleanup
148         luci.util.perror("1")
149         tools.wifi_delete_ifaces(device)
150         luci.util.perror("2")
151         tools.network_remove_interface(device)
152         luci.util.perror("3")
153         tools.firewall_zone_remove_interface("freifunk", device)
154
155
156         -- Tune wifi device
157         local devconfig = _strip_internals(uci:get_all("freifunk", "wifi_device"))
158         uci:tset("wireless", device, devconfig)
159
160         -- Create wifi iface
161         local ifconfig = _strip_internals(uci:get_all("freifunk", "wifi_iface"))
162         ifconfig.device = device
163         ifconfig.network = device
164         uci:section("wireless", "wifi-iface", nil, ifconfig)
165
166         -- Save wifi
167         uci:save("wireless")
168
169         -- Create firewall zone and add default rules (first time)
170         local newzone = tools.firewall_create_zone("freifunk", "DROP", "ACCEPT", "DROP", true)
171         if newzone then
172                 uci:foreach("freifunk", "fw_forwarding", function(section)
173                         uci:section("firewall", "forwarding", nil, _strip_internals(section))
174                 end)
175
176                 uci:foreach("freifunk", "fw_rule", function(section)
177                         uci:section("firewall", "rule", nil, _strip_internals(section))
178                 end)
179
180                 uci:save("firewall")
181         end
182
183
184         -- Crate network interface
185         local netconfig = _strip_internals(uci:get_all("freifunk", "interface"))
186         netconfig.proto = "static"
187         netconfig.ipaddr = ip
188         uci:section("network", "interface", device, netconfig)
189
190         uci:save("network")
191
192         tools.firewall_zone_add_interface("freifunk", device)
193 end
194
195
196 function olsr.write(self, section, value)
197         if value == "0" then
198                 return
199         end
200
201         local device = dev:formvalue(section)
202
203         -- Delete old interface
204         uci:delete_all("freifunk", "Interface", {Interface=device})
205
206         -- Write new interface
207         local olsrbase = _strip_internals(uci:get_all("freifunk", "olsr_interface"))
208         olsrbase.interface = device
209         olsrbase.ignore    = "0"
210         uci:section("olsrd", "Interface", nil, olsrbase)
211         uci:save("olsrd")
212 end
213
214
215 function share.write(self, section, value)
216         if value == "maybe" then
217                 return
218         end
219
220         uci:delete_all("firewall", "forwarding", {src="freifunk", dest="wan"})
221
222         if value == "yes" then
223                 uci:section("firewall", "forwarding", nil, {src="freifunk", dest="wan"})
224         end
225         uci:save("firewall")
226 end
227
228
229 function client.write(self, section, value)
230         if value == "0" then
231                 return
232         end
233
234         local device = dev:formvalue(section)
235
236         -- Collect IP-Address
237         local inet = net:formvalue(section)
238         local isubnet = subnet:formvalue(section)
239         local inode = node:formvalue(section)
240
241         if not inet or not isubnet or not inode then
242                 return
243         end
244
245         local dhcpbeg = 48 + tonumber(inode) * 4
246         local dclient = "%s.%s.%s" % {inet:gsub("^[0-9]+", "10"), isubnet, dhcpbeg}
247         local limit = dhcpbeg < 252 and 3 or 2
248
249         -- Delete old alias
250         uci:delete("network", device .. "dhcp")
251
252         -- Create alias
253         local aliasbase = _strip_internals(uci:get_all("freifunk", "alias"))
254         aliasbase.interface = device
255         aliasbase.ipaddr = dclient
256         aliasbase.proto = "static"
257         uci:section("network", "alias", device .. "dhcp", aliasbase)
258         uci:save("network")
259
260
261         -- Create dhcp
262         local dhcpbase = _strip_internals(uci:get_all("freifunk", "dhcp"))
263         dhcpbase.interface = device .. "dhcp"
264         dhcpbase.start = dhcpbeg
265         dhcpbase.limit = limit
266
267         uci:section("dhcp", "dhcp", device .. "dhcp", dhcpbase)
268         uci:save("dhcp")
269
270
271         -- Delete old splash
272         uci:delete_all("luci_splash", "iface", {net=device, zone="freifunk"})
273
274         -- Register splash
275         uci:section("luci_splash", "iface", nil, {net=device, zone="freifunk"})
276         uci:save("luci_splash")
277 end
278
279 return f