From 3bf19fabda14957f5ba53a08e1fcc188ad50ca09 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Mon, 5 May 2025 21:51:38 -0400 Subject: [PATCH] ssb: Don't require -u for the private command. #119 --- src/main.c | 16 ++++++++++++++-- src/ssb.db.c | 28 ++++++++++++++++++++++++++++ src/ssb.db.h | 8 ++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index a8a5ba43..9488d0a4 100644 --- a/src/main.c +++ b/src/main.c @@ -566,11 +566,11 @@ static int _tf_command_private(const char* file, int argc, char* argv[]) } } - if (show_usage || !user || !identity || !recipients || !text) + if (show_usage || !identity || !recipients || !text) { tf_printf("\n%s private [options]\n\n", file); tf_printf("options:\n"); - tf_printf(" -u, --user user User owning identity with which to publish.\n"); + tf_printf(" -u, --user user User owning identity with which to publish (optional).\n"); tf_printf(" -i, --id identity Identity with which to publish message.\n"); tf_printf(" -r, --recipients recipients Recipient identities.\n"); tf_printf(" -d, --db-path db_path SQLite database path (default: %s).\n", default_db_path); @@ -591,6 +591,13 @@ static int _tf_command_private(const char* file, int argc, char* argv[]) recipient_list[recipient_count++] = identity; + bool free_user = false; + if (!user) + { + user = tf_ssb_db_get_user_for_identity(ssb, identity); + free_user = true; + } + if (tf_ssb_db_identity_get_private_key(ssb, user, identity, private_key, sizeof(private_key))) { char* copy = tf_strdup(recipients); @@ -642,6 +649,11 @@ static int _tf_command_private(const char* file, int argc, char* argv[]) { tf_printf("Did not find private key for identity %s belonging to %s.\n", identity, user); } + + if (free_user) + { + tf_free((void*)user); + } done: tf_ssb_destroy(ssb); tf_free((void*)default_db_path); diff --git a/src/ssb.db.c b/src/ssb.db.c index aac8c906..550a4049 100644 --- a/src/ssb.db.c +++ b/src/ssb.db.c @@ -1398,6 +1398,34 @@ void tf_ssb_db_identity_visit_all(tf_ssb_t* ssb, void (*callback)(const char* id tf_ssb_release_db_reader(ssb, db); } +const char* tf_ssb_db_get_user_for_identity(tf_ssb_t* ssb, const char* public_key) +{ + const char* result = NULL; + sqlite3* db = tf_ssb_acquire_db_reader(ssb); + sqlite3_stmt* statement = NULL; + if (sqlite3_prepare(db, "SELECT user FROM identities WHERE public_key = ?", -1, &statement, NULL) == SQLITE_OK) + { + if (sqlite3_bind_text(statement, 1, (public_key && *public_key == '@') ? public_key + 1 : public_key, -1, NULL) == SQLITE_OK) + { + if (sqlite3_step(statement) == SQLITE_ROW) + { + result = tf_strdup((const char*)sqlite3_column_text(statement, 0)); + } + } + else + { + tf_printf("Bind failed: %s.\n", sqlite3_errmsg(db)); + } + sqlite3_finalize(statement); + } + else + { + tf_printf("Prepare failed: %s.\n", sqlite3_errmsg(db)); + } + tf_ssb_release_db_reader(ssb, db); + return result; +} + bool tf_ssb_db_identity_get_private_key(tf_ssb_t* ssb, const char* user, const char* public_key, uint8_t* out_private_key, size_t private_key_size) { bool success = false; diff --git a/src/ssb.db.h b/src/ssb.db.h index e78842a9..1b10b60e 100644 --- a/src/ssb.db.h +++ b/src/ssb.db.h @@ -249,6 +249,14 @@ void tf_ssb_db_identity_visit(tf_ssb_t* ssb, const char* user, void (*callback)( */ void tf_ssb_db_identity_visit_all(tf_ssb_t* ssb, void (*callback)(const char* identity, void* user_data), void* user_data); +/** +** Get the user owning an identity. +** @param ssb The SSB instance. +** @param public_key the identity. +** @return The username of the owner of the identity or NULL. +*/ +const char* tf_ssb_db_get_user_for_identity(tf_ssb_t* ssb, const char* public_key); + /** ** Get the private key for an identity in the database. ** @param ssb The SSB instance.