2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2017-08-13 13:18:56 +02:00
|
|
|
## gajim/common/app.py
|
2005-04-15 19:02:23 +02:00
|
|
|
##
|
2014-01-02 09:33:54 +01:00
|
|
|
## Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Travis Shirk <travis AT pobox.com>
|
|
|
|
## Nikos Kouremenos <kourem AT gmail.com>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2006 Junglecow J <junglecow AT gmail.com>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Stefan Bethge <stefan AT lanpartei.de>
|
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
## Stephan Erb <steve-e AT h3c.de>
|
|
|
|
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
|
2018-04-24 19:36:33 +02:00
|
|
|
## Copyright (C) 2018 Philipp Hörist <philipp @ hoerist.com>
|
2005-04-15 19:02:23 +02:00
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-04-15 19:02:23 +02: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.
|
2005-04-15 19:02:23 +02:00
|
|
|
##
|
2007-12-12 09:44:46 +01:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-04-15 19:02:23 +02: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
|
2005-04-15 19:02:23 +02: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
|
|
|
##
|
2005-04-15 19:02:23 +02:00
|
|
|
|
2014-03-09 10:25:26 +01:00
|
|
|
import os
|
2017-11-24 18:54:30 +01:00
|
|
|
import sys
|
2005-04-15 19:02:23 +02:00
|
|
|
import logging
|
2015-12-17 20:10:06 +01:00
|
|
|
import uuid
|
2017-03-28 16:59:02 +02:00
|
|
|
from distutils.version import LooseVersion as V
|
2017-11-25 15:33:54 +01:00
|
|
|
from collections import namedtuple
|
2005-08-09 20:45:16 +02:00
|
|
|
|
2018-04-24 19:36:33 +02:00
|
|
|
import nbxmpp
|
2017-09-16 11:49:31 +02:00
|
|
|
|
2018-05-21 18:49:06 +02:00
|
|
|
import gajim
|
2017-09-16 11:49:31 +02:00
|
|
|
from gajim.common import config as c_config
|
2017-08-24 01:18:01 +02:00
|
|
|
from gajim.common import configpaths
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import ged as ged_module
|
2017-08-24 01:18:01 +02:00
|
|
|
from gajim.common.contacts import LegacyContactsAPI
|
|
|
|
from gajim.common.events import Events
|
2006-10-11 17:51:42 +02:00
|
|
|
|
2005-10-20 13:17:17 +02:00
|
|
|
interface = None # The actual interface (the gtk one for the moment)
|
2017-08-24 23:20:35 +02:00
|
|
|
thread_interface = lambda *args: None # Interface to run a thread and then a callback
|
2017-09-16 11:49:31 +02:00
|
|
|
config = c_config.Config()
|
2018-05-21 18:49:06 +02:00
|
|
|
version = gajim.__version__
|
2006-10-04 02:10:49 +02:00
|
|
|
connections = {} # 'account name': 'account (connection.Connection) instance'
|
2017-09-16 11:49:31 +02:00
|
|
|
avatar_cache = {}
|
2008-04-14 17:19:09 +02:00
|
|
|
ipython_window = None
|
2017-01-26 21:34:54 +01:00
|
|
|
app = None # Gtk.Application
|
2005-05-17 22:31:43 +02:00
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
ged = ged_module.GlobalEventsDispatcher() # Global Events Dispatcher
|
2008-08-18 18:35:14 +02:00
|
|
|
nec = None # Network Events Controller
|
|
|
|
plugin_manager = None # Plugins Manager
|
2010-01-19 21:32:45 +01:00
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
logger = None
|
2005-11-22 15:46:37 +01:00
|
|
|
|
2018-04-21 12:44:10 +02:00
|
|
|
# For backwards compatibility needed
|
|
|
|
# some plugins use that
|
2006-11-04 19:15:38 +01:00
|
|
|
gajimpaths = configpaths.gajimpaths
|
2005-11-22 12:54:33 +01:00
|
|
|
|
2006-02-10 14:03:32 +01:00
|
|
|
|
2017-11-25 15:33:54 +01:00
|
|
|
RecentGroupchat = namedtuple('RecentGroupchat', ['room', 'server', 'nickname'])
|
|
|
|
|
2008-12-24 16:00:38 +01:00
|
|
|
os_info = None # used to cache os information
|
|
|
|
|
2006-09-14 18:48:03 +02:00
|
|
|
transport_type = {} # list the type of transport
|
|
|
|
|
2018-06-22 00:47:29 +02:00
|
|
|
last_message_time = {} # list of time of the latest incoming message
|
2012-08-16 18:22:29 +02:00
|
|
|
# {acct1: {jid1: time1, jid2: time2}, }
|
|
|
|
encrypted_chats = {} # list of encrypted chats {acct1: [jid1, jid2], ..}
|
2005-07-18 23:08:31 +02:00
|
|
|
|
2009-12-01 23:31:17 +01:00
|
|
|
contacts = LegacyContactsAPI()
|
2012-08-16 18:22:29 +02:00
|
|
|
gc_connected = {} # tell if we are connected to the room or not
|
|
|
|
# {acct: {room_jid: True}}
|
|
|
|
gc_passwords = {} # list of the pass required to enter a room
|
|
|
|
# {room_jid: password}
|
|
|
|
automatic_rooms = {} # list of rooms that must be automaticaly configured
|
|
|
|
# and for which we have a list of invities
|
|
|
|
#{account: {room_jid: {'invities': []}}}
|
|
|
|
new_room_nick = None # if it's != None, use this nick instead of asking for
|
|
|
|
# a new nickname when there is a conflict.
|
2005-07-25 22:04:24 +02:00
|
|
|
|
2005-07-18 23:08:31 +02:00
|
|
|
groups = {} # list of groups
|
|
|
|
newly_added = {} # list of contacts that has just signed in
|
|
|
|
to_be_removed = {} # list of contacts that has just signed out
|
2005-11-23 19:15:24 +01:00
|
|
|
|
2006-09-14 18:48:03 +02:00
|
|
|
events = Events()
|
|
|
|
|
2011-05-04 14:02:38 +02:00
|
|
|
notification = None
|
2011-05-01 22:09:50 +02:00
|
|
|
|
2005-07-18 23:08:31 +02:00
|
|
|
nicks = {} # list of our nick names in each account
|
2006-03-28 01:32:39 +02:00
|
|
|
# should we block 'contact signed in' notifications for this account?
|
2006-03-28 01:35:46 +02:00
|
|
|
# this is only for the first 30 seconds after we change our show
|
|
|
|
# to something else than offline
|
2006-09-14 18:48:03 +02:00
|
|
|
# can also contain account/transport_jid to block notifications for contacts
|
|
|
|
# from this transport
|
2006-03-28 01:32:39 +02:00
|
|
|
block_signed_in_notifications = {}
|
2005-07-18 23:08:31 +02:00
|
|
|
con_types = {} # type of each connection (ssl, tls, tcp, ...)
|
2005-07-22 01:47:27 +02:00
|
|
|
|
|
|
|
sleeper_state = {} # whether we pass auto away / xa or not
|
|
|
|
#'off': don't use sleeper for this account
|
|
|
|
#'online': online and use sleeper
|
|
|
|
#'autoaway': autoaway and use sleeper
|
|
|
|
#'autoxa': autoxa and use sleeper
|
2005-07-22 23:27:04 +02:00
|
|
|
status_before_autoaway = {}
|
2005-07-22 02:01:05 +02:00
|
|
|
|
2007-01-02 12:43:57 +01:00
|
|
|
# jid of transport contacts for which we need to ask avatar when transport will
|
|
|
|
# be online
|
|
|
|
transport_avatar = {} # {transport_jid: [jid_list]}
|
|
|
|
|
2007-06-03 12:04:20 +02:00
|
|
|
# Is Gnome configured to activate on single click ?
|
|
|
|
single_click = False
|
2005-08-16 21:26:11 +02:00
|
|
|
SHOW_LIST = ['offline', 'connecting', 'online', 'chat', 'away', 'xa', 'dnd',
|
2010-02-08 15:08:40 +01:00
|
|
|
'invisible', 'error']
|
2005-07-22 02:01:05 +02:00
|
|
|
|
2006-09-23 19:05:20 +02:00
|
|
|
# zeroconf account name
|
2006-09-25 22:14:17 +02:00
|
|
|
ZEROCONF_ACC_NAME = 'Local'
|
2006-09-23 19:05:20 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
# These will be set in app.gui_interface.
|
2017-02-08 03:49:33 +01:00
|
|
|
idlequeue = None
|
|
|
|
socks5queue = None
|
|
|
|
|
2008-04-21 00:58:47 +02:00
|
|
|
gajim_identity = {'type': 'pc', 'category': 'client', 'name': 'Gajim'}
|
2012-12-09 21:37:51 +01:00
|
|
|
gajim_common_features = [nbxmpp.NS_BYTESTREAM, nbxmpp.NS_SI, nbxmpp.NS_FILE,
|
|
|
|
nbxmpp.NS_MUC, nbxmpp.NS_MUC_USER, nbxmpp.NS_MUC_ADMIN, nbxmpp.NS_MUC_OWNER,
|
|
|
|
nbxmpp.NS_MUC_CONFIG, nbxmpp.NS_COMMANDS, nbxmpp.NS_DISCO_INFO, 'ipv6',
|
|
|
|
'jabber:iq:gateway', nbxmpp.NS_LAST, nbxmpp.NS_PRIVACY, nbxmpp.NS_PRIVATE,
|
|
|
|
nbxmpp.NS_REGISTER, nbxmpp.NS_VERSION, nbxmpp.NS_DATA, nbxmpp.NS_ENCRYPTED,
|
|
|
|
'msglog', 'sslc2s', 'stringprep', nbxmpp.NS_PING, nbxmpp.NS_TIME_REVISED,
|
|
|
|
nbxmpp.NS_SSN, nbxmpp.NS_MOOD, nbxmpp.NS_ACTIVITY, nbxmpp.NS_NICK,
|
2017-06-14 21:29:52 +02:00
|
|
|
nbxmpp.NS_ROSTERX, nbxmpp.NS_SECLABEL, nbxmpp.NS_HASHES_2,
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_HASHES_MD5, nbxmpp.NS_HASHES_SHA1, nbxmpp.NS_HASHES_SHA256,
|
2017-09-21 20:36:45 +02:00
|
|
|
nbxmpp.NS_HASHES_SHA512, nbxmpp.NS_CONFERENCE, nbxmpp.NS_CORRECT,
|
2017-09-16 11:49:31 +02:00
|
|
|
nbxmpp.NS_EME, 'urn:xmpp:avatar:metadata+notify']
|
2008-04-21 00:58:47 +02:00
|
|
|
|
2008-05-27 12:24:29 +02:00
|
|
|
# Optional features gajim supports per account
|
|
|
|
gajim_optional_features = {}
|
|
|
|
|
|
|
|
# Capabilities hash per account
|
|
|
|
caps_hash = {}
|
2008-04-21 00:58:47 +02:00
|
|
|
|
2018-04-24 19:36:33 +02:00
|
|
|
_dependencies = {
|
2018-05-23 00:12:43 +02:00
|
|
|
'PYTHON-DBUS': False,
|
2018-04-24 19:36:33 +02:00
|
|
|
'PYBONJOUR': False,
|
|
|
|
'PYGPG': False,
|
|
|
|
'GPG_BINARY': False,
|
|
|
|
'FARSTREAM': False,
|
|
|
|
'GEOCLUE': False,
|
|
|
|
'UPNP': False,
|
|
|
|
'PYCURL': False,
|
|
|
|
'GSPELL': False,
|
|
|
|
'IDLE': False,
|
2018-05-21 19:17:47 +02:00
|
|
|
}
|
2018-04-24 19:36:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_installed(dependency):
|
|
|
|
if dependency == 'GPG':
|
|
|
|
# Alias for checking python-gnupg and the GPG binary
|
|
|
|
return _dependencies['PYGPG'] and _dependencies['GPG_BINARY']
|
|
|
|
if dependency == 'ZEROCONF':
|
|
|
|
# Alias for checking zeroconf libs
|
2018-05-23 00:12:43 +02:00
|
|
|
return _dependencies['PYTHON-DBUS'] or _dependencies['PYBONJOUR']
|
2018-04-24 19:36:33 +02:00
|
|
|
return _dependencies[dependency]
|
|
|
|
|
2018-05-04 00:36:10 +02:00
|
|
|
def is_flatpak():
|
2018-05-27 13:45:07 +02:00
|
|
|
return gajim.IS_FLATPAK
|
2018-05-04 00:36:10 +02:00
|
|
|
|
2018-04-24 19:36:33 +02:00
|
|
|
def disable_dependency(dependency):
|
|
|
|
_dependencies[dependency] = False
|
|
|
|
|
|
|
|
def detect_dependencies():
|
|
|
|
import gi
|
|
|
|
|
|
|
|
# ZEROCONF
|
|
|
|
try:
|
|
|
|
if os.name == 'nt':
|
|
|
|
import pybonjour
|
|
|
|
_dependencies['PYBONJOUR'] = True
|
|
|
|
else:
|
2018-05-23 00:12:43 +02:00
|
|
|
import dbus
|
|
|
|
_dependencies['PYTHON-DBUS'] = True
|
2018-04-24 19:36:33 +02:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# python-gnupg
|
|
|
|
try:
|
|
|
|
import gnupg
|
|
|
|
'''
|
|
|
|
We need https://pypi.python.org/pypi/python-gnupg
|
|
|
|
but https://pypi.python.org/pypi/gnupg shares the same package name.
|
|
|
|
It cannot be used as a drop-in replacement.
|
|
|
|
We test with a version check if python-gnupg is installed as it is
|
|
|
|
on a much lower version number than gnupg
|
|
|
|
Also we need at least python-gnupg 0.3.8
|
|
|
|
'''
|
|
|
|
v_gnupg = gnupg.__version__
|
|
|
|
if V(v_gnupg) < V('0.3.8') or V(v_gnupg) > V('1.0.0'):
|
|
|
|
log('gajim').info('Gajim needs python-gnupg >= 0.3.8')
|
|
|
|
raise ImportError
|
|
|
|
_dependencies['PYGPG'] = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# GPG BINARY
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
def test_gpg(binary='gpg'):
|
|
|
|
if os.name == 'nt':
|
|
|
|
gpg_cmd = binary + ' -h >nul 2>&1'
|
|
|
|
else:
|
|
|
|
gpg_cmd = binary + ' -h >/dev/null 2>&1'
|
|
|
|
if subprocess.call(gpg_cmd, shell=True):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
if test_gpg(binary='gpg2'):
|
|
|
|
_dependencies['GPG_BINARY'] = 'gpg2'
|
|
|
|
elif test_gpg(binary='gpg'):
|
|
|
|
_dependencies['GPG_BINARY'] = 'gpg'
|
|
|
|
|
|
|
|
# FARSTREAM
|
|
|
|
try:
|
|
|
|
if os.name == 'nt':
|
|
|
|
os.environ['FS_PLUGIN_PATH'] = 'gtk\\lib\\farstream-0.1'
|
|
|
|
os.environ['GST_PLUGIN_PATH'] = 'gtk\\lib\\gstreamer-0.10'
|
|
|
|
gi.require_version('Farstream', '0.2')
|
|
|
|
from gi.repository import Farstream
|
|
|
|
gi.require_version('Gst', '1.0')
|
|
|
|
from gi.repository import Gst
|
|
|
|
try:
|
|
|
|
Gst.init(None)
|
|
|
|
conference = Gst.ElementFactory.make('fsrtpconference', None)
|
|
|
|
session = conference.new_session(Farstream.MediaType.AUDIO)
|
|
|
|
except Exception as error:
|
|
|
|
log('gajim').info(error)
|
|
|
|
_dependencies['FARSTREAM'] = True
|
|
|
|
except (ImportError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# GEOCLUE
|
|
|
|
try:
|
|
|
|
gi.require_version('Geoclue', '2.0')
|
|
|
|
from gi.repository import Geoclue
|
|
|
|
_dependencies['GEOCLUE'] = True
|
|
|
|
except (ImportError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# UPNP
|
|
|
|
try:
|
|
|
|
gi.require_version('GUPnPIgd', '1.0')
|
|
|
|
from gi.repository import GUPnPIgd
|
|
|
|
gupnp_igd = GUPnPIgd.SimpleIgd()
|
|
|
|
_dependencies['UPNP'] = True
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# PYCURL
|
|
|
|
try:
|
|
|
|
import pycurl
|
|
|
|
_dependencies['PYCURL'] = True
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# IDLE
|
|
|
|
try:
|
2018-05-21 02:20:30 +02:00
|
|
|
from gajim.common import idle
|
|
|
|
if idle.Monitor.is_available():
|
2018-04-24 19:36:33 +02:00
|
|
|
_dependencies['IDLE'] = True
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# GSPELL
|
|
|
|
try:
|
|
|
|
gi.require_version('Gspell', '1')
|
|
|
|
from gi.repository import Gspell
|
|
|
|
langs = Gspell.language_get_available()
|
|
|
|
for lang in langs:
|
|
|
|
log('gajim').info('%s (%s) dict available',
|
|
|
|
lang.get_name(), lang.get_code())
|
|
|
|
if langs:
|
|
|
|
_dependencies['GSPELL'] = True
|
|
|
|
except (ImportError, ValueError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Print results
|
|
|
|
for dep, val in _dependencies.items():
|
|
|
|
log('gajim').info('%-13s %s', dep, val)
|
|
|
|
|
|
|
|
def get_gpg_binary():
|
|
|
|
return _dependencies['GPG_BINARY']
|
|
|
|
|
2012-08-31 13:42:44 +02:00
|
|
|
def get_an_id():
|
2015-12-17 20:10:06 +01:00
|
|
|
return str(uuid.uuid4())
|
2012-08-31 13:42:44 +02:00
|
|
|
|
2005-07-22 16:30:35 +02:00
|
|
|
def get_nick_from_jid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
pos = jid.find('@')
|
|
|
|
return jid[:pos]
|
2005-07-22 16:30:35 +02:00
|
|
|
|
2005-09-11 17:02:22 +02:00
|
|
|
def get_server_from_jid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
pos = jid.find('@') + 1 # after @
|
|
|
|
return jid[pos:]
|
2005-09-11 17:02:22 +02:00
|
|
|
|
2006-10-09 02:27:03 +02:00
|
|
|
def get_name_and_server_from_jid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
name = get_nick_from_jid(jid)
|
|
|
|
server = get_server_from_jid(jid)
|
|
|
|
return name, server
|
2005-09-11 17:02:22 +02:00
|
|
|
|
2005-07-26 09:22:08 +02:00
|
|
|
def get_room_and_nick_from_fjid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
# fake jid is the jid for a contact in a room
|
|
|
|
# gaim@conference.jabber.no/nick/nick-continued
|
|
|
|
# return ('gaim@conference.jabber.no', 'nick/nick-continued')
|
|
|
|
l = jid.split('/', 1)
|
|
|
|
if len(l) == 1: # No nick
|
|
|
|
l.append('')
|
|
|
|
return l
|
2005-11-02 09:12:54 +01:00
|
|
|
|
|
|
|
def get_real_jid_from_fjid(account, fjid):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return real jid or returns None, if we don't know the real jid
|
|
|
|
"""
|
|
|
|
room_jid, nick = get_room_and_nick_from_fjid(fjid)
|
|
|
|
if not nick: # It's not a fake_jid, it is a real jid
|
|
|
|
return fjid # we return the real jid
|
|
|
|
real_jid = fjid
|
|
|
|
if interface.msg_win_mgr.get_gc_control(room_jid, account):
|
|
|
|
# It's a pm, so if we have real jid it's in contact.jid
|
|
|
|
gc_contact = contacts.get_gc_contact(account, room_jid, nick)
|
|
|
|
if not gc_contact:
|
|
|
|
return
|
|
|
|
# gc_contact.jid is None when it's not a real jid (we don't know real jid)
|
|
|
|
real_jid = gc_contact.jid
|
|
|
|
return real_jid
|
2005-11-02 09:12:54 +01:00
|
|
|
|
2005-09-27 00:29:52 +02:00
|
|
|
def get_room_from_fjid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
return get_room_and_nick_from_fjid(jid)[0]
|
2005-07-22 16:30:35 +02:00
|
|
|
|
2005-07-22 02:01:05 +02:00
|
|
|
def get_contact_name_from_jid(account, jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
c = contacts.get_first_contact_from_jid(account, jid)
|
|
|
|
return c.name
|
2005-12-06 18:43:21 +01:00
|
|
|
|
2005-07-22 02:34:08 +02:00
|
|
|
def get_jid_without_resource(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
return jid.split('/')[0]
|
2005-07-22 16:30:35 +02:00
|
|
|
|
|
|
|
def construct_fjid(room_jid, nick):
|
2010-02-08 15:08:40 +01:00
|
|
|
# fake jid is the jid for a contact in a room
|
|
|
|
# gaim@conference.jabber.org/nick
|
|
|
|
return room_jid + '/' + nick
|
2005-12-06 18:43:21 +01:00
|
|
|
|
2005-07-22 16:30:35 +02:00
|
|
|
def get_resource_from_jid(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
jids = jid.split('/', 1)
|
|
|
|
if len(jids) > 1:
|
|
|
|
return jids[1] # abc@doremi.org/res/res-continued
|
|
|
|
else:
|
|
|
|
return ''
|
2008-12-02 15:09:18 +01:00
|
|
|
|
2005-08-03 15:46:48 +02:00
|
|
|
def get_number_of_accounts():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the number of ALL accounts
|
|
|
|
"""
|
|
|
|
return len(connections.keys())
|
2005-08-26 15:11:20 +02:00
|
|
|
|
2006-04-08 12:28:53 +02:00
|
|
|
def get_number_of_connected_accounts(accounts_list = None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Returns the number of CONNECTED accounts. Uou can optionally pass an
|
|
|
|
accounts_list and if you do those will be checked, else all will be checked
|
|
|
|
"""
|
|
|
|
connected_accounts = 0
|
|
|
|
if accounts_list is None:
|
|
|
|
accounts = connections.keys()
|
|
|
|
else:
|
|
|
|
accounts = accounts_list
|
|
|
|
for account in accounts:
|
|
|
|
if account_is_connected(account):
|
|
|
|
connected_accounts = connected_accounts + 1
|
|
|
|
return connected_accounts
|
2006-04-08 12:28:53 +02:00
|
|
|
|
2017-11-25 15:33:54 +01:00
|
|
|
def get_connected_accounts():
|
|
|
|
"""
|
|
|
|
Returns a list of CONNECTED accounts
|
|
|
|
"""
|
|
|
|
account_list = []
|
|
|
|
for account in connections:
|
|
|
|
if account_is_connected(account):
|
|
|
|
account_list.append(account)
|
|
|
|
return account_list
|
|
|
|
|
2018-07-16 19:44:38 +02:00
|
|
|
def get_enabled_accounts_with_labels(exclude_local=True, connected_only=False,
|
|
|
|
private_storage_only=False):
|
2018-07-06 22:14:32 +02:00
|
|
|
"""
|
|
|
|
Returns a list with [account, account_label] entries.
|
|
|
|
Order by account_label
|
|
|
|
"""
|
|
|
|
accounts = []
|
|
|
|
for acc in connections:
|
2018-07-16 19:44:38 +02:00
|
|
|
if exclude_local and account_is_zeroconf(acc):
|
|
|
|
continue
|
|
|
|
if connected_only and not account_is_connected(acc):
|
|
|
|
continue
|
|
|
|
if private_storage_only and not account_supports_private_storage(acc):
|
|
|
|
continue
|
|
|
|
|
|
|
|
acc_label = config.get_per(
|
|
|
|
'accounts', acc, 'account_label') or acc
|
|
|
|
accounts.append([acc, acc_label])
|
|
|
|
|
2018-07-06 22:14:32 +02:00
|
|
|
accounts.sort(key=lambda xs: str.lower(xs[1]))
|
|
|
|
return accounts
|
|
|
|
|
2018-07-16 19:44:38 +02:00
|
|
|
def account_is_zeroconf(account):
|
|
|
|
return connections[account].is_zeroconf
|
|
|
|
|
|
|
|
def account_supports_private_storage(account):
|
|
|
|
return connections[account].private_storage_supported
|
|
|
|
|
2006-10-04 02:10:49 +02:00
|
|
|
def account_is_connected(account):
|
2010-02-08 15:08:40 +01:00
|
|
|
if account not in connections:
|
|
|
|
return False
|
|
|
|
if connections[account].connected > 1: # 0 is offline, 1 is connecting
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2006-10-04 02:10:49 +02:00
|
|
|
|
2018-06-30 19:23:10 +02:00
|
|
|
def is_invisible(account):
|
|
|
|
return SHOW_LIST[connections[account].connected] == 'invisible'
|
|
|
|
|
2006-10-04 02:10:49 +02:00
|
|
|
def account_is_disconnected(account):
|
2010-02-08 15:08:40 +01:00
|
|
|
return not account_is_connected(account)
|
2006-10-04 02:10:49 +02:00
|
|
|
|
2008-06-13 04:18:16 +02:00
|
|
|
def zeroconf_is_connected():
|
2010-02-08 15:08:40 +01:00
|
|
|
return account_is_connected(ZEROCONF_ACC_NAME) and \
|
|
|
|
config.get_per('accounts', ZEROCONF_ACC_NAME, 'is_zeroconf')
|
2008-06-13 04:18:16 +02:00
|
|
|
|
2017-11-25 15:33:54 +01:00
|
|
|
def in_groupchat(account, room_jid):
|
|
|
|
if room_jid not in gc_connected[account]:
|
|
|
|
return False
|
|
|
|
return gc_connected[account][room_jid]
|
|
|
|
|
2006-10-04 02:10:49 +02:00
|
|
|
def get_number_of_securely_connected_accounts():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the number of the accounts that are SSL/TLS connected
|
|
|
|
"""
|
|
|
|
num_of_secured = 0
|
|
|
|
for account in connections.keys():
|
|
|
|
if account_is_securely_connected(account):
|
|
|
|
num_of_secured += 1
|
|
|
|
return num_of_secured
|
2006-10-04 02:10:49 +02:00
|
|
|
|
|
|
|
def account_is_securely_connected(account):
|
2010-02-08 15:08:40 +01:00
|
|
|
if account_is_connected(account) and \
|
|
|
|
account in con_types and con_types[account] in ('tls', 'ssl'):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2006-10-04 02:10:49 +02:00
|
|
|
|
2005-08-26 15:15:16 +02:00
|
|
|
def get_transport_name_from_jid(jid, use_config_setting = True):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2018-05-31 20:23:49 +02:00
|
|
|
Returns 'gg', 'irc' etc
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
If JID is not from transport returns None.
|
|
|
|
"""
|
|
|
|
#FIXME: jid can be None! one TB I saw had this problem:
|
|
|
|
# in the code block # it is a groupchat presence in handle_event_notify
|
|
|
|
# jid was None. Yann why?
|
|
|
|
if not jid or (use_config_setting and not config.get('use_transports_iconsets')):
|
|
|
|
return
|
|
|
|
|
|
|
|
host = get_server_from_jid(jid)
|
|
|
|
if host in transport_type:
|
|
|
|
return transport_type[host]
|
|
|
|
|
|
|
|
# host is now f.e. icq.foo.org or just icq (sometimes on hacky transports)
|
|
|
|
host_splitted = host.split('.')
|
|
|
|
if len(host_splitted) != 0:
|
|
|
|
# now we support both 'icq.' and 'icq' but not icqsucks.org
|
|
|
|
host = host_splitted[0]
|
|
|
|
|
2018-06-15 12:19:02 +02:00
|
|
|
if host in ('irc', 'icq', 'sms', 'weather', 'mrim', 'facebook'):
|
2010-02-08 15:08:40 +01:00
|
|
|
return host
|
|
|
|
elif host == 'gg':
|
|
|
|
return 'gadu-gadu'
|
|
|
|
elif host == 'jit':
|
|
|
|
return 'icq'
|
|
|
|
elif host == 'facebook':
|
|
|
|
return 'facebook'
|
|
|
|
else:
|
|
|
|
return None
|
2005-08-26 15:11:20 +02:00
|
|
|
|
|
|
|
def jid_is_transport(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
# if not '@' or '@' starts the jid then it is transport
|
|
|
|
if jid.find('@') <= 0:
|
|
|
|
return True
|
|
|
|
return False
|
2005-09-09 22:52:29 +02:00
|
|
|
|
2017-07-23 09:55:13 +02:00
|
|
|
def get_jid_from_account(account_name):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the jid we use in the given account
|
|
|
|
"""
|
|
|
|
name = config.get_per('accounts', account_name, 'name')
|
|
|
|
hostname = config.get_per('accounts', account_name, 'hostname')
|
|
|
|
jid = name + '@' + hostname
|
|
|
|
return jid
|
2005-09-11 02:32:58 +02:00
|
|
|
|
2018-05-10 10:53:56 +02:00
|
|
|
def get_account_from_jid(jid):
|
2018-05-16 05:39:44 +02:00
|
|
|
for account in config.get_per('accounts'):
|
2018-05-10 10:53:56 +02:00
|
|
|
if jid == get_jid_from_account(account):
|
2018-05-10 13:28:26 +02:00
|
|
|
return account
|
2018-05-10 10:53:56 +02:00
|
|
|
|
2006-02-26 15:13:13 +01:00
|
|
|
def get_our_jids():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Returns a list of the jids we use in our accounts
|
|
|
|
"""
|
|
|
|
our_jids = list()
|
|
|
|
for account in contacts.get_accounts():
|
|
|
|
our_jids.append(get_jid_from_account(account))
|
|
|
|
return our_jids
|
2006-02-26 15:13:13 +01:00
|
|
|
|
2005-11-20 14:27:37 +01:00
|
|
|
def get_hostname_from_account(account_name, use_srv = False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Returns hostname (if custom hostname is used, that is returned)
|
|
|
|
"""
|
|
|
|
if use_srv and connections[account_name].connected_hostname:
|
|
|
|
return connections[account_name].connected_hostname
|
|
|
|
if config.get_per('accounts', account_name, 'use_custom_host'):
|
|
|
|
return config.get_per('accounts', account_name, 'custom_host')
|
|
|
|
return config.get_per('accounts', account_name, 'hostname')
|
2005-10-18 11:07:52 +02:00
|
|
|
|
2006-01-20 18:40:45 +01:00
|
|
|
def get_notification_image_prefix(jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Returns the prefix for the notification images
|
|
|
|
"""
|
|
|
|
transport_name = get_transport_name_from_jid(jid)
|
2018-06-15 12:19:02 +02:00
|
|
|
if transport_name in ('icq', 'facebook'):
|
2010-02-08 15:08:40 +01:00
|
|
|
prefix = transport_name
|
|
|
|
else:
|
|
|
|
prefix = 'jabber'
|
|
|
|
return prefix
|
2006-01-20 18:40:45 +01:00
|
|
|
|
2006-01-20 18:50:01 +01:00
|
|
|
def get_name_from_jid(account, jid):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return from JID's shown name and if no contact returns jids
|
|
|
|
"""
|
|
|
|
contact = contacts.get_first_contact_from_jid(account, jid)
|
|
|
|
if contact:
|
|
|
|
actor = contact.get_shown_name()
|
|
|
|
else:
|
|
|
|
actor = jid
|
|
|
|
return actor
|
2006-10-04 02:10:49 +02:00
|
|
|
|
2017-11-25 15:33:54 +01:00
|
|
|
def get_muc_domain(account):
|
|
|
|
return connections[account].muc_jid.get('jabber', None)
|
|
|
|
|
|
|
|
def get_recent_groupchats(account):
|
|
|
|
recent_groupchats = config.get_per(
|
|
|
|
'accounts', account, 'recent_groupchats').split()
|
|
|
|
|
|
|
|
recent_list = []
|
|
|
|
for groupchat in recent_groupchats:
|
|
|
|
jid = nbxmpp.JID(groupchat)
|
|
|
|
recent = RecentGroupchat(
|
|
|
|
jid.getNode(), jid.getDomain(), jid.getResource())
|
|
|
|
recent_list.append(recent)
|
|
|
|
return recent_list
|
|
|
|
|
|
|
|
def add_recent_groupchat(account, room_jid, nickname):
|
|
|
|
recent = config.get_per(
|
|
|
|
'accounts', account, 'recent_groupchats').split()
|
|
|
|
full_jid = room_jid + '/' + nickname
|
|
|
|
if full_jid in recent:
|
|
|
|
recent.remove(full_jid)
|
|
|
|
recent.insert(0, full_jid)
|
|
|
|
if len(recent) > 10:
|
|
|
|
recent = recent[0:9]
|
|
|
|
config_value = ' '.join(recent)
|
|
|
|
config.set_per(
|
|
|
|
'accounts', account, 'recent_groupchats', config_value)
|
|
|
|
|
2006-10-04 02:10:49 +02:00
|
|
|
def get_priority(account, show):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the priority an account must have
|
|
|
|
"""
|
|
|
|
if not show:
|
|
|
|
show = 'online'
|
|
|
|
|
|
|
|
if show in ('online', 'chat', 'away', 'xa', 'dnd', 'invisible') and \
|
|
|
|
config.get_per('accounts', account, 'adjust_priority_with_status'):
|
2017-07-24 21:48:46 +02:00
|
|
|
prio = config.get_per('accounts', account, 'autopriority_' + show)
|
|
|
|
else:
|
|
|
|
prio = config.get_per('accounts', account, 'priority')
|
|
|
|
if prio < -128:
|
|
|
|
prio = -128
|
|
|
|
elif prio > 127:
|
|
|
|
prio = 127
|
|
|
|
return prio
|
2017-09-21 19:39:03 +02:00
|
|
|
|
|
|
|
def log(domain):
|
2018-04-24 19:36:33 +02:00
|
|
|
if domain != 'gajim':
|
|
|
|
domain = 'gajim.%s' % domain
|
|
|
|
return logging.getLogger(domain)
|
2017-11-24 18:54:30 +01:00
|
|
|
|
|
|
|
def prefers_app_menu():
|
|
|
|
if sys.platform == 'darwin':
|
|
|
|
return True
|
|
|
|
if sys.platform == 'win32':
|
|
|
|
return False
|
|
|
|
return app.prefers_app_menu()
|
2018-06-28 00:04:17 +02:00
|
|
|
|
2018-07-09 00:21:24 +02:00
|
|
|
def get_app_window(cls, account=None):
|
2018-06-28 00:04:17 +02:00
|
|
|
for win in app.get_windows():
|
2018-07-06 21:43:53 +02:00
|
|
|
if isinstance(cls, str):
|
|
|
|
if type(win).__name__ == cls:
|
2018-07-09 00:21:24 +02:00
|
|
|
if account is not None:
|
|
|
|
if account != win.account:
|
|
|
|
continue
|
2018-07-06 21:43:53 +02:00
|
|
|
return win
|
|
|
|
elif isinstance(win, cls):
|
2018-07-09 00:21:24 +02:00
|
|
|
if account is not None:
|
|
|
|
if account != win.account:
|
|
|
|
continue
|
2018-06-28 00:04:17 +02:00
|
|
|
return win
|
|
|
|
return None
|