Python1320

Untitled

Oct 25th, 2010
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.17 KB | None | 0 0
  1. if alan and alan.socket then alan.socket = nil end
  2.  
  3. do
  4.     module("alan", package.seeall)
  5.  
  6.     require("oosocks")
  7.  
  8.     cookies = {}
  9.     answer = ""
  10.     name = "Alan"
  11.     pending_answers = {}
  12.  
  13.     socket = OOSock(IPPROTO_TCP)
  14.  
  15.     function AlanURL()
  16.         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))
  17.     end
  18.  
  19.     function Connect()
  20.         socket:Connect("www.a-i.com", 80)
  21.     end
  22.  
  23.     function GetCookieID(uniqueid, callback, force)
  24.  
  25.         if not force and cookies[uniqueid] then callback(cookies[uniqueid]) return end
  26.  
  27.         local socket = OOSock(IPPROTO_TCP)
  28.  
  29.         socket:Connect("www.a-i.com", 80)
  30.  
  31.         socket:Send(
  32.             Format(
  33. [[GET %s HTTP/1.1
  34. Host: www.a-i.com
  35.  
  36. ]],
  37.  
  38.                 AlanURL()
  39.             )
  40.         )
  41.  
  42.         socket:ReceiveLine()
  43.  
  44.         socket:SetCallback(function(socket, callType, callId, err, data, peer, peerPort)
  45.             if callType == SCKCALL_REC_LINE and err == SCKERR_OK and data ~= "" then
  46.  
  47.                 if IsCookie(data) then
  48.                     cookies[uniqueid] = GetCookie(data)
  49.                     callback(cookies[uniqueid])
  50.                     --print("assigned cookie to", uniqueid, GetCookie(data))
  51.                     socket = nil
  52.                 return end
  53.  
  54.                 socket:ReceiveLine()
  55.  
  56.             end
  57.         end)
  58.  
  59.  
  60.  
  61.     end
  62.  
  63.     function SendQuestion(uniqueid, question, callback, cookie)
  64.  
  65.         if not cookies[uniqueid] then return end
  66.  
  67.         pending_answers[uniqueid] = callback
  68.  
  69.         Connect()
  70.  
  71.         socket:Send(
  72.             Format(
  73. [[GET %s&question=%s HTTP/1.1
  74. Host: www.a-i.com
  75. Cookie: %s
  76.  
  77. ]],
  78.  
  79.                 AlanURL(),
  80.                 URLEscape(question),
  81.                 cookie or cookies[uniqueid]
  82.             )
  83.         )
  84.  
  85.         socket:ReceiveLine()
  86.     end
  87.  
  88.     local cookie_found = false
  89.  
  90.     socket:SetCallback(function(socket, callType, callId, err, data, peer, peerPort)
  91.         if callType == SCKCALL_REC_LINE and err == SCKERR_OK and data ~= "" then
  92.             if IsCookie(data) and IsCookieAssigned(GetCookie(data)) then
  93.                 cookie_found = GetUniqueIDFromCookie(GetCookie(data))
  94.             end
  95.  
  96.             if IsAnswer(data) then print(GetAnswer(data)) end
  97.             if IsCookie(data) then print(GetCookie(data)) end
  98.  
  99.             if cookie_found and IsAnswer(data) and pending_answers[cookie_found] then
  100.                 pending_answers[cookie_found](GetAnswer(data))
  101.                 pending_answers[cookie_found] = nil
  102.                 cookie_found = false
  103.             return end
  104.  
  105.             socket:ReceiveLine()
  106.         end
  107.     end)
  108.  
  109.     function URLEscape(s)
  110.         return string.gsub(s, "([^A-Za-z0-9_])", function(c)
  111.             return string.format("%%%02x", string.byte(c))
  112.         end)
  113.     end
  114.  
  115.     function IsCookieAssigned(cookie)
  116.         return table.HasValue(cookies, cookie)
  117.     end
  118.  
  119.     function GetUniqueIDFromCookie(cookie)
  120.         for key, value in pairs(cookies) do
  121.             if value == cookie then return key end
  122.         end
  123.     end
  124.  
  125.     function IsCookie(data)
  126.         return data:Left(12) == "Set-Cookie: "
  127.     end
  128.  
  129.     function GetCookie(data)
  130.         return data:sub(34, -10)
  131.     end
  132.  
  133.     function SetCookie(cookie, player)
  134.         cookies[data] = player
  135.     end
  136.  
  137.     function IsAnswer(data)
  138.         return data:Left(16) == "<option>answer ="
  139.     end
  140.  
  141.     function GetAnswer(data)
  142.         local answer = data:sub(17, -1):Trim()
  143.         return answer
  144.     end
  145.  
  146. end
  147.  
  148. alan.GetCookieID("caps", function(cookie)
  149.     alan.SendQuestion("caps", "hey", print)
  150.     timer.Simple(3, alan.SendQuestion, "caps", "what are you doing", print)
  151. end)
Advertisement
Add Comment
Please, Sign In to add comment