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"}, call("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 (#ver1 > 0)
76 or not ver2 or not (#ver2 > 0)
77 or not comp or not (#comp > 0) then return nil end
78 -- correct compare string
79 if comp == "<>" or comp == "><" or comp == "!=" or comp == "~=" then comp = "~="
80 elseif comp == "<=" or comp == "<" or comp == "=<" then comp = "<="
81 elseif comp == ">=" or comp == ">" or comp == "=>" then comp = ">="
82 elseif comp == "=" or comp == "==" then comp = "=="
83 elseif comp == "<<" then comp = "<"
84 elseif comp == ">>" then comp = ">"
87 local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
88 local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
90 for i = 1, math.max(table.getn(av1),table.getn(av2)), 1 do
91 local s1 = av1[i] or ""
92 local s2 = av2[i] or ""
93 local n1 = tonumber(s1)
94 local n2 = tonumber(s2)
96 -- one numeric and other empty string then set other to 0
97 if n1 and not n2 and (not s2 or #s2 == 0) then n2 = 0 end
98 if n2 and not n1 and (not s1 or #s1 == 0) then n1 = 0 end
100 local nc = (n1 and n2) -- numeric compare
103 -- first "not equal" found return true
104 if comp == "~=" and (n1 ~= n2) then return true end
105 -- first "lower" found return true
106 if (comp == "<" or comp == "<=") and (n1 < n2) then return true end
107 -- first "greater" found return true
108 if (comp == ">" or comp == ">=") and (n1 > n2) then return true end
109 -- not equal then return false
110 if (n1 ~= n2) then return false end
112 if comp == "~=" and (s1 ~= s2) then return true end
113 if (comp == "<" or comp == "<=") and (s1 < s2) then return true end
114 if (comp == ">" or comp == ">=") and (s1 > s2) then return true end
115 if (s1 ~= s2) then return false end
118 -- all equal then true
122 -- read version information for given package if installed
123 function ipkg_ver_installed(pkg)
125 local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
129 ln = control:read("*l")
130 if ln and ln:match("^Version: ") then
131 version = ln:gsub("^Version: ", "")
140 -- replacement of build-in Flag.parse of cbi.lua
141 -- modified to mark section as changed if value changes
142 -- current parse did not do this, but it is done AbstaractValue.parse()
143 function flag_parse(self, section)
144 local fexists = self.map:formvalue(
145 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
148 local fvalue = self:formvalue(section) and self.enabled or self.disabled
149 local cvalue = self:cfgvalue(section)
150 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
151 self:write(section, fvalue)
155 if (fvalue ~= cvalue) then self.section.changed = true end
158 self.section.changed = true