about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/blocking.c1
-rw-r--r--src/xmpp/bookmark.c6
-rw-r--r--src/xmpp/connection.c784
-rw-r--r--src/xmpp/connection.h35
-rw-r--r--src/xmpp/iq.c129
-rw-r--r--src/xmpp/iq.h2
-rw-r--r--src/xmpp/message.c7
-rw-r--r--src/xmpp/presence.c25
-rw-r--r--src/xmpp/roster.c5
-rw-r--r--src/xmpp/session.c397
-rw-r--r--src/xmpp/session.h54
-rw-r--r--src/xmpp/stanza.c3
-rw-r--r--src/xmpp/xmpp.h40
13 files changed, 835 insertions, 653 deletions
diff --git a/src/xmpp/blocking.c b/src/xmpp/blocking.c
index c78f5452..766313e0 100644
--- a/src/xmpp/blocking.c
+++ b/src/xmpp/blocking.c
@@ -46,6 +46,7 @@
 #include "log.h"
 #include "common.h"
 #include "ui/ui.h"
+#include "xmpp/session.h"
 #include "xmpp/connection.h"
 #include "xmpp/stanza.h"
 #include "xmpp/iq.h"
diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c
index c4b0e4f1..0f88a129 100644
--- a/src/xmpp/bookmark.c
+++ b/src/xmpp/bookmark.c
@@ -177,7 +177,7 @@ bookmark_join(const char *jid)
     if (found == NULL) {
         return FALSE;
     } else {
-        char *account_name = jabber_get_account_name();
+        char *account_name = session_get_account_name();
         ProfAccount *account = accounts_get_account(account_name);
         Bookmark *item = found->data;
         if (!muc_active(item->jid)) {
@@ -276,7 +276,7 @@ _bookmark_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
     if (bookmark_ac == NULL) {
         bookmark_ac = autocomplete_new();
     }
-    my_jid = jid_create(jabber_get_fulljid());
+    my_jid = jid_create(connection_get_fulljid());
 
     ptr = xmpp_stanza_get_children(ptr);
     while (ptr) {
@@ -333,7 +333,7 @@ _bookmark_result_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
         if (autojoin_val) {
             Jid *room_jid;
 
-            char *account_name = jabber_get_account_name();
+            char *account_name = session_get_account_name();
             ProfAccount *account = accounts_get_account(account_name);
             if (nick == NULL) {
                 nick = account->muc_nick;
diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c
index f76fdc14..091c0bcf 100644
--- a/src/xmpp/connection.c
+++ b/src/xmpp/connection.c
@@ -34,9 +34,9 @@
 
 #include "config.h"
 
-#include <assert.h>
-#include <string.h>
 #include <stdlib.h>
+#include <string.h>
+#include <assert.h>
 
 #ifdef HAVE_LIBMESODE
 #include <mesode.h>
@@ -45,444 +45,277 @@
 #include <strophe.h>
 #endif
 
-#include "chat_session.h"
-#include "common.h"
-#include "config/preferences.h"
-#include "jid.h"
 #include "log.h"
-#include "muc.h"
-#include "plugins/plugins.h"
-#include "profanity.h"
+#include "config/preferences.h"
 #include "event/server_events.h"
-#include "xmpp/bookmark.h"
-#include "xmpp/blocking.h"
-#include "xmpp/capabilities.h"
 #include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/iq.h"
-#include "xmpp/message.h"
-#include "xmpp/presence.h"
-#include "xmpp/roster.h"
-#include "xmpp/stanza.h"
-#include "xmpp/xmpp.h"
-
-static struct _jabber_conn_t {
-    xmpp_log_t *log;
-    xmpp_ctx_t *ctx;
-    xmpp_conn_t *conn;
+
+typedef struct prof_conn_t {
+    xmpp_log_t *xmpp_log;
+    xmpp_ctx_t *xmpp_ctx;
+    xmpp_conn_t *xmpp_conn;
     jabber_conn_status_t conn_status;
     char *presence_message;
     int priority;
     char *domain;
-} jabber_conn;
-
-static GHashTable *available_resources;
-static GSList *disco_items;
-
-// for auto reconnect
-static struct {
-    char *name;
-    char *passwd;
-} saved_account;
+    GHashTable *available_resources;
+    GHashTable *features_by_jid;
+} ProfConnection;
 
-static struct {
-    char *name;
-    char *jid;
-    char *passwd;
-    char *altdomain;
-    int port;
-    char *tls_policy;
-} saved_details;
-
-static GTimer *reconnect_timer;
-
-static log_level_t _get_log_level(xmpp_log_level_t xmpp_level);
-static xmpp_log_level_t _get_xmpp_log_level(void);
-
-static void _xmpp_file_logger(void *const userdata, const xmpp_log_level_t level, const char *const area,
-    const char *const msg);
+static ProfConnection conn;
 
 static xmpp_log_t* _xmpp_get_file_logger(void);
-
-static jabber_conn_status_t _jabber_connect(const char *const fulljid, const char *const passwd,
-    const char *const altdomain, int port, const char *const tls_policy);
-
-static void _jabber_reconnect(void);
-static void _jabber_lost_connection(void);
-static void _connection_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status, const int error,
+static xmpp_log_level_t _get_xmpp_log_level(void);
+static void _xmpp_file_logger(void *const userdata, const xmpp_log_level_t level, const char *const area, const char *const msg);
+static log_level_t _get_log_level(const xmpp_log_level_t xmpp_level);
+static void _connection_handler(xmpp_conn_t *const xmpp_conn, const xmpp_conn_event_t status, const int error,
     xmpp_stream_error_t *const stream_error, void *const userdata);
 
-void _connection_free_saved_account(void);
-void _connection_free_saved_details(void);
-void _connection_free_session_data(void);
+#ifdef HAVE_LIBMESODE
+static int _connection_certfail_cb(xmpp_tlscert_t *xmpptlscert, const char *const errormsg);
+#endif
 
-static void
-_info_destroy(DiscoInfo *info)
+void connection_init(void)
 {
-    if (info) {
-        free(info->item);
-        if (info->features) {
-            g_hash_table_destroy(info->features);
-        }
-        free(info);
-    }
-}
-
-void
-jabber_init(void)
-{
-    log_info("Initialising XMPP");
-    jabber_conn.conn_status = JABBER_STARTED;
-    jabber_conn.presence_message = NULL;
-    jabber_conn.conn = NULL;
-    jabber_conn.ctx = NULL;
-    jabber_conn.domain = NULL;
-    presence_sub_requests_init();
-    caps_init();
-    available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy);
-    disco_items = NULL;
     xmpp_initialize();
+    conn.conn_status = JABBER_STARTED;
+    conn.presence_message = NULL;
+    conn.xmpp_conn = NULL;
+    conn.xmpp_ctx = NULL;
+    conn.domain = NULL;
+    conn.features_by_jid = NULL;
+    conn.available_resources = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)resource_destroy);
 }
 
 jabber_conn_status_t
-jabber_connect_with_account(const ProfAccount *const account)
+connection_connect(const char *const fulljid, const char *const passwd, const char *const altdomain, int port,
+    const char *const tls_policy)
 {
-    assert(account != NULL);
-
-    log_info("Connecting using account: %s", account->name);
+    assert(fulljid != NULL);
+    assert(passwd != NULL);
 
-    // save account name and password for reconnect
-    if (saved_account.name) {
-        free(saved_account.name);
-    }
-    saved_account.name = strdup(account->name);
-    if (saved_account.passwd) {
-        free(saved_account.passwd);
+    Jid *jid = jid_create(fulljid);
+    if (jid == NULL) {
+        log_error("Malformed JID not able to connect: %s", fulljid);
+        conn.conn_status = JABBER_DISCONNECTED;
+        return conn.conn_status;
+    } else if (jid->fulljid == NULL) {
+        log_error("Full JID required to connect, received: %s", fulljid);
+        conn.conn_status = JABBER_DISCONNECTED;
+        jid_destroy(jid);
+        return conn.conn_status;
     }
-    saved_account.passwd = strdup(account->password);
-
-    // connect with fulljid
-    Jid *jidp = jid_create_from_bare_and_resource(account->jid, account->resource);
-    jabber_conn_status_t result =
-        _jabber_connect(jidp->fulljid, account->password, account->server, account->port, account->tls_policy);
-    jid_destroy(jidp);
+    jid_destroy(jid);
 
-    return result;
-}
+    log_info("Connecting as %s", fulljid);
 
-jabber_conn_status_t
-jabber_connect_with_details(const char *const jid, const char *const passwd, const char *const altdomain,
-    const int port, const char *const tls_policy)
-{
-    assert(jid != NULL);
-    assert(passwd != NULL);
+    if (conn.xmpp_log) {
+        free(conn.xmpp_log);
+    }
+    conn.xmpp_log = _xmpp_get_file_logger();
 
-    // save details for reconnect, remember name for account creating on success
-    saved_details.name = strdup(jid);
-    saved_details.passwd = strdup(passwd);
-    if (altdomain) {
-        saved_details.altdomain = strdup(altdomain);
-    } else {
-        saved_details.altdomain = NULL;
+    if (conn.xmpp_conn) {
+        xmpp_conn_release(conn.xmpp_conn);
     }
-    if (port != 0) {
-        saved_details.port = port;
-    } else {
-        saved_details.port = 0;
+    if (conn.xmpp_ctx) {
+        xmpp_ctx_free(conn.xmpp_ctx);
     }
-    if (tls_policy) {
-        saved_details.tls_policy = strdup(tls_policy);
-    } else {
-        saved_details.tls_policy = NULL;
+    conn.xmpp_ctx = xmpp_ctx_new(NULL, conn.xmpp_log);
+    if (conn.xmpp_ctx == NULL) {
+        log_warning("Failed to get libstrophe ctx during connect");
+        return JABBER_DISCONNECTED;
     }
-
-    // use 'profanity' when no resourcepart in provided jid
-    Jid *jidp = jid_create(jid);
-    if (jidp->resourcepart == NULL) {
-        jid_destroy(jidp);
-        jidp = jid_create_from_bare_and_resource(jid, "profanity");
-        saved_details.jid = strdup(jidp->fulljid);
-    } else {
-        saved_details.jid = strdup(jid);
+    conn.xmpp_conn = xmpp_conn_new(conn.xmpp_ctx);
+    if (conn.xmpp_conn == NULL) {
+        log_warning("Failed to get libstrophe conn during connect");
+        return JABBER_DISCONNECTED;
     }
-    jid_destroy(jidp);
-
-    // connect with fulljid
-    log_info("Connecting without account, JID: %s", saved_details.jid);
-
-    return _jabber_connect(
-        saved_details.jid,
-        passwd,
-        saved_details.altdomain,
-        saved_details.port,
-        saved_details.tls_policy);
-}
+    xmpp_conn_set_jid(conn.xmpp_conn, fulljid);
+    xmpp_conn_set_pass(conn.xmpp_conn, passwd);
 
-void
-connection_autoping_fail(void)
-{
-    if (jabber_conn.conn_status == JABBER_CONNECTED) {
-        log_info("Closing connection");
-        char *account_name = jabber_get_account_name();
-        const char *fulljid = jabber_get_fulljid();
-        plugins_on_disconnect(account_name, fulljid);
-        accounts_set_last_activity(jabber_get_account_name());
-        jabber_conn.conn_status = JABBER_DISCONNECTING;
-        xmpp_disconnect(jabber_conn.conn);
-
-        while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
-            jabber_process_events(10);
-        }
-        if (jabber_conn.conn) {
-            xmpp_conn_release(jabber_conn.conn);
-            jabber_conn.conn = NULL;
-        }
-        if (jabber_conn.ctx) {
-            xmpp_ctx_free(jabber_conn.ctx);
-            jabber_conn.ctx = NULL;
-        }
+    if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) {
+        xmpp_conn_set_flags(conn.xmpp_conn, XMPP_CONN_FLAG_MANDATORY_TLS);
+    } else if (g_strcmp0(tls_policy, "disable") == 0) {
+        xmpp_conn_set_flags(conn.xmpp_conn, XMPP_CONN_FLAG_DISABLE_TLS);
     }
 
-    FREE_SET_NULL(jabber_conn.presence_message);
-    FREE_SET_NULL(jabber_conn.domain);
+#ifdef HAVE_LIBMESODE
+    char *cert_path = prefs_get_string(PREF_TLS_CERTPATH);
+    if (cert_path) {
+        xmpp_conn_tlscert_path(conn.xmpp_conn, cert_path);
+    }
+    prefs_free_string(cert_path);
 
-    jabber_conn.conn_status = JABBER_DISCONNECTED;
-    _jabber_lost_connection();
-}
+    int connect_status = xmpp_connect_client(
+        conn.xmpp_conn,
+        altdomain,
+        port,
+        _connection_certfail_cb,
+        _connection_handler,
+        conn.xmpp_ctx);
+#else
+    int connect_status = xmpp_connect_client(
+        conn.xmpp_conn,
+        altdomain,
+        port,
+        _connection_handler,
+        conn.xmpp_ctx);
+#endif
 
-void
-jabber_disconnect(void)
-{
-    // if connected, send end stream and wait for response
-    if (jabber_conn.conn_status == JABBER_CONNECTED) {
-        char *account_name = jabber_get_account_name();
-        const char *fulljid = jabber_get_fulljid();
-        plugins_on_disconnect(account_name, fulljid);
-        log_info("Closing connection");
-        accounts_set_last_activity(jabber_get_account_name());
-        jabber_conn.conn_status = JABBER_DISCONNECTING;
-        xmpp_disconnect(jabber_conn.conn);
-
-        while (jabber_get_connection_status() == JABBER_DISCONNECTING) {
-            jabber_process_events(10);
-        }
-        _connection_free_saved_account();
-        _connection_free_saved_details();
-        _connection_free_session_data();
-        if (jabber_conn.conn) {
-            xmpp_conn_release(jabber_conn.conn);
-            jabber_conn.conn = NULL;
-        }
-        if (jabber_conn.ctx) {
-            xmpp_ctx_free(jabber_conn.ctx);
-            jabber_conn.ctx = NULL;
-        }
+    if (connect_status == 0) {
+        conn.conn_status = JABBER_CONNECTING;
+    } else {
+        conn.conn_status = JABBER_DISCONNECTED;
     }
 
-    jabber_conn.conn_status = JABBER_STARTED;
-    FREE_SET_NULL(jabber_conn.presence_message);
-    FREE_SET_NULL(jabber_conn.domain);
+    return conn.conn_status;
 }
 
-void
-jabber_shutdown(void)
+jabber_conn_status_t
+connection_get_status(void)
 {
-    _connection_free_saved_account();
-    _connection_free_saved_details();
-    _connection_free_session_data();
-    xmpp_shutdown();
-    free(jabber_conn.log);
-    jabber_conn.log = NULL;
+    return conn.conn_status;
 }
 
 void
-jabber_process_events(int millis)
-{
-    int reconnect_sec;
-
-    switch (jabber_conn.conn_status)
-    {
-        case JABBER_CONNECTED:
-        case JABBER_CONNECTING:
-        case JABBER_DISCONNECTING:
-            xmpp_run_once(jabber_conn.ctx, millis);
-            break;
-        case JABBER_DISCONNECTED:
-            reconnect_sec = prefs_get_reconnect();
-            if ((reconnect_sec != 0) && reconnect_timer) {
-                int elapsed_sec = g_timer_elapsed(reconnect_timer, NULL);
-                if (elapsed_sec > reconnect_sec) {
-                    _jabber_reconnect();
-                }
-            }
-            break;
-        default:
-            break;
-    }
+connection_set_status(jabber_conn_status_t status)
+{
+    conn.conn_status = status;
 }
 
-GList*
-jabber_get_available_resources(void)
+xmpp_conn_t*
+connection_get_conn(void)
 {
-    return g_hash_table_get_values(available_resources);
+    return conn.xmpp_conn;
 }
 
-jabber_conn_status_t
-jabber_get_connection_status(void)
+xmpp_ctx_t*
+connection_get_ctx(void)
 {
-    return (jabber_conn.conn_status);
+    return conn.xmpp_ctx;
 }
 
-GSList*
-connection_get_disco_items(void)
+const char*
+connection_get_fulljid(void)
 {
-    return (disco_items);
+    return xmpp_conn_get_jid(conn.xmpp_conn);
 }
 
-gboolean
-jabber_service_supports(const char *const feature)
+GHashTable*
+connection_get_features(const char *const jid)
 {
-    DiscoInfo *disco_info;
-    while (disco_items) {
-        disco_info = disco_items->data;
-        if (g_hash_table_lookup_extended(disco_info->features, feature, NULL, NULL)) {
-            return TRUE;
-        }
-        disco_items = g_slist_next(disco_items);
-    }
-
-    return FALSE;
+    return g_hash_table_lookup(conn.features_by_jid, jid);
 }
 
-void
-connection_set_disco_items(GSList *_disco_items)
+GList*
+connection_get_available_resources(void)
 {
-    disco_items = _disco_items;
+    return g_hash_table_get_values(conn.available_resources);
 }
 
-xmpp_conn_t*
-connection_get_conn(void)
+void
+connection_add_available_resource(Resource *resource)
 {
-    return jabber_conn.conn;
+    g_hash_table_replace(conn.available_resources, strdup(resource->name), resource);
 }
 
-xmpp_ctx_t*
-connection_get_ctx(void)
+void
+connection_remove_available_resource(const char *const resource)
 {
-    return jabber_conn.ctx;
+    g_hash_table_remove(conn.available_resources, resource);
 }
 
-const char*
-jabber_get_fulljid(void)
+void
+connection_remove_all_available_resources(void)
 {
-    return xmpp_conn_get_jid(jabber_conn.conn);
+    g_hash_table_remove_all(conn.available_resources);
 }
 
 char*
-jabber_get_presence_message(void)
+connection_create_uuid(void)
 {
-    return jabber_conn.presence_message;
+    return xmpp_uuid_gen(conn.xmpp_ctx);
 }
 
-char*
-jabber_get_account_name(void)
+void
+connection_free_uuid(char *uuid)
 {
-    return saved_account.name;
+    if (uuid) {
+        xmpp_free(conn.xmpp_ctx, uuid);
+    }
 }
 
 char*
-jabber_create_uuid(void)
+connection_get_domain(void)
 {
-    return xmpp_uuid_gen(jabber_conn.ctx);
+    return conn.domain;
 }
 
-void
-jabber_free_uuid(char *uuid)
+char*
+connection_get_presence_msg(void)
 {
-    if (uuid) {
-        xmpp_free(jabber_conn.ctx, uuid);
-    }
+    return conn.presence_message;
 }
 
 void
-connection_set_presence_message(const char *const message)
+connection_free_conn(void)
 {
-    FREE_SET_NULL(jabber_conn.presence_message);
-    if (message) {
-        jabber_conn.presence_message = strdup(message);
+    if (conn.xmpp_conn) {
+        xmpp_conn_release(conn.xmpp_conn);
+        conn.xmpp_conn = NULL;
     }
 }
 
 void
-connection_set_priority(const int priority)
+connection_free_ctx(void)
 {
-    jabber_conn.priority = priority;
+    if (conn.xmpp_ctx) {
+        xmpp_ctx_free(conn.xmpp_ctx);
+        conn.xmpp_ctx = NULL;
+    }
 }
 
 void
-connection_add_available_resource(Resource *resource)
+connection_free_presence_msg(void)
 {
-    g_hash_table_replace(available_resources, strdup(resource->name), resource);
+    FREE_SET_NULL(conn.presence_message);
 }
 
 void
-connection_remove_available_resource(const char *const resource)
+connection_set_presence_msg(const char *const message)
 {
-    g_hash_table_remove(available_resources, resource);
+    FREE_SET_NULL(conn.presence_message);
+    if (message) {
+        conn.presence_message = strdup(message);
+    }
 }
 
 void
-_connection_free_saved_account(void)
+connection_free_domain(void)
 {
-    FREE_SET_NULL(saved_account.name);
-    FREE_SET_NULL(saved_account.passwd);
+    FREE_SET_NULL(conn.domain);
 }
 
 void
-_connection_free_saved_details(void)
+connection_free_log(void)
 {
-    FREE_SET_NULL(saved_details.name);
-    FREE_SET_NULL(saved_details.jid);
-    FREE_SET_NULL(saved_details.passwd);
-    FREE_SET_NULL(saved_details.altdomain);
-    FREE_SET_NULL(saved_details.tls_policy);
+    free(conn.xmpp_log);
+    conn.xmpp_log = NULL;
 }
 
 void
-_connection_free_session_data(void)
+connection_set_priority(const int priority)
 {
-    g_slist_free_full(disco_items, (GDestroyNotify)_info_destroy);
-    disco_items = NULL;
-    g_hash_table_remove_all(available_resources);
-    chat_sessions_clear();
-    presence_clear_sub_requests();
+    conn.priority = priority;
 }
 
 #ifdef HAVE_LIBMESODE
-static int
-_connection_certfail_cb(xmpp_tlscert_t *xmpptlscert, const char *const errormsg)
-{
-    int version = xmpp_conn_tlscert_version(xmpptlscert);
-    char *serialnumber = xmpp_conn_tlscert_serialnumber(xmpptlscert);
-    char *subjectname = xmpp_conn_tlscert_subjectname(xmpptlscert);
-    char *issuername = xmpp_conn_tlscert_issuername(xmpptlscert);
-    char *fingerprint = xmpp_conn_tlscert_fingerprint(xmpptlscert);
-    char *notbefore = xmpp_conn_tlscert_notbefore(xmpptlscert);
-    char *notafter = xmpp_conn_tlscert_notafter(xmpptlscert);
-    char *key_alg = xmpp_conn_tlscert_key_algorithm(xmpptlscert);
-    char *signature_alg = xmpp_conn_tlscert_signature_algorithm(xmpptlscert);
-
-    TLSCertificate *cert = tlscerts_new(fingerprint, version, serialnumber, subjectname, issuername, notbefore,
-        notafter, key_alg, signature_alg);
-    int res = sv_ev_certfail(errormsg, cert);
-    tlscerts_free(cert);
-
-    return res;
-}
-
 TLSCertificate*
-jabber_get_tls_peer_cert(void)
+connection_get_tls_peer_cert(void)
 {
-    xmpp_tlscert_t *xmpptlscert = xmpp_conn_tls_peer_cert(jabber_conn.conn);
+    xmpp_tlscert_t *xmpptlscert = xmpp_conn_tls_peer_cert(conn.xmpp_conn);
     int version = xmpp_conn_tlscert_version(xmpptlscert);
     char *serialnumber = xmpp_conn_tlscert_serialnumber(xmpptlscert);
     char *subjectname = xmpp_conn_tlscert_subjectname(xmpptlscert);
@@ -496,240 +329,132 @@ jabber_get_tls_peer_cert(void)
     TLSCertificate *cert = tlscerts_new(fingerprint, version, serialnumber, subjectname, issuername, notbefore,
         notafter, key_alg, signature_alg);
 
-    xmpp_conn_free_tlscert(jabber_conn.ctx, xmpptlscert);
+    xmpp_conn_free_tlscert(conn.xmpp_ctx, xmpptlscert);
 
     return cert;
 }
 #endif
 
 gboolean
-jabber_conn_is_secured(void)
+connection_is_secured(void)
 {
-    if (jabber_conn.conn_status == JABBER_CONNECTED) {
-        return xmpp_conn_is_secured(jabber_conn.conn) == 0 ? FALSE : TRUE;
+    if (conn.conn_status == JABBER_CONNECTED) {
+        return xmpp_conn_is_secured(conn.xmpp_conn) == 0 ? FALSE : TRUE;
     } else {
         return FALSE;
     }
 }
 
 gboolean
-jabber_send_stanza(const char *const stanza)
+connection_send_stanza(const char *const stanza)
 {
-    if (jabber_conn.conn_status != JABBER_CONNECTED) {
+    if (conn.conn_status != JABBER_CONNECTED) {
         return FALSE;
     } else {
-        xmpp_send_raw_string(jabber_conn.conn, "%s", stanza);
+        xmpp_send_raw_string(conn.xmpp_conn, "%s", stanza);
         return TRUE;
     }
 }
 
-static jabber_conn_status_t
-_jabber_connect(const char *const fulljid, const char *const passwd, const char *const altdomain, int port,
-    const char *const tls_policy)
+void
+connection_disco_items_free(void)
 {
-    assert(fulljid != NULL);
-    assert(passwd != NULL);
-
-    Jid *jid = jid_create(fulljid);
+    g_hash_table_destroy(conn.features_by_jid);
+    conn.features_by_jid = NULL;
+}
 
-    if (jid == NULL) {
-        log_error("Malformed JID not able to connect: %s", fulljid);
-        jabber_conn.conn_status = JABBER_DISCONNECTED;
-        return jabber_conn.conn_status;
-    } else if (jid->fulljid == NULL) {
-        log_error("Full JID required to connect, received: %s", fulljid);
-        jabber_conn.conn_status = JABBER_DISCONNECTED;
-        jid_destroy(jid);
-        return jabber_conn.conn_status;
-    }
+gboolean
+connection_supports(const char *const feature)
+{
+    GList *jids = g_hash_table_get_keys(conn.features_by_jid);
 
-    jid_destroy(jid);
+    GList *curr = jids;
+    while (curr) {
+        char *jid = curr->data;
+        GHashTable *features = g_hash_table_lookup(conn.features_by_jid, jid);
+        if (features && g_hash_table_lookup(features, feature)) {
+            return TRUE;
+        }
 
-    log_info("Connecting as %s", fulljid);
-    if (jabber_conn.log) {
-        free(jabber_conn.log);
+        curr = g_list_next(curr);
     }
-    jabber_conn.log = _xmpp_get_file_logger();
 
-    if (jabber_conn.conn) {
-        xmpp_conn_release(jabber_conn.conn);
-    }
-    if (jabber_conn.ctx) {
-        xmpp_ctx_free(jabber_conn.ctx);
-    }
-    jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log);
-    if (jabber_conn.ctx == NULL) {
-        log_warning("Failed to get libstrophe ctx during connect");
-        return JABBER_DISCONNECTED;
-    }
-    jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx);
-    if (jabber_conn.conn == NULL) {
-        log_warning("Failed to get libstrophe conn during connect");
-        return JABBER_DISCONNECTED;
-    }
-    xmpp_conn_set_jid(jabber_conn.conn, fulljid);
-    xmpp_conn_set_pass(jabber_conn.conn, passwd);
+    g_list_free(jids);
 
-    if (!tls_policy || (g_strcmp0(tls_policy, "force") == 0)) {
-        xmpp_conn_set_flags(jabber_conn.conn, XMPP_CONN_FLAG_MANDATORY_TLS);
-    } else if (g_strcmp0(tls_policy, "disable") == 0) {
-        xmpp_conn_set_flags(jabber_conn.conn, XMPP_CONN_FLAG_DISABLE_TLS);
-    }
+    return FALSE;
+}
 
-#ifdef HAVE_LIBMESODE
-    char *cert_path = prefs_get_string(PREF_TLS_CERTPATH);
-    if (cert_path) {
-        xmpp_conn_tlscert_path(jabber_conn.conn, cert_path);
-    }
-    prefs_free_string(cert_path);
-#endif
+char*
+connection_jid_for_feature(const char *const feature)
+{
+    GList *jids = g_hash_table_get_keys(conn.features_by_jid);
 
-#ifdef HAVE_LIBMESODE
-    int connect_status = xmpp_connect_client(
-        jabber_conn.conn,
-        altdomain,
-        port,
-        _connection_certfail_cb,
-        _connection_handler,
-        jabber_conn.ctx);
-#else
-    int connect_status = xmpp_connect_client(
-        jabber_conn.conn,
-        altdomain,
-        port,
-        _connection_handler,
-        jabber_conn.ctx);
-#endif
+    GList *curr = jids;
+    while (curr) {
+        char *jid = curr->data;
+        GHashTable *features = g_hash_table_lookup(conn.features_by_jid, jid);
+        if (features && g_hash_table_lookup(features, feature)) {
+            return jid;
+        }
 
-    if (connect_status == 0) {
-        jabber_conn.conn_status = JABBER_CONNECTING;
-    } else {
-        jabber_conn.conn_status = JABBER_DISCONNECTED;
+        curr = g_list_next(curr);
     }
 
-    return jabber_conn.conn_status;
+    g_list_free(jids);
+
+    return NULL;
 }
 
-static void
-_jabber_reconnect(void)
+void
+connection_set_disco_items(GSList *items)
 {
-    // reconnect with account.
-    ProfAccount *account = accounts_get_account(saved_account.name);
+    GSList *curr = items;
+    while (curr) {
+        DiscoItem *item = curr->data;
+        g_hash_table_insert(conn.features_by_jid, strdup(item->jid),
+            g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL));
 
-    if (account == NULL) {
-        log_error("Unable to reconnect, account no longer exists: %s", saved_account.name);
-    } else {
-        char *fulljid = create_fulljid(account->jid, account->resource);
-        log_debug("Attempting reconnect with account %s", account->name);
-        _jabber_connect(fulljid, saved_account.passwd, account->server, account->port, account->tls_policy);
-        free(fulljid);
-        g_timer_start(reconnect_timer);
-    }
-}
+        iq_disco_info_request_onconnect(item->jid);
 
-static void
-_jabber_lost_connection(void)
-{
-    sv_ev_lost_connection();
-    if (prefs_get_reconnect() != 0) {
-        assert(reconnect_timer == NULL);
-        reconnect_timer = g_timer_new();
-    } else {
-        _connection_free_saved_account();
-        _connection_free_saved_details();
+        curr = g_slist_next(curr);
     }
-    _connection_free_session_data();
 }
 
 static void
-_connection_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status, const int error,
+_connection_handler(xmpp_conn_t *const xmpp_conn, const xmpp_conn_event_t status, const int error,
     xmpp_stream_error_t *const stream_error, void *const userdata)
 {
     // login success
     if (status == XMPP_CONN_CONNECT) {
         log_debug("Connection handler: XMPP_CONN_CONNECT");
-        jabber_conn.conn_status = JABBER_CONNECTED;
-
-        int secured = xmpp_conn_is_secured(jabber_conn.conn);
 
-        // logged in with account
-        if (saved_account.name) {
-            log_debug("Connection handler: logged in with account name: %s", saved_account.name);
-            sv_ev_login_account_success(saved_account.name, secured);
+        conn.conn_status = JABBER_CONNECTED;
 
-        // logged in without account, use details to create new account
-        } else {
-            log_debug("Connection handler: logged in with jid: %s", saved_details.name);
-            accounts_add(saved_details.name, saved_details.altdomain, saved_details.port, saved_details.tls_policy);
-            accounts_set_jid(saved_details.name, saved_details.jid);
-
-            sv_ev_login_account_success(saved_details.name, secured);
-            saved_account.name = strdup(saved_details.name);
-            saved_account.passwd = strdup(saved_details.passwd);
-
-            _connection_free_saved_details();
-        }
-
-        Jid *my_jid = jid_create(jabber_get_fulljid());
-        jabber_conn.domain = strdup(my_jid->domainpart);
+        Jid *my_jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn));
+        conn.domain = strdup(my_jid->domainpart);
         jid_destroy(my_jid);
 
-        chat_sessions_init();
-
-        message_handlers_init();
-        presence_handlers_init();
-        iq_handlers_init();
-
-        roster_request();
-        bookmark_request();
-        blocking_request();
-
-        // items discovery
-        DiscoInfo *info = malloc(sizeof(struct disco_info_t));
-        info->item = strdup(jabber_conn.domain);
-        info->features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
-        disco_items = g_slist_append(disco_items, info);
-        iq_disco_info_request_onconnect(info->item);
-        iq_disco_items_request_onconnect(jabber_conn.domain);
-
-        if (prefs_get_boolean(PREF_CARBONS)){
-            iq_enable_carbons();
-        }
+        conn.features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_destroy);
+        g_hash_table_insert(conn.features_by_jid, strdup(conn.domain), g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL));
 
-        if ((prefs_get_reconnect() != 0) && reconnect_timer) {
-            g_timer_destroy(reconnect_timer);
-            reconnect_timer = NULL;
-        }
+        session_login_success(connection_is_secured());
 
     } else if (status == XMPP_CONN_DISCONNECT) {
         log_debug("Connection handler: XMPP_CONN_DISCONNECT");
 
         // lost connection for unknown reason
-        if (jabber_conn.conn_status == JABBER_CONNECTED) {
+        if (conn.conn_status == JABBER_CONNECTED) {
             log_debug("Connection handler: Lost connection for unknown reason");
-            _jabber_lost_connection();
+            session_lost_connection();
 
         // login attempt failed
-        } else if (jabber_conn.conn_status != JABBER_DISCONNECTING) {
+        } else if (conn.conn_status != JABBER_DISCONNECTING) {
             log_debug("Connection handler: Login failed");
-            if (reconnect_timer == NULL) {
-                log_debug("Connection handler: No reconnect timer");
-                sv_ev_failed_login();
-                _connection_free_saved_account();
-                _connection_free_saved_details();
-                _connection_free_session_data();
-            } else {
-                log_debug("Connection handler: Restarting reconnect timer");
-                if (prefs_get_reconnect() != 0) {
-                    g_timer_start(reconnect_timer);
-                }
-                // free resources but leave saved_user untouched
-                _connection_free_session_data();
-            }
+            session_login_failed();
         }
 
         // close stream response from server after disconnect is handled too
-        jabber_conn.conn_status = JABBER_DISCONNECTED;
+        conn.conn_status = JABBER_DISCONNECTED;
     } else if (status == XMPP_CONN_FAIL) {
         log_debug("Connection handler: XMPP_CONN_FAIL");
     } else {
@@ -737,22 +462,43 @@ _connection_handler(xmpp_conn_t *const conn, const xmpp_conn_event_t status, con
     }
 }
 
-static log_level_t
-_get_log_level(const xmpp_log_level_t xmpp_level)
+#ifdef HAVE_LIBMESODE
+static int
+_connection_certfail_cb(xmpp_tlscert_t *xmpptlscert, const char *const errormsg)
 {
-    if (xmpp_level == XMPP_LEVEL_DEBUG) {
-        return PROF_LEVEL_DEBUG;
-    } else if (xmpp_level == XMPP_LEVEL_INFO) {
-        return PROF_LEVEL_INFO;
-    } else if (xmpp_level == XMPP_LEVEL_WARN) {
-        return PROF_LEVEL_WARN;
-    } else {
-        return PROF_LEVEL_ERROR;
-    }
+    int version = xmpp_conn_tlscert_version(xmpptlscert);
+    char *serialnumber = xmpp_conn_tlscert_serialnumber(xmpptlscert);
+    char *subjectname = xmpp_conn_tlscert_subjectname(xmpptlscert);
+    char *issuername = xmpp_conn_tlscert_issuername(xmpptlscert);
+    char *fingerprint = xmpp_conn_tlscert_fingerprint(xmpptlscert);
+    char *notbefore = xmpp_conn_tlscert_notbefore(xmpptlscert);
+    char *notafter = xmpp_conn_tlscert_notafter(xmpptlscert);
+    char *key_alg = xmpp_conn_tlscert_key_algorithm(xmpptlscert);
+    char *signature_alg = xmpp_conn_tlscert_signature_algorithm(xmpptlscert);
+
+    TLSCertificate *cert = tlscerts_new(fingerprint, version, serialnumber, subjectname, issuername, notbefore,
+        notafter, key_alg, signature_alg);
+    int res = sv_ev_certfail(errormsg, cert);
+    tlscerts_free(cert);
+
+    return res;
+}
+#endif
+
+static xmpp_log_t*
+_xmpp_get_file_logger(void)
+{
+    xmpp_log_level_t level = _get_xmpp_log_level();
+    xmpp_log_t *file_log = malloc(sizeof(xmpp_log_t));
+
+    file_log->handler = _xmpp_file_logger;
+    file_log->userdata = &level;
+
+    return file_log;
 }
 
 static xmpp_log_level_t
-_get_xmpp_log_level()
+_get_xmpp_log_level(void)
 {
     log_level_t prof_level = log_get_filter();
 
@@ -777,14 +523,16 @@ _xmpp_file_logger(void *const userdata, const xmpp_log_level_t level, const char
     }
 }
 
-static xmpp_log_t*
-_xmpp_get_file_logger()
+static log_level_t
+_get_log_level(const xmpp_log_level_t xmpp_level)
 {
-    xmpp_log_level_t level = _get_xmpp_log_level();
-    xmpp_log_t *file_log = malloc(sizeof(xmpp_log_t));
-
-    file_log->handler = _xmpp_file_logger;
-    file_log->userdata = &level;
-
-    return file_log;
+    if (xmpp_level == XMPP_LEVEL_DEBUG) {
+        return PROF_LEVEL_DEBUG;
+    } else if (xmpp_level == XMPP_LEVEL_INFO) {
+        return PROF_LEVEL_INFO;
+    } else if (xmpp_level == XMPP_LEVEL_WARN) {
+        return PROF_LEVEL_WARN;
+    } else {
+        return PROF_LEVEL_ERROR;
+    }
 }
diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h
index f964aca3..f4d0e387 100644
--- a/src/xmpp/connection.h
+++ b/src/xmpp/connection.h
@@ -35,27 +35,34 @@
 #ifndef XMPP_CONNECTION_H
 #define XMPP_CONNECTION_H
 
-#include "config.h"
+#include "xmpp/xmpp.h"
 
-#ifdef HAVE_LIBMESODE
-#include <mesode.h>
-#endif
-#ifdef HAVE_LIBSTROPHE
-#include <strophe.h>
-#endif
+void connection_init(void);
+
+jabber_conn_status_t connection_connect(const char *const fulljid, const char *const passwd, const char *const altdomain, int port,
+    const char *const tls_policy);
 
-#include "resource.h"
+void connection_set_status(jabber_conn_status_t status);
+void connection_set_presence_msg(const char *const message);
+void connection_set_priority(const int priority);
+void connection_set_priority(int priority);
+void connection_set_disco_items(GSList *items);
 
-typedef int(*ProfIdCallback)(xmpp_stanza_t *const stanza, void *const userdata);
+void connection_free_conn(void);
+void connection_free_ctx(void);
+void connection_free_presence_msg(void);
+void connection_free_domain(void);
+void connection_free_log(void);
 
 xmpp_conn_t* connection_get_conn(void);
 xmpp_ctx_t* connection_get_ctx(void);
-void connection_set_priority(int priority);
-void connection_set_presence_message(const char *const message);
+char *connection_get_domain(void);
+char* connection_jid_for_feature(const char *const feature);
+GHashTable* connection_get_features(const char *const jid);
+void connection_disco_items_free(void);
+
 void connection_add_available_resource(Resource *resource);
 void connection_remove_available_resource(const char *const resource);
-void connection_autoping_fail(void);
-GSList* connection_get_disco_items(void);
-void connection_set_disco_items(GSList *disco_items);
+void connection_remove_all_available_resources(void);
 
 #endif
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index d000defd..c87b45a4 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -59,11 +59,13 @@
 #include "event/server_events.h"
 #include "xmpp/capabilities.h"
 #include "xmpp/blocking.h"
-#include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/stanza.h"
 #include "xmpp/form.h"
 #include "roster_list.h"
 #include "xmpp/xmpp.h"
+#include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/iq.h"
 #include "xmpp/roster.h"
 #include "plugins/plugins.h"
@@ -225,7 +227,7 @@ iq_id_handler_add(const char *const id, ProfIdCallback func, void *userdata)
 void
 iq_autoping_check(void)
 {
-    if (jabber_get_connection_status() != JABBER_CONNECTED) {
+    if (connection_get_status() != JABBER_CONNECTED) {
         return;
     }
 
@@ -243,7 +245,7 @@ iq_autoping_check(void)
     if (timeout > 0 && seconds_elapsed >= timeout) {
         cons_show("Autoping response timed out afer %u seconds.", timeout);
         log_debug("Autoping check: timed out afer %u seconds, disconnecting", timeout);
-        connection_autoping_fail();
+        session_autoping_fail();
         autoping_wait = FALSE;
         g_timer_destroy(autoping_time);
         autoping_time = NULL;
@@ -253,7 +255,7 @@ iq_autoping_check(void)
 void
 iq_set_autoping(const int seconds)
 {
-    if (jabber_get_connection_status() != JABBER_CONNECTED) {
+    if (connection_get_status() != JABBER_CONNECTED) {
         return;
     }
 
@@ -307,36 +309,21 @@ iq_disable_carbons(void)
 void
 iq_http_upload_request(HTTPUpload *upload)
 {
-    GSList *disco_items = connection_get_disco_items();
-    DiscoInfo *disco_info;
-    if (disco_items && (g_slist_length(disco_items) > 0)) {
-        while (disco_items) {
-            disco_info = disco_items->data;
-            if (g_hash_table_lookup_extended(disco_info->features, STANZA_NS_HTTP_UPLOAD, NULL, NULL)) {
-                break;
-            }
-            disco_items = g_slist_next(disco_items);
-            if (!disco_items) {
-                cons_show_error("XEP-0363 HTTP File Upload is not supported by the server");
-                return;
-            }
-        }
-    } else {
-        cons_show_error("No disco items");
+    char *jid = connection_jid_for_feature(STANZA_NS_HTTP_UPLOAD);
+    if (jid == NULL) {
+        cons_show_error("XEP-0363 HTTP File Upload is not supported by the server");
         return;
     }
 
     xmpp_ctx_t * const ctx = connection_get_ctx();
     char *id = create_unique_id("http_upload_request");
-
-    xmpp_stanza_t *iq = stanza_create_http_upload_request(ctx, id, disco_info->item, upload);
-
+    xmpp_stanza_t *iq = stanza_create_http_upload_request(ctx, id, jid, upload);
     iq_id_handler_add(id, _http_upload_response_id_handler, upload);
-
     free(id);
 
     iq_send_stanza(iq);
     xmpp_stanza_release(iq);
+
     return;
 }
 
@@ -959,7 +946,7 @@ _manual_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
 static int
 _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata)
 {
-    if (jabber_get_connection_status() != JABBER_CONNECTED) {
+    if (connection_get_status() != JABBER_CONNECTED) {
         return 1;
     }
 
@@ -1916,32 +1903,19 @@ _disco_info_response_id_handler_onconnect(xmpp_stanza_t *const stanza, void *con
     xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
 
     if (query) {
-        xmpp_stanza_t *child = xmpp_stanza_get_children(query);
-
-        GSList *disco_items = connection_get_disco_items();
-        DiscoInfo *disco_info;
-        if (disco_items && (g_slist_length(disco_items) > 0)) {
-            while (disco_items) {
-                disco_info = disco_items->data;
-                if (g_strcmp0(disco_info->item, from) == 0) {
-                    break;
-                }
-                disco_items = g_slist_next(disco_items);
-                if (!disco_items) {
-                    log_error("No matching disco item found for %s", from);
-                    return 1;
-                }
-            }
-        } else {
+        GHashTable *features = connection_get_features(from);
+        if (features == NULL) {
+            log_error("No matching disco item found for %s", from);
             return 1;
         }
 
+        xmpp_stanza_t *child = xmpp_stanza_get_children(query);
         while (child) {
             const char *stanza_name = xmpp_stanza_get_name(child);
             if (g_strcmp0(stanza_name, STANZA_NAME_FEATURE) == 0) {
                 const char *var = xmpp_stanza_get_attribute(child, STANZA_ATTR_VAR);
                 if (var) {
-                    g_hash_table_add(disco_info->features, strdup(var));
+                    g_hash_table_add(features, strdup(var));
                 }
             }
             child = xmpp_stanza_get_next(child);
@@ -2013,32 +1987,42 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
     const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
     GSList *items = NULL;
 
-    if ((g_strcmp0(id, "confreq") == 0) || (g_strcmp0(id, "discoitemsreq") == 0) || (g_strcmp0(id, "discoitemsreq_onconnect") == 0)) {
-        log_debug("Response to query: %s", id);
-        xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
-
-        if (query) {
-            xmpp_stanza_t *child = xmpp_stanza_get_children(query);
-            while (child) {
-                const char *stanza_name = xmpp_stanza_get_name(child);
-                if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) {
-                    const char *item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
-                    if (item_jid) {
-                        DiscoItem *item = malloc(sizeof(struct disco_item_t));
-                        item->jid = strdup(item_jid);
-                        const char *item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
-                        if (item_name) {
-                            item->name = strdup(item_name);
-                        } else {
-                            item->name = NULL;
-                        }
-                        items = g_slist_append(items, item);
-                    }
-                }
+    if ((g_strcmp0(id, "confreq") != 0) &&
+            (g_strcmp0(id, "discoitemsreq") != 0) &&
+            (g_strcmp0(id, "discoitemsreq_onconnect") != 0)) {
+        return;
+    }
+
+    log_debug("Response to query: %s", id);
 
-                child = xmpp_stanza_get_next(child);
+    xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
+    if (query == NULL) {
+        return;
+    }
+
+    xmpp_stanza_t *child = xmpp_stanza_get_children(query);
+    if (child == NULL) {
+        return;
+    }
+
+    while (child) {
+        const char *stanza_name = xmpp_stanza_get_name(child);
+        if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) {
+            const char *item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
+            if (item_jid) {
+                DiscoItem *item = malloc(sizeof(struct disco_item_t));
+                item->jid = strdup(item_jid);
+                const char *item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
+                if (item_name) {
+                    item->name = strdup(item_name);
+                } else {
+                    item->name = NULL;
+                }
+                items = g_slist_append(items, item);
             }
         }
+
+        child = xmpp_stanza_get_next(child);
     }
 
     if (g_strcmp0(id, "confreq") == 0) {
@@ -2046,18 +2030,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
     } else if (g_strcmp0(id, "discoitemsreq") == 0) {
         cons_show_disco_items(items, from);
     } else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) {
-        GSList *res_items = items;
-        if (res_items && (g_slist_length(res_items) > 0)) {
-            while (res_items) {
-                DiscoItem *item = res_items->data;
-                DiscoInfo *info = malloc(sizeof(struct disco_info_t));
-                info->item = strdup(item->jid);
-                info->features = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
-                connection_set_disco_items(g_slist_append(connection_get_disco_items(), info));
-                iq_disco_info_request_onconnect(info->item);
-                res_items = g_slist_next(res_items);
-            }
-        }
+        connection_set_disco_items(items);
     }
 
     g_slist_free_full(items, (GDestroyNotify)_item_destroy);
diff --git a/src/xmpp/iq.h b/src/xmpp/iq.h
index 274afdc6..2b7a6c6d 100644
--- a/src/xmpp/iq.h
+++ b/src/xmpp/iq.h
@@ -35,6 +35,8 @@
 #ifndef XMPP_IQ_H
 #define XMPP_IQ_H
 
+typedef int(*ProfIdCallback)(xmpp_stanza_t *const stanza, void *const userdata);
+
 void iq_handlers_init(void);
 void iq_send_stanza(xmpp_stanza_t *const stanza);
 void iq_id_handler_add(const char *const id, ProfIdCallback func, void *userdata);
diff --git a/src/xmpp/message.c b/src/xmpp/message.c
index 6d999d61..4d49ba98 100644
--- a/src/xmpp/message.c
+++ b/src/xmpp/message.c
@@ -51,11 +51,12 @@
 #include "profanity.h"
 #include "ui/ui.h"
 #include "event/server_events.h"
-#include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/message.h"
 #include "xmpp/roster.h"
 #include "roster_list.h"
 #include "xmpp/stanza.h"
+#include "xmpp/connection.h"
 #include "xmpp/xmpp.h"
 #include "pgp/gpg.h"
 #include "plugins/plugins.h"
@@ -204,7 +205,7 @@ message_send_chat_pgp(const char *const barejid, const char *const msg)
 
     xmpp_stanza_t *message = NULL;
 #ifdef HAVE_LIBGPGME
-    char *account_name = jabber_get_account_name();
+    char *account_name = session_get_account_name();
     ProfAccount *account = accounts_get_account(account_name);
     if (account->pgp_keyid) {
         Jid *jidp = jid_create(jid);
@@ -739,7 +740,7 @@ _handle_carbons(xmpp_stanza_t *const stanza)
 
         Jid *jid_from = jid_create(from);
         Jid *jid_to = jid_create(to);
-        Jid *my_jid = jid_create(jabber_get_fulljid());
+        Jid *my_jid = jid_create(connection_get_fulljid());
 
         // check for and deal with message
         xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(message, STANZA_NAME_BODY);
diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c
index 4d59326b..6ca5127c 100644
--- a/src/xmpp/presence.c
+++ b/src/xmpp/presence.c
@@ -55,8 +55,9 @@
 #include "profanity.h"
 #include "ui/ui.h"
 #include "event/server_events.h"
-#include "xmpp/capabilities.h"
 #include "xmpp/connection.h"
+#include "xmpp/capabilities.h"
+#include "xmpp/session.h"
 #include "xmpp/stanza.h"
 #include "xmpp/iq.h"
 #include "xmpp/xmpp.h"
@@ -238,7 +239,7 @@ presence_reset_sub_request_search(void)
 void
 presence_send(const resource_presence_t presence_type, const char *const msg, const int idle, char *signed_status)
 {
-    if (jabber_get_connection_status() != JABBER_CONNECTED) {
+    if (connection_get_status() != JABBER_CONNECTED) {
         log_warning("Error setting presence, not connected.");
         return;
     }
@@ -250,10 +251,10 @@ presence_send(const resource_presence_t presence_type, const char *const msg, co
     }
 
     xmpp_ctx_t * const ctx = connection_get_ctx();
-    const int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(), presence_type);
+    const int pri = accounts_get_priority_for_presence_type(session_get_account_name(), presence_type);
     const char *show = stanza_get_presence_string_from_type(presence_type);
 
-    connection_set_presence_message(msg);
+    connection_set_presence_msg(msg);
     connection_set_priority(pri);
 
     xmpp_stanza_t *presence = stanza_create_presence(ctx);
@@ -289,7 +290,7 @@ presence_send(const resource_presence_t presence_type, const char *const msg, co
     if (last == NULL) {
         last = STANZA_TEXT_ONLINE;
     }
-    char *account = jabber_get_account_name();
+    char *account = session_get_account_name();
     accounts_set_last_presence(account, last);
     accounts_set_last_status(account, msg);
     free(id);
@@ -330,10 +331,10 @@ presence_join_room(const char *const room, const char *const nick, const char *c
     log_debug("Sending room join presence to: %s", jid->fulljid);
     xmpp_ctx_t *ctx = connection_get_ctx();
     resource_presence_t presence_type =
-        accounts_get_last_presence(jabber_get_account_name());
+        accounts_get_last_presence(session_get_account_name());
     const char *show = stanza_get_presence_string_from_type(presence_type);
-    char *status = jabber_get_presence_message();
-    int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(),
+    char *status = connection_get_presence_msg();
+    int pri = accounts_get_priority_for_presence_type(session_get_account_name(),
         presence_type);
 
     xmpp_stanza_t *presence = stanza_create_room_join_presence(ctx, jid->fulljid, passwd);
@@ -357,10 +358,10 @@ presence_change_room_nick(const char *const room, const char *const nick)
     log_debug("Sending room nickname change to: %s, nick: %s", room, nick);
     xmpp_ctx_t *ctx = connection_get_ctx();
     resource_presence_t presence_type =
-        accounts_get_last_presence(jabber_get_account_name());
+        accounts_get_last_presence(session_get_account_name());
     const char *show = stanza_get_presence_string_from_type(presence_type);
-    char *status = jabber_get_presence_message();
-    int pri = accounts_get_priority_for_presence_type(jabber_get_account_name(),
+    char *status = connection_get_presence_msg();
+    int pri = accounts_get_priority_for_presence_type(session_get_account_name(),
         presence_type);
 
     char *full_room_jid = create_fulljid(room, nick);
@@ -720,7 +721,7 @@ _muc_user_handler(xmpp_stanza_t *const stanza)
     }
 
     // handle self presence
-    if (stanza_is_muc_self_presence(stanza, jabber_get_fulljid())) {
+    if (stanza_is_muc_self_presence(stanza, connection_get_fulljid())) {
         log_debug("Room self presence received from %s", from_jid->fulljid);
 
         // self unavailable
diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c
index 0cb37014..95d4223e 100644
--- a/src/xmpp/roster.c
+++ b/src/xmpp/roster.c
@@ -55,8 +55,9 @@
 #include "event/client_events.h"
 #include "tools/autocomplete.h"
 #include "config/preferences.h"
-#include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/iq.h"
+#include "xmpp/connection.h"
 #include "xmpp/roster.h"
 #include "roster_list.h"
 #include "xmpp/stanza.h"
@@ -217,7 +218,7 @@ roster_set_handler(xmpp_stanza_t *const stanza)
     }
 
     // if from attribute exists and it is not current users barejid, ignore push
-    Jid *my_jid = jid_create(jabber_get_fulljid());
+    Jid *my_jid = jid_create(connection_get_fulljid());
     const char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
     if (from && (strcmp(from, my_jid->barejid) != 0)) {
         jid_destroy(my_jid);
diff --git a/src/xmpp/session.c b/src/xmpp/session.c
new file mode 100644
index 00000000..dd26c5e3
--- /dev/null
+++ b/src/xmpp/session.c
@@ -0,0 +1,397 @@
+/*
+ * session.c
+ *
+ * Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#include "config.h"
+
+#include <assert.h>
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef HAVE_LIBMESODE
+#include <mesode.h>
+#endif
+#ifdef HAVE_LIBSTROPHE
+#include <strophe.h>
+#endif
+
+#include "chat_session.h"
+#include "common.h"
+#include "config/preferences.h"
+#include "jid.h"
+#include "log.h"
+#include "muc.h"
+#include "plugins/plugins.h"
+#include "profanity.h"
+#include "event/server_events.h"
+#include "xmpp/bookmark.h"
+#include "xmpp/blocking.h"
+#include "xmpp/connection.h"
+#include "xmpp/capabilities.h"
+#include "xmpp/session.h"
+#include "xmpp/iq.h"
+#include "xmpp/message.h"
+#include "xmpp/presence.h"
+#include "xmpp/roster.h"
+#include "xmpp/stanza.h"
+#include "xmpp/xmpp.h"
+
+// for auto reconnect
+static struct {
+    char *name;
+    char *passwd;
+} saved_account;
+
+static struct {
+    char *name;
+    char *jid;
+    char *passwd;
+    char *altdomain;
+    int port;
+    char *tls_policy;
+} saved_details;
+
+static GTimer *reconnect_timer;
+
+static void _session_reconnect(void);
+
+static void _session_free_saved_account(void);
+static void _session_free_saved_details(void);
+static void _session_free_session_data(void);
+
+void
+session_init(void)
+{
+    log_info("Initialising XMPP");
+    connection_init();
+    presence_sub_requests_init();
+    caps_init();
+}
+
+jabber_conn_status_t
+session_connect_with_account(const ProfAccount *const account)
+{
+    assert(account != NULL);
+
+    log_info("Connecting using account: %s", account->name);
+
+    // save account name and password for reconnect
+    if (saved_account.name) {
+        free(saved_account.name);
+    }
+    saved_account.name = strdup(account->name);
+    if (saved_account.passwd) {
+        free(saved_account.passwd);
+    }
+    saved_account.passwd = strdup(account->password);
+
+    // connect with fulljid
+    Jid *jidp = jid_create_from_bare_and_resource(account->jid, account->resource);
+    jabber_conn_status_t result =
+        connection_connect(jidp->fulljid, account->password, account->server, account->port, account->tls_policy);
+    jid_destroy(jidp);
+
+    return result;
+}
+
+jabber_conn_status_t
+session_connect_with_details(const char *const jid, const char *const passwd, const char *const altdomain,
+    const int port, const char *const tls_policy)
+{
+    assert(jid != NULL);
+    assert(passwd != NULL);
+
+    // save details for reconnect, remember name for account creating on success
+    saved_details.name = strdup(jid);
+    saved_details.passwd = strdup(passwd);
+    if (altdomain) {
+        saved_details.altdomain = strdup(altdomain);
+    } else {
+        saved_details.altdomain = NULL;
+    }
+    if (port != 0) {
+        saved_details.port = port;
+    } else {
+        saved_details.port = 0;
+    }
+    if (tls_policy) {
+        saved_details.tls_policy = strdup(tls_policy);
+    } else {
+        saved_details.tls_policy = NULL;
+    }
+
+    // use 'profanity' when no resourcepart in provided jid
+    Jid *jidp = jid_create(jid);
+    if (jidp->resourcepart == NULL) {
+        jid_destroy(jidp);
+        jidp = jid_create_from_bare_and_resource(jid, "profanity");
+        saved_details.jid = strdup(jidp->fulljid);
+    } else {
+        saved_details.jid = strdup(jid);
+    }
+    jid_destroy(jidp);
+
+    // connect with fulljid
+    log_info("Connecting without account, JID: %s", saved_details.jid);
+
+    return connection_connect(
+        saved_details.jid,
+        passwd,
+        saved_details.altdomain,
+        saved_details.port,
+        saved_details.tls_policy);
+}
+
+void
+session_autoping_fail(void)
+{
+    if (connection_get_status() == JABBER_CONNECTED) {
+        log_info("Closing connection");
+        char *account_name = session_get_account_name();
+        const char *fulljid = connection_get_fulljid();
+        plugins_on_disconnect(account_name, fulljid);
+        accounts_set_last_activity(session_get_account_name());
+        connection_set_status(JABBER_DISCONNECTING);
+        xmpp_disconnect(connection_get_conn());
+
+        while (connection_get_status() == JABBER_DISCONNECTING) {
+            session_process_events(10);
+        }
+
+        connection_free_conn();
+        connection_free_ctx();
+    }
+
+    connection_free_presence_msg();
+    connection_free_domain();
+
+    connection_set_status(JABBER_DISCONNECTED);
+
+    session_lost_connection();
+}
+
+void
+session_disconnect(void)
+{
+    // if connected, send end stream and wait for response
+    if (connection_get_status() == JABBER_CONNECTED) {
+        char *account_name = session_get_account_name();
+        const char *fulljid = connection_get_fulljid();
+        plugins_on_disconnect(account_name, fulljid);
+        log_info("Closing connection");
+        accounts_set_last_activity(session_get_account_name());
+        connection_set_status(JABBER_DISCONNECTING);
+        xmpp_disconnect(connection_get_conn());
+
+        while (connection_get_status() == JABBER_DISCONNECTING) {
+            session_process_events(10);
+        }
+        _session_free_saved_account();
+        _session_free_saved_details();
+        _session_free_session_data();
+
+        connection_free_conn();
+        connection_free_ctx();
+    }
+
+    connection_free_presence_msg();
+    connection_free_domain();
+
+    connection_set_status(JABBER_STARTED);
+}
+
+void
+session_shutdown(void)
+{
+    _session_free_saved_account();
+    _session_free_saved_details();
+    _session_free_session_data();
+    xmpp_shutdown();
+    connection_free_log();
+}
+
+void
+session_process_events(int millis)
+{
+    int reconnect_sec;
+
+    jabber_conn_status_t conn_status = connection_get_status();
+    switch (conn_status)
+    {
+        case JABBER_CONNECTED:
+        case JABBER_CONNECTING:
+        case JABBER_DISCONNECTING:
+            xmpp_run_once(connection_get_ctx(), millis);
+            break;
+        case JABBER_DISCONNECTED:
+            reconnect_sec = prefs_get_reconnect();
+            if ((reconnect_sec != 0) && reconnect_timer) {
+                int elapsed_sec = g_timer_elapsed(reconnect_timer, NULL);
+                if (elapsed_sec > reconnect_sec) {
+                    _session_reconnect();
+                }
+            }
+            break;
+        default:
+            break;
+    }
+}
+
+char*
+session_get_account_name(void)
+{
+    return saved_account.name;
+}
+
+void
+session_login_success(gboolean secured)
+{
+    // logged in with account
+    if (saved_account.name) {
+        log_debug("Connection handler: logged in with account name: %s", saved_account.name);
+        sv_ev_login_account_success(saved_account.name, secured);
+
+    // logged in without account, use details to create new account
+    } else {
+        log_debug("Connection handler: logged in with jid: %s", saved_details.name);
+        accounts_add(saved_details.name, saved_details.altdomain, saved_details.port, saved_details.tls_policy);
+        accounts_set_jid(saved_details.name, saved_details.jid);
+
+        sv_ev_login_account_success(saved_details.name, secured);
+        saved_account.name = strdup(saved_details.name);
+        saved_account.passwd = strdup(saved_details.passwd);
+
+        _session_free_saved_details();
+    }
+
+    chat_sessions_init();
+
+    message_handlers_init();
+    presence_handlers_init();
+    iq_handlers_init();
+
+    roster_request();
+    bookmark_request();
+    blocking_request();
+
+    // items discovery
+    char *domain = connection_get_domain();
+    iq_disco_info_request_onconnect(domain);
+    iq_disco_items_request_onconnect(domain);
+
+    if (prefs_get_boolean(PREF_CARBONS)){
+        iq_enable_carbons();
+    }
+
+    if ((prefs_get_reconnect() != 0) && reconnect_timer) {
+        g_timer_destroy(reconnect_timer);
+        reconnect_timer = NULL;
+    }
+}
+
+void
+session_login_failed(void)
+{
+    if (reconnect_timer == NULL) {
+        log_debug("Connection handler: No reconnect timer");
+        sv_ev_failed_login();
+        _session_free_saved_account();
+        _session_free_saved_details();
+        _session_free_session_data();
+    } else {
+        log_debug("Connection handler: Restarting reconnect timer");
+        if (prefs_get_reconnect() != 0) {
+            g_timer_start(reconnect_timer);
+        }
+        // free resources but leave saved_user untouched
+        _session_free_session_data();
+    }
+}
+
+void
+session_lost_connection(void)
+{
+    sv_ev_lost_connection();
+    if (prefs_get_reconnect() != 0) {
+        assert(reconnect_timer == NULL);
+        reconnect_timer = g_timer_new();
+    } else {
+        _session_free_saved_account();
+        _session_free_saved_details();
+    }
+    _session_free_session_data();
+}
+
+static void
+_session_reconnect(void)
+{
+    // reconnect with account.
+    ProfAccount *account = accounts_get_account(saved_account.name);
+
+    if (account == NULL) {
+        log_error("Unable to reconnect, account no longer exists: %s", saved_account.name);
+    } else {
+        char *fulljid = create_fulljid(account->jid, account->resource);
+        log_debug("Attempting reconnect with account %s", account->name);
+        connection_connect(fulljid, saved_account.passwd, account->server, account->port, account->tls_policy);
+        free(fulljid);
+        g_timer_start(reconnect_timer);
+    }
+}
+
+static void
+_session_free_saved_account(void)
+{
+    FREE_SET_NULL(saved_account.name);
+    FREE_SET_NULL(saved_account.passwd);
+}
+
+static void
+_session_free_saved_details(void)
+{
+    FREE_SET_NULL(saved_details.name);
+    FREE_SET_NULL(saved_details.jid);
+    FREE_SET_NULL(saved_details.passwd);
+    FREE_SET_NULL(saved_details.altdomain);
+    FREE_SET_NULL(saved_details.tls_policy);
+}
+
+static void
+_session_free_session_data(void)
+{
+    connection_disco_items_free();
+    connection_remove_all_available_resources();
+    chat_sessions_clear();
+    presence_clear_sub_requests();
+}
+
diff --git a/src/xmpp/session.h b/src/xmpp/session.h
new file mode 100644
index 00000000..62337386
--- /dev/null
+++ b/src/xmpp/session.h
@@ -0,0 +1,54 @@
+/*
+ * session.h
+ *
+ * Copyright (C) 2012 - 2016 James Booth <boothj5@gmail.com>
+ *
+ * This file is part of Profanity.
+ *
+ * Profanity is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Profanity is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * In addition, as a special exception, the copyright holders give permission to
+ * link the code of portions of this program with the OpenSSL library under
+ * certain conditions as described in each individual source file, and
+ * distribute linked combinations including the two.
+ *
+ * You must obey the GNU General Public License in all respects for all of the
+ * code used other than OpenSSL. If you modify file(s) with this exception, you
+ * may extend this exception to your version of the file(s), but you are not
+ * obligated to do so. If you do not wish to do so, delete this exception
+ * statement from your version. If you delete this exception statement from all
+ * source files in the program, then also delete it here.
+ *
+ */
+
+#ifndef XMPP_SESSION_H
+#define XMPP_SESSION_H
+
+#include "config.h"
+
+#ifdef HAVE_LIBMESODE
+#include <mesode.h>
+#endif
+#ifdef HAVE_LIBSTROPHE
+#include <strophe.h>
+#endif
+
+#include "resource.h"
+
+void session_login_success(gboolean secured);
+void session_login_failed(void);
+void session_lost_connection(void);
+void session_autoping_fail(void);
+
+#endif
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index 01ae3f2d..db6dd76f 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -53,9 +53,10 @@
 
 #include "common.h"
 #include "log.h"
-#include "xmpp/connection.h"
+#include "xmpp/session.h"
 #include "xmpp/stanza.h"
 #include "xmpp/capabilities.h"
+#include "xmpp/connection.h"
 #include "xmpp/form.h"
 
 #include "muc.h"
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index d8478642..167a4bbf 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -105,31 +105,27 @@ typedef struct disco_identity_t {
     char *category;
 } DiscoIdentity;
 
-typedef struct disco_info_t {
-    char *item;
-    GHashTable *features;
-} DiscoInfo;
-
-void jabber_init(void);
-jabber_conn_status_t jabber_connect_with_details(const char *const jid, const char *const passwd,
+void session_init(void);
+jabber_conn_status_t session_connect_with_details(const char *const jid, const char *const passwd,
     const char *const altdomain, const int port, const char *const tls_policy);
-jabber_conn_status_t jabber_connect_with_account(const ProfAccount *const account);
-void jabber_disconnect(void);
-void jabber_shutdown(void);
-void jabber_process_events(int millis);
-const char* jabber_get_fulljid(void);
-jabber_conn_status_t jabber_get_connection_status(void);
-char* jabber_get_presence_message(void);
-char* jabber_get_account_name(void);
-GList* jabber_get_available_resources(void);
-char* jabber_create_uuid(void);
-void jabber_free_uuid(char *uuid);
+jabber_conn_status_t session_connect_with_account(const ProfAccount *const account);
+void session_disconnect(void);
+void session_shutdown(void);
+void session_process_events(int millis);
+char* session_get_account_name(void);
+
+jabber_conn_status_t connection_get_status(void);
+char *connection_get_presence_msg(void);
+const char* connection_get_fulljid(void);
+char* connection_create_uuid(void);
+void connection_free_uuid(char *uuid);
 #ifdef HAVE_LIBMESODE
-TLSCertificate* jabber_get_tls_peer_cert(void);
+TLSCertificate* connection_get_tls_peer_cert(void);
 #endif
-gboolean jabber_conn_is_secured(void);
-gboolean jabber_send_stanza(const char *const stanza);
-gboolean jabber_service_supports(const char *const feature);
+gboolean connection_is_secured(void);
+gboolean connection_send_stanza(const char *const stanza);
+GList* connection_get_available_resources(void);
+gboolean connection_supports(const char *const feature);
 
 char* message_send_chat(const char *const barejid, const char *const msg, const char *const oob_url);
 char* message_send_chat_otr(const char *const barejid, const char *const msg);