comparison src/plugins/plugin_misc_identity.py @ 2253:db468f24b9fc

plugin identity: plugin identity first draft: this new plugin is a high level way to manage profile or entity identities, i.e. informations about them. It has been created to handle easily data which can be in several location (e.g. roster, vcard-tmp, vcard4, etc.) For now, only nickname can be gotten/set (set only works for profile's own identity, while get can be used on any jid).
author Goffi <goffi@goffi.org>
date Sun, 21 May 2017 20:04:57 +0200
parents
children 8b37a62336c3
comparison
equal deleted inserted replaced
2252:cffa50c9f26b 2253:db468f24b9fc
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for managing xep-0054
5 # Copyright (C) 2009-2016 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.log import getLogger
24 log = getLogger(__name__)
25 from twisted.internet import defer
26 from twisted.words.protocols.jabber import jid
27
28
29 PLUGIN_INFO = {
30 C.PI_NAME: "Identity Plugin",
31 C.PI_IMPORT_NAME: "IDENTITY",
32 C.PI_TYPE: C.PLUG_TYPE_MISC ,
33 C.PI_PROTOCOLS: [],
34 C.PI_DEPENDENCIES: ["XEP-0054"],
35 C.PI_RECOMMENDATIONS: [],
36 C.PI_MAIN: "Identity",
37 C.PI_HANDLER: "no",
38 C.PI_DESCRIPTION: _("""Identity manager""")
39 }
40
41
42 class Identity(object):
43
44 def __init__(self, host):
45 log.info(_(u"Plugin Identity initialization"))
46 self.host = host
47 self._v = host.plugins[u'XEP-0054']
48 host.bridge.addMethod(u"identityGet", u".plugin", in_sign=u'ss', out_sign=u'a{ss}', method=self._getIdentity, async=True)
49 host.bridge.addMethod(u"identitySet", u".plugin", in_sign=u'a{ss}s', out_sign=u'', method=self._setIdentity, async=True)
50
51 def _getIdentity(self, jid_str, profile):
52 jid_ = jid.JID(jid_str)
53 client = self.host.getClient(profile)
54 return self.getIdentity(client, jid_)
55
56 @defer.inlineCallbacks
57 def getIdentity(self, client, jid_):
58 """Retrieve identity of an entity
59
60 @param jid_(jid.JID): entity to check
61 @return (dict(unicode, unicode)): identity data where key can be:
62 - nick: nickname of the entity
63 nickname is checked from, in this order:
64 roster, vCard, user part of jid
65 cache is used when possible
66 """
67 id_data = {}
68 # we first check roster
69 roster_item = yield client.roster.getItem(jid_.userhostJID)
70 if roster_item is not None and roster_item.name:
71 id_data[u'nick'] = roster_item.name
72 else:
73 # then vcard
74 nick = yield self._v.getNick(client, jid_)
75 id_data[u'nick'] = nick if nick else jid_.user
76
77 defer.returnValue(id_data)
78
79 def _setIdentity(self, id_data, profile):
80 client = self.host.getClient(profile)
81 return self.setIdentity(client, id_data)
82
83 def setIdentity(self, client, id_data):
84 """Update profile's identity
85
86 @param id_data(dict[unicode, unicode]): data to update, key can be:
87 - nick: nickname
88 the vCard will be updated
89 """
90 if id_data.keys() != [u'nick']:
91 raise NotImplementedError(u'Only nick can be updated for now')
92 if u'nick' in id_data:
93 return self._v.setNick(client, id_data[u'nick'])
94