Token submission

This commit is contained in:
Neale Pickett 2015-06-03 15:31:26 -06:00
parent 3b33ed2b81
commit 8c98b0b56b
2 changed files with 46 additions and 0 deletions

View File

@ -44,6 +44,14 @@ for cat, biggest in pairs(max_by_cat) do
body = body .. "</dd>\n" body = body .. "</dd>\n"
end end
body = body .. "</dl>\n" body = body .. "</dl>\n"
body = body .. "<fieldset><legend>Sandia Token:</legend>"
body = body .. "<p>Example: <samp>sandia:5:xylep-radar-nanox</samp></p>"
body = body .. "<form action='cgi-bin/token.cgi'>"
body = body .. "Team Hash: <input name='t'><br>"
body = body .. "Token: <input name='k'>"
body = body .. "<input type='submit'>"
body = body .. "</form>"
body = body .. "</fieldset>"
body = body .. "<p>Reloading this page periodically may yield updated puzzle lists.</p>" body = body .. "<p>Reloading this page periodically may yield updated puzzle lists.</p>"
koth.page("Open Puzzles", body) koth.page("Open Puzzles", body)

38
www/cgi-bin/token.cgi Executable file
View File

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