Merge pull request #1709 from dibdot/get_interface-fix
[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 util  = require("luci.util")
7 local templ = require("luci.template")
8 local i18n  = require("luci.i18n")
9
10 function index()
11         if not nixio.fs.access("/etc/config/adblock") then
12                 return
13         end
14         entry({"admin", "services", "adblock"}, firstchild(), _("Adblock"), 30).dependent = false
15         entry({"admin", "services", "adblock", "tab_from_cbi"}, cbi("adblock/overview_tab", {hideresetbtn=true, hidesavebtn=true}), _("Overview"), 10).leaf = true
16         entry({"admin", "services", "adblock", "logfile"}, call("logread"), _("View Logfile"), 20).leaf = true
17         entry({"admin", "services", "adblock", "advanced"}, firstchild(), _("Advanced"), 100)
18         entry({"admin", "services", "adblock", "advanced", "blacklist"}, cbi("adblock/blacklist_tab"), _("Edit Blacklist"), 110).leaf = true
19         entry({"admin", "services", "adblock", "advanced", "whitelist"}, cbi("adblock/whitelist_tab"), _("Edit Whitelist"), 120).leaf = true
20         entry({"admin", "services", "adblock", "advanced", "configuration"}, cbi("adblock/configuration_tab"), _("Edit Configuration"), 130).leaf = true
21         entry({"admin", "services", "adblock", "advanced", "query"}, template("adblock/query"), _("Query domains"), 140).leaf = true
22         entry({"admin", "services", "adblock", "advanced", "result"}, call("queryData"), nil, 150).leaf = true
23 end
24
25 function logread()
26         local logfile
27
28         if nixio.fs.access("/var/log/messages") then
29                 logfile = util.trim(util.exec("grep -F 'adblock-' /var/log/messages"))
30         else
31                 logfile = util.trim(util.exec("logread -e 'adblock-'"))
32         end
33         templ.render("adblock/logread", {title = i18n.translate("Adblock Logfile"), content = logfile})
34 end
35
36 function queryData(domain)
37         if domain then
38                 luci.http.prepare_content("text/plain")
39                 local cmd = "/etc/init.d/adblock query %s 2>&1"
40                 local util = io.popen(cmd % util.shellquote(domain))
41                 if util then
42                         while true do
43                                 local line = util:read("*l")
44                                 if not line then
45                                         break
46                                 end
47                                 luci.http.write(line)
48                                 luci.http.write("\n")
49                         end
50                         util:close()
51                 end
52         end
53 end