Add missing statics, and remove the 'tildefriends check' command.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4838 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
2024-02-10 16:50:00 +00:00
parent 51a327c52d
commit 6c5a7b0751
17 changed files with 93 additions and 304 deletions

View File

@ -1033,132 +1033,6 @@ JSValue tf_ssb_format_message(JSContext* context, const char* previous, const ch
return value;
}
bool _tf_ssb_update_message_id(sqlite3* db, const char* old_id, const char* new_id)
{
bool success = false;
sqlite3_stmt* statement = NULL;
if (sqlite3_prepare(db, "UPDATE messages SET id = ? WHERE id = ?", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, new_id, -1, NULL) == SQLITE_OK &&
sqlite3_bind_text(statement, 2, old_id, -1, NULL) == SQLITE_OK)
{
success = sqlite3_step(statement) == SQLITE_DONE;
}
sqlite3_finalize(statement);
}
return success;
}
bool tf_ssb_db_check(sqlite3* db, const char* check_author)
{
JSMallocFunctions funcs = { 0 };
tf_get_js_malloc_functions(&funcs);
JSRuntime* runtime = JS_NewRuntime2(&funcs, NULL);
JSContext* context = JS_NewContext(runtime);
sqlite3_stmt* statement = NULL;
int result = check_author ?
sqlite3_prepare(db, "SELECT id, previous, author, sequence, timestamp, hash, content, signature, sequence_before_author FROM messages WHERE author = ? ORDER BY author, sequence", -1, &statement, NULL) :
sqlite3_prepare(db, "SELECT id, previous, author, sequence, timestamp, hash, content, signature, sequence_before_author FROM messages ORDER BY author, sequence", -1, &statement, NULL);
if (result == SQLITE_OK)
{
if (check_author)
{
sqlite3_bind_text(statement, 1, check_author, -1, NULL);
}
char previous_id[k_id_base64_len];
int64_t previous_sequence = -1;
char previous_author[k_id_base64_len] = { 0 };
while (sqlite3_step(statement) == SQLITE_ROW)
{
const char* id = (const char*)sqlite3_column_text(statement, 0);
const char* previous = (const char*)sqlite3_column_text(statement, 1);
const char* author = (const char*)sqlite3_column_text(statement, 2);
int64_t sequence = sqlite3_column_int64(statement, 3);
double timestamp = sqlite3_column_double(statement, 4);
const char* hash = (const char*)sqlite3_column_text(statement, 5);
const char* content = (const char*)sqlite3_column_text(statement, 6);
const char* signature = (const char*)sqlite3_column_text(statement, 7);
bool sequence_before_author = sqlite3_column_int(statement, 8);
JSValue message = tf_ssb_format_message(context, previous, author, sequence, timestamp, hash, content, signature, sequence_before_author);
char out_signature[512];
char actual_id[k_id_base64_len];
bool actual_sequence_before_author = false;
JSValue j = JS_JSONStringify(context, message, JS_NULL, JS_NewInt32(context, 2));
const char* jv = JS_ToCString(context, j);
bool delete_following = false;
if (strcmp(author, previous_author))
{
tf_printf("%s\n", author);
}
if (strcmp(author, previous_author) == 0 && sequence != previous_sequence + 1)
{
tf_printf("Detected gap in messages for %s at sequence = %" PRId64 " => %" PRId64 ".\n", author, previous_sequence, sequence);
delete_following = true;
}
else
{
if (tf_ssb_verify_and_strip_signature(context, message, actual_id, sizeof(actual_id), out_signature, sizeof(out_signature), &actual_sequence_before_author))
{
if (previous && strcmp(previous, previous_id))
{
tf_printf("%s:%d previous was %s should be %s\n", id, (int)sequence, previous_id, previous);
}
if (strcmp(id, actual_id))
{
if (_tf_ssb_update_message_id(db, id, actual_id))
{
tf_printf("updated %s to %s\n", id, actual_id);
}
else
{
tf_printf("failed to update %s to %s\n", id, actual_id);
}
}
}
else
{
tf_printf("%s sequence=%" PRId64 " unable to verify signature for %s sequence_before_author=%d message=[%.*s]\n", author, sequence, id, sequence_before_author, (int)strlen(jv), jv);
delete_following = true;
}
}
if (delete_following)
{
tf_printf("Deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
sqlite3_stmt* delete_statement = NULL;
if (sqlite3_prepare(db, "DELETE FROM messages WHERE author = ? AND sequence >= ?", -1, &delete_statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(delete_statement, 1, author, -1, NULL) == SQLITE_OK &&
sqlite3_bind_int64(delete_statement, 2, sequence) == SQLITE_OK)
{
if (sqlite3_step(delete_statement) != SQLITE_DONE)
{
tf_printf("Error deleting author = %s sequence >= %" PRId64 ".\n", author, sequence);
}
}
sqlite3_finalize(delete_statement);
}
}
snprintf(previous_author, sizeof(previous_author), "%s", author);
previous_sequence = sequence;
JS_FreeCString(context, jv);
JS_FreeValue(context, j);
snprintf(previous_id, sizeof(previous_id), "%s", id);
JS_FreeValue(context, message);
}
sqlite3_finalize(statement);
}
JS_FreeContext(context);
JS_FreeRuntime(runtime);
return false;
}
int tf_ssb_db_identity_get_count_for_user(tf_ssb_t* ssb, const char* user)
{
int count = 0;