Anton Shestakov <av6@dwimlabs.net>, Wed, 11 May 2016 00:09:34 +0800
rollbot: add the default value of --nick option to its help string
rollbot.py
Permissions: -rw-r--r--
from argparse import ArgumentParser, FileType, SUPPRESS if sys.version_info < (3, 0): sys.setdefaultencoding('utf8') class RollBot(sleekxmpp.ClientXMPP): def __init__(self, jid, password, nick, prefix): sleekxmpp.ClientXMPP.__init__(self, jid, password) self.add_event_handler('session_start', self.start) self.add_event_handler('message', self.message) self.add_event_handler('groupchat_message', self.muc_message) self.add_event_handler('groupchat_invite', self.muc_invite) self.add_event_handler('groupchat_direct_invite', self.muc_direct_invite) if msg['type'] not in ('chat', 'normal'): rbody = self.respond(msg['body'].strip()) def muc_message(self, msg): if msg['mucnick'] == self.nick: mbody = msg['body'].lstrip() if mbody.startswith(self.nick): mbody = mbody[len(self.nick):] elif mbody.startswith(self.prefix): mbody = mbody[len(self.prefix):] mbody = mbody.lstrip(':, \t') rbody = self.respond(mbody) rbody = '%s: %s' % (msg['mucnick'], rbody) def muc_invite(self, invite): password = '' # https://github.com/fritzy/SleekXMPP/issues/409 self.plugin['xep_0045'].joinMUC(room, self.nick, password=password, wait=True) def muc_direct_invite(self, invite): room = invite['groupchat_invite']['jid'] password = invite['groupchat_invite']['password'] self.plugin['xep_0045'].joinMUC(room, self.nick, password=password, wait=True) def respond(self, message, prefix=None): for command in self.commands: response = command.parse(message) def lookup(key, args, config, default=None): return getattr(args, key) return config.get(key, default) parser = ArgumentParser(argument_default=SUPPRESS) parser.add_argument('-c', '--config', help='configuration file (YAML)', type=FileType('r')) group = parser.add_argument_group('main configuration', 'options that can be specified in the configuration file') group.add_argument('-j', '--jid', help='JID to use') group.add_argument('-p', '--password', help='password to use') group.add_argument('-n', '--nick', help='MUC nickname (default: rollbot)') group.add_argument('--prefix', help='command prefix (default: !)') group = parser.add_mutually_exclusive_group() group.add_argument('-q', '--quiet', help='set logging to ERROR', action='store_const', dest='loglevel', const=logging.ERROR, default=logging.INFO) group.add_argument('-d', '--debug', help='set logging to DEBUG', action='store_const', dest='loglevel', const=logging.DEBUG, default=logging.INFO) args = parser.parse_args() logging.basicConfig(level=args.loglevel, format='%(asctime)s %(levelname)-8s %(message)s') if hasattr(args, 'config'): config = yaml.safe_load(args.config) jid = lookup('jid', args, config) password = lookup('password', args, config) nick = lookup('nick', args, config, default='rollbot') prefix = lookup('prefix', args, config, default='!') rollbot = RollBot(jid, password, nick, prefix) rollbot.auto_authorize = True rollbot.auto_subscribe = True rollbot.register_plugin('xep_0030') # Service Discovery rollbot.register_plugin('xep_0045') # Multi-User Chat rollbot.register_plugin('xep_0092') # Software Version rollbot.register_plugin('xep_0199') # XMPP Ping rollbot.register_plugin('xep_0202') # Entity Time rollbot.register_plugin('xep_0249') # Direct MUC Invitations logging.info('RollBot connected') rollbot.process(block=True) logging.info('RollBot disconnected') logging.fatal("RollBot couldn't connect") if __name__ == '__main__':