Remove package bmx6-luci in favor of packages/luci-app-bmx6
[feed/routing.git] / packages / luci-app-bmx6 / files / 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
116 local candidates = { "/usr/bin/wget", "/bin/wget" }
117 local _, bin
118 for _, bin in ipairs(candidates) do
119 if nixiofs.access(bin, "x") then
120 nixio.exec(bin, "-q", "-O", "-", url)
121 end
122 end
123 return
124 else
125 wfd:close()
126 rfd:setblocking(false)
127
128 local buffer = { }
129 local err1, err2
130
131 while true do
132 local ready = nixio.poll({{ fd = rfd, events = nixio.poll_flags("in") }}, timeout)
133 if not ready then
134 nixio.kill(pid, nixio.const.SIGKILL)
135 err1 = "timeout"
136 break
137 end
138
139 local rv = rfd:read(4096)
140 if rv then
141 -- eof
142 if #rv == 0 then
143 break
144 end
145
146 buffer[#buffer+1] = rv
147 else
148 -- error
149 if nixio.errno() ~= nixio.const.EAGAIN and
150 nixio.errno() ~= nixio.const.EWOULDBLOCK then
151 err1 = "error"
152 err2 = nixio.errno()
153 end
154 end
155 end
156
157 nixio.waitpid(pid, "nohang")
158 if not err1 then
159 return table.concat(buffer)
160 else
161 return nil, err1, err2
162 end
163 end
164 end
165
166 function getOptions(name)
167 -- Getting json and Checking if bmx6-json is avaiable
168 local options = get("options")
169 if options == nil or options.OPTIONS == nil then
170 m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
171 return nil
172 else
173 options = options.OPTIONS
174 end
175
176 -- Filtering by the option name
177 local i,_
178 local namedopt = nil
179 if name ~= nil then
180 for _,i in ipairs(options) do
181 if i.name == name and i.CHILD_OPTIONS ~= nil then
182 namedopt = i.CHILD_OPTIONS
183 break
184 end
185 end
186 end
187
188 return namedopt
189 end
190
191 -- Rturns a help string formated to be used in HTML scope
192 function getHtmlHelp(opt)
193 if opt == nil then return nil end
194
195 local help = ""
196 if opt.help ~= nil then
197 help = text2html(opt.help)
198 end
199 if opt.syntax ~= nil then
200 help = help .. "<br/><b>Syntax: </b>" .. text2html(opt.syntax)
201 end
202
203 return help
204 end
205
206 function testandreload()
207 local test = sys.call('bmx6 -c --test > /tmp/bmx6-luci.err.tmp')
208 if test ~= 0 then
209 return sys.exec("cat /tmp/bmx6-luci.err.tmp")
210 end
211
212 local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
213 if err ~= 0 then
214 return sys.exec("cat /tmp/bmx6-luci.err.tmp")
215 end
216
217 return nil
218 end
219