#! /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("") print("" .. title .. "") print("

" .. title .. "

") if (body) then print("
") print(body) print("
") end print('') print('
') print('') print('') print('') print('') print('
') print("") 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("state/assigned.txt")) if (f) then f:close() break end moth.base = moth.base .. "../" end return moth