Python1320

Untitled

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