diff --git a/src/command.c b/src/command.c index 740294a3..98b63fa4 100644 --- a/src/command.c +++ b/src/command.c @@ -465,7 +465,7 @@ static gboolean _cmd_connect(const char * const inp, struct cmd_help_t help) { gboolean result = FALSE; - jabber_conn_status_t conn_status = jabber_connection_status(); + jabber_conn_status_t conn_status = jabber_get_connection_status(); if ((conn_status != JABBER_DISCONNECTED) && (conn_status != JABBER_STARTED)) { cons_show("You are either connected already, or a login is in process."); @@ -558,7 +558,7 @@ _cmd_prefs(const char * const inp, struct cmd_help_t help) static gboolean _cmd_ros(const char * const inp, struct cmd_help_t help) { - jabber_conn_status_t conn_status = jabber_connection_status(); + jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) cons_show("You are not currently connected."); @@ -571,7 +571,7 @@ _cmd_ros(const char * const inp, struct cmd_help_t help) static gboolean _cmd_who(const char * const inp, struct cmd_help_t help) { - jabber_conn_status_t conn_status = jabber_connection_status(); + jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); @@ -589,7 +589,7 @@ _cmd_msg(const char * const inp, struct cmd_help_t help) char *usr = NULL; char *msg = NULL; - jabber_conn_status_t conn_status = jabber_connection_status(); + jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); @@ -786,7 +786,7 @@ _update_presence(const jabber_presence_t presence, msg = NULL; } - jabber_conn_status_t conn_status = jabber_connection_status(); + jabber_conn_status_t conn_status = jabber_get_connection_status(); if (conn_status != JABBER_CONNECTED) { cons_show("You are not currently connected."); diff --git a/src/jabber.c b/src/jabber.c index 70f8d45c..71f8dfab 100644 --- a/src/jabber.c +++ b/src/jabber.c @@ -27,7 +27,6 @@ #include "chat_log.h" #include "common.h" -#include "contact_list.h" #include "jabber.h" #include "log.h" #include "preferences.h" @@ -44,68 +43,23 @@ static struct _jabber_conn_t { int tls_disabled; } jabber_conn; -static log_level_t get_log_level(xmpp_log_level_t xmpp_level) -{ - 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; - } -} +static log_level_t _get_log_level(xmpp_log_level_t xmpp_level); +static xmpp_log_level_t _get_xmpp_log_level(); +static void _xmpp_file_logger(void * const userdata, + const xmpp_log_level_t level, const char * const area, + const char * const msg); +static xmpp_log_t * _xmpp_get_file_logger(); -static xmpp_log_level_t get_xmpp_log_level() -{ - log_level_t prof_level = log_get_filter(); - - if (prof_level == PROF_LEVEL_DEBUG) { - return XMPP_LEVEL_DEBUG; - } else if (prof_level == PROF_LEVEL_INFO) { - return XMPP_LEVEL_INFO; - } else if (prof_level == PROF_LEVEL_WARN) { - return XMPP_LEVEL_WARN; - } else { - return XMPP_LEVEL_ERROR; - } -} - -void -xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, - const char * const area, const char * const msg) -{ - log_level_t prof_level = get_log_level(level); - log_msg(prof_level, area, msg); -} - -xmpp_log_t * -xmpp_get_file_logger() -{ - 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; -} - -// private XMPP handlers -static void _jabber_conn_handler(xmpp_conn_t * const conn, +// XMPP event handlers +static void _connection_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata); - -static int _jabber_message_handler(xmpp_conn_t * const conn, +static int _message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); - static int _roster_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); - -static int _jabber_presence_handler(xmpp_conn_t * const conn, +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); void @@ -117,12 +71,6 @@ jabber_init(const int disable_tls) jabber_conn.tls_disabled = disable_tls; } -jabber_conn_status_t -jabber_connection_status(void) -{ - return (jabber_conn.conn_status); -} - jabber_conn_status_t jabber_connect(const char * const user, const char * const passwd) @@ -130,7 +78,7 @@ jabber_connect(const char * const user, log_info("Connecting as %s", user); xmpp_initialize(); - jabber_conn.log = xmpp_get_file_logger(); + jabber_conn.log = _xmpp_get_file_logger(); jabber_conn.ctx = xmpp_ctx_new(NULL, jabber_conn.log); jabber_conn.conn = xmpp_conn_new(jabber_conn.ctx); @@ -141,7 +89,7 @@ jabber_connect(const char * const user, xmpp_conn_disable_tls(jabber_conn.conn); int connect_status = xmpp_connect_client(jabber_conn.conn, NULL, 0, - _jabber_conn_handler, jabber_conn.ctx); + _connection_handler, jabber_conn.ctx); if (connect_status == 0) jabber_conn.conn_status = JABBER_CONNECTING; @@ -151,12 +99,6 @@ jabber_connect(const char * const user, return jabber_conn.conn_status; } -const char * -jabber_get_jid(void) -{ - return xmpp_conn_get_jid(jabber_conn.conn); -} - gboolean jabber_disconnect(void) { @@ -295,8 +237,20 @@ jabber_update_presence(jabber_presence_t status, const char * const msg) xmpp_stanza_release(pres); } +jabber_conn_status_t +jabber_get_connection_status(void) +{ + return (jabber_conn.conn_status); +} + +const char * +jabber_get_jid(void) +{ + return xmpp_conn_get_jid(jabber_conn.conn); +} + static int -_jabber_message_handler(xmpp_conn_t * const conn, +_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body"); @@ -339,7 +293,7 @@ _jabber_message_handler(xmpp_conn_t * const conn, } static void -_jabber_conn_handler(xmpp_conn_t * const conn, +_connection_handler(xmpp_conn_t * const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t * const stream_error, void * const userdata) { @@ -350,8 +304,8 @@ _jabber_conn_handler(xmpp_conn_t * const conn, prof_handle_login_success(jid); xmpp_stanza_t* pres; - xmpp_handler_add(conn, _jabber_message_handler, NULL, "message", NULL, ctx); - xmpp_handler_add(conn, _jabber_presence_handler, NULL, "presence", NULL, ctx); + xmpp_handler_add(conn, _message_handler, NULL, "message", NULL, ctx); + xmpp_handler_add(conn, _presence_handler, NULL, "presence", NULL, ctx); xmpp_id_handler_add(conn, _roster_handler, "roster", ctx); xmpp_timed_handler_add(conn, _ping_timed_handler, PING_INTERVAL, ctx); @@ -446,7 +400,6 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) ping = xmpp_stanza_new(ctx); xmpp_stanza_set_name(ping, "ping"); - // FIXME add ping namespace to libstrophe xmpp_stanza_set_ns(ping, "urn:xmpp:ping"); xmpp_stanza_add_child(iq, ping); @@ -459,7 +412,7 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) } static int -_jabber_presence_handler(xmpp_conn_t * const conn, +_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { const char *jid = xmpp_conn_get_jid(jabber_conn.conn); @@ -495,3 +448,54 @@ _jabber_presence_handler(xmpp_conn_t * const conn, return 1; } + +static log_level_t +_get_log_level(xmpp_log_level_t xmpp_level) +{ + 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; + } +} + +static xmpp_log_level_t +_get_xmpp_log_level() +{ + log_level_t prof_level = log_get_filter(); + + if (prof_level == PROF_LEVEL_DEBUG) { + return XMPP_LEVEL_DEBUG; + } else if (prof_level == PROF_LEVEL_INFO) { + return XMPP_LEVEL_INFO; + } else if (prof_level == PROF_LEVEL_WARN) { + return XMPP_LEVEL_WARN; + } else { + return XMPP_LEVEL_ERROR; + } +} + +static void +_xmpp_file_logger(void * const userdata, const xmpp_log_level_t level, + const char * const area, const char * const msg) +{ + log_level_t prof_level = _get_log_level(level); + log_msg(prof_level, area, msg); +} + +static xmpp_log_t * +_xmpp_get_file_logger() +{ + 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; +} + diff --git a/src/jabber.h b/src/jabber.h index 4a27c55a..7e04d0bb 100644 --- a/src/jabber.h +++ b/src/jabber.h @@ -41,15 +41,14 @@ typedef enum { } jabber_presence_t; void jabber_init(const int disable_tls); -jabber_conn_status_t jabber_connection_status(void); -jabber_presence_t jabber_presence_status(void); jabber_conn_status_t jabber_connect(const char * const user, const char * const passwd); gboolean jabber_disconnect(void); void jabber_roster_request(void); void jabber_process_events(void); void jabber_send(const char * const msg, const char * const recipient); -const char * jabber_get_jid(void); void jabber_update_presence(jabber_presence_t status, const char * const msg); +const char * jabber_get_jid(void); +jabber_conn_status_t jabber_get_connection_status(void); #endif diff --git a/src/profanity.c b/src/profanity.c index ccaad847..564afa0e 100644 --- a/src/profanity.c +++ b/src/profanity.c @@ -277,7 +277,7 @@ _shutdown_init(void) gboolean wait_response = jabber_disconnect(); if (wait_response) { - while (jabber_connection_status() == JABBER_DISCONNECTING) { + while (jabber_get_connection_status() == JABBER_DISCONNECTING) { jabber_process_events(); } }