Merge pull request #3789 from weblate/weblate-openwrt-luci
[project/luci.git] / applications / luci-app-adblock / luasrc / model / cbi / adblock / whitelist_tab.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 local fs = require("nixio.fs")
5 local util = require("luci.util")
6 local uci = require("luci.model.uci").cursor()
7 local input = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
8
9 if not fs.access(input) then
10 m = SimpleForm("error", nil, translate("Input file not found, please check your configuration."))
11 m.reset = false
12 m.submit = false
13 return m
14 end
15
16 if fs.stat(input).size >= 102400 then
17 m = SimpleForm("error", nil,
18 translate("The file size is too large for online editing in LuCI (≥ 100 KB). ")
19 .. translate("Please edit this file directly in a terminal session."))
20 m.reset = false
21 m.submit = false
22 return m
23 end
24
25 m = SimpleForm("input", nil)
26 m:append(Template("adblock/adblock_css"))
27 m.submit = translate("Save")
28 m.reset = false
29
30 s = m:section(SimpleSection, nil,
31 translatef("This form allows you to modify the content of the adblock whitelist (%s). ", input)
32 .. translate("Please add only one domain per line. Comments introduced with '#' are allowed - ip addresses, wildcards and regex are not."))
33
34 f = s:option(TextValue, "data")
35 f.datatype = "string"
36 f.rows = 20
37 f.rmempty = true
38
39 function f.cfgvalue()
40 return fs.readfile(input) or ""
41 end
42
43 function f.write(self, section, data)
44 return fs.writefile(input, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
45 end
46
47 function f.remove(self, section, value)
48 return fs.writefile(input, "")
49 end
50
51 function s.handle(self, state, data)
52 return true
53 end
54
55 return m