2ea00672771c960868532558fc714094f27078fc
[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
15 module("luci.controller.freifunk.freifunk", package.seeall)
16
17 function index()
18         local uci = require "luci.model.uci".cursor()
19         local page
20
21         -- Frontend
22         page          = node()
23         page.lock     = true
24         page.target   = alias("freifunk")
25         page.subindex = true
26         page.index    = false
27
28         page          = node("freifunk")
29         page.title    = _("Freifunk")
30         page.target   = alias("freifunk", "index")
31         page.order    = 5
32         page.setuser  = "nobody"
33         page.setgroup = "nogroup"
34         page.i18n     = "freifunk"
35         page.index    = true
36
37         page          = node("freifunk", "index")
38         page.target   = template("freifunk/index")
39         page.title    = _("Overview")
40         page.order    = 10
41         page.indexignore = true
42
43         page          = node("freifunk", "contact")
44         page.target   = template("freifunk/contact")
45         page.title    = _("Contact")
46         page.order    = 15
47
48         page          = node("freifunk", "status")
49         page.target   = template("freifunk/public_status")
50         page.title    = _("Status")
51         page.order    = 20
52         page.i18n     = "base"
53         page.setuser  = false
54         page.setgroup = false
55
56         entry({"freifunk", "status.json"}, call("jsonstatus"))
57         entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload")
58
59         if nixio.fs.access("/usr/sbin/luci-splash") then
60                 assign({"freifunk", "status", "splash"}, {"splash", "publicstatus"}, _("Splash"), 40)
61         end
62
63         assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, _("OLSR"), 30)
64
65         if nixio.fs.access("/etc/config/luci_statistics") then
66                 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, _("Statistics"), 40)
67         end
68
69         -- backend
70         assign({"mini", "freifunk"}, {"admin", "freifunk"}, _("Freifunk"), 5)
71         entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), _("Freifunk"), 5)
72
73         page        = node("admin", "freifunk")
74         page.target = template("freifunk/adminindex")
75         page.title  = _("Freifunk")
76         page.order  = 5
77
78         page        = node("admin", "freifunk", "basics")
79         page.target = cbi("freifunk/basics")
80         page.title  = _("Basic Settings")
81         page.order  = 5
82         
83         page        = node("admin", "freifunk", "basics", "profile")
84         page.target = cbi("freifunk/profile")
85         page.title  = _("Profile")
86         page.order  = 10
87
88         page        = node("admin", "freifunk", "basics", "profile_expert")
89         page.target = cbi("freifunk/profile_expert")
90         page.title  = _("Profile (Expert)")
91         page.order  = 20
92
93         page        = node("admin", "freifunk", "Index-Page")
94         page.target = cbi("freifunk/user_index")
95         page.title  = _("Index Page")
96         page.order  = 50
97
98         page        = node("admin", "freifunk", "contact")
99         page.target = cbi("freifunk/contact")
100         page.title  = _("Contact")
101         page.order  = 15
102
103         entry({"freifunk", "map"}, template("freifunk-map/frame"), _("Map"), 50)
104         entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
105         entry({"admin", "freifunk", "profile_error"}, template("freifunk/profile_error"))
106 end
107
108 local function fetch_olsrd()
109         local sys = require "luci.sys"
110         local util = require "luci.util"
111         local table = require "table"
112         local rawdata = sys.httpget("http://127.0.0.1:2006/")
113
114         if #rawdata == 0 then
115                 if nixio.fs.access("/proc/net/ipv6_route", "r") then
116                         rawdata = sys.httpget("http://[::1]:2006/")
117                         if #rawdata == 0 then
118                                 return nil
119                         end
120                 else
121                         return nil
122                 end
123         end
124
125         local data = {}
126
127         local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
128
129
130         for i, tbl in ipairs(tables) do
131                 local lines = util.split(tbl, "\r?\n", nil, true)
132                 local name  = table.remove(lines, 1):sub(8)
133                 local keys  = util.split(table.remove(lines, 1), "\t")
134                 local split = #keys - 1
135
136                 data[name] = {}
137
138                 for j, line in ipairs(lines) do
139                         local fields = util.split(line, "\t", split)
140                         data[name][j] = {}
141                         for k, key in pairs(keys) do
142                                 data[name][j][key] = fields[k]
143                         end
144
145                         if data[name][j].Linkcost then
146                                 data[name][j].LinkQuality,
147                                 data[name][j].NLQ,
148                                 data[name][j].ETX =
149                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
150                         end
151                 end
152         end
153
154         return data
155 end
156
157 function zeroes()
158         local string = require "string"
159         local http = require "luci.http"
160         local zeroes = string.rep(string.char(0), 8192)
161         local cnt = 0
162         local lim = 1024 * 1024 * 1024
163         
164         http.prepare_content("application/x-many-zeroes")
165
166         while cnt < lim do
167                 http.write(zeroes)
168                 cnt = cnt + #zeroes
169         end
170 end
171
172 function jsonstatus()
173         local root = {}
174         local sys = require "luci.sys"
175         local uci = require "luci.model.uci"
176         local util = require "luci.util"
177         local http = require "luci.http"
178         local json = require "luci.json"
179         local ltn12 = require "luci.ltn12"
180         local version = require "luci.version"
181         local webadmin = require "luci.tools.webadmin"
182
183         local cursor = uci.cursor_state()
184
185         local ffzone = webadmin.firewall_find_zone("freifunk")
186         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
187         local ffwifs = ffznet and util.split(ffznet, " ") or {}
188
189
190         root.protocol = 1
191
192         root.system = {
193                 uptime = {sys.uptime()},
194                 loadavg = {sys.loadavg()},
195                 sysinfo = {sys.sysinfo()},
196                 hostname = sys.hostname()
197         }
198
199         root.firmware = {
200                 luciname=version.luciname,
201                 luciversion=version.luciversion,
202                 distname=version.distname,
203                 distversion=version.distversion
204         }
205
206         root.freifunk = {}
207         cursor:foreach("freifunk", "public", function(s)
208                 root.freifunk[s[".name"]] = s
209         end)
210
211         cursor:foreach("system", "system", function(s)
212                 root.geo = {
213                         latitude = s.latitude,
214                         longitude = s.longitude
215                 }
216         end)
217
218         root.network = {}
219         root.wireless = {devices = {}, interfaces = {}, status = {}}
220         local wifs = root.wireless.interfaces
221         local netdata = luci.sys.net.deviceinfo() or {}
222
223         for _, vif in ipairs(ffwifs) do
224                 root.network[vif] = cursor:get_all("network", vif)
225                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
226                 cursor:foreach("wireless", "wifi-iface", function(s)
227                         if s.device == vif and s.network == vif then
228                                 wifs[#wifs+1] = s
229                                 if s.ifname then
230                                         local iwinfo = luci.sys.wifi.getiwinfo(s.ifname)
231                                         if iwinfo then
232                                                 root.wireless.status[s.ifname] = { }
233
234                                                 local _, f
235                                                 for _, f in ipairs({
236                                                         "channel", "txpower", "bitrate", "signal", "noise",
237                                                         "quality", "quality_max", "mode", "ssid", "bssid", "encryption", "ifname"
238                                                 }) do
239                                                         root.wireless.status[s.ifname][f] = iwinfo[f]
240                                                 end
241                                         end
242                                 end
243                         end
244                 end)
245         end
246
247         root.olsrd = fetch_olsrd()
248
249         http.prepare_content("application/json")
250         ltn12.pump.all(json.Encoder(root):source(), http.write)
251 end