From d73e277f40508e107cb1e887be111ea4121b8b08 Mon Sep 17 00:00:00 2001
From: Jefry Lagrange <jefry.reyes@gmail.com>
Date: Fri, 10 Jun 2011 23:07:34 -0400
Subject: [PATCH] resuming before binding

---
 src/common/connection.py     |  7 ++++---
 src/common/xmpp/auth_nb.py   | 16 ++++++++--------
 src/common/xmpp/client_nb.py | 14 +++++++++++++-
 src/common/xmpp/smacks.py    |  2 +-
 4 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/src/common/connection.py b/src/common/connection.py
index 46daadfa0c..e5ac4b86d4 100644
--- a/src/common/connection.py
+++ b/src/common/connection.py
@@ -712,7 +712,7 @@ class Connection(CommonConnection, ConnectionHandlers):
         self.streamError = ''
         self.secret_hmac = str(random.random())[2:]
         
-        self.sm = Smacks(self)
+        self.sm = Smacks(self) # Stream Management 
         
         gajim.ged.register_event_handler('privacy-list-received', ged.CORE,
             self._nec_privacy_list_received)
@@ -805,8 +805,9 @@ class Connection(CommonConnection, ConnectionHandlers):
             self.old_show = gajim.SHOW_LIST[self.connected]
         self.connected = 0
         if not self.on_purpose:
-            gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
-                show='offline'))
+            if not (self.sm and self.sm.resumption):
+                gajim.nec.push_incoming_event(OurShowEvent(None, conn=self,
+                                                           show='offline'))
             self.disconnect()
             if gajim.config.get_per('accounts', self.name, 'autoreconnect'):
                 self.connected = -1
diff --git a/src/common/xmpp/auth_nb.py b/src/common/xmpp/auth_nb.py
index 9039b12f5f..79b967c204 100644
--- a/src/common/xmpp/auth_nb.py
+++ b/src/common/xmpp/auth_nb.py
@@ -574,6 +574,7 @@ class NonBlockingBind(PlugIn):
         PlugIn.__init__(self)
         self.bound = None
         self.supports_sm = False
+        self.resuming = False
 
     def plugin(self, owner):
         ''' Start resource binding, if allowed at this time. Used internally. '''
@@ -597,6 +598,8 @@ class NonBlockingBind(PlugIn):
 
         if feats.getTag('sm', namespace=NS_STREAM_MGMT):
             self.supports_sm = True # server supports stream management
+            if self.resuming:
+                self._owner._caller.sm.resume_request()
 
         if not feats.getTag('bind', namespace=NS_BIND):
             log.info('Server does not requested binding.')
@@ -621,6 +624,8 @@ class NonBlockingBind(PlugIn):
         """
         Perform binding. Use provided resource name or random (if not provided).
         """
+        if self.resuming: # We don't bind if we resume the stream
+            return
         self.on_bound = on_bound
         self._resource = resource
         if self._resource:
@@ -646,13 +651,8 @@ class NonBlockingBind(PlugIn):
                 sm = self._owner._caller.sm
                 if self.supports_sm:
                     # starts negociation
-                    if sm._owner and sm.resumption:
-                        sm.set_owner(self._owner)
-                        sm.resume_request()
-                    else:
-                        sm.set_owner(self._owner)
-                        sm.negociate()
-                        
+                    sm.set_owner(self._owner)
+                    sm.negociate()    
                     self._owner.Dispatcher.sm = sm
                     
                 if hasattr(self, 'session') and self.session == -1:
@@ -670,7 +670,7 @@ class NonBlockingBind(PlugIn):
         else:
             log.info('Binding failed: timeout expired.')
             self.on_bound(None)
-
+            
     def _on_session(self, resp):
         self._owner.onreceive(None)
         if isResultNode(resp):
diff --git a/src/common/xmpp/client_nb.py b/src/common/xmpp/client_nb.py
index dc4c4476b8..aafb5f775a 100644
--- a/src/common/xmpp/client_nb.py
+++ b/src/common/xmpp/client_nb.py
@@ -521,8 +521,20 @@ class NonBlockingClient:
             self.connected = None # FIXME: is this intended? We use ''elsewhere
             self._on_sasl_auth(None)
         elif self.SASL.startsasl == 'success':
-            auth_nb.NonBlockingBind.get_instance().PlugIn(self)
+            nb_bind = auth_nb.NonBlockingBind.get_instance() 
+            sm = self._caller.sm
+            if  sm._owner and sm.resumption:
+                nb_bind.resuming = True
+                sm.set_owner(self)
+                self.Dispatcher.sm = sm
+                nb_bind.PlugIn(self)
+                return
+                
+            
+            nb_bind.PlugIn(self)
             self.onreceive(self._on_auth_bind)
+            
+           
         return True
 
     def _on_auth_bind(self, data):
diff --git a/src/common/xmpp/smacks.py b/src/common/xmpp/smacks.py
index b1b6dac03d..182ef00741 100644
--- a/src/common/xmpp/smacks.py
+++ b/src/common/xmpp/smacks.py
@@ -73,7 +73,7 @@ class Smacks():
             return
         resume = Acks()
         resume.buildResume(self.in_h, self.session_id)
-        self._owner.Connection.send(resume, True)            
+        self._owner.Connection.send(resume, False)            
     
     def send_ack(self, disp, stanza):
         ack = Acks()
-- 
GitLab