]> kaliko git repositories - sid.git/blob - sid/plugin.py
6ff12eed45f01194df1597082f105d8519c3a242
[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     """Simple Plugin object to derive from:
22         Exposes the bot object and its logger
23         Provides some send helpers
24     """
25
26     def __init__(self, bot):
27         self.bot = bot
28         self.log = bot.log.getChild(self.__class__.__name__)
29
30     def send(self, dest, msg, mtype='chat'):
31         """Send msg to dest
32             msg = {
33                 mbody: 'text',
34                 mhtml: '<b>text</b>,  # optional'
35             }
36         """
37         if isinstance(msg, str):
38             msg = {'mbody': msg}
39         msg.setdefault('mhtml', None)
40         self.bot.send_message(mto=dest,
41                               mtype=mtype,
42                               **msg)
43
44     def reply(self, rcv, msg):
45         """Smart reply to message received.
46         Replies <msg> in private or on the muc depending on <rcv>
47         """
48         to = rcv['from']
49         if rcv['type'] == 'groupchat':
50             to = rcv['mucroom']
51         self.send(to, msg, mtype=rcv['type'])
52
53     def shutdown(self):
54         pass