2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/common/contacts.py
|
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
|
|
|
##
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Travis Shirk <travis AT pobox.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## 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>
|
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
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## This file is part of Gajim.
|
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
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
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
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-12-12 09:44:46 +01:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
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
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
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
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
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
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-12-12 09:44:46 +01:00
|
|
|
##
|
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
|
|
|
|
from gajim.common.account import Account
|
|
|
|
from gajim import common
|
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
|
|
|
|
2009-11-05 15:57:43 +01:00
|
|
|
class XMPPEntity(object):
|
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
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, jid, account, resource, show, status, name,
|
2011-08-27 22:41:55 +02:00
|
|
|
our_chatstate, chatstate, client_caps=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
XMPPEntity.__init__(self, jid, account, resource)
|
|
|
|
|
|
|
|
self.show = show
|
|
|
|
self.status = status
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
self.client_caps = client_caps or caps_cache.NullClientCaps()
|
|
|
|
|
|
|
|
# please read xep-85 http://www.xmpp.org/extensions/xep-0085.html
|
|
|
|
# this holds what WE SEND to contact (our current chatstate)
|
|
|
|
self.our_chatstate = our_chatstate
|
|
|
|
# this is contact's chatstate
|
|
|
|
self.chatstate = chatstate
|
|
|
|
|
|
|
|
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
|
|
|
|
else:
|
|
|
|
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,
|
2017-09-16 11:49:31 +02:00
|
|
|
our_chatstate=None, chatstate=None, idle_time=None, avatar_sha=None):
|
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
|
|
|
|
|
|
|
CommonContact.__init__(self, jid, account, resource, show, status, name,
|
2011-08-27 22:41:55 +02:00
|
|
|
our_chatstate, 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
|
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')]
|
|
|
|
elif self.is_groupchat():
|
|
|
|
return [_('Groupchats')]
|
|
|
|
elif self.is_transport():
|
|
|
|
return [_('Transports')]
|
|
|
|
elif not self.groups:
|
|
|
|
return [_('General')]
|
|
|
|
else:
|
|
|
|
return self.groups
|
|
|
|
|
|
|
|
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
|
|
|
|
if self.sub in ('none', 'from') and (self.name or len(self.groups)):
|
|
|
|
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):
|
2017-08-13 13:18:56 +02:00
|
|
|
for account in common.app.gc_connected:
|
|
|
|
if self.jid in common.app.gc_connected[account]:
|
2010-02-08 15:08:40 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, room_jid, account, name='', show='', status='', role='',
|
2011-08-27 22:41:55 +02:00
|
|
|
affiliation='', jid='', resource='', our_chatstate=None,
|
2017-09-16 11:49:31 +02:00
|
|
|
chatstate=None, avatar_sha=None):
|
2009-11-26 11:21:43 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
CommonContact.__init__(self, jid, account, resource, show, status, name,
|
2011-08-27 22:41:55 +02:00
|
|
|
our_chatstate, 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,
|
2017-09-16 11:49:31 +02:00
|
|
|
sub='none', client_caps=self.client_caps, avatar_sha=self.avatar_sha)
|
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)
|
|
|
|
|
|
|
|
def get_accounts(self):
|
2016-10-10 15:22:51 +02:00
|
|
|
return list(self._accounts.keys())
|
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='',
|
2017-09-16 11:49:31 +02:00
|
|
|
client_caps=None, our_chatstate=None, chatstate=None, idle_time=None,
|
|
|
|
avatar_sha=None):
|
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,
|
|
|
|
our_chatstate=our_chatstate, chatstate=chatstate,
|
2017-09-16 11:49:31 +02:00
|
|
|
idle_time=idle_time, avatar_sha=avatar_sha)
|
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='',
|
|
|
|
keyID=''):
|
|
|
|
# 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',
|
|
|
|
status='', sub='none', keyID=keyID)
|
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,
|
|
|
|
our_chatstate=contact.our_chatstate, chatstate=contact.chatstate,
|
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)
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
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
|
2017-08-13 13:18:56 +02:00
|
|
|
if common.app.jid_is_transport(jid) and not \
|
2010-02-08 15:08:40 +01:00
|
|
|
_('Transports') in groups:
|
|
|
|
# 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)
|
|
|
|
else:
|
|
|
|
raise AttributeError(attr_name)
|
|
|
|
|
|
|
|
def create_gc_contact(self, room_jid, account, name='', show='', status='',
|
2017-09-16 11:49:31 +02:00
|
|
|
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
|
|
|
|
return GC_Contact(room_jid, account, name, show, status, role, affiliation, jid,
|
2017-09-16 11:49:31 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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):
|
|
|
|
if contact.jid not in self._contacts:
|
|
|
|
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':
|
|
|
|
# Do not use self.remove_contact: it deteles
|
|
|
|
# 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)
|
|
|
|
if len(self._contacts[contact.jid]) == 0:
|
|
|
|
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
|
|
|
|
"""
|
|
|
|
return self._contacts.get(jid, [])
|
|
|
|
|
|
|
|
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
|
|
|
|
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)
|
2010-02-08 15:08:40 +01:00
|
|
|
return self.get_contact(barejid, resource)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
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 ?
|
|
|
|
if not len(self._rooms[gc_contact.room_jid]):
|
|
|
|
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
|
|
|
|
|
|
|
def get_gc_contact(self, room_jid, nick):
|
|
|
|
nick_list = self.get_nick_list(room_jid)
|
|
|
|
if not nick in nick_list:
|
|
|
|
return None
|
|
|
|
return self._rooms[room_jid][nick]
|
|
|
|
|
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])
|
|
|
|
return gcc != None
|
|
|
|
|
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:
|
2017-08-13 13:18:56 +02:00
|
|
|
common.app.connections[brother_account].store_metacontacts(
|
2010-02-08 15:08:40 +01:00
|
|
|
self._metacontacts_tags[brother_account])
|
|
|
|
# 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})
|
2017-08-13 13:18:56 +02:00
|
|
|
common.app.connections[account].store_metacontacts(
|
2010-02-08 15:08:40 +01:00
|
|
|
self._metacontacts_tags[account])
|
|
|
|
|
|
|
|
def remove_metacontact(self, account, jid):
|
|
|
|
if not account in self._metacontacts_tags:
|
|
|
|
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)
|
2017-08-13 13:18:56 +02:00
|
|
|
common.app.connections[account].store_metacontacts(
|
2010-02-08 15:08:40 +01:00
|
|
|
self._metacontacts_tags[account])
|
|
|
|
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 = []
|
|
|
|
for account in self._metacontacts_tags:
|
|
|
|
if tag in self._metacontacts_tags[account]:
|
|
|
|
for data in self._metacontacts_tags[account][tag]:
|
|
|
|
data['account'] = account
|
|
|
|
answers.append(data)
|
|
|
|
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):
|
|
|
|
"""
|
|
|
|
Which of the family will be the big brother under wich all others will be
|
|
|
|
?
|
|
|
|
"""
|
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()
|