bmx6json.lua 5.2 KB
Newer Older
P
p4u 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
--[[
    Copyright (C) 2011 Pau Escrich <pau@dabax.net>
    Contributors Jo-Philipp Wich <xm@subsignal.org>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
--]]

local ltn12 = require("luci.ltn12")
local json = require("luci.json")
local util = require("luci.util")
local uci = require("luci.model.uci")
local sys = require("luci.sys")
local template = require("luci.template")
local http = require("luci.http")
local string = require("string")
local table = require("table")
local nixio = require("nixio")
local nixiofs = require("nixio.fs")
local ipairs = ipairs

module "luci.model.bmx6json"

-- Returns a LUA object from bmx6 JSON daemon

function get(field, host)
	local url
	if host ~= nil then
		if host:match(":") then
			url = 'http://[%s]/cgi-bin/bmx6-info?' % host
		else
			url = 'http://%s/cgi-bin/bmx6-info?' % host
		end
	else
		url = uci.cursor():get("luci-bmx6","luci","json")
	end

	if url == nil then
		 print_error("bmx6 json url not configured, cannot fetch bmx6 daemon data",true)
		 return nil
	 end

	 local json_url = util.split(url,":")
	 local raw = ""

	if json_url[1] == "http"  then
		raw,err = wget(url..field,1000)
	else

		if json_url[1] == "exec" then
			raw = sys.exec(json_url[2]..' '..field)
		else
			print_error("bmx6 json url not recognized, cannot fetch bmx6 daemon data. Use http: or exec:",true)
			return nil
		end

	end

	local data = nil

	if raw and raw:len() > 10 then
		local decoder = json.Decoder()
		ltn12.pump.all(ltn12.source.string(raw), decoder:sink())
		data = decoder:get()
--	else
--		print_error("Cannot get data from bmx6 daemon",true)
--		return nil
	end

	return data
end

function print_error(txt,popup)
	util.perror(txt)
	sys.call("logger -t bmx6json " .. txt)

	if popup then
		http.write('<script type="text/javascript">alert("Some error detected, please check it: '..txt..'");</script>')
	else
		http.write("<h1>Dammit! some error detected</h1>")
		http.write("bmx6-luci: " .. txt)
		http.write('<p><FORM><INPUT TYPE="BUTTON" VALUE="Go Back" ONCLICK="history.go(-1)"></FORM></p>')
	end

end

function text2html(txt)
	txt = string.gsub(txt,"<","{")
	txt = string.gsub(txt,">","}")
	txt = util.striptags(txt)
	return txt
end


function wget(url, timeout)
	local rfd, wfd = nixio.pipe()
	local pid = nixio.fork()
	if pid == 0 then
		rfd:close()
		nixio.dup(wfd, nixio.stdout)

		local candidates = { "/usr/bin/wget", "/bin/wget" }
		local _, bin
		for _, bin in ipairs(candidates) do
			if nixiofs.access(bin, "x") then
				nixio.exec(bin, "-q", "-O", "-", url)
			end
		end
		return
	else
		wfd:close()
		rfd:setblocking(false)

		local buffer = { }
		local err1, err2

		while true do
			local ready = nixio.poll({{ fd = rfd, events = nixio.poll_flags("in") }}, timeout)
			if not ready then
				nixio.kill(pid, nixio.const.SIGKILL)
				err1 = "timeout"
				break
			end

			local rv = rfd:read(4096)
			if rv then
				-- eof
				if #rv == 0 then
					break
				end

				buffer[#buffer+1] = rv
			else
				-- error
				if nixio.errno() ~= nixio.const.EAGAIN and
				   nixio.errno() ~= nixio.const.EWOULDBLOCK then
				   	err1 = "error"
				   	err2 = nixio.errno()
				end
			end
		end

		nixio.waitpid(pid, "nohang")
		if not err1 then
			return table.concat(buffer)
		else
			return nil, err1, err2
		end
	end
end

function getOptions(name)
	-- Getting json and Checking if bmx6-json is avaiable
	local options = get("options")
	if options == nil or options.OPTIONS == nil then
		m.message = "bmx6-json plugin is not running or some mistake in luci-bmx6 configuration, check /etc/config/luci-bmx6"
		return nil
	else
		options = options.OPTIONS
	end

	-- Filtering by the option name
	local i,_
	local namedopt = nil
	if name ~= nil then
		for _,i in ipairs(options) do
			if i.name == name and i.CHILD_OPTIONS ~= nil then
				namedopt = i.CHILD_OPTIONS
				break
			end
		end
	end

	return namedopt
end

-- Rturns a help string formated to be used in HTML scope
function getHtmlHelp(opt)
	if opt == nil then return nil end
	
	local help = ""
	if opt.help ~= nil then
		help = text2html(opt.help)
	end
	if opt.syntax ~= nil then
		help = help .. "<br/><b>Syntax: </b>" .. text2html(opt.syntax)
	end		

	return help
end

function testandreload()
	local test = sys.call('bmx6 -c --test > /tmp/bmx6-luci.err.tmp')
	if test ~= 0 then
		return sys.exec("cat /tmp/bmx6-luci.err.tmp")
	end

	local err = sys.call('bmx6 -c --configReload > /tmp/bmx6-luci.err.tmp')
		if err ~= 0 then
		return sys.exec("cat /tmp/bmx6-luci.err.tmp")
	end

	return nil
end