Freufunk: I CAN HAZ ZEROES PLZKTHX
[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
19         local page  = node()
20         page.lock   = true
21         page.target = alias("freifunk")
22         page.subindex = true
23         page.index = false
24
25         local page    = node("freifunk")
26         page.title    = "Freifunk"
27         page.target   = alias("freifunk", "index")
28         page.order    = 5
29         page.setuser  = "nobody"
30         page.setgroup = "nogroup"
31         page.i18n     = "freifunk"
32         page.index    = true
33
34         local page  = node("freifunk", "index")
35         page.target = template("freifunk/index")
36         page.title  = "Übersicht"
37         page.order  = 10
38         page.indexignore = true
39
40         local page  = node("freifunk", "index", "contact")
41         page.target = template("freifunk/contact")
42         page.title  = "Kontakt"
43
44         entry({"freifunk", "status"}, alias("freifunk", "status", "status"), "Status", 20)
45
46         local page  = node("freifunk", "status", "status")
47         page.target = form("freifunk/public_status")
48         page.title  = i18n("overview")
49         page.order  = 20
50         page.i18n   = "admin-core"
51         page.setuser  = false
52         page.setgroup = false
53
54         entry({"freifunk", "status.json"}, call("jsonstatus"))
55         entry({"freifunk", "status", "zeroes"}, call("zeroes"), "Testdownload") 
56
57         assign({"freifunk", "olsr"}, {"admin", "status", "olsr"}, "OLSR", 30)
58
59         if luci.fs.access("/etc/config/luci_statistics") then
60                 assign({"freifunk", "graph"}, {"admin", "statistics", "graph"}, i18n("stat_statistics", "Statistiken"), 40)
61         end
62
63         assign({"mini", "freifunk"}, {"admin", "freifunk"}, "Freifunk", 15)
64         entry({"admin", "freifunk"}, alias("admin", "freifunk", "index"), "Freifunk", 15)
65         local page  = node("admin", "freifunk", "index")
66         page.target = cbi("freifunk/freifunk")
67         page.title  = "Freifunk"
68         page.order  = 30
69
70         local page  = node("admin", "freifunk", "contact")
71         page.target = cbi("freifunk/contact")
72         page.title  = "Kontakt"
73         page.order  = 40
74 end
75
76 local function fetch_olsrd()
77         local sys = require "luci.sys"
78         local util = require "luci.util"
79         local table = require "table"
80         local rawdata = sys.httpget("http://127.0.0.1:2006/")
81
82         if #rawdata == 0 then
83                 if luci.fs.access("/proc/net/ipv6_route", "r") then
84                         rawdata = sys.httpget("http://[::1]:2006/")
85                         if #rawdata == 0 then
86                                 return nil
87                         end
88                 else
89                         return nil
90                 end
91         end
92
93         local data = {}
94
95         local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
96
97
98         for i, tbl in ipairs(tables) do
99                 local lines = util.split(tbl, "\r?\n", nil, true)
100                 local name  = table.remove(lines, 1):sub(8)
101                 local keys  = util.split(table.remove(lines, 1), "\t")
102                 local split = #keys - 1
103
104                 data[name] = {}
105
106                 for j, line in ipairs(lines) do
107                         local fields = util.split(line, "\t", split)
108                         data[name][j] = {}
109                         for k, key in pairs(keys) do
110                                 data[name][j][key] = fields[k]
111                         end
112
113                         if data[name][j].Linkcost then
114                                 data[name][j].LinkQuality,
115                                 data[name][j].NLQ,
116                                 data[name][j].ETX =
117                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
118                         end
119                 end
120         end
121
122         return data
123 end
124
125 function zeroes()
126         local string = require "string"
127         local http = require "luci.http"
128         local zeroes = string.rep(string.char(0), 8192)
129         local cnt = 0
130         local lim = 1024 * 1024 * 1024
131         
132         http.prepare_content("application/x-many-zeroes")
133
134         while cnt < lim do
135                 http.write(zeroes)
136                 cnt = cnt + #zeroes
137         end
138 end
139
140 function jsonstatus()
141         local root = {}
142         local sys = require "luci.sys"
143         local uci = require "luci.model.uci"
144         local util = require "luci.util"
145         local http = require "luci.http"
146         local json = require "luci.json"
147         local ltn12 = require "luci.ltn12"
148         local version = require "luci.version"
149         local webadmin = require "luci.tools.webadmin"
150
151         local cursor = uci.cursor_state()
152
153         local ffzone = webadmin.firewall_find_zone("freifunk")
154         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
155         local ffwifs = ffznet and util.split(ffznet, " ") or {}
156
157
158         root.protocol = 1
159
160         root.system = {
161                 uptime = {sys.uptime()},
162                 loadavg = {sys.loadavg()},
163                 sysinfo = {sys.sysinfo()},
164                 hostname = sys.hostname()
165         }
166
167         root.firmware = {
168                 luciname=version.luciname,
169                 luciversion=version.luciversion,
170                 distname=version.distname,
171                 distversion=version.distversion
172         }
173
174         root.freifunk = {}
175         cursor:foreach("freifunk", "public", function(s)
176                 root.freifunk[s[".name"]] = s
177         end)
178
179         cursor:foreach("system", "system", function(s)
180                 root.geo = {
181                         latitude = s.latitude,
182                         longitude = s.longitude
183                 }
184         end)
185
186         root.network = {}
187         root.wireless = {devices = {}, interfaces = {}, status = {}}
188         local wifs = root.wireless.interfaces
189         local wifidata = luci.sys.wifi.getiwconfig() or {}
190         local netdata = luci.sys.net.deviceinfo() or {}
191
192         for _, vif in ipairs(ffwifs) do
193                 root.network[vif] = cursor:get_all("network", vif)
194                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
195                 cursor:foreach("wireless", "wifi-iface", function(s)
196                         if s.device == vif and s.network == vif then
197                                 wifs[#wifs+1] = s
198                                 if s.ifname then
199                                         root.wireless.status[s.ifname] = wifidata[s.ifname]
200                                 end
201                         end
202                 end)
203         end
204
205         root.olsrd = fetch_olsrd()
206
207         http.prepare_content("application/json")
208         ltn12.pump.all(json.Encoder(root):source(), http.write)
209 end