luci-app-adblock: sync with adblock 3.5.4
[project/luci.git] / applications / luci-app-adblock / luasrc / controller / adblock.lua
1 -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 module("luci.controller.adblock", package.seeall)
5
6 local sys = require("luci.sys")
7 local util = require("luci.util")
8 local http = require("luci.http")
9 local templ = require("luci.template")
10 local i18n = require("luci.i18n")
11 local json = require("luci.jsonc")
12 local uci = require("luci.model.uci").cursor()
13 local fs = require("nixio.fs")
14
15 function index()
16 if not nixio.fs.access("/etc/config/adblock") then
17 return
18 end
19 entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
20 entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
21 entry({"admin", "services", "adblock", "log"}, template("adblock/logread"), _("View Logfile"), 20).leaf = true
22 entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
23 entry({"admin", "services", "adblock", "advanced", "blacklist"}, form("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
24 entry({"admin", "services", "adblock", "advanced", "whitelist"}, form("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
25 entry({"admin", "services", "adblock", "advanced", "configuration"}, form("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
26 entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
27 entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
28 entry({"admin", "services", "adblock", "logread"}, call("logread"), nil).leaf = true
29 entry({"admin", "services", "adblock", "status"}, call("status_update"), nil).leaf = true
30 entry({"admin", "services", "adblock", "action"}, call("adb_action"), nil).leaf = true
31 end
32
33 function adb_action(value)
34 if value == "Suspend" then
35 luci.sys.call("/etc/init.d/adblock suspend >/dev/null 2>&1")
36 elseif value == "Resume" then
37 luci.sys.call("/etc/init.d/adblock resume >/dev/null 2>&1")
38 elseif value == "Refresh" then
39 luci.sys.call("/etc/init.d/adblock reload >/dev/null 2>&1")
40 end
41 luci.http.prepare_content("text/plain")
42 luci.http.write("0")
43 end
44
45 function status_update()
46 local rt_file
47 local content
48
49 rt_file = uci:get("adblock", "global", "adb_rtfile") or "/tmp/adb_runtime.json"
50
51 if fs.access(rt_file) then
52 content = json.parse(fs.readfile(rt_file))
53 if content then
54 http.prepare_content("application/json")
55 http.write_json(content)
56 end
57 end
58 end
59
60 function logread()
61 local content
62
63 if nixio.fs.access("/var/log/messages") then
64 content = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
65 else
66 content = util.trim(util.exec("logread -e 'adblock-'"))
67 end
68
69 if content == "" then
70 content = "No adblock related logs yet!"
71 end
72 http.write(content)
73 end
74
75 function queryData(domain)
76 if domain then
77 luci.http.prepare_content("text/plain")
78 local cmd = "/etc/init.d/adblock query %s 2>&1"
79 local util = io.popen(cmd % util.shellquote(domain))
80 if util then
81 while true do
82 local line = util:read("*l")
83 if not line then
84 break
85 end
86 luci.http.write(line)
87 luci.http.write("\n")
88 end
89 util:close()
90 end
91 end
92 end