2 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
3 Licensed to the public under the Apache License 2.0.
7 local fs = require "nixio.fs"
8 local utl = require "luci.util"
13 local services_file_empty = true
14 local has_services = false
15 local uci = require "luci.model.uci".cursor()
16 local ip = require "luci.ip"
18 uci:foreach("olsrd", "LoadPlugin", function(s)
19 if s.library == "olsrd_nameservice" then
20 local services_file=s.services_file
21 if services_file and fs.access(services_file) then
23 rawdata = fs.readfile(s.services_file)
25 services_file="/var/run/services_olsr"
26 if fs.access(services_file) then
28 rawdata = fs.readfile(services_file)
31 services_file=services_file..".ipv6"
32 if services_file and fs.access(services_file) then
34 rawdata6 = fs.readfile(services_file)
36 services_file="/var/run/services_olsr.ipv6"
37 if fs.access(services_file) then
39 rawdata6 = fs.readfile(services_file)
42 if rawdata and #rawdata ~= 0 then
43 services_file_empty = nil
45 if rawdata6 and #rawdata6 ~= 0 then
46 services_file_empty = nil
52 if not has_services or services_file_empty then
56 <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
62 function fetch_services()
64 if rawdata and #rawdata ~= 0 then
65 tables = utl.split(utl.trim(rawdata), "\n", nil, true)
66 -- remove first 3 lines
68 table.remove(tables,1)
72 if rawdata6 and #rawdata6 ~= 0 then
73 tables6 = utl.split(utl.trim(rawdata6), "\n", nil, true)
74 -- remove first 3 lines
76 table.remove(tables6,1)
80 -- store last line in last_update and remove it, then remove another empty line at the end
81 last_update=table.remove(tables)
83 last_update=table.remove(tables6)
85 for k, v in ipairs(tables6) do
86 table.insert(tables, v)
90 local services = fetch_services()
92 if luci.http.formvalue("status") == "1" then
94 for k, line in ipairs(services) do
95 local field = utl.split(line, "[#|]", split, true)
96 local origin_lnk = ip.IPv6(pcdata(field[4]))
97 local origin_link = ""
98 if origin_lnk and origin_lnk:is6() then
99 origin_link = "["..origin_lnk:string().."]"
101 origin_link = pcdata(field[4])
103 local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
108 origin_link = origin_link,
112 luci.http.prepare_content("application/json")
113 luci.http.write_json(rv)
121 <script type="text/javascript">//<![CDATA[
123 XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
126 var tbody = document.getElementById('olsr_services');
130 for (var idx = 0; idx < info.length; idx++)
132 var service = info[idx];
134 '<div class="tr cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
135 '<div class="td cbi-section-table-cell" style="text-align: left"><a href="%s">%s</a></div>' +
136 '<div class="td cbi-section-table-cell" style="text-align: left">%s</div>' +
137 '<div class="td cbi-section-table-cell" style="text-align: left"><a href="http://%s/cgi-bin-status.html">%s</a></div>' +
139 service.url, service.descr, service.proto, service.origin_link, service.origin || '?'
151 <h2 name="content"><%:Services%></h2>
153 <fieldset class="cbi-section">
154 <legend><%:Internal services%></legend>
155 <div class="table cbi-section-table">
156 <div class="tr cbi-section-table-titles">
157 <div class="th cbi-section-table-cell"><%:Url%></div>
158 <div class="th cbi-section-table-cell"><%:Protocol%></div>
159 <div class="th cbi-section-table-cell"><%:Source%></div>
163 for k, line in ipairs(services) do
165 -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
166 local field = utl.split(line, "[#|]", split, true)
167 local origin_lnk = ip.IPv6(pcdata(field[4]))
169 if origin_lnk and origin_lnk:is6() then
170 origin_link = "["..origin_lnk:string().."]"
172 origin_link = pcdata(field[4])
174 local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
177 <div class="tr cbi-section-table-row cbi-rowstyle-<%=i%>">
178 <div class="td cbi-section-table-cell" style="text-align: left"><a href="<%=url%>"><%=descr%></a></div>
179 <div class="td cbi-section-table-cell" style="text-align: left"><%=proto%></div>
180 <div class="td cbi-section-table-cell" style="text-align: left"><a href="http://<%=origin_link%>/cgi-bin-status.html"><%=origin%></a></div>