Merge pull request #2350 from TDT-AG/pr/20181204-luci-mod-system
[oweals/luci.git] / applications / luci-app-olsr-services / luasrc / view / freifunk-services / services.htm
1 <%#
2  Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
3  Licensed to the public under the Apache License 2.0.
4 -%>
5
6 <%
7 local fs  = require "nixio.fs"
8 local utl = require "luci.util"
9 local last_update
10 local i = 1
11 local rawdata
12 local rawdata6
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"
17
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
22                         has_services = true
23                         rawdata = fs.readfile(s.services_file)
24                 else
25                         services_file="/var/run/services_olsr"
26                         if fs.access(services_file) then
27                                 has_services = true
28                                 rawdata = fs.readfile(services_file)
29                         end
30                 end
31                 services_file=services_file..".ipv6"
32                 if services_file and fs.access(services_file) then
33                         has_services = true
34                         rawdata6 = fs.readfile(services_file)
35                 else
36                         services_file="/var/run/services_olsr.ipv6"
37                         if fs.access(services_file) then
38                                 has_services = true
39                                 rawdata6 = fs.readfile(services_file)
40                         end
41                 end
42                 if rawdata and #rawdata ~= 0 then
43                         services_file_empty = nil
44                 end
45                 if rawdata6 and #rawdata6 ~= 0 then
46                         services_file_empty = nil
47                 end
48         end
49 end)
50
51
52 if not has_services or services_file_empty then
53 %>
54         <%+header%>
55         <br />
56         <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
57         <%+footer%>
58 <%
59         return
60 end
61
62 function fetch_services()
63         local tables = {}
64         if rawdata and #rawdata ~= 0 then
65                 tables = utl.split(utl.trim(rawdata), "\n", nil, true)
66                 -- remove first 3 lines
67                 for i = 1,3 do
68                         table.remove(tables,1)
69                 end
70         end
71         local tables6 = {}
72         if rawdata6 and #rawdata6 ~= 0 then
73                 tables6 = utl.split(utl.trim(rawdata6), "\n", nil, true)
74                 -- remove first 3 lines
75                 for i = 1,3 do
76                         table.remove(tables6,1)
77                 end
78         end
79
80         -- store last line in last_update and remove it, then remove another empty line at the end
81         last_update=table.remove(tables)
82         table.remove(tables)
83         last_update=table.remove(tables6)
84         table.remove(tables6)
85         for k, v in ipairs(tables6) do
86                 table.insert(tables, v)
87         end
88         return tables
89 end
90 local services = fetch_services()
91
92 if luci.http.formvalue("status") == "1" then
93         local rv = {}
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().."]"
100                 else
101                         origin_link = pcdata(field[4])
102                 end
103                 local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
104                 rv[#rv+1] = {
105                         url = url,
106                         proto = proto,
107                         origin = origin,
108                         origin_link = origin_link,
109                         descr = descr,
110                         }
111         end
112         luci.http.prepare_content("application/json")
113         luci.http.write_json(rv)
114         return
115 end
116
117 %>
118
119 <%+header%>
120
121 <script type="text/javascript">//<![CDATA[
122
123         XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
124                 function(x, info)
125                 {
126                 var tbody = document.getElementById('olsr_services');
127                         if (tbody)
128                         {
129                                 var s = '<div class="tr cbi-section-table-titles">' +
130                                 '<div class="th cbi-section-table-cell"><%:Url%></div>' +
131                                 '<div class="th cbi-section-table-cell"><%:Protocol%></div>' +
132                                 '<div class="th cbi-section-table-cell"><%:Source%></div>' +
133                                 '</div>';
134
135                                 for (var idx = 0; idx < info.length; idx++)
136                                 {
137                                 var service = info[idx];
138                                 s += String.format(
139                                         '<div class="tr cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
140                                                 '<div class="td cbi-section-table-cell left"><a href="%s">%s</a></div>' +
141                                                 '<div class="td cbi-section-table-cell left">%s</div>' +
142                                                 '<div class="td cbi-section-table-cell left"><a href="http://%s/cgi-bin-status.html">%s</a></div>' +
143                                         '</div>',
144                                         service.url, service.descr, service.proto, service.origin_link, service.origin || '?'
145                                         );
146                                 }
147                                 tbody.innerHTML = s;
148                         }
149                 }
150         );
151 //]]></script>
152         
153
154
155
156         <h2 name="content"><%:Services%></h2>
157
158         <fieldset class="cbi-section">
159         <legend><%:Internal services%></legend>
160         <div class="table cbi-section-table" id="olsr_services">
161                         <div class="tr cbi-section-table-titles">
162                                 <div class="th cbi-section-table-cell"><%:Url%></div>
163                                 <div class="th cbi-section-table-cell"><%:Protocol%></div>
164                                 <div class="th cbi-section-table-cell"><%:Source%></div>
165                         </div>
166
167         <%
168                 for k, line in ipairs(services) do
169                         local field = {}
170                         -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
171                         local field = utl.split(line, "[#|]", split, true)
172                         local origin_lnk = ip.IPv6(pcdata(field[4]))
173                         local origin_link
174                         if origin_lnk and origin_lnk:is6() then
175                                 origin_link = "["..origin_lnk:string().."]"
176                         else
177                                 origin_link = pcdata(field[4])
178                         end
179                         local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
180                         %>
181
182                         <div class="tr cbi-section-table-row cbi-rowstyle-<%=i%>">
183                                 <div class="td cbi-section-table-cell left"><a href="<%=url%>"><%=descr%></a></div>
184                                 <div class="td cbi-section-table-cell left"><%=proto%></div>
185                                 <div class="td cbi-section-table-cell left"><a href="http://<%=origin_link%>/cgi-bin-status.html"><%=origin%></a></div>
186                         </div>
187                         <% i = ((i % 2) + 1)
188                 end %>
189         </div>
190         <br />
191         <%=last_update%>
192         </fieldset>
193 <%+footer%>