diff mcabber/mcabber/xmpp.c @ 1682:d1e8fb14ce2d

Destroy lm connection object on disconnect
author Myhailo Danylenko <isbear@ukrpost.net>
date Tue, 19 Jan 2010 09:53:12 +0200
parents 41c26b7d2890
children b09f82f61745
line wrap: on
line diff
--- a/mcabber/mcabber/xmpp.c	Tue Jan 19 15:45:25 2010 +0200
+++ b/mcabber/mcabber/xmpp.c	Tue Jan 19 09:53:12 2010 +0200
@@ -43,7 +43,7 @@
 
 #define RECONNECTION_TIMEOUT    60L
 
-LmConnection* lconnection;
+LmConnection* lconnection = NULL;
 static guint AutoConnection;
 
 inline void update_last_use(void);
@@ -100,7 +100,8 @@
   LmMessage *iq;
   char *cleanjid;
 
-  if (!lm_connection_is_authenticated(lconnection)) return;
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+    return;
 
   cleanjid = jidtodisp(bjid); // Stripping resource, just in case...
 
@@ -138,7 +139,8 @@
   LmMessageNode *x;
   char *cleanjid;
 
-  if (!lm_connection_is_authenticated(lconnection)) return;
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+    return;
 
   // XXX We should check name's and group's correctness
 
@@ -168,7 +170,8 @@
   LmMessage *iq;
   char *cleanjid;
 
-  if (!lm_connection_is_authenticated(lconnection)) return;
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+    return;
 
   cleanjid = jidtodisp(bjid); // Stripping resource, just in case...
 
@@ -303,7 +306,7 @@
   if (encrypted)
     *encrypted = 0;
 
-  if (!lm_connection_is_authenticated(lconnection))
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
     return;
 
   if (!text && type == ROSTER_TYPE_USER)
@@ -484,7 +487,8 @@
   char *rjid, *fjid = NULL;
   struct jep0085 *jep85 = NULL;
 
-  if (!lm_connection_is_authenticated(lconnection)) return;
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+    return;
 
   sl_buddy = roster_find(bjid, jidsearch, ROSTER_TYPE_USER);
 
@@ -542,7 +546,8 @@
   struct jep0022 *jep22 = NULL;
   guint jep22_state;
 
-  if (!lm_connection_is_authenticated(lconnection)) return;
+  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+    return;
 
   rname = strchr(fjid, JID_RESOURCE_SEPARATOR);
   barejid = jidtodisp(fjid);
@@ -840,13 +845,14 @@
 
 gboolean xmpp_reconnect()
 {
-  if (!lm_connection_is_authenticated(lconnection))
+  if (!lconnection)
     xmpp_connect();
   return FALSE;
 }
 
 static void _try_to_reconnect(void)
 {
+  xmpp_disconnect();
   if (AutoConnection)
     g_timeout_add_seconds(RECONNECTION_TIMEOUT, xmpp_reconnect, NULL);
 }
@@ -1595,8 +1601,7 @@
   LmMessageHandler *handler;
   GError *error = NULL;
 
-  if (lconnection && lm_connection_is_open(lconnection))
-    xmpp_disconnect();
+  xmpp_disconnect();
 
   servername = settings_opt_get("server");
   userjid    = settings_opt_get("jid");
@@ -1768,14 +1773,18 @@
 
 void xmpp_disconnect(void)
 {
-  if (!lconnection || !lm_connection_is_authenticated(lconnection))
+  if (!lconnection)
     return;
-
-  // Launch pre-disconnect internal hook
-  hook_execute_internal("hook-pre-disconnect");
-  // Announce it to  everyone else
-  xmpp_setstatus(offline, NULL, "", FALSE);
-  lm_connection_close(lconnection, NULL);
+  if (lm_connection_is_authenticated(lconnection)) {
+    // Launch pre-disconnect internal hook
+    hook_execute_internal("hook-pre-disconnect");
+    // Announce it to  everyone else
+    xmpp_setstatus(offline, NULL, "", FALSE);
+  }
+  if (lm_connection_is_open(lconnection))
+    lm_connection_close(lconnection, NULL);
+  lm_connection_unref(lconnection);
+  lconnection = NULL;
 }
 
 void xmpp_setstatus(enum imstatus st, const char *recipient, const char *msg,
@@ -1805,7 +1814,7 @@
   // Only send the packet if we're online.
   // (But we want to update internal status even when disconnected,
   // in order to avoid some problems during network failures)
-  if (lm_connection_is_authenticated(lconnection)) {
+  if (lconnection && lm_connection_is_authenticated(lconnection)) {
     const char *s_msg = (st != invisible ? msg : NULL);
     m = lm_message_new_presence(st, recipient, s_msg);
     insert_entity_capabilities(m->node, st); // Entity Capabilities (XEP-0115)
@@ -1828,7 +1837,7 @@
   // If we didn't change our _global_ status, we are done
   if (recipient) return;
 
-  if (lm_connection_is_authenticated(lconnection)) {
+  if (lconnection && lm_connection_is_authenticated(lconnection)) {
     // Send presence to chatrooms
     if (st != invisible) {
       struct T_presence room_presence;
@@ -2049,7 +2058,7 @@
   if (!changed)
     return;
 
-  if (lm_connection_is_authenticated(lconnection))
+  if (lconnection && lm_connection_is_authenticated(lconnection))
     send_storage(bookmarks);
   else
     scr_LogPrint(LPRINT_LOGNORM,
@@ -2190,7 +2199,7 @@
   if (!changed)
     return;
 
-  if (lm_connection_is_authenticated(lconnection))
+  if (lconnection && lm_connection_is_authenticated(lconnection))
     send_storage(rosternotes);
   else
     scr_LogPrint(LPRINT_LOGNORM,