improved the irc lib
This commit is contained in:
parent
6d40b403c6
commit
c7fce2c7b4
@ -39,8 +39,23 @@ if sys.version_info < (3,):
|
|||||||
|
|
||||||
class IRCClientError(Exception):
|
class IRCClientError(Exception):
|
||||||
pass
|
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:
|
class IRCClient:
|
||||||
""" IRC Client class. This handles one connection to a server.
|
""" IRC Client class. This handles one connection to a server.
|
||||||
This can be used either with or without IRCApp ( see connect() docs )
|
This can be used either with or without IRCApp ( see connect() docs )
|
||||||
@ -117,16 +132,14 @@ class IRCClient:
|
|||||||
bargs.append(bytes(arg, encoding))
|
bargs.append(bytes(arg, encoding))
|
||||||
elif isinstance(arg, bytes):
|
elif isinstance(arg, bytes):
|
||||||
bargs.append(arg)
|
bargs.append(arg)
|
||||||
elif type(arg).__name__ == 'unicode':
|
|
||||||
bargs.append(arg.encode(encoding))
|
|
||||||
else:
|
else:
|
||||||
raise IRCClientError('Refusing to send one of the args from provided: %s'
|
raise IRCClientError('Refusing to send one of the args from provided: %s'
|
||||||
% repr([(type(arg), arg) for arg in args]))
|
% repr([(type(arg), arg) for arg in args]))
|
||||||
|
|
||||||
msg = bytes(" ", "ascii").join(bargs)
|
msg = bytes(" ", "utf_8").join(bargs)
|
||||||
logging.info('---> send "%s"' % msg)
|
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):
|
def connect(self):
|
||||||
""" initiates the connection to the server set in self.host:self.port
|
""" initiates the connection to the server set in self.host:self.port
|
||||||
@ -181,7 +194,13 @@ class IRCClient:
|
|||||||
if self.socket:
|
if self.socket:
|
||||||
logging.info('closing socket')
|
logging.info('closing socket')
|
||||||
self.socket.close()
|
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:
|
class IRCApp:
|
||||||
""" This class manages several IRCClient instances without the use of threads.
|
""" This class manages several IRCClient instances without the use of threads.
|
||||||
|
@ -65,6 +65,7 @@ class WolfBotHandler(DefaultCommandHandler):
|
|||||||
print(fro, to)
|
print(fro, to)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
logging.basicConfig(level=logging.DEBUG)
|
||||||
cli = IRCClient(WolfBotHandler, host="irc.freenode.net", port=6667, nick="wolfbot2-alpha",
|
cli = IRCClient(WolfBotHandler, host="irc.freenode.net", port=6667, nick="wolfbot2-alpha",
|
||||||
connect_cb=connect_callback)
|
connect_cb=connect_callback)
|
||||||
|
|
||||||
@ -76,13 +77,13 @@ def main():
|
|||||||
|
|
||||||
@cmd("!say", True)
|
@cmd("!say", True)
|
||||||
def join(cli, rawnick, rest):
|
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", True)
|
||||||
@cmd("!bye", False)
|
@cmd("!bye", False)
|
||||||
def forced_exit(cli, rawnick, *rest):
|
def forced_exit(cli, rawnick, *rest):
|
||||||
if parse_nick(rawnick)[0] in botconfig.ADMINS:
|
if parse_nick(rawnick)[0] in botconfig.ADMINS:
|
||||||
helpers.quit(cli, "Forced quit from admin")
|
cli.quit("Forced quit from admin")
|
||||||
raise SystemExit
|
raise SystemExit
|
||||||
|
|
||||||
#Game Logic Ends
|
#Game Logic Ends
|
||||||
|
Loading…
x
Reference in New Issue
Block a user