Advertisement
killer64

server processor

Dec 11th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.15 KB | None | 0 0
  1. server={rcon={},interface={}}
  2. local uload={}
  3. local function regunload(obj)
  4.     uload[#uload+1]=obj
  5.     return #uload
  6. end
  7. local rcon
  8. do -- rcon
  9.     local conv,uconv
  10.     do
  11.         local str_byte=string.byte
  12.         local str_char=string.char
  13.         local str_len=string.len
  14.         local str_sub=string.sub
  15.         local m_floor=math.floor
  16.         function conv(txt)
  17.             local s={str_byte(txt,1,4)}
  18.             return s[1]+(s[2]*256)+(s[3]*65536)+(s[4]*16777216)
  19.         end
  20.         function uconv(num)
  21.             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))
  22.         end
  23.     end
  24.     local colors={
  25.         ["0"]="01",
  26.         ["1"]="02",
  27.         ["2"]="03",
  28.         ["3"]="10",
  29.         ["4"]="69",
  30.         ["5"]="06",
  31.         ["6"]="73",
  32.         ["7"]="15",
  33.         ["8"]="14",
  34.         ["9"]="12",
  35.         ["a"]="09",
  36.         ["b"]="11",
  37.         ["c"]="04",
  38.         ["d"]="13",
  39.         ["e"]="08",
  40.         ["f"]="17",
  41.     }
  42.     local function fmtclr(txt)
  43.         for k,v in pairs(colors) do
  44.             txt=txt:gsub("\167"..k,"\003"..v)
  45.         end
  46.         return txt
  47.     end
  48.     local buffer=""
  49.     function server.rcon.connect(y)
  50.         rcon=socket.connect("localhost",25575)
  51.         while not rcon do
  52.             print("[RCON] connecting")
  53.             y(hook.timer(5))
  54.             rcon=socket.connect("localhost",25575)
  55.         end
  56.         rcon=asc(rcon)
  57.         print("[RCON] connected")
  58.         rcon:send("\30\0\0\0\42\0\0\0\3\0\0\0-snip rcon password-\0\0")
  59.     end
  60.     regunload(hook.async(function(y)
  61.         while true do
  62.             if not rcon then
  63.                 server.rcon.connect(y)
  64.             end
  65.             local d=rcon:receive(12,y)
  66.             local length=conv(d:sub(1,4))
  67.             local stype=conv(d:sub(9,12))
  68.             d=rcon:receive(length-8,y):sub(1,-3)
  69.             rcon.buffer=""
  70.             --print(table.concat({string.byte(d,1,#d)},","))
  71.             if stype==0 then
  72.                 if d~="" then
  73.                     for m in d:gmatch("[^\n]+") do
  74.                         irc.say(config.chan,"[RCON] "..fmtclr(m))
  75.                     end
  76.                 end
  77.             end
  78.         end
  79.     end))
  80.     function server.rcon.run(txt)
  81.         rcon:send(uconv(#txt+10).."\42\0\0\0\2\0\0\0"..txt.."\0\0")
  82.     end
  83.     regunload(hook.add("command_rcon",function(dat,txt)
  84.         if not admin.auth(dat,"rcon") then
  85.             return
  86.         end
  87.         server.rcon.run(txt)
  88.     end))
  89. end
  90. local secret="-snip-"
  91. do
  92.     local irc2mc,mc2irc
  93.     do
  94.         local _irc2mc={
  95.             ["$00"]="$f",
  96.             ["$01"]="$0",
  97.             ["$02"]="$1",
  98.             ["$03"]="$2",
  99.             ["$04"]="$c",
  100.             ["$05"]="$4",
  101.             ["$06"]="$5",
  102.             ["$07"]="$6",
  103.             ["$08"]="$e",
  104.             ["$09"]="$a",
  105.             ["$10"]="$3",
  106.             ["$11"]="$b",
  107.             ["$12"]="$9",
  108.             ["$13"]="$d",
  109.             ["$14"]="$8",
  110.             ["$15"]="$7",
  111.             ["\2"]="",
  112.             ["\9"]="",
  113.             ["\19"]="",
  114.             ["\15"]="$r",
  115.             ["\21"]="",
  116.         }
  117.         for k,v in pairs(_irc2mc) do
  118.             _irc2mc[k]=nil
  119.             _irc2mc[k:gsub("%$","\3")]=v:gsub("%$","\194\167")
  120.         end
  121.         local _mc2irc={}
  122.         for k,v in pairs(_irc2mc) do
  123.             if v~="" then
  124.                 _mc2irc[v]=k
  125.             end
  126.         end
  127.         function mc2irc(txt)
  128.             for k,v in pairs(_mc2irc) do
  129.                 txt=txt:gsub(k,v)
  130.             end
  131.             return txt
  132.         end
  133.         function irc2mc(txt)
  134.             txt=txt:gsub("\3(%d+)",function(c) return "\3"..("0"):rep(2-#c)..c end)
  135.             txt=txt:gsub("\3%d%d,(%d)","")
  136.             txt=txt:gsub("\3%d%d,(%d)","")
  137.             for k,v in pairs(_irc2mc) do
  138.                 txt=txt:gsub(k,v)
  139.             end
  140.             txt=txt:gsub("\3%d%d","")
  141.             return txt
  142.         end
  143.     end
  144.     local sv=socket.bind("*",1338)
  145.     sv:settimeout(0)
  146.     addsocket(sv)
  147.     local intr
  148.     regunload(sv)
  149.     local clients={}
  150.     regunload(clients)
  151.     regunload(hook.add("main",function()
  152.         local cl=sv:accept()
  153.         if cl then
  154.             cl:settimeout(0)
  155.             local s=addsocket(cl)
  156.             clients[cl]=s
  157.         end
  158.         for k,v in pairs(clients) do
  159.             local s,e=k:receive()
  160.             if s then
  161.                 if k==intr then
  162.                     local dat=pt.d(s)
  163.                     if not dat then
  164.                         irc.say(config.chan,"error parsing "..s.." "..tostring(e))
  165.                     elseif dat[1]=="event" then
  166.                         hook.queue("mc_event",unpack(dat,2))
  167.                         if dat[2]=="chat_message" then
  168.                             local px=dat[4]:sub(1,1)
  169.                             if px~="!" and px~="/" then
  170.                                 irc.say(config.chan,"<\00314"..dat[3].."\015> "..mc2irc(dat[4]))
  171.                             elseif dat[4]:sub(1,4)=="/me " and #dat[4]>4 then
  172.                                 irc.say(config.chan,"* \00314"..dat[3].."\015 "..mc2irc(dat[4]:sub(5)))
  173.                             end
  174.                             if px=="!" then
  175.                                 local cmd,txt=dat[4]:match("^!(.-) (.+)$")
  176.                                 cmd=cmd or dat[4]:sub(2)
  177.                                 hook.queue("mc_command_"..cmd,dat[3],txt)
  178.                                 hook.queue("mc_command",cmd,dat[3],txt)
  179.                             end
  180.                         elseif dat[2]=="player_login" then
  181.                             irc.say(config.chan,"\00314"..dat[3].."\015 joined the game")
  182.                         elseif dat[2]=="player_logout" then
  183.                             irc.say(config.chan,"\00314"..dat[3].."\015 left the game")
  184.                         end
  185.                     end
  186.                 elseif s==secret then
  187.                     irc.say(config.chan,"Server started :D")
  188.                     if intr then
  189.                         intr:close()
  190.                     end
  191.                     intr=k
  192.                 end
  193.             elseif e=="closed" then
  194.                 if k==intr then
  195.                     irc.say(config.chan,"Server stopped :<")
  196.                 end
  197.                 clients[k]=nil
  198.                 remsocket(v)
  199.             end
  200.         end
  201.     end))
  202.     regunload(hook.add("chat",function(dat,txt,ia)
  203.         if dat.chan==config.chan then
  204.             if ia then
  205.                 server.interface.send("say","* \194\1677"..dat.nick.."\194\167r "..irc2mc(txt))
  206.             else
  207.                 server.interface.send("say","<\194\1677"..dat.nick.."\194\167r> "..irc2mc(txt))
  208.             end
  209.         end
  210.     end))
  211.     regunload(hook.add("nick",function(n1,n2)
  212.         server.interface.send("say","\194\1677"..n1.."\194\167r is now known as \194\1677"..n2)
  213.     end))
  214.     regunload(hook.add("raw",function(raw)
  215.         local ni,rn,hn,cmd,dat=raw:match("^:(.-)!(.-)@(.-) (.+)$")
  216.         if ni and intr then -- matched
  217.             local scmd,txt=cmd:match("^(.-) :(.+)$")
  218.             if txt then
  219.                 cmd=scmd
  220.             end
  221.             cmd=pt.split(cmd)
  222.             if ((cmd[1]=="PART" or cmd[1]=="JOIN") and cmd[2]==config.chan) or cmd[1]=="QUIT" then
  223.                 local out="\194\1677"..ni.."\194\167r has "..({["PART"]="left",["JOIN"]="joined",["QUIT"]="quit"})[cmd[1]].." chat"
  224.                 if txt then
  225.                     out=out.." ("..txt..")"
  226.                 end
  227.                 server.interface.send("say",out)
  228.             end
  229.         end
  230.     end))
  231.     function server.interface.send(...)
  232.         if intr then
  233.             intr:send(pt.c(...).."\n")
  234.         end
  235.     end
  236. end
  237. function unload()
  238.     if rcon then
  239.         rcon:close()
  240.     end
  241.     for k,v in pairs(uload) do
  242.         if type(v)=="table" then
  243.             for n,l in pairs(v) do
  244.                 if type(l)=="number" then
  245.                     n:close()
  246.                     remsocket(l)
  247.                 end
  248.             end
  249.         elseif type(v)=="function" then
  250.             hook.del(v)
  251.         elseif type(v)=="userdata" then
  252.             v:close()
  253.             remsocket(v)
  254.         end
  255.     end
  256.     server=nil
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement