2.68
main.c
1/* snac - A simple, minimalistic ActivityPub instance */
2/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
3
4#include "xs.h"
5#include "xs_io.h"
6#include "xs_json.h"
7#include "xs_time.h"
8#include "xs_openssl.h"
9
10#include "snac.h"
11
12#include <sys/stat.h>
13
14int usage(void)
15{
16 printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
17 printf("Copyright (c) 2022 - 2024 grunfink et al. / MIT license\n");
18 printf("\n");
19 printf("Commands:\n");
20 printf("\n");
21 printf("init [{basedir}] Initializes the data storage\n");
22 printf("upgrade {basedir} Upgrade to a new version\n");
23 printf("adduser {basedir} [{uid}] Adds a new user\n");
24 printf("deluser {basedir} {uid} Deletes a user\n");
25 printf("httpd {basedir} Starts the HTTPD daemon\n");
26 printf("purge {basedir} Purges old data\n");
27 printf("state {basedir} Prints server state\n");
28 printf("webfinger {basedir} {account} Queries about an account (@user@host or actor url)\n");
29 printf("queue {basedir} {uid} Processes a user queue\n");
30 printf("follow {basedir} {uid} {actor} Follows an actor\n");
31 printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
32 printf("request {basedir} {uid} {url} Requests an object\n");
33 printf("insert {basedir} {uid} {url} Requests an object and inserts it into the timeline\n");
34 printf("actor {basedir} [{uid}] {url} Requests an actor\n");
35 printf("note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n");
36 printf("note_unlisted {basedir} {uid} {text} [files...] Sends an unlisted note with optional attachments\n");
37 printf("boost|announce {basedir} {uid} {url} Boosts (announces) a post\n");
38 printf("unboost {basedir} {uid} {url} Unboosts a post\n");
39 printf("resetpwd {basedir} {uid} Resets the password of a user\n");
40 printf("ping {basedir} {uid} {actor} Pings an actor\n");
41 printf("webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n");
42 printf("pin {basedir} {uid} {msg_url} Pins a message\n");
43 printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
44 printf("bookmark {basedir} {uid} {msg_url} Bookmarks a message\n");
45 printf("unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n");
46 printf("block {basedir} {instance_url} Blocks a full instance\n");
47 printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
48 printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
49 printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
50 printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n");
51 printf("search {basedir} {uid} {regex} Searches posts by content\n");
52 printf("export_csv {basedir} {uid} Exports data as CSV files into current directory\n");
53 printf("alias {basedir} {uid} {account} Sets account (@user@host or actor url) as an alias\n");
54 printf("migrate {basedir} {uid} Migrates to the account defined as the alias\n");
55 printf("import_csv {basedir} {uid} Imports data from CSV files in the current directory\n");
56 printf("import_list {basedir} {uid} {file} Imports a Mastodon CSV list file\n");
57 printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n");
58
59 return 1;
60}
61
62
63char *get_argv(int *argi, int argc, char *argv[])
64{
65 if (*argi < argc)
66 return argv[(*argi)++];
67 else
68 return NULL;
69}
70
71
72#define GET_ARGV() get_argv(&argi, argc, argv)
73
74int main(int argc, char *argv[])
75{
76 char *cmd;
77 char *basedir;
78 char *user;
79 char *url;
80 int argi = 1;
81 snac snac;
82
83 /* ensure group has write access */
84 umask(0007);
85
86 if ((cmd = GET_ARGV()) == NULL)
87 return usage();
88
89 if (strcmp(cmd, "init") == 0) { /** **/
90 /* initialize the data storage */
91 /* ... */
92 basedir = GET_ARGV();
93
94 return snac_init(basedir);
95 }
96
97 if (strcmp(cmd, "upgrade") == 0) { /** **/
98 int ret;
99
100 /* upgrade */
101 if ((basedir = GET_ARGV()) == NULL)
102 return usage();
103
104 if ((ret = srv_open(basedir, 1)) == 1)
105 srv_log(xs_dup("OK"));
106
107 return ret;
108 }
109
110 if (strcmp(cmd, "markdown") == 0) { /** **/
111 /* undocumented, for testing only */
112 xs *c = xs_readall(stdin);
113 xs *fc = not_really_markdown(c, NULL, NULL);
114
115 printf("<html>\n%s\n</html>\n", fc);
116 return 0;
117 }
118
119 if ((basedir = GET_ARGV()) == NULL)
120 return usage();
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 ((url = GET_ARGV()) == NULL)
292 return usage();
293
294 if (strcmp(cmd, "alias") == 0) { /** **/
295 xs *actor = NULL;
296 xs *uid = NULL;
297 int status = HTTP_STATUS_OK;
298
299 if (*url == '\0')
300 actor = xs_dup("");
301 else
302 status = webfinger_request(url, &actor, &uid);
303
304 if (valid_status(status)) {
305 if (strcmp(actor, snac.actor) == 0) {
306 snac_log(&snac, xs_fmt("You can't be your own alias"));
307 return 1;
308 }
309 else {
310 snac.config = xs_dict_set(snac.config, "alias", actor);
311 snac.config = xs_dict_set(snac.config, "alias_raw", url);
312
313 user_persist(&snac, 1);
314 }
315 }
316 else {
317 snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
318 return 1;
319 }
320
321 return 0;
322 }
323
324 if (strcmp(cmd, "webfinger_s") == 0) { /** **/
325 xs *actor = NULL;
326 xs *uid = NULL;
327 int status;
328
329 status = webfinger_request_signed(&snac, url, &actor, &uid);
330
331 printf("status: %d\n", status);
332 if (actor != NULL)
333 printf("actor: %s\n", actor);
334 if (uid != NULL)
335 printf("uid: %s\n", uid);
336
337 return 0;
338 }
339
340 if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
341 xs *msg = msg_admiration(&snac, url, "Announce");
342
343 if (msg != NULL) {
344 enqueue_message(&snac, msg);
345
346 if (dbglevel) {
347 xs_json_dump(msg, 4, stdout);
348 }
349 }
350
351 return 0;
352 }
353
354 if (strcmp(cmd, "unboost") == 0) { /** **/
355 xs *msg = msg_repulsion(&snac, url, "Announce");
356
357 if (msg != NULL) {
358 enqueue_message(&snac, msg);
359
360 if (dbglevel) {
361 xs_json_dump(msg, 4, stdout);
362 }
363 }
364
365 return 0;
366 }
367
368 if (strcmp(cmd, "follow") == 0) { /** **/
369 xs *msg = msg_follow(&snac, url);
370
371 if (msg != NULL) {
372 const char *actor = xs_dict_get(msg, "object");
373
374 following_add(&snac, actor, msg);
375
376 enqueue_output_by_actor(&snac, msg, actor, 0);
377
378 if (dbglevel) {
379 xs_json_dump(msg, 4, stdout);
380 }
381 }
382
383 return 0;
384 }
385
386 if (strcmp(cmd, "unfollow") == 0) { /** **/
387 xs *object = NULL;
388
389 if (valid_status(following_get(&snac, url, &object))) {
390 xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
391
392 following_del(&snac, url);
393
394 enqueue_output_by_actor(&snac, msg, url, 0);
395
396 snac_log(&snac, xs_fmt("unfollowed actor %s", url));
397 }
398 else
399 snac_log(&snac, xs_fmt("actor is not being followed %s", url));
400
401 return 0;
402 }
403
404 if (strcmp(cmd, "limit") == 0) { /** **/
405 int ret;
406
407 if (!following_check(&snac, url))
408 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
409 else
410 if ((ret = limit(&snac, url)) == 0)
411 snac_log(&snac, xs_fmt("actor %s is now limited", url));
412 else
413 snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
414
415 return 0;
416 }
417
418 if (strcmp(cmd, "unlimit") == 0) { /** **/
419 int ret;
420
421 if (!following_check(&snac, url))
422 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
423 else
424 if ((ret = unlimit(&snac, url)) == 0)
425 snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
426 else
427 snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
428
429 return 0;
430 }
431
432 if (strcmp(cmd, "search") == 0) { /** **/
433 int to;
434
435 /* 'url' contains the regex */
436 xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
437
438 int c = 0;
439 const char *v;
440
441 /* print results as standalone links */
442 while (xs_list_next(r, &v, &c)) {
443 printf("%s/admin/p/%s\n", snac.actor, v);
444 }
445
446 return 0;
447 }
448
449 if (strcmp(cmd, "ping") == 0) { /** **/
450 xs *actor_o = NULL;
451
452 if (!xs_startswith(url, "https:/")) {
453 /* try to resolve via webfinger */
454 if (!valid_status(webfinger_request(url, &url, NULL))) {
455 srv_log(xs_fmt("cannot resolve %s via webfinger", url));
456 return 1;
457 }
458 }
459
460 if (valid_status(actor_request(&snac, url, &actor_o))) {
461 xs *msg = msg_ping(&snac, url);
462
463 enqueue_output_by_actor(&snac, msg, url, 0);
464
465 if (dbglevel) {
466 xs_json_dump(msg, 4, stdout);
467 }
468
469 srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
470 }
471 else {
472 srv_log(xs_fmt("Error getting actor %s", url));
473 return 1;
474 }
475
476 return 0;
477 }
478
479 if (strcmp(cmd, "pin") == 0) { /** **/
480 int ret = pin(&snac, url);
481 if (ret < 0) {
482 fprintf(stderr, "error pinning %s %d\n", url, ret);
483 return 1;
484 }
485
486 return 0;
487 }
488
489 if (strcmp(cmd, "unpin") == 0) { /** **/
490 int ret = unpin(&snac, url);
491 if (ret < 0) {
492 fprintf(stderr, "error unpinning %s %d\n", url, ret);
493 return 1;
494 }
495
496 return 0;
497 }
498
499 if (strcmp(cmd, "bookmark") == 0) { /** **/
500 int ret = bookmark(&snac, url);
501 if (ret < 0) {
502 fprintf(stderr, "error bookmarking %s %d\n", url, ret);
503 return 1;
504 }
505
506 return 0;
507 }
508
509 if (strcmp(cmd, "unbookmark") == 0) { /** **/
510 int ret = unbookmark(&snac, url);
511 if (ret < 0) {
512 fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
513 return 1;
514 }
515
516 return 0;
517 }
518
519 if (strcmp(cmd, "question") == 0) { /** **/
520 int end_secs = 5 * 60;
521 xs *opts = xs_split(url, ";");
522
523 xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
524 xs *c_msg = msg_create(&snac, msg);
525
526 if (dbglevel) {
527 xs_json_dump(c_msg, 4, stdout);
528 }
529
530 enqueue_message(&snac, c_msg);
531 enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
532
533 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
534
535 return 0;
536 }
537
538 if (strcmp(cmd, "request") == 0) { /** **/
539 int status;
540 xs *data = NULL;
541
542 status = activitypub_request(&snac, url, &data);
543
544 printf("status: %d\n", status);
545
546 if (data != NULL) {
547 xs_json_dump(data, 4, stdout);
548 }
549
550 return 0;
551 }
552
553 if (strcmp(cmd, "insert") == 0) { /** **/
554 int status;
555 xs *data = NULL;
556
557 status = activitypub_request(&snac, url, &data);
558
559 printf("status: %d\n", status);
560
561 if (data != NULL) {
562 xs_json_dump(data, 4, stdout);
563 enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
564
565 if (!timeline_here(&snac, url))
566 timeline_add(&snac, url, data);
567 else
568 printf("Post %s already here\n", url);
569 }
570
571 return 0;
572 }
573
574 if (strcmp(cmd, "request2") == 0) { /** **/
575 enqueue_object_request(&snac, url, 2);
576
577 return 0;
578 }
579
580 if (strcmp(cmd, "actor") == 0) { /** **/
581 int status;
582 xs *data = NULL;
583
584 status = actor_request(&snac, url, &data);
585
586 printf("status: %d\n", status);
587
588 if (valid_status(status)) {
589 xs_json_dump(data, 4, stdout);
590 }
591
592 return 0;
593 }
594
595 if (strcmp(cmd, "import_list") == 0) { /** **/
596 import_list_csv(&snac, url);
597
598 return 0;
599 }
600
601 if (strcmp(cmd, "import_block_list") == 0) { /** **/
602 import_blocked_accounts_csv(&snac, url);
603
604 return 0;
605 }
606
607 if (strcmp(cmd, "note") == 0 || strcmp(cmd, "note_unlisted") == 0) { /** **/
608 xs *content = NULL;
609 xs *msg = NULL;
610 xs *c_msg = NULL;
611 xs *attl = xs_list_new();
612 char *fn = NULL;
613
614 /* iterate possible attachments */
615 while ((fn = GET_ARGV())) {
616 FILE *f;
617
618 if ((f = fopen(fn, "rb")) != NULL) {
619 /* get the file size and content */
620 fseek(f, 0, SEEK_END);
621 int sz = ftell(f);
622 fseek(f, 0, SEEK_SET);
623 xs *atc = xs_readall(f);
624 fclose(f);
625
626 char *ext = strrchr(fn, '.');
627 xs *hash = xs_md5_hex(fn, strlen(fn));
628 xs *id = xs_fmt("%s%s", hash, ext);
629 xs *url = xs_fmt("%s/s/%s", snac.actor, id);
630
631 /* store */
632 static_put(&snac, id, atc, sz);
633
634 xs *l = xs_list_new();
635
636 l = xs_list_append(l, url);
637 l = xs_list_append(l, ""); /* alt text */
638
639 attl = xs_list_append(attl, l);
640 }
641 else
642 fprintf(stderr, "Error opening '%s' as attachment\n", fn);
643 }
644
645 if (strcmp(url, "-e") == 0) {
646 /* get the content from an editor */
647 FILE *f;
648
649 unlink("/tmp/snac-edit.txt");
650 system("$EDITOR /tmp/snac-edit.txt");
651
652 if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
653 content = xs_readall(f);
654 fclose(f);
655
656 unlink("/tmp/snac-edit.txt");
657 }
658 else {
659 printf("Nothing to send\n");
660 return 1;
661 }
662 }
663 else
664 if (strcmp(url, "-") == 0) {
665 /* get the content from stdin */
666 content = xs_readall(stdin);
667 }
668 else
669 content = xs_dup(url);
670
671 msg = msg_note(&snac, content, NULL, NULL, attl, 0, getenv("LANG"));
672
673 if (strcmp(cmd, "note_unlisted") == 0) {
674 /* according to Mastodon, "unlisted" posts (now called "quiet public")
675 has the public address as a cc instead of to, so toggle it */
676 xs *to = xs_dup(xs_dict_get(msg, "to"));
677 msg = xs_dict_set(msg, "cc", to);
678 msg = xs_dict_set(msg, "to", xs_stock(XSTYPE_LIST));
679 }
680
681 c_msg = msg_create(&snac, msg);
682
683 if (dbglevel) {
684 xs_json_dump(c_msg, 4, stdout);
685 }
686
687 enqueue_message(&snac, c_msg);
688
689 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
690
691 return 0;
692 }
693
694 fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
695
696 return 1;
697}