1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
4 module("luci.controller.privoxy", package.seeall)
6 local NX = require "nixio"
7 local NXFS = require "nixio.fs"
8 local HTTP = require "luci.http"
9 local UCI = require "luci.model.uci"
10 local UTIL = require "luci.util"
11 local SYS = require "luci.sys"
13 PRIVOXY_MIN = "3.0.22-0" -- minimum version of service required
16 entry( {"admin", "services", "privoxy"}, cbi("privoxy"), _("Privoxy WEB proxy"), 59)
17 entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
18 entry( {"admin", "services", "privoxy", "startstop"}, post("startstop") ).leaf = true
19 entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
22 -- called by XHR.get from detail_logview.htm
24 -- read application settings
25 local uci = UCI.cursor()
26 local logdir = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
27 local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
30 local lfile=logdir .. "/" .. logfile
31 local ldata=NXFS.readfile(lfile)
32 if not ldata or #ldata == 0 then
38 -- called by XHR.get from detail_startstop.htm
40 local pid = get_pid(true)
42 SYS.call("/etc/init.d/privoxy stop")
43 NX.nanosleep(1) -- sleep a second
44 if NX.kill(pid, 0) then -- still running
45 NX.kill(pid, 9) -- send SIGKILL
49 SYS.call("/etc/init.d/privoxy start")
50 NX.nanosleep(1) -- sleep a second
51 pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
52 if pid > 0 and not NX.kill(pid, 0) then
53 pid = 0 -- process did not start
56 HTTP.write(tostring(pid)) -- HTTP needs string not number
59 -- called by XHR.poll from detail_startstop.htm
60 -- and from lua (with parameter "true")
61 function get_pid(from_lua)
62 local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
63 if pid > 0 and not NX.kill(pid, 0) then
69 HTTP.write(tostring(pid)) -- HTTP needs string not number
73 -- compare versions using "<=" "<" ">" ">=" "=" "<<" ">>"
74 function ipkg_ver_compare(ver1, comp, ver2)
75 if not ver1 or not ver2
76 or not comp or not (#comp > 0) then return nil end
77 -- correct compare string
78 if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
79 elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
80 elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
81 elseif comp == "=" or comp == "==" then comp = "=="
82 elseif comp == "<<" then comp = "<"
83 elseif comp == ">>" then comp = ">"
86 local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
87 local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
89 for i = 1, math.max(table.getn(av1),table.getn(av2)), 1 do
90 local s1 = av1[i] or ""
91 local s2 = av2[i] or ""
93 -- first "not equal" found return true
94 if comp == "~=" and (s1 ~= s2) then return true end
95 -- first "lower" found return true
96 if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
97 -- first "greater" found return true
98 if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
99 -- not equal then return false
100 if (s1 ~= s2) then return false end
103 -- all equal and not compare greater or lower then true
104 return not (comp == "<" or comp == ">")
107 -- read version information for given package if installed
108 function ipkg_ver_installed(pkg)
110 local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
114 ln = control:read("*l")
115 if ln and ln:match("^Version: ") then
116 version = ln:gsub("^Version: ", "")
125 -- replacement of build-in Flag.parse of cbi.lua
126 -- modified to mark section as changed if value changes
127 -- current parse did not do this, but it is done AbstaractValue.parse()
128 function flag_parse(self, section)
129 local fexists = self.map:formvalue(
130 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
133 local fvalue = self:formvalue(section) and self.enabled or self.disabled
134 local cvalue = self:cfgvalue(section)
135 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
136 self:write(section, fvalue)
140 if (fvalue ~= cvalue) then self.section.changed = true end
143 self.section.changed = true