diff --git a/sat/plugins/plugin_misc_email_invitation.py b/sat/plugins/plugin_misc_email_invitation.py index a838dd29..02b4552b 100644 --- a/sat/plugins/plugin_misc_email_invitation.py +++ b/sat/plugins/plugin_misc_email_invitation.py @@ -20,6 +20,7 @@ import shortuuid 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 @@ class InvitationsPlugin(object): 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 @@ class InvitationsPlugin(object): """ 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)