#!/usr/bin/lua
require("luci.http")
-require("luci.sys")
+require("luci.util")
require("luci.model.uci")
-- Init state session
-luci.model.uci.set_savedir(luci.model.uci.savedir_state)
+luci.model.uci.load_state("luci_splash")
local uci = luci.model.uci
-local cfg = uci.config
function main(argv)
-- Add a lease to state and invoke add_rule
function add_lease(mac)
- cfg.luci_splash[""] = "lease"
- cfg.luci_splash[""].mac = mac
- cfg.luci_splash[""].start = os.time()
+ uci.section("luci_splash", "lease", nil, {
+ mac = mac,
+ start = os.time()
+ })
add_rule(mac)
- uci.save()
+ uci.save_state("luci_splash")
end
-- Remove a lease from state and invoke remove_rule
function remove_lease(mac)
mac = mac:lower()
+ local del = {}
uci.foreach("luci_splash", "lease",
function (section)
if section.mac:lower() == mac then
- remove_rule(mac)
- uci.delete("luci_splash", section[".name"])
+ table.insert(del, section[".name"])
end
end)
- uci.save()
+ for i,j in ipairs(del) do
+ remove_rule(j)
+ uci.delete("luci_splash", j)
+ end
+
+ uci.save_state("luci_splash")
end
function listrules()
local cmd = "iptables -t nat -L luci_splash_leases | grep RETURN |"
cmd = cmd .. "egrep -io [0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+"
- return luci.util.split(luci.sys.exec(cmd))
+ return luci.util.split(luci.util.exec(cmd))
end
local leases = uci.get_all("luci_splash")
-- Convert leasetime to seconds
- local leasetime = tonumber(cfg.luci_splash.general.leasetime) * 3600
+ local leasetime = tonumber(uci.get("luci_splash", "general", "leasetime")) * 3600
-- Clean state file
+ uci.load_state("luci_splash")
uci.revert("luci_splash")
remove_rule(v.mac)
else
-- Rewrite state
- cfg.luci_splash[""] = "lease"
- cfg.luci_splash[""].mac = v.mac
- cfg.luci_splash[""].start = v.start
+ uci.section("luci_splash", "lease", nil, {
+ mac = v.mac,
+ start = v.start
+ })
written[v.mac:lower()] = 1
end
end
end
end
- uci.save("luci_splash")
+ uci.save_state("luci_splash")
end
main(arg)
\ No newline at end of file