2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
# Travis Shirk <travis AT pobox.com>
|
|
|
|
# Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
# Copyright (C) 2007 Lukas Petrovicky <lukas AT petrovicky.net>
|
|
|
|
# Tomasz Melcer <liori AT exroot.org>
|
|
|
|
# Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
# Copyright (C) 2007-2008 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
# Copyright (C) 2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
# Jonathan Schleifer <js-gajim AT webkeks.org>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
Merged in trunk updates, including meta_contacts
Merged revisions 4951,4962-4969 via svnmerge from
svn://svn.gajim.org/gajim/trunk
........
r4951 | nk | 2005-12-30 16:50:36 -0700 (Fri, 30 Dec 2005) | 1 line
fixes in greek transl
........
r4962 | asterix | 2006-01-01 11:41:04 -0700 (Sun, 01 Jan 2006) | 2 lines
merge meta_contacts branch with trunk. Meta contacts are not in gajim yet, but framework is here. We now use gajim.contacts.many_functions() to handle contacts and groupchat_contacts.
........
r4963 | asterix | 2006-01-01 11:43:24 -0700 (Sun, 01 Jan 2006) | 2 lines
correct contacts file
........
r4964 | asterix | 2006-01-01 11:47:26 -0700 (Sun, 01 Jan 2006) | 2 lines
dict.remove() doesn't exists, it's del dict[]
........
r4965 | asterix | 2006-01-01 11:50:15 -0700 (Sun, 01 Jan 2006) | 2 lines
some missing commits from branch
........
r4966 | asterix | 2006-01-01 11:53:30 -0700 (Sun, 01 Jan 2006) | 2 lines
end of gc_contact.nick -> gc_contact.name
........
r4967 | asterix | 2006-01-01 12:05:59 -0700 (Sun, 01 Jan 2006) | 2 lines
new ACE option: send_sha_in_gc_presence that allow to send sha info in groupchat presences
........
r4968 | asterix | 2006-01-01 12:12:36 -0700 (Sun, 01 Jan 2006) | 2 lines
0.9.1-2 in debian that solve the group bug (commit [4924])
........
r4969 | asterix | 2006-01-01 12:31:13 -0700 (Sun, 01 Jan 2006) | 2 lines
typo
........
2006-01-01 21:06:26 +01:00
|
|
|
|
2012-06-17 00:41:33 +02:00
|
|
|
try:
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import caps_cache
|
2018-10-06 23:04:28 +02:00
|
|
|
from gajim.common.i18n import _
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.account import Account
|
|
|
|
from gajim import common
|
2018-09-29 21:48:21 +02:00
|
|
|
from gajim.common.const import Chatstate
|
2013-01-01 23:18:36 +01:00
|
|
|
except ImportError as e:
|
2012-06-17 00:41:33 +02:00
|
|
|
if __name__ != "__main__":
|
2013-01-05 00:03:36 +01:00
|
|
|
raise ImportError(str(e))
|
2009-10-27 20:31:09 +01:00
|
|
|
|
2018-09-16 11:56:56 +02:00
|
|
|
class XMPPEntity:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Base representation of entities in XMPP
|
|
|
|
"""
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, jid, account, resource):
|
|
|
|
self.jid = jid
|
|
|
|
self.resource = resource
|
|
|
|
self.account = account
|
2009-10-27 20:31:09 +01:00
|
|
|
|
2009-11-05 15:57:43 +01:00
|
|
|
class CommonContact(XMPPEntity):
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
def __init__(self, jid, account, resource, show, presence, status, name,
|
2018-09-29 21:48:21 +02:00
|
|
|
chatstate, client_caps=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
XMPPEntity.__init__(self, jid, account, resource)
|
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
self._show = show
|
|
|
|
self._presence = presence
|
2010-02-08 15:08:40 +01:00
|
|
|
self.status = status
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.client_caps = client_caps or caps_cache.NullClientCaps()
|
|
|
|
|
|
|
|
# this is contact's chatstate
|
2018-09-29 21:48:21 +02:00
|
|
|
self._chatstate = chatstate
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-10-07 22:35:27 +02:00
|
|
|
self._is_pm_contact = False
|
|
|
|
|
2018-06-22 20:23:35 +02:00
|
|
|
@property
|
|
|
|
def show(self):
|
|
|
|
return self._show
|
|
|
|
|
|
|
|
@show.setter
|
|
|
|
def show(self, value):
|
|
|
|
self._show = value
|
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
@property
|
|
|
|
def presence(self):
|
|
|
|
return self._presence
|
|
|
|
|
|
|
|
@presence.setter
|
|
|
|
def presence(self, value):
|
|
|
|
self._presence = value
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_available(self):
|
|
|
|
return self._presence.is_available
|
|
|
|
|
2018-09-29 21:48:21 +02:00
|
|
|
@property
|
|
|
|
def chatstate_enum(self):
|
|
|
|
return self._chatstate
|
|
|
|
|
|
|
|
@property
|
|
|
|
def chatstate(self):
|
|
|
|
if self._chatstate is None:
|
|
|
|
return
|
|
|
|
return str(self._chatstate)
|
|
|
|
|
|
|
|
@chatstate.setter
|
|
|
|
def chatstate(self, value):
|
|
|
|
if value is None:
|
|
|
|
self._chatstate = value
|
|
|
|
else:
|
|
|
|
self._chatstate = Chatstate[value.upper()]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_gc_contact(self):
|
|
|
|
return isinstance(self, GC_Contact)
|
|
|
|
|
2018-10-07 22:35:27 +02:00
|
|
|
@property
|
|
|
|
def is_pm_contact(self):
|
|
|
|
return self._is_pm_contact
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_full_jid(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_shown_name(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def supports(self, requested_feature):
|
|
|
|
"""
|
|
|
|
Return True if the contact has advertised to support the feature
|
|
|
|
identified by the given namespace. False otherwise.
|
|
|
|
"""
|
|
|
|
if self.show == 'offline':
|
|
|
|
# Unfortunately, if all resources are offline, the contact
|
|
|
|
# includes the last resource that was online. Check for its
|
|
|
|
# show, so we can be sure it's existant. Otherwise, we still
|
|
|
|
# return caps for a contact that has no resources left.
|
|
|
|
return False
|
2018-09-18 10:14:04 +02:00
|
|
|
return caps_cache.client_supports(self.client_caps, requested_feature)
|
2009-10-28 23:37:40 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Contact(CommonContact):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Information concerning a contact
|
|
|
|
"""
|
2017-02-08 03:12:41 +01:00
|
|
|
def __init__(self, jid, account, name='', groups=None, show='', status='',
|
2011-08-27 22:41:55 +02:00
|
|
|
sub='', ask='', resource='', priority=0, keyID='', client_caps=None,
|
2018-10-07 22:35:27 +02:00
|
|
|
chatstate=None, idle_time=None, avatar_sha=None, groupchat=False,
|
|
|
|
is_pm_contact=False):
|
2013-01-02 13:54:02 +01:00
|
|
|
if not isinstance(jid, str):
|
|
|
|
print('no str')
|
2017-02-08 03:12:41 +01:00
|
|
|
if groups is None:
|
|
|
|
groups = []
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
CommonContact.__init__(self, jid, account, resource, show,
|
|
|
|
None, status, name,
|
2018-09-29 21:48:21 +02:00
|
|
|
chatstate, client_caps=client_caps)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.contact_name = '' # nick choosen by contact
|
2014-10-26 16:43:44 +01:00
|
|
|
self.groups = [i if i else _('General') for i in set(groups)] # filter duplicate values
|
2017-09-16 11:49:31 +02:00
|
|
|
self.avatar_sha = avatar_sha
|
2018-04-19 22:11:41 +02:00
|
|
|
self._is_groupchat = groupchat
|
2018-10-07 22:35:27 +02:00
|
|
|
self._is_pm_contact = is_pm_contact
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.sub = sub
|
|
|
|
self.ask = ask
|
|
|
|
|
|
|
|
self.priority = priority
|
|
|
|
self.keyID = keyID
|
2017-07-26 22:24:19 +02:00
|
|
|
self.idle_time = idle_time
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self.pep = {}
|
|
|
|
|
|
|
|
def get_full_jid(self):
|
|
|
|
if self.resource:
|
|
|
|
return self.jid + '/' + self.resource
|
|
|
|
return self.jid
|
|
|
|
|
|
|
|
def get_shown_name(self):
|
|
|
|
if self.name:
|
|
|
|
return self.name
|
|
|
|
if self.contact_name:
|
|
|
|
return self.contact_name
|
|
|
|
return self.jid.split('@')[0]
|
|
|
|
|
|
|
|
def get_shown_groups(self):
|
|
|
|
if self.is_observer():
|
|
|
|
return [_('Observers')]
|
2018-09-18 10:14:04 +02:00
|
|
|
if self.is_groupchat():
|
2010-02-08 15:08:40 +01:00
|
|
|
return [_('Groupchats')]
|
2018-09-18 10:14:04 +02:00
|
|
|
if self.is_transport():
|
2010-02-08 15:08:40 +01:00
|
|
|
return [_('Transports')]
|
2018-09-18 10:14:04 +02:00
|
|
|
if not self.groups:
|
2010-02-08 15:08:40 +01:00
|
|
|
return [_('General')]
|
2018-09-18 10:14:04 +02:00
|
|
|
return self.groups
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def is_hidden_from_roster(self):
|
|
|
|
"""
|
|
|
|
If contact should not be visible in roster
|
|
|
|
"""
|
|
|
|
# XEP-0162: http://www.xmpp.org/extensions/xep-0162.html
|
|
|
|
if self.is_transport():
|
|
|
|
return False
|
|
|
|
if self.sub in ('both', 'to'):
|
|
|
|
return False
|
|
|
|
if self.sub in ('none', 'from') and self.ask == 'subscribe':
|
|
|
|
return False
|
2018-09-16 01:10:04 +02:00
|
|
|
if self.sub in ('none', 'from') and (self.name or self.groups):
|
2010-02-08 15:08:40 +01:00
|
|
|
return False
|
|
|
|
if _('Not in Roster') in self.groups:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_observer(self):
|
|
|
|
# XEP-0162: http://www.xmpp.org/extensions/xep-0162.html
|
|
|
|
is_observer = False
|
|
|
|
if self.sub == 'from' and not self.is_transport()\
|
|
|
|
and self.is_hidden_from_roster():
|
|
|
|
is_observer = True
|
|
|
|
return is_observer
|
|
|
|
|
|
|
|
def is_groupchat(self):
|
2018-04-19 22:11:41 +02:00
|
|
|
return self._is_groupchat
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
@property
|
|
|
|
def is_connected(self):
|
|
|
|
from gajim.common import app
|
|
|
|
try:
|
|
|
|
return app.gc_connected[self.account.name][self.jid]
|
2018-12-30 20:44:59 +01:00
|
|
|
except Exception:
|
2018-12-30 01:42:59 +01:00
|
|
|
return False
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def is_transport(self):
|
|
|
|
# if not '@' or '@' starts the jid then contact is transport
|
|
|
|
return self.jid.find('@') <= 0
|
2006-10-15 15:23:05 +02:00
|
|
|
|
2006-10-10 17:57:20 +02:00
|
|
|
|
2009-10-28 23:37:40 +01:00
|
|
|
class GC_Contact(CommonContact):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Information concerning each groupchat contact
|
|
|
|
"""
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
def __init__(self, room_jid, account, name='', show='', presence=None,
|
|
|
|
status='', role='', affiliation='', jid='', resource='',
|
|
|
|
chatstate=None, avatar_sha=None):
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
CommonContact.__init__(self, jid, account, resource, show,
|
|
|
|
presence, status, name, chatstate)
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.room_jid = room_jid
|
|
|
|
self.role = role
|
|
|
|
self.affiliation = affiliation
|
2017-09-16 11:49:31 +02:00
|
|
|
self.avatar_sha = avatar_sha
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_full_jid(self):
|
|
|
|
return self.room_jid + '/' + self.name
|
Merged in trunk updates, including meta_contacts
Merged revisions 4951,4962-4969 via svnmerge from
svn://svn.gajim.org/gajim/trunk
........
r4951 | nk | 2005-12-30 16:50:36 -0700 (Fri, 30 Dec 2005) | 1 line
fixes in greek transl
........
r4962 | asterix | 2006-01-01 11:41:04 -0700 (Sun, 01 Jan 2006) | 2 lines
merge meta_contacts branch with trunk. Meta contacts are not in gajim yet, but framework is here. We now use gajim.contacts.many_functions() to handle contacts and groupchat_contacts.
........
r4963 | asterix | 2006-01-01 11:43:24 -0700 (Sun, 01 Jan 2006) | 2 lines
correct contacts file
........
r4964 | asterix | 2006-01-01 11:47:26 -0700 (Sun, 01 Jan 2006) | 2 lines
dict.remove() doesn't exists, it's del dict[]
........
r4965 | asterix | 2006-01-01 11:50:15 -0700 (Sun, 01 Jan 2006) | 2 lines
some missing commits from branch
........
r4966 | asterix | 2006-01-01 11:53:30 -0700 (Sun, 01 Jan 2006) | 2 lines
end of gc_contact.nick -> gc_contact.name
........
r4967 | asterix | 2006-01-01 12:05:59 -0700 (Sun, 01 Jan 2006) | 2 lines
new ACE option: send_sha_in_gc_presence that allow to send sha info in groupchat presences
........
r4968 | asterix | 2006-01-01 12:12:36 -0700 (Sun, 01 Jan 2006) | 2 lines
0.9.1-2 in debian that solve the group bug (commit [4924])
........
r4969 | asterix | 2006-01-01 12:31:13 -0700 (Sun, 01 Jan 2006) | 2 lines
typo
........
2006-01-01 21:06:26 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_shown_name(self):
|
|
|
|
return self.name
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2017-10-19 21:12:27 +02:00
|
|
|
def get_avatar(self, *args, **kwargs):
|
|
|
|
return common.app.interface.get_avatar(self.avatar_sha, *args, **kwargs)
|
2017-09-16 11:49:31 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def as_contact(self):
|
|
|
|
"""
|
|
|
|
Create a Contact instance from this GC_Contact instance
|
|
|
|
"""
|
|
|
|
return Contact(jid=self.get_full_jid(), account=self.account,
|
2011-08-27 22:41:55 +02:00
|
|
|
name=self.name, groups=[], show=self.show, status=self.status,
|
2018-10-07 22:35:27 +02:00
|
|
|
sub='none', client_caps=self.client_caps, avatar_sha=self.avatar_sha,
|
|
|
|
is_pm_contact=True)
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2009-10-27 20:31:09 +01:00
|
|
|
|
2009-12-01 23:31:17 +01:00
|
|
|
class LegacyContactsAPI:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
This is a GOD class for accessing contact and groupchat information.
|
|
|
|
The API has several flaws:
|
|
|
|
|
|
|
|
* it mixes concerns because it deals with contacts, groupchats,
|
|
|
|
groupchat contacts and metacontacts
|
|
|
|
* some methods like get_contact() may return None. This leads to
|
|
|
|
a lot of duplication all over Gajim because it is not sure
|
|
|
|
if we receive a proper contact or just None.
|
|
|
|
|
|
|
|
It is a long way to cleanup this API. Therefore just stick with it
|
|
|
|
and use it as before. We will try to figure out a migration path.
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
self._metacontact_manager = MetacontactManager(self)
|
|
|
|
self._accounts = {}
|
|
|
|
|
|
|
|
def change_account_name(self, old_name, new_name):
|
|
|
|
self._accounts[new_name] = self._accounts[old_name]
|
|
|
|
self._accounts[new_name].name = new_name
|
|
|
|
del self._accounts[old_name]
|
|
|
|
|
|
|
|
self._metacontact_manager.change_account_name(old_name, new_name)
|
|
|
|
|
|
|
|
def add_account(self, account_name):
|
|
|
|
self._accounts[account_name] = Account(account_name, Contacts(),
|
|
|
|
GC_Contacts())
|
|
|
|
self._metacontact_manager.add_account(account_name)
|
|
|
|
|
2018-05-20 20:15:08 +02:00
|
|
|
def get_accounts(self, zeroconf=True):
|
|
|
|
accounts = list(self._accounts.keys())
|
|
|
|
if not zeroconf:
|
|
|
|
if 'Local' in accounts:
|
|
|
|
accounts.remove('Local')
|
|
|
|
return accounts
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def remove_account(self, account):
|
|
|
|
del self._accounts[account]
|
|
|
|
self._metacontact_manager.remove_account(account)
|
|
|
|
|
2017-02-08 03:12:41 +01:00
|
|
|
def create_contact(self, jid, account, name='', groups=None, show='',
|
2010-02-08 15:08:40 +01:00
|
|
|
status='', sub='', ask='', resource='', priority=0, keyID='',
|
2018-09-29 21:48:21 +02:00
|
|
|
client_caps=None, chatstate=None, idle_time=None,
|
2018-04-19 22:11:41 +02:00
|
|
|
avatar_sha=None, groupchat=False):
|
2017-02-08 03:12:41 +01:00
|
|
|
if groups is None:
|
|
|
|
groups = []
|
2010-02-08 15:08:40 +01:00
|
|
|
# Use Account object if available
|
|
|
|
account = self._accounts.get(account, account)
|
|
|
|
return Contact(jid=jid, account=account, name=name, groups=groups,
|
2011-08-27 22:41:55 +02:00
|
|
|
show=show, status=status, sub=sub, ask=ask, resource=resource,
|
|
|
|
priority=priority, keyID=keyID, client_caps=client_caps,
|
2018-09-29 21:48:21 +02:00
|
|
|
chatstate=chatstate, idle_time=idle_time, avatar_sha=avatar_sha,
|
|
|
|
groupchat=groupchat)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def create_self_contact(self, jid, account, resource, show, status, priority,
|
|
|
|
name='', keyID=''):
|
2017-08-13 13:18:56 +02:00
|
|
|
conn = common.app.connections[account]
|
|
|
|
nick = name or common.app.nicks[account]
|
2010-02-08 15:08:40 +01:00
|
|
|
account = self._accounts.get(account, account) # Use Account object if available
|
|
|
|
self_contact = self.create_contact(jid=jid, account=account,
|
|
|
|
name=nick, groups=['self_contact'], show=show, status=status,
|
2011-08-27 22:41:55 +02:00
|
|
|
sub='both', ask='none', priority=priority, keyID=keyID,
|
2010-02-08 15:08:40 +01:00
|
|
|
resource=resource)
|
|
|
|
self_contact.pep = conn.pep
|
|
|
|
return self_contact
|
|
|
|
|
2011-08-27 22:41:55 +02:00
|
|
|
def create_not_in_roster_contact(self, jid, account, resource='', name='',
|
2018-12-25 20:30:36 +01:00
|
|
|
keyID='', groupchat=False):
|
2011-08-27 22:41:55 +02:00
|
|
|
# Use Account object if available
|
|
|
|
account = self._accounts.get(account, account)
|
2010-02-08 15:08:40 +01:00
|
|
|
return self.create_contact(jid=jid, account=account, resource=resource,
|
2011-08-27 22:41:55 +02:00
|
|
|
name=name, groups=[_('Not in Roster')], show='not in roster',
|
2018-12-25 20:30:36 +01:00
|
|
|
status='', sub='none', keyID=keyID, groupchat=groupchat)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def copy_contact(self, contact):
|
|
|
|
return self.create_contact(contact.jid, contact.account,
|
2011-08-27 22:41:55 +02:00
|
|
|
name=contact.name, groups=contact.groups, show=contact.show,
|
|
|
|
status=contact.status, sub=contact.sub, ask=contact.ask,
|
|
|
|
resource=contact.resource, priority=contact.priority,
|
|
|
|
keyID=contact.keyID, client_caps=contact.client_caps,
|
2018-10-01 21:24:52 +02:00
|
|
|
chatstate=contact.chatstate_enum,
|
2017-09-16 11:49:31 +02:00
|
|
|
idle_time=contact.idle_time, avatar_sha=contact.avatar_sha)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def add_contact(self, account, contact):
|
|
|
|
if account not in self._accounts:
|
|
|
|
self.add_account(account)
|
|
|
|
return self._accounts[account].contacts.add_contact(contact)
|
|
|
|
|
|
|
|
def remove_contact(self, account, contact):
|
|
|
|
if account not in self._accounts:
|
|
|
|
return
|
|
|
|
return self._accounts[account].contacts.remove_contact(contact)
|
|
|
|
|
|
|
|
def remove_jid(self, account, jid, remove_meta=True):
|
|
|
|
self._accounts[account].contacts.remove_jid(jid)
|
|
|
|
if remove_meta:
|
|
|
|
self._metacontact_manager.remove_metacontact(account, jid)
|
|
|
|
|
2018-04-19 22:11:41 +02:00
|
|
|
def get_groupchat_contact(self, account, jid):
|
|
|
|
return self._accounts[account].contacts.get_groupchat_contact(jid)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_contacts(self, account, jid):
|
|
|
|
return self._accounts[account].contacts.get_contacts(jid)
|
|
|
|
|
|
|
|
def get_contact(self, account, jid, resource=None):
|
|
|
|
return self._accounts[account].contacts.get_contact(jid, resource=resource)
|
|
|
|
|
2019-01-01 16:19:35 +01:00
|
|
|
def get_contact_strict(self, account, jid, resource):
|
|
|
|
return self._accounts[account].contacts.get_contact_strict(jid, resource)
|
|
|
|
|
2017-10-19 21:12:27 +02:00
|
|
|
def get_avatar(self, account, *args, **kwargs):
|
|
|
|
return self._accounts[account].contacts.get_avatar(*args, **kwargs)
|
2017-09-16 11:49:31 +02:00
|
|
|
|
|
|
|
def get_avatar_sha(self, account, jid):
|
|
|
|
return self._accounts[account].contacts.get_avatar_sha(jid)
|
|
|
|
|
|
|
|
def set_avatar(self, account, jid, sha):
|
|
|
|
self._accounts[account].contacts.set_avatar(jid, sha)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def iter_contacts(self, account):
|
|
|
|
for contact in self._accounts[account].contacts.iter_contacts():
|
|
|
|
yield contact
|
|
|
|
|
|
|
|
def get_contact_from_full_jid(self, account, fjid):
|
|
|
|
return self._accounts[account].contacts.get_contact_from_full_jid(fjid)
|
|
|
|
|
|
|
|
def get_first_contact_from_jid(self, account, jid):
|
|
|
|
return self._accounts[account].contacts.get_first_contact_from_jid(jid)
|
|
|
|
|
|
|
|
def get_contacts_from_group(self, account, group):
|
|
|
|
return self._accounts[account].contacts.get_contacts_from_group(group)
|
|
|
|
|
|
|
|
def get_contacts_jid_list(self, account):
|
|
|
|
return self._accounts[account].contacts.get_contacts_jid_list()
|
|
|
|
|
|
|
|
def get_jid_list(self, account):
|
|
|
|
return self._accounts[account].contacts.get_jid_list()
|
|
|
|
|
|
|
|
def change_contact_jid(self, old_jid, new_jid, account):
|
|
|
|
return self._accounts[account].change_contact_jid(old_jid, new_jid)
|
|
|
|
|
|
|
|
def get_highest_prio_contact_from_contacts(self, contacts):
|
|
|
|
if not contacts:
|
|
|
|
return None
|
|
|
|
prim_contact = contacts[0]
|
|
|
|
for contact in contacts[1:]:
|
|
|
|
if int(contact.priority) > int(prim_contact.priority):
|
|
|
|
prim_contact = contact
|
|
|
|
return prim_contact
|
|
|
|
|
|
|
|
def get_contact_with_highest_priority(self, account, jid):
|
|
|
|
contacts = self.get_contacts(account, jid)
|
|
|
|
if not contacts and '/' in jid:
|
|
|
|
# jid may be a fake jid, try it
|
|
|
|
room, nick = jid.split('/', 1)
|
|
|
|
contact = self.get_gc_contact(account, room, nick)
|
|
|
|
return contact
|
|
|
|
return self.get_highest_prio_contact_from_contacts(contacts)
|
|
|
|
|
2017-02-08 03:12:41 +01:00
|
|
|
def get_nb_online_total_contacts(self, accounts=None, groups=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the number of online contacts and the total number of contacts
|
|
|
|
"""
|
2017-02-08 03:12:41 +01:00
|
|
|
if not accounts:
|
2010-02-08 15:08:40 +01:00
|
|
|
accounts = self.get_accounts()
|
2017-02-08 03:12:41 +01:00
|
|
|
if groups is None:
|
|
|
|
groups = []
|
2010-02-08 15:08:40 +01:00
|
|
|
nbr_online = 0
|
|
|
|
nbr_total = 0
|
|
|
|
for account in accounts:
|
2017-08-13 13:18:56 +02:00
|
|
|
our_jid = common.app.get_jid_from_account(account)
|
2010-02-08 15:08:40 +01:00
|
|
|
for jid in self.get_jid_list(account):
|
|
|
|
if jid == our_jid:
|
|
|
|
continue
|
2018-10-06 23:06:07 +02:00
|
|
|
if (common.app.jid_is_transport(jid) and
|
|
|
|
_('Transports') not in groups):
|
2010-02-08 15:08:40 +01:00
|
|
|
# do not count transports
|
|
|
|
continue
|
|
|
|
if self.has_brother(account, jid, accounts) and not \
|
|
|
|
self.is_big_brother(account, jid, accounts):
|
|
|
|
# count metacontacts only once
|
|
|
|
continue
|
2012-09-24 12:24:10 +02:00
|
|
|
contact = self._accounts[account].contacts._contacts[jid][0]
|
2010-02-08 15:08:40 +01:00
|
|
|
if _('Not in roster') in contact.groups:
|
|
|
|
continue
|
|
|
|
in_groups = False
|
|
|
|
if groups == []:
|
|
|
|
in_groups = True
|
|
|
|
else:
|
|
|
|
for group in groups:
|
|
|
|
if group in contact.get_shown_groups():
|
|
|
|
in_groups = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if in_groups:
|
|
|
|
if contact.show not in ('offline', 'error'):
|
|
|
|
nbr_online += 1
|
|
|
|
nbr_total += 1
|
|
|
|
return nbr_online, nbr_total
|
|
|
|
|
|
|
|
def __getattr__(self, attr_name):
|
|
|
|
# Only called if self has no attr_name
|
|
|
|
if hasattr(self._metacontact_manager, attr_name):
|
|
|
|
return getattr(self._metacontact_manager, attr_name)
|
2018-09-18 10:14:04 +02:00
|
|
|
raise AttributeError(attr_name)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
def create_gc_contact(self, room_jid, account, name='', show='',
|
|
|
|
presence=None, status='', role='',
|
|
|
|
affiliation='', jid='', resource='', avatar_sha=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
account = self._accounts.get(account, account) # Use Account object if available
|
2018-12-30 01:42:59 +01:00
|
|
|
return GC_Contact(room_jid, account, name, show, presence, status,
|
|
|
|
role, affiliation, jid, resource, avatar_sha=avatar_sha)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def add_gc_contact(self, account, gc_contact):
|
|
|
|
return self._accounts[account].gc_contacts.add_gc_contact(gc_contact)
|
|
|
|
|
|
|
|
def remove_gc_contact(self, account, gc_contact):
|
|
|
|
return self._accounts[account].gc_contacts.remove_gc_contact(gc_contact)
|
|
|
|
|
|
|
|
def remove_room(self, account, room_jid):
|
|
|
|
return self._accounts[account].gc_contacts.remove_room(room_jid)
|
|
|
|
|
|
|
|
def get_gc_list(self, account):
|
|
|
|
return self._accounts[account].gc_contacts.get_gc_list()
|
|
|
|
|
|
|
|
def get_nick_list(self, account, room_jid):
|
|
|
|
return self._accounts[account].gc_contacts.get_nick_list(room_jid)
|
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
def get_gc_contact_list(self, account, room_jid):
|
|
|
|
return self._accounts[account].gc_contacts.get_gc_contact_list(room_jid)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_gc_contact(self, account, room_jid, nick):
|
|
|
|
return self._accounts[account].gc_contacts.get_gc_contact(room_jid, nick)
|
|
|
|
|
2012-06-17 00:41:33 +02:00
|
|
|
def is_gc_contact(self, account, jid):
|
|
|
|
return self._accounts[account].gc_contacts.is_gc_contact(jid)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_nb_role_total_gc_contacts(self, account, room_jid, role):
|
|
|
|
return self._accounts[account].gc_contacts.get_nb_role_total_gc_contacts(room_jid, role)
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2017-09-16 11:49:31 +02:00
|
|
|
def set_gc_avatar(self, account, room_jid, nick, sha):
|
|
|
|
contact = self.get_gc_contact(account, room_jid, nick)
|
|
|
|
if contact is None:
|
|
|
|
return
|
|
|
|
contact.avatar_sha = sha
|
|
|
|
|
2018-09-29 21:48:21 +02:00
|
|
|
def get_combined_chatstate(self, account, jid):
|
|
|
|
return self._accounts[account].contacts.get_combined_chatstate(jid)
|
|
|
|
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2009-12-01 23:31:17 +01:00
|
|
|
class Contacts():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
This is a breakout of the contact related behavior of the old
|
|
|
|
Contacts class (which is not called LegacyContactsAPI)
|
|
|
|
"""
|
|
|
|
def __init__(self):
|
|
|
|
# list of contacts {jid1: [C1, C2]}, } one Contact per resource
|
|
|
|
self._contacts = {}
|
|
|
|
|
|
|
|
def add_contact(self, contact):
|
2018-09-01 12:44:14 +02:00
|
|
|
if contact.jid not in self._contacts or contact.is_groupchat():
|
2010-02-08 15:08:40 +01:00
|
|
|
self._contacts[contact.jid] = [contact]
|
|
|
|
return
|
|
|
|
contacts = self._contacts[contact.jid]
|
|
|
|
# We had only one that was offline, remove it
|
|
|
|
if len(contacts) == 1 and contacts[0].show == 'offline':
|
2018-06-27 00:52:14 +02:00
|
|
|
# Do not use self.remove_contact: it deletes
|
2010-02-08 15:08:40 +01:00
|
|
|
# self._contacts[account][contact.jid]
|
|
|
|
contacts.remove(contacts[0])
|
|
|
|
# If same JID with same resource already exists, use the new one
|
|
|
|
for c in contacts:
|
|
|
|
if c.resource == contact.resource:
|
|
|
|
self.remove_contact(c)
|
|
|
|
break
|
|
|
|
contacts.append(contact)
|
|
|
|
|
|
|
|
def remove_contact(self, contact):
|
|
|
|
if contact.jid not in self._contacts:
|
|
|
|
return
|
|
|
|
if contact in self._contacts[contact.jid]:
|
|
|
|
self._contacts[contact.jid].remove(contact)
|
2018-09-16 01:10:04 +02:00
|
|
|
if not self._contacts[contact.jid]:
|
2010-02-08 15:08:40 +01:00
|
|
|
del self._contacts[contact.jid]
|
|
|
|
|
|
|
|
def remove_jid(self, jid):
|
|
|
|
"""
|
|
|
|
Remove all contacts for a given jid
|
|
|
|
"""
|
|
|
|
if jid in self._contacts:
|
|
|
|
del self._contacts[jid]
|
|
|
|
|
|
|
|
def get_contacts(self, jid):
|
|
|
|
"""
|
|
|
|
Return the list of contact instances for this jid
|
|
|
|
"""
|
2019-01-01 16:19:35 +01:00
|
|
|
return list(self._contacts.get(jid, []))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_contact(self, jid, resource=None):
|
|
|
|
### WARNING ###
|
|
|
|
# This function returns a *RANDOM* resource if resource = None!
|
|
|
|
# Do *NOT* use if you need to get the contact to which you
|
|
|
|
# send a message for example, as a bare JID in Jabber means
|
|
|
|
# highest available resource, which this function ignores!
|
|
|
|
"""
|
|
|
|
Return the contact instance for the given resource if it's given else the
|
|
|
|
first contact is no resource is given or None if there is not
|
|
|
|
"""
|
|
|
|
if jid in self._contacts:
|
|
|
|
if not resource:
|
|
|
|
return self._contacts[jid][0]
|
|
|
|
for c in self._contacts[jid]:
|
|
|
|
if c.resource == resource:
|
|
|
|
return c
|
2013-11-28 21:24:17 +01:00
|
|
|
return self._contacts[jid][0]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-02-26 22:24:30 +01:00
|
|
|
def get_contact_strict(self, jid, resource):
|
|
|
|
"""
|
|
|
|
Return the contact instance for the given resource or None
|
|
|
|
"""
|
|
|
|
if jid in self._contacts:
|
|
|
|
for c in self._contacts[jid]:
|
|
|
|
if c.resource == resource:
|
|
|
|
return c
|
|
|
|
|
2018-04-19 22:11:41 +02:00
|
|
|
def get_groupchat_contact(self, jid):
|
|
|
|
if jid in self._contacts:
|
|
|
|
contacts = self._contacts[jid]
|
|
|
|
if contacts[0].is_groupchat():
|
|
|
|
return contacts[0]
|
|
|
|
|
2017-10-19 21:12:27 +02:00
|
|
|
def get_avatar(self, jid, size=None, scale=None):
|
2017-09-16 11:49:31 +02:00
|
|
|
if jid not in self._contacts:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for resource in self._contacts[jid]:
|
|
|
|
if resource.avatar_sha is None:
|
|
|
|
continue
|
2017-10-19 21:12:27 +02:00
|
|
|
avatar = common.app.interface.get_avatar(
|
|
|
|
resource.avatar_sha, size, scale)
|
2017-09-16 11:49:31 +02:00
|
|
|
if avatar is None:
|
|
|
|
self.set_avatar(jid, None)
|
|
|
|
return avatar
|
|
|
|
|
|
|
|
def get_avatar_sha(self, jid):
|
|
|
|
if jid not in self._contacts:
|
|
|
|
return None
|
|
|
|
|
|
|
|
for resource in self._contacts[jid]:
|
|
|
|
if resource.avatar_sha is not None:
|
|
|
|
return resource.avatar_sha
|
|
|
|
return None
|
|
|
|
|
|
|
|
def set_avatar(self, jid, sha):
|
|
|
|
if jid not in self._contacts:
|
|
|
|
return
|
|
|
|
for resource in self._contacts[jid]:
|
|
|
|
resource.avatar_sha = sha
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def iter_contacts(self):
|
2013-01-10 19:47:57 +01:00
|
|
|
for jid in list(self._contacts.keys()):
|
2010-02-08 15:08:40 +01:00
|
|
|
for contact in self._contacts[jid][:]:
|
|
|
|
yield contact
|
|
|
|
|
|
|
|
def get_jid_list(self):
|
2013-01-10 19:47:57 +01:00
|
|
|
return list(self._contacts.keys())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_contacts_jid_list(self):
|
2013-01-02 13:54:02 +01:00
|
|
|
return [jid for jid, contact in self._contacts.items() if not
|
2010-03-08 17:33:53 +01:00
|
|
|
contact[0].is_groupchat()]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_contact_from_full_jid(self, fjid):
|
|
|
|
"""
|
|
|
|
Get Contact object for specific resource of given jid
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
barejid, resource = common.app.get_room_and_nick_from_fjid(fjid)
|
2018-02-26 22:24:30 +01:00
|
|
|
return self.get_contact_strict(barejid, resource)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_first_contact_from_jid(self, jid):
|
|
|
|
if jid in self._contacts:
|
|
|
|
return self._contacts[jid][0]
|
|
|
|
|
|
|
|
def get_contacts_from_group(self, group):
|
|
|
|
"""
|
|
|
|
Return all contacts in the given group
|
|
|
|
"""
|
|
|
|
group_contacts = []
|
|
|
|
for jid in self._contacts:
|
|
|
|
contacts = self.get_contacts(jid)
|
|
|
|
if group in contacts[0].groups:
|
|
|
|
group_contacts += contacts
|
|
|
|
return group_contacts
|
|
|
|
|
|
|
|
def change_contact_jid(self, old_jid, new_jid):
|
|
|
|
if old_jid not in self._contacts:
|
|
|
|
return
|
|
|
|
self._contacts[new_jid] = []
|
|
|
|
for _contact in self._contacts[old_jid]:
|
|
|
|
_contact.jid = new_jid
|
|
|
|
self._contacts[new_jid].append(_contact)
|
|
|
|
del self._contacts[old_jid]
|
2009-11-11 23:37:46 +01:00
|
|
|
|
2018-09-29 21:48:21 +02:00
|
|
|
def get_combined_chatstate(self, jid):
|
|
|
|
if jid not in self._contacts:
|
|
|
|
return
|
|
|
|
contacts = self._contacts[jid]
|
|
|
|
states = []
|
|
|
|
for contact in contacts:
|
|
|
|
if contact.chatstate_enum is None:
|
|
|
|
continue
|
|
|
|
states.append(contact.chatstate_enum)
|
|
|
|
|
|
|
|
return str(min(states)) if states else None
|
|
|
|
|
2009-11-11 23:37:46 +01:00
|
|
|
|
2009-11-10 21:08:25 +01:00
|
|
|
class GC_Contacts():
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
# list of contacts that are in gc {room_jid: {nick: C}}}
|
|
|
|
self._rooms = {}
|
|
|
|
|
|
|
|
def add_gc_contact(self, gc_contact):
|
|
|
|
if gc_contact.room_jid not in self._rooms:
|
|
|
|
self._rooms[gc_contact.room_jid] = {gc_contact.name: gc_contact}
|
|
|
|
else:
|
|
|
|
self._rooms[gc_contact.room_jid][gc_contact.name] = gc_contact
|
|
|
|
|
|
|
|
def remove_gc_contact(self, gc_contact):
|
|
|
|
if gc_contact.room_jid not in self._rooms:
|
|
|
|
return
|
|
|
|
if gc_contact.name not in self._rooms[gc_contact.room_jid]:
|
|
|
|
return
|
|
|
|
del self._rooms[gc_contact.room_jid][gc_contact.name]
|
|
|
|
# It was the last nick in room ?
|
2018-09-16 01:10:04 +02:00
|
|
|
if not self._rooms[gc_contact.room_jid]:
|
2010-02-08 15:08:40 +01:00
|
|
|
del self._rooms[gc_contact.room_jid]
|
|
|
|
|
|
|
|
def remove_room(self, room_jid):
|
|
|
|
if room_jid in self._rooms:
|
|
|
|
del self._rooms[room_jid]
|
|
|
|
|
|
|
|
def get_gc_list(self):
|
|
|
|
return self._rooms.keys()
|
|
|
|
|
|
|
|
def get_nick_list(self, room_jid):
|
|
|
|
gc_list = self.get_gc_list()
|
|
|
|
if not room_jid in gc_list:
|
|
|
|
return []
|
2013-01-07 15:33:41 +01:00
|
|
|
return list(self._rooms[room_jid].keys())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-12-30 01:42:59 +01:00
|
|
|
def get_gc_contact_list(self, room_jid):
|
|
|
|
try:
|
|
|
|
return list(self._rooms[room_jid].values())
|
|
|
|
except Exception:
|
|
|
|
return []
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_gc_contact(self, room_jid, nick):
|
2018-09-08 20:44:20 +02:00
|
|
|
try:
|
|
|
|
return self._rooms[room_jid][nick]
|
|
|
|
except KeyError:
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
|
|
|
|
2012-06-17 00:41:33 +02:00
|
|
|
def is_gc_contact(self, jid):
|
|
|
|
"""
|
|
|
|
>>> gc = GC_Contacts()
|
|
|
|
>>> gc._rooms = {'gajim@conference.gajim.org' : {'test' : True}}
|
|
|
|
>>> gc.is_gc_contact('gajim@conference.gajim.org/test')
|
|
|
|
True
|
|
|
|
>>> gc.is_gc_contact('test@jabbim.com')
|
|
|
|
False
|
|
|
|
"""
|
|
|
|
jid = jid.split('/')
|
|
|
|
if len(jid) != 2:
|
|
|
|
return False
|
|
|
|
gcc = self.get_gc_contact(jid[0], jid[1])
|
2018-09-18 10:26:27 +02:00
|
|
|
return gcc is not None
|
2012-06-17 00:41:33 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_nb_role_total_gc_contacts(self, room_jid, role):
|
|
|
|
"""
|
|
|
|
Return the number of group chat contacts for the given role and the total
|
|
|
|
number of group chat contacts
|
|
|
|
"""
|
|
|
|
if room_jid not in self._rooms:
|
|
|
|
return 0, 0
|
|
|
|
nb_role = nb_total = 0
|
|
|
|
for nick in self._rooms[room_jid]:
|
|
|
|
if self._rooms[room_jid][nick].role == role:
|
|
|
|
nb_role += 1
|
|
|
|
nb_total += 1
|
|
|
|
return nb_role, nb_total
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2009-11-05 19:43:25 +01:00
|
|
|
|
|
|
|
class MetacontactManager():
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, contacts):
|
|
|
|
self._metacontacts_tags = {}
|
|
|
|
self._contacts = contacts
|
|
|
|
|
|
|
|
def change_account_name(self, old_name, new_name):
|
|
|
|
self._metacontacts_tags[new_name] = self._metacontacts_tags[old_name]
|
|
|
|
del self._metacontacts_tags[old_name]
|
|
|
|
|
|
|
|
def add_account(self, account):
|
|
|
|
if account not in self._metacontacts_tags:
|
|
|
|
self._metacontacts_tags[account] = {}
|
|
|
|
|
|
|
|
def remove_account(self, account):
|
|
|
|
del self._metacontacts_tags[account]
|
|
|
|
|
|
|
|
def define_metacontacts(self, account, tags_list):
|
|
|
|
self._metacontacts_tags[account] = tags_list
|
|
|
|
|
|
|
|
def _get_new_metacontacts_tag(self, jid):
|
|
|
|
if not jid in self._metacontacts_tags:
|
|
|
|
return jid
|
|
|
|
#FIXME: can this append ?
|
|
|
|
assert False
|
|
|
|
|
|
|
|
def iter_metacontacts_families(self, account):
|
|
|
|
for tag in self._metacontacts_tags[account]:
|
|
|
|
family = self._get_metacontacts_family_from_tag(account, tag)
|
|
|
|
yield family
|
|
|
|
|
|
|
|
def _get_metacontacts_tag(self, account, jid):
|
|
|
|
"""
|
|
|
|
Return the tag of a jid
|
|
|
|
"""
|
|
|
|
if not account in self._metacontacts_tags:
|
|
|
|
return None
|
|
|
|
for tag in self._metacontacts_tags[account]:
|
|
|
|
for data in self._metacontacts_tags[account][tag]:
|
|
|
|
if data['jid'] == jid:
|
|
|
|
return tag
|
|
|
|
return None
|
|
|
|
|
|
|
|
def add_metacontact(self, brother_account, brother_jid, account, jid, order=None):
|
|
|
|
tag = self._get_metacontacts_tag(brother_account, brother_jid)
|
|
|
|
if not tag:
|
|
|
|
tag = self._get_new_metacontacts_tag(brother_jid)
|
|
|
|
self._metacontacts_tags[brother_account][tag] = [{'jid': brother_jid,
|
|
|
|
'tag': tag}]
|
|
|
|
if brother_account != account:
|
2018-07-27 15:46:02 +02:00
|
|
|
con = common.app.connections[brother_account]
|
|
|
|
con.get_module('MetaContacts').store_metacontacts(
|
|
|
|
self._metacontacts_tags[brother_account])
|
2010-02-08 15:08:40 +01:00
|
|
|
# be sure jid has no other tag
|
|
|
|
old_tag = self._get_metacontacts_tag(account, jid)
|
|
|
|
while old_tag:
|
|
|
|
self.remove_metacontact(account, jid)
|
|
|
|
old_tag = self._get_metacontacts_tag(account, jid)
|
|
|
|
if tag not in self._metacontacts_tags[account]:
|
|
|
|
self._metacontacts_tags[account][tag] = [{'jid': jid, 'tag': tag}]
|
|
|
|
else:
|
|
|
|
if order:
|
|
|
|
self._metacontacts_tags[account][tag].append({'jid': jid,
|
|
|
|
'tag': tag, 'order': order})
|
|
|
|
else:
|
|
|
|
self._metacontacts_tags[account][tag].append({'jid': jid,
|
|
|
|
'tag': tag})
|
2018-07-27 15:46:02 +02:00
|
|
|
con = common.app.connections[account]
|
|
|
|
con.get_module('MetaContacts').store_metacontacts(
|
|
|
|
self._metacontacts_tags[account])
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def remove_metacontact(self, account, jid):
|
2018-07-27 15:46:02 +02:00
|
|
|
if account not in self._metacontacts_tags:
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
found = None
|
|
|
|
for tag in self._metacontacts_tags[account]:
|
|
|
|
for data in self._metacontacts_tags[account][tag]:
|
|
|
|
if data['jid'] == jid:
|
|
|
|
found = data
|
|
|
|
break
|
|
|
|
if found:
|
|
|
|
self._metacontacts_tags[account][tag].remove(found)
|
2018-07-27 15:46:02 +02:00
|
|
|
con = common.app.connections[account]
|
|
|
|
con.get_module('MetaContacts').store_metacontacts(
|
|
|
|
self._metacontacts_tags[account])
|
2010-02-08 15:08:40 +01:00
|
|
|
break
|
|
|
|
|
|
|
|
def has_brother(self, account, jid, accounts):
|
|
|
|
tag = self._get_metacontacts_tag(account, jid)
|
|
|
|
if not tag:
|
|
|
|
return False
|
|
|
|
meta_jids = self._get_metacontacts_jids(tag, accounts)
|
|
|
|
return len(meta_jids) > 1 or len(meta_jids[account]) > 1
|
|
|
|
|
|
|
|
def is_big_brother(self, account, jid, accounts):
|
|
|
|
family = self.get_metacontacts_family(account, jid)
|
|
|
|
if family:
|
|
|
|
nearby_family = [data for data in family
|
|
|
|
if account in accounts]
|
|
|
|
bb_data = self._get_metacontacts_big_brother(nearby_family)
|
|
|
|
if bb_data['jid'] == jid and bb_data['account'] == account:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _get_metacontacts_jids(self, tag, accounts):
|
|
|
|
"""
|
|
|
|
Return all jid for the given tag in the form {acct: [jid1, jid2],.}
|
|
|
|
"""
|
|
|
|
answers = {}
|
|
|
|
for account in self._metacontacts_tags:
|
|
|
|
if tag in self._metacontacts_tags[account]:
|
|
|
|
if account not in accounts:
|
|
|
|
continue
|
|
|
|
answers[account] = []
|
|
|
|
for data in self._metacontacts_tags[account][tag]:
|
|
|
|
answers[account].append(data['jid'])
|
|
|
|
return answers
|
|
|
|
|
|
|
|
def get_metacontacts_family(self, account, jid):
|
|
|
|
"""
|
|
|
|
Return the family of the given jid, including jid in the form:
|
|
|
|
[{'account': acct, 'jid': jid, 'order': order}, ] 'order' is optional
|
|
|
|
"""
|
|
|
|
tag = self._get_metacontacts_tag(account, jid)
|
|
|
|
return self._get_metacontacts_family_from_tag(account, tag)
|
|
|
|
|
|
|
|
def _get_metacontacts_family_from_tag(self, account, tag):
|
|
|
|
if not tag:
|
|
|
|
return []
|
|
|
|
answers = []
|
2018-09-18 08:37:18 +02:00
|
|
|
if tag in self._metacontacts_tags[account]:
|
|
|
|
for data in self._metacontacts_tags[account][tag]:
|
|
|
|
data['account'] = account
|
|
|
|
answers.append(data)
|
2010-02-08 15:08:40 +01:00
|
|
|
return answers
|
|
|
|
|
2018-01-20 21:12:37 +01:00
|
|
|
def _metacontact_key(self, data):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Data is {'jid': jid, 'account': account, 'order': order} order is
|
|
|
|
optional
|
|
|
|
"""
|
|
|
|
show_list = ['not in roster', 'error', 'offline', 'invisible', 'dnd',
|
2018-01-20 21:12:37 +01:00
|
|
|
'xa', 'away', 'chat', 'online', 'requested', 'message']
|
|
|
|
|
|
|
|
jid = data['jid']
|
|
|
|
account = data['account']
|
2010-02-08 15:08:40 +01:00
|
|
|
# contact can be null when a jid listed in the metacontact data
|
|
|
|
# is not in our roster
|
2018-01-20 21:12:37 +01:00
|
|
|
contact = self._contacts.get_contact_with_highest_priority(
|
|
|
|
account, jid)
|
|
|
|
show = show_list.index(contact.show) if contact else 0
|
|
|
|
priority = contact.priority if contact else 0
|
|
|
|
has_order = 'order' in data
|
|
|
|
order = data.get('order', 0)
|
|
|
|
transport = common.app.get_transport_name_from_jid(jid)
|
|
|
|
server = common.app.get_server_from_jid(jid)
|
|
|
|
myserver = common.app.config.get_per('accounts', account, 'hostname')
|
|
|
|
return (bool(contact), show > 2, has_order, order, bool(transport),
|
|
|
|
show, priority, server == myserver, jid, account)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def get_nearby_family_and_big_brother(self, family, account):
|
|
|
|
"""
|
|
|
|
Return the nearby family and its Big Brother
|
|
|
|
|
|
|
|
Nearby family is the part of the family that is grouped with the
|
|
|
|
metacontact. A metacontact may be over different accounts. If accounts
|
|
|
|
are not merged then the given family is split account wise.
|
|
|
|
|
|
|
|
(nearby_family, big_brother_jid, big_brother_account)
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
if common.app.config.get('mergeaccounts'):
|
2010-02-08 15:08:40 +01:00
|
|
|
# group all together
|
|
|
|
nearby_family = family
|
|
|
|
else:
|
|
|
|
# we want one nearby_family per account
|
|
|
|
nearby_family = [data for data in family if account == data['account']]
|
|
|
|
|
2015-11-15 21:07:02 +01:00
|
|
|
if not nearby_family:
|
|
|
|
return (None, None, None)
|
2010-02-08 15:08:40 +01:00
|
|
|
big_brother_data = self._get_metacontacts_big_brother(nearby_family)
|
|
|
|
big_brother_jid = big_brother_data['jid']
|
|
|
|
big_brother_account = big_brother_data['account']
|
|
|
|
|
|
|
|
return (nearby_family, big_brother_jid, big_brother_account)
|
|
|
|
|
|
|
|
def _get_metacontacts_big_brother(self, family):
|
|
|
|
"""
|
2018-06-27 00:52:14 +02:00
|
|
|
Which of the family will be the big brother under which all others will be
|
2010-02-08 15:08:40 +01:00
|
|
|
?
|
|
|
|
"""
|
2018-01-20 21:12:37 +01:00
|
|
|
return max(family, key=self._metacontact_key)
|
2012-06-17 00:41:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
import doctest
|
|
|
|
doctest.testmod()
|