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