Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_identity.py @ 2641:b42aa52d2621 0.7.0a2
0.7.0a2 release
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 08 Jul 2018 18:38:29 +0200 |
parents | 56f94936df1e |
children | 466c9690c43a |
rev | line source |
---|---|
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for managing xep-0054 |
2483 | 5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # This program is free software: you can redistribute it and/or modify |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # it under the terms of the GNU Affero General Public License as published by |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # the Free Software Foundation, either version 3 of the License, or |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # (at your option) any later version. |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # This program is distributed in the hope that it will be useful, |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # GNU Affero General Public License for more details. |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # You should have received a copy of the GNU Affero General Public License |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.i18n import _ |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.constants import Const as C |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
23 from sat.core import exceptions |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
25 |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 log = getLogger(__name__) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.internet import defer |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.words.protocols.jabber import jid |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
29 import os.path |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 PLUGIN_INFO = { |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_NAME: "Identity Plugin", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_IMPORT_NAME: "IDENTITY", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
35 C.PI_TYPE: C.PLUG_TYPE_MISC, |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_PROTOCOLS: [], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_DEPENDENCIES: ["XEP-0054"], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_RECOMMENDATIONS: [], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_MAIN: "Identity", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_HANDLER: "no", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
41 C.PI_DESCRIPTION: _("""Identity manager"""), |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 } |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 class Identity(object): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 def __init__(self, host): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 log.info(_(u"Plugin Identity initialization")) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 self.host = host |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
49 self._v = host.plugins[u"XEP-0054"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
50 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
51 u"identityGet", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
52 u".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 in_sign=u"ss", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
54 out_sign=u"a{ss}", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
55 method=self._getIdentity, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
56 async=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
57 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 host.bridge.addMethod( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
59 u"identitySet", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
60 u".plugin", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
61 in_sign=u"a{ss}s", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
62 out_sign=u"", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
63 method=self._setIdentity, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
64 async=True, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
65 ) |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def _getIdentity(self, jid_str, profile): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 jid_ = jid.JID(jid_str) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 client = self.host.getClient(profile) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 return self.getIdentity(client, jid_) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 @defer.inlineCallbacks |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def getIdentity(self, client, jid_): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 """Retrieve identity of an entity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @param jid_(jid.JID): entity to check |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @return (dict(unicode, unicode)): identity data where key can be: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 - nick: nickname of the entity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 nickname is checked from, in this order: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 roster, vCard, user part of jid |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 cache is used when possible |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 """ |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 id_data = {} |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 # we first check roster |
2427
07478106a10d
plugin identity: fixed jid.userhostJID call + capitalize user when jid is used.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
85 roster_item = yield client.roster.getItem(jid_.userhostJID()) |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 if roster_item is not None and roster_item.name: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
87 id_data[u"nick"] = roster_item.name |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
88 elif jid_.resource and self._v.isRoom(client, jid_): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
89 id_data[u"nick"] = jid_.resource |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 # and finally then vcard |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 nick = yield self._v.getNick(client, jid_) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
93 id_data[u"nick"] = nick if nick else jid_.user.capitalize() |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
95 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 avatar_path = id_data[u"avatar"] = yield self._v.getAvatar( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 client, jid_, cache_only=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 ) |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
99 except exceptions.NotFound: |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
100 pass |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
101 else: |
2467
908be289eb49
plugin identity: if we get an empty avatar, don't feel avatar_basename and remove avatar key.
Goffi <goffi@goffi.org>
parents:
2463
diff
changeset
|
102 if avatar_path: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 id_data[u"avatar_basename"] = os.path.basename(avatar_path) |
2467
908be289eb49
plugin identity: if we get an empty avatar, don't feel avatar_basename and remove avatar key.
Goffi <goffi@goffi.org>
parents:
2463
diff
changeset
|
104 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
105 del id_data[u"avatar"] |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
106 |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 defer.returnValue(id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 def _setIdentity(self, id_data, profile): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 client = self.host.getClient(profile) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 return self.setIdentity(client, id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def setIdentity(self, client, id_data): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 """Update profile's identity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 @param id_data(dict[unicode, unicode]): data to update, key can be: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 - nick: nickname |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 the vCard will be updated |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 if id_data.keys() != [u"nick"]: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
121 raise NotImplementedError(u"Only nick can be updated for now") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 if u"nick" in id_data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 return self._v.setNick(client, id_data[u"nick"]) |