about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/common.c20
-rw-r--r--src/common.h2
-rw-r--r--src/xmpp/jid.c16
3 files changed, 26 insertions, 12 deletions
diff --git a/src/common.c b/src/common.c
index d8ee9915..b4dfa9d4 100644
--- a/src/common.c
+++ b/src/common.c
@@ -489,3 +489,23 @@ get_file_paths_recursive(const char *path, GSList **contents)
         entry = g_dir_read_name(directory);
     }
 }
+
+char*
+get_random_string(int length)
+{
+    GRand *prng;
+    char *rand;
+    char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+
+    rand = calloc(length+1, sizeof(char));
+
+    prng = g_rand_new();
+
+    int i;
+    for (i = 0; i < length; i++) {
+        rand[i] = alphabet[g_rand_int_range(prng, 0, sizeof(alphabet))];
+    }
+    g_rand_free(prng);
+
+    return rand;
+}
diff --git a/src/common.h b/src/common.h
index 6d4ca39c..38ac909b 100644
--- a/src/common.h
+++ b/src/common.h
@@ -103,4 +103,6 @@ int is_regular_file(const char *path);
 int is_dir(const char *path);
 void get_file_paths_recursive(const char *directory, GSList **contents);
 
+char* get_random_string(int length);
+
 #endif
diff --git a/src/xmpp/jid.c b/src/xmpp/jid.c
index 49bf7b9c..25067050 100644
--- a/src/xmpp/jid.c
+++ b/src/xmpp/jid.c
@@ -198,18 +198,10 @@ jid_fulljid_or_barejid(Jid *jid)
 char*
 jid_random_resource(void)
 {
-    GRand *prng;
-    char rand[5];
-    char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+    char *rand = get_random_string(4);
 
-    prng = g_rand_new();
+    gchar *result = g_strdup_printf("profanity.%s", rand);
+    free(rand);
 
-    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);
+    return result;
 }