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