Add a message queue

This commit is contained in:
ShadowNinja 2013-10-23 09:37:21 -04:00
parent 2ee58834e0
commit 7adba31f8c
4 changed files with 39 additions and 12 deletions

View File

@ -28,6 +28,10 @@ function meta:send(msg, ...)
end end
end end
function meta:queue(msg)
table.insert(self.messageQueue, msg)
end
local function verify(str, errLevel) local function verify(str, errLevel)
if str:find("^:") or str:find("%s%z") then if str:find("^:") or str:find("%s%z") then
error(("malformed parameter '%s' to irc command"):format(str), errLevel) error(("malformed parameter '%s' to irc command"):format(str), errLevel)
@ -39,26 +43,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(msgs.privmsg(verify(target, 3), line)) self:queue(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(msgs.notice(verify(target, 3), line)) self:queue(msgs.notice(verify(target, 3), line))
end end
end end
function meta:join(channel, key) function meta:join(channel, key)
self:send(msgs.join( self:queue(msgs.join(
verify(channel, 3), verify(channel, 3),
key and verify(key, 3) or nil)) key and verify(key, 3) or nil))
end end
function meta:part(channel, reason) function meta:part(channel, reason)
channel = verify(channel, 3) channel = verify(channel, 3)
self:send(msgs.part(channel, reason)) self:queue(msgs.part(channel, reason))
if self.track_users then if self.track_users then
self.channels[channel] = nil self.channels[channel] = nil
end end
@ -88,6 +92,6 @@ function meta:setMode(t)
mode = table.concat{mode, "-", verify(rem, 3)} mode = table.concat{mode, "-", verify(rem, 3)}
end end
self:send(msgs.mode(verify(target, 3), mode)) self:queue(msgs.mode(verify(target, 3), mode))
end end

View File

@ -75,6 +75,10 @@ function irc:topic(channel)
-- @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, ...)
--- Queue Message to be sent to the server.
-- @param msg Message to be sent.
function irc:queue(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 text. -- @param message Message text.

View File

@ -82,7 +82,7 @@ end
local function needNewNick(conn, msg) local function needNewNick(conn, msg)
local newnick = conn.nickGenerator(msg.args[2]) local newnick = conn.nickGenerator(msg.args[2])
conn:send(msgs.nick(newnick)) conn:queue(msgs.nick(newnick))
end end
-- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons) -- ERR_ERRONEUSNICKNAME (Misspelt but remains for historical reasons)

View File

@ -10,6 +10,7 @@ local require = require
local tonumber = tonumber local tonumber = tonumber
local type = type local type = type
local pcall = pcall local pcall = pcall
local remove = table.remove
module "irc" module "irc"
@ -41,6 +42,9 @@ function new(data)
hooks = {}; hooks = {};
track_users = true; track_users = true;
supports = {}; supports = {};
messageQueue = {};
lastThought = 0;
recentMessages = 0;
} }
assert(checkNick(o.nick), "Erroneous nickname passed to irc.new") assert(checkNick(o.nick), "Erroneous nickname passed to irc.new")
return setmetatable(o, meta_preconnect) return setmetatable(o, meta_preconnect)
@ -121,17 +125,17 @@ function meta_preconnect:connect(_host, _port)
self.socket = s self.socket = s
setmetatable(self, meta) setmetatable(self, meta)
self:send(Message("CAP", {"REQ", "multi-prefix"})) self:queue(Message("CAP", {"REQ", "multi-prefix"}))
self:invoke("PreRegister", self) self:invoke("PreRegister", self)
self:send(Message("CAP", {"END"})) self:queue(Message("CAP", {"END"}))
if password then if password then
self:send(Message("PASS", {password})) self:queue(Message("PASS", {password}))
end end
self:send(msgs.nick(self.nick)) self:queue(msgs.nick(self.nick))
self:send(Message("USER", {self.username, "0", "*", self.realname})) self:queue(Message("USER", {self.username, "0", "*", self.realname}))
self.channels = {} self.channels = {}
@ -180,6 +184,21 @@ function meta:think()
break break
end end
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(remove(self.messageQueue, 1))
self.recentMessages = self.recentMessages + 1
end
self.lastThought = socket.gettime()
end end
local handlers = handlers local handlers = handlers
@ -231,6 +250,6 @@ function meta:whois(nick)
end end
function meta:topic(channel) function meta:topic(channel)
self:send(msgs.topic(channel)) self:queue(msgs.topic(channel))
end end