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