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