applications/luci-splash:
[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 lease "..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 changes = { 
39                 whitelist = { },
40                 blacklist = { },
41                 lease     = { },
42                 remove    = { }
43         }
44
45         for key, _ in pairs(macs) do
46                 local policy = luci.http.formvalue("policy.%s" % key)
47                 local mac    = luci.http.protocol.urldecode(key)
48
49                 if policy == "whitelist" or policy == "blacklist" then
50                         changes[policy][#changes[policy]+1] = mac
51                 elseif policy == "normal" then
52                         changes["lease"][#changes["lease"]+1] = mac
53                 elseif policy == "kicked" then
54                         changes["remove"][#changes["remove"]+1] = mac
55                 end
56         end
57
58         if #changes.whitelist > 0 then
59                 os.execute("luci-splash whitelist %s >/dev/null"
60                         % table.concat(changes.whitelist))
61         end
62
63         if #changes.blacklist > 0 then
64                 os.execute("luci-splash blacklist %s >/dev/null"
65                         % table.concat(changes.blacklist))
66         end
67
68         if #changes.lease > 0 then
69                 os.execute("luci-splash lease %s >/dev/null"
70                         % table.concat(changes.lease))
71         end
72
73         if #changes.remove > 0 then
74                 os.execute("luci-splash remove %s >/dev/null"
75                         % table.concat(changes.remove))
76         end
77
78         luci.template.render("admin_status/splash", { is_admin = true })
79 end