diff --git a/doc/irc.luadoc b/doc/irc.luadoc index 5ab1e34..8442cce 100644 --- a/doc/irc.luadoc +++ b/doc/irc.luadoc @@ -127,21 +127,20 @@ function irc:shutdown() -- @name Connection -- @class table ---- Class representing an IRC message. Objects of this class may contain the following fields: --- --- Fields may be missing. The only guarantee is that args is a table. --- Messages have the following methods: --- +--- Class representing an IRC message. -- @name Message -- @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: +-- --- List of hooks you can use with irc:hook. -- The parameter list describes the parameters passed to the callback function. diff --git a/init.lua b/init.lua index c9f6065..b695c22 100644 --- a/init.lua +++ b/init.lua @@ -125,17 +125,17 @@ function meta_preconnect:connect(_host, _port) self.socket = s setmetatable(self, meta) - self:queue(Message("CAP", {"REQ", "multi-prefix"})) + self:queue(Message({command="CAP", args={"REQ", "multi-prefix"}})) self:invoke("PreRegister", self) - self:queue(Message("CAP", {"END"})) + self:queue(Message({command="CAP", args={"END"}})) if password then - self:queue(Message("PASS", {password})) + self:queue(Message({command="PASS", args={password}})) end 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 = {} @@ -178,9 +178,7 @@ function meta:think() local line = getline(self, 3) if line and #line > 0 then if not self:invoke("OnRaw", line) then - local msg = Message() - msg:fromRFC1459(line) - self:handle(msg) + self:handle(Message({raw=line})) end else break @@ -229,8 +227,7 @@ function meta:whois(nick) while true do local line = getline(self, 3) if line then - local msg = Message() - msg:fromRFC1249(line) + local msg = Message({raw=line}) local handler = whoisHandlers[msg.command] if handler then diff --git a/messages.lua b/messages.lua index 02c6963..cd64c51 100644 --- a/messages.lua +++ b/messages.lua @@ -11,11 +11,13 @@ msgs = {} local msg_meta = {} msg_meta.__index = msg_meta -function Message(cmd, args) - return setmetatable({ - command = cmd, - args = args or {}, - }, msg_meta) +function Message(opts) + opts = opts or {} + setmetatable(opts, msg_meta) + if opts.raw then + opts:fromRFC1459(opts.raw) + end + return opts end local tag_escapes = { @@ -108,6 +110,7 @@ function msg_meta:fromRFC1459(line) self.command, pos = line:match("(%S+)()") line = line:sub(pos) + self.args = self.args or {} for pos, param in line:gmatch("()(%S+)") do if param:sub(1, 1) == ":" then param = line:sub(pos + 1) @@ -119,15 +122,15 @@ function msg_meta:fromRFC1459(line) end function msgs.privmsg(to, text) - return Message("PRIVMSG", {to, text}) + return Message({command="PRIVMSG", args={to, text}}) end function msgs.notice(to, text) - return Message("NOTICE", {to, text}) + return Message({command="NOTICE", args={to, text}}) end 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 function msgs.ctcp(command, to, args) @@ -136,27 +139,27 @@ function msgs.ctcp(command, to, args) s = ' '..args end s = s..'\x01' - return Message("PRIVMSG", {to, s}) + return Message({command="PRIVMSG", args={to, s}}) end function msgs.kick(channel, target, reason) - return Message("KICK", {channel, target, reason}) + return Message({command="KICK", args={channel, target, reason}}) end function msgs.join(channel, key) - return Message("JOIN", {channel, key}) + return Message({command="JOIN", args={channel, key}}) end function msgs.part(channel, reason) - return Message("PART", {channel, reason}) + return Message({command="PART", args={channel, reason}}) end function msgs.quit(reason) - return Message("QUIT", {reason}) + return Message({command="QUIT", args={reason}}) end function msgs.kill(target, reason) - return Message("KILL", {target, reason}) + return Message({command="KILL", args={target, reason}}) end function msgs.kline(time, mask, reason, operreason) @@ -166,7 +169,7 @@ function msgs.kline(time, mask, reason, operreason) else args = {mask, reason..'|'..operreason} end - return Message("KLINE", args) + return Message({command="KLINE", args=args}) end function msgs.whois(nick, server) @@ -176,19 +179,19 @@ function msgs.whois(nick, server) else args = {nick} end - return Message("WHOIS", args) + return Message({command="WHOIS", args=args}) end function msgs.topic(channel, text) - return Message("TOPIC", {channel, text}) + return Message({command="TOPIC", args={channel, text}}) end function msgs.invite(channel, target) - return Message("INVITE", {channel, target}) + return Message({command="INVITE", args={channel, target}}) end function msgs.nick(nick) - return Message("NICK", {nick}) + return Message({command="NICK", args={nick}}) end function msgs.mode(target, modes) @@ -197,6 +200,6 @@ function msgs.mode(target, modes) -- MODE foo :+ov Nick1 Nick2 -- MODE foo +ov Nick1 Nick2 local mt = split(modes) - return Message("MODE", {target, unpack(mt)}) + return Message({command="MODE", args={target, unpack(mt)}}) end