1
0
mirror of https://github.com/sys4-fr/server-nalc.git synced 2024-09-29 07:50:35 +02:00

New IRC version

This commit is contained in:
LeMagnesium 2015-03-10 22:23:06 +01:00
parent a5a932c8ed
commit 050f8f115f
22 changed files with 422 additions and 92 deletions

0
mods/irc/.gitignore vendored Executable file → Normal file
View File

View File

@ -1,3 +0,0 @@
[submodule "src/LuaIRC"]
path = irc
url = https://github.com/ShadowNinja/LuaIRC.git

0
mods/irc/API.md Executable file → Normal file
View File

0
mods/irc/LICENSE.txt Executable file → Normal file
View File

0
mods/irc/README.md Executable file → Normal file
View File

0
mods/irc/botcmds.lua Executable file → Normal file
View File

0
mods/irc/callback.lua Executable file → Normal file
View File

0
mods/irc/chatcmds.lua Executable file → Normal file
View File

0
mods/irc/config.lua Executable file → Normal file
View File

2
mods/irc/hooks.lua Executable file → Normal file
View File

@ -238,7 +238,7 @@ function irc.hooks.preregister(conn)
conn:send("CAP REQ sasl")
conn:send("AUTHENTICATE PLAIN")
conn:send("AUTHENTICATE "..authString)
--LuaIRC will send CAP END
conn:send("CAP END")
end

View File

@ -9,6 +9,7 @@ package.path =
-- For LuaIRC to find its files
..modpath.."/?.lua;"
..package.path
..";/usr/lib/x86_64-linux-gnu/lua/5.1/socket/?.so"
-- The build of Lua that Minetest comes with only looks for libraries under
-- /usr/local/share and /usr/local/lib but LuaSocket is often installed under

0
mods/irc/irc/.gitignore vendored Executable file → Normal file
View File

0
mods/irc/irc/LICENSE.txt Executable file → Normal file
View File

3
mods/irc/irc/README.markdown Executable file → Normal file
View File

@ -1,7 +1,8 @@
[![Build Status](https://travis-ci.org/JakobOvrum/LuaIRC.svg?branch=master)](https://travis-ci.org/JakobOvrum/LuaIRC)
LuaIRC
============
IRC library for Lua.
IRC client library for Lua.
Dependencies
-------------

16
mods/irc/irc/asyncoperations.lua Executable file → Normal file
View File

@ -1,6 +1,6 @@
local irc = require("irc.main")
local msgs = require("irc.messages")
local meta = irc.meta
local meta = {}
function meta:send(msg, ...)
if type(msg) == "table" then
@ -36,26 +36,26 @@ end
function meta:sendChat(target, msg)
-- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do
self:queue(irc.msgs.privmsg(verify(target, 3), line))
self:queue(msgs.privmsg(verify(target, 3), line))
end
end
function meta:sendNotice(target, msg)
-- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do
self:queue(irc.msgs.notice(verify(target, 3), line))
self:queue(msgs.notice(verify(target, 3), line))
end
end
function meta:join(channel, key)
self:queue(irc.msgs.join(
self:queue(msgs.join(
verify(channel, 3),
key and verify(key, 3) or nil))
end
function meta:part(channel, reason)
channel = verify(channel, 3)
self:queue(irc.msgs.part(channel, reason))
self:queue(msgs.part(channel, reason))
if self.track_users then
self.channels[channel] = nil
end
@ -85,6 +85,8 @@ function meta:setMode(t)
mode = table.concat{mode, "-", verify(rem, 3)}
end
self:queue(irc.msgs.mode(verify(target, 3), mode))
self:queue(msgs.mode(verify(target, 3), mode))
end
return meta

88
mods/irc/irc/handlers.lua Executable file → Normal file
View File

@ -1,10 +1,72 @@
local irc = require("irc.main")
local util = require("irc.util")
local msgs = require("irc.messages")
local Message = msgs.Message
irc.handlers = {}
local handlers = irc.handlers
local handlers = {}
handlers["PING"] = function(conn, msg)
conn:send(irc.Message({command="PONG", args=msg.args}))
conn:send(Message({command="PONG", args=msg.args}))
end
local function requestWanted(conn, wanted)
local args = {}
for cap, value in pairs(wanted) do
if type(value) == "string" then
cap = cap .. "=" .. value
end
if not conn.capabilities[cap] then
table.insert(args, cap)
end
end
conn:queue(Message({
command = "CAP",
args = {"REQ", table.concat(args, " ")}
})
)
end
handlers["CAP"] = function(conn, msg)
local cmd = msg.args[2]
if not cmd then
return
end
if cmd == "LS" then
local list = msg.args[3]
local last = false
if list == "*" then
list = msg.args[4]
else
last = true
end
local avail = conn.availableCapabilities
local wanted = conn.wantedCapabilities
for item in list:gmatch("(%S+)") do
local eq = item:find("=", 1, true)
local k, v
if eq then
k, v = item:sub(1, eq - 1), item:sub(eq + 1)
else
k, v = item, true
end
if not avail[k] or avail[k] ~= v then
wanted[k] = conn:invoke("OnCapabilityAvailable", k, v)
end
avail[k] = v
end
if last then
if next(wanted) then
requestWanted(conn, wanted)
end
conn:invoke("OnCapabilityList", conn.availableCapabilities)
end
elseif cmd == "ACK" then
for item in msg.args[3]:gmatch("(%S+)") do
local enabled = (item:sub(1, 1) ~= "-")
local name = enabled and item or item:sub(2)
conn:invoke("OnCapabilitySet", name, enabled)
conn.capabilities[name] = enabled
end
end
end
handlers["001"] = function(conn, msg)
@ -16,7 +78,6 @@ handlers["PRIVMSG"] = function(conn, msg)
conn:invoke("OnChat", msg.user, msg.args[1], msg.args[2])
end
handlers["NOTICE"] = function(conn, msg)
conn:invoke("OnNotice", msg.user, msg.args[1], msg.args[2])
end
@ -71,13 +132,13 @@ handlers["NICK"] = function(conn, msg)
conn:invoke("NickChange", msg.user, newNick)
end
if msg.user.nick == conn.nick then
conn.nick = newnick
conn.nick = newNick
end
end
local function needNewNick(conn, msg)
local newnick = conn.nickGenerator(msg.args[2])
conn:queue(msgs.nick(newnick))
conn:queue(irc.msgs.nick(newnick))
end
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)
@ -86,6 +147,13 @@ handlers["432"] = needNewNick
-- ERR_NICKNAMEINUSE
handlers["433"] = needNewNick
-- ERR_UNAVAILRESOURCE
handlers["437"] = function(conn, msg)
if not conn.authed then
needNewNick(conn, msg)
end
end
-- RPL_ISUPPORT
handlers["005"] = function(conn, msg)
local arglen = #msg.args
@ -122,7 +190,7 @@ handlers["353"] = function(conn, msg)
local users = conn.channels[channel].users
for nick in names:gmatch("(%S+)") do
local access, name = irc.parseNick(conn, nick)
local access, name = util.parseNick(conn, nick)
users[name] = {access = access}
end
end
@ -179,7 +247,7 @@ handlers["MODE"] = function(conn, msg)
if conn.track_users and target ~= conn.nick then
local add = true
local argNum = 1
irc.updatePrefixModes(conn)
util.updatePrefixModes(conn)
for c in modes:gmatch(".") do
if c == "+" then add = true
elseif c == "-" then add = false
@ -205,3 +273,5 @@ handlers["ERROR"] = function(conn, msg)
error(msg.args[1], 3)
end
return handlers

260
mods/irc/irc/init.lua Executable file → Normal file
View File

@ -1,9 +1,257 @@
local socket = require("socket")
local util = require("irc.util")
local handlers = require("irc.handlers")
local msgs = require("irc.messages")
local Message = msgs.Message
local irc = require("irc.main")
require("irc.util")
require("irc.asyncoperations")
require("irc.handlers")
require("irc.messages")
local meta = {}
meta.__index = meta
return irc
for k, v in pairs(require("irc.asyncoperations")) do
meta[k] = v
end
local meta_preconnect = {}
function meta_preconnect.__index(o, k)
local v = rawget(meta_preconnect, k)
if v == nil and meta[k] ~= nil then
error(("field '%s' is not accessible before connecting"):format(k), 2)
end
return v
end
meta.connected = true
meta_preconnect.connected = false
function new(data)
local o = {
nick = assert(data.nick, "Field 'nick' is required");
username = data.username or "lua";
realname = data.realname or "Lua owns";
nickGenerator = data.nickGenerator or util.defaultNickGenerator;
hooks = {};
track_users = true;
supports = {};
messageQueue = {};
lastThought = 0;
recentMessages = 0;
availableCapabilities = {};
wantedCapabilities = {};
capabilities = {};
}
assert(util.checkNick(o.nick), "Erroneous nickname passed to irc.new")
return setmetatable(o, meta_preconnect)
end
function meta:hook(name, id, f)
f = f or id
self.hooks[name] = self.hooks[name] or {}
self.hooks[name][id] = f
return id or f
end
meta_preconnect.hook = meta.hook
function meta:unhook(name, id)
local hooks = self.hooks[name]
assert(hooks, "no hooks exist for this event")
assert(hooks[id], "hook ID not found")
hooks[id] = nil
end
meta_preconnect.unhook = meta.unhook
function meta:invoke(name, ...)
local hooks = self.hooks[name]
if hooks then
for id, f in pairs(hooks) do
local ret = f(...)
if ret then
return ret
end
end
end
end
function meta_preconnect:connect(_host, _port)
local host, port, password, secure, timeout
if type(_host) == "table" then
host = _host.host
port = _host.port
timeout = _host.timeout
password = _host.password
secure = _host.secure
else
host = _host
port = _port
end
host = host or error("host name required to connect", 2)
port = port or 6667
local s = socket.tcp()
s:settimeout(timeout or 30)
assert(s:connect(host, port))
if secure then
local work, ssl = pcall(require, "ssl")
if not work then
error("LuaSec required for secure connections", 2)
end
local params
if type(secure) == "table" then
params = secure
else
params = {mode = "client", protocol = "tlsv1"}
end
s = ssl.wrap(s, params)
local success, errmsg = s:dohandshake()
if not success then
error(("could not make secure connection: %s"):format(errmsg), 2)
end
end
self.socket = s
setmetatable(self, meta)
self:invoke("PreRegister", self)
if password then
self:queue(Message({command="PASS", args={password}}))
end
self:queue(msgs.nick(self.nick))
self:queue(Message({command="USER", args={self.username, "0", "*", self.realname}}))
self.channels = {}
s:settimeout(0)
repeat
self:think()
socket.sleep(0.1)
until self.authed
end
function meta:disconnect(message)
message = message or "Bye!"
self:invoke("OnDisconnect", message, false)
self:send(msgs.quit(message))
self:shutdown()
end
function meta:shutdown()
self.socket:close()
setmetatable(self, meta_preconnect)
end
local function getline(self, errlevel)
local line, err = self.socket:receive("*l")
if not line and err ~= "timeout" and err ~= "wantread" then
self:invoke("OnDisconnect", err, true)
self:shutdown()
error(err, errlevel)
end
return line
end
function meta:think()
while true do
local line = getline(self, 3)
if line and #line > 0 then
if not self:invoke("OnRaw", line) then
self:handle(Message({raw=line}))
end
else
break
end
end
-- Handle outgoing message queue
local diff = socket.gettime() - self.lastThought
self.recentMessages = self.recentMessages - (diff * 2)
if self.recentMessages < 0 then
self.recentMessages = 0
end
for i = 1, #self.messageQueue do
if self.recentMessages > 4 then
break
end
self:send(table.remove(self.messageQueue, 1))
self.recentMessages = self.recentMessages + 1
end
self.lastThought = socket.gettime()
end
function meta:handle(msg)
local handler = handlers[msg.command]
if handler then
handler(self, msg)
end
self:invoke("Do" .. util.capitalize(msg.command), msg)
end
local whoisHandlers = {
["311"] = "userinfo";
["312"] = "node";
["319"] = "channels";
["330"] = "account"; -- Freenode
["307"] = "registered"; -- Unreal
}
function meta:whois(nick)
self:send(msgs.whois(nick))
local result = {}
while true do
local line = getline(self, 3)
if line then
local msg = Message({raw=line})
local handler = whoisHandlers[msg.command]
if handler then
result[handler] = msg.args
elseif msg.command == "318" then
break
else
self:handle(msg)
end
end
end
if result.account then
result.account = result.account[3]
elseif result.registered then
result.account = result.registered[2]
end
return result
end
function meta:topic(channel)
self:queue(msgs.topic(channel))
end
return {
new = new;
Message = Message;
msgs = msgs;
color = util.color;
bold = util.bold;
underline = util.underline;
}

77
mods/irc/irc/messages.lua Executable file → Normal file
View File

@ -1,12 +1,11 @@
local irc = require("irc.main")
irc.msgs = {}
local msgs = irc.msgs
-- Module table
local m = {}
local msg_meta = {}
msg_meta.__index = msg_meta
function irc.Message(opts)
local function Message(opts)
opts = opts or {}
setmetatable(opts, msg_meta)
if opts.raw then
@ -15,6 +14,8 @@ function irc.Message(opts)
return opts
end
m.Message = Message
local tag_escapes = {
[";"] = "\\:",
[" "] = "\\s",
@ -116,85 +117,91 @@ function msg_meta:fromRFC1459(line)
end
end
function msgs.privmsg(to, text)
return irc.Message({command="PRIVMSG", args={to, text}})
function m.privmsg(to, text)
return Message({command="PRIVMSG", args={to, text}})
end
function msgs.notice(to, text)
return irc.Message({command="NOTICE", args={to, text}})
function m.notice(to, text)
return Message({command="NOTICE", args={to, text}})
end
function msgs.action(to, text)
return irc.Message({command="PRIVMSG", args={to, ("\x01ACTION %s\x01"):format(text)}})
function m.action(to, text)
return Message({command="PRIVMSG", args={to, ("\x01ACTION %s\x01"):format(text)}})
end
function msgs.ctcp(command, to, args)
function m.ctcp(command, to, args)
s = "\x01"..command
if args then
s = ' '..args
end
s = s..'\x01'
return irc.Message({command="PRIVMSG", args={to, s}})
return Message({command="PRIVMSG", args={to, s}})
end
function msgs.kick(channel, target, reason)
return irc.Message({command="KICK", args={channel, target, reason}})
function m.kick(channel, target, reason)
return Message({command="KICK", args={channel, target, reason}})
end
function msgs.join(channel, key)
return irc.Message({command="JOIN", args={channel, key}})
function m.join(channel, key)
return Message({command="JOIN", args={channel, key}})
end
function msgs.part(channel, reason)
return irc.Message({command="PART", args={channel, reason}})
function m.part(channel, reason)
return Message({command="PART", args={channel, reason}})
end
function msgs.quit(reason)
return irc.Message({command="QUIT", args={reason}})
function m.quit(reason)
return Message({command="QUIT", args={reason}})
end
function msgs.kill(target, reason)
return irc.Message({command="KILL", args={target, reason}})
function m.kill(target, reason)
return Message({command="KILL", args={target, reason}})
end
function msgs.kline(time, mask, reason, operreason)
function m.kline(time, mask, reason, operreason)
local args = nil
if time then
args = {time, mask, reason..'|'..operreason}
else
args = {mask, reason..'|'..operreason}
end
return irc.Message({command="KLINE", args=args})
return Message({command="KLINE", args=args})
end
function msgs.whois(nick, server)
function m.whois(nick, server)
local args = nil
if server then
args = {server, nick}
else
args = {nick}
end
return irc.Message({command="WHOIS", args=args})
return Message({command="WHOIS", args=args})
end
function msgs.topic(channel, text)
return irc.Message({command="TOPIC", args={channel, text}})
function m.topic(channel, text)
return Message({command="TOPIC", args={channel, text}})
end
function msgs.invite(channel, target)
return irc.Message({command="INVITE", args={channel, target}})
function m.invite(channel, target)
return Message({command="INVITE", args={channel, target}})
end
function msgs.nick(nick)
return irc.Message({command="NICK", args={nick}})
function m.nick(nick)
return Message({command="NICK", args={nick}})
end
function msgs.mode(target, modes)
function m.mode(target, modes)
-- We have to split the modes parameter because the mode string and
-- each parameter are seperate arguments (The first command is incorrect)
-- MODE foo :+ov Nick1 Nick2
-- MODE foo +ov Nick1 Nick2
local mt = irc.split(modes)
return irc.Message({command="MODE", args={target, unpack(mt)}})
local mt = util.split(modes)
return Message({command="MODE", args={target, unpack(mt)}})
end
function m.cap(cmd, ...)
return Message({command="CAP", args={cmd, ...}})
end
return m

2
mods/irc/irc/set.lua Executable file → Normal file
View File

@ -1,4 +1,4 @@
local select = require "socket".select
local select = require("socket").select
local m = {}
local set = {}

62
mods/irc/irc/util.lua Executable file → Normal file
View File

@ -1,25 +1,8 @@
local irc = require("irc.main")
function irc.parseNick(conn, nick)
local access = {}
irc.updatePrefixModes(conn)
local namestart = 1
for i = 1, #nick - 1 do
local c = nick:sub(i, i)
if conn.prefixmode[c] then
access[conn.prefixmode[c]] = true
else
namestart = i
break
end
end
access.op = access.o
access.voice = access.v
local name = nick:sub(namestart)
return access, name
end
-- Module table
local m = {}
function irc.updatePrefixModes(conn)
function m.updatePrefixModes(conn)
if conn.prefixmode and conn.modeprefix then
return
end
@ -39,8 +22,27 @@ function irc.updatePrefixModes(conn)
end
end
function m.parseNick(conn, nick)
local access = {}
m.updatePrefixModes(conn)
local namestart = 1
for i = 1, #nick - 1 do
local c = nick:sub(i, i)
if conn.prefixmode[c] then
access[conn.prefixmode[c]] = true
else
namestart = i
break
end
end
access.op = access.o
access.voice = access.v
local name = nick:sub(namestart)
return access, name
end
-- mIRC markup scheme (de-facto standard)
irc.color = {
m.color = {
black = 1,
blue = 2,
green = 3,
@ -60,28 +62,28 @@ irc.color = {
}
local colByte = string.char(3)
setmetatable(irc.color, {__call = function(_, text, colornum)
setmetatable(m.color, {__call = function(_, text, colornum)
colornum = (type(colornum) == "string" and
assert(irc.color[colornum], "Invalid color '"..colornum.."'") or
assert(color[colornum], "Invalid color '"..colornum.."'") or
colornum)
return table.concat{colByte, tostring(colornum), text, colByte}
end})
local boldByte = string.char(2)
function irc.bold(text)
function m.bold(text)
return boldByte..text..boldByte
end
local underlineByte = string.char(31)
function irc.underline(text)
function m.underline(text)
return underlineByte..text..underlineByte
end
function irc.checkNick(nick)
function m.checkNick(nick)
return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil
end
function irc.defaultNickGenerator(nick)
function m.defaultNickGenerator(nick)
-- LuaBot -> LuaCot -> LuaCou -> ...
-- We change a random character rather than appending to the
-- nickname as otherwise the new nick could exceed the ircd's
@ -100,13 +102,13 @@ function irc.defaultNickGenerator(nick)
return nick
end
function irc.capitalize(text)
function m.capitalize(text)
-- Converts first character to upercase and the rest to lowercase.
-- "PING" -> "Ping" | "hello" -> "Hello" | "123" -> "123"
return text:sub(1, 1):upper()..text:sub(2):lower()
end
function irc.split(str, sep)
function m.split(str, sep)
local t = {}
for s in str:gmatch("%S+") do
table.insert(t, s)
@ -114,3 +116,5 @@ function irc.split(str, sep)
return t
end
return m

0
mods/irc/messages.lua Executable file → Normal file
View File

0
mods/irc/player_part.lua Executable file → Normal file
View File