You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
3.2 KiB
C

#include "cli.h"
#include "model/account.h"
#include "model/status.h"
#include "model/ap/activity.h"
#include "controller/inbox.h"
#include "format.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
struct cli_request
{
char** argv;
int argc;
char* binary_name;
};
static int cli_route_command( struct cli_request* req, const char* cmd, int count, const char* usage_args )
{
if( 0 != strcmp(req->argv[0], cmd ) ) {
return 0;
}
req->argv += 1;
req->argc -= 1;
if( req->argc < count ) {
printf( "Usage: %s %s %s", req->binary_name, cmd, usage_args );
return -1;
}
return 1;
}
static bool handle_command_follow( struct cli_request* req )
{
int res = cli_route_command( req, "follow", 1, "[account webfinger]" );
if( res != 1 ) { return !!res; }
struct account* owner = account_from_id( owner_account_id );
struct account* to_follow = account_from_webfinger( req->argv[0] );
account_follow( owner, to_follow );
account_free(to_follow);
account_free(owner);
return true;
}
static bool handle_command_unfollow( struct cli_request* req )
{
int res = cli_route_command( req, "unfollow", 1, "[account webfinger]" );
if( res != 1 ) { return !!res; }
struct account* owner = account_from_id( owner_account_id );
struct account* to_unfollow = account_from_webfinger( req->argv[0] );
account_unfollow( owner, to_unfollow );
account_free(to_unfollow);
account_free(owner);
return true;
}
static bool handle_command_activity_import( struct cli_request* req )
{
int res = cli_route_command( req, "import", 1, "[activity uri]" );
if( res != 1 ) { return !!res; }
printf( "TODO: import activity from %s\n", req->argv[0] );
return false;
}
static bool handle_command_activity( struct cli_request* req )
{
int res = cli_route_command( req, "activity", 1, "(import) ..." );
if( res != 1 ) { return !!res; }
return false
|| handle_command_activity_import( req )
;
}
static bool handle_command_status_import( struct cli_request* req )
{
int res = cli_route_command( req, "import", 1, "[status uri]" );
if( res != 1 ) { return !!res; }
struct status* s = status_fetch_from_uri( req->argv[0] );
if( s ) {
printf( "Status id=%d\n", s->id );
status_free(s);
}
}
static bool handle_command_status_sync( struct cli_request* req )
{
int res = cli_route_command( req, "sync", 1, "[status id]" );
if( res != 1 ) { return !!res; }
int id = 0;
if( 1 != sscanf( req->argv[0], "%d", &id ) ) {
return true;
}
struct status* s = status_from_id(id);
if( !s ) {
printf( "! Status %d not found\n", id );
return true;
}
status_sync(s);
status_save(s);
status_free(s);
return true;
}
static bool handle_command_status( struct cli_request* req )
{
int res = cli_route_command( req, "status", 1, "(import|sync) ..." );
if( res != 1 ) { return !!res; }
if( handle_command_status_import( req ) ) { return true; }
if( handle_command_status_sync( req ) ) { return true; }
printf( "Usage: %s status (import|sync)", req->binary_name );
return true;
}
void handle_command( char** argv, int argc )
{
struct cli_request req = {
.argv = &argv[1],
.argc = argc - 1,
.binary_name = argv[0]
};
false
|| handle_command_follow(&req)
|| handle_command_unfollow(&req)
|| handle_command_activity(&req)
|| handle_command_status(&req)
;
}