moth/www/cgi-bin/token.cgi

39 lines
1.1 KiB
Plaintext
Raw Normal View History

2015-06-03 15:31:26 -06:00
#! /usr/bin/env lua
package.path = "?.lua;cgi-bin/?.lua;www/cgi-bin/?.lua"
local cgi = require "cgi"
local moth = require "moth"
2015-06-03 15:31:26 -06:00
local team = cgi.fields['t'] or ""
local token = cgi.fields['k'] or ""
-- Check answer
local needle = token
local haystack = moth.path("tokens.txt")
local found, err = moth.anchored_search(haystack, needle)
2015-06-03 15:31:26 -06:00
if (not found) then
moth.page("Unrecognized token", err)
2015-06-03 15:31:26 -06:00
end
2015-06-03 15:42:03 -06:00
local category, points = token:match("^(.*):(.*):")
if ((category == nil) or (points == nil)) then
moth.page("Unrecognized token", "Something doesn't look right about that token")
2015-06-03 15:31:26 -06:00
end
points = tonumber(points)
2015-06-03 15:42:03 -06:00
-- Defang category name; prevent directory traversal
category = category:gsub("[^A-Za-z0-9]", "-")
local ok, err = moth.award_points(team, category, points, token)
2015-06-03 15:31:26 -06:00
if (not ok) then
moth.page("Error awarding points",
2015-06-03 15:31:26 -06:00
"<p>You entered a valid token, but there was a problem trying to give you points:</p>" ..
"<p>" .. err .. "</p>")
end
moth.page("Points awarded",
2015-06-03 15:31:26 -06:00
"<p>" .. points .. " points for " .. team .. "!</p>" ..
"<p><a href=\"../puzzles.html\">Back to puzzles</a></p>")