Python1320

Untitled

Oct 28th, 2010
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.01 KB | None | 0 0
  1. do
  2.  
  3.     module("alan", package.seeall)
  4.  
  5.     require("oosocks")
  6.  
  7.     cookies = {}
  8.     answer = ""
  9.     name = "Alan"
  10.     pending_answers = {}
  11.  
  12.     socket = OOSock(IPPROTO_TCP)
  13.  
  14.     function AlanURL()
  15.         return Format("/alan1/webface1_ctrl.asp?gender=male&name=%s", URLEscape(name))
  16.     end
  17.  
  18.     function AssignCookie(uniqueid, callback)
  19.  
  20.         if cookies[uniqueid] then callback(cookies[uniqueid]) return end
  21.  
  22.         local socket = OOSock(IPPROTO_TCP)
  23.  
  24.         socket:Connect("www.a-i.com", 80)
  25.  
  26.         socket:SendLine(Format("GET %s HTTP/1.1", AlanURL()))
  27.         socket:SendLine("Host: www.a-i.com")
  28.         socket:SendLine("")
  29.  
  30.         socket:ReceiveLine()
  31.  
  32.         socket:SetCallback(function(_, type, _, status, data)
  33.             if type == SCKCALL_REC_LINE and status == SCKERR_OK and data ~= "" then
  34.                 if HasCookie(data) then
  35.                     cookies[uniqueid] = ExtractCookie(data)
  36.                     if callback then callback(cookies[uniqueid]) end
  37.                     socket = nil
  38.                 return end
  39.  
  40.                 socket:ReceiveLine()
  41.  
  42.             end
  43.         end)
  44.  
  45.     end
  46.  
  47.     function SendQuestion(uniqueid, question, callback)
  48.  
  49.         if not cookies[uniqueid] then
  50.             print("NO COOKIE FOUND FOR ", uniqueid, " ASSIGNING A NEW COOKIE INSTEAD...")
  51.             AssignCookie(uniqueid, function(cookie) print("SENDING QUESTION WITH COOKIE ", cookie, " WITH ", uniqueid) SendQuestion(uniqueid, question, callback) end)
  52.         return end
  53.  
  54.         pending_answers[uniqueid] = callback
  55.  
  56.         socket:Connect("www.a-i.com", 80)
  57.  
  58.         socket:SendLine(Format("GET %s&question=%s HTTP/1.1", AlanURL(), URLEscape(question)))
  59.         socket:SendLine("Host: www.a-i.com")
  60.         socket:SendLine(Format("Cookie: ASPSESSIONIDACRQSRQC=%s", cookies[uniqueid]))
  61.         socket:SendLine("")
  62.        
  63.         socket:ReceiveLine()
  64.  
  65.         print("SET COOKIE TO: ", cookies[uniqueid])
  66.  
  67.     end
  68.  
  69.     local cookie_found = false
  70.     local debug_string = ""
  71.     local debug_cookie
  72.     local http_length
  73.     local length_sig = "Content-Length: "
  74.  
  75.     socket:SetCallback(function(_, type, _, status, data)
  76.         if type == SCKCALL_REC_LINE and status == SCKERR_OK then
  77.             if data ~= "" then
  78.                 if HasCookie(data) then
  79.                     local cookie = ExtractCookie(data)
  80.                     debug_cookie = cookie
  81.                     if IsCookieAssigned(cookie) then
  82.                         cookie_found = GetUniqueIDFromCookie(cookie)
  83.                     else
  84.                         cookie_found = "no match"
  85.                     end
  86.                 end
  87.  
  88.                 if cookie_found and HasAnswer(data) and pending_answers[cookie_found] then
  89.                     pending_answers[cookie_found](ExtractAnswer(data), cookies[cookie_found])
  90.                     pending_answers[cookie_found] = nil
  91.                     cookie_found = false
  92.                 return end
  93.  
  94.                 debug_string = debug_string .. data .. "\n"
  95.  
  96.                 if HasAnswer(data) then
  97.                     if not cookie_found then
  98.                         print("\n\nNO COOKIE FOUND IN ANSWER\n\nSHOWING OUTPUT UNTIL ANSWER:\n\n" .. debug_string, "\n...REST OF OUTPUT...")
  99.                     elseif cookie_found == "no match" then
  100.                         print("\n\nCOOKIE FOUND IN ANSWER BUT IT DOESN'T MATCH ANY OF THE LOCALLY STORED COOKIES\n\nTHE COOKIE FOUND WAS: " .. debug_cookie)
  101.                     end
  102.                     cookie_found = false
  103.                     debug_string = ""
  104.                 return end
  105.                
  106.                 if data:find(length_sig, nil, true) then
  107.                     http_length = data:Right(#data - #length_sig)
  108.                 end
  109.  
  110.                 socket:ReceiveLine()
  111.             elseif http_length then
  112.                 socket:Receive(http_length)
  113.                 http_length = nil
  114.             end
  115.         end
  116.     end)
  117.  
  118.     function URLEscape(s)
  119.         return string.gsub(s, "([^A-Za-z0-9_])", function(c)
  120.             return string.format("%%%02x", string.byte(c))
  121.         end)
  122.     end
  123.  
  124.     function IsCookieAssigned(cookie)
  125.         return table.HasValue(cookies, cookie)
  126.     end
  127.  
  128.     function GetUniqueIDFromCookie(cookie)
  129.         for key, value in pairs(cookies) do
  130.             if value == cookie then return key end
  131.         end
  132.     end
  133.  
  134.     function HasCookie(data)
  135.         return data:find("Set-Cookie: ", nil, true)
  136.     end
  137.  
  138.     function ExtractCookie(data)
  139.         return data:sub(34, -10)
  140.     end
  141.  
  142.     function HasAnswer(data)
  143.         return data:find("<option>answer =", nil, true)
  144.     end
  145.  
  146.     function ExtractAnswer(data)
  147.         return data:sub(17, -1):Trim()
  148.     end
  149.  
  150. end
  151.  
  152.  
  153. alan.AssignCookie("caps", function(cookie)
  154.     alan.SendQuestion("caps", "hi", function(answer, cookie)
  155.         alan.SendQuestion("caps", "what are you doing", print)
  156.     end)
  157. end)
Advertisement
Add Comment
Please, Sign In to add comment