summaryrefslogtreecommitdiffstats
path: root/bot.lua
diff options
context:
space:
mode:
authorNatanael Copa <ncopa@alpinelinux.org>2018-09-17 19:33:11 +0200
committerNatanael Copa <ncopa@alpinelinux.org>2018-09-17 19:33:11 +0200
commit1eb18eb8ad114db86ae24b90e866888d57caf2d7 (patch)
treeef9e0eb682c02075abcd3470146252f188d3fe9d /bot.lua
parent6e8ccb808ebe538ba8577f4d3170d612e54f59d2 (diff)
downloadalbotty-1eb18eb8ad114db86ae24b90e866888d57caf2d7.tar.bz2
albotty-1eb18eb8ad114db86ae24b90e866888d57caf2d7.tar.xz
implement a fifo and delay messages sent via unix socket
prevent to gett banned for flood the irc channels by sending messages from via a queue.
Diffstat (limited to 'bot.lua')
-rw-r--r--bot.lua69
1 files changed, 67 insertions, 2 deletions
diff --git a/bot.lua b/bot.lua
index 4bd71af..13c9f86 100644
--- a/bot.lua
+++ b/bot.lua
@@ -2,6 +2,7 @@
local cqueues = require("cqueues")
local socket = require("cqueues.socket")
local errno = require("cqueues.errno")
+local condition = require("cqueues.condition")
local loop = cqueues.new()
@@ -9,6 +10,46 @@ local host, port, socketpath = ...
--------------------------------------------------------------------------------
+-- fifo queue
+
+local fifo = {}
+function fifo.new()
+ local list = {}
+ list.next = list
+ list.prev = list
+ return setmetatable({list = list, condvar = condition.new()}, {__index = fifo})
+end
+
+function fifo:enqueue(data)
+ local node = { data = data}
+ node.next = self.list.next
+ node.prev = self.list
+ self.list.next.prev = node
+ self.list.next = node
+ self:signal()
+ return self
+end
+
+function fifo:dequeue()
+ local node = self.list.prev
+ self.list.prev = node.prev
+ self.list.prev.next = node.next
+ return node.data
+end
+
+function fifo:empty()
+ return self.list == self.list.next
+end
+
+function fifo:signal()
+ self.condvar:signal()
+end
+
+function fifo:getcv()
+ return self.condvar
+end
+
+--------------------------------------------------------------------------------
-- irc session
local irc = { callbacks = {} }
@@ -19,7 +60,8 @@ function irc.new(nick, username, password, realname)
password = password,
realname = realname or "albotty",
hooks = {},
- channels = {}
+ channels = {},
+ msgq = fifo.new()
}
return setmetatable(self, { __index = irc })
end
@@ -267,6 +309,25 @@ function irc:loop()
until self.conn:eof()
end
+function irc:msgq_loop()
+ while true do
+ self.msgq:getcv():wait()
+ local loop = cqueues.new()
+ loop:wrap(function()
+ while not self.msgq:empty() do
+ local m = self.msgq:dequeue()
+ self:send_chat(m.target, m.message)
+ cqueues.sleep(1.0)
+ end
+ end)
+ loop:loop()
+ end
+end
+
+function irc:enqueue(target, msg)
+ self.msgq:enqueue{target=target, message =msg}
+end
+
--------------------------------------------------------------------------
-- sockserver
@@ -285,7 +346,7 @@ end
sockserver.callbacks["/msg"] = function(self, data)
local dest,msg = string.match(data or "", "^([^ ]+) (.*)")
if dest then
- self.ircsess:send_chat(dest, msg)
+ self.ircsess:enqueue(dest, msg)
return "ok"
end
return "error: msg format"
@@ -369,6 +430,10 @@ loop:wrap(function()
end)
loop:wrap(function()
+ sess:msgq_loop()
+end)
+
+loop:wrap(function()
srv:loop(loop)
end)