From 9c30ea322c4abce3f570076d5d54662f74192393 Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Sat, 7 Feb 2015 14:37:11 +0100 Subject: [PATCH] Reverted IRC updated This commit reveses commit 696f96b0f06c59a5fdb99347a69607b451a72c2d except : ./hooks.lua, containing security fixes forbidding people called 'IRC' to use commands --- mods/irc/init.lua | 6 +- mods/irc/irc/.travis.yml | 15 -- mods/irc/irc/README.markdown | 3 +- mods/irc/irc/asyncoperations.lua | 12 +- mods/irc/irc/doc/irc.luadoc | 44 ++---- mods/irc/irc/handlers.lua | 88 ++--------- mods/irc/irc/init.lua | 260 +------------------------------ mods/irc/irc/messages.lua | 77 +++++---- mods/irc/irc/set.lua | 2 +- mods/irc/irc/util.lua | 62 ++++---- 10 files changed, 104 insertions(+), 465 deletions(-) delete mode 100644 mods/irc/irc/.travis.yml diff --git a/mods/irc/init.lua b/mods/irc/init.lua index 173d6adb..c93bcf9f 100755 --- a/mods/irc/init.lua +++ b/mods/irc/init.lua @@ -17,9 +17,9 @@ if not rawget(_G,"jit") and package.config:sub(1, 1) == "/" then package.path = package.path.. ";/usr/share/lua/5.1/?.lua".. ";/usr/share/lua/5.1/?/init.lua" - package.cpath = package.cpath.. --- ";/usr/lib/lua/5.1/?.so" - ";/usr/lib/x86_64-linux-gnu/lua/5.1/?.so" + package.cpath = package.cpath.. + -- ";/usr/lib/lua/5.1/?.so" + ";/usr/lib/x86_64-linux-gnu/lua/5.1/?.so" end irc = { diff --git a/mods/irc/irc/.travis.yml b/mods/irc/irc/.travis.yml deleted file mode 100644 index f0720708..00000000 --- a/mods/irc/irc/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -language: lua - -notifications: - email: false - -env: - global: - - secure: "kFhU+DZjhq/KbDt0DIDWnlskXMa12miNelmhhy30fQGgVIdiibDGKMNGyLahWp8CnPu1DARb5AZWK2TDfARdnURT2pgcsG83M7bYIY6cR647BWjL7oAhJ6CYEzTWJTBjeUjpN/o4vIgfXSDR0c7vboDi7Xz8ilfrBujPL2Oi/og=" - -install: - - sudo apt-get -y update - - sudo apt-get -y install lua5.1 luadoc - -script: - - ./push-luadoc.sh diff --git a/mods/irc/irc/README.markdown b/mods/irc/irc/README.markdown index 8cff1d22..d7c77149 100755 --- a/mods/irc/irc/README.markdown +++ b/mods/irc/irc/README.markdown @@ -1,8 +1,7 @@ -[![Build Status](https://travis-ci.org/JakobOvrum/LuaIRC.svg?branch=master)](https://travis-ci.org/JakobOvrum/LuaIRC) LuaIRC ============ -IRC client library for Lua. +IRC library for Lua. Dependencies ------------- diff --git a/mods/irc/irc/asyncoperations.lua b/mods/irc/irc/asyncoperations.lua index 3160899d..127d170f 100755 --- a/mods/irc/irc/asyncoperations.lua +++ b/mods/irc/irc/asyncoperations.lua @@ -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(msgs.privmsg(verify(target, 3), line)) + self:queue(irc.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(msgs.notice(verify(target, 3), line)) + self:queue(irc.msgs.notice(verify(target, 3), line)) end end function meta:join(channel, key) - self:queue(msgs.join( + self:queue(irc.msgs.join( verify(channel, 3), key and verify(key, 3) or nil)) end function meta:part(channel, reason) channel = verify(channel, 3) - self:queue(msgs.part(channel, reason)) + self:queue(irc.msgs.part(channel, reason)) if self.track_users then self.channels[channel] = nil end @@ -85,8 +85,6 @@ function meta:setMode(t) mode = table.concat{mode, "-", verify(rem, 3)} end - self:queue(msgs.mode(verify(target, 3), mode)) + self:queue(irc.msgs.mode(verify(target, 3), mode)) end -return meta - diff --git a/mods/irc/irc/doc/irc.luadoc b/mods/irc/irc/doc/irc.luadoc index c33a0b19..7bf638e6 100755 --- a/mods/irc/irc/doc/irc.luadoc +++ b/mods/irc/irc/doc/irc.luadoc @@ -1,7 +1,7 @@ --- LuaIRC is a low-level IRC library for Lua. -- All functions raise Lua exceptions on error. -- --- Use new to create a new Connection object.
+-- Use new to create a new IRC object.
-- Example:

-- --require "irc"
@@ -24,12 +24,11 @@ module "irc" ---- Create a new Connection object. Use irc:connect to connect to a server. +--- Create a new IRC object. Use irc:connect to connect to a server. -- @param user Table with fields nick, username and realname. -- The nick field is required. -- --- @return Returns a new Connection object. --- @see Connection +-- @return Returns a new irc object. function new(user) --- Hook a function to an event. @@ -47,10 +46,10 @@ function irc:unhook(name, id) --- Connect irc to an IRC server. -- @param host Host address. -- @param port Server port. [default 6667] -function irc:connect(host, port) +function irc:connect(server, port) -- @param table Table of connection details --- @see ConnectOptions +-- @see Connection function irc:connect(table) --- Disconnect irc from the server. @@ -117,26 +116,16 @@ function irc:handle(msg) function irc:shutdown() --- Table with connection information. --- @name ConnectOptions --- @class table --- @field host Server host name. --- @field port Server port. [defaults to 6667] --- @field timeout Connect timeout. [defaults to 30] --- @field password Server password. --- @field secure Boolean to enable TLS connection, pass a params table (described, [luasec]) to control +-- -- [luasec]: http://www.inf.puc-rio.br/~brunoos/luasec/reference.html - ---- Class representing a connection. -- @name Connection -- @class table --- @field authed Boolean indicating whether the connection has completed registration. --- @field connected Whether the connection is currently connected. --- @field motd The server's message of the day. Can be nil. --- @field nick The current nickname. --- @field realname The real name sent to the server. --- @field username The username/ident sent to the server. --- @field socket Raw socket used by the library. --- @field supports What the server claims to support in it's ISUPPORT message. --- Class representing an IRC message. -- @name Message @@ -156,8 +145,8 @@ function irc:shutdown() --- List of hooks you can use with irc:hook. -- The parameter list describes the parameters passed to the callback function. -- -- * Event also invoked for yourself. -- † Channel passed only when user tracking is enabled diff --git a/mods/irc/irc/handlers.lua b/mods/irc/irc/handlers.lua index f01936f6..6af5d107 100755 --- a/mods/irc/irc/handlers.lua +++ b/mods/irc/irc/handlers.lua @@ -1,72 +1,10 @@ -local util = require("irc.util") -local msgs = require("irc.messages") -local Message = msgs.Message +local irc = require("irc.main") -local handlers = {} +irc.handlers = {} +local handlers = irc.handlers handlers["PING"] = function(conn, msg) - 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 + conn:send(irc.Message({command="PONG", args=msg.args})) end handlers["001"] = function(conn, msg) @@ -78,6 +16,7 @@ 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 @@ -132,13 +71,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(irc.msgs.nick(newnick)) + conn:queue(msgs.nick(newnick)) end -- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons) @@ -147,13 +86,6 @@ 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 @@ -190,7 +122,7 @@ handlers["353"] = function(conn, msg) local users = conn.channels[channel].users for nick in names:gmatch("(%S+)") do - local access, name = util.parseNick(conn, nick) + local access, name = irc.parseNick(conn, nick) users[name] = {access = access} end end @@ -247,7 +179,7 @@ handlers["MODE"] = function(conn, msg) if conn.track_users and target ~= conn.nick then local add = true local argNum = 1 - util.updatePrefixModes(conn) + irc.updatePrefixModes(conn) for c in modes:gmatch(".") do if c == "+" then add = true elseif c == "-" then add = false @@ -273,5 +205,3 @@ handlers["ERROR"] = function(conn, msg) error(msg.args[1], 3) end -return handlers - diff --git a/mods/irc/irc/init.lua b/mods/irc/irc/init.lua index 194b6921..e2eb71e8 100755 --- a/mods/irc/irc/init.lua +++ b/mods/irc/irc/init.lua @@ -1,257 +1,9 @@ -local socket = require("socket") -local util = require("irc.util") -local handlers = require("irc.handlers") -local msgs = require("irc.messages") -local Message = msgs.Message -local meta = {} -meta.__index = meta +local irc = require("irc.main") +require("irc.util") +require("irc.asyncoperations") +require("irc.handlers") +require("irc.messages") - -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; -} +return irc diff --git a/mods/irc/irc/messages.lua b/mods/irc/irc/messages.lua index 48aa6873..2f753edc 100755 --- a/mods/irc/irc/messages.lua +++ b/mods/irc/irc/messages.lua @@ -1,11 +1,12 @@ +local irc = require("irc.main") --- Module table -local m = {} +irc.msgs = {} +local msgs = irc.msgs local msg_meta = {} msg_meta.__index = msg_meta -local function Message(opts) +function irc.Message(opts) opts = opts or {} setmetatable(opts, msg_meta) if opts.raw then @@ -14,8 +15,6 @@ local function Message(opts) return opts end -m.Message = Message - local tag_escapes = { [";"] = "\\:", [" "] = "\\s", @@ -117,91 +116,85 @@ function msg_meta:fromRFC1459(line) end end -function m.privmsg(to, text) - return Message({command="PRIVMSG", args={to, text}}) +function msgs.privmsg(to, text) + return irc.Message({command="PRIVMSG", args={to, text}}) end -function m.notice(to, text) - return Message({command="NOTICE", args={to, text}}) +function msgs.notice(to, text) + return irc.Message({command="NOTICE", args={to, text}}) end -function m.action(to, text) - return Message({command="PRIVMSG", args={to, ("\x01ACTION %s\x01"):format(text)}}) +function msgs.action(to, text) + return irc.Message({command="PRIVMSG", args={to, ("\x01ACTION %s\x01"):format(text)}}) end -function m.ctcp(command, to, args) +function msgs.ctcp(command, to, args) s = "\x01"..command if args then s = ' '..args end s = s..'\x01' - return Message({command="PRIVMSG", args={to, s}}) + return irc.Message({command="PRIVMSG", args={to, s}}) end -function m.kick(channel, target, reason) - return Message({command="KICK", args={channel, target, reason}}) +function msgs.kick(channel, target, reason) + return irc.Message({command="KICK", args={channel, target, reason}}) end -function m.join(channel, key) - return Message({command="JOIN", args={channel, key}}) +function msgs.join(channel, key) + return irc.Message({command="JOIN", args={channel, key}}) end -function m.part(channel, reason) - return Message({command="PART", args={channel, reason}}) +function msgs.part(channel, reason) + return irc.Message({command="PART", args={channel, reason}}) end -function m.quit(reason) - return Message({command="QUIT", args={reason}}) +function msgs.quit(reason) + return irc.Message({command="QUIT", args={reason}}) end -function m.kill(target, reason) - return Message({command="KILL", args={target, reason}}) +function msgs.kill(target, reason) + return irc.Message({command="KILL", args={target, reason}}) end -function m.kline(time, mask, reason, operreason) +function msgs.kline(time, mask, reason, operreason) local args = nil if time then args = {time, mask, reason..'|'..operreason} else args = {mask, reason..'|'..operreason} end - return Message({command="KLINE", args=args}) + return irc.Message({command="KLINE", args=args}) end -function m.whois(nick, server) +function msgs.whois(nick, server) local args = nil if server then args = {server, nick} else args = {nick} end - return Message({command="WHOIS", args=args}) + return irc.Message({command="WHOIS", args=args}) end -function m.topic(channel, text) - return Message({command="TOPIC", args={channel, text}}) +function msgs.topic(channel, text) + return irc.Message({command="TOPIC", args={channel, text}}) end -function m.invite(channel, target) - return Message({command="INVITE", args={channel, target}}) +function msgs.invite(channel, target) + return irc.Message({command="INVITE", args={channel, target}}) end -function m.nick(nick) - return Message({command="NICK", args={nick}}) +function msgs.nick(nick) + return irc.Message({command="NICK", args={nick}}) end -function m.mode(target, modes) +function msgs.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 = util.split(modes) - return Message({command="MODE", args={target, unpack(mt)}}) + local mt = irc.split(modes) + return irc.Message({command="MODE", args={target, unpack(mt)}}) end -function m.cap(cmd, ...) - return Message({command="CAP", args={cmd, ...}}) -end - -return m - diff --git a/mods/irc/irc/set.lua b/mods/irc/irc/set.lua index a4c68818..feec00dc 100755 --- a/mods/irc/irc/set.lua +++ b/mods/irc/irc/set.lua @@ -1,4 +1,4 @@ -local select = require("socket").select +local select = require "socket".select local m = {} local set = {} diff --git a/mods/irc/irc/util.lua b/mods/irc/irc/util.lua index 59168570..92bb76de 100755 --- a/mods/irc/irc/util.lua +++ b/mods/irc/irc/util.lua @@ -1,8 +1,25 @@ +local irc = require("irc.main") --- Module table -local m = {} +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 -function m.updatePrefixModes(conn) +function irc.updatePrefixModes(conn) if conn.prefixmode and conn.modeprefix then return end @@ -22,27 +39,8 @@ function m.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) -m.color = { +irc.color = { black = 1, blue = 2, green = 3, @@ -62,28 +60,28 @@ m.color = { } local colByte = string.char(3) -setmetatable(m.color, {__call = function(_, text, colornum) +setmetatable(irc.color, {__call = function(_, text, colornum) colornum = (type(colornum) == "string" and - assert(color[colornum], "Invalid color '"..colornum.."'") or + assert(irc.color[colornum], "Invalid color '"..colornum.."'") or colornum) return table.concat{colByte, tostring(colornum), text, colByte} end}) local boldByte = string.char(2) -function m.bold(text) +function irc.bold(text) return boldByte..text..boldByte end local underlineByte = string.char(31) -function m.underline(text) +function irc.underline(text) return underlineByte..text..underlineByte end -function m.checkNick(nick) +function irc.checkNick(nick) return nick:find("^[a-zA-Z_%-%[|%]%^{|}`][a-zA-Z0-9_%-%[|%]%^{|}`]*$") ~= nil end -function m.defaultNickGenerator(nick) +function irc.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 @@ -102,13 +100,13 @@ function m.defaultNickGenerator(nick) return nick end -function m.capitalize(text) +function irc.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 m.split(str, sep) +function irc.split(str, sep) local t = {} for s in str:gmatch("%S+") do table.insert(t, s) @@ -116,5 +114,3 @@ function m.split(str, sep) return t end -return m -