2.61
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("actor {basedir} [{uid}] {url} Requests an actor\n");
34 printf("note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n");
35 printf("boost|announce {basedir} {uid} {url} Boosts (announces) a post\n");
36 printf("unboost {basedir} {uid} {url} Unboosts a post\n");
37 printf("resetpwd {basedir} {uid} Resets the password of a user\n");
38 printf("ping {basedir} {uid} {actor} Pings an actor\n");
39 printf("webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n");
40 printf("pin {basedir} {uid} {msg_url} Pins a message\n");
41 printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
42 printf("bookmark {basedir} {uid} {msg_url} Bookmarks a message\n");
43 printf("unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n");
44 printf("block {basedir} {instance_url} Blocks a full instance\n");
45 printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
46 printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
47 printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
48 printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n");
49 printf("search {basedir} {uid} {regex} Searches posts by content\n");
50 printf("export_csv {basedir} {uid} Exports data as CSV files into current directory\n");
51 printf("alias {basedir} {uid} {account} Sets account (@user@host or actor url) as an alias\n");
52 printf("migrate {basedir} {uid} Migrates to the account defined as the alias\n");
53 printf("import_csv {basedir} {uid} Imports data from CSV files into current directory\n");
54
55 return 1;
56}
57
58
59char *get_argv(int *argi, int argc, char *argv[])
60{
61 if (*argi < argc)
62 return argv[(*argi)++];
63 else
64 return NULL;
65}
66
67
68#define GET_ARGV() get_argv(&argi, argc, argv)
69
70int main(int argc, char *argv[])
71{
72 char *cmd;
73 char *basedir;
74 char *user;
75 char *url;
76 int argi = 1;
77 snac snac;
78
79 /* ensure group has write access */
80 umask(0007);
81
82 if ((cmd = GET_ARGV()) == NULL)
83 return usage();
84
85 if (strcmp(cmd, "init") == 0) { /** **/
86 /* initialize the data storage */
87 /* ... */
88 basedir = GET_ARGV();
89
90 return snac_init(basedir);
91 }
92
93 if (strcmp(cmd, "upgrade") == 0) { /** **/
94 int ret;
95
96 /* upgrade */
97 if ((basedir = GET_ARGV()) == NULL)
98 return usage();
99
100 if ((ret = srv_open(basedir, 1)) == 1)
101 srv_log(xs_dup("OK"));
102
103 return ret;
104 }
105
106 if (strcmp(cmd, "markdown") == 0) { /** **/
107 /* undocumented, for testing only */
108 xs *c = xs_readall(stdin);
109 xs *fc = not_really_markdown(c, NULL, NULL);
110
111 printf("<html>\n%s\n</html>\n", fc);
112 return 0;
113 }
114
115 if ((basedir = GET_ARGV()) == NULL)
116 return usage();
117
118 if (!srv_open(basedir, 0)) {
119 srv_log(xs_fmt("error opening data storage at %s", basedir));
120 return 1;
121 }
122
123 if (strcmp(cmd, "adduser") == 0) { /** **/
124 user = GET_ARGV();
125
126 return adduser(user);
127
128 return 0;
129 }
130
131 if (strcmp(cmd, "httpd") == 0) { /** **/
132 httpd();
133 srv_free();
134 return 0;
135 }
136
137 if (strcmp(cmd, "purge") == 0) { /** **/
138 purge_all();
139 return 0;
140 }
141
142 if (strcmp(cmd, "state") == 0) { /** **/
143 xs *shm_name = NULL;
144 srv_state *p_state = srv_state_op(&shm_name, 1);
145
146 if (p_state == NULL)
147 return 1;
148
149 srv_state ss = *p_state;
150 int n;
151
152 printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
153 xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
154 printf("uptime: %s\n", uptime);
155 printf("job fifo size (cur): %d\n", ss.job_fifo_size);
156 printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
157 char *th_states[] = { "stopped", "waiting", "input", "output" };
158
159 for (n = 0; n < ss.n_threads; n++)
160 printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
161
162 return 0;
163 }
164
165 if ((user = GET_ARGV()) == NULL)
166 return usage();
167
168 if (strcmp(cmd, "block") == 0) { /** **/
169 int ret = instance_block(user);
170
171 if (ret < 0) {
172 fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
173 return 1;
174 }
175
176 return 0;
177 }
178
179 if (strcmp(cmd, "unblock") == 0) { /** **/
180 int ret = instance_unblock(user);
181
182 if (ret < 0) {
183 fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
184 return 1;
185 }
186
187 return 0;
188 }
189
190 if (strcmp(cmd, "webfinger") == 0) { /** **/
191 xs *actor = NULL;
192 xs *uid = NULL;
193 int status;
194
195 status = webfinger_request(user, &actor, &uid);
196
197 printf("status: %d\n", status);
198 if (actor != NULL)
199 printf("actor: %s\n", actor);
200 if (uid != NULL)
201 printf("uid: %s\n", uid);
202
203 return 0;
204 }
205
206 if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
207 /* query an actor without user (non-signed) */
208 xs *actor = NULL;
209 int status;
210
211 status = actor_request(NULL, user, &actor);
212
213 printf("status: %d\n", status);
214 if (valid_status(status)) {
215 xs_json_dump(actor, 4, stdout);
216 printf("\n");
217 }
218
219 return 0;
220 }
221
222 if (!user_open(&snac, user)) {
223 printf("invalid user '%s'\n", user);
224 return 1;
225 }
226
227 lastlog_write(&snac, "cmdline");
228
229 if (strcmp(cmd, "resetpwd") == 0) { /** **/
230 return resetpwd(&snac);
231 }
232
233 if (strcmp(cmd, "deluser") == 0) { /** **/
234 return deluser(&snac);
235 }
236
237 if (strcmp(cmd, "update") == 0) { /** **/
238 xs *a_msg = msg_actor(&snac);
239 xs *u_msg = msg_update(&snac, a_msg);
240 enqueue_message(&snac, u_msg);
241 return 0;
242 }
243
244 if (strcmp(cmd, "queue") == 0) { /** **/
245 process_user_queue(&snac);
246 return 0;
247 }
248
249 if (strcmp(cmd, "verify_links") == 0) { /** **/
250 verify_links(&snac);
251 return 0;
252 }
253
254 if (strcmp(cmd, "timeline") == 0) { /** **/
255#if 0
256 xs *list = local_list(&snac, XS_ALL);
257 xs *body = html_timeline(&snac, list, 1);
258
259 printf("%s\n", body);
260 user_free(&snac);
261 srv_free();
262#endif
263
264 xs *idx = xs_fmt("%s/private.idx", snac.basedir);
265 xs *list = index_list_desc(idx, 0, 256);
266 xs *tl = timeline_top_level(&snac, list);
267
268 xs_json_dump(tl, 4, stdout);
269
270 return 0;
271 }
272
273 if (strcmp(cmd, "export_csv") == 0) { /** **/
274 export_csv(&snac);
275 return 0;
276 }
277
278 if (strcmp(cmd, "import_csv") == 0) { /** **/
279 import_csv(&snac);
280 return 0;
281 }
282
283 if (strcmp(cmd, "migrate") == 0) { /** **/
284 return migrate_account(&snac);
285 }
286
287 if ((url = GET_ARGV()) == NULL)
288 return usage();
289
290 if (strcmp(cmd, "alias") == 0) { /** **/
291 xs *actor = NULL;
292 xs *uid = NULL;
293 int status = HTTP_STATUS_OK;
294
295 if (*url == '\0')
296 actor = xs_dup("");
297 else
298 status = webfinger_request(url, &actor, &uid);
299
300 if (valid_status(status)) {
301 if (strcmp(actor, snac.actor) == 0) {
302 snac_log(&snac, xs_fmt("You can't be your own alias"));
303 return 1;
304 }
305 else {
306 snac.config = xs_dict_set(snac.config, "alias", actor);
307 snac.config = xs_dict_set(snac.config, "alias_raw", url);
308
309 user_persist(&snac, 1);
310 }
311 }
312 else {
313 snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
314 return 1;
315 }
316
317 return 0;
318 }
319
320 if (strcmp(cmd, "webfinger_s") == 0) { /** **/
321 xs *actor = NULL;
322 xs *uid = NULL;
323 int status;
324
325 status = webfinger_request_signed(&snac, url, &actor, &uid);
326
327 printf("status: %d\n", status);
328 if (actor != NULL)
329 printf("actor: %s\n", actor);
330 if (uid != NULL)
331 printf("uid: %s\n", uid);
332
333 return 0;
334 }
335
336 if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
337 xs *msg = msg_admiration(&snac, url, "Announce");
338
339 if (msg != NULL) {
340 enqueue_message(&snac, msg);
341
342 if (dbglevel) {
343 xs_json_dump(msg, 4, stdout);
344 }
345 }
346
347 return 0;
348 }
349
350 if (strcmp(cmd, "unboost") == 0) { /** **/
351 xs *msg = msg_repulsion(&snac, url, "Announce");
352
353 if (msg != NULL) {
354 enqueue_message(&snac, msg);
355
356 if (dbglevel) {
357 xs_json_dump(msg, 4, stdout);
358 }
359 }
360
361 return 0;
362 }
363
364 if (strcmp(cmd, "follow") == 0) { /** **/
365 xs *msg = msg_follow(&snac, url);
366
367 if (msg != NULL) {
368 const char *actor = xs_dict_get(msg, "object");
369
370 following_add(&snac, actor, msg);
371
372 enqueue_output_by_actor(&snac, msg, actor, 0);
373
374 if (dbglevel) {
375 xs_json_dump(msg, 4, stdout);
376 }
377 }
378
379 return 0;
380 }
381
382 if (strcmp(cmd, "unfollow") == 0) { /** **/
383 xs *object = NULL;
384
385 if (valid_status(following_get(&snac, url, &object))) {
386 xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
387
388 following_del(&snac, url);
389
390 enqueue_output_by_actor(&snac, msg, url, 0);
391
392 snac_log(&snac, xs_fmt("unfollowed actor %s", url));
393 }
394 else
395 snac_log(&snac, xs_fmt("actor is not being followed %s", url));
396
397 return 0;
398 }
399
400 if (strcmp(cmd, "limit") == 0) { /** **/
401 int ret;
402
403 if (!following_check(&snac, url))
404 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
405 else
406 if ((ret = limit(&snac, url)) == 0)
407 snac_log(&snac, xs_fmt("actor %s is now limited", url));
408 else
409 snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
410
411 return 0;
412 }
413
414 if (strcmp(cmd, "unlimit") == 0) { /** **/
415 int ret;
416
417 if (!following_check(&snac, url))
418 snac_log(&snac, xs_fmt("actor %s is not being followed", url));
419 else
420 if ((ret = unlimit(&snac, url)) == 0)
421 snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
422 else
423 snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
424
425 return 0;
426 }
427
428 if (strcmp(cmd, "search") == 0) { /** **/
429 int to;
430
431 /* 'url' contains the regex */
432 xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
433
434 int c = 0;
435 const char *v;
436
437 /* print results as standalone links */
438 while (xs_list_next(r, &v, &c)) {
439 printf("%s/admin/p/%s\n", snac.actor, v);
440 }
441
442 return 0;
443 }
444
445 if (strcmp(cmd, "ping") == 0) { /** **/
446 xs *actor_o = NULL;
447
448 if (!xs_startswith(url, "https:/")) {
449 /* try to resolve via webfinger */
450 if (!valid_status(webfinger_request(url, &url, NULL))) {
451 srv_log(xs_fmt("cannot resolve %s via webfinger", url));
452 return 1;
453 }
454 }
455
456 if (valid_status(actor_request(&snac, url, &actor_o))) {
457 xs *msg = msg_ping(&snac, url);
458
459 enqueue_output_by_actor(&snac, msg, url, 0);
460
461 if (dbglevel) {
462 xs_json_dump(msg, 4, stdout);
463 }
464
465 srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
466 }
467 else {
468 srv_log(xs_fmt("Error getting actor %s", url));
469 return 1;
470 }
471
472 return 0;
473 }
474
475 if (strcmp(cmd, "pin") == 0) { /** **/
476 int ret = pin(&snac, url);
477 if (ret < 0) {
478 fprintf(stderr, "error pinning %s %d\n", url, ret);
479 return 1;
480 }
481
482 return 0;
483 }
484
485 if (strcmp(cmd, "unpin") == 0) { /** **/
486 int ret = unpin(&snac, url);
487 if (ret < 0) {
488 fprintf(stderr, "error unpinning %s %d\n", url, ret);
489 return 1;
490 }
491
492 return 0;
493 }
494
495 if (strcmp(cmd, "bookmark") == 0) { /** **/
496 int ret = bookmark(&snac, url);
497 if (ret < 0) {
498 fprintf(stderr, "error bookmarking %s %d\n", url, ret);
499 return 1;
500 }
501
502 return 0;
503 }
504
505 if (strcmp(cmd, "unbookmark") == 0) { /** **/
506 int ret = unbookmark(&snac, url);
507 if (ret < 0) {
508 fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
509 return 1;
510 }
511
512 return 0;
513 }
514
515 if (strcmp(cmd, "question") == 0) { /** **/
516 int end_secs = 5 * 60;
517 xs *opts = xs_split(url, ";");
518
519 xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
520 xs *c_msg = msg_create(&snac, msg);
521
522 if (dbglevel) {
523 xs_json_dump(c_msg, 4, stdout);
524 }
525
526 enqueue_message(&snac, c_msg);
527 enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
528
529 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
530
531 return 0;
532 }
533
534 if (strcmp(cmd, "request") == 0) { /** **/
535 int status;
536 xs *data = NULL;
537
538 status = activitypub_request(&snac, url, &data);
539
540 printf("status: %d\n", status);
541
542 if (data != NULL) {
543 xs_json_dump(data, 4, stdout);
544 }
545
546 return 0;
547 }
548
549 if (strcmp(cmd, "request2") == 0) { /** **/
550 enqueue_object_request(&snac, url, 2);
551
552 return 0;
553 }
554
555 if (strcmp(cmd, "actor") == 0) { /** **/
556 int status;
557 xs *data = NULL;
558
559 status = actor_request(&snac, url, &data);
560
561 printf("status: %d\n", status);
562
563 if (valid_status(status)) {
564 xs_json_dump(data, 4, stdout);
565 }
566
567 return 0;
568 }
569
570 if (strcmp(cmd, "note") == 0) { /** **/
571 xs *content = NULL;
572 xs *msg = NULL;
573 xs *c_msg = NULL;
574 xs *attl = xs_list_new();
575 char *fn = NULL;
576
577 /* iterate possible attachments */
578 while ((fn = GET_ARGV())) {
579 FILE *f;
580
581 if ((f = fopen(fn, "rb")) != NULL) {
582 /* get the file size and content */
583 fseek(f, 0, SEEK_END);
584 int sz = ftell(f);
585 fseek(f, 0, SEEK_SET);
586 xs *atc = xs_readall(f);
587 fclose(f);
588
589 char *ext = strrchr(fn, '.');
590 xs *hash = xs_md5_hex(fn, strlen(fn));
591 xs *id = xs_fmt("%s%s", hash, ext);
592 xs *url = xs_fmt("%s/s/%s", snac.actor, id);
593
594 /* store */
595 static_put(&snac, id, atc, sz);
596
597 xs *l = xs_list_new();
598
599 l = xs_list_append(l, url);
600 l = xs_list_append(l, ""); /* alt text */
601
602 attl = xs_list_append(attl, l);
603 }
604 else
605 fprintf(stderr, "Error opening '%s' as attachment\n", fn);
606 }
607
608 if (strcmp(url, "-e") == 0) {
609 /* get the content from an editor */
610 FILE *f;
611
612 unlink("/tmp/snac-edit.txt");
613 system("$EDITOR /tmp/snac-edit.txt");
614
615 if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
616 content = xs_readall(f);
617 fclose(f);
618
619 unlink("/tmp/snac-edit.txt");
620 }
621 else {
622 printf("Nothing to send\n");
623 return 1;
624 }
625 }
626 else
627 if (strcmp(url, "-") == 0) {
628 /* get the content from stdin */
629 content = xs_readall(stdin);
630 }
631 else
632 content = xs_dup(url);
633
634 msg = msg_note(&snac, content, NULL, NULL, attl, 0);
635
636 c_msg = msg_create(&snac, msg);
637
638 if (dbglevel) {
639 xs_json_dump(c_msg, 4, stdout);
640 }
641
642 enqueue_message(&snac, c_msg);
643
644 timeline_add(&snac, xs_dict_get(msg, "id"), msg);
645
646 return 0;
647 }
648
649 fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
650
651 return 1;
652}