10a593859f5599e516560afc3144ca8e19f7b873
[project/luci.git] / applications / luci-app-adblock / luasrc / model / cbi / adblock / whitelist_tab.lua
1 -- Copyright 2017 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("uci")
7 local adbinput = uci.get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
8
9 if not nixio.fs.access(adbinput) 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 nixio.fs.stat(adbinput).size > 524288 then
17         m = SimpleForm("error", nil,
18         translate("The file size is too large for online editing in LuCI (> 512 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/config_css"))
27 m.reset = false
28
29 s = m:section(SimpleSection, nil,
30         translatef("This form allows you to modify the content of the adblock whitelist (%s).<br />", adbinput)
31         .. translate("Please add only one domain per line. Comments introduced with '#' are allowed - ip addresses, wildcards and regex are not."))
32
33 f = s:option(TextValue, "data")
34 f.datatype = "string"
35 f.rows = 20
36 f.rmempty = true
37
38 function f.cfgvalue()
39         return nixio.fs.readfile(adbinput) or ""
40 end
41
42 function f.write(self, section, data)
43         return nixio.fs.writefile(adbinput, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
44 end
45
46 function s.handle(self, state, data)
47         return true
48 end
49
50 return m