diff sat/plugins/plugin_exp_invitation.py @ 2912:a3faf1c86596

plugin events: refactored invitation and personal lists logic: - invitation logic has been moved to a new generic "plugin_exp_invitation" plugin - plugin_misc_invitations has be rename "plugin_exp_email_invitation" to avoid confusion - personal event list has be refactored to use a new experimental "list of interest", which regroup all interestings items, events or other ones
author Goffi <goffi@goffi.org>
date Sun, 14 Apr 2019 08:21:51 +0200
parents sat/plugins/plugin_exp_events.py@003b8b4b56a7
children 672e6be3290f
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sat/plugins/plugin_exp_invitation.py	Sun Apr 14 08:21:51 2019 +0200
@@ -0,0 +1,187 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8 -*-
+
+# SAT plugin to detect language (experimental)
+# Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# 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 Affero General Public License for more details.
+
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+from sat.core.i18n import _
+from sat.core import exceptions
+from sat.core.constants import Const as C
+from sat.core.log import getLogger
+from twisted.internet import defer
+from twisted.words.protocols.jabber import jid
+from wokkel import disco, iwokkel
+from zope.interface import implements
+from twisted.words.protocols.jabber.xmlstream import XMPPHandler
+
+log = getLogger(__name__)
+
+
+PLUGIN_INFO = {
+    C.PI_NAME: "Invitation",
+    C.PI_IMPORT_NAME: "INVITATION",
+    C.PI_TYPE: "EXP",
+    C.PI_PROTOCOLS: [],
+    C.PI_DEPENDENCIES: ["XEP-0060"],
+    C.PI_RECOMMENDATIONS: [],
+    C.PI_MAIN: "Invitation",
+    C.PI_HANDLER: "yes",
+    C.PI_DESCRIPTION: _(u"Experimental handling of invitations"),
+}
+
+NS_INVITATION = u"https://salut-a-toi/protocol/invitation:0"
+INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(
+    ns_invit=NS_INVITATION
+)
+NS_INVITATION_LIST = NS_INVITATION + u"#list"
+
+
+class Invitation(object):
+
+    def __init__(self, host):
+        log.info(_(u"Invitation plugin initialization"))
+        self.host = host
+        self._p = self.host.plugins["XEP-0060"]
+        # map from namespace of the invitation to callback handling it
+        self._ns_cb = {}
+
+    def getHandler(self, client):
+        return PubsubInvitationHandler(self)
+
+    def registerNamespace(self, namespace, callback):
+        """Register a callback for a namespace
+
+        @param namespace(unicode): namespace handled
+        @param callback(callbable): method handling the invitation
+            For pubsub invitation, it will be called with following arguments:
+                - client
+                - service(jid.JID): pubsub service jid
+                - node(unicode): pubsub node
+                - item_id(unicode, None): pubsub item id
+                - item_elt(domish.Element): item of the invitation
+        @raise exceptions.ConflictError: this namespace is already registered
+        """
+        if namespace in self._ns_cb:
+            raise exceptions.ConflictError(
+                u"invitation namespace {namespace} is already register with {callback}"
+                .format(namespace=namespace, callback=self._ns_cb[namespace]))
+        self._ns_cb[namespace] = callback
+
+    def sendPubsubInvitation(self, client, invitee_jid, service, node,
+                              item_id):
+        """Send an invitation in a <message> stanza
+
+        @param invitee_jid(jid.JID): entitee to send invitation to
+        @param service(jid.JID): pubsub service
+        @param node(unicode): pubsub node
+        @param item_id(unicode): pubsub id
+        """
+        mess_data = {
+            "from": client.jid,
+            "to": invitee_jid,
+            "uid": "",
+            "message": {},
+            "type": C.MESS_TYPE_CHAT,
+            "subject": {},
+            "extra": {},
+        }
+        client.generateMessageXML(mess_data)
+        invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
+        pubsub_elt = invitation_elt.addElement(u"pubsub")
+        pubsub_elt[u"service"] = service.full()
+        pubsub_elt[u"node"] = node
+        pubsub_elt[u"item"] = item_id
+        client.send(mess_data[u"xml"])
+
+    @defer.inlineCallbacks
+    def _parsePubsubElt(self, client, pubsub_elt):
+        try:
+            service = jid.JID(pubsub_elt["service"])
+            node = pubsub_elt["node"]
+            item_id = pubsub_elt.getAttribute("item")
+        except (RuntimeError, KeyError):
+            log.warning(_(u"Bad invitation, ignoring"))
+            raise exceptions.DataError
+
+        try:
+            items, metadata = yield self._p.getItems(client, service, node,
+                                                     item_ids=[item_id])
+        except Exception as e:
+            log.warning(_(u"Can't get item linked with invitation: {reason}").format(
+                        reason=e))
+        try:
+            item_elt = items[0]
+        except IndexError:
+            log.warning(_(u"Invitation was linking to a non existing item"))
+            raise exceptions.DataError
+
+        try:
+            namespace = item_elt.firstChildElement().uri
+        except Exception as e:
+            log.warning(_(u"Can't retrieve namespace of invitation: {reason}").format(
+                reason = e))
+            raise exceptions.DataError
+
+        args = [service, node, item_id, item_elt]
+        defer.returnValue((namespace, args))
+
+    @defer.inlineCallbacks
+    def onInvitation(self, message_elt, client):
+        invitation_elt = message_elt.invitation
+        for elt in invitation_elt.elements():
+            if elt.uri != NS_INVITATION:
+                log.warning(u"unexpected element: {xml}".format(xml=elt.toXml()))
+                continue
+            if elt.name == u"pubsub":
+                method = self._parsePubsubElt
+            else:
+                log.warning(u"not implemented invitation element: {xml}".format(
+                    xml = elt.toXml()))
+                continue
+            try:
+                namespace, args = yield method(client, elt)
+            except exceptions.DataError:
+                log.warning(u"Can't parse invitation element: {xml}".format(
+                            xml = elt.toXml()))
+                continue
+
+            try:
+                cb = self._ns_cb[namespace]
+            except KeyError:
+                log.warning(_(u'No handler for namespace "{namespace}", invitation ignored')
+                    .format(namespace=namespace))
+            else:
+                cb(client, *args)
+
+
+class PubsubInvitationHandler(XMPPHandler):
+    implements(iwokkel.IDisco)
+
+    def __init__(self, plugin_parent):
+        self.plugin_parent = plugin_parent
+
+    def connectionInitialized(self):
+        self.xmlstream.addObserver(
+            INVITATION, self.plugin_parent.onInvitation, client=self.parent
+        )
+
+    def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
+        return [
+            disco.DiscoFeature(NS_INVITATION),
+        ]
+
+    def getDiscoItems(self, requestor, target, nodeIdentifier=""):
+        return []