Add Message class and constructors for common messages

This commit is contained in:
ShadowNinja 2014-03-17 18:52:09 +00:00
parent 9b74265690
commit e958a7242a
6 changed files with 170 additions and 27 deletions

View File

@ -3,14 +3,19 @@ local assert = assert
local error = error local error = error
local select = select local select = select
local pairs = pairs local pairs = pairs
local type = type
module "irc" module "irc"
local meta = _META local meta = _META
function meta:send(msg, ...) function meta:send(msg, ...)
if select("#", ...) > 0 then if type(msg) == "table" then
msg = msg:format(...) msg = msg:toRFC1459()
else
if select("#", ...) > 0 then
msg = msg:format(...)
end
end end
self:invoke("OnSend", msg) self:invoke("OnSend", msg)
@ -34,28 +39,26 @@ end
function meta:sendChat(target, msg) function meta:sendChat(target, msg)
-- Split the message into segments if it includes newlines. -- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do 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
end end
function meta:sendNotice(target, msg) function meta:sendNotice(target, msg)
-- Split the message into segments if it includes newlines. -- Split the message into segments if it includes newlines.
for line in msg:gmatch("([^\r\n]+)") do 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
end end
function meta:join(channel, key) function meta:join(channel, key)
if key then self:send(msgs.join(
self:send("JOIN %s :%s", verify(channel, 3), verify(key, 3)) verify(channel, 3),
else key and verify(key, 3) or nil))
self:send("JOIN %s", verify(channel, 3))
end
end end
function meta:part(channel) function meta:part(channel, reason)
channel = verify(channel, 3) channel = verify(channel, 3)
self:send("PART %s", channel) self:send(msgs.part(channel, reason))
if self.track_users then if self.track_users then
self.channels[channel] = nil self.channels[channel] = nil
end end
@ -85,5 +88,6 @@ function meta:setMode(t)
mode = table.concat{mode, "-", verify(rem, 3)} mode = table.concat{mode, "-", verify(rem, 3)}
end end
self:send("MODE %s %s", verify(target, 3), mode) self:send(msgs.mode(verify(target, 3), mode))
end end

View File

@ -70,24 +70,24 @@ function irc:whois(nick)
-- @param channel Channel to query. -- @param channel Channel to query.
function irc:topic(channel) function irc:topic(channel)
--- Send a raw line of IRC to the server. --- Send a IRC message to the server.
-- @param msg Line to be sent, excluding newline characters. -- @param msg Message or raw line to send, excluding newline characters.
-- @param ... Format parameters for <code>msg</code>, with <code>string.format</code> semantics. [optional] -- @param ... Format parameters for <code>msg</code>, with <code>string.format</code> semantics. [optional]
function irc:send(msg, ...) function irc:send(msg, ...)
--- Send a message to a channel or user. --- Send a message to a channel or user.
-- @param target Nick or channel to send to. -- @param target Nick or channel to send to.
-- @param message Message to send. -- @param message Message text.
function irc:sendChat(target, message) function irc:sendChat(target, message)
--- Send a notice to a channel or user. --- Send a notice to a channel or user.
-- @param target Nick or channel to send to. -- @param target Nick or channel to send to.
-- @param message Notice to send. -- @param message Notice text.
function irc:sendNotice(target, message) function irc:sendNotice(target, message)
--- Join a channel. --- Join a channel.
-- @param channel Channel to join. -- @param channel Channel to join.
-- @param key Channel password. [optional] -- @param key Channel key. [optional]
function irc:join(channel, key) function irc:join(channel, key)
--- Leave a channel. --- Leave a channel.
@ -123,6 +123,16 @@ function irc:shutdown()
-- @name Connection -- @name Connection
-- @class table -- @class table
--- Class representing an IRC message.
-- <ul>
-- <li><code>sender</code></li>
-- <li><code>command</code></li>
-- <li><code>args</code></li>
-- <li><code>toRFC1459()</code></li>
-- </ul>
-- @name Message
-- @class table
--- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function. --- List of hooks you can use with irc:hook. The parameter list describes the parameters passed to the callback function.
-- <ul> -- <ul>
-- <li><code>PreRegister(connection)</code>Useful for CAP commands and SASL.</li> -- <li><code>PreRegister(connection)</code>Useful for CAP commands and SASL.</li>

View File

@ -8,7 +8,7 @@ module "irc"
handlers = {} handlers = {}
handlers["PING"] = function(o, user, query) handlers["PING"] = function(o, user, query)
o:send("PONG :%s", query) o:send(Message("PONG", {query}))
end end
handlers["001"] = function(o, user, me) handlers["001"] = function(o, user, me)
@ -77,7 +77,7 @@ end
local function needNewNick(o, user, target, badnick) local function needNewNick(o, user, target, badnick)
local newnick = o.nickGenerator(badnick) local newnick = o.nickGenerator(badnick)
o:send("NICK %s", newnick) o:send(msgs.nick(newnick))
end end
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons) -- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)

View File

@ -20,6 +20,7 @@ _META = meta
require "irc.util" require "irc.util"
require "irc.asyncoperations" require "irc.asyncoperations"
require "irc.handlers" require "irc.handlers"
require "irc.messages"
local meta_preconnect = {} local meta_preconnect = {}
function meta_preconnect.__index(o, k) function meta_preconnect.__index(o, k)
@ -120,17 +121,17 @@ function meta_preconnect:connect(_host, _port)
self.socket = s self.socket = s
setmetatable(self, meta) setmetatable(self, meta)
self:send("CAP REQ multi-prefix") self:send(Message("CAP", {"REQ", "multi-prefix"}))
self:invoke("PreRegister", self) self:invoke("PreRegister", self)
self:send("CAP END") self:send(Message("CAP", {"END"}))
if password then if password then
self:send("PASS %s", password) self:send(Message("PASS", {password}))
end end
self:send("NICK %s", self.nick) self:send(msgs.nick(self.nick))
self:send("USER %s 0 * :%s", self.username, self.realname) self:send(Message("USER", {self.username, "0", "*", self.realname}))
self.channels = {} self.channels = {}
@ -146,7 +147,7 @@ function meta:disconnect(message)
message = message or "Bye!" message = message or "Bye!"
self:invoke("OnDisconnect", message, false) self:invoke("OnDisconnect", message, false)
self:send("QUIT :%s", message) self:send(msgs.quit(message))
self:shutdown() self:shutdown()
end end
@ -201,7 +202,7 @@ local whoisHandlers = {
} }
function meta:whois(nick) function meta:whois(nick)
self:send("WHOIS %s", nick) self:send(msgs.whois(nick))
local result = {} local result = {}
@ -231,6 +232,6 @@ function meta:whois(nick)
end end
function meta:topic(channel) function meta:topic(channel)
self:send("TOPIC %s", channel) self:send(msgs.topic(channel))
end end

120
messages.lua Normal file
View File

@ -0,0 +1,120 @@
local assert = assert
local setmetatable = setmetatable
local unpack = unpack
module "irc"
msgs = {}
local msg_meta = {}
msg_meta.__index = msg_meta
function Message(cmd, args)
return setmetatable({
command = cmd,
args = args or {},
}, msg_meta)
end
function msg_meta:toRFC1459()
s = self.command
argnum = #self.args
for i = 1, argnum do
local arg = self.args[i]
local startsWithColon = (arg:sub(1, 1) == ":")
local hasSpace = arg:find(" ")
if i == argnum and (hasSpace or startsWithColon) then
s = s.." :"
else
assert(not hasSpace and not startsWithColon,
"Message arguments can not be "
.."serialized to RFC1459 format")
s = s.." "
end
s = s..arg
end
return s
end
function msgs.privmsg(to, text)
return Message("PRIVMSG", {to, text})
end
function msgs.notice(to, text)
return Message("NOTICE", {to, text})
end
function msgs.action(to, text)
return Message("PRIVMSG", {to, ("\x01ACTION %s\x01"):format(text)})
end
function msgs.ctcp(command, to, args)
s = "\x01"..command
if args then
s = ' '..args
end
s = s..'\x01'
return Message("PRIVMSG", {to, s})
end
function msgs.kick(channel, target, reason)
return Message("KICK", {channel, target, reason})
end
function msgs.join(channel, key)
return Message("JOIN", {channel, key})
end
function msgs.part(channel, reason)
return Message("PART", {channel, reason})
end
function msgs.quit(reason)
return Message("QUIT", {reason})
end
function msgs.kill(target, reason)
return Message("KILL", {target, reason})
end
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("KLINE", args)
end
function msgs.whois(nick, server)
local args = nil
if server then
args = {server, nick}
else
args = {nick}
end
return Message("WHOIS", args)
end
function msgs.topic(channel, text)
return Message("TOPIC", {channel, text})
end
function msgs.invite(channel, target)
return Message("INVITE", {channel, target})
end
function msgs.nick(nick)
return Message("NICK", {nick})
end
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 :+ov Nick1 Nick2
-- MODE +ov Nick1 Nick2
mt = split(modes)
return Message("MODE", {target, unpack(mt)})
end

View File

@ -162,3 +162,11 @@ function capitalize(text)
return text:sub(1, 1):upper()..text:sub(2):lower() return text:sub(1, 1):upper()..text:sub(2):lower()
end end
function split(str, sep)
t = {}
for s in str:gmatch("%S+") do
table.insert(t, s)
end
return t
end