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