X-Git-Url: https://git.kaliko.me/?p=sid.git;a=blobdiff_plain;f=sid%2Fsid.py;h=5b85fd15211315113fc83a3907938ba74d623e70;hp=1f8cd07d32331b7135f895459bfa1c9a72382ca0;hb=1dd932993b7357e90415198689cf3b6e2c6c29a2;hpb=c42c7531be1c512846ab8795f331e676ab53f003 diff --git a/sid/sid.py b/sid/sid.py index 1f8cd07..5b85fd1 100644 --- a/sid/sid.py +++ b/sid/sid.py @@ -2,7 +2,7 @@ # Copyright (C) 2007-2012 Thomas Perl # Copyright (C) 2010, 2011 Anaël Verrier -# Copyright (C) 2014 kaliko +# Copyright (C) 2014, 2015, 2020 kaliko # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -21,7 +21,7 @@ import inspect import logging import traceback -import sleekxmpp +import slixmpp def botcmd(*args, **kwargs): @@ -41,15 +41,15 @@ def botcmd(*args, **kwargs): return lambda func: decorate(func, **kwargs) -class MUCBot(sleekxmpp.ClientXMPP): +class MUCBot(slixmpp.ClientXMPP): prefix = '!' def __init__(self, jid, password, room, nick, log_file=None, - log_level=logging.INFO): + log_level=logging.INFO): super(MUCBot, self).__init__(jid, password) - self.log = logging.getLogger(__name__) + self.log = logging.getLogger(__package__) self.plugins = list() self.commands = dict() self.room = room @@ -58,6 +58,7 @@ class MUCBot(sleekxmpp.ClientXMPP): self.__seen = dict() self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0045') # Multi-User Chat + self.register_plugin('xep_0071') # xhtml-im self.register_plugin('xep_0199') # self Ping # The session_start event will be triggered when @@ -75,7 +76,7 @@ class MUCBot(sleekxmpp.ClientXMPP): for name, value in inspect.getmembers(self): if inspect.ismethod(value) and getattr(value, '_bot_command', False): name = getattr(value, '_bot_command_name') - self.log.debug('Registered command: %s' % name) + self.log.debug('Registered command: %s', name) self.commands[name] = value def __set_logger(self, log_file=None, log_level=logging.INFO): @@ -83,13 +84,15 @@ class MUCBot(sleekxmpp.ClientXMPP): log_fd = open(log_file, 'w') if log_file else None chandler = logging.StreamHandler(log_fd) formatter = logging.Formatter( - '%(asctime)s - %(name)s - %(levelname)s - %(message)s') + '%(asctime)s - %(name)s - %(levelname)s - %(message)s' + ) chandler.setFormatter(formatter) self.log.addHandler(chandler) self.log.setLevel(log_level) - self.log.debug('set logger, log level : %s' % log_level) + self.log.debug('set logger, log level : %s', log_level) def message(self, msg): + """Messages handler""" if msg['type'] not in ('groupchat', 'chat'): self.log.warning('Unhandled message') return @@ -99,25 +102,25 @@ class MUCBot(sleekxmpp.ClientXMPP): if not body.startswith(MUCBot.prefix): return if msg['from'] not in self.__seen: - self.log.warning('Will not handle message ' - 'from unseen jid: %s' % msg['from']) + self.log.warning('Will not handle message from unseen jid: %s', msg['from']) #return args = body[1:].split() cmd = args.pop(0) if cmd not in self.commands: return - self.log.debug('cmd: {0}'.format(cmd)) + self.log.debug('cmd: %s', cmd) if args: - self.log.debug('arg: {0}'.format(args)) + self.log.debug('arg: %s', args) try: self.commands[cmd](msg, args) except Exception as err: reply = ''.join(traceback.format_exc()) - self.log.exception('An error occurred processing: {0}: {1}'.format(body, reply)) + self.log.exception('An error occurred processing: %s: %s', body, reply) if self.log.level < 10 and reply: self.send_message(mto=msg['from'].bare, mbody=reply, mtype='groupchat') def _view(self, pres): + """Track known nick""" nick = pres['from'] status = (pres['type'], pres['status']) self.__seen.update({nick: status}) @@ -137,7 +140,7 @@ class MUCBot(sleekxmpp.ClientXMPP): """ self.get_roster() self.send_presence() - self.plugin['xep_0045'].joinMUC(self.room, + self.plugin['xep_0045'].join_muc(self.room, self.nick, # If a room password is needed, use: # password=the_room_password, @@ -149,19 +152,19 @@ class MUCBot(sleekxmpp.ClientXMPP): if inspect.ismethod(value) and getattr(value, '_bot_command', False): name = getattr(value, '_bot_command_name') - self.log.debug('Registered command: %s' % name) + self.log.debug('Registered command: %s', name) self.commands[name] = value def foreach_plugin(self, method, *args, **kwds): for plugin in self.plugins: - self.log.debug('shuting down %s' % plugin.__str__) + self.log.debug('shuting down %s', plugin.__str__) getattr(plugin, method)(*args, **kwds) def shutdown_plugins(self): # TODO: why can't use event session_end|disconnected? self.log.info('shuting down') for plugin in self.plugins: - self.log.debug('shuting down %s' % plugin) + self.log.debug('shuting down %s', plugin) getattr(plugin, 'shutdown')() @botcmd @@ -170,7 +173,8 @@ class MUCBot(sleekxmpp.ClientXMPP): Automatically assigned to the "help" command.""" help_cmd = ('Type {}help '.format(self.prefix) + - ' to get more info about that specific command.') + ' to get more info about that specific command.\n\n'+ + 'SRC: http://git.kaliko.me/sid.git') if not args: if self.__doc__: description = self.__doc__.strip() @@ -189,7 +193,8 @@ class MUCBot(sleekxmpp.ClientXMPP): text = '{}\n\n{}'.format(description, usage) else: if args[0] in self.commands.keys(): - text = self.commands[args[0]].__doc__.strip() or 'undocumented' + text = self.commands[args[0]].__doc__ or 'undocumented' + text = inspect.cleandoc(text) else: text = 'That command is not defined.' if message['type'] == 'groupchat':