Advertisement
killer64

StalkerBot

Jul 26th, 2013
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.02 KB | None | 0 0
  1. require("libraries.infutil") -- an api i made in CC partially ported, i use it for serialization
  2. local socket=require "socket"
  3. local sv=socket.bind("*",8080)
  4. local irc=socket.connect("irc.esper.net",6667)
  5. irc:send("NICK StalkerBot\n")
  6. irc:send("USER StalkerBot ToastBot1.0 irc.esper.net :<3~\n")
  7. local ircstate=0
  8. irc:settimeout(0)
  9. sv:settimeout(0)
  10. local cli={}
  11. local function getmax(tbl)
  12.     local mx=0
  13.     for k,v in pairs(tbl) do
  14.         if type(k)=="number" then
  15.             if k>mx then
  16.                 mx=k
  17.             end
  18.         end
  19.     end
  20.     return mx
  21. end
  22. local function procHead(sHead)
  23.     local out={}
  24.     local t
  25.     for k,v in pairs(sHead) do
  26.         t=string.find(v," ")
  27.         if t then
  28.             out[string.sub(v,1,t-1)]=string.sub(v,t+1)
  29.         end
  30.     end
  31.     return out
  32. end
  33. local function urldecode(str)
  34.     return str:gsub("+"," "):gsub("%%(%x%x)",function(h) return string.char(tonumber(h,16)) end)
  35. end
  36. local function serve(cl,i)
  37.     cl.obj:send("HTTP/1.1 302 Found\r\nServer: potato\r\nConnection: close\r\nContent-Type: text/html\r\n")
  38.     local head=procHead(cl.head)
  39.     local req=(head.GET or head.POST or ""):match(" HTTP/.")
  40.     if req then
  41.         req=string.sub(req,1,(-#req)-3)
  42.         if string.sub(req,1,1)=="/" then
  43.             req=string.sub(req,2,-1)
  44.         end
  45.     end
  46.     if cl.post.data then
  47.         cl.post.data=urldecode(cl.post.data:sub(6)):gsub("\n"," "):gsub("\r","")
  48.         local ip,port=cl.obj:getpeername()
  49.         irc:send("PRIVMSG #pixel :["..ip..":"..port.."] "..cl.post.data.."\n")
  50.     end
  51.     local content='<form action="http://71.238.152.180:8080/" method="post"><p><input type="text" name="data"><input type="submit" value="Send"></form>'
  52.     if cl.post.data then
  53.         content=content.."<br>"..cl.post.data
  54.     end
  55.     cl.obj:send("Content-Length: "..tostring(string.len(content)).."\r\n\r\n"..content)
  56.     cl.closed=true
  57.     cl.obj:close()
  58.     cli[i]=nil
  59. end
  60. local function split(txt)
  61.     local tbl={}
  62.     for s in string.gmatch(txt,"[^ ]+") do
  63.         table.insert(tbl,s)
  64.     end
  65.     return tbl
  66. end
  67. local function sparseirc(txt)
  68.     if string.sub(txt,1,1)==":" then
  69.         local t1=string.find(txt," ")
  70.         local t2=string.find(txt," :")
  71.         local t3=string.find(txt,"!")
  72.         local name
  73.         if t3 then
  74.             name=string.sub(txt,2,t3-1)
  75.         else
  76.             name="*"
  77.         end
  78.         if t2 then
  79.             return {name,split(string.sub(txt,t1+1,t2-1)),string.sub(txt,t2+2)}
  80.         else
  81.             return {name,split(string.sub(txt,t1+1))}
  82.         end
  83.     else
  84.         local t=string.find(txt," ") or #txt+1
  85.         return {1,string.sub(txt,1,t-1),string.sub(txt,t+1)}
  86.     end
  87. end
  88. local function irc_say(txt)
  89.     irc:send("PRIVMSG #pixel :"..txt.."\n")
  90. end
  91. function love.update(dt)
  92.     local s=sv:accept()
  93.     while s do
  94.         s:settimeout(0)
  95.         local a=1
  96.         while true do
  97.             if not cli[a] then
  98.                 cli[a]={obj=s,ip=s:getpeername(),dt=0,head={},post={}}
  99.                 break
  100.             end
  101.             a=a+1
  102.         end
  103.         s=sv:accept()
  104.     end
  105.     for i=1,getmax(cli) do
  106.         local cl=cli[i]
  107.         if cl then
  108.             local s,e=cl.obj:receive(cl.post.len)
  109.             while s do
  110.                 print("[80] "..s)
  111.                 if cl.post.len then
  112.                     cl.post.data=s
  113.                     serve(cl,i)
  114.                 end
  115.                 if string.sub(s,-1,-1)=="\r" then
  116.                     s=string.sub(s,1,-2)
  117.                 end
  118.                 if s=="" then
  119.                     local tproc=procHead(cl.head)
  120.                     if tproc.POST then
  121.                         if tproc["Content-Length:"] then
  122.                             cl.post.len=tonumber(tproc["Content-Length:"])
  123.                         end
  124.                     else
  125.                         serve(cl,i)
  126.                     end
  127.                 else
  128.                     table.insert(cl.head,s)
  129.                 end
  130.                 if not cl.closed then
  131.                     s,e=cl.obj:receive(cl.post.len)
  132.                 else
  133.                     s=nil
  134.                 end
  135.             end
  136.             cl.dt=cl.dt+dt
  137.             if cl.dt>5 and not cl.closed then
  138.                 cl.closed=true
  139.                 cl.obj:close()
  140.                 cli[i]=nil
  141.             end
  142.         end
  143.     end
  144.     local s,e=irc:receive()
  145.     if s then
  146.         local c=sparseirc(s)
  147.         print(infutil.serialize(c))
  148.         if c[1]==1 and c[2]=="PING" then
  149.             irc:send("PONG "..c[3].."\n")
  150.         elseif c[1]=="*" and c[3]=="End of /MOTD command." then
  151.             irc:send("JOIN #pixel\n")
  152.         elseif c[1]~="*" and type(c[2])=="table" then
  153.             if c[2][1]=="PRIVMSG" and c[2][2]=="#pixel" then
  154.                 if string.sub(c[3],1,2)=="$$" then
  155.                     c[3]=string.sub(c[3],3)
  156.                     local p={}
  157.                     for d in string.gmatch(c[3],"%S+") do
  158.                         table.insert(p,d)
  159.                     end
  160.                 end
  161.             end
  162.         end
  163.     end
  164. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement