X-Git-Url: http://git.kaliko.me/?p=sid.git;a=blobdiff_plain;f=sid%2Fplugin.py;h=6db1d5984f07875af599dd694393ba0aeb8f69c2;hp=2b49bc9da22c9d3a57f1c02a6793da770a3c1221;hb=1c1134e00f13fee92f9a0e0996d08db32579d89a;hpb=40a4cb2e7caa70e009736ead303ce016ddac3a71 diff --git a/sid/plugin.py b/sid/plugin.py index 2b49bc9..6db1d59 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,26 +17,53 @@ from .sid import botcmd -class Plugin(object): +class Plugin: + """ + 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__) - def send(self, msg): - """ - Send msg to the current groupchat defined in self.bot.room - msg = { - mbody: 'text', - mhtml: 'text, # optional' - } + def send(self, dest, msg, mtype='chat'): + """Send msg to dest + + :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=self.bot.room, - mtype='groupchat', + self.bot.send_message(mto=dest, + mtype=mtype, **msg) + def reply(self, rcv, msg): + """Smart reply to message received. + + 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': + to = rcv['mucroom'] + self.send(to, msg, mtype=rcv['type']) + def shutdown(self): pass