cli: Fix create_invite's expire option.

This commit is contained in:
Cory McWilliams 2025-01-30 22:16:06 -05:00
parent d4a5cc6eee
commit 916aa5abbd
2 changed files with 14 additions and 7 deletions

View File

@ -808,8 +808,8 @@ static int _tf_command_create_invite(const char* file, int argc, char* argv[])
const char* default_db_path = _get_db_path();
const char* db_path = default_db_path;
const char* identity = NULL;
int use_count = 0;
int expires = 0;
int use_count = 1;
int expires = 3600;
bool show_usage = false;
const char* host = NULL;
int port = 0;
@ -868,8 +868,8 @@ static int _tf_command_create_invite(const char* file, int argc, char* argv[])
tf_printf(" -i, --identity identity Account from which to get latest sequence number.\n");
tf_printf(" -a, --address address Address to which the recipient will connect.\n");
tf_printf(" -p, --port port Port to which the recipient will connect.\n");
tf_printf(" -u, --use_count count Number of times this invite may be used.\n");
tf_printf(" -e, --expires seconds How long this invite is valid in seconds (-1 for indefinitely).\n");
tf_printf(" -u, --use_count count Number of times this invite may be used (default: 1).\n");
tf_printf(" -e, --expires seconds How long this invite is valid in seconds (-1 for indefinitely, default: 1 hour).\n");
tf_printf(" -h, --help Show this usage information.\n");
tf_free((void*)default_db_path);
return EXIT_FAILURE;

View File

@ -2182,17 +2182,23 @@ const char* tf_ssb_db_get_profile_name(sqlite3* db, const char* id)
static void _tf_ssb_db_invite_cleanup(sqlite3* db)
{
sqlite3_stmt* statement;
if (sqlite3_prepare(db, "DELETE FROM invites WHERE use_count = 0 OR expires < ?", -1, &statement, NULL) == SQLITE_OK)
if (sqlite3_prepare(db, "DELETE FROM invites WHERE use_count = 0 OR (expires > 0 AND expires < ?)", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_int64(statement, 1, (int64_t)time(NULL)) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_DONE)
{
if (sqlite3_changes(db))
{
char buffer[2] = { 0 };
size_t buffer_size = sizeof(buffer);
bool verbose = uv_os_getenv("TF_SSB_VERBOSE", buffer, &buffer_size) == 0 && strcmp(buffer, "1") == 0;
if (verbose)
{
tf_printf("Cleaned up %d used/expired invites.\n", sqlite3_changes(db));
}
}
}
else
{
tf_printf("Invite cleanup failed: %s\n", sqlite3_errmsg(db));
@ -2231,7 +2237,8 @@ bool tf_ssb_db_generate_invite(sqlite3* db, const char* id, const char* host, in
if (sqlite3_prepare(db, "INSERT INTO invites (invite_public_key, account, use_count, expires) VALUES (?, ?, ?, ?)", -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_bind_text(statement, 1, public, -1, NULL) == SQLITE_OK && sqlite3_bind_text(statement, 2, id, -1, NULL) == SQLITE_OK &&
sqlite3_bind_int(statement, 3, use_count) == SQLITE_OK && sqlite3_bind_int64(statement, 4, (int64_t)time(NULL) + expires_seconds) == SQLITE_OK)
sqlite3_bind_int(statement, 3, use_count) == SQLITE_OK &&
sqlite3_bind_int64(statement, 4, (expires_seconds > 0 ? (int64_t)time(NULL) : 0) + expires_seconds) == SQLITE_OK)
{
inserted = sqlite3_step(statement) == SQLITE_DONE;
}