luci-app-pppoe-server: Add PPPoE server configuration
[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"}, post("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 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 = ">"
84         else return nil end
85
86         local av1 = UTIL.split(ver1, "[%.%-]", nil, true)
87         local av2 = UTIL.split(ver2, "[%.%-]", nil, true)
88
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 ""
92
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
101         end
102
103         -- all equal and not compare greater or lower then true
104         return not (comp == "<" or comp == ">")
105 end
106
107 -- read version information for given package if installed
108 function ipkg_ver_installed(pkg)
109         local version = nil
110         local control = io.open("/usr/lib/opkg/info/%s.control" % pkg, "r")
111         if control then
112                 local ln
113                 repeat
114                         ln = control:read("*l")
115                         if ln and ln:match("^Version: ") then
116                                 version = ln:gsub("^Version: ", "")
117                                 break
118                         end
119                 until not ln
120                 control:close()
121         end
122         return version
123 end
124
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)
131
132         if fexists then
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)
137                 else
138                         self:remove(section)
139                 end
140                 if (fvalue ~= cvalue) then self.section.changed = true end
141         else
142                 self:remove(section)
143                 self.section.changed = true
144         end
145 end