about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-03-23 12:18:25 +0100
committerMichael Vetter <jubalh@iodoru.org>2020-04-06 10:50:20 +0200
commit11663625cc04cbe779e77509118872cacbd1b3e1 (patch)
tree29bb3f2d12adfd44063568ea88b0484fd2ffbe28 /src
parent672f3e22e87e899a38efe1a498057fe87e24be6e (diff)
downloadprofani-tty-11663625cc04cbe779e77509118872cacbd1b3e1.tar.gz
db: Have one database per account
Diffstat (limited to 'src')
-rw-r--r--src/config/files.c16
-rw-r--r--src/config/files.h2
-rw-r--r--src/database.c45
-rw-r--r--src/database.h2
-rw-r--r--src/event/common.c2
-rw-r--r--src/event/server_events.c2
-rw-r--r--src/profanity.c3
7 files changed, 47 insertions, 25 deletions
diff --git a/src/config/files.c b/src/config/files.c
index e77bfa30..bdaf6a93 100644
--- a/src/config/files.c
+++ b/src/config/files.c
@@ -140,22 +140,6 @@ files_get_log_file(char *log_file)
 }
 
 char*
-files_get_chatlog_database_path(void)
-{
-    gchar *xdg_data = _files_get_xdg_data_home();
-    GString *logfile = g_string_new(xdg_data);
-
-    g_string_append(logfile, "/profanity/chatlog.db");
-
-    char *result = strdup(logfile->str);
-
-    free(xdg_data);
-    g_string_free(logfile, TRUE);
-
-    return result;
-}
-
-char*
 files_get_config_path(char *config_base)
 {
     gchar *xdg_config = _files_get_xdg_config_home();
diff --git a/src/config/files.h b/src/config/files.h
index 124c3ac8..1e16802a 100644
--- a/src/config/files.h
+++ b/src/config/files.h
@@ -55,12 +55,12 @@
 #define DIR_PGP "pgp"
 #define DIR_OMEMO "omemo"
 #define DIR_PLUGINS "plugins"
+#define DIR_DATABASE "database"
 
 void files_create_directories(void);
 
 char* files_get_config_path(char *config_base);
 char* files_get_data_path(char *data_base);
-char* files_get_chatlog_database_path(void);
 
 char* files_get_log_file(char *log_file);
 char* files_get_inputrc_file(void);
diff --git a/src/database.c b/src/database.c
index adfbe912..54005c2b 100644
--- a/src/database.c
+++ b/src/database.c
@@ -35,28 +35,65 @@
 
 #define _GNU_SOURCE 1
 
+#include <sys/stat.h>
 #include <sqlite3.h>
 #include <stdio.h>
 #include <glib.h>
+#include <errno.h>
 
 #include "log.h"
 #include "config/files.h"
+#include "config/account.h"
 #include "xmpp/xmpp.h"
 
 static sqlite3 *g_chatlog_database;
 
+static char*
+_get_db_filename(ProfAccount *account)
+{
+    char *databasedir = files_get_data_path(DIR_DATABASE);
+    GString *basedir = g_string_new(databasedir);
+    free(databasedir);
+
+    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);
+    if (res == -1) {
+        char *errmsg = strerror(errno);
+        if (errmsg) {
+            log_error("DATABASE: error creating directory: %s, %s", basedir->str, errmsg);
+        } else {
+            log_error("DATABASE: creating directory: %s", basedir->str);
+        }
+        g_string_free(basedir, TRUE);
+        return NULL;
+    }
+
+    g_string_append(basedir, "/chatlog.db");
+    char *result = strdup(basedir->str);
+    g_string_free(basedir, TRUE);
+
+    return result;
+}
+
 bool
-log_database_init(void)
+log_database_init(ProfAccount *account)
 {
     int ret = sqlite3_initialize();
-    char *filename = files_get_chatlog_database_path();
-
 	if (ret != SQLITE_OK) {
-        free(filename);
         log_error("Error initializing SQLite database: %d", ret);
         return FALSE;
 	}
 
+    char *filename = _get_db_filename(account);
+    if (!filename) {
+        return FALSE;
+    }
+
     ret = sqlite3_open(filename, &g_chatlog_database);
     if (ret != SQLITE_OK) {
         const char *err_msg = sqlite3_errmsg(g_chatlog_database);
diff --git a/src/database.h b/src/database.h
index 3e393707..82fa6aaa 100644
--- a/src/database.h
+++ b/src/database.h
@@ -36,7 +36,7 @@
 #ifndef DATABASE_H
 #define DATABASE_H
 
-bool log_database_init(void);
+bool log_database_init(ProfAccount *account);
 void log_database_close(void);
 void log_database_add(ProfMessage *message, gboolean is_muc);
 
diff --git a/src/event/common.c b/src/event/common.c
index 05c3f30b..4dc62b07 100644
--- a/src/event/common.c
+++ b/src/event/common.c
@@ -33,6 +33,7 @@
  *
  */
 
+#include "database.h"
 #include "config/tlscerts.h"
 #include "ui/ui.h"
 #include "xmpp/chat_session.h"
@@ -67,6 +68,7 @@ ev_disconnect_cleanup(void)
 #ifdef HAVE_OMEMO
     omemo_on_disconnect();
 #endif
+    log_database_close();
 }
 
 gboolean
diff --git a/src/event/server_events.c b/src/event/server_events.c
index 348e9b74..86176078 100644
--- a/src/event/server_events.c
+++ b/src/event/server_events.c
@@ -92,6 +92,8 @@ sv_ev_login_account_success(char *account_name, gboolean secured)
     omemo_on_connect(account);
 #endif
 
+    log_database_init(account);
+
     avatar_pep_subscribe();
 
     ui_handle_login_account_success(account, secured);
diff --git a/src/profanity.c b/src/profanity.c
index e6830e20..dc7bf954 100644
--- a/src/profanity.c
+++ b/src/profanity.c
@@ -53,7 +53,6 @@
 #include "profanity.h"
 #include "common.h"
 #include "log.h"
-#include "database.h"
 #include "config/files.h"
 #include "config/tlscerts.h"
 #include "config/accounts.h"
@@ -188,7 +187,6 @@ _init(char *log_level, char *config_file, char *log_file, char *theme_name)
         log_info("Starting Profanity (%s)...", PACKAGE_VERSION);
     }
 
-    log_database_init();
     chat_log_init();
     groupchat_log_init();
     accounts_load();
@@ -257,7 +255,6 @@ _shutdown(void)
 #ifdef HAVE_OMEMO
     omemo_close();
 #endif
-    log_database_close();
     chat_log_close();
     theme_close();
     accounts_close();