about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2013-01-10 22:27:43 +0000
committerJames Booth <boothj5@gmail.com>2013-01-10 22:27:43 +0000
commit3706131e44ab3c1a6ad7e6c165a9c8cb25b2baa9 (patch)
tree28fa051ba86601f621d95a5cb764ea90488b04dd
parentc10880ced492a81115349d66204daeeb26a75864 (diff)
parent0f0e04603a1c8a08532764dddf76b8535e0bb80e (diff)
downloadprofani-tty-3706131e44ab3c1a6ad7e6c165a9c8cb25b2baa9.tar.gz
Merge remote-tracking branch 'dmitry/next'
-rw-r--r--src/chat_session.c3
-rw-r--r--src/jabber.c67
2 files changed, 31 insertions, 39 deletions
diff --git a/src/chat_session.c b/src/chat_session.c
index 3cd50404..10465032 100644
--- a/src/chat_session.c
+++ b/src/chat_session.c
@@ -63,7 +63,8 @@ chat_sessions_init(void)
 void
 chat_sessions_clear(void)
 {
-    g_hash_table_remove_all(sessions);
+    if (sessions != NULL)
+        g_hash_table_remove_all(sessions);
 }
 
 void
diff --git a/src/jabber.c b/src/jabber.c
index 65ed30df..ffe4d2b6 100644
--- a/src/jabber.c
+++ b/src/jabber.c
@@ -22,6 +22,7 @@
 
 #include <string.h>
 #include <stdlib.h>
+#include <assert.h>
 
 #include <strophe.h>
 
@@ -82,6 +83,13 @@ static int _presence_handler(xmpp_conn_t * const conn,
     xmpp_stanza_t * const stanza, void * const userdata);
 static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata);
 
+#define FREE_SET_NULL(resource) \
+{\
+    if (resource != NULL) \
+        free(resource); \
+    resource = NULL; \
+}
+
 void
 jabber_init(const int disable_tls)
 {
@@ -194,8 +202,8 @@ jabber_disconnect(void)
     // if connected, send end stream and wait for response
     if (jabber_conn.conn_status == JABBER_CONNECTED) {
         log_info("Closing connection");
-        xmpp_disconnect(jabber_conn.conn);
         jabber_conn.conn_status = JABBER_DISCONNECTING;
+        xmpp_disconnect(jabber_conn.conn);
 
         while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
             jabber_process_events();
@@ -217,7 +225,7 @@ jabber_process_events(void)
     } else if (prefs_get_reconnect() != 0) {
         if ((jabber_conn.conn_status == JABBER_DISCONNECTED) &&
             (reconnect_timer != NULL)) {
-            if (g_timer_elapsed(reconnect_timer, NULL) > (prefs_get_reconnect() * 1.0)) {
+            if (g_timer_elapsed(reconnect_timer, NULL) > prefs_get_reconnect()) {
                 log_debug("Attempting reconnect as %s", saved_user);
                 jabber_connect(saved_user, saved_password, saved_altdomain);
             }
@@ -514,10 +522,13 @@ jabber_get_status(void)
 void
 jabber_free_resources(void)
 {
-    saved_user = NULL;
-    saved_password = NULL;
+    FREE_SET_NULL(saved_user);
+    FREE_SET_NULL(saved_password);
+    FREE_SET_NULL(saved_account);
+    FREE_SET_NULL(saved_altdomain);
     chat_sessions_clear();
-    g_hash_table_remove_all(sub_requests);
+    if (sub_requests != NULL)
+        g_hash_table_remove_all(sub_requests);
     xmpp_conn_release(jabber_conn.conn);
     xmpp_ctx_free(jabber_conn.ctx);
     xmpp_shutdown();
@@ -772,55 +783,35 @@ _connection_handler(xmpp_conn_t * const conn,
             }
         }
 
-    } else {
-
-        // received close stream response from server after disconnect
-        if (jabber_conn.conn_status == JABBER_DISCONNECTING) {
-            jabber_conn.conn_status = JABBER_DISCONNECTED;
-            jabber_conn.presence = PRESENCE_OFFLINE;
-            if (saved_user != NULL) {
-                free(saved_user);
-                saved_user = NULL;
-            }
-            if (saved_password != NULL) {
-                free(saved_password);
-                saved_password = NULL;
-            }
+    } else if (status == XMPP_CONN_DISCONNECT) {
 
         // lost connection for unkown reason
-        } else if (jabber_conn.conn_status == JABBER_CONNECTED) {
+        if (jabber_conn.conn_status == JABBER_CONNECTED) {
             prof_handle_lost_connection();
             if (prefs_get_reconnect() != 0) {
+                assert(reconnect_timer == NULL);
                 reconnect_timer = g_timer_new();
+                // TODO: free resources but leave saved_* untouched
+            } else {
+                jabber_free_resources();
             }
-            xmpp_stop(ctx);
-            jabber_conn.conn_status = JABBER_DISCONNECTED;
-            jabber_conn.presence = PRESENCE_OFFLINE;
 
         // login attempt failed
-        } else {
+        } else if (jabber_conn.conn_status != JABBER_DISCONNECTING) {
             if (reconnect_timer == NULL) {
                 prof_handle_failed_login();
-                if (saved_user != NULL) {
-                    free(saved_user);
-                    saved_user = NULL;
-                }
-                if (saved_password != NULL) {
-                    free(saved_password);
-                    saved_password = NULL;
-                }
-                xmpp_stop(ctx);
-                jabber_conn.conn_status = JABBER_DISCONNECTED;
-                jabber_conn.presence = PRESENCE_OFFLINE;
+                jabber_free_resources();
             } else {
-                xmpp_stop(ctx);
                 if (prefs_get_reconnect() != 0) {
                     g_timer_start(reconnect_timer);
                 }
-                jabber_conn.conn_status = JABBER_DISCONNECTED;
-                jabber_conn.presence = PRESENCE_OFFLINE;
+                // TODO: free resources but leave saved_* untouched
             }
         }
+
+        // close stream response from server after disconnect is handled too
+        jabber_conn.conn_status = JABBER_DISCONNECTED;
+        jabber_conn.presence = PRESENCE_OFFLINE;
     }
 }
 
3 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514