From 16fa30a1e7352204ce6549356bc9ff6852d9ff41 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Thu, 19 Feb 2009 13:18:23 +0000
Subject: [PATCH] handle stream error and show message to user. Fixes #4660,
 #4823

---
 src/common/connection.py         | 10 +++++++---
 src/common/xmpp/client_nb.py     |  1 +
 src/common/xmpp/dispatcher_nb.py |  2 +-
 src/common/xmpp/simplexml.py     |  3 +++
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index d09b95d1b5..6eb2269e76 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -178,6 +178,7 @@ class Connection(ConnectionHandlers):
 		# server {'icq': ['icq.server.com', 'icq2.server.com'], }
 		self.vcard_supported = True
 		self.private_storage_supported = True
+		self.streamError = ''
 	# END __init__
 
 	def put_event(self, ev):
@@ -515,7 +516,7 @@ class Connection(ConnectionHandlers):
 		self._connect_to_next_host()
 
 
-	def _connect_to_next_host(self, retry = False):
+	def _connect_to_next_host(self, retry=False):
 		log.debug('Connection to next host')
 		if len(self._hosts):
 			# No config option exist when creating a new account
@@ -618,15 +619,18 @@ class Connection(ConnectionHandlers):
 				msg = '%s over proxy %s:%s' % (msg, self._proxy['host'], self._proxy['port'])
 			log.info(msg)
 
-	def _connect_failure(self, con_type = None):
+	def _connect_failure(self, con_type=None):
 		if not con_type:
 			# we are not retrying, and not conecting
 			if not self.retrycount and self.connected != 0:
 				self.disconnect(on_purpose = True)
 				self.dispatch('STATUS', 'offline')
+				sectxt = ''
+				if self.streamError:
+					sectxt = _('Server replied: %s\n') % self.streamError
 				self.dispatch('CONNECTION_LOST',
 					(_('Could not connect to "%s"') % self._hostname,
-					_('Check your connection or try again later.')))
+					_('%sCheck your connection or try again later.') % sectxt))
 
 	def on_proxy_failure(self, reason):
 		log.error('Connection to proxy failed: %s' % reason)
diff --git a/src/common/xmpp/client_nb.py b/src/common/xmpp/client_nb.py
index 1b4dd81980..554be2199d 100644
--- a/src/common/xmpp/client_nb.py
+++ b/src/common/xmpp/client_nb.py
@@ -122,6 +122,7 @@ class NonBlockingClient:
 				# proper connection is not established yet and it's not a proxy
 				# issue
 				log.debug('calling on_connect_failure cb')
+				self._caller.streamError = message
 				self.on_connect_failure()
 			else:
 				# with open connection, we are calling the disconnect handlers
diff --git a/src/common/xmpp/dispatcher_nb.py b/src/common/xmpp/dispatcher_nb.py
index f5335d0e57..6663cf9c63 100644
--- a/src/common/xmpp/dispatcher_nb.py
+++ b/src/common/xmpp/dispatcher_nb.py
@@ -193,7 +193,7 @@ class XMPPDispatcher(PlugIn):
 			self.Stream.Parse(data)
 			# end stream:stream tag received
 			if self.Stream and self.Stream.has_received_endtag():
-				self._owner.disconnect()
+				self._owner.disconnect(self.Stream.streamError)
 				return 0
 		except ExpatError:
 			log.error('Invalid XML received from server. Forcing disconnect.')
diff --git a/src/common/xmpp/simplexml.py b/src/common/xmpp/simplexml.py
index 237f1cec2d..50885bc9bc 100644
--- a/src/common/xmpp/simplexml.py
+++ b/src/common/xmpp/simplexml.py
@@ -355,6 +355,7 @@ class NodeBuilder:
 		self.last_is_data = 1
 		self._ptr=None
 		self.data_buffer = None
+		self.streamError = ''
 		if data:
 			self._parser.Parse(data,1)
 
@@ -411,6 +412,8 @@ class NodeBuilder:
 		log.info("DEPTH -> %i , tag -> %s" % (self.__depth, tag))
 		self.check_data_buffer()
 		if self.__depth == self._dispatch_depth:
+			if self._mini_dom.getName() == 'error':
+				self.streamError = self._mini_dom.getChildren()[0].getName()
 			self.dispatch(self._mini_dom)
 		elif self.__depth > self._dispatch_depth:
 			self._ptr = self._ptr.parent
-- 
GitLab