From e958a7242a959c401426f8d11c6b3b5dcb3b46f0 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Mon, 17 Mar 2014 18:52:09 +0000 Subject: [PATCH] Add Message class and constructors for common messages --- asyncoperations.lua | 28 ++++++----- doc/irc.luadoc | 20 ++++++-- handlers.lua | 4 +- init.lua | 17 ++++--- messages.lua | 120 ++++++++++++++++++++++++++++++++++++++++++++ util.lua | 8 +++ 6 files changed, 170 insertions(+), 27 deletions(-) create mode 100644 messages.lua diff --git a/asyncoperations.lua b/asyncoperations.lua index 9e9ba13..353b894 100644 --- a/asyncoperations.lua +++ b/asyncoperations.lua @@ -3,14 +3,19 @@ local assert = assert local error = error local select = select local pairs = pairs +local type = type module "irc" local meta = _META function meta:send(msg, ...) - if select("#", ...) > 0 then - msg = msg:format(...) + if type(msg) == "table" then + msg = msg:toRFC1459() + else + if select("#", ...) > 0 then + msg = msg:format(...) + end end self:invoke("OnSend", msg) @@ -34,28 +39,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:send("PRIVMSG %s :%s", verify(target, 3), line) + self:send(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:send("NOTICE %s :%s", verify(target, 3), line) + self:send(msgs.notice(verify(target, 3), line)) end end function meta:join(channel, key) - if key then - self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3)) - else - self:send("JOIN %s", verify(channel, 3)) - end + self:send(msgs.join( + verify(channel, 3), + key and verify(key, 3) or nil)) end -function meta:part(channel) +function meta:part(channel, reason) channel = verify(channel, 3) - self:send("PART %s", channel) + self:send(msgs.part(channel, reason)) if self.track_users then self.channels[channel] = nil end @@ -85,5 +88,6 @@ function meta:setMode(t) mode = table.concat{mode, "-", verify(rem, 3)} end - self:send("MODE %s %s", verify(target, 3), mode) + self:send(msgs.mode(verify(target, 3), mode)) end + diff --git a/doc/irc.luadoc b/doc/irc.luadoc index 9f84bc9..879c75c 100644 --- a/doc/irc.luadoc +++ b/doc/irc.luadoc @@ -70,24 +70,24 @@ function irc:whois(nick) -- @param channel Channel to query. function irc:topic(channel) ---- Send a raw line of IRC to the server. --- @param msg Line to be sent, excluding newline characters. +--- Send a IRC message to the server. +-- @param msg Message or raw line to send, excluding newline characters. -- @param ... Format parameters for msg, with string.format semantics. [optional] function irc:send(msg, ...) --- Send a message to a channel or user. -- @param target Nick or channel to send to. --- @param message Message to send. +-- @param message Message text. function irc:sendChat(target, message) --- Send a notice to a channel or user. -- @param target Nick or channel to send to. --- @param message Notice to send. +-- @param message Notice text. function irc:sendNotice(target, message) --- Join a channel. -- @param channel Channel to join. --- @param key Channel password. [optional] +-- @param key Channel key. [optional] function irc:join(channel, key) --- Leave a channel. @@ -123,6 +123,16 @@ function irc:shutdown() -- @name Connection -- @class table +--- Class representing an IRC message. +-- +-- @name Message +-- @class table + --- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function. --