whitelist_tab.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. -- Copyright 2017-2018 Dirk Brenken (dev@brenken.org)
  2. -- This is free software, licensed under the Apache License, Version 2.0
  3. local fs = require("nixio.fs")
  4. local util = require("luci.util")
  5. local uci = require("luci.model.uci").cursor()
  6. local input = uci:get("adblock", "global", "adb_whitelist") or "/etc/adblock/adblock.whitelist"
  7. if not fs.access(input) then
  8. m = SimpleForm("error", nil, translate("Input file not found, please check your configuration."))
  9. m.reset = false
  10. m.submit = false
  11. return m
  12. end
  13. if fs.stat(input).size >= 102400 then
  14. m = SimpleForm("error", nil,
  15. translate("The file size is too large for online editing in LuCI (≥ 100 KB). ")
  16. .. translate("Please edit this file directly in a terminal session."))
  17. m.reset = false
  18. m.submit = false
  19. return m
  20. end
  21. m = SimpleForm("input", nil)
  22. m:append(Template("adblock/adblock_css"))
  23. m.submit = translate("Save")
  24. m.reset = false
  25. s = m:section(SimpleSection, nil,
  26. translatef("This form allows you to modify the content of the adblock whitelist (%s). ", input)
  27. .. translate("Please add only one domain per line. Comments introduced with '#' are allowed - ip addresses, wildcards and regex are not."))
  28. f = s:option(TextValue, "data")
  29. f.datatype = "string"
  30. f.rows = 20
  31. f.rmempty = true
  32. function f.cfgvalue()
  33. return fs.readfile(input) or ""
  34. end
  35. function f.write(self, section, data)
  36. return fs.writefile(input, "\n" .. util.trim(data:gsub("\r\n", "\n")) .. "\n")
  37. end
  38. function f.remove(self, section, value)
  39. return fs.writefile(input, "")
  40. end
  41. function s.handle(self, state, data)
  42. return true
  43. end
  44. return m