Merge pull request #1983 from dibdot/adblock
[oweals/luci.git] / applications / luci-app-simple-adblock / luasrc / model / cbi / simple-adblock.lua
1 -- Copyright 2016-2018 Stan Grishin <stangri@melmac.net>
2 -- Licensed to the public under the Apache License 2.0.
3
4 m = Map("simple-adblock", translate("Simple AdBlock Settings"))
5
6 h = m:section(NamedSection, "config", "simple-adblock", translate("Service Status"))
7
8 local packageName = "simple-adblock"
9 local uci = require "luci.model.uci".cursor()
10 local util = require "luci.util"
11 local enabledFlag = uci:get(packageName, "config", "enabled")
12 local status = util.ubus('service', 'list', { name = packageName })
13 if status and status[packageName] and status[packageName]['instances'] and status[packageName]['instances']['status'] and status[packageName]['instances']['status']['data'] and status[packageName]['instances']['status']['data']['status'] then
14         status = status[packageName]['instances']['status']['data']['status']
15 else
16         status =  "Stopped"
17 end
18 if status:match("Reloading") then
19         ds = h:option(DummyValue, "_dummy", translate("Service Status"))
20         ds.template = "simple-adblock/status"
21         ds.value = status
22 else
23         en = h:option(Button, "__toggle")
24         if enabledFlag ~= "1" or status:match("Stopped") then
25                 en.title      = translate("Service is disabled/stopped")
26                 en.inputtitle = translate("Enable/Start")
27                 en.inputstyle = "apply important"
28                 if nixio.fs.access("/var/simple-adblock.cache") then
29                         ds = h:option(DummyValue, "_dummy", translate("Service Status"))
30                         ds.template = "simple-adblock/status"
31                         ds.value = "Cache file containing " .. luci.util.trim(luci.sys.exec("wc -l < /var/simple-adblock.cache")) .. " domains found"
32                 end
33         else
34                 en.title      = translate("Service is enabled/started")
35                 en.inputtitle = translate("Stop/Disable")
36                 en.inputstyle = "reset important"
37                 ds = h:option(DummyValue, "_dummy", translate("Service Status"))
38                 ds.template = "simple-adblock/status"
39                 ds.value = status
40                 if not status:match("Success") then
41                         reload = h:option(Button, "__reload")
42                         reload.title      = translate("Service started with error")
43                         reload.inputtitle = translate("Reload")
44                         reload.inputstyle = "apply important"
45                         function reload.write()
46                                 luci.sys.exec("/etc/init.d/simple-adblock reload")
47                                 luci.http.redirect(luci.dispatcher.build_url("admin/services/" .. packageName))
48                         end
49                 end
50         end
51         function en.write()
52                 enabledFlag = enabledFlag == "1" and "0" or "1"
53                 uci:set(packageName, "config", "enabled", enabledFlag)
54                 uci:save(packageName)
55                 uci:commit(packageName)
56                 if enabledFlag == "0" then
57                         luci.sys.init.stop(packageName)
58 --                      luci.sys.exec("/etc/init.d/simple-adblock killcache")
59                 else
60                         luci.sys.init.enable(packageName)
61                         luci.sys.init.start(packageName)
62                 end
63                 luci.http.redirect(luci.dispatcher.build_url("admin/services/" .. packageName))
64         end
65 end
66
67 s = m:section(NamedSection, "config", "simple-adblock", translate("Configuration"))
68 -- General options
69 s:tab("basic", translate("Basic Configuration"))
70
71 o2 = s:taboption("basic", ListValue, "verbosity", translate("Output Verbosity Setting"),translate("Controls system log and console output verbosity"))
72 o2:value("0", translate("Suppress output"))
73 o2:value("1", translate("Some output"))
74 o2:value("2", translate("Verbose output"))
75 o2.rmempty = false
76 o2.default = 2
77
78 o3 = s:taboption("basic", ListValue, "force_dns", translate("Force Router DNS"), translate("Forces Router DNS use on local devices, also known as DNS Hijacking"))
79 o3:value("0", translate("Let local devices use their own DNS servers if set"))
80 o3:value("1", translate("Force Router DNS server to all local devices"))
81 o3.rmempty = false
82 o3.default = 1
83
84 local sysfs_path = "/sys/class/leds/"
85 local leds = {}
86 if nixio.fs.access(sysfs_path) then
87         leds = nixio.util.consume((nixio.fs.dir(sysfs_path)))
88 end
89 if #leds ~= 0 then
90         o3 = s:taboption("basic", Value, "led", translate("LED to indicate status"), translate("Pick the LED not already used in")
91                 .. [[ <a href="]] .. luci.dispatcher.build_url("admin/system/leds") .. [[">]]
92                 .. translate("System LED Configuration") .. [[</a>]])
93         o3.rmempty = true
94         o3:value("", translate("none"))
95         for k, v in ipairs(leds) do
96                 o3:value(v)
97         end
98 end
99
100 s:tab("advanced", translate("Advanced Configuration"))
101
102 o6 = s:taboption("advanced", Value, "boot_delay", translate("Delay (in seconds) for on-boot start"), translate("Run service after set delay on boot"))
103 o6.default = 120
104 o6.datatype = "range(1,600)"
105
106 o7 = s:taboption("advanced", Value, "download_timeout", translate("Download time-out (in seconds)"), translate("Stop the download if it is stalled for set number of seconds"))
107 o7.default = 10
108 o7.datatype = "range(1,60)"
109
110 o8 = s:taboption("advanced", ListValue, "debug", translate("Enable Debugging"), translate("Enables debug output to /tmp/simple-adblock.log"))
111 o8:value("", translate("Disable Debugging"))
112 o8:value("1", translate("Enable Debugging"))
113 o8.rmempty = true
114 o8.default = 0
115
116
117 s2 = m:section(NamedSection, "config", "simple-adblock", translate("Whitelist and Blocklist Management"))
118 -- Whitelisted Domains
119 d1 = s2:option(DynamicList, "whitelist_domain", translate("Whitelisted Domains"), translate("Individual domains to be whitelisted"))
120 d1.addremove = false
121 d1.optional = false
122
123 -- Blacklisted Domains
124 d3 = s2:option(DynamicList, "blacklist_domain", translate("Blacklisted Domains"), translate("Individual domains to be blacklisted"))
125 d3.addremove = false
126 d3.optional = false
127
128 -- Whitelisted Domains URLs
129 d2 = s2:option(DynamicList, "whitelist_domains_url", translate("Whitelisted Domain URLs"), translate("URLs to lists of domains to be whitelisted"))
130 d2.addremove = false
131 d2.optional = false
132
133 -- Blacklisted Domains URLs
134 d4 = s2:option(DynamicList, "blacklist_domains_url", translate("Blacklisted Domain URLs"), translate("URLs to lists of domains to be blacklisted"))
135 d4.addremove = false
136 d4.optional = false
137
138 -- Blacklisted Hosts URLs
139 d5 = s2:option(DynamicList, "blacklist_hosts_url", translate("Blacklisted Hosts URLs"), translate("URLs to lists of hosts to be blacklisted"))
140 d5.addremove = false
141 d5.optional = false
142
143 return m