#! /usr/bin/lua function decode(str) local hexdec = function(h) return string.char(tonumber(h, 16)) end str = string.gsub(str, "+", " ") return string.gsub(str, "%%(%x%x)", hexdec) end function decode_query(query) local ret = {} for key, val in string.gfind(query, "([^&=]+)=([^&=]+)") do ret[string.lower(decode(key))] = decode(val) end return ret end function escape(str) str = string.gsub(str, "&", "&") str = string.gsub(str, "<", "<") str = string.gsub(str, ">", ">") return str end function djbhash(s) local hash = 5380 for i=0,string.len(s) do local c = string.byte(string.sub(s, i, i+1)) hash = math.mod(((hash * 32) + hash + c), 2147483647) end return string.format("%08x", hash) end function head(title) print("Content-type: text/html") print("") print("") print("") print("
") print("I only speak POST. Sorry.
") end inlen = tonumber(os.getenv("CONTENT_LENGTH")) if (inlen > 200) then head("Bad team name") print("That's a bit on the long side, don't you think?
") foot() end formdata = io.read(inlen) f = decode_query(formdata) team = f["t"] if (not team) or (team == "dirtbags") then head("Bad team name") print("Go back and try again.
") foot() end hash = djbhash(team) if io.open(hash) then head("Team name taken") print("Either someone's already using that team name,") print("or you found a hash collision. Either way, you're") print("going to have to pick something else.
") foot() end f = io.open(hash, "w"):write(team) head("Team registered") print("Team name: ") print(escape(team)) print("
") print("Team token: ") print(hash) print("
") print("Save your team token somewhere!") print("You will need it to claim points.
") foot()