Mercurial > libervia-backend
annotate src/plugins/plugin_misc_invitations.py @ 2289:f8276bd8baf6
plugin XEP-0277: max_items fix (last "fix" about it was actually bad)
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 29 Jun 2017 23:40:12 +0200 |
parents | 61e836cc9512 |
children | c05000d00dbb |
rev | line source |
---|---|
2184 | 1 #!/usr/bin/env python2 |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # SAT plugin for file tansfer | |
5 # Copyright (C) 2009-2016 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 _, D_ | |
21 from sat.core.constants import Const as C | |
22 from sat.core import exceptions | |
23 from sat.core.log import getLogger | |
24 log = getLogger(__name__) | |
25 import shortuuid | |
26 from sat.tools import utils | |
27 from twisted.internet import defer | |
28 from twisted.words.protocols.jabber import jid | |
29 from twisted.words.protocols.jabber import error | |
30 from sat.memory import persistent | |
31 from sat.tools import email as sat_email | |
32 | |
33 | |
34 PLUGIN_INFO = { | |
35 C.PI_NAME: "Invitations", | |
36 C.PI_IMPORT_NAME: "INVITATIONS", | |
37 C.PI_TYPE: C.PLUG_TYPE_MISC, | |
38 C.PI_DEPENDENCIES: ['XEP-0077'], | |
2256
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
39 C.PI_RECOMMENDATIONS: ["IDENTITY"], |
2184 | 40 C.PI_MAIN: "InvitationsPlugin", |
41 C.PI_HANDLER: "no", | |
42 C.PI_DESCRIPTION: _(u"""invitation of people without XMPP account""") | |
43 } | |
44 | |
45 | |
46 SUFFIX_MAX = 5 | |
47 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
|
48 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
|
49 KEY_JID = u'jid' |
2184 | 50 KEY_CREATED = u'created' |
51 KEY_LAST_CONNECTION = u'last_connection' | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
52 KEY_GUEST_PROFILE = u'guest_profile' |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
53 KEY_PASSWORD = u'password' |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
54 EXTRA_RESERVED = {KEY_ID, KEY_JID, KEY_CREATED, u'jid_', u'jid', KEY_LAST_CONNECTION, KEY_GUEST_PROFILE, KEY_PASSWORD} |
2184 | 55 DEFAULT_SUBJECT = D_(u"You have been invited by {host_name} to {app_name}") |
56 DEFAULT_BODY = D_(u"""Hello {name}! | |
57 | |
58 You have received an invitation from {host_name} to participate to "{app_name}". | |
59 To join, you just have to click on the following URL: | |
60 {url} | |
61 | |
62 Please note that this URL should not be shared with anybody! | |
63 If you want more details on {app_name}, you can check {app_url}. | |
64 | |
65 Welcome! | |
66 """) | |
67 | |
68 | |
69 class InvitationsPlugin(object): | |
70 | |
71 def __init__(self, host): | |
72 log.info(_(u"plugin Invitations initialization")) | |
73 self.host = host | |
74 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
|
75 host.bridge.addMethod("invitationCreate", ".plugin", in_sign='sssssssssa{ss}s', out_sign='a{ss}', |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
76 method=self._create, |
2184 | 77 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
|
78 host.bridge.addMethod("invitationGet", ".plugin", in_sign='s', out_sign='a{ss}', |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
79 method=self.get, |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
80 async=True) |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
81 host.bridge.addMethod("invitationModify", ".plugin", in_sign='sa{ss}b', out_sign='', |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
82 method=self._modify, |
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
|
83 async=True) |
2230
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
84 host.bridge.addMethod("invitationList", ".plugin", in_sign='s', out_sign='a{sa{ss}}', |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
85 method=self._list, |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
86 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
|
87 |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
88 def checkExtra(self, extra): |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
89 if EXTRA_RESERVED.intersection(extra): |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
90 raise ValueError(_(u"You can't use following key(s) in extra, they are reserved: {}").format( |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
91 u', '.join(EXTRA_RESERVED.intersection(extra)))) |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
92 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
93 def _create(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''): |
2184 | 94 # XXX: we don't use **kwargs here to keep arguments name for introspection with D-Bus bridge |
95 | |
96 if extra is None: | |
97 extra = {} | |
98 else: | |
99 extra = {unicode(k): unicode(v) for k,v in extra.iteritems()} | |
100 | |
101 # we need to be sure that values are unicode, else they won't be pickled correctly with D-Bus | |
102 kwargs = {"extra": extra} | |
103 for key in ("jid_", "password", "name", "host_name", "email", "language", "url_template", "message_subject", "message_body", "profile"): | |
104 value = locals()[key] | |
105 if value: | |
106 kwargs[key] = unicode(value) | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
107 d = self.create(**kwargs) |
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
|
108 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
|
109 data[KEY_JID] = data[KEY_JID].full() |
2230
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
110 return data |
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
|
111 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
|
112 return d |
2184 | 113 |
114 @defer.inlineCallbacks | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
115 def create(self, **kwargs): |
2184 | 116 ur"""create an invitation |
117 | |
118 this will create an XMPP account and a profile, and use a UUID to retrieve them. | |
119 the profile is automatically generated in the form guest@@[UUID], this way they can be retrieved easily | |
120 **kwargs: keywords arguments which can have the following keys, unset values are equivalent to None: | |
121 jid_(jid.JID, None): jid to use for invitation, the jid will be created using XEP-0077 | |
122 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
|
123 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
|
124 same as the invitation one, as jid can be used publicly (leaking the UUID), and invitation UUID give access to account. |
2184 | 125 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) |
126 password(unicode, None): password to use (will be used for XMPP account and profile) | |
127 None to automatically generate one | |
128 name(unicode, None): name of the invitee | |
2256
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
129 will be set as profile identity if present |
2184 | 130 host_name(unicode, None): name of the host |
131 email(unicode, None): email to send the invitation to | |
132 if None, no invitation email is sent, you can still associate email using extra | |
133 if email is used, extra can't have "email" key | |
134 language(unicode): language of the invitee (used notabily to translate the invitation) | |
135 TODO: not used yet | |
136 url_template(unicode, None): template to use to construct the invitation URL | |
137 use {uuid} as a placeholder for identifier | |
138 use None if you don't want to include URL (or if it is already specified in custom message) | |
139 /!\ you must put full URL, don't forget https:// | |
140 /!\ 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
|
141 message_subject(unicode, None): customised message body for the invitation email |
2184 | 142 None to use default subject |
143 uses the same substitution as for message_body | |
144 message_body(unicode, None): customised message body for the invitation email | |
145 None to use default body | |
146 use {name} as a place holder for invitee name | |
147 use {url} as a placeholder for the invitation url | |
148 use {uuid} as a placeholder for the identifier | |
149 use {app_name} as a placeholder for this software name | |
150 use {app_url} as a placeholder for this software official website | |
151 use {profile} as a placeholder for host's profile | |
152 use {host_name} as a placeholder for host's name | |
153 extra(dict, None): extra data to associate with the invitee | |
154 some keys are reserved: | |
155 - created (creation date) | |
156 if email argument is used, "email" key can't be used | |
157 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
|
158 @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
|
159 - UUID associated with the invitee (key: id) |
2184 | 160 - filled extra dictionary, as saved in the databae |
161 """ | |
162 ## initial checks | |
163 extra = kwargs.pop('extra', {}) | |
164 if set(kwargs).intersection(extra): | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
165 raise ValueError(_(u"You can't use following key(s) in both args and extra: {}").format( |
2184 | 166 u', '.join(set(kwargs).intersection(extra)))) |
167 | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
168 self.checkExtra(extra) |
2184 | 169 |
2255
ba613d32ca60
plugin invitations: doesn't raise an exception anymore if url_template is not set while email is not requested
Goffi <goffi@goffi.org>
parents:
2241
diff
changeset
|
170 email = kwargs.pop(u'email', None) |
ba613d32ca60
plugin invitations: doesn't raise an exception anymore if url_template is not set while email is not requested
Goffi <goffi@goffi.org>
parents:
2241
diff
changeset
|
171 |
ba613d32ca60
plugin invitations: doesn't raise an exception anymore if url_template is not set while email is not requested
Goffi <goffi@goffi.org>
parents:
2241
diff
changeset
|
172 if email is not None and not 'url_template' in kwargs and not 'message_body' in kwargs: |
2210
f8d61592f1fc
plugin invitations: raise ValueError if url_template and message_body are both not specified
Goffi <goffi@goffi.org>
parents:
2208
diff
changeset
|
173 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
|
174 |
2184 | 175 ## uuid |
176 log.info(_(u"creating an invitation")) | |
177 id_ = unicode(shortuuid.uuid()) | |
178 | |
179 ## XMPP account creation | |
180 password = kwargs.pop(u'password', None) | |
181 if password is None: | |
182 password = utils.generatePassword() | |
183 assert password | |
184 # XXX: password is here saved in clear in database | |
185 # it is needed for invitation as the same password is used for profile | |
186 # and SàT need to be able to automatically open the profile with the uuid | |
187 # FIXME: we could add an extra encryption key which would be used with the uuid | |
188 # when the invitee is connecting (e.g. with URL). This key would not be saved | |
189 # and could be used to encrypt profile password. | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
190 extra[KEY_PASSWORD] = password |
2184 | 191 |
192 jid_ = kwargs.pop(u'jid_', None) | |
193 if not jid_: | |
194 domain = self.host.memory.getConfig(None, 'xmpp_domain') | |
195 if not domain: | |
196 # TODO: fallback to profile's domain | |
197 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
|
198 jid_ = u"invitation-{uuid}@{domain}".format(uuid=shortuuid.uuid(), domain=domain) |
2184 | 199 jid_ = jid.JID(jid_) |
200 if jid_.user: | |
201 # we don't register account if there is no user as anonymous login is then used | |
202 try: | |
203 yield self.host.plugins['XEP-0077'].registerNewAccount(jid_, password) | |
204 except error.StanzaError as e: | |
205 prefix = jid_.user | |
206 idx = 0 | |
207 while e.condition == u'conflict': | |
208 if idx >= SUFFIX_MAX: | |
209 raise exceptions.ConflictError(_(u"Can't create XMPP account")) | |
210 jid_.user = prefix + '_' + unicode(idx) | |
211 log.info(_(u"requested jid already exists, trying with {}".format(jid_.full()))) | |
212 try: | |
213 yield self.host.plugins['XEP-0077'].registerNewAccount(jid_, password) | |
214 except error.StanzaError as e: | |
215 idx += 1 | |
216 else: | |
217 break | |
218 if e.condition != u'conflict': | |
219 raise e | |
220 | |
221 log.info(_(u"account {jid_} created").format(jid_=jid_.full())) | |
222 | |
223 ## profile creation | |
2256
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
224 |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
225 extra[KEY_GUEST_PROFILE] = guest_profile = INVITEE_PROFILE_TPL.format(uuid=id_) |
2184 | 226 # profile creation should not fail as we generate unique name ourselves |
227 yield self.host.memory.createProfile(guest_profile, password) | |
228 yield self.host.memory.startSession(password, guest_profile) | |
229 yield self.host.memory.setParam("JabberID", jid_.full(), "Connection", profile_key=guest_profile) | |
230 yield self.host.memory.setParam("Password", password, "Connection", profile_key=guest_profile) | |
2256
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
231 name = kwargs.pop(u'name', None) |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
232 if name is not None: |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
233 extra[u'name'] = name |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
234 try: |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
235 id_plugin = self.host.plugins[u'IDENTITY'] |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
236 except KeyError: |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
237 pass |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
238 else: |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
239 yield self.host.connect(guest_profile, password) |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
240 guest_client = self.host.getClient(guest_profile) |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
241 yield id_plugin.setIdentity(guest_client, {u'nick': name}) |
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
242 yield self.host.disconnect(guest_profile) |
2184 | 243 |
244 ## email | |
245 language = kwargs.pop(u'language', None) | |
246 if language is not None: | |
247 extra[u'language'] = language | |
248 | |
249 if email is not None: | |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
250 extra[u'email'] = email |
2184 | 251 url_template = kwargs.pop(u'url_template', '') |
252 format_args = { | |
253 u'uuid': id_, | |
254 u'app_name': C.APP_NAME, | |
255 u'app_url': C.APP_URL} | |
256 | |
257 if name is None: | |
258 format_args[u'name'] = email | |
259 else: | |
2256
61e836cc9512
plugin invitations: name is now registered as nickname on XMPP server thanks to plugin identity
Goffi <goffi@goffi.org>
parents:
2255
diff
changeset
|
260 format_args[u'name'] = name |
2184 | 261 |
262 profile = kwargs.pop(u'profile', None) | |
263 if profile is None: | |
264 format_args[u'profile'] = u'' | |
265 else: | |
266 format_args[u'profile'] = extra[u'profile'] = profile | |
267 | |
268 host_name = kwargs.pop(u'host_name', None) | |
269 if host_name is None: | |
270 format_args[u'host_name'] = profile or _(u"somebody") | |
271 else: | |
272 format_args[u'host_name'] = extra[u'host_name'] = host_name | |
273 | |
274 invite_url = url_template.format(**format_args) | |
275 format_args[u'url'] = invite_url | |
276 | |
277 yield sat_email.sendEmail( | |
278 self.host, | |
279 [email], | |
280 (kwargs.pop(u'message_subject', None) or DEFAULT_SUBJECT).format(**format_args), | |
281 (kwargs.pop(u'message_body', None) or DEFAULT_BODY).format(**format_args), | |
282 ) | |
283 | |
284 ## extra data saving | |
285 self.invitations[id_] = extra | |
286 | |
287 if kwargs: | |
288 log.warning(_(u"Not all arguments have been consumed: {}").format(kwargs)) | |
289 | |
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
|
290 extra[KEY_ID] = id_ |
2230
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
291 extra[KEY_JID] = jid_ |
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
|
292 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
|
293 |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
294 def get(self, id_): |
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
|
295 """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
|
296 |
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
|
297 @param id_(unicode): UUID linked to an invitation |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
298 @return (dict[unicode, unicode]): data associated to the invitation |
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
|
299 @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
|
300 """ |
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
|
301 return self.invitations[id_] |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
302 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
303 def _modify(self, id_, new_extra, replace): |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
304 return self.modify(id_, {unicode(k): unicode(v) for k,v in new_extra.iteritems()}, replace) |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
305 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
306 def modify(self, id_, new_extra, replace=False): |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
307 """Modify invitation data |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
308 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
309 @param id_(unicode): UUID linked to an invitation |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
310 @param new_extra(dict[unicode, unicode]): data to update |
2241
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
311 empty values will be deleted if replace is True |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
312 @param replace(bool): if True replace the data |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
313 else update them |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
314 @raise KeyError: there is not invitation with this id_ |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
315 """ |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
316 self.checkExtra(new_extra) |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
317 def gotCurrentData(current_data): |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
318 if replace: |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
319 new_data = new_extra |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
320 for k in EXTRA_RESERVED: |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
321 try: |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
322 new_data[k] = current_data[k] |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
323 except KeyError: |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
324 continue |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
325 else: |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
326 new_data = current_data |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
327 for k,v in new_extra.iteritems(): |
2241
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
328 if k in EXTRA_RESERVED: |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
329 log.warning(_(u"Skipping reserved key {key}".format(k))) |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
330 continue |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
331 if v: |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
332 new_data[k] = v |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
333 else: |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
334 try: |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
335 del new_data[k] |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
336 except KeyError: |
f87b673c7d17
plugin invitations: on modify an empty value delete corresponding extra keys, and reserved keys are now ignored.
Goffi <goffi@goffi.org>
parents:
2230
diff
changeset
|
337 pass |
2219
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
338 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
339 self.invitations[id_] = new_data |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
340 |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
341 d = self.invitations[id_] |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
342 d.addCallback(gotCurrentData) |
77a3d0a28642
plugin invitations: added modify method (+ bridge) and fixed email setting in extra
Goffi <goffi@goffi.org>
parents:
2212
diff
changeset
|
343 return d |
2230
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
344 |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
345 def _list(self, profile=C.PROF_KEY_NONE): |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
346 return self.list(profile) |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
347 |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
348 @defer.inlineCallbacks |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
349 def list(self, profile=C.PROF_KEY_NONE): |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
350 """List invitations |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
351 |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
352 @param profile(unicode): return invitation linked to this profile only |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
353 C.PROF_KEY_NONE: don't filter invitations |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
354 @return list(unicode): invitations uids |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
355 """ |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
356 invitations = yield self.invitations.items() |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
357 if profile != C.PROF_KEY_NONE: |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
358 invitations = {id_:data for id_, data in invitations.iteritems() if data.get(u'profile') == profile} |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
359 |
ebc0c1701811
plugin invitations: added invitationList command
Goffi <goffi@goffi.org>
parents:
2223
diff
changeset
|
360 defer.returnValue(invitations) |