From 4ba33cb13c172a7280f8b5443e10ce0e113a2c47 Mon Sep 17 00:00:00 2001 From: James Booth Date: Thu, 4 Sep 2014 01:08:10 +0100 Subject: Added /ping command --- src/xmpp/iq.c | 40 +++++++++++++++++++++++++++++++++++++++- src/xmpp/stanza.c | 5 ++++- src/xmpp/stanza.h | 2 +- src/xmpp/xmpp.h | 1 + 4 files changed, 45 insertions(+), 3 deletions(-) (limited to 'src/xmpp') diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index b4fc8176..17269fe2 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -75,6 +75,8 @@ static int _disco_items_get_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _destroy_room_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _manual_pong_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); @@ -186,6 +188,21 @@ _iq_destroy_instant_room(const char * const room_jid) xmpp_stanza_release(iq); } +static void +_iq_send_ping(const char * const target) +{ + xmpp_conn_t * const conn = connection_get_conn(); + xmpp_ctx_t * const ctx = connection_get_ctx(); + xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, target); + char *id = xmpp_stanza_get_id(iq); + + GDateTime *now = g_date_time_new_now_local(); + xmpp_id_handler_add(conn, _manual_pong_handler, id, now); + + xmpp_send(conn, iq); + xmpp_stanza_release(iq); +} + static int _error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) @@ -240,6 +257,26 @@ _pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, return 0; } +static int +_manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, + void * const userdata) +{ + GDateTime *sent = (GDateTime *)userdata; + GDateTime *now = g_date_time_new_now_local(); + + GTimeSpan elapsed = g_date_time_difference(now, sent); + int elapsed_millis = elapsed / 1000; + + g_date_time_unref(sent); + g_date_time_unref(now); + + char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + + handle_ping_result(from, elapsed_millis); + + return 0; +} + static int _ping_timed_handler(xmpp_conn_t * const conn, void * const userdata) { @@ -247,7 +284,7 @@ _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); + xmpp_stanza_t *iq = stanza_create_ping_iq(ctx, NULL); char *id = xmpp_stanza_get_id(iq); // add pong handler @@ -762,4 +799,5 @@ iq_init_module(void) iq_set_autoping = _iq_set_autoping; iq_confirm_instant_room = _iq_confirm_instant_room; iq_destroy_instant_room = _iq_destroy_instant_room; + iq_send_ping = _iq_send_ping; } diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index 51e63ccc..09de1211 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -579,11 +579,14 @@ stanza_contains_chat_state(xmpp_stanza_t *stanza) } xmpp_stanza_t * -stanza_create_ping_iq(xmpp_ctx_t *ctx) +stanza_create_ping_iq(xmpp_ctx_t *ctx, const char * const target) { xmpp_stanza_t *iq = xmpp_stanza_new(ctx); xmpp_stanza_set_name(iq, STANZA_NAME_IQ); xmpp_stanza_set_type(iq, STANZA_TYPE_GET); + if (target != NULL) { + xmpp_stanza_set_attribute(iq, STANZA_ATTR_TO, target); + } char *id = create_unique_id("ping"); xmpp_stanza_set_id(iq, id); free(id); diff --git a/src/xmpp/stanza.h b/src/xmpp/stanza.h index eb309e19..3d925787 100644 --- a/src/xmpp/stanza.h +++ b/src/xmpp/stanza.h @@ -185,7 +185,7 @@ xmpp_stanza_t* stanza_create_room_leave_presence(xmpp_ctx_t *ctx, xmpp_stanza_t* stanza_create_presence(xmpp_ctx_t * const ctx); xmpp_stanza_t* stanza_create_roster_iq(xmpp_ctx_t *ctx); -xmpp_stanza_t* stanza_create_ping_iq(xmpp_ctx_t *ctx); +xmpp_stanza_t* stanza_create_ping_iq(xmpp_ctx_t *ctx, const char * const target); xmpp_stanza_t* stanza_create_disco_info_iq(xmpp_ctx_t *ctx, const char * const id, const char * const to, const char * const node); diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index fb15e7af..78703230 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -141,6 +141,7 @@ void (*iq_disco_items_request)(gchar *jid); void (*iq_set_autoping)(int seconds); void (*iq_confirm_instant_room)(const char * const room_jid); void (*iq_destroy_instant_room)(const char * const room_jid); +void (*iq_send_ping)(const char * const target); // caps functions Capabilities* (*caps_get)(const char * const caps_str); -- cgit 1.4.1-2-gfad0