2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
# Junglecow J <junglecow AT gmail.com>
|
|
|
|
# Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org>
|
|
|
|
# Travis Shirk <travis AT pobox.com>
|
|
|
|
# Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
# Copyright (C) 2006-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
# Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
|
|
|
|
# Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
# Stephan Erb <steve-e AT h3c.de>
|
|
|
|
# Copyright (C) 2008 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/>.
|
2006-03-19 15:54:00 +01:00
|
|
|
|
2018-12-05 11:35:35 +01:00
|
|
|
import logging
|
2008-12-03 23:07:44 +01:00
|
|
|
import operator
|
2018-12-05 11:35:35 +01:00
|
|
|
import time
|
2017-02-07 20:56:26 +01:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2006-03-19 15:54:00 +01:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2018-12-05 11:35:35 +01:00
|
|
|
from gajim.common import ged
|
|
|
|
from gajim.common import helpers
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import jingle_xtls
|
2018-12-05 11:35:35 +01:00
|
|
|
from gajim.common import modules
|
2017-11-11 21:46:34 +01:00
|
|
|
from gajim.common.caps_cache import muc_caps_cache
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.connection_handlers_events import *
|
2018-04-23 18:32:01 +02:00
|
|
|
from gajim.common.const import KindConstant
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.jingle import ConnectionJingle
|
2018-12-05 11:35:35 +01:00
|
|
|
from gajim.common.nec import NetworkEvent
|
|
|
|
from gajim.common.protocol.bytestream import ConnectionSocks5Bytestream
|
|
|
|
from gajim.common.protocol.bytestream import ConnectionIBBytestream
|
|
|
|
|
2006-03-19 15:54:00 +01:00
|
|
|
|
2008-12-24 13:11:02 +01:00
|
|
|
log = logging.getLogger('gajim.c.connection_handlers')
|
|
|
|
|
2006-03-19 15:54:00 +01:00
|
|
|
# kind of events we can wait for an answer
|
2006-05-01 20:29:12 +02:00
|
|
|
AGENT_REMOVED = 'agent_removed'
|
2006-03-19 15:54:00 +01:00
|
|
|
|
2008-05-17 04:23:46 +02:00
|
|
|
# basic connection handlers used here and in zeroconf
|
|
|
|
class ConnectionHandlersBase:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
# List of IDs we are waiting answers for {id: (type_of_request, data), }
|
|
|
|
self.awaiting_answers = {}
|
|
|
|
# List of IDs that will produce a timeout is answer doesn't arrive
|
|
|
|
# {time_of_the_timeout: (id, message to send to gui), }
|
|
|
|
self.awaiting_timeouts = {}
|
|
|
|
|
|
|
|
# keep track of sessions this connection has with other JIDs
|
|
|
|
self.sessions = {}
|
|
|
|
|
2016-11-24 22:44:52 +01:00
|
|
|
# IDs of sent messages (https://trac.gajim.org/ticket/8222)
|
|
|
|
self.sent_message_ids = []
|
|
|
|
|
2012-09-18 16:07:11 +02:00
|
|
|
# We decrypt GPG messages one after the other. Keep queue in mem
|
|
|
|
self.gpg_messages_to_decrypt = []
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('presence-received', ged.CORE,
|
2010-12-19 10:39:15 +01:00
|
|
|
self._nec_presence_received)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('gc-message-received', ged.CORE,
|
2017-08-08 15:24:41 +02:00
|
|
|
self._nec_gc_message_received)
|
2010-09-08 19:55:19 +02:00
|
|
|
|
2011-01-06 16:50:38 +01:00
|
|
|
def cleanup(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('presence-received', ged.CORE,
|
2011-01-06 16:50:38 +01:00
|
|
|
self._nec_presence_received)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('gc-message-received', ged.CORE,
|
2017-08-08 15:24:41 +02:00
|
|
|
self._nec_gc_message_received)
|
2011-01-06 16:50:38 +01:00
|
|
|
|
2010-12-19 10:39:15 +01:00
|
|
|
def _nec_presence_received(self, obj):
|
|
|
|
account = obj.conn.name
|
|
|
|
if account != self.name:
|
|
|
|
return
|
|
|
|
jid = obj.jid
|
|
|
|
resource = obj.resource or ''
|
|
|
|
|
|
|
|
statuss = ['offline', 'error', 'online', 'chat', 'away', 'xa', 'dnd',
|
|
|
|
'invisible']
|
|
|
|
obj.old_show = 0
|
|
|
|
obj.new_show = statuss.index(obj.show)
|
|
|
|
|
|
|
|
obj.contact_list = []
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
highest = app.contacts.get_contact_with_highest_priority(account, jid)
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.was_highest = (highest and highest.resource == resource)
|
|
|
|
|
|
|
|
# Update contact
|
2017-08-13 13:18:56 +02:00
|
|
|
obj.contact_list = app.contacts.get_contacts(account, jid)
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.contact = None
|
|
|
|
resources = []
|
|
|
|
for c in obj.contact_list:
|
|
|
|
resources.append(c.resource)
|
|
|
|
if c.resource == resource:
|
|
|
|
obj.contact = c
|
|
|
|
break
|
|
|
|
|
|
|
|
if obj.contact:
|
|
|
|
if obj.contact.show in statuss:
|
|
|
|
obj.old_show = statuss.index(obj.contact.show)
|
|
|
|
# nick changed
|
|
|
|
if obj.contact_nickname is not None and \
|
|
|
|
obj.contact.contact_name != obj.contact_nickname:
|
|
|
|
obj.contact.contact_name = obj.contact_nickname
|
|
|
|
obj.need_redraw = True
|
|
|
|
|
2018-03-26 23:47:43 +02:00
|
|
|
elif obj.old_show != obj.new_show or obj.contact.status != \
|
|
|
|
obj.status:
|
|
|
|
obj.need_redraw = True
|
2010-12-19 10:39:15 +01:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
obj.contact = app.contacts.get_first_contact_from_jid(account,
|
2010-12-19 10:39:15 +01:00
|
|
|
jid)
|
|
|
|
if not obj.contact:
|
|
|
|
# Presence of another resource of our jid
|
|
|
|
# Create self contact and add to roster
|
|
|
|
if resource == obj.conn.server_resource:
|
|
|
|
return
|
|
|
|
# Ignore offline presence of unknown self resource
|
|
|
|
if obj.new_show < 2:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
obj.contact = app.contacts.create_self_contact(jid=jid,
|
2010-12-19 10:39:15 +01:00
|
|
|
account=account, show=obj.show, status=obj.status,
|
|
|
|
priority=obj.prio, keyID=obj.keyID,
|
|
|
|
resource=obj.resource)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.contacts.add_contact(account, obj.contact)
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.contact_list.append(obj.contact)
|
|
|
|
elif obj.contact.show in statuss:
|
|
|
|
obj.old_show = statuss.index(obj.contact.show)
|
|
|
|
if (resources != [''] and (len(obj.contact_list) != 1 or \
|
2012-01-21 16:32:52 +01:00
|
|
|
obj.contact_list[0].show not in ('not in roster', 'offline'))) and \
|
2017-08-13 13:18:56 +02:00
|
|
|
not app.jid_is_transport(jid):
|
2010-12-19 10:39:15 +01:00
|
|
|
# Another resource of an existing contact connected
|
|
|
|
obj.old_show = 0
|
2017-08-13 13:18:56 +02:00
|
|
|
obj.contact = app.contacts.copy_contact(obj.contact)
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.contact_list.append(obj.contact)
|
|
|
|
obj.contact.resource = resource
|
|
|
|
|
2018-11-13 23:47:54 +01:00
|
|
|
obj.need_redraw = True
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.need_add_in_roster = True
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.jid_is_transport(jid) and len(obj.contact_list) == 1:
|
2010-12-19 10:39:15 +01:00
|
|
|
# It's not an agent
|
|
|
|
if obj.old_show == 0 and obj.new_show > 1:
|
2017-08-13 13:18:56 +02:00
|
|
|
if not jid in app.newly_added[account]:
|
|
|
|
app.newly_added[account].append(jid)
|
|
|
|
if jid in app.to_be_removed[account]:
|
|
|
|
app.to_be_removed[account].remove(jid)
|
2010-12-19 10:39:15 +01:00
|
|
|
elif obj.old_show > 1 and obj.new_show == 0 and \
|
|
|
|
obj.conn.connected > 1:
|
2017-08-13 13:18:56 +02:00
|
|
|
if not jid in app.to_be_removed[account]:
|
|
|
|
app.to_be_removed[account].append(jid)
|
|
|
|
if jid in app.newly_added[account]:
|
|
|
|
app.newly_added[account].remove(jid)
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.need_redraw = True
|
|
|
|
|
|
|
|
obj.contact.show = obj.show
|
|
|
|
obj.contact.status = obj.status
|
|
|
|
obj.contact.priority = obj.prio
|
2017-08-13 13:18:56 +02:00
|
|
|
attached_keys = app.config.get_per('accounts', account,
|
2012-02-21 23:53:49 +01:00
|
|
|
'attached_gpg_keys').split()
|
|
|
|
if jid in attached_keys:
|
|
|
|
obj.contact.keyID = attached_keys[attached_keys.index(jid) + 1]
|
|
|
|
else:
|
|
|
|
# Do not override assigned key
|
|
|
|
obj.contact.keyID = obj.keyID
|
2010-12-19 10:39:15 +01:00
|
|
|
obj.contact.contact_nickname = obj.contact_nickname
|
2017-07-26 22:24:19 +02:00
|
|
|
obj.contact.idle_time = obj.idle_time
|
2010-12-19 10:39:15 +01:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.jid_is_transport(jid):
|
2010-12-19 10:39:15 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
# It isn't an agent
|
|
|
|
# (when contact signs out or has errors)
|
|
|
|
if obj.show in ('offline', 'error'):
|
|
|
|
# TODO: This causes problems when another
|
|
|
|
# resource signs off!
|
|
|
|
self.stop_all_active_file_transfers(obj.contact)
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get('log_contact_status_changes') and \
|
|
|
|
app.config.should_log(self.name, obj.jid):
|
|
|
|
show = app.logger.convert_show_values_to_db_api_values(obj.show)
|
2017-08-08 14:41:55 +02:00
|
|
|
if show is not None:
|
2017-11-17 21:42:44 +01:00
|
|
|
app.logger.insert_into_logs(self.name,
|
|
|
|
nbxmpp.JID(obj.jid).getStripped(),
|
2018-12-05 11:35:35 +01:00
|
|
|
time.time(),
|
2017-09-29 00:11:29 +02:00
|
|
|
KindConstant.STATUS,
|
|
|
|
message=obj.status,
|
|
|
|
show=show)
|
2010-12-19 10:39:15 +01:00
|
|
|
|
2018-03-24 01:47:03 +01:00
|
|
|
def _check_for_mam_compliance(self, room_jid, stanza_id):
|
|
|
|
namespace = muc_caps_cache.get_mam_namespace(room_jid)
|
|
|
|
if stanza_id is None and namespace == nbxmpp.NS_MAM_2:
|
2018-07-19 21:26:45 +02:00
|
|
|
log.warning('%s announces mam:2 without stanza-id', room_jid)
|
2018-03-24 01:47:03 +01:00
|
|
|
|
2017-08-08 15:24:41 +02:00
|
|
|
def _nec_gc_message_received(self, obj):
|
2017-09-07 16:42:51 +02:00
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
2018-03-24 01:47:03 +01:00
|
|
|
|
2018-11-19 18:04:32 +01:00
|
|
|
if obj.stanza.getType() == 'error':
|
|
|
|
return
|
|
|
|
|
2018-07-29 19:36:02 +02:00
|
|
|
self._check_for_mam_compliance(obj.jid, obj.stanza_id)
|
2018-03-24 01:47:03 +01:00
|
|
|
|
2017-10-22 00:39:56 +02:00
|
|
|
if (app.config.should_log(obj.conn.name, obj.jid) and
|
2017-10-22 23:27:47 +02:00
|
|
|
obj.msgtxt and obj.nick):
|
2017-08-08 15:24:41 +02:00
|
|
|
# if not obj.nick, it means message comes from room itself
|
|
|
|
# usually it hold description and can be send at each connection
|
|
|
|
# so don't store it in logs
|
2017-11-17 21:42:44 +01:00
|
|
|
app.logger.insert_into_logs(self.name,
|
|
|
|
obj.jid,
|
2017-09-29 00:11:29 +02:00
|
|
|
obj.timestamp,
|
|
|
|
KindConstant.GC_MSG,
|
|
|
|
message=obj.msgtxt,
|
|
|
|
contact_name=obj.nick,
|
2017-10-22 23:27:47 +02:00
|
|
|
additional_data=obj.additional_data,
|
2018-07-29 19:36:02 +02:00
|
|
|
stanza_id=obj.stanza_id)
|
2017-10-22 00:39:56 +02:00
|
|
|
app.logger.set_room_last_message_time(obj.room_jid, obj.timestamp)
|
2018-07-29 19:36:02 +02:00
|
|
|
self.get_module('MAM').save_archive_id(
|
|
|
|
obj.room_jid, obj.stanza_id, obj.timestamp)
|
2017-08-08 15:24:41 +02:00
|
|
|
|
2010-12-19 21:22:29 +01:00
|
|
|
# process and dispatch an error message
|
|
|
|
def dispatch_error_message(self, msg, msgtxt, session, frm, tim):
|
|
|
|
error_msg = msg.getErrorMsg()
|
|
|
|
|
|
|
|
if not error_msg:
|
|
|
|
error_msg = msgtxt
|
|
|
|
msgtxt = None
|
|
|
|
|
|
|
|
subject = msg.getSubject()
|
|
|
|
|
|
|
|
if session.is_loggable():
|
2017-11-17 21:42:44 +01:00
|
|
|
app.logger.insert_into_logs(self.name,
|
|
|
|
nbxmpp.JID(frm).getStripped(),
|
2017-09-29 00:11:29 +02:00
|
|
|
tim,
|
|
|
|
KindConstant.ERROR,
|
|
|
|
message=error_msg,
|
|
|
|
subject=subject)
|
2017-08-08 14:41:55 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(MessageErrorEvent(None, conn=self,
|
2010-12-19 21:22:29 +01:00
|
|
|
fjid=frm, error_code=msg.getErrorCode(), error_msg=error_msg,
|
2016-02-08 22:31:36 +01:00
|
|
|
msg=msgtxt, time_=tim, session=session, stanza=msg))
|
2010-12-19 21:22:29 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_sessions(self, jid):
|
|
|
|
"""
|
|
|
|
Get all sessions for the given full jid
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.interface.is_pm_contact(jid, self.name):
|
|
|
|
jid = app.get_jid_without_resource(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
try:
|
2013-01-02 17:53:42 +01:00
|
|
|
return list(self.sessions[jid].values())
|
2010-02-08 15:08:40 +01:00
|
|
|
except KeyError:
|
|
|
|
return []
|
|
|
|
|
|
|
|
def get_or_create_session(self, fjid, thread_id):
|
|
|
|
"""
|
|
|
|
Return an existing session between this connection and 'jid', returns a
|
|
|
|
new one if none exist
|
|
|
|
"""
|
|
|
|
pm = True
|
|
|
|
jid = fjid
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
if not app.interface.is_pm_contact(fjid, self.name):
|
2010-02-08 15:08:40 +01:00
|
|
|
pm = False
|
2017-08-13 13:18:56 +02:00
|
|
|
jid = app.get_jid_without_resource(fjid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
session = self.find_session(jid, thread_id)
|
|
|
|
|
|
|
|
if session:
|
|
|
|
return session
|
|
|
|
|
|
|
|
if pm:
|
|
|
|
return self.make_new_session(fjid, thread_id, type_='pm')
|
2018-09-18 10:14:04 +02:00
|
|
|
return self.make_new_session(fjid, thread_id)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def find_session(self, jid, thread_id):
|
|
|
|
try:
|
|
|
|
if not thread_id:
|
|
|
|
return self.find_null_session(jid)
|
2018-09-18 10:14:04 +02:00
|
|
|
return self.sessions[jid][thread_id]
|
2010-02-08 15:08:40 +01:00
|
|
|
except KeyError:
|
|
|
|
return None
|
|
|
|
|
2018-05-27 01:01:35 +02:00
|
|
|
def terminate_sessions(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
self.sessions = {}
|
|
|
|
|
|
|
|
def delete_session(self, jid, thread_id):
|
|
|
|
if not jid in self.sessions:
|
2017-08-13 13:18:56 +02:00
|
|
|
jid = app.get_jid_without_resource(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
if not jid in self.sessions:
|
|
|
|
return
|
|
|
|
|
|
|
|
del self.sessions[jid][thread_id]
|
|
|
|
|
|
|
|
if not self.sessions[jid]:
|
|
|
|
del self.sessions[jid]
|
|
|
|
|
|
|
|
def find_null_session(self, jid):
|
|
|
|
"""
|
|
|
|
Find all of the sessions between us and a remote jid in which we haven't
|
|
|
|
received a thread_id yet and returns the session that we last sent a
|
|
|
|
message to
|
|
|
|
"""
|
2013-01-02 17:53:42 +01:00
|
|
|
sessions = list(self.sessions[jid].values())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# sessions that we haven't received a thread ID in
|
|
|
|
idless = [s for s in sessions if not s.received_thread_id]
|
|
|
|
|
|
|
|
# filter out everything except the default session type
|
|
|
|
chat_sessions = [s for s in idless if isinstance(s,
|
2017-08-13 13:18:56 +02:00
|
|
|
app.default_session_type)]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if chat_sessions:
|
|
|
|
# return the session that we last sent a message in
|
2018-09-18 10:14:04 +02:00
|
|
|
return sorted(chat_sessions,
|
|
|
|
key=operator.attrgetter('last_send'))[-1]
|
|
|
|
return None
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2013-08-27 17:24:41 +02:00
|
|
|
def get_latest_session(self, jid):
|
|
|
|
"""
|
|
|
|
Get the session that we last sent a message to
|
|
|
|
"""
|
|
|
|
if jid not in self.sessions:
|
|
|
|
return None
|
|
|
|
sessions = self.sessions[jid].values()
|
|
|
|
if not sessions:
|
|
|
|
return None
|
|
|
|
return sorted(sessions, key=operator.attrgetter('last_send'))[-1]
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def find_controlless_session(self, jid, resource=None):
|
|
|
|
"""
|
|
|
|
Find an active session that doesn't have a control attached
|
|
|
|
"""
|
|
|
|
try:
|
2013-01-02 17:53:42 +01:00
|
|
|
sessions = list(self.sessions[jid].values())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# filter out everything except the default session type
|
|
|
|
chat_sessions = [s for s in sessions if isinstance(s,
|
2017-08-13 13:18:56 +02:00
|
|
|
app.default_session_type)]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
orphaned = [s for s in chat_sessions if not s.control]
|
|
|
|
|
|
|
|
if resource:
|
|
|
|
orphaned = [s for s in orphaned if s.resource == resource]
|
|
|
|
|
|
|
|
return orphaned[0]
|
|
|
|
except (KeyError, IndexError):
|
|
|
|
return None
|
|
|
|
|
|
|
|
def make_new_session(self, jid, thread_id=None, type_='chat', cls=None):
|
|
|
|
"""
|
|
|
|
Create and register a new session
|
|
|
|
|
|
|
|
thread_id=None to generate one.
|
|
|
|
type_ should be 'chat' or 'pm'.
|
|
|
|
"""
|
|
|
|
if not cls:
|
2017-08-13 13:18:56 +02:00
|
|
|
cls = app.default_session_type
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
sess = cls(self, nbxmpp.JID(jid), thread_id, type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# determine if this session is a pm session
|
|
|
|
# if not, discard the resource so that all sessions are stored bare
|
2018-09-18 15:24:40 +02:00
|
|
|
if type_ != 'pm':
|
2017-08-13 13:18:56 +02:00
|
|
|
jid = app.get_jid_without_resource(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if not jid in self.sessions:
|
|
|
|
self.sessions[jid] = {}
|
|
|
|
|
|
|
|
self.sessions[jid][sess.thread_id] = sess
|
|
|
|
|
|
|
|
return sess
|
2008-05-17 04:23:46 +02:00
|
|
|
|
2018-08-04 19:47:25 +02:00
|
|
|
class ConnectionHandlers(ConnectionSocks5Bytestream,
|
2018-07-22 19:12:52 +02:00
|
|
|
ConnectionHandlersBase,
|
2018-07-09 00:21:24 +02:00
|
|
|
ConnectionJingle, ConnectionIBBytestream):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
2010-02-22 16:29:01 +01:00
|
|
|
ConnectionSocks5Bytestream.__init__(self)
|
2010-08-16 14:33:50 +02:00
|
|
|
ConnectionIBBytestream.__init__(self)
|
2010-11-24 09:19:38 +01:00
|
|
|
|
|
|
|
# Handle presences BEFORE caps
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.register_incoming_event(PresenceReceivedEvent)
|
2010-11-24 09:19:38 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
ConnectionJingle.__init__(self)
|
|
|
|
ConnectionHandlersBase.__init__(self)
|
|
|
|
|
2013-07-30 20:09:52 +02:00
|
|
|
# IDs of disco#items requests
|
|
|
|
self.disco_items_ids = []
|
|
|
|
# IDs of disco#info requests
|
|
|
|
self.disco_info_ids = []
|
2018-06-24 23:22:49 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
self.continue_connect_info = None
|
|
|
|
|
2018-08-24 21:39:03 +02:00
|
|
|
# If handlers have been registered
|
|
|
|
self.handlers_registered = False
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.register_incoming_event(StreamConflictReceivedEvent)
|
|
|
|
app.nec.register_incoming_event(NotificationEvent)
|
2010-09-09 16:40:58 +02:00
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.register_event_handler('agent-removed', ged.CORE,
|
2010-11-09 21:52:22 +01:00
|
|
|
self._nec_agent_removed)
|
2010-07-20 10:35:38 +02:00
|
|
|
|
2011-01-06 16:50:38 +01:00
|
|
|
def cleanup(self):
|
|
|
|
ConnectionHandlersBase.cleanup(self)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.ged.remove_event_handler('agent-removed', ged.CORE,
|
2011-01-06 16:50:38 +01:00
|
|
|
self._nec_agent_removed)
|
2010-07-20 10:35:38 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def _ErrorCB(self, con, iq_obj):
|
|
|
|
log.debug('ErrorCB')
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(IqErrorReceivedEvent(None, conn=self,
|
2010-10-18 22:05:41 +02:00
|
|
|
stanza=iq_obj))
|
2010-09-08 19:55:19 +02:00
|
|
|
|
2017-09-28 16:50:30 +02:00
|
|
|
def _IqCB(self, con, iq_obj):
|
|
|
|
id_ = iq_obj.getID()
|
|
|
|
|
|
|
|
app.nec.push_incoming_event(NetworkEvent('raw-iq-received',
|
|
|
|
conn=self, stanza=iq_obj))
|
|
|
|
|
|
|
|
# Check if we were waiting a timeout for this id
|
|
|
|
found_tim = None
|
|
|
|
for tim in self.awaiting_timeouts:
|
|
|
|
if id_ == self.awaiting_timeouts[tim][0]:
|
|
|
|
found_tim = tim
|
|
|
|
break
|
|
|
|
if found_tim:
|
|
|
|
del self.awaiting_timeouts[found_tim]
|
|
|
|
|
|
|
|
if id_ not in self.awaiting_answers:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.awaiting_answers[id_][0] == AGENT_REMOVED:
|
|
|
|
jid = self.awaiting_answers[id_][1]
|
|
|
|
app.nec.push_incoming_event(AgentRemovedEvent(None, conn=self,
|
|
|
|
agent=jid))
|
|
|
|
del self.awaiting_answers[id_]
|
2010-08-23 14:35:03 +02:00
|
|
|
|
2010-10-18 22:05:41 +02:00
|
|
|
def _dispatch_gc_msg_with_captcha(self, stanza, msg_obj):
|
|
|
|
msg_obj.stanza = stanza
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(GcMessageReceivedEvent(None,
|
2010-10-18 22:05:41 +02:00
|
|
|
conn=self, msg_obj=msg_obj))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-11-09 21:52:22 +01:00
|
|
|
def _nec_agent_removed(self, obj):
|
|
|
|
if obj.conn.name != self.name:
|
|
|
|
return
|
|
|
|
for jid in obj.jid_list:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.debug('Removing contact %s due to unregistered transport %s',
|
|
|
|
jid, obj.agent)
|
2018-07-23 23:01:39 +02:00
|
|
|
self.get_module('Presence').unsubscribe(jid)
|
2010-11-09 21:52:22 +01:00
|
|
|
# Transport contacts can't have 2 resources
|
2017-08-13 13:18:56 +02:00
|
|
|
if jid in app.to_be_removed[self.name]:
|
2010-11-09 21:52:22 +01:00
|
|
|
# This way we'll really remove it
|
2017-08-13 13:18:56 +02:00
|
|
|
app.to_be_removed[self.name].remove(jid)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def discover_ft_proxies(self):
|
2017-08-13 13:18:56 +02:00
|
|
|
cfg_proxies = app.config.get_per('accounts', self.name,
|
2010-10-06 21:11:23 +02:00
|
|
|
'file_transfer_proxies')
|
2017-08-13 13:18:56 +02:00
|
|
|
our_jid = helpers.parse_jid(app.get_jid_from_account(self.name) + \
|
2010-08-29 20:22:20 +02:00
|
|
|
'/' + self.server_resource)
|
2017-08-13 13:18:56 +02:00
|
|
|
testit = app.config.get_per('accounts', self.name,
|
2011-10-23 20:26:28 +02:00
|
|
|
'test_ft_proxies_on_startup')
|
2010-02-08 15:08:40 +01:00
|
|
|
if cfg_proxies:
|
|
|
|
proxies = [e.strip() for e in cfg_proxies.split(',')]
|
|
|
|
for proxy in proxies:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.proxy65_manager.resolve(proxy, self.connection, our_jid,
|
2011-10-23 20:26:28 +02:00
|
|
|
testit=testit)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-07-26 00:12:04 +02:00
|
|
|
def send_first_presence(self):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.connected > 1 and self.continue_connect_info:
|
|
|
|
msg = self.continue_connect_info[1]
|
|
|
|
sign_msg = self.continue_connect_info[2]
|
|
|
|
signed = ''
|
|
|
|
send_first_presence = True
|
|
|
|
if sign_msg:
|
2010-10-06 21:11:23 +02:00
|
|
|
signed = self.get_signed_presence(msg,
|
|
|
|
self._send_first_presence)
|
2010-02-08 15:08:40 +01:00
|
|
|
if signed is None:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(GPGPasswordRequiredEvent(None,
|
2010-11-17 23:52:14 +01:00
|
|
|
conn=self, callback=self._send_first_presence))
|
2010-10-06 21:11:23 +02:00
|
|
|
# _send_first_presence will be called when user enter
|
|
|
|
# passphrase
|
2010-02-08 15:08:40 +01:00
|
|
|
send_first_presence = False
|
|
|
|
if send_first_presence:
|
|
|
|
self._send_first_presence(signed)
|
|
|
|
|
2010-10-06 21:11:23 +02:00
|
|
|
def _send_first_presence(self, signed=''):
|
2010-02-08 15:08:40 +01:00
|
|
|
show = self.continue_connect_info[0]
|
|
|
|
msg = self.continue_connect_info[1]
|
|
|
|
sign_msg = self.continue_connect_info[2]
|
|
|
|
if sign_msg and not signed:
|
|
|
|
signed = self.get_signed_presence(msg)
|
|
|
|
if signed is None:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(BadGPGPassphraseEvent(None,
|
2010-11-15 16:50:54 +01:00
|
|
|
conn=self))
|
2010-02-08 15:08:40 +01:00
|
|
|
self.USE_GPG = False
|
|
|
|
signed = ''
|
2017-08-13 13:18:56 +02:00
|
|
|
self.connected = app.SHOW_LIST.index(show)
|
2010-02-08 15:08:40 +01:00
|
|
|
sshow = helpers.get_xmpp_show(show)
|
|
|
|
# send our presence
|
|
|
|
if show == 'invisible':
|
|
|
|
self.send_invisible_presence(msg, signed, True)
|
|
|
|
return
|
|
|
|
if show not in ['offline', 'online', 'chat', 'away', 'xa', 'dnd']:
|
|
|
|
return
|
2017-08-13 13:18:56 +02:00
|
|
|
priority = app.get_priority(self.name, sshow)
|
2018-09-01 17:07:30 +02:00
|
|
|
|
|
|
|
self.get_module('Presence').send_presence(
|
|
|
|
priority=priority,
|
|
|
|
show=sshow,
|
|
|
|
status=msg,
|
|
|
|
sign=signed)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if self.connection:
|
|
|
|
self.priority = priority
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(OurShowEvent(None, conn=self,
|
2010-11-06 10:04:41 +01:00
|
|
|
show=show))
|
2018-07-22 12:18:24 +02:00
|
|
|
|
2018-09-01 20:34:56 +02:00
|
|
|
if not self.avatar_conversion:
|
|
|
|
# ask our VCard
|
|
|
|
self.get_module('VCardTemp').request_vcard()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-07-01 02:16:33 +02:00
|
|
|
self.get_module('Bookmarks').get_bookmarks()
|
2018-06-25 20:04:56 +02:00
|
|
|
self.get_module('Annotations').get_annotations()
|
2018-09-09 16:50:55 +02:00
|
|
|
self.get_module('Blocking').get_blocking_list()
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# Inform GUI we just signed in
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(SignedInEvent(None, conn=self))
|
2018-07-03 00:10:55 +02:00
|
|
|
self.get_module('PEP').send_stored_publish()
|
2010-02-08 15:08:40 +01:00
|
|
|
self.continue_connect_info = None
|
|
|
|
|
2010-08-11 08:44:49 +02:00
|
|
|
def _PubkeyGetCB(self, con, iq_obj):
|
2010-08-08 10:42:30 +02:00
|
|
|
log.info('PubkeyGetCB')
|
2010-08-11 08:44:49 +02:00
|
|
|
jid_from = helpers.get_full_jid_from_iq(iq_obj)
|
|
|
|
sid = iq_obj.getAttr('id')
|
|
|
|
jingle_xtls.send_cert(con, jid_from, sid)
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-08-11 08:44:49 +02:00
|
|
|
|
|
|
|
def _PubkeyResultCB(self, con, iq_obj):
|
2010-08-08 10:42:30 +02:00
|
|
|
log.info('PubkeyResultCB')
|
2010-08-11 08:44:49 +02:00
|
|
|
jid_from = helpers.get_full_jid_from_iq(iq_obj)
|
|
|
|
jingle_xtls.handle_new_cert(con, iq_obj, jid_from)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _StreamCB(self, con, obj):
|
2012-04-21 23:52:24 +02:00
|
|
|
log.debug('StreamCB')
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(StreamReceivedEvent(None,
|
2012-06-16 13:51:54 +02:00
|
|
|
conn=self, stanza=obj))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _register_handlers(self, con, con_type):
|
|
|
|
# try to find another way to register handlers in each class
|
|
|
|
# that defines handlers
|
2018-07-26 00:12:04 +02:00
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
con.RegisterHandler('iq', self._siSetCB, 'set', nbxmpp.NS_SI)
|
|
|
|
con.RegisterHandler('iq', self._siErrorCB, 'error', nbxmpp.NS_SI)
|
|
|
|
con.RegisterHandler('iq', self._siResultCB, 'result', nbxmpp.NS_SI)
|
2010-02-08 15:08:40 +01:00
|
|
|
con.RegisterHandler('iq', self._bytestreamSetCB, 'set',
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_BYTESTREAM)
|
2010-02-08 15:08:40 +01:00
|
|
|
con.RegisterHandler('iq', self._bytestreamResultCB, 'result',
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_BYTESTREAM)
|
2010-02-08 15:08:40 +01:00
|
|
|
con.RegisterHandler('iq', self._bytestreamErrorCB, 'error',
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_BYTESTREAM)
|
2010-08-17 15:42:34 +02:00
|
|
|
con.RegisterHandlerOnce('iq', self.IBBAllIqHandler)
|
2012-12-09 21:37:51 +01:00
|
|
|
con.RegisterHandler('iq', self.IBBIqHandler, ns=nbxmpp.NS_IBB)
|
|
|
|
con.RegisterHandler('message', self.IBBMessageHandler, ns=nbxmpp.NS_IBB)
|
2018-07-08 10:29:24 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
con.RegisterHandler('iq', self._JingleCB, 'result')
|
|
|
|
con.RegisterHandler('iq', self._JingleCB, 'error')
|
2012-12-09 21:37:51 +01:00
|
|
|
con.RegisterHandler('iq', self._JingleCB, 'set', nbxmpp.NS_JINGLE)
|
2010-02-08 15:08:40 +01:00
|
|
|
con.RegisterHandler('iq', self._ErrorCB, 'error')
|
|
|
|
con.RegisterHandler('iq', self._IqCB)
|
|
|
|
con.RegisterHandler('iq', self._ResultCB, 'result')
|
2012-04-21 23:52:24 +02:00
|
|
|
con.RegisterHandler('unknown', self._StreamCB,
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_XMPP_STREAMS, xmlns=nbxmpp.NS_STREAMS)
|
2012-04-21 23:52:24 +02:00
|
|
|
con.RegisterHandler('iq', self._PubkeyGetCB, 'get',
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_PUBKEY_PUBKEY)
|
2012-04-21 23:52:24 +02:00
|
|
|
con.RegisterHandler('iq', self._PubkeyResultCB, 'result',
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NS_PUBKEY_PUBKEY)
|
2018-06-24 17:00:13 +02:00
|
|
|
|
2018-07-07 13:52:44 +02:00
|
|
|
for handler in modules.get_handlers(self):
|
2018-12-19 22:54:18 +01:00
|
|
|
if len(handler) == 5:
|
|
|
|
name, func, typ, ns, priority = handler
|
|
|
|
con.RegisterHandler(name, func, typ, ns, priority=priority)
|
|
|
|
else:
|
|
|
|
con.RegisterHandler(*handler)
|
2018-08-24 21:39:03 +02:00
|
|
|
self.handlers_registered = True
|
2018-07-07 13:52:44 +02:00
|
|
|
|
|
|
|
def _unregister_handlers(self):
|
2018-07-07 20:01:47 +02:00
|
|
|
if not self.connection:
|
|
|
|
return
|
2018-07-07 13:52:44 +02:00
|
|
|
for handler in modules.get_handlers(self):
|
2018-12-19 22:54:18 +01:00
|
|
|
if len(handler) > 4:
|
|
|
|
handler = handler[:4]
|
2018-07-07 13:52:44 +02:00
|
|
|
self.connection.UnregisterHandler(*handler)
|
2018-08-24 21:39:03 +02:00
|
|
|
self.handlers_registered = False
|