]> kaliko git repositories - sid.git/blob - sid/plugin.py
Add ban command to plugin base object
[sid.git] / sid / plugin.py
1 # -*- coding: utf-8 -*-
2 # SPDX-FileCopyrightText: 2010, 2011 AnaĆ«l Verrier <elghinn@free.fr>
3 # SPDX-FileCopyrightText: 2014, 2020 kaliko <kaliko@azylum.org>
4
5 from .sid import botcmd
6
7
8 class Plugin:
9     """
10     Simple Plugin object to derive from:
11
12        * Exposes the bot object and its logger
13        * Provides send helpers
14
15     :param sid.sid.MUCBot bot: bot the plugin is load from
16     """
17
18     def __init__(self, bot):
19         self.bot = bot
20         self.log = bot.log.getChild(self.__class__.__name__)
21         #: :py:obj:`list` : List of tuples (event, handler)
22         self.handlers = []
23
24     def add_handlers(self):
25         """Add handlers declared in self.hanlders"""
26         for event, handler in self.handlers:
27             self.log.debug(f'Add {event} > {self.__class__.__name__}().{handler.__name__}')
28             self.bot.add_event_handler(event, handler)
29
30     def rm_handlers(self):
31         """Remove handlers declared in self.hanlders"""
32         for event, handler in self.handlers:
33             self.log.debug(f'Remove {event} > {self.__class__.__name__}().{handler.__name__}')
34             self.bot.del_event_handler(event, handler)
35
36     def send(self, dest, msg, mtype='chat'):
37         """Send msg to dest
38
39         :param str dest: Message recipient
40         :param dict,str msg: Message to send (use dict for xhtml-im)
41
42         .. note::
43           if **msg** is a :py:obj:`dict` to provide xhmlt-im massages::
44
45               msg = {
46                   mbody: 'text',
47                   mhtml: '<b>text</b>,  # optional'
48               }
49         """
50         if isinstance(msg, str):
51             msg = {'mbody': msg}
52         msg.setdefault('mhtml', None)
53         self.bot.send_message(mto=dest,
54                               mtype=mtype,
55                               **msg)
56
57     def reply(self, rcv, msg):
58         """Smart reply to message received.
59
60         Replies ``msg`` in private or on the muc depending on ``rcv``
61
62         :param rcv: The received message (slixmpp object)
63         :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format
64         """
65         to = rcv['from']
66         if rcv['type'] == 'groupchat':
67             to = rcv['mucroom']
68         self.send(to, msg, mtype=rcv['type'])
69
70     async def ban(self, jid, reason):
71         """Coroutine to ban a jid from the room
72
73         :param str jid: JID to ban
74         :param str reason: Reason
75         """
76         room = self.bot.room
77         try:
78             await self.bot['xep_0045'].set_affiliation(room,
79                     jid=jid, affiliation='outcast', reason=reason)
80         except XMPPError as error:
81             self.log.error(error)
82
83     def shutdown(self):
84         """Empty method to override. Called on bot shutdown"""
85         pass