Python1320

Untitled

Oct 25th, 2010
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 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&style=%s&name=%s&action0=name:%s&DName=%s", URLEscape(name), URLEscape(name), URLEscape(name), 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.                     callback(cookies[uniqueid])
  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.  
  51.  
  52.     end
  53.  
  54.     function SendQuestion(uniqueid, question, callback)
  55.  
  56.         if not cookies[uniqueid] then return end
  57.  
  58.         pending_answers[uniqueid] = callback
  59.  
  60.         Connect()
  61.  
  62.         socket:SendLine(Format("GET %s&question=%s HTTP/1.1", AlanURL(), URLEscape(question)))
  63.         socket:SendLine("Host: www.a-i.com")
  64.         socket:SendLine(Format("Cookie: ASPSESSIONIDACRQSRQC=%s", cookies[uniqueid]))
  65.         socket:SendLine("")
  66.  
  67.         socket:ReceiveLine()
  68.        
  69.         print("SET COOKIE TO:", Format("'Cookie: ASPSESSIONIDACRQSRQC=%s'", cookies[uniqueid]))
  70.        
  71.     end
  72.  
  73.     local cookie_found = false
  74.     local debug_string = ""
  75.    
  76.     socket:SetCallback(function(_, type, _, status, data)
  77.         if type == SCKCALL_REC_LINE and status == SCKERR_OK and data ~= "" then
  78.             if HasCookie(data) then
  79.                 local cookie = ExtractCookie(data)
  80.                 if IsCookieAssigned(cookie) then
  81.                     cookie_found = GetUniqueIDFromCookie(cookie)
  82.                 end
  83.             end
  84.            
  85.             if cookie_found and HasAnswer(data) and pending_answers[cookie_found] then
  86.                 pending_answers[cookie_found](ExtractAnswer(data), cookies[cookie_found])
  87.                 pending_answers[cookie_found] = nil
  88.                 cookie_found = false
  89.             return end
  90.  
  91.             debug_string = debug_string .. data .. "\n"
  92.            
  93.             if HasAnswer(data) then
  94.                 if not cookie_found then
  95.                     print("\n\nNO COOKIE FOUND IN ANSWER\n\nSHOWING OUTPUT UNTIL ANSWER:\n\n" .. debug_string, "\n...REST OF OUTPUT...")
  96.                 end
  97.                 cookie_found = false
  98.                 debug_string = ""
  99.             return end
  100.  
  101.             socket:ReceiveLine()
  102.         end
  103.     end)
  104.  
  105.     function URLEscape(s)
  106.         return string.gsub(s, "([^A-Za-z0-9_])", function(c)
  107.             return string.format("%%%02x", string.byte(c))
  108.         end)
  109.     end
  110.  
  111.     function IsCookieAssigned(cookie)
  112.         return table.HasValue(cookies, cookie)
  113.     end
  114.  
  115.     function GetUniqueIDFromCookie(cookie)
  116.         for key, value in pairs(cookies) do
  117.             if value == cookie then return key end
  118.         end
  119.     end
  120.  
  121.     function HasCookie(data)
  122.         return data:Left(12) == "Set-Cookie: "
  123.     end
  124.  
  125.     function ExtractCookie(data)
  126.         --print(data:sub(34, -10), data)
  127.         return data:sub(34, -10)
  128.     end
  129.  
  130.     function HasAnswer(data)
  131.         return data:Left(16) == "<option>answer ="
  132.     end
  133.  
  134.     function ExtractAnswer(data)
  135.         return data:sub(17, -1):Trim()
  136.     end
  137.  
  138. end
  139.  
  140.  
  141. alan.AssignCookie("caps", function(cookie)
  142.     --print(cookie)
  143.     alan.SendQuestion("caps", "hi", function(answer, cookie)
  144.         --print(answer, cookie)
  145.         alan.SendQuestion("caps", "what are you doing", print)
  146.     end)
  147. end)
Advertisement
Add Comment
Please, Sign In to add comment