applications, modules: remove i18n handling from controller modules as it moved to...
[project/luci.git] / applications / luci-olsr / luasrc / controller / olsr.lua
1 module("luci.controller.olsr", package.seeall)
2
3 function index()
4         if not nixio.fs.access("/etc/config/olsrd") then
5                 return
6         end
7
8         local page  = node("admin", "status", "olsr")
9         page.target = template("status-olsr/overview")
10         page.title  = _("OLSR")
11         page.i18n   = "olsr"
12         page.subindex = true
13
14         local page  = node("admin", "status", "olsr", "neighbors")
15         page.target = call("action_neigh")
16         page.title  = _("Neighbours")
17         page.subindex = true
18         page.order  = 5
19
20         local page  = node("admin", "status", "olsr", "routes")
21         page.target = call("action_routes")
22         page.title  = _("Routes")
23         page.order  = 10
24
25         local page  = node("admin", "status", "olsr", "topology")
26         page.target = call("action_topology")
27         page.title  = _("Topology")
28         page.order  = 20
29
30         local page  = node("admin", "status", "olsr", "hna")
31         page.target = call("action_hna")
32         page.title  = _("HNA")
33         page.order  = 30
34
35         local page  = node("admin", "status", "olsr", "mid")
36         page.target = call("action_mid")
37         page.title  = _("MID")
38         page.order  = 50
39
40         local page  = node("admin", "status", "olsr", "smartgw")
41         page.target = call("action_smartgw")
42         page.title  = _("SmartGW")
43         page.order  = 60
44
45         local page  = node("admin", "status", "olsr", "interfaces")
46         page.target = call("action_interfaces")
47         page.title  = _("Interfaces")
48         page.order  = 70
49
50         local ol = entry(
51                 {"admin", "services", "olsrd"},
52                 cbi("olsr/olsrd"), "OLSR"
53         )
54         ol.i18n = "olsr"
55         ol.subindex = true
56
57         entry(
58                 {"admin", "services", "olsrd", "iface"},
59                 cbi("olsr/olsrdiface")
60         ).leaf = true
61
62         entry(
63                 {"admin", "services", "olsrd", "hna"},
64                 cbi("olsr/olsrdhna"), _("HNA Announcements")
65         )
66
67         oplg = entry(
68                 {"admin", "services", "olsrd", "plugins"},
69                 cbi("olsr/olsrdplugins"), _("Plugins")
70         )
71
72         odsp = entry(
73                 {"admin", "services", "olsrd", "display"},
74                 cbi("olsr/olsrddisplay"), _("Display")
75                 )
76
77         oplg.i18n = "olsr"
78         oplg.leaf = true
79         oplg.subindex = true
80
81         local uci = require("luci.model.uci").cursor()
82         uci:foreach("olsrd", "LoadPlugin",
83                 function (section)
84                         local lib = section.library
85                         entry(
86                                 {"admin", "services", "olsrd", "plugins", lib },
87                                 cbi("olsr/olsrdplugins"),
88                                 nil --'Plugin "%s"' % lib:gsub("^olsrd_",""):gsub("%.so.+$","")
89                         )
90                 end
91         )
92 end
93
94 function action_neigh()
95         local data = fetch_txtinfo("links")
96
97         if not data or not data.Links then
98                 luci.template.render("status-olsr/error_olsr")
99                 return nil
100         end
101
102         local function compare(a, b)
103                 local c = tonumber(a.Cost)
104                 local d = tonumber(b.Cost)
105
106                 if not c or c == 0 then
107                         return false
108                 end
109
110                 if not d or d == 0 then
111                         return true
112                 end
113
114                 return c < d
115         end
116
117         table.sort(data.Links, compare)
118
119         luci.template.render("status-olsr/neighbors", {links=data.Links})
120 end
121
122 function action_routes()
123         local data = fetch_txtinfo("routes")
124
125         if not data or not data.Routes then
126                 luci.template.render("status-olsr/error_olsr")
127                 return nil
128         end
129
130         local function compare(a, b)
131                 local c = tonumber(a.ETX)
132                 local d = tonumber(b.ETX)
133
134                 if not c or c == 0 then
135                         return false
136                 end
137
138                 if not d or d == 0 then
139                         return true
140                 end
141
142                 return c < d
143         end
144
145         table.sort(data.Routes, compare)
146
147         luci.template.render("status-olsr/routes", {routes=data.Routes})
148 end
149
150 function action_topology()
151         local data = fetch_txtinfo("topology")
152
153         if not data or not data.Topology then
154                 luci.template.render("status-olsr/error_olsr")
155                 return nil
156         end
157
158         local function compare(a, b)
159                 return a["Dest. IP"] < b["Dest. IP"]
160         end
161
162         table.sort(data.Topology, compare)
163
164         luci.template.render("status-olsr/topology", {routes=data.Topology})
165 end
166
167 function action_hna()
168         local data = fetch_txtinfo("hna")
169
170         if not data or not data.HNA then
171                 luci.template.render("status-olsr/error_olsr")
172                 return nil
173         end
174
175         local function compare(a, b)
176                 return a.Destination < b.Destination
177         end
178
179         table.sort(data.HNA, compare)
180
181         luci.template.render("status-olsr/hna", {routes=data.HNA})
182 end
183
184 function action_mid()
185         local data = fetch_txtinfo("mid")
186
187         if not data or not data.MID then
188                 luci.template.render("status-olsr/error_olsr")
189                 return nil
190         end
191
192         local function compare(a, b)
193                 return a["IP address"] < b["IP address"]
194         end
195
196         table.sort(data.MID, compare)
197
198         luci.template.render("status-olsr/mid", {mids=data.MID})
199 end
200
201 function action_smartgw()
202         local data = fetch_txtinfo("gateways")
203
204         if not data or not data.Gateways then
205                 luci.template.render("status-olsr/error_olsr")
206                 return nil
207         end
208
209         local function compare(a, b)
210                 return a["ETX"] < b["ETX"]
211         end
212
213         table.sort(data.Gateways, compare)
214
215         luci.template.render("status-olsr/smartgw", {gws=data.Gateways})
216 end
217
218 function action_interfaces()
219         local data = fetch_txtinfo("interfaces")
220
221         if not data or not data.Interfaces then
222                 luci.template.render("status-olsr/error_olsr")
223                 return nil
224         end
225
226         luci.template.render("status-olsr/interfaces", {iface=data.Interfaces})
227 end
228
229 -- Internal
230 function fetch_txtinfo(otable)
231         require("luci.sys")
232         local uci = require "luci.model.uci".cursor_state()
233         local resolve = uci:get("luci_olsr", "general", "resolve")
234         otable = otable or ""
235         local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
236         local rawdatav6 = luci.sys.httpget("http://[::1]:2006/"..otable)
237         local data = {}
238         local dataindex = 0
239         local name = ""
240         local defaultgw
241
242         if #rawdata ~= 0 then
243                 local tables = luci.util.split(luci.util.trim(rawdata), "\r?\n\r?\n", nil, true)
244
245                 if otable == "links" then
246                         local route = {}
247                         luci.sys.net.routes(function(r) if r.dest:prefix() == 0 then defaultgw = r.gateway:string() end end)
248                 end
249
250                 for i, tbl in ipairs(tables) do
251                         local lines = luci.util.split(tbl, "\r?\n", nil, true)
252                         name  = table.remove(lines, 1):sub(8)
253                         local keys  = luci.util.split(table.remove(lines, 1), "\t")
254                         local split = #keys - 1
255                         if not data[name] then
256                                 data[name] = {}
257                         end
258
259                         for j, line in ipairs(lines) do
260                                 dataindex = ( dataindex + 1 )
261                                 di = dataindex
262                                 local fields = luci.util.split(line, "\t", split)
263                                 data[name][di] = {}
264                                 for k, key in pairs(keys) do
265                                         if key == "Remote IP" or key == "Dest. IP" or key == "Gateway IP" or key == "Gateway" then
266                                                 data[name][di][key] = fields[k]
267                                                 if resolve == "1" then
268                                                         hostname = nixio.getnameinfo(fields[k], "inet")
269                                                         if hostname then
270                                                                 data[name][di]["Hostname"] = hostname
271                                                         end
272                                                 end
273                                                 if key == "Remote IP" and defaultgw then
274                                                         if defaultgw == fields[k] then
275                                                                 data[name][di]["defaultgw"] = 1
276                                                         end
277                                                 end
278                                         elseif key == "Local IP" then
279                                                 data[name][di][key] = fields[k]
280                                                 data[name][di]['Local Device'] = fields[k]
281                                                 uci:foreach("network", "interface",
282                                                         function(s)
283                                                                 localip = string.gsub(fields[k], '      ', '')
284                                                                 if s.ipaddr == localip then
285                                                                         data[name][di]['Local Device'] = s['.name'] or interface
286                                                                 end
287                                                         end)
288                                         elseif key == "Interface" then
289                                                 data[name][di][key] = fields[k]
290                                                 uci:foreach("network", "interface",
291                                                 function(s)
292                                                         interface = string.gsub(fields[k], '    ', '')
293                                                         if s.ifname == interface then
294                                                                 data[name][di][key] = s['.name'] or interface
295                                                         end
296                                                 end)
297                                         else
298                                             data[name][di][key] = fields[k]
299                                 end
300                                 end
301                                 if data[name][di].Linkcost then
302                                         data[name][di].LinkQuality,
303                                         data[name][di].NLQ,
304                                         data[name][di].ETX =
305                                         data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
306                                 end
307                         end
308                 end
309         end
310
311         if #rawdatav6 ~= 0 then
312                 local tables = luci.util.split(luci.util.trim(rawdatav6), "\r?\n\r?\n", nil, true)
313                 for i, tbl in ipairs(tables) do
314                         local lines = luci.util.split(tbl, "\r?\n", nil, true)
315                         name  = table.remove(lines, 1):sub(8)
316                         local keys  = luci.util.split(table.remove(lines, 1), "\t")
317                         local split = #keys - 1
318                         if not data[name] then
319                                 data[name] = {}
320                         end
321                         for j, line in ipairs(lines) do
322                                 dataindex = ( dataindex + 1 )
323                                 di = dataindex
324                                 local fields = luci.util.split(line, "\t", split)
325                                 data[name][di] = {}
326                                 for k, key in pairs(keys) do
327                                         if key == "Remote IP" then
328                                                 data[name][di][key] = "[" .. fields[k] .. "]"
329                                                 if resolve == "1" then
330                                                         hostname = nixio.getnameinfo(fields[k], "inet6")
331                                                         if hostname then
332                                                                 data[name][di]["Hostname"] = hostname
333                                                         end
334                                                 end
335                                         elseif key == "Local IP" then
336                                                 data[name][di][key] = fields[k]
337                                                 data[name][di]['Local Device'] = fields[k]
338                                                 uci:foreach("network", "interface",
339                                                 function(s)
340                                                         local localip = string.gsub(fields[k], '        ', '')
341                                                         if s.ip6addr then
342                                                                 local ip6addr = string.gsub(s.ip6addr, '\/.*', '')
343                                                                 if ip6addr == localip then
344                                                                         data[name][di]['Local Device'] = s['.name'] or s.interface
345                                                                 end
346                                                         end
347                                                 end)
348                                         elseif key == "Dest. IP" then
349                                                 data[name][di][key] = "[" .. fields[k] .. "]"
350                                         elseif key == "Last hop IP" then
351                                                 data[name][di][key] = "[" .. fields[k] .. "]"
352                                         elseif key == "IP address" then
353                                                 data[name][di][key] = "[" .. fields[k] .. "]"
354                                         elseif key == "Gateway" then
355                                                 data[name][di][key] = "[" .. fields[k] .. "]"
356                                         else
357                                                 data[name][di][key] = fields[k]
358                                         end
359                                 end
360
361                                 if data[name][di].Linkcost then
362                                         data[name][di].LinkQuality,
363                                         data[name][di].NLQ,
364                                         data[name][di].ETX =
365                                         data[name][di].Linkcost:match("([%w.]+)/([%w.]+)[%s]+([%w.]+)")
366                                 end
367                         end
368                 end
369         end
370
371
372         if data then
373             return data
374         end
375 end