X-Git-Url: https://jfr.im/git/erebus.git/blobdiff_plain/a4eacae2beaf344a5fd5b1d231f772e9fcbe4e10..e4255e706c1531dee26437dd5a1301003dfc01c3:/bot.py diff --git a/bot.py b/bot.py index a3b16f9..513f257 100644 --- a/bot.py +++ b/bot.py @@ -9,9 +9,11 @@ class Bot(object): def __init__(self, parent, nick, user, bind, server, port, realname, chans): self.parent = parent self.nick = nick + self.user = user + self.realname = realname self.chans = chans - self.conn = BotConnection(self, nick, user, bind, server, port, realname) + self.conn = BotConnection(self, bind, server, port) def connect(self): if self.conn.connect(): self.parent.newfd(self, self.conn.socket.fileno()) @@ -28,9 +30,9 @@ class Bot(object): elif pieces[1] == "001": self.conn.registered(True) - for c in self.chans: self.join(c) + else: self.parse(line) def parse(self, line): @@ -54,24 +56,14 @@ class Bot(object): def parsemsg(self, user, chan, msg): if msg[0] == '!': #TODO check config for trigger msg = msg[1:] - else: return pieces = msg.split() - cmd = pieces[0].upper() - - if cmd == "EVAL": - try: ret = eval(' '.join(pieces[1:])) - except: self.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1])) - else: self.msg(chan, "Done: %r" % (ret)) + cmd = pieces[0].lower() - elif cmd == "EXEC": - try: exec ' '.join(pieces[1:]) - except: self.msg(chan, "Error: %s %s" % (sys.exc_info()[0], sys.exc_info()[1])) - else: self.msg(chan, "Done.") - - #TODO + if self.parent.hashook(cmd): + self.parent.gethook(cmd)(self, user, chan, *pieces[1:]) def msg(self, target, msg): if isinstance(target, self.parent.User): self.conn.send("NOTICE %s :%s" % (target.nick, msg)) @@ -90,27 +82,27 @@ class Bot(object): def quit(self, reason="Shutdown"): self.conn.send("QUIT :%s" % (reason)) + def __str__(self): return self.nick + def __repr__(self): return "" % (self.nick) + class BotConnection(object): state = 0 # 0=disconnected, 1=registering, 2=connected - def __init__(self, parent, nick, user, bind, server, port, realname): + def __init__(self, parent, bind, server, port): self.parent = parent self.buffer = '' self.socket = None - self.nick = nick - self.user = user self.bind = bind self.server = server self.port = int(port) - self.realname = realname def connect(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.bind((self.bind, 0)) self.socket.connect((self.server, self.port)) - self.send("NICK %s" % (self.nick)) - self.send("USER %s 0 * :%s" % (self.user, self.realname)) + self.send("NICK %s" % (self.parent.nick)) + self.send("USER %s 0 * :%s" % (self.parent.user, self.parent.realname)) self.state = 1 return True @@ -120,7 +112,7 @@ class BotConnection(object): #TODO: rewrite send() to queue def send(self, line): - print self.nick, '[O]', line + print self.parent.nick, '[O]', line self.write(line) def write(self, line): @@ -136,3 +128,6 @@ class BotConnection(object): self.buffer = pieces[1] return lines + + def __str__(self): return self.nick + def __repr__(self): return "" % (self.socket.fileno(), self.parent.nick)