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