Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_identity.py @ 2971:dbf24ba40396 0.7.0b3
0.7.0b3 release
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 27 Jun 2019 20:27:34 +0200 |
parents | 003b8b4b56a7 |
children | ab2696e34d29 |
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 |
2771 | 5 # Copyright (C) 2009-2019 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_) |
2682
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
93 if nick: |
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
94 id_data[u"nick"] = nick |
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
95 elif jid_.user: |
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
96 id_data[u"nick"] = jid_.user.capitalize() |
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
97 else: |
466c9690c43a
plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
98 id_data[u"nick"] = jid_.userhost() |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
100 try: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
101 avatar_path = id_data[u"avatar"] = yield self._v.getAvatar( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
102 client, jid_, cache_only=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
103 ) |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
104 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
|
105 pass |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
106 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
|
107 if avatar_path: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 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
|
109 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
110 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
|
111 |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 defer.returnValue(id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 def _setIdentity(self, id_data, profile): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 client = self.host.getClient(profile) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 return self.setIdentity(client, id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 def setIdentity(self, client, id_data): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 """Update profile's identity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 @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
|
122 - nick: nickname |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 the vCard will be updated |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 """ |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 if id_data.keys() != [u"nick"]: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 raise NotImplementedError(u"Only nick can be updated for now") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
127 if u"nick" in id_data: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 return self._v.setNick(client, id_data[u"nick"]) |