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