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