diff sat/plugins/plugin_misc_email_invitation.py @ 3337:932cf08dfdc3

plugin email invitation: new `invitationDelete` method
author Goffi <goffi@goffi.org>
date Thu, 13 Aug 2020 23:46:18 +0200
parents e38ddaf477bd
children be6d91572633
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_email_invitation.py	Thu Aug 13 23:46:18 2020 +0200
+++ b/sat/plugins/plugin_misc_email_invitation.py	Thu Aug 13 23:46:18 2020 +0200
@@ -20,6 +20,7 @@
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
 from twisted.words.protocols.jabber import error
+from twisted.words.protocols.jabber import sasl
 from sat.core.i18n import _, D_
 from sat.core.constants import Const as C
 from sat.core import exceptions
@@ -82,6 +83,9 @@
         host.bridge.addMethod("invitationGet", ".plugin", in_sign='s', out_sign='a{ss}',
                               method=self.get,
                               async_=True)
+        host.bridge.addMethod("invitationDelete", ".plugin", in_sign='s', out_sign='',
+                              method=self._delete,
+                              async_=True)
         host.bridge.addMethod("invitationModify", ".plugin", in_sign='sa{ss}b',
                               out_sign='',
                               method=self._modify,
@@ -381,6 +385,34 @@
         """
         return self.invitations[id_]
 
+    def _delete(self, id_):
+        return defer.ensureDeferred(self.delete(id_))
+
+    async def delete(self, id_):
+        """Delete an invitation data and associated XMPP account"""
+        log.info(f"deleting invitation {id_}")
+        data = await self.get(id_)
+        guest_profile = data['guest_profile']
+        password = data['password']
+        try:
+            await self.host.connect(guest_profile, password)
+            guest_client = self.host.getClient(guest_profile)
+            # XXX: be extra careful to use guest_client and not client below, as this will
+            #   delete the associated XMPP account
+            log.debug("deleting XMPP account")
+            await self.host.plugins['XEP-0077'].unregister(guest_client, None)
+        except (error.StanzaError, sasl.SASLAuthError) as e:
+            log.warning(
+                f"Can't delete {guest_profile}'s XMPP account, maybe it as already been "
+                f"deleted: {e}")
+        try:
+            await self.host.memory.asyncDeleteProfile(guest_profile, True)
+        except Exception as e:
+            log.warning(f"Can't delete guest profile {guest_profile}: {e}")
+        log.debug("removing guest data")
+        await self.invitations.adel(id_)
+        log.info(f"{id_} invitation has been deleted")
+
     def _modify(self, id_, new_extra, replace):
         return self.modify(id_, {str(k): str(v) for k,v in new_extra.items()},
                            replace)