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