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