(no commit message)
[project/luci.git] / module / admin-core / src / controller / admin / index.lua
1 module("ffluci.controller.admin.index", package.seeall)
2
3 function action_wizard()
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         
34         
35         -- Configure FF-Interface
36         uci:t_del("network", "ff")
37         uci:t_del("network", "ffdhcp")
38         
39         uci:t_set("network", "ff", nil, "interface")
40         uci:t_set("network", "ff", "type", "bridge")
41         uci:t_set("network", "ff", "proto", "static")
42         uci:t_set("network", "ff", "ipaddr", ip)
43         uci:t_set("network", "ff", "netmask", uci:t_get("freifunk", "community", "mask")) 
44         uci:t_set("network", "ff", "dns", uci:t_get("freifunk", "community", "dns")) 
45         
46         -- Enable internal routing
47         uci:t_set("freifunk", "routing", "internal", "1")
48         
49         -- Enable internet routing
50         if ffluci.http.formvalue("shareinet") then
51                 uci:t_set("freifunk", "routing", "internet", "1")
52         else
53                 uci:t_set("freifunk", "routing", "internet", "0")
54         end
55         
56         -- Configure DHCP
57         if ffluci.http.formvalue("dhcp") then
58                 local dhcpnet = uci:t_get("freifunk", "community", "dhcp"):match("^([0-9]+)")
59                 local dhcpip  = ip:gsub("^[0-9]+", dhcpnet)
60         
61                 uci:t_set("network", "ffdhcp", nil, "interface")
62                 uci:t_set("network", "ffdhcp", "proto", "static")
63                 uci:t_set("network", "ffdhcp", "ifname", "br-ff:dhcp")
64                 uci:t_set("network", "ffdhcp", "ipaddr", dhcpip)
65                 uci:t_set("network", "ffdhcp", "netmask", uci:t_get("freifunk", "community", "dhcpmask"))
66                 
67                 local dhcp = uci:t_sections("dhcp")
68                 if dhcp then
69                         for k, v in pairs(dhcp) do
70                                 if v[".type"] == "dhcp" and v.interface == "ffdhcp" then
71                                         uci:t_del("dhcp", k)
72                                 end
73                         end             
74                         
75                         local dhcpbeg = 48 + tonumber(ip:match("[0-9]+$")) * 4
76                         
77                         local sk = uci:t_add("dhcp", "dhcp")
78                         uci:t_set("dhcp", sk, "interface", "ffdhcp")
79                         uci:t_set("dhcp", sk, "start", dhcpbeg)
80                         uci:t_set("dhcp", sk, "limit", (dhcpbeg < 252) and 3 or 2)
81                         uci:t_set("dhcp", sk, "leasetime", "30m")
82                 end 
83                 
84                 local splash = uci:t_sections("luci_splash")
85                 if splash then
86                         for k, v in pairs(splash) do
87                                 if v[".type"] == "iface" then
88                                         uci:t_del("luci_splash", k)
89                                 end
90                         end             
91                         
92                         local sk = uci:t_add("luci_splash", "iface")
93                         uci:t_set("luci_splash", sk, "network", "ffdhcp")
94                 end             
95         end
96         
97         -- Configure OLSR
98         if ffluci.http.formvalue("olsr") and uci:t_sections("olsr") then
99                 for k, v in pairs(uci:t_sections("olsr")) do
100                         if v[".type"] == "Interface" or v[".type"] == "LoadPlugin" then
101                                 uci:t_del("olsr", k)
102                         end
103                 end
104                 
105                 if ffluci.http.formvalue("shareinet") then
106                         uci:t_set("olsr", "dyn_gw", nil, "LoadPlugin")
107                         uci:t_set("olsr", "dyn_gw", "Library", "olsrd_dyn_gw.so.0.4")
108                 end
109                 
110                 uci:t_set("olsr", "nameservice", nil, "LoadPlugin")
111                 uci:t_set("olsr", "nameservice", "Library", "olsrd_nameservice.so.0.3")
112                 uci:t_set("olsr", "nameservice", "name", ip:gsub("%.", "-"))
113                 uci:t_set("olsr", "nameservice", "hosts_file", "/var/etc/hosts")
114                 uci:t_set("olsr", "nameservice", "suffix", ".olsr")
115                 uci:t_set("olsr", "nameservice", "latlon_infile", "/tmp/latlon.txt")
116                 
117                 uci:t_set("olsr", "txtinfo", nil, "LoadPlugin")
118                 uci:t_set("olsr", "txtinfo", "Library", "olsrd_txtinfo.so.0.1")
119                 uci:t_set("olsr", "txtinfo", "Accept", "127.0.0.1")
120                 
121                 local oif = uci:t_add("olsr", "Interface")
122                 uci:t_set("olsr", oif, "Interface", "ff")
123                 uci:t_set("olsr", oif, "HelloInterval", "6.0")
124                 uci:t_set("olsr", oif, "HelloValidityTime", "108.0")
125                 uci:t_set("olsr", oif, "TcInterval", "4.0")
126                 uci:t_set("olsr", oif, "TcValidityTime", "324.0")
127                 uci:t_set("olsr", oif, "MidInterval", "18.0")
128                 uci:t_set("olsr", oif, "MidValidityTime", "324.0")
129                 uci:t_set("olsr", oif, "HnaInterval", "18.0")
130                 uci:t_set("olsr", oif, "HnaValidityTime", "108.0")
131         end
132         
133         -- Configure Wifi
134         local wcfg = uci:t_sections("wireless")
135         if wcfg then
136                 for iface, v in pairs(wcfg) do
137                         if v[".type"] == "wifi-device" and ffluci.http.formvalue("wifi."..iface) then
138                                 -- Cleanup
139                                 for k, j in pairs(wcfg) do
140                                         if j[".type"] == "wifi-iface" and j.device == iface then
141                                                 uci:t_del("wireless", k)
142                                         end
143                                 end
144                                 
145                                 uci:t_set("wireless", iface, "disabled", "0")
146                                 uci:t_set("wireless", iface, "mode", "11g")
147                                 uci:t_set("wireless", iface, "txantenna", 1)
148                                 uci:t_set("wireless", iface, "rxantenna", 1)
149                                 uci:t_set("wireless", iface, "channel", uci:t_get("freifunk", "community", "channel")) 
150                                 
151                                 local wif = uci:t_add("wireless", "wifi-iface")
152                                 uci:t_set("wireless", wif, "device", iface)
153                                 uci:t_set("wireless", wif, "network", "ff")
154                                 uci:t_set("wireless", wif, "mode", "adhoc")
155                                 uci:t_set("wireless", wif, "ssid", uci:t_get("freifunk", "community", "essid"))
156                                 uci:t_set("wireless", wif, "bssid", uci:t_get("freifunk", "community", "bssid"))
157                                 uci:t_set("wireless", wif, "txpower", 13)
158                         end
159                 end
160         end
161         
162         -- Save UCI
163         uci:t_save("network")
164         uci:t_save("dhcp")
165         uci:t_save("freifunk")
166         uci:t_save("luci_splash")
167         uci:t_save("olsr")
168         uci:t_save("wireless")
169
170         ffluci.http.redirect(ffluci.dispatcher.build_url("admin", "uci", "changes"))
171 end