Browse Source

improved the irc lib

master
jcao219 14 years ago
parent
commit
78fa110658
  1. 33
      oyoyo/client.py
  2. 7
      wolfbot.py

33
oyoyo/client.py

@ -39,8 +39,23 @@ if sys.version_info < (3,): @@ -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: @@ -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: @@ -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.

7
wolfbot.py

@ -65,6 +65,7 @@ class WolfBotHandler(DefaultCommandHandler): @@ -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(): @@ -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

Loading…
Cancel
Save