]> kaliko git repositories - sid.git/blobdiff - sid/plugin.py
Add ban command to plugin base object
[sid.git] / sid / plugin.py
index 447e7033647ba6ffe5dc1e89f9f47488f4b1d05e..d9ef0ae618d0b9a0122d318baab9470a06debae2 100644 (file)
@@ -1,41 +1,54 @@
 # -*- coding: utf-8 -*-
+# SPDX-FileCopyrightText: 2010, 2011 Anaël Verrier <elghinn@free.fr>
+# SPDX-FileCopyrightText: 2014, 2020 kaliko <kaliko@azylum.org>
 
-# Copyright (C) 2010, 2011 Anaël Verrier <elghinn@free.fr>
-# Copyright (C) 2014 kaliko <kaliko@azylum.org>
-
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation, version 3 only.
+from .sid import botcmd
 
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
 
-# You should have received a copy of the GNU General Public License
-# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+class Plugin:
+    """
+    Simple Plugin object to derive from:
 
-from .sid import botcmd
+       * Exposes the bot object and its logger
+       * Provides send helpers
 
-class Plugin:
-    """Simple Plugin object to derive from:
-        Exposes the bot object and its logger
-        Provides some send helpers
+    :param sid.sid.MUCBot bot: bot the plugin is load from
     """
 
     def __init__(self, bot):
         self.bot = bot
-        self.log = bot.log
+        self.log = bot.log.getChild(self.__class__.__name__)
+        #: :py:obj:`list` : List of tuples (event, handler)
+        self.handlers = []
+
+    def add_handlers(self):
+        """Add handlers declared in self.hanlders"""
+        for event, handler in self.handlers:
+            self.log.debug(f'Add {event} > {self.__class__.__name__}().{handler.__name__}')
+            self.bot.add_event_handler(event, handler)
+
+    def rm_handlers(self):
+        """Remove handlers declared in self.hanlders"""
+        for event, handler in self.handlers:
+            self.log.debug(f'Remove {event} > {self.__class__.__name__}().{handler.__name__}')
+            self.bot.del_event_handler(event, handler)
 
     def send(self, dest, msg, mtype='chat'):
         """Send msg to dest
-            msg = {
-                mbody: 'text',
-                mhtml: '<b>text</b>,  # optional'
-            }
+
+        :param str dest: Message recipient
+        :param dict,str msg: Message to send (use dict for xhtml-im)
+
+        .. note::
+          if **msg** is a :py:obj:`dict` to provide xhmlt-im massages::
+
+              msg = {
+                  mbody: 'text',
+                  mhtml: '<b>text</b>,  # optional'
+              }
         """
         if isinstance(msg, str):
-            msg = {'mbody':msg}
+            msg = {'mbody': msg}
         msg.setdefault('mhtml', None)
         self.bot.send_message(mto=dest,
                               mtype=mtype,
@@ -43,12 +56,30 @@ class Plugin:
 
     def reply(self, rcv, msg):
         """Smart reply to message received.
-        Replies <msg> in private or on the muc depending on <rcv>
+
+        Replies ``msg`` in private or on the muc depending on ``rcv``
+
+        :param rcv: The received message (slixmpp object)
+        :param dict,str msg: The message to reply, refer to :py:obj:`sid.plugin.Plugin.send` for ``msg`` format
         """
         to = rcv['from']
         if rcv['type'] == 'groupchat':
             to = rcv['mucroom']
         self.send(to, msg, mtype=rcv['type'])
 
+    async def ban(self, jid, reason):
+        """Coroutine to ban a jid from the room
+
+        :param str jid: JID to ban
+        :param str reason: Reason
+        """
+        room = self.bot.room
+        try:
+            await self.bot['xep_0045'].set_affiliation(room,
+                    jid=jid, affiliation='outcast', reason=reason)
+        except XMPPError as error:
+            self.log.error(error)
+
     def shutdown(self):
+        """Empty method to override. Called on bot shutdown"""
         pass