]> kaliko git repositories - sid.git/blob - sid/plugin.py
sphinx: Add sphinx docstring
[sid.git] / sid / plugin.py
1 # -*- coding: utf-8 -*-
2
3 # Copyright (C) 2010, 2011 AnaĆ«l Verrier <elghinn@free.fr>
4 # Copyright (C) 2014, 2020 kaliko <kaliko@azylum.org>
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, version 3 only.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 from .sid import botcmd
19
20 class Plugin:
21     """
22     Simple Plugin object to derive from:
23
24        * Exposes the bot object and its logger
25        * Provides send helpers
26
27     :param sid.sid.MUCBot bot: bot the plugin is load from
28     """
29
30     def __init__(self, bot):
31         self.bot = bot
32         self.log = bot.log.getChild(self.__class__.__name__)
33
34     def send(self, dest, msg, mtype='chat'):
35         """Send msg to dest
36
37         :param str dest: Message recipient
38         :param dict,str msg: Message to send (use dict for xhtml-im)
39
40         .. note::
41           if **msg** is a :py:obj:`dict` to provide xhmlt-im massages::
42
43               msg = {
44                   mbody: 'text',
45                   mhtml: '<b>text</b>,  # optional'
46               }
47         """
48         if isinstance(msg, str):
49             msg = {'mbody': msg}
50         msg.setdefault('mhtml', None)
51         self.bot.send_message(mto=dest,
52                               mtype=mtype,
53                               **msg)
54
55     def reply(self, rcv, msg):
56         """Smart reply to message received.
57
58         Replies ``msg`` in private or on the muc depending on ``rcv``
59
60         :param rcv: The received message (slixmpp object)
61         :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format
62         """
63         to = rcv['from']
64         if rcv['type'] == 'groupchat':
65             to = rcv['mucroom']
66         self.send(to, msg, mtype=rcv['type'])
67
68     def shutdown(self):
69         pass