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