From a2ed9e6ffab56e7d1f6c39b0c0ce0f2501325ee4 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Sat, 9 Dec 2023 15:08:47 +0100 Subject: [PATCH] Improve const correctness Signed-off-by: Steffen Jaeckel --- src/command/cmd_funcs.c | 17 +++++------------ src/config/accounts.c | 2 +- src/event/client_events.c | 3 +-- src/xmpp/connection.c | 18 +++++------------- src/xmpp/connection.h | 2 +- src/xmpp/iq.c | 16 ++++++++-------- src/xmpp/iq.h | 4 ++-- src/xmpp/presence.c | 6 +++--- src/xmpp/session.c | 2 +- src/xmpp/xmpp.h | 14 +++++++------- 10 files changed, 34 insertions(+), 50 deletions(-) diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index ac06c92d..cd169169 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -5037,17 +5037,11 @@ cmd_lastactivity(ProfWin* window, const char* const command, gchar** args) if ((g_strcmp0(args[0], "get") == 0)) { if (args[1] == NULL) { - GString* jid = g_string_new(connection_get_jid()->domainpart); - - iq_last_activity_request(jid->str); - - g_string_free(jid, TRUE); - - return TRUE; + iq_last_activity_request(connection_get_jid()->domainpart); } else { iq_last_activity_request(args[1]); - return TRUE; } + return TRUE; } cons_bad_cmd_usage(command); @@ -8330,7 +8324,7 @@ cmd_command_list(ProfWin* window, const char* const command, gchar** args) return TRUE; } - char* jid = args[1]; + const char* jid = args[1]; if (jid == NULL) { switch (window->type) { case WIN_MUC: @@ -8391,7 +8385,7 @@ cmd_command_exec(ProfWin* window, const char* const command, gchar** args) return TRUE; } - char* jid = args[2]; + const char* jid = args[2]; if (jid == NULL) { switch (window->type) { case WIN_MUC: @@ -9668,12 +9662,11 @@ cmd_change_password(ProfWin* window, const char* const command, gchar** args) return TRUE; } - auto_char char* user = connection_get_user(); auto_char char* passwd = ui_ask_password(false); auto_char char* confirm_passwd = ui_ask_password(true); if (g_strcmp0(passwd, confirm_passwd) == 0) { - iq_register_change_password(user, passwd); + iq_register_change_password(connection_get_user(), passwd); } else { cons_show("Aborted! The new password and the confirmed password do not match."); } diff --git a/src/config/accounts.c b/src/config/accounts.c index 16edc219..5f1e5d37 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -223,7 +223,7 @@ accounts_get_account(const char* const name) } else { jabber_conn_status_t conn_status = connection_get_status(); if (conn_status == JABBER_CONNECTED) { - char* conf_jid = connection_jid_for_feature(XMPP_FEATURE_MUC); + const char* conf_jid = connection_jid_for_feature(XMPP_FEATURE_MUC); if (conf_jid) { muc_service = strdup(conf_jid); } diff --git a/src/event/client_events.c b/src/event/client_events.c index a37e7b5b..4bbcad32 100644 --- a/src/event/client_events.c +++ b/src/event/client_events.c @@ -122,8 +122,7 @@ cl_ev_presence_send(const resource_presence_t presence_type, const int idle_secs char* account_name = session_get_account_name(); ProfAccount* account = accounts_get_account(account_name); if (account->pgp_keyid) { - char* msg = connection_get_presence_msg(); - signed_status = p_gpg_sign(msg, account->pgp_keyid); + signed_status = p_gpg_sign(connection_get_presence_msg(), account->pgp_keyid); } account_free(account); #endif diff --git a/src/xmpp/connection.c b/src/xmpp/connection.c index 49e82296..a3eabc82 100644 --- a/src/xmpp/connection.c +++ b/src/xmpp/connection.c @@ -648,7 +648,7 @@ connection_supports(const char* const feature) return ret; } -char* +const char* connection_jid_for_feature(const char* const feature) { if (conn.features_by_jid == NULL) { @@ -765,18 +765,10 @@ equals_our_barejid(const char* cmp) return g_strcmp0(connection_get_jid()->barejid, cmp) == 0; } -char* +const char* connection_get_user(void) { - const char* jid = connection_get_fulljid(); - if (!jid) - return NULL; - char* result = strdup(jid); - - char* split = strchr(result, '@'); - *split = '\0'; - - return result; + return connection_get_jid()->localpart; } void @@ -845,13 +837,13 @@ connection_create_stanza_id(void) return g_strdup_printf("%s%s", rndid, hmac); } -char* +const char* connection_get_domain(void) { return conn.domain; } -char* +const char* connection_get_presence_msg(void) { return conn.presence_message; diff --git a/src/xmpp/connection.h b/src/xmpp/connection.h index d4ade03a..897a9fbc 100644 --- a/src/xmpp/connection.h +++ b/src/xmpp/connection.h @@ -56,7 +56,7 @@ void connection_set_disco_items(GSList* items); xmpp_conn_t* connection_get_conn(void); xmpp_ctx_t* connection_get_ctx(void); -char* connection_get_domain(void); +const char* connection_get_domain(void); void connection_request_features(void); void connection_features_received(const char* const jid); GHashTable* connection_get_features(const char* const jid); diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c index cd310cc3..f5aed560 100644 --- a/src/xmpp/iq.c +++ b/src/xmpp/iq.c @@ -417,7 +417,7 @@ iq_rooms_cache_clear(void) } void -iq_room_list_request(gchar* conferencejid, gchar* filter) +iq_room_list_request(const char* conferencejid, char* filter) { if (g_hash_table_contains(rooms_cache, conferencejid)) { log_debug("Rooms request cached for: %s", conferencejid); @@ -466,7 +466,7 @@ iq_disable_carbons(void) void iq_http_upload_request(HTTPUpload* upload) { - char* jid = connection_jid_for_feature(STANZA_NS_HTTP_UPLOAD); + const char* jid = connection_jid_for_feature(STANZA_NS_HTTP_UPLOAD); if (jid == NULL) { cons_show_error("XEP-0363 HTTP File Upload is not supported by the server"); return; @@ -484,7 +484,7 @@ iq_http_upload_request(HTTPUpload* upload) } void -iq_disco_info_request(gchar* jid) +iq_disco_info_request(const char* jid) { xmpp_ctx_t* const ctx = connection_get_ctx(); auto_char char* id = connection_create_stanza_id(); @@ -497,7 +497,7 @@ iq_disco_info_request(gchar* jid) } void -iq_disco_info_request_onconnect(gchar* jid) +iq_disco_info_request_onconnect(const char* jid) { xmpp_ctx_t* const ctx = connection_get_ctx(); auto_char char* id = connection_create_stanza_id(); @@ -510,7 +510,7 @@ iq_disco_info_request_onconnect(gchar* jid) } void -iq_last_activity_request(gchar* jid) +iq_last_activity_request(const char* jid) { xmpp_ctx_t* const ctx = connection_get_ctx(); auto_char char* id = connection_create_stanza_id(); @@ -618,7 +618,7 @@ iq_send_caps_request_legacy(const char* const to, const char* const id, } void -iq_disco_items_request(gchar* jid) +iq_disco_items_request(const char* jid) { xmpp_ctx_t* const ctx = connection_get_ctx(); xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, "discoitemsreq", jid, NULL); @@ -627,7 +627,7 @@ iq_disco_items_request(gchar* jid) } void -iq_disco_items_request_onconnect(gchar* jid) +iq_disco_items_request_onconnect(const char* jid) { xmpp_ctx_t* const ctx = connection_get_ctx(); xmpp_stanza_t* iq = stanza_create_disco_items_iq(ctx, "discoitemsreq_onconnect", jid, NULL); @@ -1078,7 +1078,7 @@ _caps_response_legacy_id_handler(xmpp_stanza_t* const stanza, void* const userda static int _room_list_id_handler(xmpp_stanza_t* const stanza, void* const userdata) { - gchar* filter = (gchar*)userdata; + const char* filter = userdata; const char* id = xmpp_stanza_get_id(stanza); const char* from = xmpp_stanza_get_from(stanza); diff --git a/src/xmpp/iq.h b/src/xmpp/iq.h index 887e832e..9c36035e 100644 --- a/src/xmpp/iq.h +++ b/src/xmpp/iq.h @@ -42,8 +42,8 @@ typedef void (*ProfIqFreeCallback)(void* userdata); void iq_handlers_init(void); void iq_send_stanza(xmpp_stanza_t* const stanza); void iq_id_handler_add(const char* const id, ProfIqCallback func, ProfIqFreeCallback free_func, void* userdata); -void iq_disco_info_request_onconnect(gchar* jid); -void iq_disco_items_request_onconnect(gchar* jid); +void iq_disco_info_request_onconnect(const char* jid); +void iq_disco_items_request_onconnect(const char* jid); void iq_send_caps_request(const char* const to, const char* const id, const char* const node, const char* const ver); void iq_send_caps_request_for_jid(const char* const to, const char* const id, const char* const node, const char* const ver); diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c index 2b0ae7f7..2a6b3157 100644 --- a/src/xmpp/presence.c +++ b/src/xmpp/presence.c @@ -192,7 +192,7 @@ presence_send(const resource_presence_t presence_type, const int idle, char* sig return; } - char* msg = connection_get_presence_msg(); + const char* msg = connection_get_presence_msg(); if (msg) { log_debug("Updating presence: %s, \"%s\"", string_from_resource_presence(presence_type), msg); } else { @@ -282,7 +282,7 @@ presence_join_room(const char* const room, const char* const nick, const char* c resource_presence_t presence_type = accounts_get_last_presence(session_get_account_name()); const char* show = stanza_get_presence_string_from_type(presence_type); - char* status = connection_get_presence_msg(); + const char* status = connection_get_presence_msg(); int pri = accounts_get_priority_for_presence_type(session_get_account_name(), presence_type); xmpp_ctx_t* ctx = connection_get_ctx(); @@ -306,7 +306,7 @@ presence_change_room_nick(const char* const room, const char* const nick) log_debug("Sending room nickname change to: %s, nick: %s", room, nick); resource_presence_t presence_type = accounts_get_last_presence(session_get_account_name()); const char* show = stanza_get_presence_string_from_type(presence_type); - char* status = connection_get_presence_msg(); + const char* status = connection_get_presence_msg(); int pri = accounts_get_priority_for_presence_type(session_get_account_name(), presence_type); auto_char char* full_room_jid = create_fulljid(room, nick); diff --git a/src/xmpp/session.c b/src/xmpp/session.c index 5e6ae09d..de6f626a 100644 --- a/src/xmpp/session.c +++ b/src/xmpp/session.c @@ -345,7 +345,7 @@ session_login_success(gboolean secured) // items discovery connection_request_features(); - char* domain = connection_get_domain(); + const char* domain = connection_get_domain(); iq_disco_items_request_onconnect(domain); if (prefs_get_boolean(PREF_CARBONS)) { diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h index 57079d1f..3d92ae68 100644 --- a/src/xmpp/xmpp.h +++ b/src/xmpp/xmpp.h @@ -190,13 +190,13 @@ void session_reconnect_now(void); void connection_disconnect(void); jabber_conn_status_t connection_get_status(void); -char* connection_get_presence_msg(void); +const char* connection_get_presence_msg(void); void connection_set_presence_msg(const char* const message); const char* connection_get_fulljid(void); const Jid* connection_get_jid(void); const char* connection_get_barejid(void); gboolean equals_our_barejid(const char* cmp); -char* connection_get_user(void); +const char* connection_get_user(void); char* connection_create_uuid(void); void connection_free_uuid(char* uuid); TLSCertificate* connection_get_tls_peer_cert(void); @@ -205,7 +205,7 @@ gboolean connection_send_stanza(const char* const stanza); GList* connection_get_available_resources(void); int connection_count_available_resources(void); gboolean connection_supports(const char* const feature); -char* connection_jid_for_feature(const char* const feature); +const char* connection_jid_for_feature(const char* const feature); const char* connection_get_profanity_identifier(void); @@ -244,10 +244,10 @@ void iq_send_software_version(const char* const fulljid); void iq_rooms_cache_clear(void); void iq_handlers_remove_win(ProfWin* window); void iq_handlers_clear(void); -void iq_room_list_request(gchar* conferencejid, gchar* filter); -void iq_disco_info_request(gchar* jid); -void iq_disco_items_request(gchar* jid); -void iq_last_activity_request(gchar* jid); +void iq_room_list_request(const char* conferencejid, char* filter); +void iq_disco_info_request(const char* jid); +void iq_disco_items_request(const char* jid); +void iq_last_activity_request(const char* jid); void iq_set_autoping(int seconds); void iq_confirm_instant_room(const char* const room_jid); void iq_destroy_room(const char* const room_jid);