feat: complete field parity, harden export/import, add tests
Export/Import improvements: - Replace pagination with direct SQL query (db_sqlite_get_all_chat) - Wrap import in SQL transaction with rollback on error - Add fsync before fclose in export for data safety - Sort merged output by timestamp with secondary key (stanza_id, from_jid) - Export archive_id and marked_read from SQLite (lossless migration) - Add progress indication every 500 messages during write/import - Expand dedup key body prefix from 64 to 256 chars - Fix g_slist_append O(n²) → g_slist_prepend + g_slist_reverse O(n) Field parity (to_jid, to_resource, marked_read): - Add fields to ff_parsed_line_t struct - Write/parse to:|to_res:|read: metadata tags in flatfile format - Pass to_resource through _ff_add_message and all callers - Add marked_read to ProfMessage struct with -1 default (unset) - Preserve fields across export/import round-trips Tests (19 new: 11 unit + 8 functional): - Unit: to_jid_and_marked_read, bracket_in_stanza_id, backslash_in_resource, mucpm_type, all_enc_types, crlf_handling, to_jid_special_chars, multiple_lines, parsed_line_free_null_safe, no_space_rejected, unclosed_bracket - Functional: export_idempotent_no_duplicates, export_lmc_correction_survives, switch_preserves_old_backend_data, export_all_contacts, import_double_dedup, verify_after_export, switch_backends_independent_messages, export_empty_contact - Rebalance test groups: move Chat Session from Group 3 to Group 4 (25/33/30/27 instead of 25/33/36/21) - Remove hardcoded test counts from group comments Man page: - Document /history switch sqlite|flatfile
This commit is contained in:
@@ -88,6 +88,10 @@ extern db_backend_t* active_db_backend;
|
||||
#ifdef HAVE_SQLITE
|
||||
db_backend_t* db_backend_sqlite(void);
|
||||
GSList* db_sqlite_list_contacts(void);
|
||||
GSList* db_sqlite_get_all_chat(const gchar* const contact_barejid);
|
||||
void db_sqlite_begin_transaction(void);
|
||||
void db_sqlite_end_transaction(void);
|
||||
void db_sqlite_rollback_transaction(void);
|
||||
#endif
|
||||
db_backend_t* db_backend_flatfile(void);
|
||||
|
||||
|
||||
@@ -64,8 +64,8 @@ _make_dedup_key(const char* stanza_id, const char* timestamp, const char* from_j
|
||||
if (stanza_id && strlen(stanza_id) > 0)
|
||||
return g_strdup(stanza_id);
|
||||
|
||||
// Fallback: simple composite key
|
||||
auto_gchar gchar* raw = g_strdup_printf("%s|%s|%.64s",
|
||||
// Fallback: composite key from timestamp + sender + body prefix
|
||||
auto_gchar gchar* raw = g_strdup_printf("%s|%s|%.256s",
|
||||
timestamp ? timestamp : "",
|
||||
from_jid ? from_jid : "",
|
||||
body ? body : "");
|
||||
@@ -118,13 +118,13 @@ _ff_list_contacts(void)
|
||||
if (g_file_test(full_path, G_FILE_TEST_IS_REGULAR)) {
|
||||
char* jid = _dir_to_jid(entry);
|
||||
if (jid) {
|
||||
contacts = g_slist_append(contacts, jid);
|
||||
contacts = g_slist_prepend(contacts, jid);
|
||||
}
|
||||
}
|
||||
}
|
||||
g_dir_close(dir);
|
||||
|
||||
return contacts;
|
||||
return g_slist_reverse(contacts);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -161,12 +161,31 @@ _ff_read_all_lines(const char* contact_barejid)
|
||||
ff_parsed_line_t* pl = ff_parse_line(buf);
|
||||
free(buf);
|
||||
if (pl) {
|
||||
lines = g_slist_append(lines, pl);
|
||||
lines = g_slist_prepend(lines, pl);
|
||||
}
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return lines;
|
||||
return g_slist_reverse(lines);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Comparator for sorting ff_parsed_line_t by timestamp (ISO8601 is lexicographic)
|
||||
// =========================================================================
|
||||
|
||||
static gint
|
||||
_compare_parsed_lines_by_timestamp(gconstpointer a, gconstpointer b)
|
||||
{
|
||||
const ff_parsed_line_t* la = a;
|
||||
const ff_parsed_line_t* lb = b;
|
||||
int cmp = g_strcmp0(la->timestamp_str, lb->timestamp_str);
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
// Secondary key: stanza_id (stabilises sort for same-second messages)
|
||||
cmp = g_strcmp0(la->stanza_id, lb->stanza_id);
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
return g_strcmp0(la->from_jid, lb->from_jid);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
@@ -235,48 +254,8 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
||||
}
|
||||
int existing_count = g_slist_length(existing);
|
||||
|
||||
// 2. Query SQLite for this contact — get ALL messages ordered by timestamp
|
||||
// We read all messages by paginating through the existing get_previous_chat
|
||||
// But it limits to MESSAGES_TO_RETRIEVE... We need a direct SQL query.
|
||||
// Use the same approach as verify — iterate with raw SQL.
|
||||
|
||||
// For now, collect all messages into a list via repeated calls
|
||||
// Actually, let's use a simpler approach: read all from flatfile existing data,
|
||||
// then query SQLite for everything, merge, write back.
|
||||
|
||||
// Read all SQLite messages for this contact
|
||||
GSList* sqlite_lines = NULL;
|
||||
// Use the backend's get_previous_chat with a wide time range
|
||||
// This is limited to MESSAGES_TO_RETRIEVE, so we need to paginate
|
||||
gboolean has_more = TRUE;
|
||||
gchar* start_time = NULL;
|
||||
|
||||
while (has_more) {
|
||||
GSList* batch = NULL;
|
||||
db_history_result_t res = sqlite_be->get_previous_chat(
|
||||
contact, start_time, NULL, TRUE, FALSE, &batch);
|
||||
|
||||
if (res != DB_RESPONSE_SUCCESS || !batch) {
|
||||
has_more = FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
int batch_size = g_slist_length(batch);
|
||||
|
||||
// Get the last timestamp for the next query
|
||||
ProfMessage* last_msg = g_slist_last(batch)->data;
|
||||
if (last_msg && last_msg->timestamp) {
|
||||
g_free(start_time);
|
||||
start_time = g_date_time_format_iso8601(last_msg->timestamp);
|
||||
}
|
||||
|
||||
sqlite_lines = g_slist_concat(sqlite_lines, batch);
|
||||
|
||||
if (batch_size < MESSAGES_TO_RETRIEVE) {
|
||||
has_more = FALSE;
|
||||
}
|
||||
}
|
||||
g_free(start_time);
|
||||
// 2. Query SQLite for this contact — get ALL messages via direct SQL
|
||||
GSList* sqlite_lines = db_sqlite_get_all_chat(contact);
|
||||
|
||||
if (!sqlite_lines) {
|
||||
log_debug("export: no SQLite messages for %s", contact);
|
||||
@@ -317,16 +296,31 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
||||
int contact_exported = 0;
|
||||
int contact_skipped = 0;
|
||||
|
||||
// Write existing flatfile lines first (preserve originals)
|
||||
// Collect ALL lines into a merged list for sorted output
|
||||
GSList* merged = NULL;
|
||||
|
||||
// Add existing flatfile lines (mark as already seen in dedup)
|
||||
for (GSList* l = existing; l; l = l->next) {
|
||||
ff_parsed_line_t* pl = l->data;
|
||||
ff_write_line(fp, pl->timestamp_str,
|
||||
pl->type, pl->enc,
|
||||
pl->stanza_id, pl->archive_id, pl->replace_id,
|
||||
pl->from_jid, pl->from_resource, pl->message);
|
||||
// Create a shallow copy for the merged list (originals freed separately)
|
||||
ff_parsed_line_t* copy = g_malloc0(sizeof(ff_parsed_line_t));
|
||||
copy->timestamp_str = g_strdup(pl->timestamp_str);
|
||||
copy->timestamp = pl->timestamp ? g_date_time_ref(pl->timestamp) : NULL;
|
||||
copy->type = g_strdup(pl->type);
|
||||
copy->enc = g_strdup(pl->enc);
|
||||
copy->stanza_id = g_strdup(pl->stanza_id);
|
||||
copy->archive_id = g_strdup(pl->archive_id);
|
||||
copy->replace_id = g_strdup(pl->replace_id);
|
||||
copy->from_jid = g_strdup(pl->from_jid);
|
||||
copy->from_resource = g_strdup(pl->from_resource);
|
||||
copy->to_jid = g_strdup(pl->to_jid);
|
||||
copy->to_resource = g_strdup(pl->to_resource);
|
||||
copy->marked_read = pl->marked_read;
|
||||
copy->message = g_strdup(pl->message);
|
||||
merged = g_slist_prepend(merged, copy);
|
||||
}
|
||||
|
||||
// Write SQLite messages that aren't already in the flatfile
|
||||
// Add SQLite messages that aren't already in the flatfile
|
||||
for (GSList* l = sqlite_lines; l; l = l->next) {
|
||||
ProfMessage* msg = l->data;
|
||||
if (!msg || !msg->timestamp)
|
||||
@@ -345,17 +339,47 @@ log_database_export_to_flatfile(const gchar* const contact_jid)
|
||||
}
|
||||
g_hash_table_add(seen_keys, key);
|
||||
|
||||
const char* type_str = ff_get_message_type_str(msg->type);
|
||||
const char* enc_str = ff_get_message_enc_str(msg->enc);
|
||||
const char* from_res = msg->from_jid ? msg->from_jid->resourcepart : NULL;
|
||||
|
||||
ff_write_line(fp, ts, type_str, enc_str,
|
||||
sid, msg->stanzaid, NULL,
|
||||
from_jid, from_res, body);
|
||||
ff_parsed_line_t* pl = g_malloc0(sizeof(ff_parsed_line_t));
|
||||
pl->timestamp_str = g_strdup(ts);
|
||||
pl->timestamp = g_date_time_ref(msg->timestamp);
|
||||
pl->type = g_strdup(ff_get_message_type_str(msg->type));
|
||||
pl->enc = g_strdup(ff_get_message_enc_str(msg->enc));
|
||||
pl->stanza_id = g_strdup(sid);
|
||||
pl->archive_id = msg->stanzaid ? g_strdup(msg->stanzaid) : NULL;
|
||||
pl->replace_id = NULL;
|
||||
pl->from_jid = g_strdup(from_jid);
|
||||
pl->from_resource = msg->from_jid ? g_strdup(msg->from_jid->resourcepart) : NULL;
|
||||
pl->to_jid = msg->to_jid ? g_strdup(msg->to_jid->barejid) : NULL;
|
||||
pl->to_resource = msg->to_jid ? g_strdup(msg->to_jid->resourcepart) : NULL;
|
||||
pl->marked_read = msg->marked_read;
|
||||
pl->message = g_strdup(body);
|
||||
merged = g_slist_prepend(merged, pl);
|
||||
contact_exported++;
|
||||
}
|
||||
|
||||
// Sort merged list by timestamp (ISO8601 is lexicographically sortable)
|
||||
merged = g_slist_sort(merged, _compare_parsed_lines_by_timestamp);
|
||||
|
||||
// Write all lines sorted
|
||||
int written = 0;
|
||||
for (GSList* l = merged; l; l = l->next) {
|
||||
ff_parsed_line_t* pl = l->data;
|
||||
ff_write_line(fp, pl->timestamp_str,
|
||||
pl->type, pl->enc,
|
||||
pl->stanza_id, pl->archive_id, pl->replace_id,
|
||||
pl->from_jid, pl->from_resource,
|
||||
pl->to_jid, pl->to_resource, pl->marked_read,
|
||||
pl->message);
|
||||
written++;
|
||||
if (written % 500 == 0) {
|
||||
cons_show(" ... %s: %d/%d written so far", contact, written, (int)g_slist_length(merged));
|
||||
}
|
||||
}
|
||||
|
||||
g_slist_free_full(merged, (GDestroyNotify)ff_parsed_line_free);
|
||||
|
||||
fflush(fp);
|
||||
fsync(fd); // ensure data is on disk before atomic rename
|
||||
fclose(fp); // also releases flock
|
||||
|
||||
// Atomic rename
|
||||
@@ -430,48 +454,26 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
||||
// 2. Build dedup set from SQLite (existing stanza_ids + fallback keys)
|
||||
GHashTable* seen_keys = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
||||
|
||||
// Read all SQLite messages for this contact
|
||||
gboolean has_more = TRUE;
|
||||
gchar* start_time = NULL;
|
||||
|
||||
while (has_more) {
|
||||
GSList* batch = NULL;
|
||||
db_history_result_t res = sqlite_be->get_previous_chat(
|
||||
contact, start_time, NULL, TRUE, FALSE, &batch);
|
||||
|
||||
if (res != DB_RESPONSE_SUCCESS || !batch) {
|
||||
has_more = FALSE;
|
||||
break;
|
||||
}
|
||||
|
||||
int batch_size = g_slist_length(batch);
|
||||
|
||||
ProfMessage* last_msg = g_slist_last(batch)->data;
|
||||
if (last_msg && last_msg->timestamp) {
|
||||
g_free(start_time);
|
||||
start_time = g_date_time_format_iso8601(last_msg->timestamp);
|
||||
}
|
||||
|
||||
for (GSList* l = batch; l; l = l->next) {
|
||||
ProfMessage* msg = l->data;
|
||||
if (!msg)
|
||||
continue;
|
||||
auto_gchar gchar* ts = msg->timestamp ? g_date_time_format_iso8601(msg->timestamp) : g_strdup("");
|
||||
char* key = _make_dedup_key(msg->id, ts, msg->from_jid ? msg->from_jid->barejid : "", msg->plain);
|
||||
g_hash_table_add(seen_keys, key);
|
||||
}
|
||||
|
||||
g_slist_free_full(batch, (GDestroyNotify)message_free);
|
||||
|
||||
if (batch_size < MESSAGES_TO_RETRIEVE) {
|
||||
has_more = FALSE;
|
||||
}
|
||||
// Read all SQLite messages for this contact via direct SQL query
|
||||
GSList* existing_msgs = db_sqlite_get_all_chat(contact);
|
||||
for (GSList* l = existing_msgs; l; l = l->next) {
|
||||
ProfMessage* msg = l->data;
|
||||
if (!msg)
|
||||
continue;
|
||||
auto_gchar gchar* ts = msg->timestamp ? g_date_time_format_iso8601(msg->timestamp) : g_strdup("");
|
||||
char* key = _make_dedup_key(msg->id, ts, msg->from_jid ? msg->from_jid->barejid : "", msg->plain);
|
||||
g_hash_table_add(seen_keys, key);
|
||||
}
|
||||
g_free(start_time);
|
||||
g_slist_free_full(existing_msgs, (GDestroyNotify)message_free);
|
||||
|
||||
// 3. For each flatfile line not in seen_keys, insert via add_incoming/add_outgoing
|
||||
// 3. For each flatfile line not in seen_keys, insert via add_incoming
|
||||
// Wrap in a transaction for atomicity and performance
|
||||
int contact_imported = 0;
|
||||
int contact_skipped = 0;
|
||||
int total_lines = g_slist_length(ff_lines);
|
||||
|
||||
db_sqlite_begin_transaction();
|
||||
gboolean import_ok = TRUE;
|
||||
|
||||
for (GSList* l = ff_lines; l; l = l->next) {
|
||||
ff_parsed_line_t* pl = l->data;
|
||||
@@ -504,15 +506,34 @@ log_database_import_from_flatfile(const gchar* const contact_jid)
|
||||
|
||||
if (is_outgoing) {
|
||||
msg->from_jid = jid_create(myjid->barejid);
|
||||
msg->to_jid = jid_create(contact);
|
||||
if (pl->to_jid) {
|
||||
msg->to_jid = jid_create_from_bare_and_resource(pl->to_jid, pl->to_resource);
|
||||
} else {
|
||||
msg->to_jid = jid_create(contact);
|
||||
}
|
||||
} else {
|
||||
msg->from_jid = jid_create_from_bare_and_resource(pl->from_jid, pl->from_resource);
|
||||
msg->to_jid = jid_create(myjid->barejid);
|
||||
if (pl->to_jid) {
|
||||
msg->to_jid = jid_create_from_bare_and_resource(pl->to_jid, pl->to_resource);
|
||||
} else {
|
||||
msg->to_jid = jid_create(myjid->barejid);
|
||||
}
|
||||
}
|
||||
|
||||
sqlite_be->add_incoming(msg);
|
||||
message_free(msg);
|
||||
contact_imported++;
|
||||
|
||||
if (contact_imported % 500 == 0) {
|
||||
cons_show(" ... %s: %d/%d imported so far", contact, contact_imported, total_lines);
|
||||
}
|
||||
}
|
||||
|
||||
if (import_ok) {
|
||||
db_sqlite_end_transaction();
|
||||
} else {
|
||||
db_sqlite_rollback_transaction();
|
||||
cons_show_error("Import of %s failed — transaction rolled back.", contact);
|
||||
}
|
||||
|
||||
cons_show("Imported %s: %d new, %d skipped (already in SQLite)",
|
||||
|
||||
@@ -320,8 +320,8 @@ _ff_get_state(const char* contact_barejid)
|
||||
static void
|
||||
_ff_add_message(const char* type, const char* stanza_id, const char* archive_id,
|
||||
const char* replace_id, const char* from_barejid, const char* from_resource,
|
||||
const char* to_barejid, const char* message_text, GDateTime* timestamp,
|
||||
prof_enc_t enc)
|
||||
const char* to_barejid, const char* to_resource, const char* message_text,
|
||||
GDateTime* timestamp, prof_enc_t enc)
|
||||
{
|
||||
auto_gchar gchar* pref_dblog = prefs_get_string(PREF_DBLOG);
|
||||
|
||||
@@ -408,7 +408,9 @@ _ff_add_message(const char* type, const char* stanza_id, const char* archive_id,
|
||||
|
||||
ff_write_line(fp, date_fmt, type, ff_get_message_enc_str(enc),
|
||||
stanza_id, archive_id, replace_id,
|
||||
from_barejid, from_resource, effective_msg);
|
||||
from_barejid, from_resource,
|
||||
to_barejid, to_resource, -1,
|
||||
effective_msg);
|
||||
|
||||
fflush(fp);
|
||||
// fclose also releases the flock
|
||||
@@ -586,7 +588,7 @@ _flatfile_add_incoming(ProfMessage* message)
|
||||
|
||||
_ff_add_message(type, message->id, message->stanzaid, message->replace_id,
|
||||
message->from_jid->barejid, message->from_jid->resourcepart,
|
||||
to_jid->barejid, message->plain,
|
||||
to_jid->barejid, to_jid->resourcepart, message->plain,
|
||||
message->timestamp, message->enc);
|
||||
}
|
||||
|
||||
@@ -597,7 +599,7 @@ _flatfile_add_outgoing_chat(const char* const id, const char* const barejid,
|
||||
const Jid* myjid = connection_get_jid();
|
||||
_ff_add_message("chat", id, NULL, replace_id,
|
||||
myjid ? myjid->barejid : "me", myjid ? myjid->resourcepart : NULL,
|
||||
barejid, message, NULL, enc);
|
||||
barejid, NULL, message, NULL, enc);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -607,7 +609,7 @@ _flatfile_add_outgoing_muc(const char* const id, const char* const barejid,
|
||||
const Jid* myjid = connection_get_jid();
|
||||
_ff_add_message("muc", id, NULL, replace_id,
|
||||
myjid ? myjid->barejid : "me", myjid ? myjid->resourcepart : NULL,
|
||||
barejid, message, NULL, enc);
|
||||
barejid, NULL, message, NULL, enc);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -617,7 +619,7 @@ _flatfile_add_outgoing_muc_pm(const char* const id, const char* const barejid,
|
||||
const Jid* myjid = connection_get_jid();
|
||||
_ff_add_message("mucpm", id, NULL, replace_id,
|
||||
myjid ? myjid->barejid : "me", myjid ? myjid->resourcepart : NULL,
|
||||
barejid, message, NULL, enc);
|
||||
barejid, NULL, message, NULL, enc);
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
|
||||
@@ -60,6 +60,9 @@ typedef struct
|
||||
char* replace_id;
|
||||
char* from_jid;
|
||||
char* from_resource;
|
||||
char* to_jid;
|
||||
char* to_resource;
|
||||
int marked_read; // -1 = unset (NULL in DB), 0 = unread, 1 = read
|
||||
char* message;
|
||||
off_t file_offset;
|
||||
} ff_parsed_line_t;
|
||||
@@ -125,7 +128,9 @@ char* ff_unescape_meta_value(const char* val);
|
||||
char* ff_readline(FILE* fp, gboolean* truncated);
|
||||
void ff_write_line(FILE* fp, const char* timestamp, const char* type, const char* enc,
|
||||
const char* stanza_id, const char* archive_id, const char* replace_id,
|
||||
const char* from_jid, const char* from_resource, const char* message_text);
|
||||
const char* from_jid, const char* from_resource,
|
||||
const char* to_jid, const char* to_resource, int marked_read,
|
||||
const char* message_text);
|
||||
|
||||
// --- Parser helpers ---
|
||||
|
||||
|
||||
@@ -402,14 +402,16 @@ ff_readline(FILE* fp, gboolean* truncated)
|
||||
void
|
||||
ff_write_line(FILE* fp, const char* timestamp, const char* type, const char* enc,
|
||||
const char* stanza_id, const char* archive_id, const char* replace_id,
|
||||
const char* from_jid, const char* from_resource, const char* message_text)
|
||||
const char* from_jid, const char* from_resource,
|
||||
const char* to_jid, const char* to_resource, int marked_read,
|
||||
const char* message_text)
|
||||
{
|
||||
// Escape metadata values from remote peers
|
||||
auto_gchar gchar* safe_sid = ff_escape_meta_value(stanza_id);
|
||||
auto_gchar gchar* safe_aid = ff_escape_meta_value(archive_id);
|
||||
auto_gchar gchar* safe_rid = ff_escape_meta_value(replace_id);
|
||||
|
||||
// Build metadata section: [type|enc|id:...|aid:...|corrects:...]
|
||||
// Build metadata section: [type|enc|id:...|aid:...|corrects:...|to:...|to_res:...|read:...]
|
||||
GString* meta = g_string_new("[");
|
||||
g_string_append(meta, type ? type : "chat");
|
||||
g_string_append_c(meta, '|');
|
||||
@@ -423,6 +425,17 @@ ff_write_line(FILE* fp, const char* timestamp, const char* type, const char* enc
|
||||
if (safe_rid) {
|
||||
g_string_append_printf(meta, "|corrects:%s", safe_rid);
|
||||
}
|
||||
if (to_jid && strlen(to_jid) > 0) {
|
||||
auto_gchar gchar* safe_to = ff_escape_meta_value(to_jid);
|
||||
g_string_append_printf(meta, "|to:%s", safe_to);
|
||||
}
|
||||
if (to_resource && strlen(to_resource) > 0) {
|
||||
auto_gchar gchar* safe_tores = ff_escape_meta_value(to_resource);
|
||||
g_string_append_printf(meta, "|to_res:%s", safe_tores);
|
||||
}
|
||||
if (marked_read >= 0) {
|
||||
g_string_append_printf(meta, "|read:%d", marked_read ? 1 : 0);
|
||||
}
|
||||
g_string_append_c(meta, ']');
|
||||
|
||||
// Build sender — escape ": " in the resource part to prevent
|
||||
@@ -573,6 +586,8 @@ ff_parsed_line_free(ff_parsed_line_t* pl)
|
||||
g_free(pl->replace_id);
|
||||
g_free(pl->from_jid);
|
||||
g_free(pl->from_resource);
|
||||
g_free(pl->to_jid);
|
||||
g_free(pl->to_resource);
|
||||
g_free(pl->message);
|
||||
g_free(pl);
|
||||
}
|
||||
@@ -619,6 +634,7 @@ ff_parse_line(const char* line)
|
||||
|
||||
ff_parsed_line_t* result = g_malloc0(sizeof(ff_parsed_line_t));
|
||||
result->file_offset = -1;
|
||||
result->marked_read = -1; // unset by default
|
||||
|
||||
// Parse timestamp — everything up to first space followed by '['
|
||||
char* bracket_start = strchr(work, '[');
|
||||
@@ -648,6 +664,12 @@ ff_parse_line(const char* line)
|
||||
result->archive_id = ff_unescape_meta_value(parts[i] + 4);
|
||||
} else if (g_str_has_prefix(parts[i], "corrects:")) {
|
||||
result->replace_id = ff_unescape_meta_value(parts[i] + 9);
|
||||
} else if (g_str_has_prefix(parts[i], "to:")) {
|
||||
result->to_jid = ff_unescape_meta_value(parts[i] + 3);
|
||||
} else if (g_str_has_prefix(parts[i], "to_res:")) {
|
||||
result->to_resource = ff_unescape_meta_value(parts[i] + 7);
|
||||
} else if (g_str_has_prefix(parts[i], "read:")) {
|
||||
result->marked_read = atoi(parts[i] + 5) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
g_strfreev(parts);
|
||||
@@ -747,6 +769,9 @@ ff_parsed_to_profmessage(ff_parsed_line_t* pl)
|
||||
ProfMessage* msg = message_init();
|
||||
msg->id = pl->stanza_id ? g_strdup(pl->stanza_id) : NULL;
|
||||
msg->from_jid = jid_create_from_bare_and_resource(pl->from_jid, pl->from_resource);
|
||||
if (pl->to_jid) {
|
||||
msg->to_jid = jid_create_from_bare_and_resource(pl->to_jid, pl->to_resource);
|
||||
}
|
||||
msg->plain = g_strdup(pl->message ? pl->message : "");
|
||||
msg->timestamp = g_date_time_ref(pl->timestamp);
|
||||
msg->type = ff_get_message_type_type(pl->type);
|
||||
|
||||
@@ -818,6 +818,111 @@ db_backend_sqlite(void)
|
||||
return &sqlite_backend;
|
||||
}
|
||||
|
||||
void
|
||||
db_sqlite_begin_transaction(void)
|
||||
{
|
||||
if (!g_chatlog_database)
|
||||
return;
|
||||
char* err_msg = NULL;
|
||||
if (SQLITE_OK != sqlite3_exec(g_chatlog_database, "BEGIN TRANSACTION", NULL, 0, &err_msg)) {
|
||||
if (err_msg) {
|
||||
log_error("SQLite BEGIN TRANSACTION failed: %s", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
db_sqlite_end_transaction(void)
|
||||
{
|
||||
if (!g_chatlog_database)
|
||||
return;
|
||||
char* err_msg = NULL;
|
||||
if (SQLITE_OK != sqlite3_exec(g_chatlog_database, "END TRANSACTION", NULL, 0, &err_msg)) {
|
||||
if (err_msg) {
|
||||
log_error("SQLite END TRANSACTION failed: %s", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
db_sqlite_rollback_transaction(void)
|
||||
{
|
||||
if (!g_chatlog_database)
|
||||
return;
|
||||
char* err_msg = NULL;
|
||||
if (SQLITE_OK != sqlite3_exec(g_chatlog_database, "ROLLBACK", NULL, 0, &err_msg)) {
|
||||
if (err_msg) {
|
||||
log_error("SQLite ROLLBACK failed: %s", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
GSList*
|
||||
db_sqlite_get_all_chat(const gchar* const contact_barejid)
|
||||
{
|
||||
GSList* result = NULL;
|
||||
|
||||
if (!g_chatlog_database)
|
||||
return NULL;
|
||||
|
||||
const Jid* myjid = connection_get_jid();
|
||||
if (!myjid || !myjid->barejid)
|
||||
return NULL;
|
||||
|
||||
sqlite3_stmt* stmt = NULL;
|
||||
auto_sqlite char* query = sqlite3_mprintf(
|
||||
"SELECT COALESCE(B.`message`, A.`message`) AS message, "
|
||||
"A.`timestamp`, A.`from_jid`, A.`from_resource`, A.`to_jid`, A.`to_resource`, "
|
||||
"A.`type`, A.`encryption`, A.`stanza_id`, A.`archive_id`, A.`marked_read` "
|
||||
"FROM `ChatLogs` AS A "
|
||||
"LEFT JOIN `ChatLogs` AS B ON (A.`replaced_by_db_id` = B.`id` AND A.`from_jid` = B.`from_jid`) "
|
||||
"WHERE (A.`replaces_db_id` IS NULL) "
|
||||
"AND ((A.`from_jid` = %Q AND A.`to_jid` = %Q) OR (A.`from_jid` = %Q AND A.`to_jid` = %Q)) "
|
||||
"ORDER BY A.`timestamp` ASC",
|
||||
contact_barejid, myjid->barejid, myjid->barejid, contact_barejid);
|
||||
|
||||
if (!query)
|
||||
return NULL;
|
||||
|
||||
if (!_db_prepare_ctx(query, &stmt, "db_sqlite_get_all_chat()"))
|
||||
return NULL;
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
char* message = (char*)sqlite3_column_text(stmt, 0);
|
||||
char* date = (char*)sqlite3_column_text(stmt, 1);
|
||||
char* from_jid = (char*)sqlite3_column_text(stmt, 2);
|
||||
char* from_resource = (char*)sqlite3_column_text(stmt, 3);
|
||||
char* to_jid = (char*)sqlite3_column_text(stmt, 4);
|
||||
char* to_resource = (char*)sqlite3_column_text(stmt, 5);
|
||||
char* type = (char*)sqlite3_column_text(stmt, 6);
|
||||
char* encryption = (char*)sqlite3_column_text(stmt, 7);
|
||||
char* id = (char*)sqlite3_column_text(stmt, 8);
|
||||
char* archive_id = (char*)sqlite3_column_text(stmt, 9);
|
||||
int marked_read_raw = sqlite3_column_type(stmt, 10) == SQLITE_NULL ? -1 : sqlite3_column_int(stmt, 10);
|
||||
|
||||
ProfMessage* msg = message_init();
|
||||
msg->id = _db_strdup(id);
|
||||
msg->stanzaid = _db_strdup(archive_id);
|
||||
msg->from_jid = jid_create_from_bare_and_resource(from_jid, from_resource);
|
||||
msg->to_jid = jid_create_from_bare_and_resource(to_jid, to_resource);
|
||||
msg->plain = _db_strdup(message);
|
||||
if (!msg->plain)
|
||||
msg->plain = strdup("");
|
||||
msg->timestamp = g_date_time_new_from_iso8601(date, NULL);
|
||||
msg->type = _get_message_type_type(type);
|
||||
msg->enc = _get_message_enc_type(encryption);
|
||||
msg->marked_read = marked_read_raw;
|
||||
|
||||
result = g_slist_prepend(result, msg);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
return g_slist_reverse(result);
|
||||
}
|
||||
|
||||
GSList*
|
||||
db_sqlite_list_contacts(void)
|
||||
{
|
||||
|
||||
@@ -358,6 +358,7 @@ message_init(void)
|
||||
message->enc = PROF_MSG_ENC_NONE;
|
||||
message->trusted = true;
|
||||
message->type = PROF_MSG_TYPE_UNINITIALIZED;
|
||||
message->marked_read = -1;
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
@@ -175,6 +175,7 @@ typedef struct prof_message_t
|
||||
gboolean trusted;
|
||||
gboolean is_mam;
|
||||
prof_msg_type_t type;
|
||||
int marked_read; // -1 = unset, 0 = unread, 1 = read (used by export/import)
|
||||
} ProfMessage;
|
||||
|
||||
void session_init(void);
|
||||
|
||||
Reference in New Issue
Block a user