X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sid%2Fsid.py;h=373209d2e6797c6040c27510b8e492d11745635e;hb=f2f44c64470d0967913feb1bfb9868fa0c7d9e1b;hp=24f543f6e60d6ed3cdd7efbd86f24b3e3d03bb3c;hpb=8050b8698ff1f6294abceb8b022a4aecdbe8375e;p=sid.git diff --git a/sid/sid.py b/sid/sid.py index 24f543f..373209d 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 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 @@ -49,12 +49,13 @@ class MUCBot(sleekxmpp.ClientXMPP): 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 self.nick = nick self.__set_logger(log_file, log_level) + self.__seen = dict() self.register_plugin('xep_0030') # Service Discovery self.register_plugin('xep_0045') # Multi-User Chat self.register_plugin('xep_0199') # self Ping @@ -64,16 +65,17 @@ class MUCBot(sleekxmpp.ClientXMPP): # and the XML streams are ready for use. We want to # listen for this event so that we we can initialize # our roster. - self.add_event_handler("session_start", self.start) + self.add_event_handler('session_start', self.start) # Handles MUC message and dispatch - self.add_event_handler("groupchat_message", self.muc_message) + self.add_event_handler('message', self.message) + self.add_event_handler('got_online', self._view) # Discover bot internal command (ie. help) 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): @@ -85,31 +87,39 @@ class MUCBot(sleekxmpp.ClientXMPP): 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 muc_message(self, msg): - # ignore message from self - body = msg['body'] - mucfrom = msg['mucnic'] + def message(self, msg): + if msg['type'] not in ('groupchat', 'chat'): + self.log.warning('Unhandled message') + return if msg['mucnick'] == self.nick: - self.log.debug('ignoring message from me') return + body = msg['body'].strip() 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']) + #return args = body[1:].split() - cmd= args.pop(0) + 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: - reply = self.commands[cmd](msg, args) + self.commands[cmd](msg, args) except Exception as err: - reply = traceback.format_exc(err) - self.log.exception('An error occurred processing: {0}: {1}'.format(body, reply)) - self.log.debug(reply) - self.send_message(mto=msg['from'].bare, mbody=reply, mtype='groupchat') + reply = ''.join(traceback.format_exc()) + 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): + nick = pres['from'] + status = (pres['type'], pres['status']) + self.__seen.update({nick: status}) def start(self, event): """ @@ -138,19 +148,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 @@ -158,7 +168,6 @@ class MUCBot(sleekxmpp.ClientXMPP): """Returns a help string listing available options. Automatically assigned to the "help" command.""" - self.log.info(args) help_cmd = ('Type {}help '.format(self.prefix) + ' to get more info about that specific command.') if not args: @@ -178,10 +187,12 @@ class MUCBot(sleekxmpp.ClientXMPP): usage = usage + '\n\n' + help_cmd text = '{}\n\n{}'.format(description, usage) else: - self.log.debug(args[0]) - self.log.debug(args[0] in self.commands.keys()) if args[0] in self.commands.keys(): text = self.commands[args[0]].__doc__.strip() or 'undocumented' else: text = 'That command is not defined.' - return text + if message['type'] == 'groupchat': + to = message['from'].bare + else: + to = message['from'] + self.send_message(mto=to, mbody=text, mtype=message['type'])