annotate sat/plugins/plugin_misc_identity.py @ 3120:0c29155ac68b

core: backend autoconnection: A new Connection/autoconnect_backend param can be set for a profile or component to be started automatically with backend. This is specially useful for components, but can be useful for client profile too (e.g. on Android we need to start profile with backend to get notifications, this part will come with following commits). The new Sqlite.getIndParamValues method allows to retrieve the same parameters for all profiles.
author Goffi <goffi@goffi.org>
date Sat, 25 Jan 2020 21:08:32 +0100
parents ab2696e34d29
children 9d0df638c8b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
1 #!/usr/bin/env python3
2253
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
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2682
diff changeset
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):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
47 log.info(_("Plugin Identity initialization"))
2253
db468f24b9fc plugin identity: plugin identity first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 self.host = host
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
49 self._v = host.plugins["XEP-0054"]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 host.bridge.addMethod(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
51 "identityGet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
52 ".plugin",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
53 in_sign="ss",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
54 out_sign="a{ss}",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
55 method=self._getIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
56 async_=True,
2624
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(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
59 "identitySet",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
60 ".plugin",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
61 in_sign="a{ss}s",
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
62 out_sign="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 method=self._setIdentity,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
64 async_=True,
2624
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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
87 id_data["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_):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
89 id_data["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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
94 id_data["nick"] = nick
2682
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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
96 id_data["nick"] = jid_.user.capitalize()
2682
466c9690c43a plugin identity: fixed nick when there is no user part in entity
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
97 else:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
98 id_data["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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
101 avatar_path = id_data["avatar"] = yield self._v.getAvatar(
2624
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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
108 id_data["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:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
110 del id_data["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 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
125 if list(id_data.keys()) != ["nick"]:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
126 raise NotImplementedError("Only nick can be updated for now")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
127 if "nick" in id_data:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
128 return self._v.setNick(client, id_data["nick"])