moth/cgi/puzzler.cgi

31 lines
907 B
Plaintext
Raw Normal View History

2015-04-16 22:29:05 -06:00
#! /usr/bin/lua
local cgi = require "cgi"
2015-04-18 18:09:20 -06:00
local koth = require "koth"
2015-04-16 22:29:05 -06:00
2015-04-18 18:09:20 -06:00
local team = cgi.fields['t'] or ""
local category = cgi.fields['c'] or ""
local points = cgi.fields['p'] or ""
local answer = cgi.fields['a'] or ""
2015-04-18 14:24:00 -06:00
2015-04-18 18:09:20 -06:00
-- Defang category name; prevent directory traversal
category = category:gsub("[^A-Za-z0-9]", "-")
2015-04-18 14:24:00 -06:00
2015-04-18 18:09:20 -06:00
-- Check answer
local needle = points .. " " .. answer
local haystack = "../puzzles/" .. category .. "/answers.txt"
local found = koth.anchored_search(haystack, needle)
2015-04-18 14:24:00 -06:00
2015-04-18 18:09:20 -06:00
if (not found) then
koth.page("Wrong answer")
end
2015-04-18 14:24:00 -06:00
2015-04-18 18:09:20 -06:00
local ok = koth.award_points(team, category, points, "P");
if (not ok) then
koth.page("Error awarding points", "You got the right answer, but something blew up trying to give you points. Try again in a few seconds.")
end
koth.page("Points awarded",
"<p>" .. points .. " points for " .. team .. ".</p>" ..
"<p><a href=\"puzzles.html\">Back to puzzles</a></p>")