luci-app-bmx: Redesign Makefile. Split JS libraries. Clean package
[feed/routing.git] / luci-app-bmx6 / bmx6 / usr / lib / lua / luci / model / bmx6json.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 ltn12 = require("luci.ltn12")
24 local json = require("luci.json")
25 local util = require("luci.util")
26 local uci = require("luci.model.uci")
27 local sys = require("luci.sys")
28 local template = require("luci.template")
29 local http = require("luci.http")
30 local string = require("string")
31 local table = require("table")
32 local nixio = require("nixio")
33 local nixiofs = require("nixio.fs")
34 local ipairs = ipairs
35
36 module "luci.model.bmx6json"
37
38 -- Returns a LUA object from bmx6 JSON daemon
39
40 function get(field, host)
41 local url
42 if host ~= nil then
43 if host:match(":") then
44 url = 'http://[%s]/cgi-bin/bmx6-info?' % host
45 else
46 url = 'http://%s/cgi-bin/bmx6-info?' % host
47 end
48 else
49 url = uci.cursor():get("luci-bmx6","luci","json")
50 end
51
52 if url == nil then
53 print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data",true)
54 return nil
55 end
56
57 local json_url = util.split(url,":")
58 local raw = ""
59
60 if json_url[1] == "http" then
61 raw,err = wget(url..field,1000)
62 else
63
64 if json_url[1] == "exec" then
65 raw = sys.exec(json_url[2]..' '..field)
66 else
67 print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:",true)
68 return nil
69 end
70
71 end
72
73 local data = nil
74
75 if raw and raw:len() > 10 then
76 local decoder = json.Decoder()
77 ltn12.pump.all(ltn12.source.string(raw), decoder:sink())
78 data = decoder:get()
79 -- else
80 -- print_error("Cannot get data from bmx6 daemon",true)
81 -- return nil
82 end
83
84 return data
85 end
86
87 function print_error(txt,popup)
88 util.perror(txt)
89 sys.call("logger -t bmx6json " .. txt)
90
91 if popup then
92 http.write('<script type="text/javascript">alert("Some error detected, please check it: '..txt..'");</script>')
93 else
94 http.write("<h1>Dammit! some error detected</h1>")
95 http.write("bmx6-luci: " .. txt)
96 http.write('<p><FORM><INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></FORM></p>')
97 end
98
99 end
100
101 function text2html(txt)
102 txt = string.gsub(txt,"<","{")
103 txt = string.gsub(txt,">","}")
104 txt = util.striptags(txt)
105 return txt
106 end
107
108
109 function wget(url, timeout)
110 local rfd, wfd = nixio.pipe()
111 local pid = nixio.fork()
112 if pid == 0 then
113 rfd:close()
114 nixio.dup(wfd, nixio.stdout)
115 -- candidates for wget, try first ones with SSL support
116 local candidates = {{"/usr/bin/wget-ssl",1},{"/usr/bin/wget",0},{"/bin/wget",0}}
117 local _, bin
118 for _, bin in ipairs(candidates) do
119 if nixiofs.access(bin[1], "x") then
120 if bin[2] == 0 then
121 nixio.exec(bin[1], "-q", "-O", "-", url)
122 else
123 nixio.exec(bin[1], "--no-check-certificate", "-q", "-O", "-", url)
124 end
125 end
126 end
127 return
128 else
129 wfd:close()
130 rfd:setblocking(false)
131
132 local buffer = { }
133 local err1, err2
134
135 while true do
136 local ready = nixio.poll({{ fd = rfd, events = nixio.poll_flags("in") }}, timeout)
137 if not ready then
138 nixio.kill(pid, nixio.const.SIGKILL)
139 err1 = "timeout"
140 break
141 end
142
143 local rv = rfd:read(4096)
144 if rv then
145 -- eof
146 if #rv == 0 then
147 break
148 end
149
150 buffer[#buffer+1] = rv
151 else
152 -- error
153 if nixio.errno() ~= nixio.const.EAGAIN and
154 nixio.errno() ~= nixio.const.EWOULDBLOCK then
155 err1 = "error"
156 err2 = nixio.errno()
157 end
158 end
159 end
160
161 nixio.waitpid(pid, "nohang")
162 if not err1 then
163 return table.concat(buffer)
164 else
165 return nil, err1, err2
166 end
167 end
168 end
169
170 function getOptions(name)
171 -- Getting json and Checking if bmx6-json is avaiable
172 local options = get("options")
173 if options == nil or options.OPTIONS == nil then
174 m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
175 return nil
176 else
177 options = options.OPTIONS
178 end
179
180 -- Filtering by the option name
181 local i,_
182 local namedopt = nil
183 if name ~= nil then
184 for _,i in ipairs(options) do
185 if i.name == name and i.CHILD_OPTIONS ~= nil then
186 namedopt = i.CHILD_OPTIONS
187 break
188 end
189 end
190 end
191
192 return namedopt
193 end
194
195 -- Rturns a help string formated to be used in HTML scope
196 function getHtmlHelp(opt)
197 if opt == nil then return nil end
198
199 local help = ""
200 if opt.help ~= nil then
201 help = text2html(opt.help)
202 end
203 if opt.syntax ~= nil then
204 help = help .. "<br/><b>Syntax: </b>" .. text2html(opt.syntax)
205 end
206
207 return help
208 end
209
210 function testandreload()
211 local test = sys.call('bmx6 -c --test > /tmp/bmx6-luci.err.tmp')
212 if test ~= 0 then
213 return sys.exec("cat /tmp/bmx6-luci.err.tmp")
214 end
215
216 local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
217 if err ~= 0 then
218 return sys.exec("cat /tmp/bmx6-luci.err.tmp")
219 end
220
221 return nil
222 end
223