Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require("libraries.infutil") -- an api i made in CC partially ported, i use it for serialization
- local socket=require "socket"
- local sv=socket.bind("*",8080)
- local irc=socket.connect("irc.esper.net",6667)
- irc:send("NICK StalkerBot\n")
- irc:send("USER StalkerBot ToastBot1.0 irc.esper.net :<3~\n")
- local ircstate=0
- irc:settimeout(0)
- sv:settimeout(0)
- local cli={}
- local function getmax(tbl)
- local mx=0
- for k,v in pairs(tbl) do
- if type(k)=="number" then
- if k>mx then
- mx=k
- end
- end
- end
- return mx
- end
- local function procHead(sHead)
- local out={}
- local t
- for k,v in pairs(sHead) do
- t=string.find(v," ")
- if t then
- out[string.sub(v,1,t-1)]=string.sub(v,t+1)
- end
- end
- return out
- end
- local function urldecode(str)
- return str:gsub("+"," "):gsub("%%(%x%x)",function(h) return string.char(tonumber(h,16)) end)
- end
- local function serve(cl,i)
- cl.obj:send("HTTP/1.1 302 Found\r\nServer: potato\r\nConnection: close\r\nContent-Type: text/html\r\n")
- local head=procHead(cl.head)
- local req=(head.GET or head.POST or ""):match(" HTTP/.")
- if req then
- req=string.sub(req,1,(-#req)-3)
- if string.sub(req,1,1)=="/" then
- req=string.sub(req,2,-1)
- end
- end
- if cl.post.data then
- cl.post.data=urldecode(cl.post.data:sub(6)):gsub("\n"," "):gsub("\r","")
- local ip,port=cl.obj:getpeername()
- irc:send("PRIVMSG #pixel :["..ip..":"..port.."] "..cl.post.data.."\n")
- end
- local content='<form action="http://71.238.152.180:8080/" method="post"><p><input type="text" name="data"><input type="submit" value="Send"></form>'
- if cl.post.data then
- content=content.."<br>"..cl.post.data
- end
- cl.obj:send("Content-Length: "..tostring(string.len(content)).."\r\n\r\n"..content)
- cl.closed=true
- cl.obj:close()
- cli[i]=nil
- end
- local function split(txt)
- local tbl={}
- for s in string.gmatch(txt,"[^ ]+") do
- table.insert(tbl,s)
- end
- return tbl
- end
- local function sparseirc(txt)
- if string.sub(txt,1,1)==":" then
- local t1=string.find(txt," ")
- local t2=string.find(txt," :")
- local t3=string.find(txt,"!")
- local name
- if t3 then
- name=string.sub(txt,2,t3-1)
- else
- name="*"
- end
- if t2 then
- return {name,split(string.sub(txt,t1+1,t2-1)),string.sub(txt,t2+2)}
- else
- return {name,split(string.sub(txt,t1+1))}
- end
- else
- local t=string.find(txt," ") or #txt+1
- return {1,string.sub(txt,1,t-1),string.sub(txt,t+1)}
- end
- end
- local function irc_say(txt)
- irc:send("PRIVMSG #pixel :"..txt.."\n")
- end
- function love.update(dt)
- local s=sv:accept()
- while s do
- s:settimeout(0)
- local a=1
- while true do
- if not cli[a] then
- cli[a]={obj=s,ip=s:getpeername(),dt=0,head={},post={}}
- break
- end
- a=a+1
- end
- s=sv:accept()
- end
- for i=1,getmax(cli) do
- local cl=cli[i]
- if cl then
- local s,e=cl.obj:receive(cl.post.len)
- while s do
- print("[80] "..s)
- if cl.post.len then
- cl.post.data=s
- serve(cl,i)
- end
- if string.sub(s,-1,-1)=="\r" then
- s=string.sub(s,1,-2)
- end
- if s=="" then
- local tproc=procHead(cl.head)
- if tproc.POST then
- if tproc["Content-Length:"] then
- cl.post.len=tonumber(tproc["Content-Length:"])
- end
- else
- serve(cl,i)
- end
- else
- table.insert(cl.head,s)
- end
- if not cl.closed then
- s,e=cl.obj:receive(cl.post.len)
- else
- s=nil
- end
- end
- cl.dt=cl.dt+dt
- if cl.dt>5 and not cl.closed then
- cl.closed=true
- cl.obj:close()
- cli[i]=nil
- end
- end
- end
- local s,e=irc:receive()
- if s then
- local c=sparseirc(s)
- print(infutil.serialize(c))
- if c[1]==1 and c[2]=="PING" then
- irc:send("PONG "..c[3].."\n")
- elseif c[1]=="*" and c[3]=="End of /MOTD command." then
- irc:send("JOIN #pixel\n")
- elseif c[1]~="*" and type(c[2])=="table" then
- if c[2][1]=="PRIVMSG" and c[2][2]=="#pixel" then
- if string.sub(c[3],1,2)=="$$" then
- c[3]=string.sub(c[3],3)
- local p={}
- for d in string.gmatch(c[3],"%S+") do
- table.insert(p,d)
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement