Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- module("alan", package.seeall)
- require("oosocks")
- cookies = {}
- answer = ""
- name = "Alan"
- pending_answers = {}
- socket = OOSock(IPPROTO_TCP)
- function AlanURL()
- return Format("/alan1/webface1_ctrl.asp?gender=male&name=%s", URLEscape(name))
- end
- function Connect()
- socket:Connect("www.a-i.com", 80)
- end
- function AssignCookie(uniqueid, callback)
- if cookies[uniqueid] then callback(cookies[uniqueid]) return end
- local socket = OOSock(IPPROTO_TCP)
- socket:Connect("www.a-i.com", 80)
- socket:SendLine(Format("GET %s HTTP/1.1", AlanURL()))
- socket:SendLine("Host: www.a-i.com")
- socket:SendLine("")
- socket:ReceiveLine()
- socket:SetCallback(function(_, type, _, status, data)
- if type == SCKCALL_REC_LINE and status == SCKERR_OK and data ~= "" then
- if HasCookie(data) then
- cookies[uniqueid] = ExtractCookie(data)
- if callback then callback(cookies[uniqueid]) end
- --print("assigned cookie to", uniqueid, ExtractCookie(data))
- socket = nil
- return end
- socket:ReceiveLine()
- end
- end)
- end
- function SendQuestion(uniqueid, question, callback)
- if not cookies[uniqueid] then
- print("NO COOKIE FOUND FOR ", uniqueid, " ASSIGNING COOKIE INSTEAD...")
- AssignCookie(uniqueid, function(cookie) print("SENDING QUESTION WITH COOKIE ", cookie) SendQuestion(uniqueid, question, callback) end)
- return end
- pending_answers[uniqueid] = callback
- Connect()
- socket:SendLine(Format("GET %s&question=%s HTTP/1.1", AlanURL(), URLEscape(question)))
- socket:SendLine("Host: www.a-i.com")
- socket:SendLine(Format("Cookie: ASPSESSIONIDACRQSRQC=%s", cookies[uniqueid]))
- socket:SendLine("")
- socket:ReceiveLine()
- print("SET COOKIE TO: ", cookies[uniqueid])
- end
- local cookie_found = false
- local debug_string = ""
- local debug_cookie
- local http_length
- local length_sig = "Content-Length: "
- socket:SetCallback(function(_, type, _, status, data)
- if type == SCKCALL_REC_LINE and status == SCKERR_OK then
- if data ~= "" then
- if HasCookie(data) then
- local cookie = ExtractCookie(data)
- debug_cookie = cookie
- if IsCookieAssigned(cookie) then
- cookie_found = GetUniqueIDFromCookie(cookie)
- else
- cookie_found = "no match"
- end
- end
- if cookie_found and HasAnswer(data) and pending_answers[cookie_found] then
- pending_answers[cookie_found](ExtractAnswer(data), cookies[cookie_found])
- pending_answers[cookie_found] = nil
- cookie_found = false
- return end
- debug_string = debug_string .. data .. "\n"
- if HasAnswer(data) then
- if not cookie_found then
- print("\n\nNO COOKIE FOUND IN ANSWER\n\nSHOWING OUTPUT UNTIL ANSWER:\n\n" .. debug_string, "\n...REST OF OUTPUT...")
- elseif cookie_found == "no match" then
- print("\n\nCOOKIE FOUND IN ANSWER BUT IT DOESN'T MATCH ANY OF THE LOCALLY STORED COOKIES\n\nTHE COOKIE FOUND WAS: " .. debug_cookie)
- end
- cookie_found = false
- debug_string = ""
- return end
- if data:find(length_sig, nil, true) then
- http_length = data:Right(#data - #length_sig)
- end
- socket:ReceiveLine()
- elseif http_length then
- socket:Receive(http_length)
- http_length = nil
- end
- end
- end)
- function URLEscape(s)
- return string.gsub(s, "([^A-Za-z0-9_])", function(c)
- return string.format("%%%02x", string.byte(c))
- end)
- end
- function IsCookieAssigned(cookie)
- return table.HasValue(cookies, cookie)
- end
- function GetUniqueIDFromCookie(cookie)
- for key, value in pairs(cookies) do
- if value == cookie then return key end
- end
- end
- function HasCookie(data)
- return data:find("Set-Cookie: ", nil, true)
- end
- function ExtractCookie(data)
- return data:sub(34, -10)
- end
- function HasAnswer(data)
- return data:find("<option>answer =", nil, true)
- end
- function ExtractAnswer(data)
- return data:sub(17, -1):Trim()
- end
- end
- alan.AssignCookie("caps", function(cookie)
- --print(cookie)
- alan.SendQuestion("caps", "hi", function(answer, cookie)
- --print(answer, cookie)
- alan.SendQuestion("caps", "what are you doing", print)
- end)
- end)
Advertisement
Add Comment
Please, Sign In to add comment