add solution for nbcontest, progra, n°2
This commit is contained in:
parent
7e5570b1e0
commit
2be1683253
1 changed files with 116 additions and 0 deletions
116
newbiecontest/prog/calcul/calcul.lua
Executable file
116
newbiecontest/prog/calcul/calcul.lua
Executable file
|
@ -0,0 +1,116 @@
|
|||
#!/usr/bin/env lua
|
||||
|
||||
-- require ---------------------------------------------------------------------
|
||||
local luasql = require "luasql.sqlite3" -- sqlite module
|
||||
local http = require "socket.http" -- http module
|
||||
local ltn12 = require("ltn12") -- ltn12 module used to convert a sink to a table
|
||||
|
||||
-- functions -------------------------------------------------------------------
|
||||
-- Extract info about the cookie used for newbiecontest connection
|
||||
function extract_cookie(db)
|
||||
|
||||
-- cookie value
|
||||
cursor = db:execute("SELECT value FROM moz_cookies WHERE host LIKE\
|
||||
'%newbiecontest.org%' AND name LIKE '%SMFCookie%'")
|
||||
cookie_value = cursor:fetch(row)
|
||||
cursor:close()
|
||||
|
||||
-- cookie name
|
||||
cursor = db:execute("SELECT name FROM moz_cookies WHERE host LIKE\
|
||||
'%newbiecontest.org%' AND name LIKE '%SMFCookie%'")
|
||||
cookie_name = cursor:fetch(row)
|
||||
cursor:close()
|
||||
|
||||
return cookie_value, cookie_name
|
||||
|
||||
end
|
||||
|
||||
-- Extract number from a given url from newbiecontest
|
||||
function extract_number(cookie_name, cookie_value, url)
|
||||
|
||||
-- create the table
|
||||
local t = {}
|
||||
|
||||
-- add the SMF cookie to the header
|
||||
local headers = {
|
||||
["Cookie"] = cookie_name .. "=" .. cookie_value;
|
||||
}
|
||||
|
||||
-- request
|
||||
r = http.request{url = url,
|
||||
headers = headers,
|
||||
sink = ltn12.sink.table(t) -- information goes to the table t
|
||||
}
|
||||
|
||||
-- convert the answer to string
|
||||
string_r = table.concat(t)
|
||||
|
||||
-- extract the number
|
||||
number = string.match(string_r, "%d+")
|
||||
|
||||
return number
|
||||
|
||||
end
|
||||
|
||||
-- Do the sqrt(a) * b
|
||||
function calcul(a, b)
|
||||
|
||||
y = math.sqrt(a) * b
|
||||
|
||||
return math.floor(y)
|
||||
|
||||
end
|
||||
|
||||
-- Give the response back to the server
|
||||
function send_response(r, url)
|
||||
|
||||
-- crate the table
|
||||
local t = {}
|
||||
|
||||
-- concatenate the url
|
||||
url = url .. "?solution=" .. r
|
||||
|
||||
-- add the SMF cookie to the header
|
||||
local headers = {
|
||||
["Cookie"] = cookie_name .. "=" .. cookie_value;
|
||||
}
|
||||
|
||||
-- send the reply
|
||||
r = http.request{url = url,
|
||||
headers = headers,
|
||||
sink = ltn12.sink.table(t)
|
||||
}
|
||||
|
||||
print(table.concat(t)) -- print the token
|
||||
|
||||
return 0
|
||||
|
||||
end
|
||||
|
||||
-- main -----------------------------------------------------------------------
|
||||
if table.maxn(arg) < 4
|
||||
then
|
||||
print("Get. The. Fuck. Out")
|
||||
print("[i] Usage : ./calcul.lua file_db url_1 url_2 url_3")
|
||||
os.exit()
|
||||
end
|
||||
|
||||
-- sqlite connection
|
||||
env = luasql.sqlite3()
|
||||
conn = env:connect(arg[1])
|
||||
|
||||
cookie_value, cookie_name = extract_cookie(conn)
|
||||
|
||||
a = extract_number(cookie_name, cookie_value, arg[2])
|
||||
b = extract_number(cookie_name, cookie_value, arg[3])
|
||||
|
||||
r = calcul(a, b)
|
||||
|
||||
send_response(r, arg[4])
|
||||
|
||||
-- sqlite close
|
||||
env:close()
|
||||
conn:close()
|
||||
|
||||
print("Done.")
|
||||
|
Loading…
Reference in a new issue