Make Message() take a table

This commit is contained in:
ShadowNinja 2014-05-31 00:52:54 -04:00
parent c27e2ec7e9
commit 42938a5b5e
3 changed files with 41 additions and 42 deletions

View File

@ -127,21 +127,20 @@ function irc:shutdown()
-- @name Connection -- @name Connection
-- @class table -- @class table
--- Class representing an IRC message. Objects of this class may contain the following fields: --- Class representing an IRC message.
-- <ul>
-- <li><code>tags</code> - A table of IRCv3 tags.</li>
-- <li><code>prefix</code> - Prefix of the message.</li>
-- <li><code>user</code> - A User object describing the sender of the message.</li>
-- <li><code>command</code> - The IRC command.</li>
-- <li><code>args</code> - A list of the command arguments.</li>
-- </ul>
-- Fields may be missing. The only guarantee is that <code>args</code> is a table.
-- Messages have the following methods:
-- <ul>
-- <li><code>toRFC1459()</code></li>
-- </ul>
-- @name Message -- @name Message
-- @class table -- @class table
-- @field args A list of the command arguments
-- @field command The IRC command
-- @field prefix The prefix of the message
-- @field raw A raw IRC line for this message
-- @field tags A table of IRCv3 tags
-- @field user A User object describing the sender of the message
-- Fields may be missing.
-- Messages have the following methods:
-- <ul>
-- <li><code>toRFC1459()</code> - Returns the message serialized in RFC 1459 format.</li>
-- </ul>
--- List of hooks you can use with irc:hook. --- List of hooks you can use with irc:hook.
-- The parameter list describes the parameters passed to the callback function. -- The parameter list describes the parameters passed to the callback function.

View File

@ -125,17 +125,17 @@ function meta_preconnect:connect(_host, _port)
self.socket = s self.socket = s
setmetatable(self, meta) setmetatable(self, meta)
self:queue(Message("CAP", {"REQ", "multi-prefix"})) self:queue(Message({command="CAP", args={"REQ", "multi-prefix"}}))
self:invoke("PreRegister", self) self:invoke("PreRegister", self)
self:queue(Message("CAP", {"END"})) self:queue(Message({command="CAP", args={"END"}}))
if password then if password then
self:queue(Message("PASS", {password})) self:queue(Message({command="PASS", args={password}}))
end end
self:queue(msgs.nick(self.nick)) self:queue(msgs.nick(self.nick))
self:queue(Message("USER", {self.username, "0", "*", self.realname})) self:queue(Message({command="USER", args={self.username, "0", "*", self.realname}}))
self.channels = {} self.channels = {}
@ -178,9 +178,7 @@ function meta:think()
local line = getline(self, 3) local line = getline(self, 3)
if line and #line > 0 then if line and #line > 0 then
if not self:invoke("OnRaw", line) then if not self:invoke("OnRaw", line) then
local msg = Message() self:handle(Message({raw=line}))
msg:fromRFC1459(line)
self:handle(msg)
end end
else else
break break
@ -229,8 +227,7 @@ function meta:whois(nick)
while true do while true do
local line = getline(self, 3) local line = getline(self, 3)
if line then if line then
local msg = Message() local msg = Message({raw=line})
msg:fromRFC1249(line)
local handler = whoisHandlers[msg.command] local handler = whoisHandlers[msg.command]
if handler then if handler then

View File

@ -11,11 +11,13 @@ msgs = {}
local msg_meta = {} local msg_meta = {}
msg_meta.__index = msg_meta msg_meta.__index = msg_meta
function Message(cmd, args) function Message(opts)
return setmetatable({ opts = opts or {}
command = cmd, setmetatable(opts, msg_meta)
args = args or {}, if opts.raw then
}, msg_meta) opts:fromRFC1459(opts.raw)
end
return opts
end end
local tag_escapes = { local tag_escapes = {
@ -108,6 +110,7 @@ function msg_meta:fromRFC1459(line)
self.command, pos = line:match("(%S+)()") self.command, pos = line:match("(%S+)()")
line = line:sub(pos) line = line:sub(pos)
self.args = self.args or {}
for pos, param in line:gmatch("()(%S+)") do for pos, param in line:gmatch("()(%S+)") do
if param:sub(1, 1) == ":" then if param:sub(1, 1) == ":" then
param = line:sub(pos + 1) param = line:sub(pos + 1)
@ -119,15 +122,15 @@ function msg_meta:fromRFC1459(line)
end end
function msgs.privmsg(to, text) function msgs.privmsg(to, text)
return Message("PRIVMSG", {to, text}) return Message({command="PRIVMSG", args={to, text}})
end end
function msgs.notice(to, text) function msgs.notice(to, text)
return Message("NOTICE", {to, text}) return Message({command="NOTICE", args={to, text}})
end end
function msgs.action(to, text) function msgs.action(to, text)
return Message("PRIVMSG", {to, ("\x01ACTION %s\x01"):format(text)}) return Message({command="PRIVMSG", args={to, ("\x01ACTION %s\x01"):format(text)}})
end end
function msgs.ctcp(command, to, args) function msgs.ctcp(command, to, args)
@ -136,27 +139,27 @@ function msgs.ctcp(command, to, args)
s = ' '..args s = ' '..args
end end
s = s..'\x01' s = s..'\x01'
return Message("PRIVMSG", {to, s}) return Message({command="PRIVMSG", args={to, s}})
end end
function msgs.kick(channel, target, reason) function msgs.kick(channel, target, reason)
return Message("KICK", {channel, target, reason}) return Message({command="KICK", args={channel, target, reason}})
end end
function msgs.join(channel, key) function msgs.join(channel, key)
return Message("JOIN", {channel, key}) return Message({command="JOIN", args={channel, key}})
end end
function msgs.part(channel, reason) function msgs.part(channel, reason)
return Message("PART", {channel, reason}) return Message({command="PART", args={channel, reason}})
end end
function msgs.quit(reason) function msgs.quit(reason)
return Message("QUIT", {reason}) return Message({command="QUIT", args={reason}})
end end
function msgs.kill(target, reason) function msgs.kill(target, reason)
return Message("KILL", {target, reason}) return Message({command="KILL", args={target, reason}})
end end
function msgs.kline(time, mask, reason, operreason) function msgs.kline(time, mask, reason, operreason)
@ -166,7 +169,7 @@ function msgs.kline(time, mask, reason, operreason)
else else
args = {mask, reason..'|'..operreason} args = {mask, reason..'|'..operreason}
end end
return Message("KLINE", args) return Message({command="KLINE", args=args})
end end
function msgs.whois(nick, server) function msgs.whois(nick, server)
@ -176,19 +179,19 @@ function msgs.whois(nick, server)
else else
args = {nick} args = {nick}
end end
return Message("WHOIS", args) return Message({command="WHOIS", args=args})
end end
function msgs.topic(channel, text) function msgs.topic(channel, text)
return Message("TOPIC", {channel, text}) return Message({command="TOPIC", args={channel, text}})
end end
function msgs.invite(channel, target) function msgs.invite(channel, target)
return Message("INVITE", {channel, target}) return Message({command="INVITE", args={channel, target}})
end end
function msgs.nick(nick) function msgs.nick(nick)
return Message("NICK", {nick}) return Message({command="NICK", args={nick}})
end end
function msgs.mode(target, modes) function msgs.mode(target, modes)
@ -197,6 +200,6 @@ function msgs.mode(target, modes)
-- MODE foo :+ov Nick1 Nick2 -- MODE foo :+ov Nick1 Nick2
-- MODE foo +ov Nick1 Nick2 -- MODE foo +ov Nick1 Nick2
local mt = split(modes) local mt = split(modes)
return Message("MODE", {target, unpack(mt)}) return Message({command="MODE", args={target, unpack(mt)}})
end end