diff options
author | James Booth <boothj5@gmail.com> | 2014-09-07 21:50:59 +0100 |
---|---|---|
committer | James Booth <boothj5@gmail.com> | 2014-09-07 21:50:59 +0100 |
commit | 8f08c7a21ef506fdb97816d2be96a2a5e17e6527 (patch) | |
tree | 742f56aba776cc796d7c9898db1e2114d8307bf0 /src | |
parent | 790f9173afa274072dc852342b7eaeececa00b20 (diff) | |
download | profani-tty-8f08c7a21ef506fdb97816d2be96a2a5e17e6527.tar.gz |
Added error handling for /ping command responses
Diffstat (limited to 'src')
-rw-r--r-- | src/server_events.c | 10 | ||||
-rw-r--r-- | src/server_events.h | 1 | ||||
-rw-r--r-- | src/xmpp/iq.c | 40 |
3 files changed, 51 insertions, 0 deletions
diff --git a/src/server_events.c b/src/server_events.c index eda847d2..c708a8de 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -589,3 +589,13 @@ handle_ping_result(const char * const from, int millis) cons_show("Ping response from %s: %dms.", from, millis); } } + +void +handle_ping_error_result(const char * const from, const char * const error) +{ + if (error == NULL) { + cons_show_error("Error returned from pinging %s.", from); + } else { + cons_show_error("Error returned form pinging %s: %s.", from, error); + } +} diff --git a/src/server_events.h b/src/server_events.h index 54d04b54..aaad9923 100644 --- a/src/server_events.h +++ b/src/server_events.h @@ -95,5 +95,6 @@ void handle_presence_error(const char *from, const char * const type, const char *err_msg); void handle_xmpp_stanza(const char * const msg); void handle_ping_result(const char * const from, int millis); +void handle_ping_error_result(const char * const from, const char * const error); #endif diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 17269fe2..6bb2e8a5 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -261,6 +261,8 @@ static int _manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, void * const userdata) { + xmpp_ctx_t * const ctx = connection_get_ctx(); + GDateTime *sent = (GDateTime *)userdata; GDateTime *now = g_date_time_new_now_local(); @@ -271,6 +273,44 @@ _manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, g_date_time_unref(now); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + char *type = xmpp_stanza_get_type(stanza); + + // handle error responses + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + xmpp_stanza_t *error = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_ERROR); + + // no error stanza + if (error == NULL) { + handle_ping_error_result(from, NULL); + return 0; + } + + // no children of error stanza + xmpp_stanza_t *error_child = xmpp_stanza_get_children(error); + if (error_child == NULL) { + handle_ping_error_result(from, NULL); + return 0; + } + + // text child found + xmpp_stanza_t *error_text_stanza = xmpp_stanza_get_child_by_name(error, STANZA_NAME_TEXT); + if (error_text_stanza != NULL) { + char *error_text = xmpp_stanza_get_text(error_text_stanza); + + // text found + if (error_text != NULL) { + handle_ping_error_result(from, error_text); + xmpp_free(ctx, error_text); + return 0; + } + + // no text child found + } else { + char *error_child_name = xmpp_stanza_get_name(error_child); + handle_ping_error_result(from, error_child_name); + return 0; + } + } handle_ping_result(from, elapsed_millis); |