2928b854d1273cc7434616a669a4a99362109ece
[project/luci.git] / applications / luci-olsr-services / luasrc / view / freifunk-services / services.htm
1 <%#
2 LuCI - Lua Configuration Interface
3
4 Copyright 2011 Manuel Munz <freifunk at somakoma dot de>
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 -%>
13
14 <%
15 local fs  = require "luci.fs"
16 local utl = require "luci.util"
17 local last_update
18 local i = 1
19 local rawdata
20 local services_file_empty
21 local has_services = false
22 local uci = require "luci.model.uci".cursor()
23
24 uci:foreach("olsrd", "LoadPlugin", function(s)
25         if s.library == "olsrd_nameservice.so.0.3" then
26                 if s.services_file and fs.access(s.services_file) then
27                         has_services = true
28                         rawdata = fs.readfile(s.services_file)
29                         
30                 else
31                         if fs.access("/var/run/services_olsr") then
32                                 has_services = true
33                                 rawdata = fs.readfile("/var/run/services_olsr")
34                         end
35                 end
36                 if rawdata and #rawdata == 0 then
37                         services_file_empty = true
38                 end
39         end
40 end)
41
42
43 if not has_services or services_file_empty then
44 %>
45         <%+header%>
46         <br />
47         <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
48         <%+footer%>
49 <%
50         return
51 end
52
53 function fetch_services()
54         local tables = utl.split(utl.trim(rawdata), "\n", nil, true)
55         -- remove first 3 lines
56         for i = 1,3 do
57                 table.remove(tables,1)
58         end
59
60         -- store last line in last_update and remove it, then remove another empty line at the end
61         last_update=table.remove(tables)
62         table.remove(tables)
63         return tables
64 end
65 local services = fetch_services()
66
67 if luci.http.formvalue("status") == "1" then
68         local rv = {}
69         for k, line in ipairs(services) do
70                 local field = utl.split(line, "[#|]", split, true)
71                 local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
72                 rv[#rv+1] = {
73                         url = url,
74                         proto = proto,
75                         origin = origin,
76                         descr = descr,
77                         }
78         end
79         luci.http.prepare_content("application/json")
80         luci.http.write_json(rv)
81         return
82 end
83
84 %>
85
86 <%+header%>
87
88 <script type="text/javascript" src="<%=resource%>/cbi.js"></script>
89 <script type="text/javascript">//<![CDATA[
90
91         XHR.poll(10 , '<%=REQUEST_URI%>', { status: 1 },
92                 function(x, info)
93                 {
94                 var tbody = document.getElementById('olsr_services');
95                         if (tbody)
96                         {
97                                 var s = '';
98                                 for (var idx = 0; idx < info.length; idx++)
99                                 {
100                                 var service = info[idx];
101                                 s += String.format(
102                                         '<tr class="cbi-section-table-row cbi-rowstyle-'+(1 + (idx % 2))+'">' +
103                                                 '<td class="cbi-section-table-cell"><a href="%s">%s</a></td>' +
104                                                 '<td class="cbi-section-table-cell">%s</td>' +
105                                                 '<td class="cbi-section-table-cell"><a href="http://%s/cgi-bin-status.html">%s</a></td>' +
106                                         '</tr>',
107                                         service.url, service.descr, service.proto, service.origin, service.origin || '?'
108                                         );
109                                 }
110                                 tbody.innerHTML = s;
111                         }
112                 }
113         );
114 //]]></script>
115         
116
117
118
119         <h2><a id="content" name="content"><%:Services%></a></h2>
120
121         <fieldset class="cbi-section">
122         <legend><%:Internal services%></legend>
123         <table class="cbi-section-table">
124                 <thead>
125                         <tr class="cbi-section-table-titles">
126                                 <th class="cbi-section-table-cell"><%:Url%></th>
127                                 <th class="cbi-section-table-cell"><%:Protocol%></th>
128                                 <th class="cbi-section-table-cell"><%:Source%></th>
129                         </tr>
130                 </thead>
131
132                 <tbody id="olsr_services">
133         <%
134                 for k, line in ipairs(services) do
135                         local field = {}
136                         -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
137                         local field = utl.split(line, "[#|]", split, true)
138                         local url, proto, descr, origin = pcdata(field[1]), pcdata(field[2]), utl.trim(pcdata(field[3])), pcdata(field[4])
139                         %>
140
141                         <tr class="cbi-section-table-row cbi-rowstyle-<%=i%>">
142                                 <td class="cbi-section-table-cell"><a href="<%=url%>"><%=descr%></a></td>
143                                 <td class="cbi-section-table-cell"><%=proto%></td>
144                                 <td class="cbi-section-table-cell"><a href="http://<%=origin%>/cgi-bin-status.html"><%=origin%></a></td>
145                 </tr>
146                         <% i = ((i % 2) + 1)
147                 end %>
148                 </tbody>
149         </table>
150         <br />
151         <%=last_update%>
152         </fieldset>
153 <%+footer%>