From efaa1a18e7bdec8d765240ddfcbce14a89b08365 Mon Sep 17 00:00:00 2001
From: Yann Leboulanger <asterix@lagaule.org>
Date: Thu, 15 Mar 2007 12:53:29 +0000
Subject: [PATCH] add a now argument to the send function, so that stanza is
 sent instantly instead of added to queue. Use it to send answer to adhoc
 command when we disconnect. fixes #3008 and #2808

---
 src/common/commands.py           |  2 +-
 src/common/xmpp/dispatcher_nb.py |  8 ++++----
 src/common/xmpp/transports_nb.py | 14 +++++++++-----
 3 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/src/common/commands.py b/src/common/commands.py
index 09f93fea0b..4fdb6d4d72 100644
--- a/src/common/commands.py
+++ b/src/common/commands.py
@@ -135,7 +135,7 @@ class ChangeStatusCommand(AdHocCommand):
 
 		# if going offline, we need to push response so it won't go into
 		# queue and disappear
-		self.connection.connection.send(response, presencetype == 'offline')
+		self.connection.connection.send(response, now = presencetype == 'offline')
 
 		# send new status
 		gajim.interface.roster.send_status(self.connection.name, presencetype,
diff --git a/src/common/xmpp/dispatcher_nb.py b/src/common/xmpp/dispatcher_nb.py
index 9f05282f16..9b3cf342ce 100644
--- a/src/common/xmpp/dispatcher_nb.py
+++ b/src/common/xmpp/dispatcher_nb.py
@@ -398,11 +398,11 @@ class Dispatcher(PlugIn):
 			Additional callback arguments can be specified in args. '''
 		self.SendAndWaitForResponse(stanza, 0, func, args)
 	
-	def send(self, stanza, is_message = False):
+	def send(self, stanza, is_message = False, now = False):
 		''' Serialise stanza and put it on the wire. Assign an unique ID to it before send.
 			Returns assigned ID.'''
 		if type(stanza) in [type(''), type(u'')]: 
-			return self._owner.Connection.send(stanza)
+			return self._owner.Connection.send(stanza, now = now)
 		if not isinstance(stanza, Protocol): 
 			_ID=None
 		elif not stanza.getID():
@@ -426,9 +426,9 @@ class Dispatcher(PlugIn):
 		stanza.setNamespace(self._owner.Namespace)
 		stanza.setParent(self._metastream)
 		if is_message:
-			self._owner.Connection.send(stanza, True)
+			self._owner.Connection.send(stanza, True, now = now)
 		else:
-			self._owner.Connection.send(stanza)
+			self._owner.Connection.send(stanza, now = now)
 		return _ID
 	
 	def disconnect(self):
diff --git a/src/common/xmpp/transports_nb.py b/src/common/xmpp/transports_nb.py
index e10845faa2..c2dfd1924c 100644
--- a/src/common/xmpp/transports_nb.py
+++ b/src/common/xmpp/transports_nb.py
@@ -138,7 +138,7 @@ class SSLWrapper:
 
 		raise NotImplementedException()
 
-	def send(self, data, flags=None):
+	def send(self, data, flags=None, now = False):
 		raise NotImplementedException()
 
 class PyOpenSSLWrapper(SSLWrapper):
@@ -178,7 +178,7 @@ class PyOpenSSLWrapper(SSLWrapper):
 				raise SSLWrapper.Error(self.sock or self.sslobj, e)
 		return retval
 
-	def send(self, data, flags=None):
+	def send(self, data, flags=None, now = False):
 		try:
 			if flags is None: return self.sslobj.send(data)
 			else:		  return self.sslobj.send(data, flags)
@@ -219,7 +219,7 @@ class StdlibSSLWrapper(SSLWrapper):
 				raise SSLWrapper.Error(self.sock or self.sslobj, e)
 		return None
 
-	def send(self, data, flags=None):
+	def send(self, data, flags=None, now = False):
 		# we simply ignore flags since ssl object doesn't support it
 		try:
 			return self.sslobj.write(data)
@@ -574,7 +574,7 @@ class NonBlockingTcp(PlugIn, IdleObject):
 			self.on_connect = None
 		return True
 
-	def send(self, raw_data):
+	def send(self, raw_data, now = False):
 		'''Append raw_data to the queue of messages to be send. 
 		If supplied data is unicode string, encode it to utf-8.
 		'''
@@ -585,7 +585,11 @@ class NonBlockingTcp(PlugIn, IdleObject):
 			r = r.encode('utf-8')
 		elif not isinstance(r, str): 
 			r = ustr(r).encode('utf-8')
-		self.sendqueue.append(r)
+		if now:
+			self.sendqueue.insert(0, r)
+			self._do_send()
+		else:
+			self.sendqueue.append(r)
 		self._plug_idle()
 
 	def _on_send(self):
-- 
GitLab