2.95
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, OP_LIST);
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↺ %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 xs_number_get_l(xs_list_get(i, 3)));
369 }
370
371 return 0;
372 }
373
374 if (strcmp(cmd, "refresh") == 0) { /** **/
375 xs *fwers = follower_list(&snac);
376 xs *fwing = following_list(&snac);
377 const char *id;
378
379 xs_list_foreach(fwers, id)
380 enqueue_actor_refresh(&snac, id, 0);
381
382 xs_list_foreach(fwing, id)
383 enqueue_actor_refresh(&snac, id, 0);
384
385 return 0;
386 }
387
388 if ((url = GET_ARGV()) == NULL)
389 return usage(cmd);
390
391 if (strcmp(cmd, "list_members") == 0) { /** **/
392 xs *lid = list_maint(&snac, url, OP_FIND);
393
394 if (lid != NULL) {
395 xs *lcont = list_members(&snac, lid, NULL, OP_LIST);
396 const char *md5;
397
398 xs_list_foreach(lcont, md5) {
399 xs *actor = NULL;
400
401 if (valid_status(object_get_by_md5(md5, &actor))) {
402 printf("%s (%s)\n", xs_dict_get(actor, "id"), xs_dict_get_def(actor, "preferredUsername", ""));
403 }
404 }
405 }
406 else
407 fprintf(stderr, "Cannot find a list named '%s'\n", url);
408
409 return 0;
410 }
411
412 if (strcmp(cmd, "list_create") == 0) { /** **/
413 xs *lid = list_maint(&snac, url, OP_FIND);
414
415 if (lid == NULL) {
416 xs *n_lid = list_maint(&snac, url, OP_ADD);
417 printf("New list named '%s' created (%s)\n", url, n_lid);
418 }
419 else
420 fprintf(stderr, "A list named '%s' already exist\n", url);
421
422 return 0;
423 }
424
425 if (strcmp(cmd, "list_remove") == 0) { /** **/
426 xs *lid = list_maint(&snac, url, OP_FIND);
427
428 if (lid != NULL) {
429 list_maint(&snac, lid, OP_DEL);
430 printf("List '%s' (%s) deleted\n", url, lid);
431 }
432 else
433 fprintf(stderr, "Cannot find a list named '%s'\n", url);
434
435 return 0;
436 }
437
438 if (strcmp(cmd, "list_add") == 0) { /** **/
439 const char *account = GET_ARGV();
440
441 if (account != NULL) {
442 xs *lid = list_maint(&snac, url, OP_FIND);
443
444 if (lid != NULL) {
445 xs *actor = NULL;
446 xs *uid = NULL;
447
448 if (valid_status(webfinger_request(account, &actor, &uid))) {
449 xs *md5 = xs_md5_hex(actor, strlen(actor));
450
451 list_members(&snac, lid, md5, OP_ADD);
452 printf("Actor %s (%s) added to list '%s' (%s)\n", actor, uid, url, lid);
453 }
454 else
455 fprintf(stderr, "Cannot resolve account '%s'\n", account);
456 }
457 else
458 fprintf(stderr, "Cannot find a list named '%s'\n", url);
459
460 }
461
462 return 0;
463 }
464
465 if (strcmp(cmd, "list_del") == 0) { /** **/
466 const char *account = GET_ARGV();
467
468 if (account != NULL) {
469 xs *lid = list_maint(&snac, url, OP_FIND);
470
471 if (lid != NULL) {
472 xs *md5 = xs_md5_hex(account, strlen(account));
473
474 list_members(&snac, lid, md5, OP_DEL);
475 printf("Actor %s deleted from list '%s' (%s)\n", account, url, lid);
476 }
477 else
478 fprintf(stderr, "Cannot find a list named '%s'\n", url);
479
480 }
481
482 return 0;
483 }
484
485 if (strcmp(cmd, "alias") == 0) { /** **/
486 xs *actor = NULL;
487 xs *uid = NULL;
488 int status = HTTP_STATUS_OK;
489
490 if (*url == '\0')
491 actor = xs_dup("");
492 else
493 status = webfinger_request(url, &actor, &uid);
494
495 if (valid_status(status)) {
496 if (strcmp(actor, snac.actor) == 0) {
497 snac_log(&snac, xs_fmt("You can't be your own alias"));
498 return 1;
499 }
500 else {
501 snac.config = xs_dict_set(snac.config, "alias", actor);
502 snac.config = xs_dict_set(snac.config, "alias_raw", url);
503
504 user_persist(&snac, 1);
505 }
506 }
507 else {
508 snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
509 return 1;
510 }
511
512 return 0;
513 }
514
515 if (strcmp(cmd, "webfinger_s") == 0) { /** **/
516 xs *actor = NULL;
517 xs *uid = NULL;
518 int status;
519
520 status = webfinger_request_signed(&snac, url, &actor, &uid);
521
522 printf("status: %d\n", status);
523 if (actor != NULL)
524 printf("actor: %s\n", actor);
525 if (uid != NULL)
526 printf("uid: %s\n", uid);
527
528 return 0;
529 }
530
531 if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
532 xs *msg = msg_admiration(&snac, url, "Announce");
533
534 if (msg != NULL) {
535 enqueue_message(&snac, msg);
536 timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, "");
537
538 if (dbglevel) {
539 xs_json_dump(msg, 4, stdout);
540 }
541 }
542
543 return 0;
544 }
545
546
547 if (strcmp(cmd, "assist") == 0) { /** **/
548 /* undocumented: experimental (do not use) */
549 xs *msg = msg_admiration(&snac, url, "Accept");
550
551 if (msg != NULL) {
552 enqueue_message(&snac, msg);
553
554 if (dbglevel) {
555 xs_json_dump(msg, 4, stdout);
556 }
557 }
558
559 return 0;
560 }
561
562 if (strcmp(cmd, "unboost") == 0) { /** **/
563 xs *msg = msg_repulsion(&snac, url, "Announce");
564
565 if (msg != NULL) {
566 enqueue_message(&snac, msg);
567
568 if (dbglevel) {
569 xs_json_dump(msg, 4, stdout);
570 }
571 }
572
573 return 0;
574 }
575
576 if (strcmp(cmd, "follow") == 0) { /** **/
577 xs *msg = msg_follow(&snac, url);
578
579 if (msg != NULL) {
580 const char *actor = xs_dict_get(msg, "object");
581
582 following_add(&snac, actor, msg);
583
584 enqueue_output_by_actor(&snac, msg, actor, 0);
585
586 if (dbglevel) {
587 xs_json_dump(msg, 4, stdout);
588 }
589 }
590
591 return 0;
592 }
593
594 if (strcmp(cmd, "unfollow") == 0) { /** **/
595 xs *object = NULL;
596
597 if (valid_status(following_get(&snac, url, &object))) {
598 xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
599
600 following_del(&snac, url);
601
602 enqueue_output_by_actor(&snac, msg, url, 0);
603
604 snac_log(&snac, xs_fmt("unfollowed actor %s", url));
605 }
606 else
607 snac_log(&snac, xs_fmt("actor is not being followed %s", url));
608
609 return 0;
610 }
611
612 if (strcmp(cmd, "limit") == 0) { /** **/
613 int ret;
614
615 if (!following_check(&snac, url))
616 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
617 else
618 if ((ret = limit(&snac, url)) == 0)
619 snac_log(&snac, xs_fmt("actor %s is now limited", url));
620 else
621 snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
622
623 return 0;
624 }
625
626 if (strcmp(cmd, "unlimit") == 0) { /** **/
627 int ret;
628
629 if (!following_check(&snac, url))
630 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
631 else
632 if ((ret = unlimit(&snac, url)) == 0)
633 snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
634 else
635 snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
636
637 return 0;
638 }
639
640 if (strcmp(cmd, "unmute") == 0) { /** **/
641 if (is_muted(&snac, url)) {
642 unmute(&snac, url);
643
644 printf("%s unmuted\n", url);
645 }
646 else
647 printf("%s actor is not muted\n", url);
648
649 return 0;
650 }
651
652 if (strcmp(cmd, "search") == 0) { /** **/
653 int to;
654
655 /* 'url' contains the regex */
656 xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
657
658 int c = 0;
659 const char *v;
660
661 /* print results as standalone links */
662 while (xs_list_next(r, &v, &c)) {
663 printf("%s/admin/p/%s\n", snac.actor, v);
664 }
665
666 return 0;
667 }
668
669 if (strcmp(cmd, "ping") == 0) { /** **/
670 xs *actor_o = NULL;
671
672 if (!xs_startswith(url, "https:/")) {
673 /* try to resolve via webfinger */
674 if (!valid_status(webfinger_request(url, &url, NULL))) {
675 srv_log(xs_fmt("cannot resolve %s via webfinger", url));
676 return 1;
677 }
678 }
679
680 if (valid_status(actor_request(&snac, url, &actor_o))) {
681 xs *msg = msg_ping(&snac, url);
682
683 enqueue_output_by_actor(&snac, msg, url, 0);
684
685 if (dbglevel) {
686 xs_json_dump(msg, 4, stdout);
687 }
688
689 srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
690 }
691 else {
692 srv_log(xs_fmt("Error getting actor %s", url));
693 return 1;
694 }
695
696 return 0;
697 }
698
699 if (strcmp(cmd, "pin") == 0) { /** **/
700 int ret = pin(&snac, url);
701 if (ret < 0) {
702 fprintf(stderr, "error pinning %s %d\n", url, ret);
703 return 1;
704 }
705
706 return 0;
707 }
708
709 if (strcmp(cmd, "unpin") == 0) { /** **/
710 int ret = unpin(&snac, url);
711 if (ret < 0) {
712 fprintf(stderr, "error unpinning %s %d\n", url, ret);
713 return 1;
714 }
715
716 return 0;
717 }
718
719 if (strcmp(cmd, "bookmark") == 0) { /** **/
720 int ret = bookmark(&snac, url);
721 if (ret < 0) {
722 fprintf(stderr, "error bookmarking %s %d\n", url, ret);
723 return 1;
724 }
725
726 return 0;
727 }
728
729 if (strcmp(cmd, "unbookmark") == 0) { /** **/
730 int ret = unbookmark(&snac, url);
731 if (ret < 0) {
732 fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
733 return 1;
734 }
735
736 return 0;
737 }
738
739 if (strcmp(cmd, "question") == 0) { /** **/
740 int end_secs = 5 * 60;
741 xs *opts = xs_split(url, ";");
742
743 xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
744 xs *c_msg = msg_create(&snac, msg);
745
746 if (dbglevel) {
747 xs_json_dump(c_msg, 4, stdout);
748 }
749
750 enqueue_message(&snac, c_msg);
751 enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
752
753 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
754
755 return 0;
756 }
757
758 if (strcmp(cmd, "request") == 0) { /** **/
759 int status;
760 xs *data = NULL;
761
762 status = activitypub_request(&snac, url, &data);
763
764 printf("status: %d\n", status);
765
766 if (data != NULL) {
767 xs_json_dump(data, 4, stdout);
768 }
769
770 return 0;
771 }
772
773 if (strcmp(cmd, "collect_replies") == 0) { /** **/
774 enqueue_collect_replies(&snac, url);
775
776 return 0;
777 }
778
779 if (strcmp(cmd, "collect_outbox") == 0) { /** **/
780 enqueue_collect_outbox(&snac, url);
781 return 0;
782 }
783
784 if (strcmp(cmd, "insert") == 0) { /** **/
785 int status;
786 xs *data = NULL;
787
788 status = activitypub_request(&snac, url, &data);
789
790 printf("status: %d\n", status);
791
792 if (data != NULL) {
793 xs_json_dump(data, 4, stdout);
794 enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
795
796 if (!timeline_here(&snac, url))
797 timeline_add(&snac, url, data);
798 else
799 printf("Post %s already here\n", url);
800 }
801
802 return 0;
803 }
804
805 if (strcmp(cmd, "request2") == 0) { /** **/
806 enqueue_object_request(&snac, url, 2);
807
808 return 0;
809 }
810
811 if (strcmp(cmd, "actor") == 0) { /** **/
812 int status;
813 xs *data = NULL;
814
815 status = actor_request(&snac, url, &data);
816
817 printf("status: %d\n", status);
818
819 if (valid_status(status)) {
820 xs_json_dump(data, 4, stdout);
821 }
822
823 return 0;
824 }
825
826 if (strcmp(cmd, "import_list") == 0) { /** **/
827 import_list_csv(&snac, url);
828
829 return 0;
830 }
831
832 if (strcmp(cmd, "import_block_list") == 0) { /** **/
833 import_blocked_accounts_csv(&snac, url);
834
835 return 0;
836 }
837
838 if (strcmp(cmd, "note") == 0 || /** **/
839 strcmp(cmd, "note_unlisted") == 0 || /** **/
840 strcmp(cmd, "note_mention") == 0 || /** **/
841 strcmp(cmd, "note_followers") == 0) { /** **/
842 xs *content = NULL;
843 xs *msg = NULL;
844 xs *c_msg = NULL;
845 xs *attl = xs_list_new();
846 const char *fn = NULL;
847 const char *in_reply_to = NULL;
848 const char **arg_irt = NULL;
849 int arg_date = 0;
850 xs *post_date = NULL;
851
852 /* iterate possible attachments */
853 while ((fn = GET_ARGV())) {
854 FILE *f;
855
856 if (arg_irt) {
857 *arg_irt = fn;
858 arg_irt = NULL;
859 }
860 else
861 if (arg_date) {
862 /* convert to ISO */
863 time_t t = xs_parse_localtime(fn, "%Y%m%d%H%M%S");
864
865 if (t == 0) {
866 fprintf(stderr, "Invalid scheduled date format (must be YYYYmmddHHMMSS)\n");
867 return 1;
868 }
869
870 post_date = xs_str_iso_date(t);
871 arg_date = 0;
872 }
873 else
874 if (strcmp(fn, "-r") == 0) {
875 /* next argument is an inReplyTo */
876 arg_irt = &in_reply_to;
877 }
878 else
879 if (strcmp(fn, "-d") == 0) {
880 /* next argument is the schedule date */
881 arg_date = 1;
882 }
883 else
884 if ((f = fopen(fn, "rb")) != NULL) {
885 /* get the file size and content */
886 fseek(f, 0, SEEK_END);
887 int sz = ftell(f);
888 fseek(f, 0, SEEK_SET);
889 xs *atc = xs_readall(f);
890 fclose(f);
891
892 char *ext = strrchr(fn, '.');
893 char rnd[32];
894 xs_rnd_buf(rnd, sizeof(rnd));
895 xs *hash = xs_md5_hex(rnd, sizeof(rnd));
896 xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
897 xs *url = xs_fmt("%s/s/%s", snac.actor, id);
898
899 /* store */
900 static_put(&snac, id, atc, sz);
901
902 xs *l = xs_list_new();
903
904 l = xs_list_append(l, url);
905 l = xs_list_append(l, ""); /* alt text */
906
907 attl = xs_list_append(attl, l);
908 }
909 else
910 fprintf(stderr, "Error opening '%s' as attachment\n", fn);
911 }
912
913 if (strcmp(url, "-e") == 0) {
914 /* get the content from an editor */
915#define EDITOR "$EDITOR "
916 char cmd[] = EDITOR "/tmp/snac-XXXXXX";
917 FILE *f;
918 int fd = mkstemp(cmd + strlen(EDITOR));
919
920 if (fd >= 0) {
921 int status = system(cmd);
922
923 if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) {
924 content = xs_readall(f);
925 fclose(f);
926 unlink(cmd + strlen(EDITOR));
927 } else {
928 printf("Nothing to send\n");
929 close(fd);
930 return 1;
931 }
932 } else {
933 fprintf(stderr, "Temp file creation failed\n");
934 return 1;
935 }
936 }
937 else
938 if (strcmp(url, "-") == 0) {
939 /* get the content from stdin */
940 content = xs_readall(stdin);
941 }
942 else
943 content = xs_dup(url);
944
945 if (!content || !*content) {
946 fprintf(stderr, "Nothing to send\n");
947 return 1;
948 }
949
950 int scope = SCOPE_PUBLIC;
951 if (strcmp(cmd, "note_mention") == 0)
952 scope = SCOPE_MENTIONED;
953 else
954 if (strcmp(cmd, "note_unlisted") == 0)
955 scope = SCOPE_UNLISTED;
956 else
957 if (strcmp(cmd, "note_followers") == 0)
958 scope = SCOPE_FOLLOWERS;
959
960 msg = msg_note(&snac, content, NULL, in_reply_to, attl, scope, getenv("LANG"), post_date);
961
962 const char *id = xs_dict_get(msg, "id");
963
964 if (post_date)
965 schedule_add(&snac, id, msg);
966 else {
967 c_msg = msg_create(&snac, msg);
968
969 if (dbglevel) {
970 xs_json_dump(c_msg, 4, stdout);
971 }
972
973 enqueue_message(&snac, c_msg);
974 enqueue_webmention(msg);
975
976 timeline_add(&snac, id, msg);
977 }
978
979 return 0;
980 }
981
982 fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
983
984 return 1;
985}