mirror of https://github.com/dirtbags/moth.git
107 lines
2.1 KiB
Lua
107 lines
2.1 KiB
Lua
#! /usr/bin/env lua
|
|
|
|
local moth = {}
|
|
|
|
-- cut -d$ANCHOR -f2- | grep -Fx "$NEEDLE"
|
|
function moth.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
|
|
if (anchor) then
|
|
pos = line:find(anchor)
|
|
if (pos) then
|
|
line = line:sub(pos+1)
|
|
end
|
|
end
|
|
|
|
if (line == needle) then
|
|
f:close()
|
|
return true
|
|
end
|
|
end
|
|
|
|
f:close()
|
|
return false
|
|
end
|
|
|
|
function moth.page(title, body)
|
|
if (os.getenv("REQUEST_METHOD")) then
|
|
print("Content-type: text/html")
|
|
print()
|
|
end
|
|
print("<!DOCTYPE html>")
|
|
print("<html><head><title>" .. title .. "</title><link rel=\"stylesheet\" href=\"style.css\"><meta name=\"viewport\" content=\"width=device-width\"></head>")
|
|
print("<body><h1>" .. title .. "</h1>")
|
|
if (body) then
|
|
print("<section>")
|
|
print(body)
|
|
print("</section>")
|
|
end
|
|
print('<section id="sponsors">')
|
|
print('</section>')
|
|
print("</body></html>")
|
|
os.exit(0)
|
|
end
|
|
|
|
--
|
|
-- We're going to rely on `bin/once` only processing files with the right number of lines.
|
|
--
|
|
function moth.award_points(team, category, points, comment)
|
|
team = team:gsub("[^0-9a-f]", "-")
|
|
if (team == "") then
|
|
team = "-"
|
|
end
|
|
|
|
local filename = team .. "." .. category .. "." .. points
|
|
local entry = team .. " " .. category .. " " .. points
|
|
|
|
if (comment) then
|
|
entry = entry .. " " .. comment
|
|
end
|
|
|
|
local f = io.open(moth.path("state/teams/" .. team))
|
|
if (f) then
|
|
f:close()
|
|
else
|
|
return false, "No such team"
|
|
end
|
|
|
|
local ok = moth.anchored_search(moth.path("state/points.log"), entry, " ")
|
|
if (ok) then
|
|
return false, "Points already awarded"
|
|
end
|
|
|
|
local f = io.open(moth.path("state/points.new/" .. filename), "a")
|
|
if (not f) then
|
|
return false, "Unable to write to points file"
|
|
end
|
|
|
|
f:write(os.time(), " ", entry, "\n")
|
|
f:close()
|
|
|
|
return true
|
|
end
|
|
|
|
-- Most web servers cd to the directory containing the CGI.
|
|
-- Not uhttpd.
|
|
|
|
moth.base = ""
|
|
function moth.path(p)
|
|
return moth.base .. p
|
|
end
|
|
|
|
-- Traverse up to find assigned.txt
|
|
for i = 0, 5 do
|
|
local f = io.open(moth.path("assigned.txt"))
|
|
if (f) then
|
|
f:close()
|
|
break
|
|
end
|
|
moth.base = moth.base .. "../"
|
|
end
|
|
|
|
return moth
|