comparison sat/plugins/plugin_exp_file_sharing_invitation.py @ 2913:672e6be3290f

plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
author Goffi <goffi@goffi.org>
date Sun, 14 Apr 2019 08:21:51 +0200
parents
children
comparison
equal deleted inserted replaced
2912:a3faf1c86596 2913:672e6be3290f
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin to detect language (experimental)
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 from twisted.internet import defer
24 from twisted.words.protocols.jabber import jid
25 from wokkel import disco, iwokkel
26 from zope.interface import implements
27 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
28
29 log = getLogger(__name__)
30
31
32 PLUGIN_INFO = {
33 C.PI_NAME: "File Sharing Invitation",
34 C.PI_IMPORT_NAME: "FILE_SHARING_INVITATION",
35 C.PI_TYPE: "EXP",
36 C.PI_PROTOCOLS: [],
37 C.PI_DEPENDENCIES: ["XEP-0329"],
38 C.PI_RECOMMENDATIONS: [],
39 C.PI_MAIN: "FileSharingInvitation",
40 C.PI_HANDLER: "yes",
41 C.PI_DESCRIPTION: _(u"Experimental handling of invitations for file sharing"),
42 }
43
44 NS_FILE_SHARING_INVITATION = "https://salut-a-toi/protocol/file-sharing-invitation:0"
45 INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(
46 ns_invit=NS_FILE_SHARING_INVITATION
47 )
48
49
50 class FileSharingInvitation(object):
51
52 def __init__(self, host):
53 log.info(_(u"File Sharing Invitation plugin initialization"))
54 self.host = host
55
56 def getHandler(self, client):
57 return FileSharingInvitationHandler(self)
58
59 def sendFileSharingInvitation(
60 self, client, invitee_jid, service, repos_type=None, namespace=None,
61 path=None):
62 """Send an invitation in a <message> stanza
63
64 @param invitee_jid(jid.JID): entitee to send invitation to
65 @param service(jid.JID): file sharing service
66 @param repos_type(unicode, None): type of files repository, can be:
67 - None, "files": files sharing
68 - "photos": photos album
69 @param namespace(unicode, None): namespace of the shared repository
70 @param path(unicode): pubsub id
71 """
72 mess_data = {
73 "from": client.jid,
74 "to": invitee_jid,
75 "uid": "",
76 "message": {},
77 "type": C.MESS_TYPE_CHAT,
78 "subject": {},
79 "extra": {},
80 }
81 client.generateMessageXML(mess_data)
82 event_elt = mess_data["xml"].addElement("invitation", NS_FILE_SHARING_INVITATION)
83 event_elt[u"service"] = service.full()
84 if repos_type is not None:
85 event_elt[u"type"] = repos_type
86 if namespace is not None:
87 event_elt[u"namespace"] = namespace
88 if path is not None:
89 event_elt[u"path"] = path
90 client.send(mess_data[u"xml"])
91
92 @defer.inlineCallbacks
93 def onInvitation(self, message_elt, client):
94 invitation_elt = message_elt.invitation
95 try:
96 service = jid.JID(invitation_elt["service"])
97 except (RuntimeError, KeyError):
98 log.warning(_(u"Bad invitation, ignoring: {xml}").format(
99 xml=message_elt.toXml()))
100 return
101
102 repos_type = invitation_elt.getAttribute(u"type", u"files")
103 namespace = invitation_elt.getAttribute(u"namespace")
104 path = invitation_elt.getAttribute(u"path")
105 if repos_type == u"files":
106 type_human = _(u"file sharing")
107 elif repos_type == u"photos":
108 type_human = _(u"photos album")
109 log.info(_(
110 u'{profile} has received an invitation for a files repository ({type_human}) '
111 u'with namespace "{namespace}" at path [{path}]').format(
112 profile=client.profile, type_human=type_human, namespace=namespace, path=path)
113 )
114
115
116 class FileSharingInvitationHandler(XMPPHandler):
117 implements(iwokkel.IDisco)
118
119 def __init__(self, plugin_parent):
120 self.plugin_parent = plugin_parent
121
122 def connectionInitialized(self):
123 self.xmlstream.addObserver(
124 INVITATION, self.plugin_parent.onInvitation, client=self.parent
125 )
126
127 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
128 return [
129 disco.DiscoFeature(NS_FILE_SHARING_INVITATION),
130 ]
131
132 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
133 return []