]> kaliko git repositories - sid.git/blob - sid/plugin.py
Bump version
[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
21 class Plugin:
22     """
23     Simple Plugin object to derive from:
24
25        * Exposes the bot object and its logger
26        * Provides send helpers
27
28     :param sid.sid.MUCBot bot: bot the plugin is load from
29     """
30
31     def __init__(self, bot):
32         self.bot = bot
33         self.log = bot.log.getChild(self.__class__.__name__)
34
35     def send(self, dest, msg, mtype='chat'):
36         """Send msg to dest
37
38         :param str dest: Message recipient
39         :param dict,str msg: Message to send (use dict for xhtml-im)
40
41         .. note::
42           if **msg** is a :py:obj:`dict` to provide xhmlt-im massages::
43
44               msg = {
45                   mbody: 'text',
46                   mhtml: '<b>text</b>,  # optional'
47               }
48         """
49         if isinstance(msg, str):
50             msg = {'mbody': msg}
51         msg.setdefault('mhtml', None)
52         self.bot.send_message(mto=dest,
53                               mtype=mtype,
54                               **msg)
55
56     def reply(self, rcv, msg):
57         """Smart reply to message received.
58
59         Replies ``msg`` in private or on the muc depending on ``rcv``
60
61         :param rcv: The received message (slixmpp object)
62         :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format
63         """
64         to = rcv['from']
65         if rcv['type'] == 'groupchat':
66             to = rcv['mucroom']
67         self.send(to, msg, mtype=rcv['type'])
68
69     def shutdown(self):
70         pass