From 3c1e8c4e154600b8b7aeb60bbf316e39a97ece27 Mon Sep 17 00:00:00 2001 From: James Booth Date: Mon, 9 Mar 2015 23:07:51 +0000 Subject: [PATCH] Added server error handling when setting /carbons preference --- src/command/commands.c | 2 +- src/server_events.c | 12 +++++ src/server_events.h | 2 + src/xmpp/iq.c | 103 ++++++++++++++++++++++++----------------- src/xmpp/stanza.c | 8 ++-- 5 files changed, 80 insertions(+), 47 deletions(-) diff --git a/src/command/commands.c b/src/command/commands.c index 2e466f61..8c93b065 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -3903,7 +3903,7 @@ cmd_carbons(gchar **args, struct cmd_help_t help) } gboolean result = _cmd_set_boolean_preference(args[0], help, - "Carbons message", PREF_CARBONS); + "Message carbons preference", PREF_CARBONS); // enable carbons if (strcmp(args[0], "on") == 0) { diff --git a/src/server_events.c b/src/server_events.c index f6755d6a..e5602b87 100644 --- a/src/server_events.c +++ b/src/server_events.c @@ -177,6 +177,18 @@ handle_disco_info_error(const char * const from, const char * const error) } } +void +handle_enable_carbons_error(const char * const error) +{ + cons_show_error("Server error enabling message carbons: %s", error); +} + +void +handle_disable_carbons_error(const char * const error) +{ + cons_show_error("Server error disabling message carbons: %s", error); +} + void handle_room_info_error(const char * const room, const char * const error) { diff --git a/src/server_events.h b/src/server_events.h index 9f874ffa..042eb934 100644 --- a/src/server_events.h +++ b/src/server_events.h @@ -122,5 +122,7 @@ void handle_muc_occupant_online(const char * const room, const char * const nick void handle_roster_update(const char * const barejid, const char * const name, GSList *groups, const char * const subscription, gboolean pending_out); void handle_roster_received(void); +void handle_enable_carbons_error(const char * const error); +void handle_disable_carbons_error(const char * const error); #endif diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index 62193c74..028c586e 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -58,48 +58,29 @@ #define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_IQ, type, ctx) -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 _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 _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); -static int _room_config_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_config_submit_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_affiliation_list_result_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_affiliation_set_result_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_role_set_result_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_role_list_result_handler(xmpp_conn_t * const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _room_kick_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); -static int _caps_response_handler(xmpp_conn_t *const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _caps_response_handler_for_jid(xmpp_conn_t *const conn, - xmpp_stanza_t * const stanza, void * const userdata); -static int _caps_response_handler_legacy(xmpp_conn_t *const conn, - xmpp_stanza_t * const stanza, void * const userdata); +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 _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 _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); +static int _room_config_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_config_submit_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_affiliation_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_affiliation_set_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_role_set_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_role_list_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _room_kick_result_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _enable_carbons_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _disable_carbons_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); +static int _caps_response_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _caps_response_handler_for_jid(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, void * const userdata); +static int _caps_response_handler_legacy(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, void * const userdata); void iq_add_handlers(void) @@ -158,6 +139,10 @@ iq_enable_carbons() xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_enable_carbons(ctx); + char *id = xmpp_stanza_get_id(iq); + + xmpp_id_handler_add(conn, _enable_carbons_handler, id, NULL); + xmpp_send(conn, iq); xmpp_stanza_release(iq); } @@ -168,6 +153,10 @@ iq_disable_carbons() xmpp_conn_t * const conn = connection_get_conn(); xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_stanza_t *iq = stanza_disable_carbons(ctx); + char *id = xmpp_stanza_get_id(iq); + + xmpp_id_handler_add(conn, _disable_carbons_handler, id, NULL); + xmpp_send(conn, iq); xmpp_stanza_release(iq); } @@ -734,6 +723,36 @@ _caps_response_handler_legacy(xmpp_conn_t *const conn, xmpp_stanza_t * const sta return 0; } +static int +_enable_carbons_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) +{ + char *type = xmpp_stanza_get_type(stanza); + if (g_strcmp0(type, "error") == 0) { + char *error_message = stanza_get_error_message(stanza); + handle_enable_carbons_error(error_message); + log_debug("Error enabling carbons: %s", error_message); + } else { + log_debug("Message carbons enabled."); + } + + return 0; +} + +static int +_disable_carbons_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) +{ + char *type = xmpp_stanza_get_type(stanza); + if (g_strcmp0(type, "error") == 0) { + char *error_message = stanza_get_error_message(stanza); + handle_disable_carbons_error(error_message); + log_debug("Error disabling carbons: %s", error_message); + } else { + log_debug("Message carbons disabled."); + } + + return 0; +} + static int _manual_pong_handler(xmpp_conn_t *const conn, xmpp_stanza_t * const stanza, void * const userdata) diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c index f1aa9cf4..9b1431a9 100644 --- a/src/xmpp/stanza.c +++ b/src/xmpp/stanza.c @@ -201,10 +201,10 @@ stanza_create_bookmarks_pubsub_add(xmpp_ctx_t *ctx, const char * const jid, xmpp_stanza_t * stanza_enable_carbons(xmpp_ctx_t *ctx){ xmpp_stanza_t *iq = xmpp_stanza_new(ctx); - char *id = create_unique_id(NULL); + char *id = create_unique_id("carbons"); xmpp_stanza_set_name(iq, STANZA_NAME_IQ); - xmpp_stanza_set_type(iq, STANZA_TYPE_SET); + xmpp_stanza_set_type(iq, STANZA_TYPE_SET); xmpp_stanza_set_id(iq, id); free(id); @@ -220,10 +220,10 @@ stanza_enable_carbons(xmpp_ctx_t *ctx){ xmpp_stanza_t * stanza_disable_carbons(xmpp_ctx_t *ctx){ xmpp_stanza_t *iq = xmpp_stanza_new(ctx); - char *id = create_unique_id(NULL); + char *id = create_unique_id("carbons"); xmpp_stanza_set_name(iq, STANZA_NAME_IQ); - xmpp_stanza_set_type(iq, STANZA_TYPE_SET); + xmpp_stanza_set_type(iq, STANZA_TYPE_SET); xmpp_stanza_set_id(iq, id); free(id);