moth/www/register.cgi

106 lines
2.4 KiB
Plaintext
Raw Normal View History

2010-09-01 17:39:46 -06:00
#! /usr/bin/lua
2010-09-01 17:39:46 -06:00
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
2010-09-01 17:39:46 -06:00
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
2010-09-01 17:39:46 -06:00
function escape(str)
str = string.gsub(str, "&", "&")
str = string.gsub(str, "<", "&lt;")
str = string.gsub(str, ">", "&gt;")
return str
end
2010-09-01 17:39:46 -06:00
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
2010-09-01 17:39:46 -06:00
function head(title)
print("Content-type: text/html")
print("")
print("<!DOCTYPE html>")
print("<html>")
print(" <head>")
print(" <title>")
print(title)
print(" </title")
print(' <link rel="stylesheet" href="ctf.css" type="text/css">')
print(" </head>")
print(" <body>")
print(" <h1>")
print(title)
print(" </h1>")
end
2010-09-01 17:39:46 -06:00
function foot()
print(" </body>")
print("</html>")
os.exit()
end
2010-09-01 17:39:46 -06:00
if (os.getenv("REQUEST_METHOD") ~= "POST") then
print("405 Method not allowed")
print("Allow: POST")
print("Content-type: text/html")
print()
print("<h1>Method not allowed</h1>")
print("<p>I only speak POST. Sorry.</p>")
end
2010-09-01 17:39:46 -06:00
inlen = tonumber(os.getenv("CONTENT_LENGTH"))
if (inlen > 200) then
head("Bad team name")
print("<p>That's a bit on the long side, don't you think?</p>")
foot()
end
formdata = io.read(inlen)
f = decode_query(formdata)
2010-09-01 17:39:46 -06:00
team = f["t"]
if (not team) or (team == "dirtbags") then
head("Bad team name")
print("<p>Go back and try again.</p>")
foot()
end
hash = djbhash(team)
2010-09-01 17:39:46 -06:00
if io.open(hash) then
head("Team name taken")
print("<p>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.</p>")
foot()
end
2010-09-01 17:39:46 -06:00
f = io.open(hash, "w"):write(team)
2010-09-01 17:39:46 -06:00
head("Team registered")
print("<p>Team name: <samp>")
print(escape(team))
print("</samp></p>")
print("<p>Team token: <samp>")
print(hash)
print("</samp></p>")
print("<p><b>Save your team token somewhere</b>!")
print("You will need it to claim points.</p>")
foot()