annotate src/plugins/plugin_misc_invitations.py @ 2212:eaf2467d19ce

plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
author Goffi <goffi@goffi.org>
date Wed, 29 Mar 2017 19:42:42 +0200
parents df115e4a36c7
children 77a3d0a28642
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for file tansfer
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.core.i18n import _, D_
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from sat.core.constants import Const as C
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from sat.core import exceptions
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 import shortuuid
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from sat.tools import utils
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.internet import defer
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.words.protocols.jabber import jid
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from twisted.words.protocols.jabber import error
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.memory import persistent
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.tools import email as sat_email
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 PLUGIN_INFO = {
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_NAME: "Invitations",
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_IMPORT_NAME: "INVITATIONS",
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_TYPE: C.PLUG_TYPE_MISC,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DEPENDENCIES: ['XEP-0077'],
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 C.PI_MAIN: "InvitationsPlugin",
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 C.PI_HANDLER: "no",
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 C.PI_DESCRIPTION: _(u"""invitation of people without XMPP account""")
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 }
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
44
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 SUFFIX_MAX = 5
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 INVITEE_PROFILE_TPL = u"guest@@{uuid}"
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
47 KEY_ID = u'id'
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
48 KEY_JID = u'jid'
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 KEY_CREATED = u'created'
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 KEY_LAST_CONNECTION = u'last_connection'
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
51 EXTRA_RESERVED = {KEY_ID, KEY_JID, KEY_CREATED, u'jid_', u'jid', KEY_LAST_CONNECTION}
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 DEFAULT_SUBJECT = D_(u"You have been invited by {host_name} to {app_name}")
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 DEFAULT_BODY = D_(u"""Hello {name}!
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 You have received an invitation from {host_name} to participate to "{app_name}".
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 To join, you just have to click on the following URL:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 {url}
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 Please note that this URL should not be shared with anybody!
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 If you want more details on {app_name}, you can check {app_url}.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 Welcome!
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 class InvitationsPlugin(object):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 def __init__(self, host):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 log.info(_(u"plugin Invitations initialization"))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 self.host = host
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.invitations = persistent.LazyPersistentBinaryDict(u'invitations')
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
72 host.bridge.addMethod("invitationCreate", ".plugin", in_sign='sssssssssa{ss}s', out_sign='a{ss}',
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 method=self._createInvitation,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 async=True)
2212
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
75 host.bridge.addMethod("invitationGet", ".plugin", in_sign='s', out_sign='a{ss}',
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
76 method=self.getInvitation,
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
77 async=True)
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
78
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def _createInvitation(self, jid_=u'', password=u'', name=u'', host_name=u'', email=u'', language=u'', url_template=u'', message_subject=u'', message_body=u'', extra=None, profile=u''):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 # XXX: we don't use **kwargs here to keep arguments name for introspection with D-Bus bridge
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if extra is None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 extra = {}
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 else:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 extra = {unicode(k): unicode(v) for k,v in extra.iteritems()}
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 # we need to be sure that values are unicode, else they won't be pickled correctly with D-Bus
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 kwargs = {"extra": extra}
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 for key in ("jid_", "password", "name", "host_name", "email", "language", "url_template", "message_subject", "message_body", "profile"):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 value = locals()[key]
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 if value:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 kwargs[key] = unicode(value)
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
93 d = self.createInvitation(**kwargs)
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
94 def serialize(data):
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
95 data[KEY_JID] = data[KEY_JID].full()
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
96 d.addCallback(serialize)
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
97 return d
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 @defer.inlineCallbacks
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 def createInvitation(self, **kwargs):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 ur"""create an invitation
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
102
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 this will create an XMPP account and a profile, and use a UUID to retrieve them.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 the profile is automatically generated in the form guest@@[UUID], this way they can be retrieved easily
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 **kwargs: keywords arguments which can have the following keys, unset values are equivalent to None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 jid_(jid.JID, None): jid to use for invitation, the jid will be created using XEP-0077
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 if the jid has no user part, an anonymous account will be used (no XMPP account created in this case)
2208
c316c6f6a737 plugin invitations: fixed leak of uuid in jid
Goffi <goffi@goffi.org>
parents: 2185
diff changeset
108 if None, automatically generate an account name (in the form "invitation-[random UUID]@domain.tld") (note that this UUID is not the
c316c6f6a737 plugin invitations: fixed leak of uuid in jid
Goffi <goffi@goffi.org>
parents: 2185
diff changeset
109 same as the invitation one, as jid can be used publicly (leaking the UUID), and invitation UUID give access to account.
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 in case of conflict, a suffix number is added to the account until a free one if found (with a failure if SUFFIX_MAX is reached)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 password(unicode, None): password to use (will be used for XMPP account and profile)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 None to automatically generate one
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 name(unicode, None): name of the invitee
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 host_name(unicode, None): name of the host
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 email(unicode, None): email to send the invitation to
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if None, no invitation email is sent, you can still associate email using extra
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 if email is used, extra can't have "email" key
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 language(unicode): language of the invitee (used notabily to translate the invitation)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 TODO: not used yet
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 url_template(unicode, None): template to use to construct the invitation URL
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 use {uuid} as a placeholder for identifier
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 use None if you don't want to include URL (or if it is already specified in custom message)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 /!\ you must put full URL, don't forget https://
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 /!\ the URL will give access to the invitee account, you should warn in message to not publish it publicly
2210
f8d61592f1fc plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents: 2208
diff changeset
125 message_subject(unicode, None): customised message body for the invitation email
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 None to use default subject
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 uses the same substitution as for message_body
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 message_body(unicode, None): customised message body for the invitation email
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 None to use default body
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 use {name} as a place holder for invitee name
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 use {url} as a placeholder for the invitation url
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 use {uuid} as a placeholder for the identifier
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 use {app_name} as a placeholder for this software name
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 use {app_url} as a placeholder for this software official website
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 use {profile} as a placeholder for host's profile
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 use {host_name} as a placeholder for host's name
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 extra(dict, None): extra data to associate with the invitee
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 some keys are reserved:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 - created (creation date)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 if email argument is used, "email" key can't be used
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 profile(unicode, None): profile of the host (person who is inviting)
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
142 @return (dict[unicode, unicode]): dictionary with:
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
143 - UUID associated with the invitee (key: id)
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 - filled extra dictionary, as saved in the databae
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 """
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 ## initial checks
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 extra = kwargs.pop('extra', {})
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 if set(kwargs).intersection(extra):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 raise exceptions.ValueError(_(u"You can't use following key(s) in both args and extra: {}").format(
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 u', '.join(set(kwargs).intersection(extra))))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 if EXTRA_RESERVED.intersection(extra):
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 raise exceptions.ValueError(_(u"You can't use following key(s) in extra, they are reserved: {}").format(
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 u', '.join(EXTRA_RESERVED.intersection(extra))))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
155
2210
f8d61592f1fc plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents: 2208
diff changeset
156 if not 'url_template' in extra and not 'message_body' in extra:
f8d61592f1fc plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents: 2208
diff changeset
157 raise ValueError(_(u"You need to provide url_template if you use default message body"))
f8d61592f1fc plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents: 2208
diff changeset
158
f8d61592f1fc plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents: 2208
diff changeset
159
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 ## uuid
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 log.info(_(u"creating an invitation"))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 id_ = unicode(shortuuid.uuid())
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 ## XMPP account creation
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 password = kwargs.pop(u'password', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 if password is None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 password = utils.generatePassword()
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
168 assert password
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 # XXX: password is here saved in clear in database
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
170 # it is needed for invitation as the same password is used for profile
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 # and SàT need to be able to automatically open the profile with the uuid
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 # FIXME: we could add an extra encryption key which would be used with the uuid
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 # when the invitee is connecting (e.g. with URL). This key would not be saved
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 # and could be used to encrypt profile password.
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 extra[u'password'] = password
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 jid_ = kwargs.pop(u'jid_', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 if not jid_:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 domain = self.host.memory.getConfig(None, 'xmpp_domain')
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 if not domain:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
181 # TODO: fallback to profile's domain
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
182 raise ValueError(_(u"You need to specify xmpp_domain in sat.conf"))
2208
c316c6f6a737 plugin invitations: fixed leak of uuid in jid
Goffi <goffi@goffi.org>
parents: 2185
diff changeset
183 jid_ = u"invitation-{uuid}@{domain}".format(uuid=shortuuid.uuid(), domain=domain)
2184
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
184 jid_ = jid.JID(jid_)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
185 if jid_.user:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
186 # we don't register account if there is no user as anonymous login is then used
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
187 try:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
188 yield self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
189 except error.StanzaError as e:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
190 prefix = jid_.user
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
191 idx = 0
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
192 while e.condition == u'conflict':
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
193 if idx >= SUFFIX_MAX:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
194 raise exceptions.ConflictError(_(u"Can't create XMPP account"))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
195 jid_.user = prefix + '_' + unicode(idx)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
196 log.info(_(u"requested jid already exists, trying with {}".format(jid_.full())))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 try:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
198 yield self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
199 except error.StanzaError as e:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
200 idx += 1
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
201 else:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
202 break
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
203 if e.condition != u'conflict':
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
204 raise e
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
206 log.info(_(u"account {jid_} created").format(jid_=jid_.full()))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
207
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
208 ## profile creation
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
209 extra['guest_profile'] = guest_profile = INVITEE_PROFILE_TPL.format(uuid=id_)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
210 # profile creation should not fail as we generate unique name ourselves
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
211 yield self.host.memory.createProfile(guest_profile, password)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
212 yield self.host.memory.startSession(password, guest_profile)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
213 yield self.host.memory.setParam("JabberID", jid_.full(), "Connection", profile_key=guest_profile)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
214 yield self.host.memory.setParam("Password", password, "Connection", profile_key=guest_profile)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 ## email
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 language = kwargs.pop(u'language', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
218 if language is not None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
219 extra[u'language'] = language
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
220 email = kwargs.pop(u'email', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222 if email is not None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 url_template = kwargs.pop(u'url_template', '')
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224 format_args = {
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
225 u'uuid': id_,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
226 u'app_name': C.APP_NAME,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
227 u'app_url': C.APP_URL}
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
228
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
229 name = kwargs.pop(u'name', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
230 if name is None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
231 format_args[u'name'] = email
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
232 else:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
233 format_args[u'name'] = extra[u'name'] = name
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
234
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
235 profile = kwargs.pop(u'profile', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
236 if profile is None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
237 format_args[u'profile'] = u''
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238 else:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 format_args[u'profile'] = extra[u'profile'] = profile
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
240
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
241 host_name = kwargs.pop(u'host_name', None)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
242 if host_name is None:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
243 format_args[u'host_name'] = profile or _(u"somebody")
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 else:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 format_args[u'host_name'] = extra[u'host_name'] = host_name
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
246
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
247 invite_url = url_template.format(**format_args)
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
248 format_args[u'url'] = invite_url
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
249
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 yield sat_email.sendEmail(
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251 self.host,
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 [email],
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
253 (kwargs.pop(u'message_subject', None) or DEFAULT_SUBJECT).format(**format_args),
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
254 (kwargs.pop(u'message_body', None) or DEFAULT_BODY).format(**format_args),
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
255 )
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
257 ## extra data saving
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
258 self.invitations[id_] = extra
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 if kwargs:
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 log.warning(_(u"Not all arguments have been consumed: {}").format(kwargs))
e0f91efa404a plugin invitations: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
262
2211
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
263 extra[KEY_ID] = id_
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
264 extra[KEY_JID] = jid
df115e4a36c7 plugin invitations: invitation id and invitee jid are now added to return dict in invitationCreate, bridge signature has changed too
Goffi <goffi@goffi.org>
parents: 2210
diff changeset
265 defer.returnValue(extra)
2212
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
266
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
267 def getInvitation(self, id_):
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
268 """Retrieve invitation linked to uuid if it exists
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
269
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
270 @param id_(unicode): UUID linked to an invitation
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
271 @return dict(unicode, unicode): data associated to the invitation
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
272 @raise KeyError: there is not invitation with this id_
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
273 """
eaf2467d19ce plugin invitations: added getInvitation method, it return invitation data and raise an error if it is not found
Goffi <goffi@goffi.org>
parents: 2211
diff changeset
274 return self.invitations[id_]