Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- server={rcon={},interface={}}
- local uload={}
- local function regunload(obj)
- uload[#uload+1]=obj
- return #uload
- end
- local rcon
- do -- rcon
- local conv,uconv
- do
- local str_byte=string.byte
- local str_char=string.char
- local str_len=string.len
- local str_sub=string.sub
- local m_floor=math.floor
- function conv(txt)
- local s={str_byte(txt,1,4)}
- return s[1]+(s[2]*256)+(s[3]*65536)+(s[4]*16777216)
- end
- function uconv(num)
- return str_char(num%256)..str_char(math.floor(num/256)%65535)..str_char(math.floor(num/65535)%16777216)..str_char(math.floor(num/16777216))
- end
- end
- local colors={
- ["0"]="01",
- ["1"]="02",
- ["2"]="03",
- ["3"]="10",
- ["4"]="69",
- ["5"]="06",
- ["6"]="73",
- ["7"]="15",
- ["8"]="14",
- ["9"]="12",
- ["a"]="09",
- ["b"]="11",
- ["c"]="04",
- ["d"]="13",
- ["e"]="08",
- ["f"]="17",
- }
- local function fmtclr(txt)
- for k,v in pairs(colors) do
- txt=txt:gsub("\167"..k,"\003"..v)
- end
- return txt
- end
- local buffer=""
- function server.rcon.connect(y)
- rcon=socket.connect("localhost",25575)
- while not rcon do
- print("[RCON] connecting")
- y(hook.timer(5))
- rcon=socket.connect("localhost",25575)
- end
- rcon=asc(rcon)
- print("[RCON] connected")
- rcon:send("\30\0\0\0\42\0\0\0\3\0\0\0-snip rcon password-\0\0")
- end
- regunload(hook.async(function(y)
- while true do
- if not rcon then
- server.rcon.connect(y)
- end
- local d=rcon:receive(12,y)
- local length=conv(d:sub(1,4))
- local stype=conv(d:sub(9,12))
- d=rcon:receive(length-8,y):sub(1,-3)
- rcon.buffer=""
- --print(table.concat({string.byte(d,1,#d)},","))
- if stype==0 then
- if d~="" then
- for m in d:gmatch("[^\n]+") do
- irc.say(config.chan,"[RCON] "..fmtclr(m))
- end
- end
- end
- end
- end))
- function server.rcon.run(txt)
- rcon:send(uconv(#txt+10).."\42\0\0\0\2\0\0\0"..txt.."\0\0")
- end
- regunload(hook.add("command_rcon",function(dat,txt)
- if not admin.auth(dat,"rcon") then
- return
- end
- server.rcon.run(txt)
- end))
- end
- local secret="-snip-"
- do
- local irc2mc,mc2irc
- do
- local _irc2mc={
- ["$00"]="$f",
- ["$01"]="$0",
- ["$02"]="$1",
- ["$03"]="$2",
- ["$04"]="$c",
- ["$05"]="$4",
- ["$06"]="$5",
- ["$07"]="$6",
- ["$08"]="$e",
- ["$09"]="$a",
- ["$10"]="$3",
- ["$11"]="$b",
- ["$12"]="$9",
- ["$13"]="$d",
- ["$14"]="$8",
- ["$15"]="$7",
- ["\2"]="",
- ["\9"]="",
- ["\19"]="",
- ["\15"]="$r",
- ["\21"]="",
- }
- for k,v in pairs(_irc2mc) do
- _irc2mc[k]=nil
- _irc2mc[k:gsub("%$","\3")]=v:gsub("%$","\194\167")
- end
- local _mc2irc={}
- for k,v in pairs(_irc2mc) do
- if v~="" then
- _mc2irc[v]=k
- end
- end
- function mc2irc(txt)
- for k,v in pairs(_mc2irc) do
- txt=txt:gsub(k,v)
- end
- return txt
- end
- function irc2mc(txt)
- txt=txt:gsub("\3(%d+)",function(c) return "\3"..("0"):rep(2-#c)..c end)
- txt=txt:gsub("\3%d%d,(%d)","")
- txt=txt:gsub("\3%d%d,(%d)","")
- for k,v in pairs(_irc2mc) do
- txt=txt:gsub(k,v)
- end
- txt=txt:gsub("\3%d%d","")
- return txt
- end
- end
- local sv=socket.bind("*",1338)
- sv:settimeout(0)
- addsocket(sv)
- local intr
- regunload(sv)
- local clients={}
- regunload(clients)
- regunload(hook.add("main",function()
- local cl=sv:accept()
- if cl then
- cl:settimeout(0)
- local s=addsocket(cl)
- clients[cl]=s
- end
- for k,v in pairs(clients) do
- local s,e=k:receive()
- if s then
- if k==intr then
- local dat=pt.d(s)
- if not dat then
- irc.say(config.chan,"error parsing "..s.." "..tostring(e))
- elseif dat[1]=="event" then
- hook.queue("mc_event",unpack(dat,2))
- if dat[2]=="chat_message" then
- local px=dat[4]:sub(1,1)
- if px~="!" and px~="/" then
- irc.say(config.chan,"<\00314"..dat[3].."\015> "..mc2irc(dat[4]))
- elseif dat[4]:sub(1,4)=="/me " and #dat[4]>4 then
- irc.say(config.chan,"* \00314"..dat[3].."\015 "..mc2irc(dat[4]:sub(5)))
- end
- if px=="!" then
- local cmd,txt=dat[4]:match("^!(.-) (.+)$")
- cmd=cmd or dat[4]:sub(2)
- hook.queue("mc_command_"..cmd,dat[3],txt)
- hook.queue("mc_command",cmd,dat[3],txt)
- end
- elseif dat[2]=="player_login" then
- irc.say(config.chan,"\00314"..dat[3].."\015 joined the game")
- elseif dat[2]=="player_logout" then
- irc.say(config.chan,"\00314"..dat[3].."\015 left the game")
- end
- end
- elseif s==secret then
- irc.say(config.chan,"Server started :D")
- if intr then
- intr:close()
- end
- intr=k
- end
- elseif e=="closed" then
- if k==intr then
- irc.say(config.chan,"Server stopped :<")
- end
- clients[k]=nil
- remsocket(v)
- end
- end
- end))
- regunload(hook.add("chat",function(dat,txt,ia)
- if dat.chan==config.chan then
- if ia then
- server.interface.send("say","* \194\1677"..dat.nick.."\194\167r "..irc2mc(txt))
- else
- server.interface.send("say","<\194\1677"..dat.nick.."\194\167r> "..irc2mc(txt))
- end
- end
- end))
- regunload(hook.add("nick",function(n1,n2)
- server.interface.send("say","\194\1677"..n1.."\194\167r is now known as \194\1677"..n2)
- end))
- regunload(hook.add("raw",function(raw)
- local ni,rn,hn,cmd,dat=raw:match("^:(.-)!(.-)@(.-) (.+)$")
- if ni and intr then -- matched
- local scmd,txt=cmd:match("^(.-) :(.+)$")
- if txt then
- cmd=scmd
- end
- cmd=pt.split(cmd)
- if ((cmd[1]=="PART" or cmd[1]=="JOIN") and cmd[2]==config.chan) or cmd[1]=="QUIT" then
- local out="\194\1677"..ni.."\194\167r has "..({["PART"]="left",["JOIN"]="joined",["QUIT"]="quit"})[cmd[1]].." chat"
- if txt then
- out=out.." ("..txt..")"
- end
- server.interface.send("say",out)
- end
- end
- end))
- function server.interface.send(...)
- if intr then
- intr:send(pt.c(...).."\n")
- end
- end
- end
- function unload()
- if rcon then
- rcon:close()
- end
- for k,v in pairs(uload) do
- if type(v)=="table" then
- for n,l in pairs(v) do
- if type(l)=="number" then
- n:close()
- remsocket(l)
- end
- end
- elseif type(v)=="function" then
- hook.del(v)
- elseif type(v)=="userdata" then
- v:close()
- remsocket(v)
- end
- end
- server=nil
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement