9ffc404ce9da74eb237ee173e39a7c6c50f4c8ab
[project/luci.git] / applications / luci-app-privoxy / luasrc / controller / privoxy.lua
1 -- Copyright 2014 Christian Schoenebeck <christian dot schoenebeck at gmail dot com>
2 -- Licensed under the Apache License, Version 2.0
3
4 module("luci.controller.privoxy", package.seeall)
5
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"
12
13 PRIVOXY_MIN = "3.0.22-0"        -- minimum version of service required
14
15 function index()
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
20 end
21
22 -- called by XHR.get from detail_logview.htm
23 function logread()
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"
28         uci:unload("privoxy")
29
30         local lfile=logdir .. "/" .. logfile
31         local ldata=NXFS.readfile(lfile)
32         if not ldata or #ldata == 0 then
33                 ldata="_nodata_"
34         end
35         HTTP.write(ldata)
36 end
37
38 -- called by XHR.get from detail_startstop.htm
39 function startstop()
40         local pid = get_pid(true)
41         if pid > 0 then
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
46                 end
47                 pid = 0
48         else
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
54                 end
55         end
56         HTTP.write(tostring(pid))       -- HTTP needs string not number
57 end
58
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
64                 pid = 0
65         end
66         if from_lua then
67                 return pid
68         else
69                 HTTP.write(tostring(pid))       -- HTTP needs string not number
70         end
71 end
72
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 = ">"
85         else return nil end
86
87         local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
88         local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
89
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)
95
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
99
100                 local nc = (n1 and n2)  -- numeric compare
101
102                 if nc then
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
111                 else
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
116                 end
117         end
118         -- all equal then true
119         return true
120 end
121
122 -- read version information for given package if installed
123 function ipkg_ver_installed(pkg)
124         local version = nil
125         local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
126         if control then
127                 local ln
128                 repeat
129                         ln = control:read("*l")
130                         if ln and ln:match("^Version: ") then
131                                 version = ln:gsub("^Version: ", "")
132                                 break
133                         end
134                 until not ln
135                 control:close()
136         end
137         return version
138 end
139
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)
146
147         if fexists then
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)
152                 else
153                         self:remove(section)
154                 end
155                 if (fvalue ~= cvalue) then self.section.changed = true end
156         else
157                 self:remove(section)
158                 self.section.changed = true
159         end
160 end