applications/ffwizard: Massive changes to the ffwizard to make it more generic. Also...
[project/luci.git] / modules / freifunk / luasrc / controller / freifunk / freifunk.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14 module("luci.controller.freifunk.freifunk", package.seeall)
15
16 function index()
17 local i18n = luci.i18n.translate
18 local uci = require "luci.model.uci".cursor()
19
20 -- Frontend
21 local page = node()
22 page.lock = true
23 page.target = alias("freifunk")
24 page.subindex = true
25 page.index = false
26
27 local page = node("freifunk")
28 page.title = "Freifunk"
29 page.target = alias("freifunk", "index")
30 page.order = 5
31 page.setuser = "nobody"
32 page.setgroup = "nogroup"
33 page.i18n = "freifunk"
34 page.index = true
35
36 local page = node("freifunk", "index")
37 page.target = template("freifunk/index")
38 page.title = "√úbersicht"
39 page.order = 10
40 page.indexignore = true
41
42 local page = node("freifunk", "index", "contact")
43 page.target = template("freifunk/contact")
44 page.title = "Kontakt"
45 page.order = 10
46
47 local page = node("freifunk", "status")
48 page.target = template("freifunk/public_status")
49 page.title = i18n("Status")
50 page.order = 20
51 page.i18n = "base"
52 page.setuser = false
53 page.setgroup = false
54
55 entry({"freifunk", "status.json"}, call("jsonstatus"))
56 entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
57 entry({"freifunk", "status", "public_status_json"}, call("public_status_json")).leaf = true
58
59 assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
60
61 if nixio.fs.access("/etc/config/luci_statistics") then
62 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("Statistics"), 40)
63 end
64
65 -- backend
66 assign({"mini", "freifunk"}, {"admin", "freifunk"}, "Freifunk", 5)
67 entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 5)
68
69 local page = node("admin", "freifunk")
70 page.target = template("freifunk/adminindex")
71 page.title = "Freifunk"
72 page.order = 5
73
74 local page = node("admin", "freifunk", "basics")
75 page.target = cbi("freifunk/basics")
76 page.title = "Grundeinstellungen"
77 page.order = 5
78
79 local page = node("admin", "freifunk", "basics", "profile")
80 page.target = cbi("freifunk/profile")
81 page.title = "Profile"
82 page.order = 10
83
84 local page = node("admin", "freifunk", "basics", "profile_expert")
85 page.target = cbi("freifunk/profile_expert")
86 page.title = "Profile (Expert)"
87 page.order = 20
88
89 local page = node("admin", "freifunk", "Index-Page")
90 page.target = cbi("freifunk/user_index")
91 page.title = "Index-Page"
92 page.order = 50
93
94 local page = node("admin", "freifunk", "contact")
95 page.target = cbi("freifunk/contact")
96 page.title = "Kontakt"
97 page.order = 15
98
99 entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("Karte"), 50)
100 entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
101
102 uci:foreach("olsrd", "LoadPlugin", function(s)
103 if s.library == "olsrd_nameservice.so.0.3" then
104 has_serv = true
105 end
106 end)
107 if has_serv then
108 entry({"freifunk", "services"}, template("freifunk-services/services"), i18n("Services"), 60)
109 end
110 end
111
112 local function fetch_olsrd()
113 local sys = require "luci.sys"
114 local util = require "luci.util"
115 local table = require "table"
116 local rawdata = sys.httpget("http://127.0.0.1:2006/")
117
118 if #rawdata == 0 then
119 if nixio.fs.access("/proc/net/ipv6_route", "r") then
120 rawdata = sys.httpget("http://[::1]:2006/")
121 if #rawdata == 0 then
122 return nil
123 end
124 else
125 return nil
126 end
127 end
128
129 local data = {}
130
131 local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
132
133
134 for i, tbl in ipairs(tables) do
135 local lines = util.split(tbl, "\r?\n", nil, true)
136 local name = table.remove(lines, 1):sub(8)
137 local keys = util.split(table.remove(lines, 1), "\t")
138 local split = #keys - 1
139
140 data[name] = {}
141
142 for j, line in ipairs(lines) do
143 local fields = util.split(line, "\t", split)
144 data[name][j] = {}
145 for k, key in pairs(keys) do
146 data[name][j][key] = fields[k]
147 end
148
149 if data[name][j].Linkcost then
150 data[name][j].LinkQuality,
151 data[name][j].NLQ,
152 data[name][j].ETX =
153 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
154 end
155 end
156 end
157
158 return data
159 end
160
161 function zeroes()
162 local string = require "string"
163 local http = require "luci.http"
164 local zeroes = string.rep(string.char(0), 8192)
165 local cnt = 0
166 local lim = 1024 * 1024 * 1024
167
168 http.prepare_content("application/x-many-zeroes")
169
170 while cnt < lim do
171 http.write(zeroes)
172 cnt = cnt + #zeroes
173 end
174 end
175
176 function jsonstatus()
177 local root = {}
178 local sys = require "luci.sys"
179 local uci = require "luci.model.uci"
180 local util = require "luci.util"
181 local http = require "luci.http"
182 local json = require "luci.json"
183 local ltn12 = require "luci.ltn12"
184 local version = require "luci.version"
185 local webadmin = require "luci.tools.webadmin"
186
187 local cursor = uci.cursor_state()
188
189 local ffzone = webadmin.firewall_find_zone("freifunk")
190 local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
191 local ffwifs = ffznet and util.split(ffznet, " ") or {}
192
193
194 root.protocol = 1
195
196 root.system = {
197 uptime = {sys.uptime()},
198 loadavg = {sys.loadavg()},
199 sysinfo = {sys.sysinfo()},
200 hostname = sys.hostname()
201 }
202
203 root.firmware = {
204 luciname=version.luciname,
205 luciversion=version.luciversion,
206 distname=version.distname,
207 distversion=version.distversion
208 }
209
210 root.freifunk = {}
211 cursor:foreach("freifunk", "public", function(s)
212 root.freifunk[s[".name"]] = s
213 end)
214
215 cursor:foreach("system", "system", function(s)
216 root.geo = {
217 latitude = s.latitude,
218 longitude = s.longitude
219 }
220 end)
221
222 root.network = {}
223 root.wireless = {devices = {}, interfaces = {}, status = {}}
224 local wifs = root.wireless.interfaces
225 local wifidata = luci.sys.wifi.getiwconfig() or {}
226 local netdata = luci.sys.net.deviceinfo() or {}
227
228 for _, vif in ipairs(ffwifs) do
229 root.network[vif] = cursor:get_all("network", vif)
230 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
231 cursor:foreach("wireless", "wifi-iface", function(s)
232 if s.device == vif and s.network == vif then
233 wifs[#wifs+1] = s
234 if s.ifname then
235 root.wireless.status[s.ifname] = wifidata[s.ifname]
236 end
237 end
238 end)
239 end
240
241 root.olsrd = fetch_olsrd()
242
243 http.prepare_content("application/json")
244 ltn12.pump.all(json.Encoder(root):source(), http.write)
245 end
246
247 function public_status_json()
248 local twa = require "luci.tools.webadmin"
249 local sys = require "luci.sys"
250 local i18n = require "luci.i18n"
251 local path = luci.dispatcher.context.requestpath
252 local rv = { }
253
254 local dev
255 for dev in path[#path]:gmatch("[%w%.%-]+") do
256 local j = { id = dev }
257 local iw = luci.sys.wifi.getiwinfo(dev)
258 if iw then
259 local f
260 for _, f in ipairs({
261 "channel", "txpower", "bitrate", "signal", "noise",
262 "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
263 }) do
264 j[f] = iw[f]
265 end
266 end
267 rv[#rv+1] = j
268 end
269
270 local load1, load5, load15 = sys.loadavg()
271
272 local _, _, memtotal, memcached, membuffers, memfree = sys.sysinfo()
273 local mem = string.format("%.2f MB (%.2f %s, %.2f %s, %.2f %s, %.2f %s)",
274 tonumber(memtotal) / 1024,
275 tonumber(memtotal - memfree) / 1024,
276 tostring(i18n.translate("used")),
277 memfree / 1024,
278 tostring(i18n.translate("free")),
279 memcached / 1024,
280 tostring(i18n.translate("cached")),
281 membuffers / 1024,
282 tostring(i18n.translate("buffered"))
283 )
284
285 local dr4 = sys.net.defaultroute()
286 local dr6 = sys.net.defaultroute6()
287
288 if dr6 then
289 def6 = {
290 gateway = dr6.nexthop:string(),
291 dest = dr6.dest:string(),
292 dev = dr6.device,
293 metr = dr6.metric }
294 end
295
296 if dr4 then
297 def4 = {
298 gateway = dr4.gateway:string(),
299 dest = dr4.dest:string(),
300 dev = dr4.device,
301 metr = dr4.metric }
302 end
303
304 rv[#rv+1] = {
305 time = os.date("%c"),
306 uptime = twa.date_format(tonumber(sys.uptime())),
307 load = string.format("%.2f, %.2f, %.2f", load1, load5, load15),
308 mem = mem,
309 defroutev4 = def4,
310 defroutev6 = def6
311 }
312
313 luci.http.prepare_content("application/json")
314 luci.http.write_json(rv)
315 return
316 end