Python1320

Untitled

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