2.84
utils.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_random.h"
10#include "xs_glob.h"
11#include "xs_curl.h"
12#include "xs_regex.h"
13#include "xs_http.h"
14
15#include "snac.h"
16
17#include <sys/stat.h>
18#include <stdlib.h>
19
20static const char * const default_srv_config = "{"
21 "\"host\": \"\","
22 "\"prefix\": \"\","
23 "\"address\": \"127.0.0.1\","
24 "\"port\": 8001,"
25 "\"layout\": 0.0,"
26 "\"dbglevel\": 0,"
27 "\"queue_retry_minutes\": 2,"
28 "\"queue_retry_max\": 10,"
29 "\"queue_timeout\": 6,"
30 "\"queue_timeout_2\": 8,"
31 "\"cssurls\": [\"\"],"
32 "\"def_timeline_entries\": 50,"
33 "\"max_timeline_entries\": 50,"
34 "\"timeline_purge_days\": 120,"
35 "\"local_purge_days\": 0,"
36 "\"min_account_age\": 0,"
37 "\"admin_email\": \"\","
38 "\"admin_account\": \"\","
39 "\"title\": \"\","
40 "\"short_description\": \"\","
41 "\"short_description_raw\": false,"
42 "\"protocol\": \"https\","
43 "\"fastcgi\": false"
44 "}";
45
46static const char * const default_css =
47 "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n"
48 "pre { overflow-x: scroll; }\n"
49 "blockquote { font-style: italic; }\n"
50 ".snac-embedded-video, img { max-width: 100% }\n"
51 ".snac-origin { font-size: 85% }\n"
52 ".snac-score { float: right; font-size: 85% }\n"
53 ".snac-top-user { text-align: center; padding-bottom: 2em }\n"
54 ".snac-top-user-name { font-size: 200% }\n"
55 ".snac-top-user-id { font-size: 150% }\n"
56 ".snac-announcement { border: black 1px solid; padding: 0.5em }\n"
57 ".snac-avatar { float: left; height: 2.5em; width: 2.5em; padding: 0.25em }\n"
58 ".snac-author { font-size: 90%; text-decoration: none }\n"
59 ".snac-author-tag { font-size: 80% }\n"
60 ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n"
61 ".snac-top-controls { padding-bottom: 1.5em }\n"
62 ".snac-post { border-top: 1px solid #a0a0a0; padding-top: 0.5em; padding-bottom: 0.5em; }\n"
63 ".snac-children { padding-left: 1em; border-left: 1px solid #a0a0a0; }\n"
64 ".snac-thread-cont { border-top: 1px dashed #a0a0a0; }\n"
65 ".snac-textarea { font-family: inherit; width: 100% }\n"
66 ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n"
67 ".snac-btn-mute { float: right; margin-left: 0.5em }\n"
68 ".snac-btn-unmute { float: right; margin-left: 0.5em }\n"
69 ".snac-btn-follow { float: right; margin-left: 0.5em }\n"
70 ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n"
71 ".snac-btn-hide { float: right; margin-left: 0.5em }\n"
72 ".snac-btn-delete { float: right; margin-left: 0.5em }\n"
73 ".snac-btn-limit { float: right; margin-left: 0.5em }\n"
74 ".snac-btn-unlimit { float: right; margin-left: 0.5em }\n"
75 ".snac-footer { margin-top: 2em; font-size: 75% }\n"
76 ".snac-poll-result { margin-left: auto; margin-right: auto; }\n"
77 ".snac-list-of-lists { padding-left: 0; }\n"
78 ".snac-list-of-lists li { display: inline; border: 1px solid #a0a0a0; border-radius: 25px;\n"
79 " margin-right: 0.5em; padding-left: 0.5em; padding-right: 0.5em; }\n"
80 ".snac-no-more-unseen-posts { border-top: 1px solid #a0a0a0; border-bottom: 1px solid #a0a0a0; padding: 0.5em 0; margin: 1em 0; }\n"
81 "@media (prefers-color-scheme: dark) { \n"
82 " body, input, textarea { background-color: #000; color: #fff; }\n"
83 " a { color: #7799dd }\n"
84 " a:visited { color: #aa99dd }\n"
85 "}\n"
86;
87
88const char *snac_blurb =
89 "<p><b>%host%</b> is a <a href=\"https:/"
90 "/en.wikipedia.org/wiki/Fediverse\">Fediverse</a> "
91 "instance that uses the <a href=\"https:/"
92 "/en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> "
93 "protocol. In other words, users at this host can communicate with people "
94 "that use software like Mastodon, Pleroma, Friendica, etc. "
95 "all around the world.</p>\n"
96 "<p>This server runs the "
97 "<a href=\"" WHAT_IS_SNAC_URL "\">snac</a> software and there is no "
98 "automatic sign-up process.</p>\n"
99;
100
101static const char * const greeting_html =
102 "<!DOCTYPE html>\n"
103 "<html><head>\n"
104 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
105 "<link rel=\"icon\" type=\"image/x-icon\" href=\"https://%host%/favicon.ico\"/>\n"
106 "<style>*{color-scheme:light dark}body{margin:auto;max-width:50em}</style>\n"
107 "<title>Welcome to %host%</title>\n</head>\n"
108 "<body>\n"
109 "%blurb%"
110 "<p>The following users are part of this community:</p>\n"
111 "\n"
112 "%userlist%\n"
113 "\n"
114 "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
115 "</body></html>\n";
116
117
118int write_default_css(void)
119{
120 FILE *f;
121
122 xs *sfn = xs_fmt("%s/style.css", srv_basedir);
123 if ((f = fopen(sfn, "w")) == NULL)
124 return 1;
125
126 fwrite(default_css, strlen(default_css), 1, f);
127 fclose(f);
128
129 return 0;
130}
131
132
133int snac_init(const char *basedir)
134{
135 FILE *f;
136
137 if (basedir == NULL) {
138 printf("Base directory: "); fflush(stdout);
139 srv_basedir = xs_strip_i(xs_readline(stdin));
140 }
141 else
142 srv_basedir = xs_str_new(basedir);
143
144 if (srv_basedir == NULL || *srv_basedir == '\0')
145 return 1;
146
147 if (xs_endswith(srv_basedir, "/"))
148 srv_basedir = xs_crop_i(srv_basedir, 0, -1);
149
150 if (mtime(srv_basedir) != 0.0) {
151 printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
152 return 1;
153 }
154
155 srv_config = xs_json_loads(default_srv_config);
156
157 xs *layout = xs_number_new(disk_layout);
158 srv_config = xs_dict_set(srv_config, "layout", layout);
159
160 int is_unix_socket = 0;
161
162 printf("Network address or full path to unix socket [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
163 {
164 xs *i = xs_strip_i(xs_readline(stdin));
165 if (*i) {
166 srv_config = xs_dict_set(srv_config, "address", i);
167
168 if (*i == '/')
169 is_unix_socket = 1;
170 }
171 }
172
173 if (!is_unix_socket) {
174 printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
175 {
176 xs *i = xs_strip_i(xs_readline(stdin));
177 if (*i) {
178 xs *n = xs_number_new(atoi(i));
179 srv_config = xs_dict_set(srv_config, "port", n);
180 }
181 }
182 }
183 else {
184 xs *n = xs_number_new(0);
185 srv_config = xs_dict_set(srv_config, "port", n);
186 }
187
188 printf("Host name: "); fflush(stdout);
189 {
190 xs *i = xs_strip_i(xs_readline(stdin));
191 if (*i == '\0')
192 return 1;
193
194 srv_config = xs_dict_set(srv_config, "host", i);
195 }
196
197 printf("URL prefix: "); fflush(stdout);
198 {
199 xs *i = xs_strip_i(xs_readline(stdin));
200
201 if (*i) {
202 if (xs_endswith(i, "/"))
203 i = xs_crop_i(i, 0, -1);
204
205 srv_config = xs_dict_set(srv_config, "prefix", i);
206 }
207 }
208
209 printf("Admin email address (optional): "); fflush(stdout);
210 {
211 xs *i = xs_strip_i(xs_readline(stdin));
212
213 srv_config = xs_dict_set(srv_config, "admin_email", i);
214 }
215
216 if (mkdirx(srv_basedir) == -1) {
217 printf("ERROR: cannot create directory '%s'\n", srv_basedir);
218 return 1;
219 }
220
221 xs *udir = xs_fmt("%s/user", srv_basedir);
222 mkdirx(udir);
223
224 xs *odir = xs_fmt("%s/object", srv_basedir);
225 mkdirx(odir);
226
227 xs *qdir = xs_fmt("%s/queue", srv_basedir);
228 mkdirx(qdir);
229
230 xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
231 mkdirx(ibdir);
232
233 xs *langdir = xs_fmt("%s/lang", srv_basedir);
234 mkdirx(langdir);
235
236 xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
237 if ((f = fopen(gfn, "w")) == NULL) {
238 printf("ERROR: cannot create '%s'\n", gfn);
239 return 1;
240 }
241
242 xs *gh = xs_replace(greeting_html, "%blurb%", snac_blurb);
243 fwrite(gh, strlen(gh), 1, f);
244 fclose(f);
245
246 if (write_default_css()) {
247 printf("ERROR: cannot create style.css\n");
248 return 1;
249 }
250
251 xs *cfn = xs_fmt("%s/server.json", srv_basedir);
252 if ((f = fopen(cfn, "w")) == NULL) {
253 printf("ERROR: cannot create '%s'\n", cfn);
254 return 1;
255 }
256
257 xs_json_dump(srv_config, 4, f);
258 fclose(f);
259
260 printf("Done.\n\n");
261
262 printf("Wanted web UI language files (.po) must be copied manually to %s\n", langdir);
263
264 return 0;
265}
266
267
268void new_password(const char *uid, xs_str **clear_pwd, xs_str **hashed_pwd)
269/* creates a random password */
270{
271 int rndbuf[3];
272
273 xs_rnd_buf(rndbuf, sizeof(rndbuf));
274
275 *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
276 *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
277}
278
279
280int adduser(const char *uid)
281/* creates a new user */
282{
283 snac snac;
284 xs *config = xs_dict_new();
285 xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
286 xs *pwd = NULL;
287 xs *pwd_f = NULL;
288 xs *key = NULL;
289 FILE *f;
290
291 if (uid == NULL) {
292 printf("Username: "); fflush(stdout);
293 uid = xs_strip_i(xs_readline(stdin));
294 }
295
296 if (!validate_uid(uid)) {
297 printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
298 return 1;
299 }
300
301 if (user_open(&snac, uid)) {
302 printf("ERROR: user '%s' already exists\n", snac.uid);
303 return 1;
304 }
305
306 new_password(uid, &pwd, &pwd_f);
307
308 config = xs_dict_append(config, "uid", uid);
309 config = xs_dict_append(config, "name", uid);
310 config = xs_dict_append(config, "avatar", "");
311 config = xs_dict_append(config, "bio", "");
312 config = xs_dict_append(config, "cw", "");
313 config = xs_dict_append(config, "published", date);
314 config = xs_dict_append(config, "passwd", pwd_f);
315
316 xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
317
318 if (mkdirx(basedir) == -1) {
319 printf("ERROR: cannot create directory '%s'\n", basedir);
320 return 0;
321 }
322
323 const char *dirs[] = {
324 "followers", "following", "muted", "hidden",
325 "public", "private", "queue", "history",
326 "static", NULL };
327 int n;
328
329 for (n = 0; dirs[n]; n++) {
330 xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
331 mkdirx(d);
332 }
333
334 /* add a specially short data retention time for the relay */
335 if (strcmp(uid, "relay") == 0)
336 config = xs_dict_set(config, "purge_days", xs_stock(1));
337
338 xs *cfn = xs_fmt("%s/user.json", basedir);
339
340 if ((f = fopen(cfn, "w")) == NULL) {
341 printf("ERROR: cannot create '%s'\n", cfn);
342 return 1;
343 }
344 else {
345 xs_json_dump(config, 4, f);
346 fclose(f);
347 }
348
349 printf("\nCreating RSA key...\n");
350 key = xs_evp_genkey(2048);
351 printf("Done.\n");
352
353 xs *kfn = xs_fmt("%s/key.json", basedir);
354
355 if ((f = fopen(kfn, "w")) == NULL) {
356 printf("ERROR: cannot create '%s'\n", kfn);
357 return 1;
358 }
359 else {
360 xs_json_dump(key, 4, f);
361 fclose(f);
362 }
363
364 printf("\nUser password is %s\n", pwd);
365
366 printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
367
368 return 0;
369}
370
371
372int resetpwd(snac *snac)
373/* creates a new password for the user */
374{
375 xs *clear_pwd = NULL;
376 xs *hashed_pwd = NULL;
377 xs *fn = xs_fmt("%s/user.json", snac->basedir);
378 FILE *f;
379 int ret = 0;
380
381 new_password(snac->uid, &clear_pwd, &hashed_pwd);
382
383 snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
384
385 if ((f = fopen(fn, "w")) != NULL) {
386 xs_json_dump(snac->config, 4, f);
387 fclose(f);
388
389 printf("New password for user %s is %s\n", snac->uid, clear_pwd);
390 }
391 else {
392 printf("ERROR: cannot write to %s\n", fn);
393 ret = 1;
394 }
395
396 return ret;
397}
398
399
400void rm_rf(const char *dir)
401/* does an rm -rf (yes, I'm also scared) */
402{
403 xs *d = xs_str_cat(xs_dup(dir), "/" "*");
404 xs *l = xs_glob(d, 0, 0);
405 xs_list *p = l;
406 const xs_str *v;
407
408 if (dbglevel >= 1)
409 printf("Deleting directory %s\n", dir);
410
411 while (xs_list_iter(&p, &v)) {
412 struct stat st;
413
414 if (stat(v, &st) != -1) {
415 if (st.st_mode & S_IFDIR) {
416 rm_rf(v);
417 }
418 else {
419 if (dbglevel >= 1)
420 printf("Deleting file %s\n", v);
421
422 if (unlink(v) == -1)
423 printf("ERROR: cannot delete file %s\n", v);
424 }
425 }
426 else
427 printf("ERROR: stat() fail for %s\n", v);
428 }
429
430 if (rmdir(dir) == -1)
431 printf("ERROR: cannot delete directory %s\n", dir);
432}
433
434
435int deluser(snac *user)
436/* deletes a user */
437{
438 int ret = 0;
439 xs *fwers = following_list(user);
440 xs_list *p = fwers;
441 const xs_str *v;
442
443 while (xs_list_iter(&p, &v)) {
444 xs *object = NULL;
445
446 if (valid_status(following_get(user, v, &object))) {
447 xs *msg = msg_undo(user, xs_dict_get(object, "object"));
448
449 following_del(user, v);
450
451 enqueue_output_by_actor(user, msg, v, 0);
452
453 printf("Unfollowing actor %s\n", v);
454 }
455 }
456
457 grave(user->uid, 1);
458
459 rm_rf(user->basedir);
460
461 return ret;
462}
463
464
465void verify_links(snac *user)
466/* verifies a user's links */
467{
468 xs *metadata = NULL;
469 const xs_dict *md = xs_dict_get(user->config, "metadata");
470 const char *k, *v;
471 int changed = 0;
472
473 xs *headers = xs_dict_new();
474 headers = xs_dict_append(headers, "accept", "text/html");
475 headers = xs_dict_append(headers, "user-agent", USER_AGENT " (link verify)");
476
477 if (xs_type(md) == XSTYPE_DICT)
478 metadata = xs_dup(md);
479 else
480 if (xs_type(md) == XSTYPE_STRING) {
481 /* convert to dict for easier iteration */
482 metadata = xs_dict_new();
483 xs *l = xs_split(md, "\n");
484 const char *ll;
485
486 xs_list_foreach(l, ll) {
487 xs *kv = xs_split_n(ll, "=", 1);
488 const char *k = xs_list_get(kv, 0);
489 const char *v = xs_list_get(kv, 1);
490
491 if (k && v) {
492 xs *kk = xs_strip_i(xs_dup(k));
493 xs *vv = xs_strip_i(xs_dup(v));
494 metadata = xs_dict_set(metadata, kk, vv);
495 }
496 }
497 }
498
499 int c = 0;
500 while (metadata && xs_dict_next(metadata, &k, &v, &c)) {
501 xs *wfinger = NULL;
502 const char *ov = NULL;
503
504 /* is it an account handle? */
505 if (*v == '@' && strchr(v + 1, '@')) {
506 /* resolve it via webfinger */
507 if (valid_status(webfinger_request(v, &wfinger, NULL)) && xs_is_string(wfinger)) {
508 ov = v;
509 v = wfinger;
510
511 /* store the alias */
512 if (user->links == NULL)
513 user->links = xs_dict_new();
514
515 user->links = xs_dict_set(user->links, ov, v);
516
517 changed++;
518 }
519 }
520
521 /* not an https link? skip */
522 if (!xs_startswith(v, "https:/" "/"))
523 continue;
524
525 int status;
526 xs *req = NULL;
527 xs *payload = NULL;
528 int p_size = 0;
529
530 req = xs_http_request("GET", v, headers, NULL, 0, &status,
531 &payload, &p_size, 0);
532
533 if (!valid_status(status)) {
534 snac_log(user, xs_fmt("link %s verify error %d", v, status));
535 continue;
536 }
537
538 /* extract the links */
539 xs *ls = xs_regex_select(payload, "< *(a|link) +[^>]+>");
540
541 xs_list *lp = ls;
542 const char *ll;
543 int vfied = 0;
544
545 while (!vfied && xs_list_iter(&lp, &ll)) {
546 /* extract href and rel */
547 xs *r = xs_regex_select(ll, "(href|rel) *= *(\"[^\"]*\"|'[^']*')");
548
549 /* must have both attributes */
550 if (xs_list_len(r) != 2)
551 continue;
552
553 xs *href = NULL;
554 int is_rel_me = 0;
555 xs_list *pr = r;
556 const char *ar;
557
558 while (xs_list_iter(&pr, &ar)) {
559 xs *nq = xs_dup(ar);
560
561 nq = xs_replace_i(nq, "\"", "");
562 nq = xs_replace_i(nq, "'", "");
563
564 xs *r2 = xs_split_n(nq, "=", 1);
565 if (xs_list_len(r2) != 2)
566 continue;
567
568 xs *ak = xs_strip_i(xs_dup(xs_list_get(r2, 0)));
569 xs *av = xs_strip_i(xs_dup(xs_list_get(r2, 1)));
570
571 if (strcmp(ak, "href") == 0)
572 href = xs_dup(av);
573 else
574 if (strcmp(ak, "rel") == 0) {
575 /* split the value by spaces */
576 xs *vbs = xs_split(av, " ");
577
578 /* is any of it "me"? */
579 if (xs_list_in(vbs, "me") != -1)
580 is_rel_me = 1;
581 }
582 }
583
584 /* after all this acrobatics, do we have an href and a rel="me"? */
585 if (href != NULL && is_rel_me) {
586 /* is it the same as the actor? */
587 if (strcmp(href, user->actor) == 0) {
588 /* got it! */
589 xs *verified_time = xs_number_new((double)time(NULL));
590
591 if (user->links == NULL)
592 user->links = xs_dict_new();
593
594 user->links = xs_dict_set(user->links, v, verified_time);
595
596 vfied = 1;
597 }
598 else
599 snac_debug(user, 1,
600 xs_fmt("verify link %s rel='me' found but not related (%s)", v, href));
601 }
602 }
603
604 if (vfied) {
605 changed++;
606 snac_log(user, xs_fmt("link %s verified", v));
607 }
608 else {
609 snac_log(user, xs_fmt("link %s not verified (rel='me' not found)", v));
610 }
611 }
612
613 if (changed) {
614 FILE *f;
615
616 /* update the links.json file */
617 xs *fn = xs_fmt("%s/links.json", user->basedir);
618 xs *bfn = xs_fmt("%s.bak", fn);
619
620 rename(fn, bfn);
621
622 if ((f = fopen(fn, "w")) != NULL) {
623 xs_json_dump(user->links, 4, f);
624 fclose(f);
625 }
626 else
627 rename(bfn, fn);
628 }
629}
630
631
632void export_csv(snac *user)
633/* exports user data to current directory in a way that pleases Mastodon */
634{
635 FILE *f;
636 xs *fn = NULL;
637
638 fn = xs_fmt("%s/export/bookmarks.csv", user->basedir);
639 if ((f = fopen(fn, "w")) != NULL) {
640 snac_log(user, xs_fmt("Creating %s...", fn));
641
642 xs *l = bookmark_list(user);
643 const char *md5;
644
645 xs_list_foreach(l, md5) {
646 xs *post = NULL;
647
648 if (valid_status(object_get_by_md5(md5, &post))) {
649 const char *id = xs_dict_get(post, "id");
650
651 if (xs_type(id) == XSTYPE_STRING)
652 fprintf(f, "%s\n", id);
653 }
654 }
655
656 fclose(f);
657 }
658 else
659 snac_log(user, xs_fmt("Cannot create file %s", fn));
660
661 xs_free(fn);
662 fn = xs_fmt("%s/export/blocked_accounts.csv", user->basedir);
663 if ((f = fopen(fn, "w")) != NULL) {
664 snac_log(user, xs_fmt("Creating %s...", fn));
665
666 xs *l = muted_list(user);
667 const char *actor;
668
669 xs_list_foreach(l, actor) {
670 xs *uid = NULL;
671
672 webfinger_request_fake(actor, NULL, &uid);
673 fprintf(f, "%s\n", uid);
674 }
675
676 fclose(f);
677 }
678 else
679 snac_log(user, xs_fmt("Cannot create file %s", fn));
680
681 xs_free(fn);
682 fn = xs_fmt("%s/export/lists.csv", user->basedir);
683 if ((f = fopen(fn, "w")) != NULL) {
684 snac_log(user, xs_fmt("Creating %s...", fn));
685
686 xs *lol = list_maint(user, NULL, 0);
687 const xs_list *li;
688
689 xs_list_foreach(lol, li) {
690 const char *lid = xs_list_get(li, 0);
691 const char *ltitle = xs_list_get(li, 1);
692
693 xs *actors = list_members(user, lid, NULL, 0);
694 const char *md5;
695
696 xs_list_foreach(actors, md5) {
697 xs *actor = NULL;
698
699 if (valid_status(object_get_by_md5(md5, &actor))) {
700 const char *id = xs_dict_get(actor, "id");
701 xs *uid = NULL;
702
703 webfinger_request_fake(id, NULL, &uid);
704 fprintf(f, "%s,%s\n", ltitle, uid);
705 }
706 }
707 }
708
709 fclose(f);
710 }
711 else
712 snac_log(user, xs_fmt("Cannot create file %s", fn));
713
714 xs_free(fn);
715 fn = xs_fmt("%s/export/following_accounts.csv", user->basedir);
716 if ((f = fopen(fn, "w")) != NULL) {
717 snac_log(user, xs_fmt("Creating %s...", fn));
718
719 fprintf(f, "Account address,Show boosts,Notify on new posts,Languages\n");
720
721 xs *fwing = following_list(user);
722 const char *actor;
723
724 xs_list_foreach(fwing, actor) {
725 xs *uid = NULL;
726
727 webfinger_request_fake(actor, NULL, &uid);
728 fprintf(f, "%s,%s,false,\n", uid, limited(user, actor, 0) ? "false" : "true");
729 }
730
731 fclose(f);
732 }
733 else
734 snac_log(user, xs_fmt("Cannot create file %s", fn));
735}
736
737
738void export_posts(snac *user)
739/* exports all posts to an OrderedCollection */
740{
741 xs *ifn = xs_fmt("%s/public.idx", user->basedir);
742 xs *index = index_list(ifn, XS_ALL);
743 xs *ofn = xs_fmt("%s/export/outbox.json", user->basedir);
744 FILE *f;
745
746 if ((f = fopen(ofn, "w")) == NULL) {
747 snac_log(user, xs_fmt("Cannot create file %s", ofn));
748 return;
749 }
750
751 int cnt = 0;
752
753 /* raw output */
754 fprintf(f, "{\"@context\": \"https:/" "/www.w3.org/ns/activitystreams\",");
755 fprintf(f, "\"id\": \"outbox.json\",");
756 fprintf(f, "\"type\": \"OrderedCollection\",");
757 fprintf(f, "\"orderedItems\": [");
758
759 const char *md5;
760
761 snac_log(user, xs_fmt("Creating %s...", ofn));
762
763 xs_list_foreach(index, md5) {
764 xs *obj = NULL;
765
766 if (!valid_status(object_get_by_md5(md5, &obj)))
767 continue;
768
769 const char *type = xs_dict_get(obj, "type");
770
771 if (!xs_is_string(type) || strcmp(type, "Note"))
772 continue;
773
774 const char *atto = get_atto(obj);
775
776 if (!xs_is_string(atto) || strcmp(atto, user->actor))
777 continue;
778
779 if (cnt)
780 fprintf(f, ",");
781
782 xs *c_msg = msg_create(user, obj);
783 xs_json_dump(c_msg, 0, f);
784 cnt++;
785 }
786
787 fprintf(f, "], \"totalItems\": %d}", cnt);
788
789 fclose(f);
790}
791
792
793void import_blocked_accounts_csv(snac *user, const char *ifn)
794/* imports a Mastodon CSV file of blocked accounts */
795{
796 FILE *f;
797 xs *l = xs_split(ifn, "/");
798 xs *fn = xs_fmt("%s/import/%s", user->basedir, xs_list_get(l, -1));
799
800 if ((f = fopen(fn, "r")) != NULL) {
801 snac_log(user, xs_fmt("Importing from %s...", fn));
802
803 while (!feof(f)) {
804 xs *l = xs_strip_i(xs_readline(f));
805
806 if (*l && strchr(l, '@') != NULL) {
807 xs *url = NULL;
808 xs *uid = NULL;
809
810 if (valid_status(webfinger_request(l, &url, &uid))) {
811 if (is_muted(user, url))
812 snac_log(user, xs_fmt("Actor %s already MUTEd", url));
813 else {
814 mute(user, url);
815 snac_log(user, xs_fmt("MUTEd actor %s", url));
816 }
817 }
818 else
819 snac_log(user, xs_fmt("Webfinger error for account %s", l));
820 }
821 }
822
823 fclose(f);
824 }
825 else
826 snac_log(user, xs_fmt("Cannot open file %s", fn));
827}
828
829
830void import_following_accounts_csv(snac *user, const char *ifn)
831/* imports a Mastodon CSV file of accounts to follow */
832{
833 FILE *f;
834 xs *l = xs_split(ifn, "/");
835 xs *fn = xs_fmt("%s/import/%s", user->basedir, xs_list_get(l, -1));
836
837 if ((f = fopen(fn, "r")) != NULL) {
838 snac_log(user, xs_fmt("Importing from %s...", fn));
839
840 while (!feof(f)) {
841 xs *l = xs_strip_i(xs_readline(f));
842
843 if (*l) {
844 xs *l2 = xs_split(l, ",");
845 const char *acct = xs_list_get(l2, 0);
846 const char *show = xs_list_get(l2, 1);
847
848 if (acct) {
849 /* not a valid account? skip (probably the CSV header) */
850 if (strchr(acct, '@') == NULL)
851 continue;
852
853 xs *msg = msg_follow(user, acct);
854
855 if (msg == NULL) {
856 snac_log(user, xs_fmt("Cannot follow %s -- server down?", acct));
857 continue;
858 }
859
860 const char *actor = xs_dict_get(msg, "object");
861
862 if (following_check(user, actor))
863 snac_log(user, xs_fmt("Actor %s already followed", actor));
864 else {
865 following_add(user, actor, msg);
866
867 enqueue_output_by_actor(user, msg, actor, 0);
868
869 snac_log(user, xs_fmt("Following %s", actor));
870 }
871
872 if (show && strcmp(show, "false") == 0) {
873 limit(user, actor);
874 snac_log(user, xs_fmt("Limiting boosts from actor %s", actor));
875 }
876 else {
877 unlimit(user, actor);
878 snac_log(user, xs_fmt("Unlimiting boosts from actor %s", actor));
879 }
880 }
881 }
882 }
883
884 fclose(f);
885 }
886 else
887 snac_log(user, xs_fmt("Cannot open file %s", fn));
888}
889
890
891void import_list_csv(snac *user, const char *ifn)
892/* imports a Mastodon CSV file list */
893{
894 FILE *f;
895 xs *l = xs_split(ifn, "/");
896 xs *fn = xs_fmt("%s/import/%s", user->basedir, xs_list_get(l, -1));
897
898 if ((f = fopen(fn, "r")) != NULL) {
899 snac_log(user, xs_fmt("Importing from %s...", fn));
900
901 while (!feof(f)) {
902 xs *l = xs_strip_i(xs_readline(f));
903
904 if (*l) {
905 xs *l2 = xs_split(l, ",");
906 const char *lname = xs_list_get(l2, 0);
907 const char *acct = xs_list_get(l2, 1);
908
909 if (lname && acct) {
910 /* create the list */
911 xs *list_id = list_maint(user, lname, 1);
912
913 xs *url = NULL;
914 xs *uid = NULL;
915
916 if (valid_status(webfinger_request(acct, &url, &uid))) {
917 xs *actor_md5 = xs_md5_hex(url, strlen(url));
918
919 list_members(user, list_id, actor_md5, 1);
920 snac_log(user, xs_fmt("Added %s to list %s", url, lname));
921
922 if (!following_check(user, url)) {
923 xs *msg = msg_follow(user, url);
924
925 if (msg == NULL) {
926 snac_log(user, xs_fmt("Cannot follow %s -- server down?", acct));
927 continue;
928 }
929
930 following_add(user, url, msg);
931
932 enqueue_output_by_actor(user, msg, url, 0);
933
934 snac_log(user, xs_fmt("Following %s", url));
935 }
936 }
937 else
938 snac_log(user, xs_fmt("Webfinger error while adding %s to list %s", acct, lname));
939 }
940 }
941 }
942
943 fclose(f);
944 }
945 else
946 snac_log(user, xs_fmt("Cannot open file %s", fn));
947}
948
949
950void import_csv(snac *user)
951/* import CSV files from Mastodon */
952{
953 FILE *f;
954
955 import_blocked_accounts_csv(user, "blocked_accounts.csv");
956
957 import_following_accounts_csv(user, "following_accounts.csv");
958
959 import_list_csv(user, "lists.csv");
960
961 xs *fn = xs_fmt("%s/import/bookmarks.csv", user->basedir);
962 if ((f = fopen(fn, "r")) != NULL) {
963 snac_log(user, xs_fmt("Importing from %s...", fn));
964
965 while (!feof(f)) {
966 xs *l = xs_strip_i(xs_readline(f));
967
968 if (*l) {
969 xs *post = NULL;
970
971 if (!valid_status(object_get(l, &post))) {
972 if (!valid_status(activitypub_request(user, l, &post))) {
973 snac_log(user, xs_fmt("Error getting object %s for bookmarking", l));
974 continue;
975 }
976 }
977
978 if (post == NULL)
979 continue;
980
981 /* request the actor that created the post */
982 const char *actor = get_atto(post);
983
984 if (xs_type(actor) == XSTYPE_STRING)
985 actor_request(user, actor, NULL);
986
987 object_add_ow(l, post);
988 timeline_add(user, l, post);
989
990 bookmark(user, l);
991
992 snac_log(user, xs_fmt("Bookmarked %s", l));
993 }
994 }
995
996 fclose(f);
997 }
998 else
999 snac_log(user, xs_fmt("Cannot open file %s", fn));
1000}
1001
1002static const struct {
1003 const char *proto;
1004 unsigned short default_port;
1005} FALLBACK_PORTS[] = {
1006 /* caution: https > http, smpts > smtp */
1007 {"https", 443},
1008 {"http", 80},
1009 {"smtps", 465},
1010 {"smtp", 25}
1011};
1012
1013int parse_port(const char *url, const char **errstr)
1014{
1015 const char *col, *rcol;
1016 int tmp, ret = -1;
1017
1018 if (errstr)
1019 *errstr = NULL;
1020
1021 if (!(col = strchr(url, ':'))) {
1022 if (errstr)
1023 *errstr = "bad url";
1024
1025 return -1;
1026 }
1027
1028 for (size_t i = 0; i < sizeof(FALLBACK_PORTS) / sizeof(*FALLBACK_PORTS); ++i) {
1029 if (memcmp(url, FALLBACK_PORTS[i].proto, strlen(FALLBACK_PORTS[i].proto)) == 0) {
1030 ret = FALLBACK_PORTS[i].default_port;
1031 break;
1032 }
1033 }
1034
1035 if (!(rcol = strchr(col + 1, ':')))
1036 rcol = col;
1037
1038 if (rcol) {
1039 tmp = atoi(rcol + 1);
1040 if (tmp == 0) {
1041 if (ret != -1)
1042 return ret;
1043
1044 if (errstr)
1045 *errstr = strerror(errno);
1046
1047 return -1;
1048 }
1049
1050 return tmp;
1051 }
1052
1053 if (errstr)
1054 *errstr = "unknown protocol";
1055
1056 return -1;
1057}