From 78fa1106581381f67ac3953c806bf1787e81a369 Mon Sep 17 00:00:00 2001 From: jcao219 Date: Mon, 4 Jul 2011 04:26:03 -0500 Subject: [PATCH] improved the irc lib --- oyoyo/client.py | 33 ++++++++++++++++++++++++++------- wolfbot.py | 7 ++++--- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/oyoyo/client.py b/oyoyo/client.py index fb2279e..af1ea99 100644 --- a/oyoyo/client.py +++ b/oyoyo/client.py @@ -39,8 +39,23 @@ if sys.version_info < (3,): class IRCClientError(Exception): pass - - + +def add_commands(d): + def dec(cls): + for key in d: + def func(x): + def gen(self, *a): + self.send(x, *a) + return gen + setattr(cls, d[key], func(key)) + return cls + return dec +@add_commands({"JOIN": "join", + "MODE": "mode", + "USER": "user", + "NICK": "nick", + "NOTICE": "notice", + "PART": "part"}) class IRCClient: """ IRC Client class. This handles one connection to a server. This can be used either with or without IRCApp ( see connect() docs ) @@ -117,16 +132,14 @@ class IRCClient: bargs.append(bytes(arg, encoding)) elif isinstance(arg, bytes): bargs.append(arg) - elif type(arg).__name__ == 'unicode': - bargs.append(arg.encode(encoding)) else: raise IRCClientError('Refusing to send one of the args from provided: %s' % repr([(type(arg), arg) for arg in args])) - msg = bytes(" ", "ascii").join(bargs) + msg = bytes(" ", "utf_8").join(bargs) logging.info('---> send "%s"' % msg) - self.socket.send(msg + bytes("\r\n", "ascii")) + self.socket.send(msg + bytes("\r\n", "utf_8")) def connect(self): """ initiates the connection to the server set in self.host:self.port @@ -181,7 +194,13 @@ class IRCClient: if self.socket: logging.info('closing socket') self.socket.close() - + def msg(self, user, msg): + for line in msg.split('\n'): + self.send("PRIVMSG", user, ":{0}".format(line)) + def quit(self, msg): + self.send("QUIT :" + msg) + def identify(self, passwd, authuser="NickServ"): + self.msg(authuser, "IDENTIFY {0}".format(passwd)) class IRCApp: """ This class manages several IRCClient instances without the use of threads. diff --git a/wolfbot.py b/wolfbot.py index ebc5da0..867b955 100644 --- a/wolfbot.py +++ b/wolfbot.py @@ -65,6 +65,7 @@ class WolfBotHandler(DefaultCommandHandler): print(fro, to) def main(): + logging.basicConfig(level=logging.DEBUG) cli = IRCClient(WolfBotHandler, host="irc.freenode.net", port=6667, nick="wolfbot2-alpha", connect_cb=connect_callback) @@ -76,13 +77,13 @@ def main(): @cmd("!say", True) def join(cli, rawnick, rest): - helpers.msg(cli, botconfig.CHANNEL, "{0} says: {1}".format(parse_nick(rawnick)[0], rest)) - + cli.msg(botconfig.CHANNEL, "{0} says: {1}".format(parse_nick(rawnick)[0], rest)) + @cmd("!bye", True) @cmd("!bye", False) def forced_exit(cli, rawnick, *rest): if parse_nick(rawnick)[0] in botconfig.ADMINS: - helpers.quit(cli, "Forced quit from admin") + cli.quit("Forced quit from admin") raise SystemExit #Game Logic Ends