moth/www/cgi-bin/koth.lua

110 lines
2.4 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
2015-05-28 13:58:54 -06:00
print('<section id="sponsors">')
print('<img src="../images/lanl.png" alt="Los Alamos National Laboratory">')
print('<img src="../images/doe.png" alt="US Department Of Energy">')
print('<img src="../images/sandia.png" alt="Sandia National Laboratories">')
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