X-Git-Url: http://git.kaliko.me/?a=blobdiff_plain;f=sid%2Fplugin.py;h=efc9a146c31f798eedb48cdb22e0d799b5a704f1;hb=ce3cd5d64e51d2075237b909762a1ed0459eaf80;hp=447e7033647ba6ffe5dc1e89f9f47488f4b1d05e;hpb=c42c7531be1c512846ab8795f331e676ab53f003;p=sid.git diff --git a/sid/plugin.py b/sid/plugin.py index 447e703..efc9a14 100644 --- a/sid/plugin.py +++ b/sid/plugin.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Copyright (C) 2010, 2011 Anaël Verrier -# Copyright (C) 2014 kaliko +# Copyright (C) 2014, 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 @@ -17,25 +17,51 @@ from .sid import botcmd + class Plugin: - """Simple Plugin object to derive from: - Exposes the bot object and its logger - Provides some send helpers + """ + Simple Plugin object to derive from: + + * Exposes the bot object and its logger + * Provides send helpers + + :param sid.sid.MUCBot bot: bot the plugin is load from """ def __init__(self, bot): self.bot = bot - self.log = bot.log + self.log = bot.log.getChild(self.__class__.__name__) + #: :py:obj:`list` : List of tuples (event, handler) + self.handlers = [] + + def add_handlers(self): + """Add handlers declared in self.hanlders""" + for event, handler in self.handlers: + self.log.debug(f'Add {event} > {self.__class__.__name__}().{handler.__name__}') + self.bot.add_event_handler(event, handler) + + def rm_handlers(self): + """Remove handlers declared in self.hanlders""" + for event, handler in self.handlers: + self.log.debug(f'Remove {event} > {self.__class__.__name__}().{handler.__name__}') + self.bot.del_event_handler(event, handler) def send(self, dest, msg, mtype='chat'): """Send msg to dest - msg = { - mbody: 'text', - mhtml: 'text, # optional' - } + + :param str dest: Message recipient + :param dict,str msg: Message to send (use dict for xhtml-im) + + .. note:: + if **msg** is a :py:obj:`dict` to provide xhmlt-im massages:: + + msg = { + mbody: 'text', + mhtml: 'text, # optional' + } """ if isinstance(msg, str): - msg = {'mbody':msg} + msg = {'mbody': msg} msg.setdefault('mhtml', None) self.bot.send_message(mto=dest, mtype=mtype, @@ -43,7 +69,11 @@ class Plugin: def reply(self, rcv, msg): """Smart reply to message received. - Replies in private or on the muc depending on + + Replies ``msg`` in private or on the muc depending on ``rcv`` + + :param rcv: The received message (slixmpp object) + :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format """ to = rcv['from'] if rcv['type'] == 'groupchat': @@ -51,4 +81,5 @@ class Plugin: self.send(to, msg, mtype=rcv['type']) def shutdown(self): + """Empty method to override. Called on bot shutdown""" pass