Skip to content

Instantly share code, notes, and snippets.

@pingbird
Created March 10, 2014 22:59
Show Gist options
  • Select an option

  • Save pingbird/9476234 to your computer and use it in GitHub Desktop.

Select an option

Save pingbird/9476234 to your computer and use it in GitHub Desktop.
fail ircd
local socket=require("socket")
local sv=socket.bind("*",1338)
local function nxt(tbl)
local n=1
while tbl[n] do
n=n+1
end
return n
end
local function tpairs(tbl)
local i={}
local c=1
for k,v in pairs(tbl) do
i[c]=k
c=c+1
end
c=0
return function()
c=c+1
return i[c],tbl[i[c]]
end
end
local cli={}
local sel={sv}
local ndat={
nick={},
}
local function send(cl,txt)
print(cli[cl].ip.."<"..txt)
cl:send(":"..txt.."\n")
end
local function close(cl)
local nick=cli[cl].nick
if nick then
for scl,sdat in tpairs(cli) do
if not sdat.init then
send(scl,nick.."!potato@potato QUIT :No more potato :<")
end
end
ndat.nick[nick]=nil
end
cli[cl]=nil
for k,v in tpairs(sel) do
if v==cl then
v:close()
sel[k]=nil
break
end
end
end
sv:settimeout(0)
local function validnick(txt)
return txt:gsub("[^%^%w_%-%[%]%(%)|]",""):sub(1,25)
end
local function join(cl)
local snick=cli[cl].nick
for scl,sdat in tpairs(cli) do
send(scl,snick.."!potato@potato JOIN #potato")
send(scl,"potato MODE #potato +o "..snick)
end
local o=""
for snick,scl in pairs(ndat.nick) do
o=o.."@"..snick.." "
end
send(cl,"potato 353 "..snick.." = #potato :"..o:sub(1,-2))
send(cl,"potato 366 "..snick.." #potato :End of /NAMES list.")
end
while true do
local cl=sv:accept()
if cl then
print("got client "..cl:getpeername())
cl:settimeout(0)
cli[cl]={init=true,ip=cl:getpeername()..":"..nxt(sel)}
sel[nxt(sel)]=cl
end
for cl,dat in tpairs(cli) do
local s,e=cl:receive()
if s then
print(dat.ip..">"..s)
s:gsub("^PING(.*)",function(pong)
cl:send("PONG"..pong.."\n")
end)
s:gsub("^NICK :?(.*)",function(nick)
if dat.init then
send(cl,nick.."!potato@potato MODE "..nick.." :+i")
local snick=validnick(nick)
while ndat.nick[snick] or snick=="" do
snick="Potato"..math.random(100000,999999)
end
if ndat.nick[nick] or validnick(nick)=="" then
send(cl,nick.."!potato@potato NICK :"..snick)
end
ndat.nick[snick]=cl
dat.nick=snick
join(cl)
dat.init=false
else
nick=validnick(nick)
if not ndat.nick[nick] and nick~="" then
ndat.nick[dat.nick]=nil
ndat.nick[nick]=cl
for scl,sdat in tpairs(cli) do
send(scl,dat.nick.."!potato@potato NICK :"..nick)
end
dat.nick=nick
end
end
end)
if not dat.init then
s:gsub("^MODE #potato (.+)",function(txt)
for scl,sdat in tpairs(cli) do
if not sdat.init then
send(scl,dat.nick.." MODE #potato "..txt)
end
end
end)
s:gsub("^JOIN #potato$",function()
join(cl)
end)
s:gsub("^TOPIC #potato (.+)",function(txt)
for scl,sdat in tpairs(cli) do
if not sdat.init then
send(scl,dat.nick.." TOPIC #potato "..txt)
end
end
end)
s:gsub("^KICK #potato (.+)",function(txt)
for scl,sdat in tpairs(cli) do
if not sdat.init then
send(scl,dat.nick.." KICK #potato "..txt)
end
end
end)
s:gsub("^PRIVMSG #potato :(.*)",function(txt)
for scl,sdat in tpairs(cli) do
if scl~=cl and not sdat.init then
send(scl,dat.nick.."!potato@potato PRIVMSG #potato :"..txt)
end
end
end)
s:gsub("^a (.+)",function(txt)
for scl,sdat in tpairs(cli) do
pcall(send,scl,txt)
end
end)
end
elseif e=="closed" then
close(cl)
end
end
socket.select(sel)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment