annotate src/plugins/plugin_misc_identity.py @ 2320:f4a0723042ee

jp (pubsub/search): fixed encoding for stdin in external commands
author Goffi <goffi@goffi.org>
date Sun, 09 Jul 2017 15:20:31 +0200
parents db468f24b9fc
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
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
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core.log import getLogger
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 log = getLogger(__name__)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import defer
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import jid
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 PLUGIN_INFO = {
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 C.PI_NAME: "Identity Plugin",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 C.PI_IMPORT_NAME: "IDENTITY",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 C.PI_TYPE: C.PLUG_TYPE_MISC ,
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 C.PI_PROTOCOLS: [],
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 C.PI_DEPENDENCIES: ["XEP-0054"],
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 C.PI_RECOMMENDATIONS: [],
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 C.PI_MAIN: "Identity",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 C.PI_HANDLER: "no",
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 C.PI_DESCRIPTION: _("""Identity manager""")
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 }
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
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 class Identity(object):
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 def __init__(self, host):
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 log.info(_(u"Plugin Identity initialization"))
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 self.host = host
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 self._v = host.plugins[u'XEP-0054']
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 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
49 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
50
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 def _getIdentity(self, jid_str, profile):
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 jid_ = jid.JID(jid_str)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 client = self.host.getClient(profile)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 return self.getIdentity(client, jid_)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 @defer.inlineCallbacks
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def getIdentity(self, client, jid_):
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 """Retrieve identity of an entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
59
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 @param jid_(jid.JID): entity to check
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 @return (dict(unicode, unicode)): identity data where key can be:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 - nick: nickname of the entity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 nickname is checked from, in this order:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 roster, vCard, user part of jid
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 cache is used when possible
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 id_data = {}
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 # we first check roster
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 roster_item = yield client.roster.getItem(jid_.userhostJID)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 if roster_item is not None and roster_item.name:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 id_data[u'nick'] = roster_item.name
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 else:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 # then vcard
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 nick = yield self._v.getNick(client, jid_)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 id_data[u'nick'] = nick if nick else jid_.user
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
76
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 defer.returnValue(id_data)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 def _setIdentity(self, id_data, profile):
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 client = self.host.getClient(profile)
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 return self.setIdentity(client, id_data)
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 def setIdentity(self, client, id_data):
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """Update profile's identity
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 @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
87 - nick: nickname
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 the vCard will be updated
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 """
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 if id_data.keys() != [u'nick']:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 raise NotImplementedError(u'Only nick can be updated for now')
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 if u'nick' in id_data:
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return self._v.setNick(client, id_data[u'nick'])
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94