1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2010, 2011 Anaƫl Verrier <elghinn@free.fr>
4 # Copyright (C) 2014, 2020 kaliko <kaliko@azylum.org>
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.
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.
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/>.
18 from .sid import botcmd
23 Simple Plugin object to derive from:
25 * Exposes the bot object and its logger
26 * Provides send helpers
28 :param sid.sid.MUCBot bot: bot the plugin is load from
31 def __init__(self, bot):
33 self.log = bot.log.getChild(self.__class__.__name__)
34 #: :py:obj:`list` : List of tuples (event, handler)
37 def add_handlers(self):
38 """Add handlers declared in self.hanlders"""
39 for event, handler in self.handlers:
40 self.log.debug(f'Add {event} > {self.__class__.__name__}().{handler.__name__}')
41 self.bot.add_event_handler(event, handler)
43 def rm_handlers(self):
44 """Remove handlers declared in self.hanlders"""
45 for event, handler in self.handlers:
46 self.log.debug(f'Remove {event} > {self.__class__.__name__}().{handler.__name__}')
47 self.bot.del_event_handler(event, handler)
49 def send(self, dest, msg, mtype='chat'):
52 :param str dest: Message recipient
53 :param dict,str msg: Message to send (use dict for xhtml-im)
56 if **msg** is a :py:obj:`dict` to provide xhmlt-im massages::
60 mhtml: '<b>text</b>, # optional'
63 if isinstance(msg, str):
65 msg.setdefault('mhtml', None)
66 self.bot.send_message(mto=dest,
70 def reply(self, rcv, msg):
71 """Smart reply to message received.
73 Replies ``msg`` in private or on the muc depending on ``rcv``
75 :param rcv: The received message (slixmpp object)
76 :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format
79 if rcv['type'] == 'groupchat':
81 self.send(to, msg, mtype=rcv['type'])
84 """Empty method to override. Called on bot shutdown"""