convert luci.fs users to nixio.fs api
[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 nixio.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
75         entry({"freifunk", "map"}, template("freifunk-map/frame"), i18n("freifunk_map", "Karte"), 50)
76         entry({"freifunk", "map", "content"}, template("freifunk-map/map"), nil, 51)
77 end
78
79 local function fetch_olsrd()
80         local sys = require "luci.sys"
81         local util = require "luci.util"
82         local table = require "table"
83         local rawdata = sys.httpget("http://127.0.0.1:2006/")
84
85         if #rawdata == 0 then
86                 if nixio.fs.access("/proc/net/ipv6_route", "r") then
87                         rawdata = sys.httpget("http://[::1]:2006/")
88                         if #rawdata == 0 then
89                                 return nil
90                         end
91                 else
92                         return nil
93                 end
94         end
95
96         local data = {}
97
98         local tables = util.split(util.trim(rawdata), "\r?\n\r?\n", nil, true)
99
100
101         for i, tbl in ipairs(tables) do
102                 local lines = util.split(tbl, "\r?\n", nil, true)
103                 local name  = table.remove(lines, 1):sub(8)
104                 local keys  = util.split(table.remove(lines, 1), "\t")
105                 local split = #keys - 1
106
107                 data[name] = {}
108
109                 for j, line in ipairs(lines) do
110                         local fields = util.split(line, "\t", split)
111                         data[name][j] = {}
112                         for k, key in pairs(keys) do
113                                 data[name][j][key] = fields[k]
114                         end
115
116                         if data[name][j].Linkcost then
117                                 data[name][j].LinkQuality,
118                                 data[name][j].NLQ,
119                                 data[name][j].ETX =
120                                 data[name][j].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
121                         end
122                 end
123         end
124
125         return data
126 end
127
128 function zeroes()
129         local string = require "string"
130         local http = require "luci.http"
131         local zeroes = string.rep(string.char(0), 8192)
132         local cnt = 0
133         local lim = 1024 * 1024 * 1024
134         
135         http.prepare_content("application/x-many-zeroes")
136
137         while cnt < lim do
138                 http.write(zeroes)
139                 cnt = cnt + #zeroes
140         end
141 end
142
143 function jsonstatus()
144         local root = {}
145         local sys = require "luci.sys"
146         local uci = require "luci.model.uci"
147         local util = require "luci.util"
148         local http = require "luci.http"
149         local json = require "luci.json"
150         local ltn12 = require "luci.ltn12"
151         local version = require "luci.version"
152         local webadmin = require "luci.tools.webadmin"
153
154         local cursor = uci.cursor_state()
155
156         local ffzone = webadmin.firewall_find_zone("freifunk")
157         local ffznet = ffzone and cursor:get("firewall", ffzone, "network")
158         local ffwifs = ffznet and util.split(ffznet, " ") or {}
159
160
161         root.protocol = 1
162
163         root.system = {
164                 uptime = {sys.uptime()},
165                 loadavg = {sys.loadavg()},
166                 sysinfo = {sys.sysinfo()},
167                 hostname = sys.hostname()
168         }
169
170         root.firmware = {
171                 luciname=version.luciname,
172                 luciversion=version.luciversion,
173                 distname=version.distname,
174                 distversion=version.distversion
175         }
176
177         root.freifunk = {}
178         cursor:foreach("freifunk", "public", function(s)
179                 root.freifunk[s[".name"]] = s
180         end)
181
182         cursor:foreach("system", "system", function(s)
183                 root.geo = {
184                         latitude = s.latitude,
185                         longitude = s.longitude
186                 }
187         end)
188
189         root.network = {}
190         root.wireless = {devices = {}, interfaces = {}, status = {}}
191         local wifs = root.wireless.interfaces
192         local wifidata = luci.sys.wifi.getiwconfig() or {}
193         local netdata = luci.sys.net.deviceinfo() or {}
194
195         for _, vif in ipairs(ffwifs) do
196                 root.network[vif] = cursor:get_all("network", vif)
197                 root.wireless.devices[vif] = cursor:get_all("wireless", vif)
198                 cursor:foreach("wireless", "wifi-iface", function(s)
199                         if s.device == vif and s.network == vif then
200                                 wifs[#wifs+1] = s
201                                 if s.ifname then
202                                         root.wireless.status[s.ifname] = wifidata[s.ifname]
203                                 end
204                         end
205                 end)
206         end
207
208         root.olsrd = fetch_olsrd()
209
210         http.prepare_content("application/json")
211         ltn12.pump.all(json.Encoder(root):source(), http.write)
212 end