410b6608c33213e633c75d2e8e78612c49ac5313
[project/luci.git] / contrib / package / luci-splash / src / luci-splash / sync.lua
1 #!/usr/bin/haserl --shell=luac --accept-none
2 dofile("/usr/lib/luci-splash/splash.lua")
3
4 local written = {}
5 local time = os.time()
6
7 -- Current leases in state files
8 local leases = uci:show("luci_splash").luci_splash
9
10 -- Convert leasetime to seconds
11 local leasetime = tonumber(uci:get("luci_splash", "general", "leasetime")) * 3600
12
13 -- Clean state file
14 uci:revert("luci_splash")
15
16
17 -- For all leases
18 for k, v in pairs(uci:show("luci_splash")) do
19 if v[".type"] == "lease" then
20 if os.difftime(time, tonumber(v.start)) > leasetime then
21 -- Remove expired
22 remove_rule(v.mac)
23 else
24 -- Rewrite state
25 local n = uci:add("luci_splash", "lease")
26 uci:set("luci_splash", n, "mac", v.mac)
27 uci:set("luci_splash", n, "start", v.start)
28 written[v.mac] = 1
29 end
30 end
31 end
32
33
34 -- Delete rules without state
35 for i, r in ipairs(listrules()) do
36 if not written[r] then
37 remove_rule(r)
38 end
39 end