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