about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-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();
.py?id=aefd8048d2b246c0111ac1227ade2a9a9d38c35c'>aefd8048 ^
0128bee7 ^
c2238598 ^

7b1db25e ^
1f2ffbb3 ^

5d4f9249 ^
1f2ffbb3 ^
5d4f9249 ^


1f2ffbb3 ^
c2238598 ^

5d4f9249 ^
1f2ffbb3 ^




c2238598 ^
1f2ffbb3 ^

3cec441a ^
37aac04b ^
3cec441a ^
1f2ffbb3 ^

c2238598 ^
037862e3 ^


c2238598 ^

037862e3 ^
c2238598 ^
037862e3 ^





c2238598 ^
c2238598 ^


7a488bda ^
037862e3 ^
3cec441a ^

7a488bda ^




73811372 ^

a2853ab6 ^
73811372 ^

1f2ffbb3 ^
c2238598 ^
037862e3 ^

3cec441a ^


c2238598 ^
037862e3 ^

c2238598 ^



0128bee7 ^
c135aa1c ^
37aac04b ^
c135aa1c ^











d0008325 ^







ac226d23 ^











37aac04b ^








d0008325 ^
ac226d23 ^
3937f010 ^
ac226d23 ^
3937f010 ^
d4e2dfe9 ^

1621a40c ^

0128bee7 ^
80d26787 ^

d0008325 ^



9b894208 ^




aefd8048 ^
d0008325 ^
27490819 ^
d0008325 ^
7b1db25e ^
1621a40c ^
c2238598 ^
1621a40c ^

037862e3 ^
c2238598 ^

d0008325 ^

c2238598 ^


142a3127 ^
27490819 ^

b289f679 ^

d0008325 ^
c2238598 ^



27490819 ^


e8bab380 ^
27490819 ^
b289f679 ^

d0008325 ^
7b1db25e ^
d0008325 ^
5b841638 ^


d0008325 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238