From 5472c6965afc6b1a8df2c620b0548655c8440ab1 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 26 Jan 2014 00:15:34 +0000 Subject: [PATCH 1/3] Moved xmpp ping to iq module --- src/command/commands.c | 2 +- src/xmpp/connection.c | 37 ------------------------------------- src/xmpp/iq.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/xmpp/xmpp.h | 2 +- 4 files changed, 43 insertions(+), 39 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index b282f525..b01dbfa6 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -2257,7 +2257,7 @@ cmd_autoping(gchar **args, struct cmd_help_t help) if (_strtoi(value, &intval, 0, INT_MAX) == 0) { prefs_set_autoping(intval); - jabber_set_autoping(intval); + iq_set_autoping(intval); if (intval == 0) { cons_show("Autoping disabled.", intval); } else { diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 8566a8d6..4833d56c 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -87,7 +87,6 @@ static void _jabber_reconnect(void); 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 _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata); void _connection_free_saved_account(void); void _connection_free_saved_details(void); @@ -224,20 +223,6 @@ _jabber_process_events(void) } -static void -_jabber_set_autoping(const int seconds) -{ - if (jabber_conn.conn_status == JABBER_CONNECTED) { - xmpp_timed_handler_delete(jabber_conn.conn, _ping_timed_handler); - - if (seconds != 0) { - int millis = seconds * 1000; - xmpp_timed_handler_add(jabber_conn.conn, _ping_timed_handler, millis, - jabber_conn.ctx); - } - } -} - static GList * _jabber_get_available_resources(void) { @@ -459,8 +444,6 @@ _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) { - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; - // login success if (status == XMPP_CONN_CONNECT) { log_debug("Connection handler: XMPP_CONN_CONNECT"); @@ -494,11 +477,6 @@ _connection_handler(xmpp_conn_t * const conn, presence_add_handlers(); iq_add_handlers(); - if (prefs_get_autoping() != 0) { - int millis = prefs_get_autoping() * 1000; - xmpp_timed_handler_add(conn, _ping_timed_handler, millis, ctx); - } - roster_request(); bookmark_request(); jabber_conn.conn_status = JABBER_CONNECTED; @@ -556,20 +534,6 @@ _connection_handler(xmpp_conn_t * const conn, } } -static int -_ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) -{ - if (jabber_conn.conn_status == JABBER_CONNECTED) { - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; - - xmpp_stanza_t *iq = stanza_create_ping_iq(ctx); - xmpp_send(conn, iq); - xmpp_stanza_release(iq); - } - - return 1; -} - static log_level_t _get_log_level(const xmpp_log_level_t xmpp_level) { @@ -629,7 +593,6 @@ jabber_init_module(void) jabber_disconnect = _jabber_disconnect; jabber_shutdown = _jabber_shutdown; jabber_process_events = _jabber_process_events; - jabber_set_autoping = _jabber_set_autoping; jabber_get_available_resources = _jabber_get_available_resources; jabber_get_connection_status = _jabber_get_connection_status; jabber_get_fulljid = _jabber_get_fulljid; diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index ff5e47ec..13650398 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -35,6 +35,7 @@ #include "log.h" #include "muc.h" #include "profanity.h" +#include "config/preferences.h" #include "server_events.h" #include "xmpp/capabilities.h" #include "xmpp/connection.h" @@ -60,6 +61,8 @@ static int _disco_items_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _disco_items_get_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 iq_add_handlers(void) @@ -79,6 +82,28 @@ iq_add_handlers(void) HANDLE(STANZA_NS_VERSION, STANZA_TYPE_RESULT, _version_result_handler); HANDLE(STANZA_NS_PING, STANZA_TYPE_GET, _ping_get_handler); + + if (prefs_get_autoping() != 0) { + int millis = prefs_get_autoping() * 1000; + xmpp_timed_handler_add(conn, _ping_timed_handler, millis, ctx); + } +} + +static void +_iq_set_autoping(const int seconds) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + + if (jabber_get_connection_status() == JABBER_CONNECTED) { + xmpp_timed_handler_delete(conn, _ping_timed_handler); + + if (seconds != 0) { + int millis = seconds * 1000; + xmpp_timed_handler_add(conn, _ping_timed_handler, millis, + ctx); + } + } } static void @@ -136,6 +161,21 @@ _error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } +static int +_ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) +{ + xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + + if (jabber_get_connection_status() == JABBER_CONNECTED) { + + xmpp_stanza_t *iq = stanza_create_ping_iq(ctx); + xmpp_send(conn, iq); + xmpp_stanza_release(iq); + } + + return 1; +} + static int _version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -581,4 +621,5 @@ iq_init_module(void) iq_disco_info_request = _iq_disco_info_request; iq_disco_items_request = _iq_disco_items_request; iq_send_software_version = _iq_send_software_version; + iq_set_autoping = _iq_set_autoping; } diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 5a331b04..dd91e398 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -94,7 +94,6 @@ const char * (*jabber_get_fulljid)(void); const char * (*jabber_get_domain)(void); jabber_conn_status_t (*jabber_get_connection_status)(void); char * (*jabber_get_presence_message)(void); -void (*jabber_set_autoping)(int seconds); char* (*jabber_get_account_name)(void); GList * (*jabber_get_available_resources)(void); @@ -127,6 +126,7 @@ void (*iq_send_software_version)(const char * const fulljid); void (*iq_room_list_request)(gchar *conferencejid); void (*iq_disco_info_request)(gchar *jid); void (*iq_disco_items_request)(gchar *jid); +void (*iq_set_autoping)(int seconds); // caps functions Capabilities* (*caps_get)(const char * const caps_str); From 3d7e9232543361e5202f648c5b0209ec30d103c3 Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 26 Jan 2014 00:18:15 +0000 Subject: [PATCH 2/3] Use generated id on ping iqs --- src/xmpp/stanza.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 97cebc73..635d12db 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -394,7 +394,8 @@ stanza_create_ping_iq(xmpp_ctx_t *ctx) xmpp_stanza_t *iq = xmpp_stanza_new(ctx); xmpp_stanza_set_name(iq, STANZA_NAME_IQ); xmpp_stanza_set_type(iq, STANZA_TYPE_GET); - xmpp_stanza_set_id(iq, "c2s1"); + char *id = generate_unique_id("ping"); + xmpp_stanza_set_id(iq, id); xmpp_stanza_t *ping = xmpp_stanza_new(ctx); xmpp_stanza_set_name(ping, STANZA_NAME_PING); From 16de26790678e55c10cc2ae79ac9a15234fe734e Mon Sep 17 00:00:00 2001 From: James Booth Date: Sun, 26 Jan 2014 00:53:24 +0000 Subject: [PATCH 3/3] Added id handler for pings, disable ping on error type 'cancel' --- src/server_events.c | 8 ++++++++ src/server_events.h | 1 + src/xmpp/iq.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/src/server_events.c b/src/server_events.c index 26612816..a87d22a2 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -461,3 +461,11 @@ handle_roster_add(const char * const barejid, const char * const name) ui_roster_add(barejid, name); ui_current_page_off(); } + +void +handle_autoping_cancel(void) +{ + prefs_set_autoping(0); + cons_show_error("Server ping not supported, autoping disabled."); + ui_current_page_off(); +} diff --git a/src/server_events.h b/src/server_events.h index 64bd04a7..7704cd44 100644 --- a/src/server_events.h +++ b/src/server_events.h @@ -74,5 +74,6 @@ void handle_group_remove(const char * const contact, const char * const group); void handle_roster_remove(const char * const barejid); void handle_roster_add(const char * const barejid, const char * const name); +void handle_autoping_cancel(void); #endif diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 13650398..3766d2c4 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -161,6 +161,37 @@ _error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, return 1; } +static int +_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + char *id = xmpp_stanza_get_id(stanza); + char *type = xmpp_stanza_get_type(stanza); + + if (id != NULL && type != NULL) { + // show warning if error + if (strcmp(type, STANZA_TYPE_ERROR) == 0) { + log_warning("Server ping (id=%s) responded with error", id); + + // turn off autoping if error type is 'cancel' + xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); + if (error != NULL) { + char *errtype = xmpp_stanza_get_type(error); + if (errtype != NULL) { + if (strcmp(errtype, "cancel") == 0) { + log_warning("Server ping (id=%s) error type 'cancel', disabling autoping.", id); + handle_autoping_cancel(); + xmpp_timed_handler_delete(conn, _ping_timed_handler); + } + } + } + } + } + + // remove this handler + return 0; +} + static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) { @@ -169,6 +200,11 @@ _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) if (jabber_get_connection_status() == JABBER_CONNECTED) { xmpp_stanza_t *iq = stanza_create_ping_iq(ctx); + char *id = xmpp_stanza_get_id(iq); + + // add pong handler + xmpp_id_handler_add(conn, _pong_handler, id, ctx); + xmpp_send(conn, iq); xmpp_stanza_release(iq); }