comparison sat/plugins/plugin_misc_identity.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_misc_identity.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for managing xep-0054
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2014 Emmanuel Gil Peyrot (linkmauve@linkmauve.fr)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 from sat.core.i18n import _
22 from sat.core.constants import Const as C
23 from sat.core import exceptions
24 from sat.core.log import getLogger
25 log = getLogger(__name__)
26 from twisted.internet import defer
27 from twisted.words.protocols.jabber import jid
28 import os.path
29
30
31 PLUGIN_INFO = {
32 C.PI_NAME: "Identity Plugin",
33 C.PI_IMPORT_NAME: "IDENTITY",
34 C.PI_TYPE: C.PLUG_TYPE_MISC ,
35 C.PI_PROTOCOLS: [],
36 C.PI_DEPENDENCIES: ["XEP-0054"],
37 C.PI_RECOMMENDATIONS: [],
38 C.PI_MAIN: "Identity",
39 C.PI_HANDLER: "no",
40 C.PI_DESCRIPTION: _("""Identity manager""")
41 }
42
43
44 class Identity(object):
45
46 def __init__(self, host):
47 log.info(_(u"Plugin Identity initialization"))
48 self.host = host
49 self._v = host.plugins[u'XEP-0054']
50 host.bridge.addMethod(u"identityGet", u".plugin", in_sign=u'ss', out_sign=u'a{ss}', method=self._getIdentity, async=True)
51 host.bridge.addMethod(u"identitySet", u".plugin", in_sign=u'a{ss}s', out_sign=u'', method=self._setIdentity, async=True)
52
53 def _getIdentity(self, jid_str, profile):
54 jid_ = jid.JID(jid_str)
55 client = self.host.getClient(profile)
56 return self.getIdentity(client, jid_)
57
58 @defer.inlineCallbacks
59 def getIdentity(self, client, jid_):
60 """Retrieve identity of an entity
61
62 @param jid_(jid.JID): entity to check
63 @return (dict(unicode, unicode)): identity data where key can be:
64 - nick: nickname of the entity
65 nickname is checked from, in this order:
66 roster, vCard, user part of jid
67 cache is used when possible
68 """
69 id_data = {}
70 # we first check roster
71 roster_item = yield client.roster.getItem(jid_.userhostJID())
72 if roster_item is not None and roster_item.name:
73 id_data[u'nick'] = roster_item.name
74 elif jid_.resource and self._v.isRoom(client, jid_):
75 id_data[u'nick'] = jid_.resource
76 else:
77 # and finally then vcard
78 nick = yield self._v.getNick(client, jid_)
79 id_data[u'nick'] = nick if nick else jid_.user.capitalize()
80
81 try:
82 avatar_path = id_data[u'avatar'] = yield self._v.getAvatar(client, jid_, cache_only=False)
83 except exceptions.NotFound:
84 pass
85 else:
86 if avatar_path:
87 id_data[u'avatar_basename'] = os.path.basename(avatar_path)
88 else:
89 del id_data[u'avatar']
90
91 defer.returnValue(id_data)
92
93 def _setIdentity(self, id_data, profile):
94 client = self.host.getClient(profile)
95 return self.setIdentity(client, id_data)
96
97 def setIdentity(self, client, id_data):
98 """Update profile's identity
99
100 @param id_data(dict[unicode, unicode]): data to update, key can be:
101 - nick: nickname
102 the vCard will be updated
103 """
104 if id_data.keys() != [u'nick']:
105 raise NotImplementedError(u'Only nick can be updated for now')
106 if u'nick' in id_data:
107 return self._v.setNick(client, id_data[u'nick'])
108