Update qos.lua to add custom QOS classes to luci droplist
[project/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.so.0.3" 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" src="<%=resource%>/cbi.js"></script>
122 <script type="text/javascript">//<![CDATA[
123
124         XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
125                 function(x, info)
126                 {
127                 var tbody = document.getElementById('olsr_services');
128                         if (tbody)
129                         {
130                                 var s = '';
131                                 for (var idx = 0; idx < info.length; idx++)
132                                 {
133                                 var service = info[idx];
134                                 s += String.format(
135                                         '<tr class="cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
136                                                 '<td class="cbi-section-table-titles"><a href="%s">%s</a></td>' +
137                                                 '<td class="cbi-section-table-titles">%s</td>' +
138                                                 '<td class="cbi-section-table-titles"><a href="http://%s/cgi-bin-status.html">%s</a></td>' +
139                                         '</tr>',
140                                         service.url, service.descr, service.proto, service.origin_link, service.origin || '?'
141                                         );
142                                 }
143                                 tbody.innerHTML = s;
144                         }
145                 }
146         );
147 //]]></script>
148         
149
150
151
152         <h2><a id="content" name="content"><%:Services%></a></h2>
153
154         <fieldset class="cbi-section">
155         <legend><%:Internal services%></legend>
156         <table class="cbi-section-table">
157                 <thead>
158                         <tr class="cbi-section-table-titles">
159                                 <th class="cbi-section-table-cell"><%:Url%></th>
160                                 <th class="cbi-section-table-cell"><%:Protocol%></th>
161                                 <th class="cbi-section-table-cell"><%:Source%></th>
162                         </tr>
163                 </thead>
164
165                 <tbody id="olsr_services">
166         <%
167                 for k, line in ipairs(services) do
168                         local field = {}
169                         -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
170                         local field = utl.split(line, "[#|]", split, true)
171                         local origin_lnk = ip.IPv6(pcdata(field[4]))
172                         local origin_link
173                         if origin_lnk and origin_lnk:is6() then
174                                 origin_link = "["..origin_lnk:string().."]"
175                         else
176                                 origin_link = pcdata(field[4])
177                         end
178                         local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
179                         %>
180
181                         <tr class="cbi-section-table-row cbi-rowstyle-<%=i%>">
182                                 <td class="cbi-section-table-titles"><a href="<%=url%>"><%=descr%></a></td>
183                                 <td class="cbi-section-table-titles"><%=proto%></td>
184                                 <td class="cbi-section-table-titles"><a href="http://<%=origin_link%>/cgi-bin-status.html"><%=origin%></a></td>
185                         </tr>
186                         <% i = ((i % 2) + 1)
187                 end %>
188                 </tbody>
189         </table>
190         <br />
191         <%=last_update%>
192         </fieldset>
193 <%+footer%>