about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-07-01 10:05:45 +0200
committerMichael Vetter <jubalh@iodoru.org>2020-07-01 10:05:45 +0200
commit1224aa414e56a763412ee33c2b8779d4e6cb1608 (patch)
tree45c16465961c5365379f4f99971be1fafe031b1f
parent7d6ef8f4c8fd4d2a6560095b16e9ba147d3c4c47 (diff)
downloadprofani-tty-1224aa414e56a763412ee33c2b8779d4e6cb1608.tar.gz
Use files_get_account_data_path instead of duplicate code
We often had a use case where we want the account specific data dir.
Let's create a function for this instead of doing it by hand each time.
-rw-r--r--src/config/files.c20
-rw-r--r--src/config/files.h3
-rw-r--r--src/database.c26
-rw-r--r--src/omemo/omemo.c35
-rw-r--r--src/otr/otr.c86
-rw-r--r--src/pgp/gpg.c22
6 files changed, 87 insertions, 105 deletions
diff --git a/src/config/files.c b/src/config/files.c
index da2b66bb..80a777ae 100644
--- a/src/config/files.c
+++ b/src/config/files.c
@@ -154,7 +154,7 @@ files_get_config_path(char *config_base)
 }
 
 gchar*
-files_get_data_path(char *data_base)
+files_get_data_path(const char *const data_base)
 {
     gchar *xdg_data = _files_get_xdg_data_home();
     GString *file_str = g_string_new(xdg_data);
@@ -167,6 +167,24 @@ files_get_data_path(char *data_base)
     return result;
 }
 
+gchar*
+files_get_account_data_path(const char *const specific_dir, const char *const jid)
+{
+    gchar *data_dir = files_get_data_path(specific_dir);
+    GString *result_dir = g_string_new(data_dir);
+    g_free(data_dir);
+
+    gchar *account_dir = str_replace(jid, "@", "_at_");
+    g_string_append(result_dir, "/");
+    g_string_append(result_dir, account_dir);
+    g_free(account_dir);
+
+    gchar *result = g_strdup(result_dir->str);
+    g_string_free(result_dir, TRUE);
+
+    return result;
+}
+
 static char*
 _files_get_xdg_config_home(void)
 {
diff --git a/src/config/files.h b/src/config/files.h
index c87c4501..7b6b37a5 100644
--- a/src/config/files.h
+++ b/src/config/files.h
@@ -61,7 +61,8 @@
 void files_create_directories(void);
 
 gchar* files_get_config_path(char *config_base);
-gchar* files_get_data_path(char *data_base);
+gchar* files_get_data_path(const char *const data_base);
+gchar* files_get_account_data_path(const char *const specific_dir, const char *const jid);
 
 gchar* files_get_log_file(char *log_file);
 gchar* files_get_inputrc_file(void);
diff --git a/src/database.c b/src/database.c
index 271c64b6..3337e8df 100644
--- a/src/database.c
+++ b/src/database.c
@@ -56,31 +56,25 @@ static prof_msg_type_t _get_message_type_type(const char *const type);
 static char*
 _get_db_filename(ProfAccount *account)
 {
-    char *databasedir = files_get_data_path(DIR_DATABASE);
-    GString *basedir = g_string_new(databasedir);
-    free(databasedir);
+    gchar *database_dir = files_get_account_data_path(DIR_DATABASE, account->jid);
 
-    g_string_append(basedir, "/");
-
-    gchar *account_dir = str_replace(account->jid, "@", "_at_");
-    g_string_append(basedir, account_dir);
-    free(account_dir);
-
-    int res = g_mkdir_with_parents(basedir->str, S_IRWXU);
+    int res = g_mkdir_with_parents(database_dir, S_IRWXU);
     if (res == -1) {
         char *errmsg = strerror(errno);
         if (errmsg) {
-            log_error("DATABASE: error creating directory: %s, %s", basedir->str, errmsg);
+            log_error("DATABASE: error creating directory: %s, %s", database_dir, errmsg);
         } else {
-            log_error("DATABASE: creating directory: %s", basedir->str);
+            log_error("DATABASE: creating directory: %s", database_dir);
         }
-        g_string_free(basedir, TRUE);
+        g_free(database_dir);
         return NULL;
     }
 
-    g_string_append(basedir, "/chatlog.db");
-    char *result = strdup(basedir->str);
-    g_string_free(basedir, TRUE);
+    GString *chatlog_filename = g_string_new(database_dir);
+    g_string_append(chatlog_filename, "/chatlog.db");
+    gchar *result = g_strdup(chatlog_filename->str);
+    g_string_free(chatlog_filename, TRUE);
+    g_free(database_dir);
 
     return result;
 }
diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c
index 0718a287..343f2f60 100644
--- a/src/omemo/omemo.c
+++ b/src/omemo/omemo.c
@@ -227,37 +227,30 @@ omemo_on_connect(ProfAccount *account)
     omemo_ctx.device_list_handler = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL);
     omemo_ctx.known_devices = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)_g_hash_table_free);
 
-    char *omemodir = files_get_data_path(DIR_OMEMO);
-    GString *basedir = g_string_new(omemodir);
-    free(omemodir);
-    gchar *account_dir = str_replace(account->jid, "@", "_at_");
-    g_string_append(basedir, "/");
-    g_string_append(basedir, account_dir);
-    g_string_append(basedir, "/");
-    free(account_dir);
-
-    omemo_ctx.identity_filename = g_string_new(basedir->str);
-    g_string_append(omemo_ctx.identity_filename, "identity.txt");
-    omemo_ctx.trust_filename = g_string_new(basedir->str);
-    g_string_append(omemo_ctx.trust_filename, "trust.txt");
-    omemo_ctx.sessions_filename = g_string_new(basedir->str);
-    g_string_append(omemo_ctx.sessions_filename, "sessions.txt");
-    omemo_ctx.known_devices_filename = g_string_new(basedir->str);
-    g_string_append(omemo_ctx.known_devices_filename, "known_devices.txt");
+    gchar *omemo_dir = files_get_account_data_path(DIR_OMEMO, account->jid);
+
+    omemo_ctx.identity_filename = g_string_new(omemo_dir);
+    g_string_append(omemo_ctx.identity_filename, "/identity.txt");
+    omemo_ctx.trust_filename = g_string_new(omemo_dir);
+    g_string_append(omemo_ctx.trust_filename, "/trust.txt");
+    omemo_ctx.sessions_filename = g_string_new(omemo_dir);
+    g_string_append(omemo_ctx.sessions_filename, "/sessions.txt");
+    omemo_ctx.known_devices_filename = g_string_new(omemo_dir);
+    g_string_append(omemo_ctx.known_devices_filename, "/known_devices.txt");
 
 
     errno = 0;
-    int res = g_mkdir_with_parents(basedir->str, S_IRWXU);
+    int res = g_mkdir_with_parents(omemo_dir, S_IRWXU);
     if (res == -1) {
         char *errmsg = strerror(errno);
         if (errmsg) {
-            log_error("OMEMO: error creating directory: %s, %s", basedir->str, errmsg);
+            log_error("OMEMO: error creating directory: %s, %s", omemo_dir, errmsg);
         } else {
-            log_error("OMEMO: creating directory: %s", basedir->str);
+            log_error("OMEMO: creating directory: %s", omemo_dir);
         }
     }
 
-    g_string_free(basedir, TRUE);
+    g_free(omemo_dir);
 
     omemo_devicelist_subscribe();
 
diff --git a/src/otr/otr.c b/src/otr/otr.c
index 3c3ed618..38d35afb 100644
--- a/src/otr/otr.c
+++ b/src/otr/otr.c
@@ -127,24 +127,18 @@ static void
 cb_write_fingerprints(void *opdata)
 {
     gcry_error_t err = 0;
+    gchar *otr_dir = files_get_account_data_path(DIR_OTR, jid);
+
+    GString *fpsfilename = g_string_new(otr_dir);
+    g_string_append(fpsfilename, "/fingerprints.txt");
 
-    char *otrdir = files_get_data_path(DIR_OTR);
-    GString *basedir = g_string_new(otrdir);
-    free(otrdir);
-    gchar *account_dir = str_replace(jid, "@", "_at_");
-    g_string_append(basedir, "/");
-    g_string_append(basedir, account_dir);
-    g_string_append(basedir, "/");
-    free(account_dir);
-
-    GString *fpsfilename = g_string_new(basedir->str);
-    g_string_append(fpsfilename, "fingerprints.txt");
     err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
     if (err != GPG_ERR_NO_ERROR) {
         log_error("Failed to write fingerprints file");
         cons_show_error("Failed to create fingerprints file");
     }
-    g_string_free(basedir, TRUE);
+
+    g_free(otr_dir);
     g_string_free(fpsfilename, TRUE);
 }
 
@@ -216,19 +210,12 @@ otr_on_connect(ProfAccount *account)
     jid = strdup(account->jid);
     log_info("Loading OTR key for %s", jid);
 
-    char *otrdir = files_get_data_path(DIR_OTR);
-    GString *basedir = g_string_new(otrdir);
-    free(otrdir);
-    gchar *account_dir = str_replace(jid, "@", "_at_");
-    g_string_append(basedir, "/");
-    g_string_append(basedir, account_dir);
-    g_string_append(basedir, "/");
-    free(account_dir);
-
-    if (!mkdir_recursive(basedir->str)) {
-        log_error("Could not create %s for account %s.", basedir->str, jid);
-        cons_show_error("Could not create %s for account %s.", basedir->str, jid);
-        g_string_free(basedir, TRUE);
+    gchar *otr_dir = files_get_account_data_path(DIR_OTR, jid);
+
+    if (!mkdir_recursive(otr_dir)) {
+        log_error("Could not create %s for account %s.", otr_dir, jid);
+        cons_show_error("Could not create %s for account %s.", otr_dir, jid);
+        g_free(otr_dir);
         return;
     }
 
@@ -239,8 +226,8 @@ otr_on_connect(ProfAccount *account)
 
     gcry_error_t err = 0;
 
-    GString *keysfilename = g_string_new(basedir->str);
-    g_string_append(keysfilename, "keys.txt");
+    GString *keysfilename = g_string_new(otr_dir);
+    g_string_append(keysfilename, "/keys.txt");
     if (!g_file_test(keysfilename->str, G_FILE_TEST_IS_REGULAR)) {
         log_info("No OTR private key file found %s", keysfilename->str);
         data_loaded = FALSE;
@@ -250,7 +237,7 @@ otr_on_connect(ProfAccount *account)
         if (err != GPG_ERR_NO_ERROR) {
             log_warning("Failed to read OTR private key file: %s", keysfilename->str);
             cons_show_error("Failed to read OTR private key file: %s", keysfilename->str);
-            g_string_free(basedir, TRUE);
+            g_free(otr_dir);
             g_string_free(keysfilename, TRUE);
             return;
         }
@@ -259,7 +246,7 @@ otr_on_connect(ProfAccount *account)
         if (!privkey) {
             log_warning("No OTR private key found for account \"%s\", protocol \"xmpp\" in file: %s", jid, keysfilename->str);
             cons_show_error("No OTR private key found for account \"%s\", protocol \"xmpp\" in file: %s", jid, keysfilename->str);
-            g_string_free(basedir, TRUE);
+            g_free(otr_dir);
             g_string_free(keysfilename, TRUE);
             return;
         }
@@ -267,8 +254,8 @@ otr_on_connect(ProfAccount *account)
         data_loaded = TRUE;
     }
 
-    GString *fpsfilename = g_string_new(basedir->str);
-    g_string_append(fpsfilename, "fingerprints.txt");
+    GString *fpsfilename = g_string_new(otr_dir);
+    g_string_append(fpsfilename, "/fingerprints.txt");
     if (!g_file_test(fpsfilename->str, G_FILE_TEST_IS_REGULAR)) {
         log_info("No OTR fingerprints file found %s", fpsfilename->str);
         data_loaded = FALSE;
@@ -277,7 +264,7 @@ otr_on_connect(ProfAccount *account)
         err = otrl_privkey_read_fingerprints(user_state, fpsfilename->str, NULL, NULL);
         if (err != GPG_ERR_NO_ERROR) {
             log_error("Failed to load OTR fingerprints file: %s", fpsfilename->str);
-            g_string_free(basedir, TRUE);
+            g_free(otr_dir);
             g_string_free(keysfilename, TRUE);
             g_string_free(fpsfilename, TRUE);
             return;
@@ -291,7 +278,7 @@ otr_on_connect(ProfAccount *account)
         cons_show("Loaded OTR private key for %s", jid);
     }
 
-    g_string_free(basedir, TRUE);
+    g_free(otr_dir);
     g_string_free(keysfilename, TRUE);
     g_string_free(fpsfilename, TRUE);
     return;
@@ -394,18 +381,11 @@ otr_keygen(ProfAccount *account)
     jid = strdup(account->jid);
     log_info("Generating OTR key for %s", jid);
 
-    char *otrdir = files_get_data_path(DIR_OTR);
-    GString *basedir = g_string_new(otrdir);
-    free(otrdir);
-    gchar *account_dir = str_replace(jid, "@", "_at_");
-    g_string_append(basedir, "/");
-    g_string_append(basedir, account_dir);
-    g_string_append(basedir, "/");
-    free(account_dir);
-
-    if (!mkdir_recursive(basedir->str)) {
-        log_error("Could not create %s for account %s.", basedir->str, jid);
-        cons_show_error("Could not create %s for account %s.", basedir->str, jid);
+    gchar *otr_dir = files_get_account_data_path(DIR_OTR, jid);
+
+    if (!mkdir_recursive(otr_dir)) {
+        log_error("Could not create %s for account %s.", otr_dir, jid);
+        cons_show_error("Could not create %s for account %s.", otr_dir, jid);
         g_string_free(basedir, TRUE);
         return;
     }
@@ -413,14 +393,14 @@ otr_keygen(ProfAccount *account)
     gcry_error_t err = 0;
 
     GString *keysfilename = g_string_new(basedir->str);
-    g_string_append(keysfilename, "keys.txt");
+    g_string_append(keysfilename, "/keys.txt");
     log_debug("Generating private key file %s for %s", keysfilename->str, jid);
     cons_show("Generating private key, this may take some time.");
     cons_show("Moving the mouse randomly around the screen may speed up the process!");
     ui_update();
     err = otrl_privkey_generate(user_state, keysfilename->str, account->jid, "xmpp");
     if (err != GPG_ERR_NO_ERROR) {
-        g_string_free(basedir, TRUE);
+        g_free(otr_dir);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to generate private key");
         cons_show_error("Failed to generate private key");
@@ -430,12 +410,12 @@ otr_keygen(ProfAccount *account)
     cons_show("");
     cons_show("Private key generation complete.");
 
-    GString *fpsfilename = g_string_new(basedir->str);
-    g_string_append(fpsfilename, "fingerprints.txt");
+    GString *fpsfilename = g_string_new(otr_dir);
+    g_string_append(fpsfilename, "/fingerprints.txt");
     log_debug("Generating fingerprints file %s for %s", fpsfilename->str, jid);
     err = otrl_privkey_write_fingerprints(user_state, fpsfilename->str);
     if (err != GPG_ERR_NO_ERROR) {
-        g_string_free(basedir, TRUE);
+        g_free(otr_dir);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to create fingerprints file");
         cons_show_error("Failed to create fingerprints file");
@@ -445,7 +425,7 @@ otr_keygen(ProfAccount *account)
 
     err = otrl_privkey_read(user_state, keysfilename->str);
     if (err != GPG_ERR_NO_ERROR) {
-        g_string_free(basedir, TRUE);
+        g_free(otr_dir);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to load private key");
         data_loaded = FALSE;
@@ -454,7 +434,7 @@ otr_keygen(ProfAccount *account)
 
     err = otrl_privkey_read_fingerprints(user_state, fpsfilename->str, NULL, NULL);
     if (err != GPG_ERR_NO_ERROR) {
-        g_string_free(basedir, TRUE);
+        g_free(otr_dir);
         g_string_free(keysfilename, TRUE);
         log_error("Failed to load fingerprints");
         data_loaded = FALSE;
@@ -463,7 +443,7 @@ otr_keygen(ProfAccount *account)
 
     data_loaded = TRUE;
 
-    g_string_free(basedir, TRUE);
+    g_free(otr_dir);
     g_string_free(keysfilename, TRUE);
     g_string_free(fpsfilename, TRUE);
     return;
diff --git a/src/pgp/gpg.c b/src/pgp/gpg.c
index b50dd73f..d0834923 100644
--- a/src/pgp/gpg.c
+++ b/src/pgp/gpg.c
@@ -161,30 +161,26 @@ p_gpg_close(void)
 void
 p_gpg_on_connect(const char *const barejid)
 {
-    char *pgpdir = files_get_data_path(DIR_PGP);
-    GString *pubsfile = g_string_new(pgpdir);
-    free(pgpdir);
-    gchar *account_dir = str_replace(barejid, "@", "_at_");
-    g_string_append(pubsfile, "/");
-    g_string_append(pubsfile, account_dir);
-    free(account_dir);
+    gchar* pubsfile = files_get_account_data_path(DIR_PGP, barejid);
 
     // mkdir if doesn't exist for account
     errno = 0;
-    int res = g_mkdir_with_parents(pubsfile->str, S_IRWXU);
+    int res = g_mkdir_with_parents(pubsfile, S_IRWXU);
     if (res == -1) {
         char *errmsg = strerror(errno);
         if (errmsg) {
-            log_error("Error creating directory: %s, %s", pubsfile->str, errmsg);
+            log_error("Error creating directory: %s, %s", pubsfile, errmsg);
         } else {
-            log_error("Error creating directory: %s", pubsfile->str);
+            log_error("Error creating directory: %s", pubsfile);
         }
     }
 
     // create or read publickeys
-    g_string_append(pubsfile, "/pubkeys");
-    pubsloc = pubsfile->str;
-    g_string_free(pubsfile, FALSE);
+    GString *pubtmp = g_string_new(pubsfile);
+    g_string_append(pubtmp, "/pubkeys");
+    pubsloc = pubtmp->str;
+    g_string_free(pubtmp, FALSE);
+    g_free(pubsfile);
 
     if (g_file_test(pubsloc, G_FILE_TEST_EXISTS)) {
         g_chmod(pubsloc, S_IRUSR | S_IWUSR);