about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2019-04-12 15:58:12 +0200
committerGitHub <noreply@github.com>2019-04-12 15:58:12 +0200
commit17a17a84a9a5943b87b6e38ec2c76222eca93a0b (patch)
tree9a9b1c2951e91e1cb13a2c85b1b94d6efb2c466b
parent0dfe61c01cddf48a0a614927bb29a52625464525 (diff)
parent9714d1d867bac6fdbc3d124f923a36a1dc2ea504 (diff)
downloadprofani-tty-17a17a84a9a5943b87b6e38ec2c76222eca93a0b.tar.gz
Merge pull request #1053 from paulfariello/feature/random_resource
Add random string at the end of the default resource
-rw-r--r--src/config/accounts.c6
-rw-r--r--src/xmpp/jid.c19
-rw-r--r--src/xmpp/jid.h1
-rw-r--r--src/xmpp/session.c4
4 files changed, 27 insertions, 3 deletions
diff --git a/src/config/accounts.c b/src/config/accounts.c
index f53f53c9..1c6441db 100644
--- a/src/config/accounts.c
+++ b/src/config/accounts.c
@@ -124,16 +124,17 @@ accounts_add(const char *account_name, const char *altdomain, const int port, co
 {
     // set account name and resource
     const char *barejid = account_name;
-    const char *resource = "profanity";
+    char *resource = jid_random_resource();
     Jid *jid = jid_create(account_name);
     if (jid) {
         barejid = jid->barejid;
         if (jid->resourcepart) {
-            resource = jid->resourcepart;
+            resource = g_strdup(jid->resourcepart);
         }
     }
 
     if (g_key_file_has_group(accounts, account_name)) {
+        g_free(resource);
         jid_destroy(jid);
         return;
     }
@@ -174,6 +175,7 @@ accounts_add(const char *account_name, const char *altdomain, const int port, co
     autocomplete_add(enabled_ac, account_name);
 
     jid_destroy(jid);
+    g_free(resource);
 }
 
 int
diff --git a/src/xmpp/jid.c b/src/xmpp/jid.c
index 51e1fa3c..49bf7b9c 100644
--- a/src/xmpp/jid.c
+++ b/src/xmpp/jid.c
@@ -194,3 +194,22 @@ jid_fulljid_or_barejid(Jid *jid)
         return jid->barejid;
     }
 }
+
+char*
+jid_random_resource(void)
+{
+    GRand *prng;
+    char rand[5];
+    char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+    prng = g_rand_new();
+
+    int i;
+    for (i = 0; i < 4; i++) {
+        rand[i] = alphabet[g_rand_int_range(prng, 0, sizeof(alphabet))];
+    }
+    rand[4] = '\0';
+    g_rand_free(prng);
+
+    return g_strdup_printf("profanity.%s", rand);
+}
diff --git a/src/xmpp/jid.h b/src/xmpp/jid.h
index fc0e388f..4af0e381 100644
--- a/src/xmpp/jid.h
+++ b/src/xmpp/jid.h
@@ -57,5 +57,6 @@ char* create_fulljid(const char *const barejid, const char *const resource);
 char* get_nick_from_full_jid(const char *const full_room_jid);
 
 char* jid_fulljid_or_barejid(Jid *jid);
+char* jid_random_resource(void);
 
 #endif
diff --git a/src/xmpp/session.c b/src/xmpp/session.c
index 675f23af..67cbb17f 100644
--- a/src/xmpp/session.c
+++ b/src/xmpp/session.c
@@ -173,7 +173,9 @@ session_connect_with_details(const char *const jid, const char *const passwd, co
     Jid *jidp = jid_create(jid);
     if (jidp->resourcepart == NULL) {
         jid_destroy(jidp);
-        jidp = jid_create_from_bare_and_resource(jid, "profanity");
+        char *resource = jid_random_resource();
+        jidp = jid_create_from_bare_and_resource(jid, resource);
+        free(resource);
         saved_details.jid = strdup(jidp->fulljid);
     } else {
         saved_details.jid = strdup(jid);