bmx6: migrate init.d script to procd syntax
[feed/routing.git] / luci-app-bmx6 / files / usr / lib / lua / luci / controller / bmx6.lua
1 --[[
2 Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3 Contributors Jo-Philipp Wich <xm@subsignal.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
21 --]]
22
23 local bmx6json = require("luci.model.bmx6json")
24
25 module("luci.controller.bmx6", package.seeall)
26
27 function index()
28 local place = {}
29 local ucim = require "luci.model.uci"
30 local uci = ucim.cursor()
31
32 -- checking if ignore is on
33 if uci:get("luci-bmx6","luci","ignore") == "1" then
34 return nil
35 end
36
37 -- getting value from uci database
38 local uci_place = uci:get("luci-bmx6","luci","place")
39
40 -- default values
41 if uci_place == nil then
42 place = {"bmx6"}
43 else
44 local util = require "luci.util"
45 place = util.split(uci_place," ")
46 end
47
48 -- getting position of menu
49 local uci_position = uci:get("luci-bmx6","luci","position")
50
51 ---------------------------
52 -- Starting with the pages
53 ---------------------------
54
55 --- status (default)
56 entry(place,call("action_nodes_j"),place[#place],tonumber(uci_position))
57
58 table.insert(place,"Status")
59 entry(place,call("action_status_j"),"Status",0)
60 table.remove(place)
61
62 --- nodes
63 table.insert(place,"Nodes")
64 entry(place,call("action_nodes_j"),"Nodes",1)
65 table.remove(place)
66
67 --- links
68 table.insert(place,"Links")
69 entry(place,call("action_links"),"Links",2).leaf = true
70 table.remove(place)
71
72 -- Tunnels
73 table.insert(place,"Tunnels")
74 entry(place,call("action_tunnels_j"), "Tunnels", 3).leaf = true
75 table.remove(place)
76
77 --- Chat
78 table.insert(place,"Chat")
79 entry(place,call("action_chat"),"Chat",5)
80 table.remove(place)
81
82 --- Graph
83 table.insert(place,"Graph")
84 entry(place, template("bmx6/graph"), "Graph",4)
85 table.remove(place)
86
87 --- Topology (hidden)
88 table.insert(place,"topology")
89 entry(place, call("action_topology"), nil)
90 table.remove(place)
91
92 --- configuration (CBI)
93 table.insert(place,"Configuration")
94 entry(place, cbi("bmx6/main"), "Configuration",6).dependent=false
95
96 table.insert(place,"General")
97 entry(place, cbi("bmx6/main"), "General",1)
98 table.remove(place)
99
100 table.insert(place,"Advanced")
101 entry(place, cbi("bmx6/advanced"), "Advanced",5)
102 table.remove(place)
103
104 table.insert(place,"Interfaces")
105 entry(place, cbi("bmx6/interfaces"), "Interfaces",2)
106 table.remove(place)
107
108 table.insert(place,"Tunnels")
109 entry(place, cbi("bmx6/tunnels"), "Tunnels",3)
110 table.remove(place)
111
112 table.insert(place,"Plugins")
113 entry(place, cbi("bmx6/plugins"), "Plugins",6)
114 table.remove(place)
115
116 table.insert(place,"HNAv6")
117 entry(place, cbi("bmx6/hna"), "HNAv6",4)
118 table.remove(place)
119
120 table.remove(place)
121
122 end
123
124 function action_status_j()
125 luci.template.render("bmx6/status_j", {})
126 end
127
128
129 function action_nodes_j()
130 local http = require "luci.http"
131 local link_non_js = "/cgi-bin/luci" .. http.getenv("PATH_INFO") .. '/nodes_nojs'
132
133 luci.template.render("bmx6/nodes_j", {link_non_js=link_non_js})
134 end
135
136 function action_gateways_j()
137 luci.template.render("bmx6/gateways_j", {})
138 end
139
140 function action_tunnels_j()
141 luci.template.render("bmx6/tunnels_j", {})
142 end
143
144 function action_links(host)
145 local links = bmx6json.get("links", host)
146 local devlinks = {}
147 local _,l
148
149 if links ~= nil then
150 links = links.links
151 for _,l in ipairs(links) do
152 devlinks[l.viaDev] = {}
153 end
154 for _,l in ipairs(links) do
155 l.name = luci.util.split(l.name,'.')[1]
156 table.insert(devlinks[l.viaDev],l)
157 end
158 end
159
160 luci.template.render("bmx6/links", {links=devlinks})
161 end
162
163 function action_topology()
164 local originators = bmx6json.get("originators/all")
165 local o,i,l,i2
166 local first = true
167 local topology = '[ '
168 local cache = '/tmp/bmx6-topology.json'
169 local offset = 60
170
171 local cachefd = io.open(cache,r)
172 local update = false
173
174 if cachefd ~= nil then
175 local lastupdate = tonumber(cachefd:read("*line")) or 0
176 if os.time() >= lastupdate + offset then
177 update = true
178 else
179 topology = cachefd:read("*all")
180 end
181 cachefd:close()
182 end
183
184 if cachefd == nil or update then
185 for i,o in ipairs(originators) do
186 local links = bmx6json.get("links",o.primaryIp)
187 if links then
188 if first then
189 first = false
190 else
191 topology = topology .. ', '
192 end
193
194 topology = topology .. '{ "name": "%s", "links": [' %o.name
195
196 local first2 = true
197
198 for i2,l in ipairs(links.links) do
199 if first2 then
200 first2 = false
201 else
202 topology = topology .. ', '
203 end
204 name = l.name or l.llocalIp or "unknown"
205 topology = topology .. '{ "name": "%s", "rxRate": %s, "txRate": %s }'
206 %{ name, l.rxRate, l.txRate }
207
208 end
209
210 topology = topology .. ']}'
211 end
212
213 end
214
215 topology = topology .. ' ]'
216
217 -- Upgrading the content of the cache file
218 cachefd = io.open(cache,'w+')
219 cachefd:write(os.time()..'\n')
220 cachefd:write(topology)
221 cachefd:close()
222 end
223
224 luci.http.prepare_content("application/json")
225 luci.http.write(topology)
226 end
227
228
229 function action_chat()
230 local sms_dir = "/var/run/bmx6/sms"
231 local rcvd_dir = sms_dir .. "/rcvdSms"
232 local send_file = sms_dir .. "/sendSms/chat"
233 local sms_list = bmx6json.get("rcvdSms")
234 local sender = ""
235 local sms_file = ""
236 local chat = {}
237 local to_send = nil
238 local sent = ""
239 local fd = nil
240
241 if luci.sys.call("test -d " .. sms_dir) ~= 0 then
242 luci.template.render("bmx6/error", {txt="sms plugin disabled or some problem with directory " .. sms_dir})
243 return nil
244 end
245
246 sms_list = luci.util.split(luci.util.exec("ls "..rcvd_dir.."/*:chat"))
247
248 for _,sms_path in ipairs(sms_list) do
249 if #sms_path > #rcvd_dir then
250 sms_file = luci.util.split(sms_path,'/')
251 sms_file = sms_file[#sms_file]
252 sender = luci.util.split(sms_file,':')[1]
253
254 -- Trying to clean the name
255 if string.find(sender,".") ~= nil then
256 sender = luci.util.split(sender,".")[1]
257 end
258
259 fd = io.open(sms_path,"r")
260 chat[sender] = fd:read()
261 fd:close()
262 end
263 end
264
265 to_send = luci.http.formvalue("toSend")
266 if to_send ~= nil and #to_send > 1 then
267 fd = io.open(send_file,"w")
268 fd:write(to_send)
269 fd:close()
270 sent = to_send
271 else
272 sent = luci.util.exec("cat "..send_file)
273 end
274
275 luci.template.render("bmx6/chat", {chat=chat,sent=sent})
276 end
277