Mercurial > libervia-backend
annotate sat/plugins/plugin_misc_identity.py @ 2566:735467eb6cf7
core: replaced gtk3reactor by gireactor
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 03 Apr 2018 20:54:17 +0200 |
parents | 26edcf3a30eb |
children | 56f94936df1e |
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 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 log = getLogger(__name__) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.internet import defer |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 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
|
28 import os.path |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
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 PLUGIN_INFO = { |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_NAME: "Identity Plugin", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_IMPORT_NAME: "IDENTITY", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_TYPE: C.PLUG_TYPE_MISC , |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_PROTOCOLS: [], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_DEPENDENCIES: ["XEP-0054"], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_RECOMMENDATIONS: [], |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_MAIN: "Identity", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 C.PI_HANDLER: "no", |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 C.PI_DESCRIPTION: _("""Identity manager""") |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 } |
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 class Identity(object): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
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 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self._v = host.plugins[u'XEP-0054'] |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 host.bridge.addMethod(u"identityGet", u".plugin", in_sign=u'ss', out_sign=u'a{ss}', method=self._getIdentity, async=True) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 host.bridge.addMethod(u"identitySet", u".plugin", in_sign=u'a{ss}s', out_sign=u'', method=self._setIdentity, async=True) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 def _getIdentity(self, jid_str, profile): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 jid_ = jid.JID(jid_str) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 client = self.host.getClient(profile) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 return self.getIdentity(client, jid_) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 @defer.inlineCallbacks |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 def getIdentity(self, client, jid_): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 """Retrieve identity of an entity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 @param jid_(jid.JID): entity to check |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 @return (dict(unicode, unicode)): identity data where key can be: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 - nick: nickname of the entity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 nickname is checked from, in this order: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 roster, vCard, user part of jid |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 cache is used when possible |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 """ |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 id_data = {} |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 # 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
|
71 roster_item = yield client.roster.getItem(jid_.userhostJID()) |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 if roster_item is not None and roster_item.name: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 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
|
74 elif jid_.resource and self._v.isRoom(client, jid_): |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
75 id_data[u'nick'] = jid_.resource |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 else: |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
77 # and finally then vcard |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 nick = yield self._v.getNick(client, jid_) |
2427
07478106a10d
plugin identity: fixed jid.userhostJID call + capitalize user when jid is used.
Goffi <goffi@goffi.org>
parents:
2414
diff
changeset
|
79 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
|
80 |
2463
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
81 try: |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
82 avatar_path = id_data[u'avatar'] = yield self._v.getAvatar(client, jid_, cache_only=False) |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
83 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
|
84 pass |
65a6d2496504
plugin identity: return resource for MUC room occupants' nicks + added avatar and avatar_basename.
Goffi <goffi@goffi.org>
parents:
2427
diff
changeset
|
85 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
|
86 if avatar_path: |
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
|
87 id_data[u'avatar_basename'] = os.path.basename(avatar_path) |
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
|
88 else: |
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
|
89 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
|
90 |
2253
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 defer.returnValue(id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 def _setIdentity(self, id_data, profile): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 client = self.host.getClient(profile) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 return self.setIdentity(client, id_data) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 def setIdentity(self, client, id_data): |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 """Update profile's identity |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 @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
|
101 - nick: nickname |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 the vCard will be updated |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 """ |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 if id_data.keys() != [u'nick']: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 raise NotImplementedError(u'Only nick can be updated for now') |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 if u'nick' in id_data: |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 return self._v.setNick(client, id_data[u'nick']) |
db468f24b9fc
plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 |