luci-app-privoxy: move from openwrt/packages to openwrt/luci
[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 SYS  = require "luci.sys"
11
12 PRIVOXY_MIN = "3.0.22-0"        -- minimum version of service required
13
14 function index()
15         local _sys      = require "luci.sys"
16         local _verinst  = _sys.exec([[opkg list-installed ]] .. "privoxy" .. [[ | cut -d " " -f 3 ]])
17         local _cmd      = [[opkg compare-versions "]] .. _verinst .. [[" ">=" "]] .. "3.0.22-0" .. [["]]
18         local _verok    = tonumber(_sys.call(_cmd))
19
20         -- check config file and version
21         if not nixio.fs.access("/etc/config/privoxy") or (_verok == 0) then
22                 entry( {"admin", "services", "privoxy"}, cbi("privoxy/apperror",
23                         {hideapplybtn=true, hidesavebtn=true, hideresetbtn=true }), _("Privoxy WEB proxy"), 59)
24         else
25                 entry( {"admin", "services", "privoxy"}, cbi("privoxy/detail"), _("Privoxy WEB proxy"), 59)
26                 entry( {"admin", "services", "privoxy", "logview"}, call("logread") ).leaf = true
27                 entry( {"admin", "services", "privoxy", "startstop"}, call("startstop") ).leaf = true
28                 entry( {"admin", "services", "privoxy", "status"}, call("get_pid") ).leaf = true
29         end
30 end
31
32 -- called by XHR.get from detail_logview.htm
33 function logread()
34         -- read application settings
35         local uci     = UCI.cursor()
36         local logdir  = uci:get("privoxy", "privoxy", "logdir") or "/var/log"
37         local logfile = uci:get("privoxy", "privoxy", "logfile") or "privoxy.log"
38         uci:unload("privoxy")
39
40         local lfile=logdir .. "/" .. logfile
41         local ldata=NXFS.readfile(lfile)
42         if not ldata or #ldata == 0 then
43                 ldata="_nodata_"
44         end
45         HTTP.write(ldata)
46 end
47
48 -- called by XHR.get from detail_startstop.htm
49 function startstop()
50         local pid = get_pid(true)
51         if pid > 0 then
52                 SYS.call("/etc/init.d/privoxy stop")
53                 NX.nanosleep(1)         -- sleep a second
54                 if NX.kill(pid, 0) then -- still running
55                         NX.kill(pid, 9) -- send SIGKILL
56                 end
57                 pid = 0
58         else
59                 SYS.call("/etc/init.d/privoxy start")
60                 NX.nanosleep(1)         -- sleep a second
61                 pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
62                 if pid > 0 and not NX.kill(pid, 0) then
63                         pid = 0         -- process did not start
64                 end
65         end
66         HTTP.write(tostring(pid))       -- HTTP needs string not number
67 end
68
69 -- called by XHR.poll from detail_startstop.htm
70 -- and from lua (with parameter "true")
71 function get_pid(from_lua)
72         local pid = tonumber(NXFS.readfile("/var/run/privoxy.pid") or 0 )
73         if pid > 0 and not NX.kill(pid, 0) then
74                 pid = 0
75         end
76         if from_lua then
77                 return pid
78         else
79                 HTTP.write(tostring(pid))       -- HTTP needs string not number
80         end
81 end
82
83 -- get the "name" of the current active theme
84 function get_theme()
85         local _uci  = UCI.cursor()
86         local _base = _uci:get("luci", "main", "mediaurlbase")  -- only pathname
87         _uci:unload("luci")
88
89         for k, v in pairs(luci.config.themes) do
90                 if k:sub(1, 1) ~= "." and v == _base then
91                         return k
92                 end
93         end
94         return nil
95 end
96
97 -- replacement of build-in Flag.parse of cbi.lua
98 -- modified to mark section as changed if value changes
99 -- current parse did not do this, but it is done AbstaractValue.parse()
100 function flag_parse(self, section)
101         local fexists = self.map:formvalue(
102                 luci.cbi.FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
103
104         if fexists then
105                 local fvalue = self:formvalue(section) and self.enabled or self.disabled
106                 local cvalue = self:cfgvalue(section)
107                 if fvalue ~= self.default or (not self.optional and not self.rmempty) then
108                         self:write(section, fvalue)
109                 else
110                         self:remove(section)
111                 end
112                 if (fvalue ~= cvalue) then self.section.changed = true end
113         else
114                 self:remove(section)
115                 self.section.changed = true
116         end
117 end