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.

249 lines
5.5 KiB
C

#include "cli.h"
// Submodules
#include "util/format.h"
#include "json/json.h"
#include "ap/object.h"
// Model
#include "model/account.h"
#include "model/status.h"
#include "model/notification.h"
#include "model/gc.h"
// View
#include "view/api/Status.h"
#include "view/api/Notification.h"
// Controller
#include "controller/inbox.h"
// Standard Library
#include <stdio.h>
#include <stdlib.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 ) ) {
printf( "No match for %s==%s\n", 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] );
if( !to_follow ) {
printf( "Could not find account %s\n", req->argv[0] );
return true;
}
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);
}
return true;
}
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;
}
static bool handle_command_reindex( struct cli_request* req )
{
int res = cli_route_command( req, "reindex", 0, "" );
if( res != 1 ) { return !!res; }
account_reindex();
return true;
}
static bool handle_command_test( struct cli_request* req )
{
int res = cli_route_command( req, "test", 0, "" );
if( res != 1 ) { return !!res; }
struct account* owner_account = account_from_id( owner_account_id );
struct ap_object* obj = account_ap_actor( owner_account );
ap_object_write_to_FILE( obj, stdout );
printf( "\n" );
ap_object_free(obj);
account_free(owner_account);
return true;
}
static bool handle_command_gc( struct cli_request* req )
{
int res = cli_route_command( req, "gc", 0, "" );
if( res != 1 ) { return !!res; }
gc_run();
return true;
}
bool handle_command_account_sync( struct cli_request* req, int account_id )
{
int res = cli_route_command( req, "sync", 0, "" );
if( res != 1 ) { return !!res; }
printf( "Syncing account %d\n", account_id );
account_sync_from_activity_pub( account_id );
return true;
}
bool handle_command_update( struct cli_request* req )
{
int res = cli_route_command( req, "update", 0, "update" );
if( res != 1 ) { return !!res; }
struct account* owner = account_from_id( owner_account_id );
account_update(owner);
account_free(owner);
return true;
}
bool handle_command_account( struct cli_request* req )
{
int res = cli_route_command( req, "account", 1, "account (id)" );
if( res != 1 ) { return !!res; }
bool result = false;
int account_id = atoi(req->argv[0]);
req->argv += 1;
req->argc -= 1;
if( handle_command_account_sync(req,account_id) ) { goto done; }
result = false;
cleanup:
return result;
done:
result = true;
goto cleanup;
}
bool handle_command( char** argv, int argc )
{
struct cli_request req = {
.argv = &argv[1],
.argc = argc - 1,
.binary_name = argv[0]
};
if( false
|| handle_command_follow(&req)
|| handle_command_unfollow(&req)
|| handle_command_activity(&req)
|| handle_command_status(&req)
|| handle_command_account(&req)
|| handle_command_reindex(&req)
|| handle_command_test(&req)
|| handle_command_update(&req)
|| handle_command_gc(&req)
) { return true; }
return false;
}