modules/freifunk: When olsrd was loaded but there were no neighbors then the page...
[project/luci.git] / modules / freifunk / luasrc / view / freifunk-services / services.htm
1 <%+header%>
2
3 <%
4 local fs  = require "luci.fs"
5 local utl = require "luci.util"
6
7 -- check if nameservice plugin is enabled and where to find the services file
8 local has_services = false
9
10 local uci = require "luci.model.uci".cursor()
11 uci:foreach("olsrd", "LoadPlugin", function(s)
12         if s.library == "olsrd_nameservice.so.0.3" then
13                 if s.services_file and fs.access(s.services_file) then
14                         has_services = true
15                         services_file = s.services_file
16                 else
17                         if fs.access("/var/run/services_olsr") then
18                                 has_services = true
19                                 services_file = "/var/run/services_olsr"
20                         end
21                 end
22         end
23 end)
24
25
26 function fetch_services()
27         local rawdata = fs.readfile(services_file)
28
29         if #rawdata == 0 then
30                 return nil
31         end
32
33         local data = {}
34
35         local tables = utl.split(utl.trim(rawdata), "\n", nil, true)
36         -- remove the first 3 lines
37         table.remove(tables,1)
38         table.remove(tables,1)
39         table.remove(tables,1)
40
41         -- store last line in last_update and remove it, then remove another empty line at the end
42         last_update=table.remove(tables)
43         table.remove(tables)
44
45         for i, tbl in ipairs(tables) do
46                 local lines = utl.split(tbl, "\n", nil, true)
47         end
48         
49         return tables
50 end
51 %>
52
53 <% if has_services then %>
54 <div class="cbi-map">
55         <h2><a id="content" name="content"><%:Services%></a></h2>
56         <div class="cbi-map-descr"/>
57                 <div class="cbi-section">
58                         <div class="cbi-section-node">
59                                 <table class="cbi-section-table">
60                                         <tr class="cbi-section-table-titles">
61                                                 <th class="cbi-section-table-cell"><%:Url%></th>
62                                                 <th class="cbi-section-table-cell"><%:Source%></th>
63                                         </tr>
64
65         <% table=fetch_services()
66         local i = 1
67         for k, line in ipairs(table) do
68                 local field = {}
69                 -- split line at # and |, 1=url, 2=proto, 3=description, 4=source
70                 local field = utl.split(line, "[#|]", split, true)
71                 local url,descr,origin = pcdata(field[1]),pcdata(field[3]),pcdata(field[4])
72                 %>
73                                         <tr class="cbi-section-table-row cbi-rowstyle-<%=i%>">
74                                                 <td class="cbi-section-table-cell"><a href="<%=url%>"><%=descr%></a></td>
75                                                 <td class="cbi-section-table-cell"><%=origin%></td>
76                                         </tr>
77         <% if i == 1 then i = 0 elseif i == 0 then i = 1 end %>
78         <%end%>
79                                 </table>
80                         </div>
81                 </div>
82         </div>
83         <br />
84         <%=last_update%>
85
86 <%else%>
87         <%:No services can be shown, because olsrd is not running or the olsrd-nameservice Plugin is not loaded.%>
88 <%end%>
89
90 <%+footer%>