moth/www/cgi-bin/moth.lua

107 lines
2.1 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 moth = {}
2015-04-18 18:09:20 -06:00
-- 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
2016-02-23 10:56:01 -07:00
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
2016-02-23 10:56:01 -07:00
2015-04-18 18:09:20 -06:00
if (line == needle) then
f:close()
2015-04-18 18:09:20 -06:00
return true
end
end
2016-02-23 10:56:01 -07:00
f:close()
2015-04-18 18:09:20 -06:00
return false
end
function moth.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>")
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('</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 moth.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
2016-02-23 10:56:01 -07:00
2015-04-18 19:13:07 -06:00
if (comment) then
entry = entry .. " " .. comment
end
2016-02-23 10:56:01 -07:00
local f = io.open(moth.path("state/teams/" .. team))
if (f) then
f:close()
else
return false, "No such team"
end
2016-02-23 10:56:01 -07:00
local ok = moth.anchored_search(moth.path("state/points.log"), entry, " ")
if (ok) then
2015-04-18 19:13:07 -06:00
return false, "Points already awarded"
end
2016-02-23 10:56:01 -07:00
local f = io.open(moth.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
2016-02-23 10:56:01 -07:00
2015-04-18 19:13:07 -06:00
f:write(os.time(), " ", entry, "\n")
f:close()
2016-02-23 10:56:01 -07:00
2015-04-18 19:13:07 -06:00
return true
end
2015-04-21 07:57:11 -06:00
-- Most web servers cd to the directory containing the CGI.
-- Not uhttpd.
moth.base = ""
function moth.path(p)
return moth.base .. p
2015-04-21 07:57:11 -06:00
end
-- Traverse up to find assigned.txt
for i = 0, 5 do
local f = io.open(moth.path("assigned.txt"))
2015-04-21 07:57:11 -06:00
if (f) then
f:close()
break
end
moth.base = moth.base .. "../"
2015-04-21 07:57:11 -06:00
end
2015-04-18 18:09:20 -06:00
return moth