From b9d45b216419c5909a6df2ad7f7133615efabbfc Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Wed, 4 Feb 2015 22:33:14 +0100 Subject: [PATCH] Updated IRC Mod - (Finally) Updated the IRC mod : new dependencies, new LuaIRC module --- mods/irc/chatcmds.lua | 4 + mods/irc/hooks.lua | 10 +- mods/irc/init.lua | 5 +- mods/irc/irc/.gitignore | 2 + 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/main.lua | 241 ---------------------------- mods/irc/irc/messages.lua | 77 ++++----- mods/irc/irc/push-luadoc.sh | 19 +++ mods/irc/irc/set.lua | 2 +- mods/irc/irc/util.lua | 62 ++++---- 15 files changed, 497 insertions(+), 347 deletions(-) create mode 100644 mods/irc/irc/.travis.yml delete mode 100755 mods/irc/irc/main.lua create mode 100755 mods/irc/irc/push-luadoc.sh diff --git a/mods/irc/chatcmds.lua b/mods/irc/chatcmds.lua index 03599e9c..b8ac4665 100755 --- a/mods/irc/chatcmds.lua +++ b/mods/irc/chatcmds.lua @@ -45,6 +45,10 @@ minetest.register_chatcommand("irc_names", { params = "", description = "List the users in IRC.", func = function(name, params) + if not irc.connected then + minetest.chat_send_player(name, "Not connected to IRC. Use /irc_connect to connect.") + return + end local users = { } for k, v in pairs(irc.conn.channels[irc.config.channel].users) do table.insert(users, k) diff --git a/mods/irc/hooks.lua b/mods/irc/hooks.lua index 975c89cb..6c8e2438 100755 --- a/mods/irc/hooks.lua +++ b/mods/irc/hooks.lua @@ -104,6 +104,14 @@ end function irc.hooks.channelChat(msg) local text = normalize(msg.args[2]) + irc:check_botcmd(msg) + + -- Don't let a user impersonate someone else by using the nick "IRC" + if msg.user.nick == "IRC" then + irc:sendLocal(" "..text) + return + end + -- Support multiple servers in a channel better by converting: -- " message" into " message" -- " *** player joined/left the game" into "*** player joined/left server" @@ -117,8 +125,6 @@ function irc.hooks.channelChat(msg) local foundaction, _, actionnick, actionmessage = text:find("^%* ([^%s]+) (.*)$") - irc:check_botcmd(msg) - if text:sub(1, 5) == "[off]" then return elseif foundchat then diff --git a/mods/irc/init.lua b/mods/irc/init.lua index c93bcf9f..ec06422f 100755 --- a/mods/irc/init.lua +++ b/mods/irc/init.lua @@ -17,9 +17,8 @@ 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" end irc = { diff --git a/mods/irc/irc/.gitignore b/mods/irc/irc/.gitignore index b25c15b8..3ad5d87a 100755 --- a/mods/irc/irc/.gitignore +++ b/mods/irc/irc/.gitignore @@ -1 +1,3 @@ +/gh-pages/ *~ + diff --git a/mods/irc/irc/.travis.yml b/mods/irc/irc/.travis.yml new file mode 100644 index 00000000..f0720708 --- /dev/null +++ b/mods/irc/irc/.travis.yml @@ -0,0 +1,15 @@ +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 d7c77149..8cff1d22 100755 --- a/mods/irc/irc/README.markdown +++ b/mods/irc/irc/README.markdown @@ -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 ------------- diff --git a/mods/irc/irc/asyncoperations.lua b/mods/irc/irc/asyncoperations.lua index 127d170f..3160899d 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(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 + diff --git a/mods/irc/irc/doc/irc.luadoc b/mods/irc/irc/doc/irc.luadoc index 7bf638e6..c33a0b19 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 IRC object.
+-- Use new to create a new Connection object.
-- Example:

-- --require "irc"
@@ -24,11 +24,12 @@ module "irc" ---- Create a new IRC object. Use irc:connect to connect to a server. +--- Create a new Connection 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 irc object. +-- @return Returns a new Connection object. +-- @see Connection function new(user) --- Hook a function to an event. @@ -46,10 +47,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(server, port) +function irc:connect(host, port) -- @param table Table of connection details --- @see Connection +-- @see ConnectOptions function irc:connect(table) --- Disconnect irc from the server. @@ -116,16 +117,26 @@ function irc:handle(msg) function irc:shutdown() --- Table with connection information. ---
    ---
  • host - Server host name.
  • ---
  • port - Server port. [defaults to 6667]
  • ---
  • timeout - Connect timeout. [defaults to 30]
  • ---
  • password - Server password.
  • ---
  • secure - Boolean to enable TLS connection, pass a params table (described, [luasec]) to control
  • ---
+-- @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 @@ -145,8 +156,8 @@ function irc:shutdown() --- List of hooks you can use with irc:hook. -- The parameter list describes the parameters passed to the callback function. --
    ---
  • PreRegister(connection)Useful for CAP commands and SASL.
  • ---
  • OnRaw(line) - (any non false/nil return value assumes line handled and will not be further processed)
  • +--
  • PreRegister() - Usefull for requesting capabilities.
  • +--
  • OnRaw(line) - Any non false/nil return value assumes line handled and will not be further processed.
  • --
  • OnSend(line)
  • --
  • OnDisconnect(message, errorOccurred)
  • --
  • OnChat(user, channel, message)
  • @@ -162,7 +173,10 @@ function irc:shutdown() --
  • OnUserMode(modes)
  • --
  • OnChannelMode(user, channel, modes)
  • --
  • OnModeChange(user, target, modes, ...)* ('...' contains mode options such as banmasks)
  • ---
  • DoX(msg)'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)
  • +--
  • OnCapabilityList(caps)
  • +--
  • OnCapabilityAvailable(cap, value) Called only when a capability becomes available or changes.
  • +--
  • OnCapabilitySet(cap, enabled)*
  • +--
  • DoX(msg)* - 'X' is any IRC command or numeric with the first letter capitalized (eg, DoPing and Do001)
  • --
-- * 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 6af5d107..f01936f6 100755 --- a/mods/irc/irc/handlers.lua +++ b/mods/irc/irc/handlers.lua @@ -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 + diff --git a/mods/irc/irc/init.lua b/mods/irc/irc/init.lua index e2eb71e8..194b6921 100755 --- a/mods/irc/irc/init.lua +++ b/mods/irc/irc/init.lua @@ -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; +} diff --git a/mods/irc/irc/main.lua b/mods/irc/irc/main.lua deleted file mode 100755 index ece0cf2c..00000000 --- a/mods/irc/irc/main.lua +++ /dev/null @@ -1,241 +0,0 @@ -local socket = require "socket" - --- Module table -local irc = {} - -local meta = {} -meta.__index = meta -irc.meta = meta - -local meta_preconnect = {} -function meta_preconnect.__index(o, k) - local v = rawget(meta_preconnect, k) - - if not v and meta[k] then - error(("field '%s' is not accessible before connecting"):format(k), 2) - end - return v -end - -function irc.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 irc.defaultNickGenerator; - hooks = {}; - track_users = true; - supports = {}; - messageQueue = {}; - lastThought = 0; - recentMessages = 0; - } - assert(irc.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 - if f(...) then - return true - 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:queue(irc.Message({command="CAP", args={"REQ", "multi-prefix"}})) - - self:invoke("PreRegister", self) - self:queue(irc.Message({command="CAP", args={"END"}})) - - if password then - self:queue(irc.Message({command="PASS", args={password}})) - end - - self:queue(irc.msgs.nick(self.nick)) - self:queue(irc.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(irc.msgs.quit(message)) - - self:shutdown() -end - -function meta:shutdown() - self.socket:close() - setmetatable(self, nil) -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(irc.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 - -local handlers = rawget(_G,"handlers") - -function meta:handle(msg) - local handler = irc.handlers[msg.command] - if handler then - handler(self, msg) - end - self:invoke("Do" .. irc.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(irc.msgs.whois(nick)) - - local result = {} - - while true do - local line = getline(self, 3) - if line then - local msg = irc.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(irc.msgs.topic(channel)) -end - -return irc - diff --git a/mods/irc/irc/messages.lua b/mods/irc/irc/messages.lua index 2f753edc..48aa6873 100755 --- a/mods/irc/irc/messages.lua +++ b/mods/irc/irc/messages.lua @@ -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 + diff --git a/mods/irc/irc/push-luadoc.sh b/mods/irc/irc/push-luadoc.sh new file mode 100755 index 00000000..6f77ff29 --- /dev/null +++ b/mods/irc/irc/push-luadoc.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +if [ "$TRAVIS_REPO_SLUG" == "JakobOvrum/LuaIRC" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_BRANCH" == "master" ]; then + + echo -e "Generating luadoc...\n" + + git config --global user.email "travis@travis-ci.org" + git config --global user.name "travis-ci" + git clone --quiet --branch=gh-pages https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG} gh-pages > /dev/null + + cd gh-pages + git rm -rf ./doc + sh ./generate.sh + git add -f ./doc + git commit -m "Lastest documentation on successful travis build $TRAVIS_BUILD_NUMBER auto-pushed to gh-pages" + git push -fq origin gh-pages > /dev/null + + echo -e "Published luadoc to gh-pages.\n" +fi diff --git a/mods/irc/irc/set.lua b/mods/irc/irc/set.lua index feec00dc..a4c68818 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 92bb76de..59168570 100755 --- a/mods/irc/irc/util.lua +++ b/mods/irc/irc/util.lua @@ -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 +