2024-09-16 14:11:01 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "packet.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "client/ui.h"
|
|
|
|
#include "client/db.h"
|
|
|
|
#include "client/user.h"
|
|
|
|
|
2024-09-18 09:41:54 +02:00
|
|
|
static int callback(void *ignore, int argc, char **argv, char **azColName)
|
2024-09-16 14:11:01 +02:00
|
|
|
{
|
2024-09-20 18:22:08 +02:00
|
|
|
char *username = memalloc(MAX_NAME);
|
2024-09-16 14:11:01 +02:00
|
|
|
strcpy(username, argv[0]);
|
2024-09-18 09:41:54 +02:00
|
|
|
/* Add only if it isn't talking yourself */
|
|
|
|
if (strncmp(username, USERNAME, MAX_NAME))
|
|
|
|
add_username(username);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2024-09-16 14:11:01 +02:00
|
|
|
|
2024-09-18 09:41:54 +02:00
|
|
|
static int get_shared_key(void *ignore, int argc, char **argv, char **column)
|
|
|
|
{
|
2024-09-16 14:11:01 +02:00
|
|
|
for(int i = 0; i < argc; i++) {
|
2024-09-18 09:41:54 +02:00
|
|
|
printf("%s = %s\n", column[i], argv[i] ? argv[i] : "NULL");
|
2024-09-16 14:11:01 +02:00
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2024-09-18 09:41:54 +02:00
|
|
|
|
2024-09-16 14:11:01 +02:00
|
|
|
int sqlite_init()
|
|
|
|
{
|
|
|
|
sqlite3 *db;
|
|
|
|
char *err_msg = 0;
|
|
|
|
|
2024-09-20 18:50:24 +02:00
|
|
|
char *zen_db = memalloc(PATH_MAX);
|
2024-09-20 19:23:34 +02:00
|
|
|
char *data_dir = replace_home(CLIENT_DATA_DIR);
|
|
|
|
snprintf(zen_db, PATH_MAX, "%s/%s", data_dir, "data.db");
|
|
|
|
free(data_dir);
|
2024-09-20 18:50:24 +02:00
|
|
|
if (access(zen_db, W_OK) != 0) {
|
2024-09-20 19:23:34 +02:00
|
|
|
/* If data file doesn't exist, most likely data dir won't exist too */
|
|
|
|
mkdir_p(CLIENT_DATA_DIR);
|
2024-09-20 18:50:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int rc = sqlite3_open(zen_db, &db);
|
2024-09-16 14:11:01 +02:00
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
|
|
|
sqlite3_close(db);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-09-18 09:41:54 +02:00
|
|
|
char *users_statement = "CREATE TABLE IF NOT EXISTS Users(Username TEXT, SharedKey TEXT, Test TEXT);";
|
|
|
|
char *shared_key_statement = "SELECT * FROM Users;";
|
2024-09-16 14:11:01 +02:00
|
|
|
char *messages_statement = "CREATE TABLE IF NOT EXISTS Messages(Username TEXT, );";
|
|
|
|
//"INSERT INTO Users VALUES('night', 'test', '1');";
|
|
|
|
|
|
|
|
rc = sqlite3_exec(db, users_statement, 0, 0, &err_msg);
|
|
|
|
|
|
|
|
if (rc != SQLITE_OK) {
|
|
|
|
error(0, "SQL error: %s", err_msg);
|
|
|
|
sqlite3_free(err_msg);
|
|
|
|
} else {
|
|
|
|
/* error(0, "Table created successfully"); */
|
|
|
|
}
|
|
|
|
|
|
|
|
// Select and print all entries
|
2024-09-18 09:41:54 +02:00
|
|
|
rc = sqlite3_exec(db, "SELECT * FROM Users", callback, NULL, &err_msg);
|
2024-09-16 14:11:01 +02:00
|
|
|
|
2024-09-18 09:41:54 +02:00
|
|
|
if (rc != SQLITE_OK) {
|
2024-09-16 14:11:01 +02:00
|
|
|
error(0, "SQL error: %s\n", err_msg);
|
|
|
|
sqlite3_free(err_msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
sqlite3_close(db);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|