Fixed a design flaw in luci.model.uci
[project/luci.git] / applications / luci-splash / root / usr / sbin / luci-splash
old mode 100644 (file)
new mode 100755 (executable)
index 347bdcc..520f877
@@ -1,13 +1,12 @@
 #!/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)
@@ -61,28 +60,34 @@ end
 
 -- 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
 
 
@@ -135,7 +140,7 @@ 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
 
 
@@ -148,9 +153,10 @@ function sync()
        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")
        
        
@@ -162,9 +168,10 @@ function sync()
                                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
@@ -178,7 +185,7 @@ function sync()
                end
        end
        
-       uci.save("luci_splash")
+       uci.save_state("luci_splash")
 end
 
 main(arg)
\ No newline at end of file