From 2a92169351f88ab3bfe1acf15628878c5e8ef520 Mon Sep 17 00:00:00 2001 From: James Booth Date: Wed, 5 Aug 2015 00:26:29 +0100 Subject: Use id handler for software version requests, handle errors --- src/xmpp/iq.c | 33 +++++++++++++++++++++++++++------ src/xmpp/stanza.c | 6 +++++- 2 files changed, 32 insertions(+), 7 deletions(-) (limited to 'src/xmpp') diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 18abad36..7ed44363 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -67,10 +67,10 @@ typedef struct p_room_info_data_t { static int _error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _ping_get_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _version_get_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _disco_info_get_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _disco_info_response_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); static int _room_info_response_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); -static int _version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); 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 _destroy_room_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); @@ -103,7 +103,6 @@ iq_add_handlers(void) HANDLE(XMPP_NS_DISCO_ITEMS, STANZA_TYPE_RESULT, _disco_items_result_handler); HANDLE(STANZA_NS_VERSION, STANZA_TYPE_GET, _version_get_handler); - HANDLE(STANZA_NS_VERSION, STANZA_TYPE_RESULT, _version_result_handler); HANDLE(STANZA_NS_PING, STANZA_TYPE_GET, _ping_get_handler); @@ -301,6 +300,10 @@ iq_send_software_version(const char * const fulljid) xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_create_software_version_iq(ctx, fulljid); + + char *id = xmpp_stanza_get_id(iq); + xmpp_id_handler_add(conn, _version_result_handler, id, NULL); + xmpp_send(conn, iq); xmpp_stanza_release(iq); } @@ -836,16 +839,34 @@ _version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, log_debug("IQ version result handler fired."); } + char *type = xmpp_stanza_get_type(stanza); + char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); + + if (g_strcmp0(type, STANZA_TYPE_RESULT) != 0) { + if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) { + char *error_message = stanza_get_error_message(stanza); + ui_handle_software_version_error(from, error_message); + free(error_message); + } else { + ui_handle_software_version_error(from, "unknown error"); + log_error("Software version result with unrecognised type attribute."); + } + + return 0; + } + const char *jid = xmpp_stanza_get_attribute(stanza, "from"); xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY); if (query == NULL) { - return 1; + log_error("Software version result recieved with no query element."); + return 0; } char *ns = xmpp_stanza_get_ns(query); if (g_strcmp0(ns, STANZA_NS_VERSION) != 0) { - return 1; + log_error("Software version result recieved without namespace."); + return 0; } char *name_str = NULL; @@ -880,7 +901,7 @@ _version_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, jid_destroy(jidp); - return 1; + return 0; } static int @@ -1567,4 +1588,4 @@ _disco_items_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stan g_slist_free_full(items, (GDestroyNotify)_item_destroy); return 1; -} \ No newline at end of file +} diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index be85c330..bb32932a 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -877,7 +877,11 @@ stanza_create_software_version_iq(xmpp_ctx_t *ctx, const char * const fulljid) 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, "sv"); + + char *id = create_unique_id("sv"); + xmpp_stanza_set_id(iq, id); + free(id); + xmpp_stanza_set_attribute(iq, "to", fulljid); xmpp_stanza_t *query = xmpp_stanza_new(ctx); -- cgit 1.4.1-2-gfad0