main main.c
   1/* snac - A simple, minimalistic ActivityPub instance */
   2/* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
   3
   4#include "xs.h"
   5#include "xs_io.h"
   6#include "xs_json.h"
   7#include "xs_time.h"
   8#include "xs_bearssl.h"
   9#include "xs_match.h"
  10#include "xs_random.h"
  11#include "xs_http.h"
  12
  13#include "snac.h"
  14
  15#include <sys/stat.h>
  16#include <sys/wait.h>
  17
  18int usage(const char *cmd)
  19{
  20    printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  21    printf("Copyright (c) 2022 - 2026 grunfink et al. / MIT license\n");
  22    printf("\n");
  23
  24    if (cmd == NULL) {
  25        printf("Commands:\n");
  26        printf("\n");
  27    }
  28
  29    const char *cmds =
  30        "init [{basedir}]                     Initializes the data storage\n"
  31        "upgrade {basedir}                    Upgrade to a new version\n"
  32        "adduser {basedir} [{uid}]            Adds a new user\n"
  33        "deluser {basedir} {uid}              Deletes a user\n"
  34        "update {basedir} {uid}               Sends a user's updated profile\n"
  35        "httpd {basedir}                      Starts the HTTPD daemon\n"
  36        "purge {basedir}                      Purges old data\n"
  37        "state {basedir}                      Prints server state\n"
  38        "webfinger {basedir} {account}        Queries about an account (@user@host or actor url)\n"
  39        "queue {basedir} {uid}                Processes a user queue\n"
  40        "follow {basedir} {uid} {actor}       Follows an actor\n"
  41        "unfollow {basedir} {uid} {actor}     Unfollows an actor\n"
  42        "request {basedir} {uid} {url}        Requests an object\n"
  43        "insert {basedir} {uid} {url}         Requests an object and inserts it into the timeline\n"
  44        "collect_replies {basedir} {uid} {url} Collects all replies from a post\n"
  45        "actor {basedir} [{uid}] {url}        Requests an actor\n"
  46        "note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n"
  47        "note_unlisted {basedir} {uid} {text} [files...] Sends an unlisted note with optional attachments\n"
  48        "note_mention {basedir} {uid} {text} [files...] Sends a note only to mentioned accounts\n"
  49        "note_followers {basedir} {uid} {text} [files...] Sends a note only to followers\n"
  50        "boost|announce {basedir} {uid} {url} Boosts (announces) a post\n"
  51        "unboost {basedir} {uid} {url}        Unboosts a post\n"
  52        "resetpwd {basedir} {uid}             Resets the password of a user\n"
  53        "ping {basedir} {uid} {actor}         Pings an actor\n"
  54        "webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n"
  55        "pin {basedir} {uid} {msg_url}        Pins a message\n"
  56        "unpin {basedir} {uid} {msg_url}      Unpins a message\n"
  57        "bookmark {basedir} {uid} {msg_url}   Bookmarks a message\n"
  58        "unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n"
  59        "block {basedir} {instance_url}       Blocks a full instance\n"
  60        "unblock {basedir} {instance_url}     Unblocks a full instance\n"
  61        "limit {basedir} {uid} {actor}        Limits an actor (drops their announces)\n"
  62        "unlimit {basedir} {uid} {actor}      Unlimits an actor\n"
  63        "cool {basedir} {uid} {actor}         Mark an actor as cool (notify whenever they post)\n"
  64        "uncool {basedir} {uid} {actor}       Actor is no longer cool\n"
  65        "muted {basedir} {uid}                Lists the muted actors\n"
  66        "unmute {basedir} {uid} {actor}       Unmutes a previously muted actor\n"
  67        "verify_links {basedir} {uid}         Verifies a user's links (in the metadata)\n"
  68        "search {basedir} {uid} {regex}       Searches posts by content\n"
  69        "export_csv {basedir} {uid}           Exports followers, lists, MUTEd and bookmarks to CSV\n"
  70        "export_posts {basedir} {uid}         Exports all posts to outbox.json\n"
  71        "alias {basedir} {uid} {account}      Sets account (@user@host or actor url) as an alias\n"
  72        "migrate {basedir} {uid}              Migrates to the account defined as the alias\n"
  73        "import_csv {basedir} {uid}           Imports data from CSV files\n"
  74        "import_list {basedir} {uid} {file}   Imports a Mastodon CSV list file\n"
  75        "import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n"
  76        "lists {basedir} {uid}                Returns the names of the lists created by the user\n"
  77        "list_members {basedir} {uid} {name}  Returns the list of accounts inside a list\n"
  78        "list_create {basedir} {uid} {name}   Creates a new list\n"
  79        "list_remove {basedir} {uid} {name}   Removes an existing list\n"
  80        "list_add {basedir} {uid} {name} {acct} Adds an account (@user@host or actor url) to a list\n"
  81        "list_del {basedir} {uid} {name} {actor} Deletes an actor url from a list\n"
  82        "top_ten {basedir} {uid} [{N}]        Prints the most popular posts\n"
  83        "refresh {basedir} {uid}              Refreshes all actors\n";
  84
  85    if (cmd == NULL)
  86        printf("%s", cmds);
  87    else {
  88        /* only show help for the entered command */
  89        xs *l = xs_split(cmds, "\n");
  90        const char *v;
  91        int cnt = 0;
  92
  93        xs_list_foreach(l, v) {
  94            if (xs_str_in(v, cmd) != -1) {
  95                printf("%s\n", v);
  96                cnt++;
  97            }
  98        }
  99
 100        if (cnt == 0)
 101            printf("%s", cmds);
 102    }
 103
 104    return 1;
 105}
 106
 107
 108char *get_argv(int *argi, int argc, char *argv[])
 109{
 110    if (*argi < argc)
 111        return argv[(*argi)++];
 112    else
 113        return NULL;
 114}
 115
 116
 117#define GET_ARGV() get_argv(&argi, argc, argv)
 118
 119int main(int argc, char *argv[])
 120{
 121    char *cmd;
 122    char *basedir;
 123    char *user;
 124    char *url;
 125    int argi = 1;
 126    snac snac;
 127
 128    /* ensure group has write access */
 129    umask(0007);
 130
 131    if ((cmd = GET_ARGV()) == NULL)
 132        return usage(cmd);
 133
 134    if (strcmp(cmd, "init") == 0) { /** **/
 135        /* initialize the data storage */
 136        /* ... */
 137        basedir = GET_ARGV();
 138
 139        return snac_init(basedir);
 140    }
 141
 142    if ((basedir = getenv("SNAC_BASEDIR")) == NULL) {
 143        if ((basedir = GET_ARGV()) == NULL)
 144            return usage(cmd);
 145    }
 146
 147    if (strcmp(cmd, "upgrade") == 0) { /** **/
 148        int ret;
 149
 150        /* upgrade */
 151        if ((ret = srv_open(basedir, 1)) == 1)
 152            srv_log(xs_dup("OK"));
 153
 154        return ret;
 155    }
 156
 157    if (!srv_open(basedir, 0)) {
 158        srv_log(xs_fmt("error opening data storage at %s", basedir));
 159        return 1;
 160    }
 161
 162    if (strcmp(cmd, "adduser") == 0) { /** **/
 163        user = GET_ARGV();
 164
 165        return adduser(user);
 166
 167        return 0;
 168    }
 169
 170    if (strcmp(cmd, "httpd") == 0) { /** **/
 171        httpd();
 172        srv_free();
 173        return 0;
 174    }
 175
 176    if (strcmp(cmd, "purge") == 0) { /** **/
 177        purge_all();
 178        return 0;
 179    }
 180
 181    if (strcmp(cmd, "poll_hashtag_rss") == 0) { /** **/
 182        rss_poll_hashtags();
 183        return 0;
 184    }
 185
 186    if (strcmp(cmd, "state") == 0) { /** **/
 187        xs *shm_name = NULL;
 188        srv_state *p_state = srv_state_op(&shm_name, 1);
 189
 190        if (p_state == NULL)
 191            return 1;
 192
 193        srv_state ss = *p_state;
 194        int n;
 195
 196        printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
 197        xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
 198        printf("uptime: %s\n", uptime);
 199        printf("job fifo size (cur): %d\n", ss.job_fifo_size);
 200        printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
 201        char *th_states[] = { "stopped", "waiting", "input", "output" };
 202
 203        for (n = 0; n < ss.n_threads; n++)
 204            printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
 205
 206        return 0;
 207    }
 208
 209    if ((user = GET_ARGV()) == NULL)
 210        return usage(cmd);
 211
 212    if (strcmp(cmd, "block") == 0) { /** **/
 213        int ret = instance_block(user);
 214
 215        if (ret < 0) {
 216            fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
 217            return 1;
 218        }
 219
 220        return 0;
 221    }
 222
 223    if (strcmp(cmd, "unblock") == 0) { /** **/
 224        int ret = instance_unblock(user);
 225
 226        if (ret < 0) {
 227            fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
 228            return 1;
 229        }
 230
 231        return 0;
 232    }
 233
 234    if (strcmp(cmd, "webfinger") == 0) { /** **/
 235        xs *actor = NULL;
 236        xs *uid = NULL;
 237        int status;
 238
 239        status = webfinger_request(user, &actor, &uid);
 240
 241        printf("status: %d\n", status);
 242        if (actor != NULL)
 243            printf("actor: %s\n", actor);
 244        if (uid != NULL)
 245            printf("uid: %s\n", uid);
 246
 247        return 0;
 248    }
 249
 250    if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
 251        /* query an actor without user (non-signed) */
 252        xs *actor = NULL;
 253        int status;
 254
 255        status = actor_request(NULL, user, &actor);
 256
 257        printf("status: %d\n", status);
 258        if (valid_status(status)) {
 259            xs_json_dump(actor, 4, stdout);
 260            printf("\n");
 261        }
 262
 263        return 0;
 264    }
 265
 266    if (!user_open(&snac, user)) {
 267        printf("invalid user '%s'\n", user);
 268        return 1;
 269    }
 270
 271    lastlog_write(&snac, "cmdline");
 272
 273    if (strcmp(cmd, "resetpwd") == 0) { /** **/
 274        return resetpwd(&snac);
 275    }
 276
 277    if (strcmp(cmd, "deluser") == 0) { /** **/
 278        return deluser(&snac);
 279    }
 280
 281    if (strcmp(cmd, "update") == 0) { /** **/
 282        xs *a_msg = msg_actor(&snac);
 283        xs *u_msg = msg_update(&snac, a_msg);
 284        enqueue_message(&snac, u_msg);
 285        return 0;
 286    }
 287
 288    if (strcmp(cmd, "queue") == 0) { /** **/
 289        process_user_queue(&snac);
 290        return 0;
 291    }
 292
 293    if (strcmp(cmd, "verify_links") == 0) { /** **/
 294        verify_links(&snac);
 295        return 0;
 296    }
 297
 298    if (strcmp(cmd, "timeline") == 0) { /** **/
 299#if 0
 300        xs *list = local_list(&snac, XS_ALL);
 301        xs *body = html_timeline(&snac, list, 1);
 302
 303        printf("%s\n", body);
 304        user_free(&snac);
 305        srv_free();
 306#endif
 307
 308        xs *idx  = xs_fmt("%s/private.idx", snac.basedir);
 309        xs *list = index_list_desc(idx, 0, 256);
 310        xs *tl   = timeline_top_level(&snac, list);
 311
 312        xs_json_dump(tl, 4, stdout);
 313
 314        return 0;
 315    }
 316
 317    if (strcmp(cmd, "export_csv") == 0) { /** **/
 318        export_csv(&snac);
 319        return 0;
 320    }
 321
 322    if (strcmp(cmd, "export_posts") == 0) { /** **/
 323        export_posts(&snac);
 324        return 0;
 325    }
 326
 327    if (strcmp(cmd, "import_csv") == 0) { /** **/
 328        import_csv(&snac);
 329        return 0;
 330    }
 331
 332    if (strcmp(cmd, "migrate") == 0) { /** **/
 333        return migrate_account(&snac);
 334    }
 335
 336    if (strcmp(cmd, "lists") == 0) { /** **/
 337        xs *lol = list_maint(&snac, NULL, OP_LIST);
 338        const xs_list *l;
 339
 340        xs_list_foreach(lol, l) {
 341            printf("%s (%s)\n", xs_list_get(l, 1), xs_list_get(l, 0));
 342        }
 343
 344        return 0;
 345    }
 346
 347    if (strcmp(cmd, "muted") == 0) { /** **/
 348        xs *l = muted_list(&snac);
 349        const char *v;
 350
 351        xs_list_foreach(l, v)
 352            printf("%s\n", v);
 353
 354        return 0;
 355    }
 356
 357    if (strcmp(cmd, "top_ten") == 0) { /** **/
 358        int count = 10;
 359        const char *n = GET_ARGV();
 360        if (xs_is_string(n))
 361            count = atoi(n);
 362
 363        xs *l = user_top_ten(&snac, count);
 364        const xs_list *i;
 365
 366        xs_list_foreach(l, i) {
 367            printf("%s %ld★ %ld↺ %ld☺\n", xs_list_get(i, 0),
 368                xs_number_get_l(xs_list_get(i, 1)),
 369                xs_number_get_l(xs_list_get(i, 2)),
 370                xs_number_get_l(xs_list_get(i, 3)));
 371        }
 372
 373        return 0;
 374    }
 375
 376    if (strcmp(cmd, "refresh") == 0) { /** **/
 377        xs *fwers = follower_list(&snac);
 378        xs *fwing = following_list(&snac);
 379        const char *id;
 380
 381        xs_list_foreach(fwers, id)
 382            enqueue_actor_refresh(&snac, id, 0);
 383
 384        xs_list_foreach(fwing, id)
 385            enqueue_actor_refresh(&snac, id, 0);
 386
 387        return 0;
 388    }
 389
 390    if ((url = GET_ARGV()) == NULL)
 391        return usage(cmd);
 392
 393    if (strcmp(cmd, "list_members") == 0) { /** **/
 394        xs *lid = list_maint(&snac, url, OP_FIND);
 395
 396        if (lid != NULL) {
 397            xs *lcont = list_members(&snac, lid, NULL, OP_LIST);
 398            const char *md5;
 399
 400            xs_list_foreach(lcont, md5) {
 401                xs *actor = NULL;
 402
 403                if (valid_status(object_get_by_md5(md5, &actor))) {
 404                    printf("%s (%s)\n", xs_dict_get(actor, "id"), xs_dict_get_def(actor, "preferredUsername", ""));
 405                }
 406            }
 407        }
 408        else
 409            fprintf(stderr, "Cannot find a list named '%s'\n", url);
 410
 411        return 0;
 412    }
 413
 414    if (strcmp(cmd, "list_create") == 0) { /** **/
 415        xs *lid = list_maint(&snac, url, OP_FIND);
 416
 417        if (lid == NULL) {
 418            xs *n_lid = list_maint(&snac, url, OP_ADD);
 419            printf("New list named '%s' created (%s)\n", url, n_lid);
 420        }
 421        else
 422            fprintf(stderr, "A list named '%s' already exist\n", url);
 423
 424        return 0;
 425    }
 426
 427    if (strcmp(cmd, "list_remove") == 0) { /** **/
 428        xs *lid = list_maint(&snac, url, OP_FIND);
 429
 430        if (lid != NULL) {
 431            list_maint(&snac, lid, OP_DEL);
 432            printf("List '%s' (%s) deleted\n", url, lid);
 433        }
 434        else
 435            fprintf(stderr, "Cannot find a list named '%s'\n", url);
 436
 437        return 0;
 438    }
 439
 440    if (strcmp(cmd, "list_add") == 0) { /** **/
 441        const char *account = GET_ARGV();
 442
 443        if (account != NULL) {
 444            xs *lid = list_maint(&snac, url, OP_FIND);
 445
 446            if (lid != NULL) {
 447                xs *actor = NULL;
 448                xs *uid = NULL;
 449
 450                if (valid_status(webfinger_request(account, &actor, &uid))) {
 451                    xs *md5 = xs_md5_hex(actor, strlen(actor));
 452
 453                    list_members(&snac, lid, md5, OP_ADD);
 454                    printf("Actor %s (%s) added to list '%s' (%s)\n", actor, uid, url, lid);
 455                }
 456                else
 457                    fprintf(stderr, "Cannot resolve account '%s'\n", account);
 458            }
 459            else
 460                fprintf(stderr, "Cannot find a list named '%s'\n", url);
 461
 462        }
 463
 464        return 0;
 465    }
 466
 467    if (strcmp(cmd, "list_del") == 0) { /** **/
 468        const char *account = GET_ARGV();
 469
 470        if (account != NULL) {
 471            xs *lid = list_maint(&snac, url, OP_FIND);
 472
 473            if (lid != NULL) {
 474                xs *md5 = xs_md5_hex(account, strlen(account));
 475
 476                list_members(&snac, lid, md5, OP_DEL);
 477                printf("Actor %s deleted from list '%s' (%s)\n", account, url, lid);
 478            }
 479            else
 480                fprintf(stderr, "Cannot find a list named '%s'\n", url);
 481
 482        }
 483
 484        return 0;
 485    }
 486
 487    if (strcmp(cmd, "alias") == 0) { /** **/
 488        xs *actor = NULL;
 489        xs *uid = NULL;
 490        int status = HTTP_STATUS_OK;
 491
 492        if (*url == '\0')
 493            actor = xs_dup("");
 494        else
 495            status = webfinger_request(url, &actor, &uid);
 496
 497        if (valid_status(status)) {
 498            if (strcmp(actor, snac.actor) == 0) {
 499                snac_log(&snac, xs_fmt("You can't be your own alias"));
 500                return 1;
 501            }
 502            else {
 503                snac.config = xs_dict_set(snac.config, "alias", actor);
 504                snac.config = xs_dict_set(snac.config, "alias_raw", url);
 505
 506                user_persist(&snac, 1);
 507            }
 508        }
 509        else {
 510            snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
 511            return 1;
 512        }
 513
 514        return 0;
 515    }
 516
 517    if (strcmp(cmd, "webfinger_s") == 0) { /** **/
 518        xs *actor = NULL;
 519        xs *uid = NULL;
 520        int status;
 521
 522        status = webfinger_request_signed(&snac, url, &actor, &uid);
 523
 524        printf("status: %d\n", status);
 525        if (actor != NULL)
 526            printf("actor: %s\n", actor);
 527        if (uid != NULL)
 528            printf("uid: %s\n", uid);
 529
 530        return 0;
 531    }
 532
 533    if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
 534        xs *msg = msg_admiration(&snac, url, "Announce");
 535
 536        if (msg != NULL) {
 537            enqueue_message(&snac, msg);
 538            timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, "");
 539
 540            if (dbglevel) {
 541                xs_json_dump(msg, 4, stdout);
 542            }
 543        }
 544
 545        return 0;
 546    }
 547
 548
 549    if (strcmp(cmd, "assist") == 0) { /** **/
 550        /* undocumented: experimental (do not use) */
 551        xs *msg = msg_admiration(&snac, url, "Accept");
 552
 553        if (msg != NULL) {
 554            enqueue_message(&snac, msg);
 555
 556            if (dbglevel) {
 557                xs_json_dump(msg, 4, stdout);
 558            }
 559        }
 560
 561        return 0;
 562    }
 563
 564    if (strcmp(cmd, "unboost") == 0) { /** **/
 565        xs *msg = msg_repulsion(&snac, url, "Announce");
 566
 567        if (msg != NULL) {
 568            enqueue_message(&snac, msg);
 569
 570            if (dbglevel) {
 571                xs_json_dump(msg, 4, stdout);
 572            }
 573        }
 574
 575        return 0;
 576    }
 577
 578    if (strcmp(cmd, "follow") == 0) { /** **/
 579        xs *msg = msg_follow(&snac, url);
 580
 581        if (msg != NULL) {
 582            const char *actor = xs_dict_get(msg, "object");
 583
 584            following_add(&snac, actor, msg);
 585
 586            enqueue_output_by_actor(&snac, msg, actor, 0);
 587
 588            if (dbglevel) {
 589                xs_json_dump(msg, 4, stdout);
 590            }
 591        }
 592
 593        return 0;
 594    }
 595
 596    if (strcmp(cmd, "unfollow") == 0) { /** **/
 597        xs *object = NULL;
 598
 599        if (valid_status(following_get(&snac, url, &object))) {
 600            xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
 601
 602            following_del(&snac, url);
 603
 604            enqueue_output_by_actor(&snac, msg, url, 0);
 605
 606            snac_log(&snac, xs_fmt("unfollowed actor %s", url));
 607        }
 608        else
 609            snac_log(&snac, xs_fmt("actor is not being followed %s", url));
 610
 611        return 0;
 612    }
 613
 614    if (strcmp(cmd, "limit") == 0) { /** **/
 615        int ret;
 616
 617        if (!following_check(&snac, url))
 618            snac_log(&snac, xs_fmt("actor %s is not being followed", url));
 619        else
 620        if ((ret = limit(&snac, url)) == 0)
 621            snac_log(&snac, xs_fmt("actor %s is now limited", url));
 622        else
 623            snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
 624
 625        return 0;
 626    }
 627
 628    if (strcmp(cmd, "unlimit") == 0) { /** **/
 629        int ret;
 630
 631        if (!following_check(&snac, url))
 632            snac_log(&snac, xs_fmt("actor %s is not being followed", url));
 633        else
 634        if ((ret = unlimit(&snac, url)) == 0)
 635            snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
 636        else
 637            snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
 638
 639        return 0;
 640    }
 641
 642    if (strcmp(cmd, "cool") == 0) { /** **/
 643        int ret;
 644
 645        if ((ret = cool(&snac, url, OP_ADD)) == 0)
 646            snac_log(&snac, xs_fmt("posts from actor %s will now be notified", url));
 647        else
 648            snac_log(&snac, xs_fmt("error marking actor %s as cool (%d)", url, ret));
 649
 650        return 0;
 651    }
 652
 653    if (strcmp(cmd, "uncool") == 0) { /** **/
 654        int ret;
 655
 656        if ((ret = cool(&snac, url, OP_DEL)) == 0)
 657            snac_log(&snac, xs_fmt("posts from actor %s will no longer be notified", url));
 658        else
 659            snac_log(&snac, xs_fmt("error unmarking actor %s as cool (%d)", url, ret));
 660
 661        return 0;
 662    }
 663
 664    if (strcmp(cmd, "unmute") == 0) { /** **/
 665        if (is_muted(&snac, url)) {
 666            unmute(&snac, url);
 667
 668            printf("%s unmuted\n", url);
 669        }
 670        else
 671            printf("%s actor is not muted\n", url);
 672
 673        return 0;
 674    }
 675
 676    if (strcmp(cmd, "search") == 0) { /** **/
 677        int to;
 678
 679        /* 'url' contains the regex */
 680        xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
 681
 682        int c = 0;
 683        const char *v;
 684
 685        /* print results as standalone links */
 686        while (xs_list_next(r, &v, &c)) {
 687            printf("%s/admin/p/%s\n", snac.actor, v);
 688        }
 689
 690        return 0;
 691    }
 692
 693    if (strcmp(cmd, "ping") == 0) { /** **/
 694        xs *actor_o = NULL;
 695
 696        if (!xs_startswith(url, "https:/")) {
 697            /* try to resolve via webfinger */
 698            if (!valid_status(webfinger_request(url, &url, NULL))) {
 699                srv_log(xs_fmt("cannot resolve %s via webfinger", url));
 700                return 1;
 701            }
 702        }
 703
 704        if (valid_status(actor_request(&snac, url, &actor_o))) {
 705            xs *msg = msg_ping(&snac, url);
 706
 707            enqueue_output_by_actor(&snac, msg, url, 0);
 708
 709            if (dbglevel) {
 710                xs_json_dump(msg, 4, stdout);
 711            }
 712
 713            srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
 714        }
 715        else {
 716            srv_log(xs_fmt("Error getting actor %s", url));
 717            return 1;
 718        }
 719
 720        return 0;
 721    }
 722
 723    if (strcmp(cmd, "pin") == 0) { /** **/
 724        int ret = pin(&snac, url);
 725        if (ret < 0) {
 726            fprintf(stderr, "error pinning %s %d\n", url, ret);
 727            return 1;
 728        }
 729
 730        return 0;
 731    }
 732
 733    if (strcmp(cmd, "unpin") == 0) { /** **/
 734        int ret = unpin(&snac, url);
 735        if (ret < 0) {
 736            fprintf(stderr, "error unpinning %s %d\n", url, ret);
 737            return 1;
 738        }
 739
 740        return 0;
 741    }
 742
 743    if (strcmp(cmd, "bookmark") == 0) { /** **/
 744        int ret = bookmark(&snac, url);
 745        if (ret < 0) {
 746            fprintf(stderr, "error bookmarking %s %d\n", url, ret);
 747            return 1;
 748        }
 749
 750        return 0;
 751    }
 752
 753    if (strcmp(cmd, "unbookmark") == 0) { /** **/
 754        int ret = unbookmark(&snac, url);
 755        if (ret < 0) {
 756            fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
 757            return 1;
 758        }
 759
 760        return 0;
 761    }
 762
 763    if (strcmp(cmd, "question") == 0) { /** **/
 764        int end_secs = 5 * 60;
 765        xs *opts = xs_split(url, ";");
 766
 767        xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
 768        xs *c_msg = msg_create(&snac, msg);
 769
 770        if (dbglevel) {
 771            xs_json_dump(c_msg, 4, stdout);
 772        }
 773
 774        enqueue_message(&snac, c_msg);
 775        enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
 776
 777        timeline_add(&snac, xs_dict_get(msg, "id"), msg);
 778
 779        return 0;
 780    }
 781
 782    if (strcmp(cmd, "request") == 0) { /** **/
 783        int status;
 784        xs *data = NULL;
 785
 786        status = activitypub_request(&snac, url, &data);
 787
 788        printf("status: %d\n", status);
 789
 790        if (data != NULL) {
 791            xs_json_dump(data, 4, stdout);
 792        }
 793
 794        return 0;
 795    }
 796
 797    if (strcmp(cmd, "collect_replies") == 0) { /** **/
 798        enqueue_collect_replies(&snac, url);
 799
 800        return 0;
 801    }
 802
 803    if (strcmp(cmd, "collect_outbox") == 0) { /** **/
 804        enqueue_collect_outbox(&snac, url);
 805        return 0;
 806    }
 807
 808    if (strcmp(cmd, "insert") == 0) { /** **/
 809        int status;
 810        xs *data = NULL;
 811
 812        status = activitypub_request(&snac, url, &data);
 813
 814        printf("status: %d\n", status);
 815
 816        if (data != NULL) {
 817            xs_json_dump(data, 4, stdout);
 818            enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
 819
 820            if (!timeline_here(&snac, url))
 821                timeline_add(&snac, url, data);
 822            else
 823                printf("Post %s already here\n", url);
 824        }
 825
 826        return 0;
 827    }
 828
 829    if (strcmp(cmd, "request2") == 0) { /** **/
 830        enqueue_object_request(&snac, url, 2);
 831
 832        return 0;
 833    }
 834
 835    if (strcmp(cmd, "actor") == 0) { /** **/
 836        int status;
 837        xs *data = NULL;
 838
 839        status = actor_request(&snac, url, &data);
 840
 841        printf("status: %d\n", status);
 842
 843        if (valid_status(status)) {
 844            xs_json_dump(data, 4, stdout);
 845        }
 846
 847        return 0;
 848    }
 849
 850    if (strcmp(cmd, "import_list") == 0) { /** **/
 851        import_list_csv(&snac, url);
 852
 853        return 0;
 854    }
 855
 856    if (strcmp(cmd, "import_block_list") == 0) { /** **/
 857        import_blocked_accounts_csv(&snac, url);
 858
 859        return 0;
 860    }
 861
 862    if (strcmp(cmd, "note") == 0 ||             /** **/
 863        strcmp(cmd, "note_unlisted") == 0 ||    /** **/
 864        strcmp(cmd, "note_mention") == 0 ||     /** **/
 865        strcmp(cmd, "note_followers") == 0) {   /** **/
 866        xs *content = NULL;
 867        xs *msg = NULL;
 868        xs *c_msg = NULL;
 869        xs *attl = xs_list_new();
 870        const char *fn = NULL;
 871        const char *in_reply_to = NULL;
 872        const char **arg_irt = NULL;
 873        int arg_date = 0;
 874        xs *post_date = NULL;
 875
 876        /* iterate possible attachments */
 877        while ((fn = GET_ARGV())) {
 878            FILE *f;
 879
 880            if (arg_irt) {
 881                *arg_irt = fn;
 882                arg_irt = NULL;
 883            }
 884            else
 885            if (arg_date) {
 886                /* convert to ISO */
 887                time_t t = xs_parse_localtime(fn, "%Y%m%d%H%M%S");
 888
 889                if (t == 0) {
 890                    fprintf(stderr, "Invalid scheduled date format (must be YYYYmmddHHMMSS)\n");
 891                    return 1;
 892                }
 893
 894                post_date = xs_str_iso_date(t);
 895                arg_date = 0;
 896            }
 897            else
 898            if (strcmp(fn, "-r") == 0) {
 899                /* next argument is an inReplyTo */
 900                arg_irt = &in_reply_to;
 901            }
 902            else
 903            if (strcmp(fn, "-d") == 0) {
 904                /* next argument is the schedule date */
 905                arg_date = 1;
 906            }
 907            else
 908            if ((f = fopen(fn, "rb")) != NULL) {
 909                /* get the file size and content */
 910                fseek(f, 0, SEEK_END);
 911                int sz = ftell(f);
 912                fseek(f, 0, SEEK_SET);
 913                xs *atc = xs_readall(f);
 914                fclose(f);
 915
 916                char *ext = strrchr(fn, '.');
 917                char rnd[32];
 918                xs_rnd_buf(rnd, sizeof(rnd));
 919                xs *hash  = xs_md5_hex(rnd, sizeof(rnd));
 920                xs *id    = xs_fmt("post-%s%s", hash, ext ? ext : "");
 921                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
 922
 923                /* store */
 924                static_put(&snac, id, atc, sz);
 925
 926                xs *l = xs_list_new();
 927
 928                l = xs_list_append(l, url);
 929                l = xs_list_append(l, ""); /* alt text */
 930
 931                attl = xs_list_append(attl, l);
 932            }
 933            else
 934                fprintf(stderr, "Error opening '%s' as attachment\n", fn);
 935        }
 936
 937        if (strcmp(url, "-e") == 0) {
 938            /* get the content from an editor */
 939#define EDITOR "$EDITOR "
 940            char cmd[] = EDITOR "/tmp/snac-XXXXXX";
 941            FILE *f;
 942            int fd = mkstemp(cmd + strlen(EDITOR));
 943
 944            if (fd >= 0) {
 945                int status = system(cmd);
 946
 947                if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) {
 948                    content = xs_readall(f);
 949                    fclose(f);
 950                    unlink(cmd + strlen(EDITOR));
 951                } else {
 952                    printf("Nothing to send\n");
 953                    close(fd);
 954                    return 1;
 955                }
 956            } else {
 957                fprintf(stderr, "Temp file creation failed\n");
 958                return 1;
 959            }
 960        }
 961        else
 962        if (strcmp(url, "-") == 0) {
 963            /* get the content from stdin */
 964            content = xs_readall(stdin);
 965        }
 966        else
 967            content = xs_dup(url);
 968
 969        if (!content || !*content) {
 970            fprintf(stderr, "Nothing to send\n");
 971            return 1;
 972        }
 973
 974        int scope = SCOPE_PUBLIC;
 975        if (strcmp(cmd, "note_mention") == 0)
 976            scope = SCOPE_MENTIONED;
 977        else
 978        if (strcmp(cmd, "note_unlisted") == 0)
 979            scope = SCOPE_UNLISTED;
 980        else
 981        if (strcmp(cmd, "note_followers") == 0)
 982            scope = SCOPE_FOLLOWERS;
 983
 984        msg = msg_note(&snac, content, NULL, in_reply_to, attl, scope, getenv("LANG"), post_date);
 985
 986        const char *id = xs_dict_get(msg, "id");
 987
 988        if (post_date)
 989            schedule_add(&snac, id, msg);
 990        else {
 991            c_msg = msg_create(&snac, msg);
 992
 993            if (dbglevel) {
 994                xs_json_dump(c_msg, 4, stdout);
 995            }
 996
 997            enqueue_message(&snac, c_msg);
 998            enqueue_webmention(msg);
 999
1000            timeline_add(&snac, id, msg);
1001        }
1002
1003        return 0;
1004    }
1005
1006    fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
1007
1008    return 1;
1009}