-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatdbhandler.cpp
More file actions
270 lines (233 loc) · 9.62 KB
/
Copy pathchatdbhandler.cpp
File metadata and controls
270 lines (233 loc) · 9.62 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "chatdbhandler.h"
#include "usersystem.h"
#include "encryptionutils.h"
#include "crypto_helpers.h"
#include <QSqlRecord>
#include <QVariant>
ChatDatabaseHandler::ChatDatabaseHandler(QObject *parent)
: QObject(parent), dbInitialized(false) {}
ChatDatabaseHandler::~ChatDatabaseHandler() {
if (db.isOpen()) db.close();
}
bool ChatDatabaseHandler::initialize() {
if (dbInitialized) return true;
db = QSqlDatabase::database();
if (!db.isValid() || !db.isOpen()) {
qDebug() << "Database connection invalid or closed:" << db.lastError().text();
return false;
}
// Ensure tables exist
QSqlQuery query(db);
query.exec(R"(
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
email TEXT,
password_hash TEXT,
failed_attempts INTEGER DEFAULT 0
)
)");
query.exec(R"(
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sender_id INTEGER,
recipient_id INTEGER,
ciphertext TEXT,
metadata TEXT,
timestamp TEXT,
delivered INTEGER DEFAULT 0,
FOREIGN KEY(sender_id) REFERENCES users(id),
FOREIGN KEY(recipient_id) REFERENCES users(id)
)
)");
query.exec("CREATE INDEX IF NOT EXISTS idx_messages_recipient ON messages(recipient_id)");
dbInitialized = true;
return true;
}
QString ChatDatabaseHandler::loginUser(const QString &username, const QString &password) {
// Prefer using UserSystem for verification; this legacy path retained for compatibility
if (!dbInitialized) return {};
QSqlQuery query(db);
query.prepare("SELECT password_hash, failed_attempts FROM users WHERE username = :username");
query.bindValue(":username", username);
if (!query.exec() || !query.next()) return {};
QString storedHex = query.value(0).toString();
int attempts = query.value(1).toInt();
// Verify using the same PBKDF2 scheme as UserSystem
QByteArray stored = QByteArray::fromHex(storedHex.toLatin1());
bool ok = false;
if (stored.size() >= 48) {
QByteArray salt = stored.left(16);
QByteArray dk = stored.mid(16,32);
QByteArray test = pbkdf2_hmac_sha256(password.toUtf8(), salt, 100000, dk.size());
ok = (test == dk);
}
if (!ok) {
attempts++;
updateFailedAttempts(username, attempts);
if (attempts >= 3) {
deleteUndeliveredMessagesForUser(username);
resetFailedAttempts(username);
qDebug() << "3 failed attempts reached. Undelivered messages deleted.";
}
return {};
}
resetFailedAttempts(username);
return username;
}
bool ChatDatabaseHandler::registerUser(const QString &username, const QString &email, const QString &passwordHash) {
if (!dbInitialized) return false;
QSqlQuery check(db);
check.prepare("SELECT id FROM users WHERE username = :username OR email = :email");
check.bindValue(":username", username);
check.bindValue(":email", email);
if (check.exec() && check.next()) return false;
QSqlQuery insert(db);
insert.prepare("INSERT INTO users (username, email, password_hash) VALUES (:u, :e, :p)");
insert.bindValue(":u", username);
insert.bindValue(":e", email);
insert.bindValue(":p", passwordHash);
return insert.exec();
}
bool ChatDatabaseHandler::updateFailedAttempts(const QString &username, int attempts) {
QSqlQuery q(db);
q.prepare("UPDATE users SET failed_attempts = :a WHERE username = :u");
q.bindValue(":a", attempts);
q.bindValue(":u", username);
return q.exec();
}
bool ChatDatabaseHandler::resetFailedAttempts(const QString &username) {
return updateFailedAttempts(username, 0);
}
int ChatDatabaseHandler::getFailedAttempts(const QString &username) {
QSqlQuery q(db);
q.prepare("SELECT failed_attempts FROM users WHERE username = :u");
q.bindValue(":u", username);
if (q.exec() && q.next()) return q.value(0).toInt();
return 0;
}
QString ChatDatabaseHandler::getPasswordHash(const QString &username) {
QSqlQuery q(db);
q.prepare("SELECT password_hash FROM users WHERE username = :u");
q.bindValue(":u", username);
if (q.exec() && q.next()) return q.value(0).toString();
return {};
}
QString ChatDatabaseHandler::userExists(const QString &username) {
if (!dbInitialized) return {};
QSqlQuery q(db);
q.prepare("SELECT username FROM users WHERE username = :u LIMIT 1");
q.bindValue(":u", username);
if (q.exec() && q.next()) return q.value(0).toString();
return {};
}
bool ChatDatabaseHandler::markMessageDelivered(int messageId) {
QSqlQuery q(db);
q.prepare("UPDATE messages SET delivered = 1 WHERE id = :id");
q.bindValue(":id", messageId);
return q.exec();
}
bool ChatDatabaseHandler::deleteUndeliveredMessagesForUser(const QString &username) {
QSqlQuery q(db);
q.prepare(R"(
DELETE FROM messages
WHERE receiver_id = (SELECT id FROM users WHERE username = :u)
AND delivered = 0
)");
q.bindValue(":u", username);
return q.exec();
}
void ChatDatabaseHandler::enqueueOfflineMessage(const Message &msg) {
QMutexLocker locker(&queueMutex);
offlineMessageQueue.push(msg);
// Encrypt plaintext and serialize metadata
EncryptionUtils enc;
MessageEncryptionMetadata md;
QByteArray ciphertext = enc.encryptMessage(msg.content.toUtf8(), md);
QByteArray metadataJson = enc.serializeMetadata(md);
QSqlQuery q(db);
q.prepare(R"(
INSERT INTO messages (sender_id, recipient_id, ciphertext, metadata, timestamp, delivered)
VALUES ((SELECT id FROM users WHERE username = :s),
(SELECT id FROM users WHERE username = :r),
:c, :m, :t, 0)
)");
q.bindValue(":s", msg.sender);
q.bindValue(":r", msg.receiver);
q.bindValue(":c", QString(ciphertext.toBase64()));
q.bindValue(":m", QString::fromUtf8(metadataJson));
q.bindValue(":t", msg.timestamp.toString(Qt::ISODate));
if (!q.exec()) qDebug() << "Failed to queue message:" << q.lastError().text();
}
bool ChatDatabaseHandler::sendDirectMessage(const QString &sender, const QString &recipient, const QString &content, UserSystem &userSystem) {
if (!dbInitialized || content.isEmpty()) return false;
if (!userSystem.isUserOnline(recipient)) {
enqueueOfflineMessage({sender, recipient, content, QDateTime::currentDateTime()});
return true;
}
// Encrypt plaintext and serialize metadata for immediate delivery record
EncryptionUtils enc;
MessageEncryptionMetadata md;
QByteArray ciphertext = enc.encryptMessage(content.toUtf8(), md);
QByteArray metadataJson = enc.serializeMetadata(md);
QSqlQuery q(db);
q.prepare(R"(
INSERT INTO messages (sender_id, recipient_id, ciphertext, metadata, timestamp, delivered)
VALUES ((SELECT id FROM users WHERE username = :s),
(SELECT id FROM users WHERE username = :r),
:c, :m, :t, 1)
)");
q.bindValue(":s", sender);
q.bindValue(":r", recipient);
q.bindValue(":c", QString(ciphertext.toBase64()));
q.bindValue(":m", QString::fromUtf8(metadataJson));
q.bindValue(":t", QDateTime::currentDateTime().toString(Qt::ISODate));
return q.exec();
}
bool ChatDatabaseHandler::dequeueOfflineMessage(Message &msg) {
QMutexLocker locker(&queueMutex);
if (offlineMessageQueue.empty()) return false;
msg = offlineMessageQueue.front();
offlineMessageQueue.pop();
return true;
}
QList<std::tuple<QString, QString, QString, QDateTime>> ChatDatabaseHandler::getDirectMessageHistory(const QString &u1, const QString &u2, int limit) {
QList<std::tuple<QString, QString, QString, QDateTime>> messages;
if (!dbInitialized) return messages;
QSqlQuery q(db);
q.prepare(R"(
SELECT s.username, r.username, m.ciphertext, m.metadata, m.timestamp
FROM messages m
JOIN users s ON m.sender_id = s.id
JOIN users r ON m.recipient_id = r.id
WHERE (s.username = :u1 AND r.username = :u2)
OR (s.username = :u2 AND r.username = :u1)
ORDER BY m.timestamp DESC LIMIT :l
)");
q.bindValue(":u1", u1);
q.bindValue(":u2", u2);
q.bindValue(":l", limit);
if (q.exec()) {
while (q.next()) {
const QString senderName = q.value(0).toString();
const QString receiverName = q.value(1).toString();
const QString ciphertextB64 = q.value(2).toString();
const QString metadataStr = q.value(3).toString();
const QDateTime ts = QDateTime::fromString(q.value(4).toString(), Qt::ISODate);
QString plaintext;
if (!metadataStr.isEmpty()) {
EncryptionUtils enc;
MessageEncryptionMetadata md = enc.deserializeMetadata(metadataStr.toUtf8());
QByteArray cipher = QByteArray::fromBase64(ciphertextB64.toUtf8());
QByteArray plain = enc.decryptMessage(cipher, md);
plaintext = QString::fromUtf8(plain);
} else {
// Fallback for legacy rows stored as plaintext
plaintext = ciphertextB64;
}
messages.append({senderName, receiverName, plaintext, ts});
}
std::reverse(messages.begin(), messages.end());
}
return messages;
}