luci-0.8: splash: add controller actions for new interactive status page
[project/luci.git] / applications / luci-splash / luasrc / controller / splash / splash.lua
1 module("luci.controller.splash.splash", package.seeall)
2
3 function index()
4 entry({"admin", "services", "splash"}, cbi("splash/splash"), "Client-Splash")
5
6 node("splash").target = call("action_dispatch")
7 node("splash", "activate").target = call("action_activate")
8 node("splash", "splash").target = template("splash_splash/splash")
9
10 entry({"admin", "status", "splash"}, call("action_status_admin"), "Client-Splash")
11 end
12
13 function action_dispatch()
14 local mac = luci.sys.net.ip4mac(luci.http.getenv("REMOTE_ADDR")) or ""
15 local status = luci.util.execl("luci-splash status "..mac)[1]
16 if #mac > 0 and ( status == "whitelisted" or status == "lease" ) then
17 luci.http.redirect(luci.dispatcher.build_url())
18 else
19 luci.http.redirect(luci.dispatcher.build_url("splash", "splash"))
20 end
21 end
22
23 function action_activate()
24 local ip = luci.http.getenv("REMOTE_ADDR") or "127.0.0.1"
25 local mac = luci.sys.net.ip4mac(ip:match("^[\[::ffff:]*(%d+.%d+%.%d+%.%d+)\]*$"))
26 if mac and luci.http.formvalue("accept") then
27 os.execute("luci-splash add "..mac.." >/dev/null 2>&1")
28 luci.http.redirect(luci.model.uci.cursor():get("freifunk", "community", "homepage"))
29 else
30 luci.http.redirect(luci.dispatcher.build_url())
31 end
32 end
33
34 function action_status_admin()
35 local uci = luci.model.uci.cursor_state()
36 local macs = luci.http.formvaluetable("save")
37
38 local function delete_mac(what, mac)
39 uci:delete_all("luci_splash", what,
40 function(s)
41 return ( s.mac and s.mac:lower() == mac )
42 end)
43 end
44
45 local function leases(mac)
46 local leases = { }
47
48 uci:foreach("luci_splash", "lease", function(s)
49 if s.start and s.mac and s.mac:lower() ~= mac then
50 leases[#leases+1] = {
51 start = s.start,
52 mac = s.mac
53 }
54 end
55 end)
56
57 uci:revert("luci_splash")
58
59 return leases
60 end
61
62 local function commit(leases, no_commit)
63 if not no_commit then
64 uci:save("luci_splash")
65 uci:commit("luci_splash")
66 end
67
68 for _, l in ipairs(leases) do
69 uci:section("luci_splash", "lease", nil, l)
70 end
71
72 uci:save("luci_splash")
73 os.execute("/etc/init.d/luci_splash restart")
74 end
75
76 for key, _ in pairs(macs) do
77 local policy = luci.http.formvalue("policy.%s" % key)
78 local mac = luci.http.protocol.urldecode(key)
79 local lslist = leases(policy ~= "kick" and mac)
80
81 delete_mac("blacklist", mac)
82 delete_mac("whitelist", mac)
83
84 if policy == "whitelist" or policy == "blacklist" then
85 uci:section("luci_splash", policy, nil, { mac = mac })
86 elseif policy == "normal" then
87 lslist[#lslist+1] = { mac = mac, start = os.time() }
88 elseif policy == "kick" then
89 for _, l in ipairs(lslist) do
90 if l.mac:lower() == mac then l.kicked="1" end
91 end
92 end
93
94 commit(lslist)
95 end
96
97 luci.template.render("admin_status/splash", { is_admin = true })
98 end