# 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):
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())
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
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))
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
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:
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]