2.67 main.c
  1/* snac - A simple, minimalistic ActivityPub instance */
  2/* copyright (c) 2022 - 2024 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_openssl.h"
  9
 10#include "snac.h"
 11
 12#include <sys/stat.h>
 13
 14int usage(void)
 15{
 16    printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
 17    printf("Copyright (c) 2022 - 2024 grunfink et al. / MIT license\n");
 18    printf("\n");
 19    printf("Commands:\n");
 20    printf("\n");
 21    printf("init [{basedir}]                     Initializes the data storage\n");
 22    printf("upgrade {basedir}                    Upgrade to a new version\n");
 23    printf("adduser {basedir} [{uid}]            Adds a new user\n");
 24    printf("deluser {basedir} {uid}              Deletes a user\n");
 25    printf("httpd {basedir}                      Starts the HTTPD daemon\n");
 26    printf("purge {basedir}                      Purges old data\n");
 27    printf("state {basedir}                      Prints server state\n");
 28    printf("webfinger {basedir} {account}        Queries about an account (@user@host or actor url)\n");
 29    printf("queue {basedir} {uid}                Processes a user queue\n");
 30    printf("follow {basedir} {uid} {actor}       Follows an actor\n");
 31    printf("unfollow {basedir} {uid} {actor}     Unfollows an actor\n");
 32    printf("request {basedir} {uid} {url}        Requests an object\n");
 33    printf("insert {basedir} {uid} {url}         Requests an object and inserts it into the timeline\n");
 34    printf("actor {basedir} [{uid}] {url}        Requests an actor\n");
 35    printf("note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n");
 36    printf("boost|announce {basedir} {uid} {url} Boosts (announces) a post\n");
 37    printf("unboost {basedir} {uid} {url}        Unboosts a post\n");
 38    printf("resetpwd {basedir} {uid}             Resets the password of a user\n");
 39    printf("ping {basedir} {uid} {actor}         Pings an actor\n");
 40    printf("webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n");
 41    printf("pin {basedir} {uid} {msg_url}        Pins a message\n");
 42    printf("unpin {basedir} {uid} {msg_url}      Unpins a message\n");
 43    printf("bookmark {basedir} {uid} {msg_url}   Bookmarks a message\n");
 44    printf("unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n");
 45    printf("block {basedir} {instance_url}       Blocks a full instance\n");
 46    printf("unblock {basedir} {instance_url}     Unblocks a full instance\n");
 47    printf("limit {basedir} {uid} {actor}        Limits an actor (drops their announces)\n");
 48    printf("unlimit {basedir} {uid} {actor}      Unlimits an actor\n");
 49    printf("verify_links {basedir} {uid}         Verifies a user's links (in the metadata)\n");
 50    printf("search {basedir} {uid} {regex}       Searches posts by content\n");
 51    printf("export_csv {basedir} {uid}           Exports data as CSV files into current directory\n");
 52    printf("alias {basedir} {uid} {account}      Sets account (@user@host or actor url) as an alias\n");
 53    printf("migrate {basedir} {uid}              Migrates to the account defined as the alias\n");
 54    printf("import_csv {basedir} {uid}           Imports data from CSV files in the current directory\n");
 55    printf("import_list {basedir} {uid} {file}   Imports a Mastodon CSV list file\n");
 56    printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n");
 57
 58    return 1;
 59}
 60
 61
 62char *get_argv(int *argi, int argc, char *argv[])
 63{
 64    if (*argi < argc)
 65        return argv[(*argi)++];
 66    else
 67        return NULL;
 68}
 69
 70
 71#define GET_ARGV() get_argv(&argi, argc, argv)
 72
 73int main(int argc, char *argv[])
 74{
 75    char *cmd;
 76    char *basedir;
 77    char *user;
 78    char *url;
 79    int argi = 1;
 80    snac snac;
 81
 82    /* ensure group has write access */
 83    umask(0007);
 84
 85    if ((cmd = GET_ARGV()) == NULL)
 86        return usage();
 87
 88    if (strcmp(cmd, "init") == 0) { /** **/
 89        /* initialize the data storage */
 90        /* ... */
 91        basedir = GET_ARGV();
 92
 93        return snac_init(basedir);
 94    }
 95
 96    if (strcmp(cmd, "upgrade") == 0) { /** **/
 97        int ret;
 98
 99        /* upgrade */
100        if ((basedir = GET_ARGV()) == NULL)
101            return usage();
102
103        if ((ret = srv_open(basedir, 1)) == 1)
104            srv_log(xs_dup("OK"));
105
106        return ret;
107    }
108
109    if (strcmp(cmd, "markdown") == 0) { /** **/
110        /* undocumented, for testing only */
111        xs *c = xs_readall(stdin);
112        xs *fc = not_really_markdown(c, NULL, NULL);
113
114        printf("<html>\n%s\n</html>\n", fc);
115        return 0;
116    }
117
118    if ((basedir = GET_ARGV()) == NULL)
119        return usage();
120
121    if (!srv_open(basedir, 0)) {
122        srv_log(xs_fmt("error opening data storage at %s", basedir));
123        return 1;
124    }
125
126    if (strcmp(cmd, "adduser") == 0) { /** **/
127        user = GET_ARGV();
128
129        return adduser(user);
130
131        return 0;
132    }
133
134    if (strcmp(cmd, "httpd") == 0) { /** **/
135        httpd();
136        srv_free();
137        return 0;
138    }
139
140    if (strcmp(cmd, "purge") == 0) { /** **/
141        purge_all();
142        return 0;
143    }
144
145    if (strcmp(cmd, "state") == 0) { /** **/
146        xs *shm_name = NULL;
147        srv_state *p_state = srv_state_op(&shm_name, 1);
148
149        if (p_state == NULL)
150            return 1;
151
152        srv_state ss = *p_state;
153        int n;
154
155        printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
156        xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
157        printf("uptime: %s\n", uptime);
158        printf("job fifo size (cur): %d\n", ss.job_fifo_size);
159        printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
160        char *th_states[] = { "stopped", "waiting", "input", "output" };
161
162        for (n = 0; n < ss.n_threads; n++)
163            printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
164
165        return 0;
166    }
167
168    if ((user = GET_ARGV()) == NULL)
169        return usage();
170
171    if (strcmp(cmd, "block") == 0) { /** **/
172        int ret = instance_block(user);
173
174        if (ret < 0) {
175            fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
176            return 1;
177        }
178
179        return 0;
180    }
181
182    if (strcmp(cmd, "unblock") == 0) { /** **/
183        int ret = instance_unblock(user);
184
185        if (ret < 0) {
186            fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
187            return 1;
188        }
189
190        return 0;
191    }
192
193    if (strcmp(cmd, "webfinger") == 0) { /** **/
194        xs *actor = NULL;
195        xs *uid = NULL;
196        int status;
197
198        status = webfinger_request(user, &actor, &uid);
199
200        printf("status: %d\n", status);
201        if (actor != NULL)
202            printf("actor: %s\n", actor);
203        if (uid != NULL)
204            printf("uid: %s\n", uid);
205
206        return 0;
207    }
208
209    if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
210        /* query an actor without user (non-signed) */
211        xs *actor = NULL;
212        int status;
213
214        status = actor_request(NULL, user, &actor);
215
216        printf("status: %d\n", status);
217        if (valid_status(status)) {
218            xs_json_dump(actor, 4, stdout);
219            printf("\n");
220        }
221
222        return 0;
223    }
224
225    if (!user_open(&snac, user)) {
226        printf("invalid user '%s'\n", user);
227        return 1;
228    }
229
230    lastlog_write(&snac, "cmdline");
231
232    if (strcmp(cmd, "resetpwd") == 0) { /** **/
233        return resetpwd(&snac);
234    }
235
236    if (strcmp(cmd, "deluser") == 0) { /** **/
237        return deluser(&snac);
238    }
239
240    if (strcmp(cmd, "update") == 0) { /** **/
241        xs *a_msg = msg_actor(&snac);
242        xs *u_msg = msg_update(&snac, a_msg);
243        enqueue_message(&snac, u_msg);
244        return 0;
245    }
246
247    if (strcmp(cmd, "queue") == 0) { /** **/
248        process_user_queue(&snac);
249        return 0;
250    }
251
252    if (strcmp(cmd, "verify_links") == 0) { /** **/
253        verify_links(&snac);
254        return 0;
255    }
256
257    if (strcmp(cmd, "timeline") == 0) { /** **/
258#if 0
259        xs *list = local_list(&snac, XS_ALL);
260        xs *body = html_timeline(&snac, list, 1);
261
262        printf("%s\n", body);
263        user_free(&snac);
264        srv_free();
265#endif
266
267        xs *idx  = xs_fmt("%s/private.idx", snac.basedir);
268        xs *list = index_list_desc(idx, 0, 256);
269        xs *tl   = timeline_top_level(&snac, list);
270
271        xs_json_dump(tl, 4, stdout);
272
273        return 0;
274    }
275
276    if (strcmp(cmd, "export_csv") == 0) { /** **/
277        export_csv(&snac);
278        return 0;
279    }
280
281    if (strcmp(cmd, "import_csv") == 0) { /** **/
282        import_csv(&snac);
283        return 0;
284    }
285
286    if (strcmp(cmd, "migrate") == 0) { /** **/
287        return migrate_account(&snac);
288    }
289
290    if ((url = GET_ARGV()) == NULL)
291        return usage();
292
293    if (strcmp(cmd, "alias") == 0) { /** **/
294        xs *actor = NULL;
295        xs *uid = NULL;
296        int status = HTTP_STATUS_OK;
297
298        if (*url == '\0')
299            actor = xs_dup("");
300        else
301            status = webfinger_request(url, &actor, &uid);
302
303        if (valid_status(status)) {
304            if (strcmp(actor, snac.actor) == 0) {
305                snac_log(&snac, xs_fmt("You can't be your own alias"));
306                return 1;
307            }
308            else {
309                snac.config = xs_dict_set(snac.config, "alias", actor);
310                snac.config = xs_dict_set(snac.config, "alias_raw", url);
311
312                user_persist(&snac, 1);
313            }
314        }
315        else {
316            snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
317            return 1;
318        }
319
320        return 0;
321    }
322
323    if (strcmp(cmd, "webfinger_s") == 0) { /** **/
324        xs *actor = NULL;
325        xs *uid = NULL;
326        int status;
327
328        status = webfinger_request_signed(&snac, url, &actor, &uid);
329
330        printf("status: %d\n", status);
331        if (actor != NULL)
332            printf("actor: %s\n", actor);
333        if (uid != NULL)
334            printf("uid: %s\n", uid);
335
336        return 0;
337    }
338
339    if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
340        xs *msg = msg_admiration(&snac, url, "Announce");
341
342        if (msg != NULL) {
343            enqueue_message(&snac, msg);
344
345            if (dbglevel) {
346                xs_json_dump(msg, 4, stdout);
347            }
348        }
349
350        return 0;
351    }
352
353    if (strcmp(cmd, "unboost") == 0) { /** **/
354        xs *msg = msg_repulsion(&snac, url, "Announce");
355
356        if (msg != NULL) {
357            enqueue_message(&snac, msg);
358
359            if (dbglevel) {
360                xs_json_dump(msg, 4, stdout);
361            }
362        }
363
364        return 0;
365    }
366
367    if (strcmp(cmd, "follow") == 0) { /** **/
368        xs *msg = msg_follow(&snac, url);
369
370        if (msg != NULL) {
371            const char *actor = xs_dict_get(msg, "object");
372
373            following_add(&snac, actor, msg);
374
375            enqueue_output_by_actor(&snac, msg, actor, 0);
376
377            if (dbglevel) {
378                xs_json_dump(msg, 4, stdout);
379            }
380        }
381
382        return 0;
383    }
384
385    if (strcmp(cmd, "unfollow") == 0) { /** **/
386        xs *object = NULL;
387
388        if (valid_status(following_get(&snac, url, &object))) {
389            xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
390
391            following_del(&snac, url);
392
393            enqueue_output_by_actor(&snac, msg, url, 0);
394
395            snac_log(&snac, xs_fmt("unfollowed actor %s", url));
396        }
397        else
398            snac_log(&snac, xs_fmt("actor is not being followed %s", url));
399
400        return 0;
401    }
402
403    if (strcmp(cmd, "limit") == 0) { /** **/
404        int ret;
405
406        if (!following_check(&snac, url))
407            snac_log(&snac, xs_fmt("actor %s is not being followed", url));
408        else
409        if ((ret = limit(&snac, url)) == 0)
410            snac_log(&snac, xs_fmt("actor %s is now limited", url));
411        else
412            snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
413
414        return 0;
415    }
416
417    if (strcmp(cmd, "unlimit") == 0) { /** **/
418        int ret;
419
420        if (!following_check(&snac, url))
421            snac_log(&snac, xs_fmt("actor %s is not being followed", url));
422        else
423        if ((ret = unlimit(&snac, url)) == 0)
424            snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
425        else
426            snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
427
428        return 0;
429    }
430
431    if (strcmp(cmd, "search") == 0) { /** **/
432        int to;
433
434        /* 'url' contains the regex */
435        xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
436
437        int c = 0;
438        const char *v;
439
440        /* print results as standalone links */
441        while (xs_list_next(r, &v, &c)) {
442            printf("%s/admin/p/%s\n", snac.actor, v);
443        }
444
445        return 0;
446    }
447
448    if (strcmp(cmd, "ping") == 0) { /** **/
449        xs *actor_o = NULL;
450
451        if (!xs_startswith(url, "https:/")) {
452            /* try to resolve via webfinger */
453            if (!valid_status(webfinger_request(url, &url, NULL))) {
454                srv_log(xs_fmt("cannot resolve %s via webfinger", url));
455                return 1;
456            }
457        }
458
459        if (valid_status(actor_request(&snac, url, &actor_o))) {
460            xs *msg = msg_ping(&snac, url);
461
462            enqueue_output_by_actor(&snac, msg, url, 0);
463
464            if (dbglevel) {
465                xs_json_dump(msg, 4, stdout);
466            }
467
468            srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
469        }
470        else {
471            srv_log(xs_fmt("Error getting actor %s", url));
472            return 1;
473        }
474
475        return 0;
476    }
477
478    if (strcmp(cmd, "pin") == 0) { /** **/
479        int ret = pin(&snac, url);
480        if (ret < 0) {
481            fprintf(stderr, "error pinning %s %d\n", url, ret);
482            return 1;
483        }
484
485        return 0;
486    }
487
488    if (strcmp(cmd, "unpin") == 0) { /** **/
489        int ret = unpin(&snac, url);
490        if (ret < 0) {
491            fprintf(stderr, "error unpinning %s %d\n", url, ret);
492            return 1;
493        }
494
495        return 0;
496    }
497
498    if (strcmp(cmd, "bookmark") == 0) { /** **/
499        int ret = bookmark(&snac, url);
500        if (ret < 0) {
501            fprintf(stderr, "error bookmarking %s %d\n", url, ret);
502            return 1;
503        }
504
505        return 0;
506    }
507
508    if (strcmp(cmd, "unbookmark") == 0) { /** **/
509        int ret = unbookmark(&snac, url);
510        if (ret < 0) {
511            fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
512            return 1;
513        }
514
515        return 0;
516    }
517
518    if (strcmp(cmd, "question") == 0) { /** **/
519        int end_secs = 5 * 60;
520        xs *opts = xs_split(url, ";");
521
522        xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
523        xs *c_msg = msg_create(&snac, msg);
524
525        if (dbglevel) {
526            xs_json_dump(c_msg, 4, stdout);
527        }
528
529        enqueue_message(&snac, c_msg);
530        enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
531
532        timeline_add(&snac, xs_dict_get(msg, "id"), msg);
533
534        return 0;
535    }
536
537    if (strcmp(cmd, "request") == 0) { /** **/
538        int status;
539        xs *data = NULL;
540
541        status = activitypub_request(&snac, url, &data);
542
543        printf("status: %d\n", status);
544
545        if (data != NULL) {
546            xs_json_dump(data, 4, stdout);
547        }
548
549        return 0;
550    }
551
552    if (strcmp(cmd, "insert") == 0) { /** **/
553        int status;
554        xs *data = NULL;
555
556        status = activitypub_request(&snac, url, &data);
557
558        printf("status: %d\n", status);
559
560        if (data != NULL) {
561            xs_json_dump(data, 4, stdout);
562            enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
563
564            if (!timeline_here(&snac, url))
565                timeline_add(&snac, url, data);
566            else
567                printf("Post %s already here\n", url);
568        }
569
570        return 0;
571    }
572
573    if (strcmp(cmd, "request2") == 0) { /** **/
574        enqueue_object_request(&snac, url, 2);
575
576        return 0;
577    }
578
579    if (strcmp(cmd, "actor") == 0) { /** **/
580        int status;
581        xs *data = NULL;
582
583        status = actor_request(&snac, url, &data);
584
585        printf("status: %d\n", status);
586
587        if (valid_status(status)) {
588            xs_json_dump(data, 4, stdout);
589        }
590
591        return 0;
592    }
593
594    if (strcmp(cmd, "import_list") == 0) { /** **/
595        import_list_csv(&snac, url);
596
597        return 0;
598    }
599
600    if (strcmp(cmd, "import_block_list") == 0) { /** **/
601        import_blocked_accounts_csv(&snac, url);
602
603        return 0;
604    }
605
606    if (strcmp(cmd, "note") == 0) { /** **/
607        xs *content = NULL;
608        xs *msg = NULL;
609        xs *c_msg = NULL;
610        xs *attl = xs_list_new();
611        char *fn = NULL;
612
613        /* iterate possible attachments */
614        while ((fn = GET_ARGV())) {
615            FILE *f;
616
617            if ((f = fopen(fn, "rb")) != NULL) {
618                /* get the file size and content */
619                fseek(f, 0, SEEK_END);
620                int sz = ftell(f);
621                fseek(f, 0, SEEK_SET);
622                xs *atc = xs_readall(f);
623                fclose(f);
624
625                char *ext = strrchr(fn, '.');
626                xs *hash  = xs_md5_hex(fn, strlen(fn));
627                xs *id    = xs_fmt("%s%s", hash, ext);
628                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
629
630                /* store */
631                static_put(&snac, id, atc, sz);
632
633                xs *l = xs_list_new();
634
635                l = xs_list_append(l, url);
636                l = xs_list_append(l, ""); /* alt text */
637
638                attl = xs_list_append(attl, l);
639            }
640            else
641                fprintf(stderr, "Error opening '%s' as attachment\n", fn);
642        }
643
644        if (strcmp(url, "-e") == 0) {
645            /* get the content from an editor */
646            FILE *f;
647
648            unlink("/tmp/snac-edit.txt");
649            system("$EDITOR /tmp/snac-edit.txt");
650
651            if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
652                content = xs_readall(f);
653                fclose(f);
654
655                unlink("/tmp/snac-edit.txt");
656            }
657            else {
658                printf("Nothing to send\n");
659                return 1;
660            }
661        }
662        else
663        if (strcmp(url, "-") == 0) {
664            /* get the content from stdin */
665            content = xs_readall(stdin);
666        }
667        else
668            content = xs_dup(url);
669
670        msg = msg_note(&snac, content, NULL, NULL, attl, 0);
671
672        c_msg = msg_create(&snac, msg);
673
674        if (dbglevel) {
675            xs_json_dump(c_msg, 4, stdout);
676        }
677
678        enqueue_message(&snac, c_msg);
679
680        timeline_add(&snac, xs_dict_get(msg, "id"), msg);
681
682        return 0;
683    }
684
685    fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
686
687    return 1;
688}