forked from cory/tildefriends
ssb: Support publishing private messages from the command-line. #89
This commit is contained in:
135
src/main.c
135
src/main.c
@ -144,6 +144,7 @@ static void _create_directories_for_file(const char* path, int mode)
|
||||
static int _tf_command_export(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_import(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_publish(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_private(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_run(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_sandbox(const char* file, int argc, char* argv[]);
|
||||
static int _tf_command_has_blob(const char* file, int argc, char* argv[]);
|
||||
@ -168,6 +169,7 @@ const command_t k_commands[] = {
|
||||
{ "import", _tf_command_import, "Import apps to SSB." },
|
||||
{ "export", _tf_command_export, "Export apps from SSB." },
|
||||
{ "publish", _tf_command_publish, "Append a message to a feed." },
|
||||
{ "private", _tf_command_private, "Append a private post message to a feed." },
|
||||
{ "get_sequence", _tf_command_get_sequence, "Get the last sequence number for a feed." },
|
||||
{ "get_identity", _tf_command_get_identity, "Get the server account identity." },
|
||||
{ "get_profile", _tf_command_get_profile, "Get profile information for the given identity." },
|
||||
@ -505,6 +507,139 @@ static int _tf_command_publish(const char* file, int argc, char* argv[])
|
||||
return result;
|
||||
}
|
||||
|
||||
static int _tf_command_private(const char* file, int argc, char* argv[])
|
||||
{
|
||||
const char* user = NULL;
|
||||
const char* identity = NULL;
|
||||
const char* default_db_path = _get_db_path();
|
||||
const char* db_path = default_db_path;
|
||||
const char* text = NULL;
|
||||
const char* recipients = NULL;
|
||||
bool show_usage = false;
|
||||
|
||||
while (!show_usage)
|
||||
{
|
||||
static const struct option k_options[] = {
|
||||
{ "user", required_argument, NULL, 'u' },
|
||||
{ "id", required_argument, NULL, 'i' },
|
||||
{ "recipients", required_argument, NULL, 'r' },
|
||||
{ "db-path", required_argument, NULL, 'd' },
|
||||
{ "text", required_argument, NULL, 'c' },
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ 0 },
|
||||
};
|
||||
int c = getopt_long(argc, argv, "u:i:d:t:r:h", k_options, NULL);
|
||||
if (c == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case '?':
|
||||
case 'h':
|
||||
default:
|
||||
show_usage = true;
|
||||
break;
|
||||
case 'u':
|
||||
user = optarg;
|
||||
break;
|
||||
case 'i':
|
||||
identity = optarg;
|
||||
break;
|
||||
case 'd':
|
||||
db_path = optarg;
|
||||
break;
|
||||
case 't':
|
||||
text = optarg;
|
||||
break;
|
||||
case 'r':
|
||||
recipients = optarg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (show_usage || !user || !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(" -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);
|
||||
tf_printf(" -t, --text text Private post text.\n");
|
||||
tf_printf(" -h, --help Show this usage information.\n");
|
||||
tf_free((void*)default_db_path);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int result = EXIT_FAILURE;
|
||||
tf_printf("Posting %s as account %s belonging to %s...\n", text, identity, user);
|
||||
_create_directories_for_file(db_path, 0700);
|
||||
tf_ssb_t* ssb = tf_ssb_create(NULL, NULL, db_path, NULL);
|
||||
uint8_t private_key[512] = { 0 };
|
||||
const char* recipient_list[k_max_private_message_recipients] = { 0 };
|
||||
int recipient_count = 0;
|
||||
|
||||
recipient_list[recipient_count++] = identity;
|
||||
|
||||
if (tf_ssb_db_identity_get_private_key(ssb, user, identity, private_key, sizeof(private_key)))
|
||||
{
|
||||
char* copy = tf_strdup(recipients);
|
||||
char* next = NULL;
|
||||
const char* it = strtok_r(copy, ",", &next);
|
||||
while (it)
|
||||
{
|
||||
if (recipient_count == k_max_private_message_recipients)
|
||||
{
|
||||
tf_printf("Too many recipients (max %d).\n", k_max_private_message_recipients);
|
||||
goto done;
|
||||
}
|
||||
recipient_list[recipient_count++] = it;
|
||||
it = strtok_r(NULL, ",", &next);
|
||||
}
|
||||
|
||||
JSContext* context = tf_ssb_get_context(ssb);
|
||||
JSValue message = JS_NewObject(context);
|
||||
JS_SetPropertyStr(context, message, "type", JS_NewString(context, "post"));
|
||||
JS_SetPropertyStr(context, message, "text", JS_NewString(context, text));
|
||||
JSValue recps = JS_NewArray(context);
|
||||
for (int i = 0; i < recipient_count; i++)
|
||||
{
|
||||
JS_SetPropertyUint32(context, recps, i, JS_NewString(context, recipient_list[i]));
|
||||
}
|
||||
JS_SetPropertyStr(context, message, "recps", recps);
|
||||
JSValue json = JS_JSONStringify(context, message, JS_NULL, JS_NULL);
|
||||
const char* message_str = JS_ToCString(context, json);
|
||||
char* encrypted = tf_ssb_private_message_encrypt(private_key, recipient_list, recipient_count, message_str, strlen(message_str));
|
||||
if (encrypted)
|
||||
{
|
||||
int64_t sequence = 0;
|
||||
char previous[k_id_base64_len] = { 0 };
|
||||
tf_ssb_db_get_latest_message_by_author(ssb, identity, &sequence, previous, sizeof(previous));
|
||||
|
||||
JSValue content = JS_NewString(context, encrypted);
|
||||
JSValue to_publish = tf_ssb_sign_message(ssb, identity, private_key, content, previous, sequence);
|
||||
tf_ssb_verify_strip_and_store_message(ssb, to_publish, _tf_published_callback, &result);
|
||||
JS_FreeValue(context, to_publish);
|
||||
JS_FreeValue(context, content);
|
||||
}
|
||||
tf_free(encrypted);
|
||||
JS_FreeCString(context, message_str);
|
||||
JS_FreeValue(context, json);
|
||||
JS_FreeValue(context, message);
|
||||
tf_free(copy);
|
||||
}
|
||||
else
|
||||
{
|
||||
tf_printf("Did not find private key for identity %s belonging to %s.\n", identity, user);
|
||||
}
|
||||
done:
|
||||
tf_ssb_destroy(ssb);
|
||||
tf_free((void*)default_db_path);
|
||||
return result;
|
||||
}
|
||||
|
||||
static int _tf_command_store_blob(const char* file, int argc, char* argv[])
|
||||
{
|
||||
const char* default_db_path = _get_db_path();
|
||||
|
Reference in New Issue
Block a user