Complete but undebugged puzzler.cgi

This commit is contained in:
Neale Pickett 2015-04-18 18:09:20 -06:00
parent cdc06beb0e
commit bc233157c1
2 changed files with 67 additions and 6 deletions

45
cgi/koth.lua Normal file
View File

@ -0,0 +1,45 @@
#! /usr/bin/lua
local koth = {}
-- cut -d$ANCHOR -f2- | grep -Fx "$NEEDLE"
function anchored_search(haystack, needle, anchor)
for line in io.lines(haystack) do
if (anchor) then
pos = line:find(anchor)
if (pos) then
line = line:sub(pos+1)
end
end
if (line == needle) then
return true
end
end
return false
end
function koth.anchored_search(haystack, needle, anchor)
local ok, ret = pcall(anchored_search, haystack, needle, anchor)
return ok and ret
end
function koth.page(title, body)
print("Content-type: text/html")
print()
print("<!DOCTYPE html>")
print("<html><head><title>" .. title .. "</title><link rel=\"stylesheet\" href=\"css/style.css\"></head>")
print("<body><h1>" .. title .. "</h1>")
if (body) then
print("<section>")
print(body)
print("</section>")
end
print("</body></html>")
os.exit(0)
end
return koth

View File

@ -1,14 +1,30 @@
#! /usr/bin/lua
local cgi = require "cgi"
local koth = require "koth"
local team = cgi.fields['t'] or ""
local category = cgi.fields['c'] or ""
local points = cgi.fields['p'] or ""
local answer = cgi.fields['a'] or ""
-- Read in team and answer
-- Defang category name; prevent directory traversal
category = category:gsub("[^A-Za-z0-9]", "-")
-- Check answer
local needle = points .. " " .. answer
local haystack = "../puzzles/" .. category .. "/answers.txt"
local found = koth.anchored_search(haystack, needle)
if (not found) then
koth.page("Wrong answer")
end
print("Content-type: text/html")
print()
print("<pre>")
print(cgi.fields["t"])
print("</pre>")
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>")