annotate sat/plugins/plugin_exp_invitation.py @ 2922:28c969432557

plugin XEP-0106: jid escaping implementation
author Goffi <goffi@goffi.org>
date Fri, 26 Apr 2019 11:57:26 +0200
parents 672e6be3290f
children b256e90612d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin to detect language (experimental)
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2765
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _
2243
5e12fc5ae52a plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents: 2231
diff changeset
21 from sat.core import exceptions
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core.constants import Const as C
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import defer
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
25 from twisted.words.protocols.jabber import jid
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
26 from wokkel import disco, iwokkel
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
27 from zope.interface import implements
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
28 from twisted.words.protocols.jabber.xmlstream import XMPPHandler
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
29
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
30 log = getLogger(__name__)
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
34 C.PI_NAME: "Invitation",
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
35 C.PI_IMPORT_NAME: "INVITATION",
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_TYPE: "EXP",
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_PROTOCOLS: [],
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
38 C.PI_DEPENDENCIES: [u"XEP-0060", u"XEP-0329"],
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
39 C.PI_RECOMMENDATIONS: [],
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
40 C.PI_MAIN: "Invitation",
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
41 C.PI_HANDLER: "yes",
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
42 C.PI_DESCRIPTION: _(u"Experimental handling of invitations"),
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 }
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
45 NS_INVITATION = u"https://salut-a-toi/protocol/invitation:0"
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
46 INVITATION = '/message/invitation[@xmlns="{ns_invit}"]'.format(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 ns_invit=NS_INVITATION
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
48 )
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 NS_INVITATION_LIST = NS_INVITATION + u"#list"
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
52 class Invitation(object):
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 def __init__(self, host):
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 log.info(_(u"Invitation plugin initialization"))
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 self.host = host
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 self._p = self.host.plugins["XEP-0060"]
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 # map from namespace of the invitation to callback handling it
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 self._ns_cb = {}
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
61 def getHandler(self, client):
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 return PubsubInvitationHandler(self)
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
63
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 def registerNamespace(self, namespace, callback):
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
65 """Register a callback for a namespace
2243
5e12fc5ae52a plugin events: separation of event node and invitees node
Goffi <goffi@goffi.org>
parents: 2231
diff changeset
66
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
67 @param namespace(unicode): namespace handled
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
68 @param callback(callbable): method handling the invitation
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
69 For pubsub invitation, it will be called with following arguments:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
70 - client
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
71 - service(jid.JID): pubsub service jid
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
72 - node(unicode): pubsub node
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
73 - item_id(unicode, None): pubsub item id
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
74 - item_elt(domish.Element): item of the invitation
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
75 For file sharing invitation, it will be called with following arguments:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
76 - client
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
77 - service(jid.JID): service jid of the file repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
78 - repos_type(unicode): type of the repository, can be:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
79 - files: generic file sharing
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
80 - photos: photos album
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
81 - namespace(unicode, None): namespace of the repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
82 - path(unicode, None): path of the repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
83 - name(unicode, None): name of the repository
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
84 @raise exceptions.ConflictError: this namespace is already registered
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
85 """
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
86 if namespace in self._ns_cb:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87 raise exceptions.ConflictError(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
88 u"invitation namespace {namespace} is already register with {callback}"
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 .format(namespace=namespace, callback=self._ns_cb[namespace]))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
90 self._ns_cb[namespace] = callback
2231
230fc5b609a8 plugin event: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
92 def _generateBaseInvitation(self, client, invitee_jid):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
93 """Generate common mess_data end invitation_elt
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
94
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
95 @param invitee_jid(jid.JID): entitee to send invitation to
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
96 @return (tuple[dict, domish.Element): mess_data and invitation_elt
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
97 """
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
98 mess_data = {
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
99 "from": client.jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
100 "to": invitee_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
101 "uid": "",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
102 "message": {},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
103 "type": C.MESS_TYPE_CHAT,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
104 "subject": {},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
105 "extra": {},
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
106 }
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
107 client.generateMessageXML(mess_data)
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
109 return mess_data, invitation_elt
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
110
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
111 def sendPubsubInvitation(self, client, invitee_jid, service, node,
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
112 item_id):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
113 """Send an pubsub invitation in a <message> stanza
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
114
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
115 @param invitee_jid(jid.JID): entitee to send invitation to
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
116 @param service(jid.JID): pubsub service
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
117 @param node(unicode): pubsub node
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
118 @param item_id(unicode): pubsub id
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
119 """
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
120 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid)
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
121 pubsub_elt = invitation_elt.addElement(u"pubsub")
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
122 pubsub_elt[u"service"] = service.full()
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
123 pubsub_elt[u"node"] = node
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
124 pubsub_elt[u"item"] = item_id
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
125 return client.send(mess_data[u"xml"])
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
126
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
127 def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None,
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
128 namespace=None, path=None, name=None):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
129 """Send a file sharing invitation in a <message> stanza
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
130
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
131 @param invitee_jid(jid.JID): entitee to send invitation to
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
132 @param service(jid.JID): file sharing service
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
133 @param repos_type(unicode, None): type of files repository, can be:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
134 - None, "files": files sharing
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
135 - "photos": photos album
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
136 @param namespace(unicode, None): namespace of the shared repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
137 @param path(unicode, None): path of the shared repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
138 @param name(unicode, None): name of the shared repository
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
139 """
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
140 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid)
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
141 file_sharing_elt = invitation_elt.addElement(u"file_sharing")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
142 file_sharing_elt[u"service"] = service.full()
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
143 if repos_type is not None:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
144 if repos_type not in (u"files", "photos"):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
145 msg = u"unknown repository type: {repos_type}".format(repos_type=repos_type)
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
146 log.warning(msg)
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
147 raise exceptions.DateError(msg)
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
148 file_sharing_elt[u"type"] = repos_type
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
149 if namespace is not None:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
150 file_sharing_elt[u"namespace"] = namespace
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
151 if path is not None:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
152 file_sharing_elt[u"path"] = path
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
153 if name is not None:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
154 file_sharing_elt[u"name"] = name
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
155 return client.send(mess_data[u"xml"])
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
156
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
157 @defer.inlineCallbacks
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
158 def _parsePubsubElt(self, client, pubsub_elt):
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
159 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
160 service = jid.JID(pubsub_elt["service"])
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
161 node = pubsub_elt["node"]
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
162 item_id = pubsub_elt.getAttribute("item")
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
163 except (RuntimeError, KeyError):
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
164 log.warning(_(u"Bad invitation, ignoring"))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
165 raise exceptions.DataError
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
166
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
167 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
168 items, metadata = yield self._p.getItems(client, service, node,
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
169 item_ids=[item_id])
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
170 except Exception as e:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
171 log.warning(_(u"Can't get item linked with invitation: {reason}").format(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
172 reason=e))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
173 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
174 item_elt = items[0]
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
175 except IndexError:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
176 log.warning(_(u"Invitation was linking to a non existing item"))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
177 raise exceptions.DataError
2287
ea869f30f204 plugin events: added eventInvite command as a helper for the complex invitation workflow:
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
178
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
179 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
180 namespace = item_elt.firstChildElement().uri
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
181 except Exception as e:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
182 log.warning(_(u"Can't retrieve namespace of invitation: {reason}").format(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
183 reason = e))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
184 raise exceptions.DataError
2287
ea869f30f204 plugin events: added eventInvite command as a helper for the complex invitation workflow:
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
185
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
186 args = [service, node, item_id, item_elt]
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
187 defer.returnValue((namespace, args))
2287
ea869f30f204 plugin events: added eventInvite command as a helper for the complex invitation workflow:
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
188
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
189 def _parseFileSharingElt(self, client, file_sharing_elt):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
190 try:
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
191 service = jid.JID(file_sharing_elt["service"])
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
192 except (RuntimeError, KeyError):
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
193 log.warning(_(u"Bad invitation, ignoring"))
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
194 raise exceptions.DataError
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
195 repos_type = file_sharing_elt.getAttribute(u"type", u"files")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
196 namespace = file_sharing_elt.getAttribute(u"namespace")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
197 path = file_sharing_elt.getAttribute(u"path")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
198 name = file_sharing_elt.getAttribute(u"name")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
199 args = [service, repos_type, namespace, path, name]
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
200 ns_fis = self.host.getNamespace(u"fis")
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
201 return ns_fis, args
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
202
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
203 @defer.inlineCallbacks
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
204 def onInvitation(self, message_elt, client):
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
205 log.debug(u"invitation received [{profile}]".format(profile=client.profile))
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
206 invitation_elt = message_elt.invitation
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
207 for elt in invitation_elt.elements():
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
208 if elt.uri != NS_INVITATION:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
209 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml()))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
210 continue
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
211 if elt.name == u"pubsub":
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
212 method = self._parsePubsubElt
2913
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
213 elif elt.name == u"file_sharing":
672e6be3290f plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
Goffi <goffi@goffi.org>
parents: 2912
diff changeset
214 method = self._parseFileSharingElt
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
215 else:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
216 log.warning(u"not implemented invitation element: {xml}".format(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
217 xml = elt.toXml()))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
218 continue
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
219 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
220 namespace, args = yield method(client, elt)
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
221 except exceptions.DataError:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
222 log.warning(u"Can't parse invitation element: {xml}".format(
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
223 xml = elt.toXml()))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
224 continue
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
225
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
226 try:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
227 cb = self._ns_cb[namespace]
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
228 except KeyError:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
229 log.warning(_(u'No handler for namespace "{namespace}", invitation ignored')
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
230 .format(namespace=namespace))
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
231 else:
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
232 cb(client, *args)
2287
ea869f30f204 plugin events: added eventInvite command as a helper for the complex invitation workflow:
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
233
ea869f30f204 plugin events: added eventInvite command as a helper for the complex invitation workflow:
Goffi <goffi@goffi.org>
parents: 2272
diff changeset
234
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
235 class PubsubInvitationHandler(XMPPHandler):
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
236 implements(iwokkel.IDisco)
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
237
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
238 def __init__(self, plugin_parent):
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
239 self.plugin_parent = plugin_parent
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
240
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
241 def connectionInitialized(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
242 self.xmlstream.addObserver(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
243 INVITATION, self.plugin_parent.onInvitation, client=self.parent
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
244 )
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
245
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
246 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
247 return [
2912
a3faf1c86596 plugin events: refactored invitation and personal lists logic:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
248 disco.DiscoFeature(NS_INVITATION),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
249 ]
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
250
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2616
diff changeset
251 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
2616
1cc88adb5142 plugin events: invitations improvments + personal list
Goffi <goffi@goffi.org>
parents: 2612
diff changeset
252 return []