moth/www/cgi-bin/koth.lua

121 lines
2.6 KiB
Lua
Raw Normal View History

2015-05-26 10:16:22 -06:00
#! /usr/bin/env lua
2015-04-18 18:09:20 -06:00
local koth = {}
-- cut -d$ANCHOR -f2- | grep -Fx "$NEEDLE"
function koth.anchored_search(haystack, needle, anchor)
local f, err = io.open(haystack)
if (not f) then
return false, err
end
for line in f:lines() do
2015-04-18 18:09:20 -06:00
if (anchor) then
pos = line:find(anchor)
if (pos) then
line = line:sub(pos+1)
end
end
if (line == needle) then
f:close()
2015-04-18 18:09:20 -06:00
return true
end
end
f:close()
2015-04-18 18:09:20 -06:00
return false
end
function koth.page(title, body)
2015-04-19 23:14:31 -06:00
if (os.getenv("REQUEST_METHOD")) then
print("Content-type: text/html")
print()
end
2015-04-18 18:09:20 -06:00
print("<!DOCTYPE html>")
2015-04-21 07:57:11 -06:00
print("<html><head><title>" .. title .. "</title><link rel=\"stylesheet\" href=\"../style.css\"><meta name=\"viewport\" content=\"width=device-width\"></head>")
2015-04-18 18:09:20 -06:00
print("<body><h1>" .. title .. "</h1>")
if (body) then
print("<section>")
print(body)
print("</section>")
end
2017-10-25 16:46:24 -06:00
print('<nav>')
print('<ul>')
print('<li><a href="../register.html">Register</a></li>')
print('<li><a href="../puzzles.html">Puzzles</a></li>')
print('<li><a href="../scoreboard.html">Scoreboard</a></li>')
print('</ul>')
print('</nav>')
2015-05-28 13:58:54 -06:00
print('<section id="sponsors">')
2017-11-03 13:41:39 -06:00
print('<img src="../images/logo0.png" alt="">')
print('<img src="../images/logo1.png" alt="">')
print('<img src="../images/logo2.png" alt="">')
print('<img src="../images/logo3.png" alt="">')
2018-05-11 08:27:31 -06:00
print('<img src="../images/logo4.png" alt="">')
2015-05-28 13:58:54 -06:00
print('</section>')
2015-04-18 18:09:20 -06:00
print("</body></html>")
os.exit(0)
end
2015-04-18 19:13:07 -06:00
--
-- We're going to rely on `bin/once` only processing files with the right number of lines.
--
function koth.award_points(team, category, points, comment)
team = team:gsub("[^0-9a-f]", "-")
2015-05-28 13:58:54 -06:00
if (team == "") then
team = "-"
end
2015-04-19 22:57:35 -06:00
local filename = team .. "." .. category .. "." .. points
2015-04-18 19:13:07 -06:00
local entry = team .. " " .. category .. " " .. points
if (comment) then
entry = entry .. " " .. comment
end
2015-04-21 07:57:11 -06:00
local f = io.open(koth.path("state/teams/" .. team))
if (f) then
f:close()
else
return false, "No such team"
end
2015-04-21 07:57:11 -06:00
local ok = koth.anchored_search(koth.path("state/points.log"), entry, " ")
if (ok) then
2015-04-18 19:13:07 -06:00
return false, "Points already awarded"
end
2015-04-21 07:57:11 -06:00
local f = io.open(koth.path("state/points.new/" .. filename), "a")
2015-04-18 19:13:07 -06:00
if (not f) then
return false, "Unable to write to points file"
end
f:write(os.time(), " ", entry, "\n")
f:close()
return true
end
2015-04-21 07:57:11 -06:00
-- Most web servers cd to the directory containing the CGI.
-- Not uhttpd.
koth.base = ""
function koth.path(p)
return koth.base .. p
end
-- Traverse up to find assigned.txt
for i = 0, 5 do
2017-10-11 18:55:34 -06:00
local f = io.open(koth.path("state/assigned.txt"))
2015-04-21 07:57:11 -06:00
if (f) then
f:close()
break
end
koth.base = koth.base .. "../"
end
2015-04-18 18:09:20 -06:00
return koth