]> jfr.im git - erebus.git/commitdiff
add msg queueing
authorzonidjan <redacted>
Mon, 12 Sep 2016 11:48:43 +0000 (06:48 -0500)
committerzonidjan <redacted>
Mon, 12 Sep 2016 11:48:43 +0000 (06:48 -0500)
bot.py
erebus.py

diff --git a/bot.py b/bot.py
index fa4eaf1b6eb123a77a0b6c780db3dde2164a202f..63ccecb701592ae1d4617e22e5af0ecadcef39bf 100644 (file)
--- a/bot.py
+++ b/bot.py
@@ -3,7 +3,8 @@
 # Erebus IRC bot - Author: John Runyon
 # "Bot" and "BotConnection" classes (handling a specific "arm")
 
-import socket, sys, time
+import socket, sys, time, threading
+from collections import deque
 
 #bots = {'erebus': bot.Bot(nick='Erebus', user='erebus', bind='', server='irc.quakenet.org', port=6667, realname='Erebus')}
 class Bot(object):
@@ -23,6 +24,11 @@ class Bot(object):
                        self.chans = [self.parent.newchannel(self, row['chname']) for row in chansres]
 
                self.conn = BotConnection(self, bind, server, port)
+
+               self.msgqueue = deque()
+               self.makemsgtimer()
+
+
        def connect(self):
                if self.conn.connect():
                        self.parent.newfd(self, self.conn.socket.fileno())
@@ -99,6 +105,10 @@ class Bot(object):
                                del self.parent.users[oldnick.lower()]
                        self.parent.users[newnick.lower()].nickchange(newnick)
 
+               elif pieces[0] == "ERROR":
+                       sys.exit(2)
+                       os._exit(2)
+
                elif pieces[1] == "MODE": #TODO parse for ops/voices (at least)
                        pass
 
@@ -156,12 +166,32 @@ class Bot(object):
        def msg(self, target, msg):
                if target is None or msg is None: return
 
-               if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg))
-               elif isinstance(target, self.parent.Channel): self.conn.send("PRIVMSG %s :%s" % (target.name, msg))
-               elif isinstance(target, basestring):
-                       if target[0] == '#': self.conn.send("PRIVMSG %s :%s" % (target, msg))
-                       else: self.conn.send("NOTICE %s :%s" % (target, msg))
-               else: raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string')
+               self.msgqueue.append((target,msg))
+               if not self.msgtimer.is_alive():
+                       self.msgtimer.start()
+
+       def fastmsg(self, target, msg):
+
+               if isinstance(target, self.parent.User): target = target.nick
+               elif isinstance(target, self.parent.Channel): target = target.name
+               elif not isinstance(target, basestring): raise TypeError('Bot.msg() "target" must be Erebus.User, Erebus.Channel, or string')
+
+               if target[0] == '#': command = "PRIVMSG %s :%s" % (target, msg)
+               else: command = "NOTICE %s :%s" % (target, msg)
+
+               self.conn.send(command)
+
+       def _popmsg(self):
+               self.makemsgtimer()
+
+               try:
+                       self.fastmsg(*self.msgqueue.popleft())
+                       self.msgtimer.start()
+               except IndexError: pass
+
+       def makemsgtimer(self):
+               self.msgtimer = threading.Timer(2, self._popmsg)
+               self.msgtimer.daemon = True
 
        def join(self, chan):
                self.conn.send("JOIN %s" % (chan))
index 34a4ef133b338e5485b542b0170108c2e254cbf7..1c62a4423e33adfc070751ddca6c338e64163977 100644 (file)
--- a/erebus.py
+++ b/erebus.py
@@ -27,7 +27,9 @@ class Erebus(object):
                        self.chans = []
 
                def msg(self, *args, **kwargs):
-                       main.randbot.msg(self, *args, **kwargs)
+                       main.randbot().msg(self, *args, **kwargs)
+               def fastmsg(self, *args, **kwargs):
+                       main.randbot().fastmsg(self, *args, **kwargs)
 
                def isauthed(self):
                        return self.auth is not None
@@ -84,7 +86,9 @@ class Erebus(object):
 
 
                def msg(self, *args, **kwargs):
-                       self.bot.msg(self.name, *args, **kwargs)
+                       self.bot.msg(self, *args, **kwargs)
+               def fastmsg(self, *args, **kwargs):
+                       self.bot.fastmsg(self, *args, **kwargs)
 
                def levelof(self, auth):
                        if auth is None:
@@ -253,6 +257,11 @@ def setup():
        global cfg, main
 
        cfg = config.Config('bot.config')
+
+       pidfile = open(cfg.pidfile, 'w')
+       pidfile.write(str(os.getpid()))
+       pidfile.close()
+
        main = Erebus(cfg)
 
        autoloads = [mod for mod, yes in cfg.items('autoloads') if int(yes) == 1]