about summary refs log tree commit diff stats
path: root/src/xmpp
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2023-04-09 18:24:23 +0200
committerGitHub <noreply@github.com>2023-04-09 18:24:23 +0200
commite52ca2fbaa02acec3b2b757369ef5740e7a103da (patch)
treee80a507290c46f82b8a5394c30ec24e8e879f270 /src/xmpp
parent19921f61c14095cadc55b329dd7c8f02bdc79d31 (diff)
parent5d3c8ce7c164f74f606ff06d1adf849821591a51 (diff)
downloadprofani-tty-e52ca2fbaa02acec3b2b757369ef5740e7a103da.tar.gz
Merge pull request #1815 from H3rnand3zzz/feature/the-client-switcher
Feature: Allow setting client identification name/version manually
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/iq.c102
-rw-r--r--src/xmpp/stanza.c24
2 files changed, 73 insertions, 53 deletions
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index b7beafaf..e48d187a 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -1612,6 +1612,17 @@ _version_get_handler(xmpp_stanza_t* const stanza)
     xmpp_ctx_t* const ctx = connection_get_ctx();
     const char* id = xmpp_stanza_get_id(stanza);
     const char* from = xmpp_stanza_get_from(stanza);
+    ProfAccount* account = accounts_get_account(session_get_account_name());
+    auto_char char* client = account->client != NULL ? strdup(account->client) : NULL;
+    bool is_custom_client = client != NULL;
+    gchar* custom_version_str = NULL;
+    if (is_custom_client) {
+        custom_version_str = strstr(client, " ");
+        if (custom_version_str != NULL) {
+            *custom_version_str = '\0'; // Split string on name and version
+            custom_version_str++;
+        }
+    }
 
     if (id) {
         log_debug("IQ version get handler fired, id: %s.", id);
@@ -1619,28 +1630,35 @@ _version_get_handler(xmpp_stanza_t* const stanza)
         log_debug("IQ version get handler fired.");
     }
 
-    if (from) {
-        if (prefs_get_boolean(PREF_ADV_NOTIFY_DISCO_OR_VERSION)) {
-            cons_show("Received IQ version request (XEP-0092) from %s", from);
-        }
+    if (!from)
+        return;
 
-        xmpp_stanza_t* response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, id);
-        xmpp_stanza_set_to(response, from);
+    if (prefs_get_boolean(PREF_ADV_NOTIFY_DISCO_OR_VERSION)) {
+        cons_show("Received IQ version request (XEP-0092) from %s", from);
+    }
 
-        xmpp_stanza_t* query = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
-        xmpp_stanza_set_ns(query, STANZA_NS_VERSION);
-
-        xmpp_stanza_t* name = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_name(name, "name");
-        xmpp_stanza_t* name_txt = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_text(name_txt, "Profanity");
-        xmpp_stanza_add_child(name, name_txt);
-
-        xmpp_stanza_t* version = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_name(version, "version");
-        xmpp_stanza_t* version_txt = xmpp_stanza_new(ctx);
-        GString* version_str = g_string_new(PACKAGE_VERSION);
+    xmpp_stanza_t* response = xmpp_iq_new(ctx, STANZA_TYPE_RESULT, id);
+    xmpp_stanza_set_to(response, from);
+
+    xmpp_stanza_t* query = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
+    xmpp_stanza_set_ns(query, STANZA_NS_VERSION);
+
+    xmpp_stanza_t* name = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(name, "name");
+    xmpp_stanza_t* name_txt = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_text(name_txt, is_custom_client ? client : "Profanity");
+    xmpp_stanza_add_child(name, name_txt);
+    xmpp_stanza_add_child(query, name);
+    bool include_os = prefs_get_boolean(PREF_REVEAL_OS) && !is_custom_client;
+    xmpp_stanza_t* os;
+    xmpp_stanza_t* os_txt;
+    xmpp_stanza_t* version = xmpp_stanza_new(ctx);
+    xmpp_stanza_set_name(version, "version");
+    xmpp_stanza_t* version_txt = xmpp_stanza_new(ctx);
+    GString* version_str = g_string_new(PACKAGE_VERSION);
+
+    if (!is_custom_client) {
         if (strcmp(PACKAGE_STATUS, "development") == 0) {
 #ifdef HAVE_GIT_VERSION
             g_string_append(version_str, "dev.");
@@ -1653,11 +1671,8 @@ _version_get_handler(xmpp_stanza_t* const stanza)
         }
         xmpp_stanza_set_text(version_txt, version_str->str);
         xmpp_stanza_add_child(version, version_txt);
+        xmpp_stanza_add_child(query, version);
 
-        xmpp_stanza_t* os;
-        xmpp_stanza_t* os_txt;
-
-        bool include_os = prefs_get_boolean(PREF_REVEAL_OS);
         if (include_os) {
             os = xmpp_stanza_new(ctx);
             xmpp_stanza_set_name(os, "os");
@@ -1680,31 +1695,30 @@ _version_get_handler(xmpp_stanza_t* const stanza)
             xmpp_stanza_set_text(os_txt, "Unknown");
 #endif
             xmpp_stanza_add_child(os, os_txt);
-        }
-
-        xmpp_stanza_add_child(query, name);
-        xmpp_stanza_add_child(query, version);
-        if (include_os) {
             xmpp_stanza_add_child(query, os);
         }
-        xmpp_stanza_add_child(response, query);
+    }
+    if (is_custom_client && custom_version_str != NULL) {
+        xmpp_stanza_set_text(version_txt, custom_version_str);
+        xmpp_stanza_add_child(version, version_txt);
+        xmpp_stanza_add_child(query, version);
+    }
 
-        iq_send_stanza(response);
+    xmpp_stanza_add_child(response, query);
+    iq_send_stanza(response);
 
-        g_string_free(version_str, TRUE);
-        xmpp_stanza_release(name_txt);
-        xmpp_stanza_release(version_txt);
-        if (include_os) {
-            xmpp_stanza_release(os_txt);
-        }
-        xmpp_stanza_release(name);
-        xmpp_stanza_release(version);
-        if (include_os) {
-            xmpp_stanza_release(os);
-        }
-        xmpp_stanza_release(query);
-        xmpp_stanza_release(response);
+    // Cleanup
+    g_string_free(version_str, TRUE);
+    xmpp_stanza_release(version_txt);
+    xmpp_stanza_release(name_txt);
+    if (include_os) {
+        xmpp_stanza_release(os_txt);
+        xmpp_stanza_release(os);
     }
+    xmpp_stanza_release(name);
+    xmpp_stanza_release(version);
+    xmpp_stanza_release(query);
+    xmpp_stanza_release(response);
 }
 
 static void
diff --git a/src/xmpp/stanza.c b/src/xmpp/stanza.c
index fab6c5e4..bf353113 100644
--- a/src/xmpp/stanza.c
+++ b/src/xmpp/stanza.c
@@ -975,19 +975,25 @@ stanza_create_caps_query_element(xmpp_ctx_t* ctx)
     xmpp_stanza_t* identity = xmpp_stanza_new(ctx);
     xmpp_stanza_set_name(identity, "identity");
     xmpp_stanza_set_attribute(identity, "category", "client");
-    xmpp_stanza_set_type(identity, "console");
 
-    GString* name_str = g_string_new("Profanity ");
-    g_string_append(name_str, PACKAGE_VERSION);
-    if (g_strcmp0(PACKAGE_STATUS, "development") == 0) {
+    ProfAccount* account = accounts_get_account(session_get_account_name());
+    gchar* client = account->client;
+    bool is_custom_client = client != NULL;
+
+    GString* name_str = g_string_new(is_custom_client ? client : "Profanity ");
+    if (!is_custom_client) {
+        xmpp_stanza_set_type(identity, "console");
+        g_string_append(name_str, PACKAGE_VERSION);
+        if (g_strcmp0(PACKAGE_STATUS, "development") == 0) {
 #ifdef HAVE_GIT_VERSION
-        g_string_append(name_str, "dev.");
-        g_string_append(name_str, PROF_GIT_BRANCH);
-        g_string_append(name_str, ".");
-        g_string_append(name_str, PROF_GIT_REVISION);
+            g_string_append(name_str, "dev.");
+            g_string_append(name_str, PROF_GIT_BRANCH);
+            g_string_append(name_str, ".");
+            g_string_append(name_str, PROF_GIT_REVISION);
 #else
-        g_string_append(name_str, "dev");
+            g_string_append(name_str, "dev");
 #endif
+        }
     }
     xmpp_stanza_set_attribute(identity, "name", name_str->str);
     g_string_free(name_str, TRUE);