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