2.70 html.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_regex.h"
   8#include "xs_set.h"
   9#include "xs_openssl.h"
  10#include "xs_time.h"
  11#include "xs_mime.h"
  12#include "xs_match.h"
  13#include "xs_html.h"
  14#include "xs_curl.h"
  15#include "xs_unicode.h"
  16
  17#include "snac.h"
  18
  19int login(snac *snac, const xs_dict *headers)
  20/* tries a login */
  21{
  22    int logged_in = 0;
  23    const char *auth = xs_dict_get(headers, "authorization");
  24
  25    if (auth && xs_startswith(auth, "Basic ")) {
  26        int sz;
  27        xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  28        xs *s2 = xs_base64_dec(s1, &sz);
  29
  30        xs *l1 = xs_split_n(s2, ":", 1);
  31
  32        if (xs_list_len(l1) == 2) {
  33            const char *user = xs_list_get(l1, 0);
  34            const char *pwd  = xs_list_get(l1, 1);
  35            const char *addr = xs_or(xs_dict_get(headers, "remote-addr"),
  36                                     xs_dict_get(headers, "x-forwarded-for"));
  37
  38            if (badlogin_check(user, addr)) {
  39                logged_in = check_password(user, pwd,
  40                    xs_dict_get(snac->config, "passwd"));
  41
  42                if (!logged_in)
  43                    badlogin_inc(user, addr);
  44            }
  45        }
  46    }
  47
  48    if (logged_in)
  49        lastlog_write(snac, "web");
  50
  51    return logged_in;
  52}
  53
  54
  55xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems, const char *proxy)
  56/* replaces all the :shortnames: with the emojis in tag */
  57{
  58    if (!xs_is_null(tag)) {
  59        xs *tag_list = NULL;
  60        if (xs_type(tag) == XSTYPE_DICT) {
  61            /* not a list */
  62            tag_list = xs_list_new();
  63            tag_list = xs_list_append(tag_list, tag);
  64        } else {
  65            /* is a list */
  66            tag_list = xs_dup(tag);
  67        }
  68
  69        xs *style = xs_fmt("height: %dem; width: %dem; vertical-align: middle;", ems, ems);
  70
  71        const char *v;
  72        int c = 0;
  73
  74        while (xs_list_next(tag_list, &v, &c)) {
  75            const char *t = xs_dict_get(v, "type");
  76
  77            if (t && strcmp(t, "Emoji") == 0) {
  78                const char *n = xs_dict_get(v, "name");
  79                const char *i = xs_dict_get(v, "icon");
  80
  81                if (n && i) {
  82                    const char *u = xs_dict_get(i, "url");
  83                    xs *url = make_url(u, proxy, 0);
  84
  85                    xs_html *img = xs_html_sctag("img",
  86                        xs_html_attr("loading", "lazy"),
  87                        xs_html_attr("src", url),
  88                        xs_html_attr("style", style));
  89
  90                    xs *s1 = xs_html_render(img);
  91                    s = xs_replace_i(s, n, s1);
  92                }
  93            }
  94        }
  95    }
  96
  97    return s;
  98}
  99
 100
 101xs_str *actor_name(xs_dict *actor, const char *proxy)
 102/* gets the actor name */
 103{
 104    const char *v;
 105
 106    if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
 107        if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
 108            v = "anonymous";
 109        }
 110    }
 111
 112    return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"), 1, proxy);
 113}
 114
 115
 116xs_html *html_actor_icon(snac *user, xs_dict *actor, const char *date,
 117                        const char *udate, const char *url, int priv,
 118                        int in_people, const char *proxy, const char *lang)
 119{
 120    xs_html *actor_icon = xs_html_tag("p", NULL);
 121
 122    xs *avatar = NULL;
 123    const char *v;
 124    int fwing = 0;
 125    int fwer = 0;
 126
 127    xs *name = actor_name(actor, proxy);
 128
 129    /* get the avatar */
 130    if ((v = xs_dict_get(actor, "icon")) != NULL) {
 131        /* if it's a list (Peertube), get the first one */
 132        if (xs_type(v) == XSTYPE_LIST)
 133            v = xs_list_get(v, 0);
 134
 135        if ((v = xs_dict_get(v, "url")) != NULL)
 136            avatar = make_url(v, proxy, 0);
 137    }
 138
 139    if (avatar == NULL)
 140        avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
 141
 142    const char *actor_id = xs_dict_get(actor, "id");
 143    xs *href = NULL;
 144
 145    if (user) {
 146        fwer = follower_check(user, actor_id);
 147        fwing = following_check(user, actor_id);
 148    }
 149
 150    if (user && !in_people) {
 151        /* if this actor is a follower or being followed, create an
 152           anchored link to the people page instead of the actor url */
 153        if (fwer || fwing) {
 154            xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
 155            href = xs_fmt("%s/people#%s", user->actor, md5);
 156        }
 157    }
 158
 159    if (href == NULL)
 160        href = xs_dup(actor_id);
 161
 162    xs_html_add(actor_icon,
 163        xs_html_sctag("img",
 164            xs_html_attr("loading", "lazy"),
 165            xs_html_attr("class",   "snac-avatar"),
 166            xs_html_attr("src",     avatar),
 167            xs_html_attr("alt",     "")),
 168        xs_html_tag("a",
 169            xs_html_attr("href",    href),
 170            xs_html_attr("class",   "p-author h-card snac-author"),
 171            xs_html_raw(name))); /* name is already html-escaped */
 172
 173    if (!xs_is_null(url)) {
 174        xs *md5 = xs_md5_hex(url, strlen(url));
 175
 176        xs_html_add(actor_icon,
 177            xs_html_text(" "),
 178            xs_html_tag("a",
 179                xs_html_attr("href", (char *)url),
 180                xs_html_attr("title", md5),
 181                xs_html_text("ยป")));
 182    }
 183
 184    if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
 185        xs_html_add(actor_icon,
 186            xs_html_text(" "),
 187            xs_html_tag("span",
 188                xs_html_attr("title", "bot"),
 189                xs_html_raw("🤖")));
 190    }
 191
 192    if (fwing && fwer) {
 193        xs_html_add(actor_icon,
 194            xs_html_text(" "),
 195            xs_html_tag("span",
 196                xs_html_attr("title", "mutual relation"),
 197                xs_html_raw("🤝")));
 198    }
 199
 200    if (priv) {
 201        xs_html_add(actor_icon,
 202            xs_html_text(" "),
 203            xs_html_tag("span",
 204                xs_html_attr("title", "private"),
 205                xs_html_raw("🔒")));
 206    }
 207
 208    if (xs_is_null(date)) {
 209        xs_html_add(actor_icon,
 210            xs_html_raw(" "));
 211    }
 212    else {
 213        xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
 214        xs *date_title = xs_dup(date);
 215
 216        if (!xs_is_null(udate)) {
 217            xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
 218
 219            date_label = xs_str_cat(date_label, " / ", sd);
 220
 221            date_title = xs_str_cat(date_title, " / ", udate);
 222        }
 223
 224        if (xs_is_string(lang))
 225            date_title = xs_str_cat(date_title, " (", lang, ")");
 226
 227        xs_html_add(actor_icon,
 228            xs_html_text(" "),
 229            xs_html_tag("time",
 230                xs_html_attr("class", "dt-published snac-pubdate"),
 231                xs_html_attr("title", date_title),
 232                xs_html_text(date_label)));
 233    }
 234
 235    {
 236        const char *username, *id;
 237
 238        if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
 239            /* This should never be reached */
 240            username = "anonymous";
 241        }
 242
 243        if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
 244            /* This should never be reached */
 245            id = "https://social.example.org/anonymous";
 246        }
 247
 248        /* "LIKE AN ANIMAL" */
 249        xs *domain = xs_split(id, "/");
 250        xs *user   = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
 251
 252        xs_html_add(actor_icon,
 253            xs_html_sctag("br", NULL),
 254            xs_html_tag("a",
 255                xs_html_attr("href",  xs_dict_get(actor, "id")),
 256                xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
 257                xs_html_text(user)));
 258    }
 259
 260    return actor_icon;
 261}
 262
 263
 264xs_html *html_msg_icon(snac *user, const char *actor_id, const xs_dict *msg, const char *proxy)
 265{
 266    xs *actor = NULL;
 267    xs_html *actor_icon = NULL;
 268
 269    if (actor_id && valid_status(actor_get_refresh(user, actor_id, &actor))) {
 270        const char *date  = NULL;
 271        const char *udate = NULL;
 272        const char *url   = NULL;
 273        const char *lang  = NULL;
 274        int priv    = 0;
 275        const char *type = xs_dict_get(msg, "type");
 276
 277        if (xs_match(type, POSTLIKE_OBJECT_TYPE))
 278            url = xs_dict_get(msg, "id");
 279
 280        priv = !is_msg_public(msg);
 281
 282        date  = xs_dict_get(msg, "published");
 283        udate = xs_dict_get(msg, "updated");
 284
 285        lang = xs_dict_get(msg, "contentMap");
 286        if (xs_is_dict(lang)) {
 287            const char *v;
 288            int c = 0;
 289
 290            xs_dict_next(lang, &lang, &v, &c);
 291        }
 292        else
 293            lang = NULL;
 294
 295        actor_icon = html_actor_icon(user, actor, date, udate, url, priv, 0, proxy, lang);
 296    }
 297
 298    return actor_icon;
 299}
 300
 301
 302xs_html *html_note(snac *user, const char *summary,
 303                   const char *div_id, const char *form_id,
 304                   const char *ta_plh, const char *ta_content,
 305                   const char *edit_id, const char *actor_id,
 306                   const xs_val *cw_yn, const char *cw_text,
 307                   const xs_val *mnt_only, const char *redir,
 308                   const char *in_reply_to, int poll,
 309                   const char *att_file, const char *att_alt_text,
 310                   int is_draft)
 311/* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */
 312{
 313    xs *action = xs_fmt("%s/admin/note", user->actor);
 314
 315    xs_html *form;
 316
 317    xs_html *note = xs_html_tag("div",
 318        xs_html_tag("details",
 319            xs_html_tag("summary",
 320                xs_html_text(summary)),
 321                xs_html_tag("p", NULL),
 322                xs_html_tag("div",
 323                    xs_html_attr("class", "snac-note"),
 324                    xs_html_attr("id",    div_id),
 325                    form = xs_html_tag("form",
 326                        xs_html_attr("autocomplete", "off"),
 327                        xs_html_attr("method",       "post"),
 328                        xs_html_attr("action",       action),
 329                        xs_html_attr("enctype",      "multipart/form-data"),
 330                        xs_html_attr("id",           form_id),
 331                        xs_html_tag("textarea",
 332                            xs_html_attr("class",    "snac-textarea"),
 333                            xs_html_attr("name",     "content"),
 334                            xs_html_attr("rows",     "4"),
 335                            xs_html_attr("wrap",     "virtual"),
 336                            xs_html_attr("required", "required"),
 337                            xs_html_attr("placeholder", ta_plh),
 338                            xs_html_text(ta_content)),
 339                        xs_html_tag("p", NULL),
 340                        xs_html_text(L("Sensitive content: ")),
 341                        xs_html_sctag("input",
 342                            xs_html_attr("type", "checkbox"),
 343                            xs_html_attr("name", "sensitive"),
 344                            xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
 345                        xs_html_sctag("input",
 346                            xs_html_attr("type", "text"),
 347                            xs_html_attr("name", "summary"),
 348                            xs_html_attr("placeholder", L("Sensitive content description")),
 349                            xs_html_attr("value", xs_is_null(cw_text) ? "" : cw_text))))));
 350
 351    if (actor_id)
 352        xs_html_add(form,
 353            xs_html_sctag("input",
 354                xs_html_attr("type",  "hidden"),
 355                xs_html_attr("name",  "to"),
 356                xs_html_attr("value", actor_id)));
 357    else {
 358        /* no actor_id; ask for mentioned_only */
 359        xs_html_add(form,
 360            xs_html_tag("p", NULL),
 361            xs_html_text(L("Only for mentioned people: ")),
 362            xs_html_sctag("input",
 363                xs_html_attr("type",  "checkbox"),
 364                xs_html_attr("name",  "mentioned_only"),
 365                xs_html_attr(xs_type(mnt_only) == XSTYPE_TRUE ? "checked" : "", NULL)));
 366    }
 367
 368    if (redir)
 369        xs_html_add(form,
 370            xs_html_sctag("input",
 371                xs_html_attr("type",  "hidden"),
 372                xs_html_attr("name",  "redir"),
 373                xs_html_attr("value", redir)));
 374
 375    if (in_reply_to)
 376        xs_html_add(form,
 377            xs_html_sctag("input",
 378                xs_html_attr("type",  "hidden"),
 379                xs_html_attr("name",  "in_reply_to"),
 380                xs_html_attr("value", in_reply_to)));
 381    else
 382        xs_html_add(form,
 383            xs_html_tag("p", NULL),
 384            xs_html_text(L("Reply to (URL): ")),
 385            xs_html_sctag("input",
 386                xs_html_attr("type",     "url"),
 387                xs_html_attr("name",     "in_reply_to"),
 388                xs_html_attr("placeholder", "Optional URL to reply to")));
 389
 390    xs_html_add(form,
 391        xs_html_tag("p", NULL),
 392        xs_html_tag("span",
 393            xs_html_attr("title", L("Don't send, but store as a draft")),
 394            xs_html_text(L("Draft:")),
 395            xs_html_sctag("input",
 396                xs_html_attr("type", "checkbox"),
 397                xs_html_attr("name", "is_draft"),
 398                xs_html_attr(is_draft ? "checked" : "", NULL))));
 399
 400    if (edit_id)
 401        xs_html_add(form,
 402            xs_html_sctag("input",
 403                xs_html_attr("type",  "hidden"),
 404                xs_html_attr("name",  "edit_id"),
 405                xs_html_attr("value", edit_id)));
 406
 407    /* attachment controls */
 408    xs_html *att;
 409
 410    xs_html_add(form,
 411        xs_html_tag("p", NULL),
 412        att = xs_html_tag("details",
 413            xs_html_tag("summary",
 414                xs_html_text(L("Attachment..."))),
 415            xs_html_tag("p", NULL)));
 416
 417    if (att_file && *att_file)
 418        xs_html_add(att,
 419            xs_html_text(L("File:")),
 420            xs_html_sctag("input",
 421                xs_html_attr("type", "text"),
 422                xs_html_attr("name", "attach_url"),
 423                xs_html_attr("title", L("Clear this field to delete the attachment")),
 424                xs_html_attr("value", att_file)));
 425    else
 426        xs_html_add(att,
 427            xs_html_sctag("input",
 428                xs_html_attr("type",    "file"),
 429                xs_html_attr("name",    "attach")));
 430
 431    xs_html_add(att,
 432        xs_html_text(" "),
 433        xs_html_sctag("input",
 434            xs_html_attr("type",    "text"),
 435            xs_html_attr("name",    "alt_text"),
 436            xs_html_attr("value",   att_alt_text),
 437            xs_html_attr("placeholder", L("Attachment description"))));
 438
 439    /* add poll controls */
 440    if (poll) {
 441        xs_html_add(form,
 442            xs_html_tag("p", NULL),
 443            xs_html_tag("details",
 444                xs_html_tag("summary",
 445                    xs_html_text(L("Poll..."))),
 446                xs_html_tag("p",
 447                    xs_html_text(L("Poll options (one per line, up to 8):")),
 448                    xs_html_sctag("br", NULL),
 449                    xs_html_tag("textarea",
 450                        xs_html_attr("class",    "snac-textarea"),
 451                        xs_html_attr("name",     "poll_options"),
 452                        xs_html_attr("rows",     "4"),
 453                        xs_html_attr("wrap",     "virtual"),
 454                        xs_html_attr("placeholder", "Option 1...\nOption 2...\nOption 3...\n..."))),
 455                xs_html_tag("select",
 456                    xs_html_attr("name",    "poll_multiple"),
 457                    xs_html_tag("option",
 458                        xs_html_attr("value", "off"),
 459                        xs_html_text(L("One choice"))),
 460                    xs_html_tag("option",
 461                        xs_html_attr("value", "on"),
 462                        xs_html_text(L("Multiple choices")))),
 463                xs_html_text(" "),
 464                xs_html_tag("select",
 465                    xs_html_attr("name",    "poll_end_secs"),
 466                    xs_html_tag("option",
 467                        xs_html_attr("value", "300"),
 468                        xs_html_text(L("End in 5 minutes"))),
 469                    xs_html_tag("option",
 470                        xs_html_attr("value", "3600"),
 471                        xs_html_attr("selected", NULL),
 472                        xs_html_text(L("End in 1 hour"))),
 473                    xs_html_tag("option",
 474                        xs_html_attr("value", "86400"),
 475                        xs_html_text(L("End in 1 day"))))));
 476    }
 477
 478    xs_html_add(form,
 479        xs_html_tag("p", NULL),
 480        xs_html_sctag("input",
 481            xs_html_attr("type",     "submit"),
 482            xs_html_attr("class",    "button"),
 483            xs_html_attr("value",    L("Post"))),
 484        xs_html_tag("p", NULL));
 485
 486    return note;
 487}
 488
 489
 490static xs_html *html_base_head(void)
 491{
 492    xs_html *head = xs_html_tag("head",
 493        xs_html_sctag("meta",
 494            xs_html_attr("name", "viewport"),
 495            xs_html_attr("content", "width=device-width, initial-scale=1")),
 496        xs_html_sctag("meta",
 497            xs_html_attr("name", "generator"),
 498            xs_html_attr("content", USER_AGENT)));
 499
 500    /* add server CSS and favicon */
 501    xs *f;
 502    f = xs_fmt("%s/favicon.ico", srv_baseurl);
 503    const xs_list *p = xs_dict_get(srv_config, "cssurls");
 504    const char *v;
 505    int c = 0;
 506
 507    while (xs_list_next(p, &v, &c)) {
 508        xs_html_add(head,
 509            xs_html_sctag("link",
 510                xs_html_attr("rel",  "stylesheet"),
 511                xs_html_attr("type", "text/css"),
 512                xs_html_attr("href", v)));
 513    }
 514
 515    xs_html_add(head,
 516        xs_html_sctag("link",
 517            xs_html_attr("rel",  "icon"),
 518            xs_html_attr("type", "image/x-icon"),
 519            xs_html_attr("href", f)));
 520
 521    return head;
 522}
 523
 524
 525xs_html *html_instance_head(void)
 526{
 527    xs_html *head = html_base_head();
 528
 529    {
 530        FILE *f;
 531        xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
 532
 533        if ((f = fopen(g_css_fn, "r")) != NULL) {
 534            xs *css = xs_readall(f);
 535            fclose(f);
 536
 537            xs_html_add(head,
 538                xs_html_tag("style",
 539                    xs_html_raw(css)));
 540        }
 541    }
 542
 543    const char *host  = xs_dict_get(srv_config, "host");
 544    const char *title = xs_dict_get(srv_config, "title");
 545
 546    xs_html_add(head,
 547        xs_html_tag("title",
 548            xs_html_text(title && *title ? title : host)));
 549
 550    return head;
 551}
 552
 553
 554static xs_html *html_instance_body(void)
 555{
 556    const char *host  = xs_dict_get(srv_config, "host");
 557    const char *sdesc = xs_dict_get(srv_config, "short_description");
 558    const char *email = xs_dict_get(srv_config, "admin_email");
 559    const char *acct  = xs_dict_get(srv_config, "admin_account");
 560
 561    xs *blurb = xs_replace(snac_blurb, "%host%", host);
 562
 563    xs_html *dl;
 564
 565    xs_html *body = xs_html_tag("body",
 566        xs_html_tag("div",
 567            xs_html_attr("class", "snac-instance-blurb"),
 568            xs_html_raw(blurb), /* pure html */
 569            dl = xs_html_tag("dl", NULL)));
 570
 571    if (sdesc && *sdesc) {
 572        xs_html_add(dl,
 573            xs_html_tag("di",
 574                xs_html_tag("dt",
 575                    xs_html_text(L("Site description"))),
 576                xs_html_tag("dd",
 577                    xs_html_text(sdesc))));
 578    }
 579    if (email && *email) {
 580        xs *mailto = xs_fmt("mailto:%s", email);
 581
 582        xs_html_add(dl,
 583            xs_html_tag("di",
 584                xs_html_tag("dt",
 585                    xs_html_text(L("Admin email"))),
 586                xs_html_tag("dd",
 587                    xs_html_tag("a",
 588                        xs_html_attr("href", mailto),
 589                        xs_html_text(email)))));
 590    }
 591    if (acct && *acct) {
 592        xs *url    = xs_fmt("%s/%s", srv_baseurl, acct);
 593        xs *handle = xs_fmt("@%s@%s", acct, host);
 594
 595        xs_html_add(dl,
 596            xs_html_tag("di",
 597                xs_html_tag("dt",
 598                    xs_html_text(L("Admin account"))),
 599                xs_html_tag("dd",
 600                    xs_html_tag("a",
 601                        xs_html_attr("href", url),
 602                        xs_html_text(handle)))));
 603    }
 604
 605    return body;
 606}
 607
 608
 609xs_html *html_user_head(snac *user, const char *desc, const char *url)
 610{
 611    xs_html *head = html_base_head();
 612
 613    /* add the user CSS */
 614    {
 615        xs *css = NULL;
 616        int size;
 617
 618        /* try to open the user css */
 619        if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
 620            /* it's not there; try to open the server-wide css */
 621            FILE *f;
 622            xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
 623
 624            if ((f = fopen(g_css_fn, "r")) != NULL) {
 625                css = xs_readall(f);
 626                fclose(f);
 627            }
 628        }
 629
 630        if (css != NULL) {
 631            xs_html_add(head,
 632                xs_html_tag("style",
 633                    xs_html_raw(css)));
 634        }
 635    }
 636
 637    /* title */
 638    xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
 639        user->uid, xs_dict_get(srv_config, "host"));
 640
 641    xs_html_add(head,
 642        xs_html_tag("title",
 643            xs_html_text(title)));
 644
 645    xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
 646
 647    if (avatar == NULL || *avatar == '\0') {
 648        xs_free(avatar);
 649        avatar = xs_fmt("%s/susie.png", srv_baseurl);
 650    }
 651
 652    /* create a description field */
 653    xs *s_desc = NULL;
 654    int n;
 655
 656    if (desc == NULL)
 657        s_desc = xs_dup(xs_dict_get(user->config, "bio"));
 658    else
 659        s_desc = xs_dup(desc);
 660
 661    /* show metrics in og:description? */
 662    if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
 663        xs *fwers = follower_list(user);
 664        xs *fwing = following_list(user);
 665
 666        xs *s1 = xs_fmt(L("%d following, %d followers ยท "),
 667            xs_list_len(fwing), xs_list_len(fwers));
 668
 669        s_desc = xs_str_prepend_i(s_desc, s1);
 670    }
 671
 672    /* shorten desc to a reasonable size */
 673    for (n = 0; s_desc[n]; n++) {
 674        if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
 675            break;
 676    }
 677
 678    s_desc[n] = '\0';
 679
 680    /* og properties */
 681    xs_html_add(head,
 682        xs_html_sctag("meta",
 683            xs_html_attr("property", "og:site_name"),
 684            xs_html_attr("content", xs_dict_get(srv_config, "host"))),
 685        xs_html_sctag("meta",
 686            xs_html_attr("property", "og:title"),
 687            xs_html_attr("content", title)),
 688        xs_html_sctag("meta",
 689            xs_html_attr("property", "og:description"),
 690            xs_html_attr("content", s_desc)),
 691        xs_html_sctag("meta",
 692            xs_html_attr("property", "og:image"),
 693            xs_html_attr("content", avatar)),
 694        xs_html_sctag("meta",
 695            xs_html_attr("property", "og:width"),
 696            xs_html_attr("content", "300")),
 697        xs_html_sctag("meta",
 698            xs_html_attr("property", "og:height"),
 699            xs_html_attr("content", "300")));
 700
 701    /* RSS link */
 702    xs *rss_url = xs_fmt("%s.rss", user->actor);
 703    xs_html_add(head,
 704        xs_html_sctag("link",
 705            xs_html_attr("rel", "alternate"),
 706            xs_html_attr("type", "application/rss+xml"),
 707            xs_html_attr("title", "RSS"),
 708            xs_html_attr("href", rss_url)));
 709
 710    /* ActivityPub alternate link (actor id) */
 711    xs_html_add(head,
 712        xs_html_sctag("link",
 713            xs_html_attr("rel", "alternate"),
 714            xs_html_attr("type", "application/activity+json"),
 715            xs_html_attr("href", url ? url : user->actor)));
 716
 717    return head;
 718}
 719
 720
 721static xs_html *html_user_body(snac *user, int read_only)
 722{
 723    const char *proxy = NULL;
 724
 725    if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
 726        proxy = user->actor;
 727
 728    xs_html *body = xs_html_tag("body", NULL);
 729
 730    /* top nav */
 731    xs_html *top_nav = xs_html_tag("nav",
 732        xs_html_attr("class", "snac-top-nav"));
 733
 734    xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
 735
 736    if (avatar == NULL || *avatar == '\0') {
 737        xs_free(avatar);
 738        avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
 739    }
 740
 741    xs_html_add(top_nav,
 742        xs_html_sctag("img",
 743            xs_html_attr("src", avatar),
 744            xs_html_attr("class", "snac-avatar"),
 745            xs_html_attr("alt", "")));
 746
 747    if (read_only) {
 748        xs *rss_url = xs_fmt("%s.rss", user->actor);
 749        xs *admin_url = xs_fmt("%s/admin", user->actor);
 750
 751        xs_html_add(top_nav,
 752            xs_html_tag("a",
 753                xs_html_attr("href", rss_url),
 754                xs_html_text(L("RSS"))),
 755            xs_html_text(" - "),
 756            xs_html_tag("a",
 757                xs_html_attr("href", admin_url),
 758                xs_html_attr("rel", "nofollow"),
 759                xs_html_text(L("private"))));
 760    }
 761    else {
 762        int n_len = notify_new_num(user);
 763        xs_html *notify_count = NULL;
 764
 765        /* show the number of new notifications, if there are any */
 766        if (n_len) {
 767            xs *n_len_str = xs_fmt(" %d ", n_len);
 768            notify_count = xs_html_tag("sup",
 769                xs_html_attr("style", "background-color: red; color: white;"),
 770                xs_html_text(n_len_str));
 771        }
 772        else
 773            notify_count = xs_html_text("");
 774
 775        xs *admin_url = xs_fmt("%s/admin", user->actor);
 776        xs *notify_url = xs_fmt("%s/notifications", user->actor);
 777        xs *people_url = xs_fmt("%s/people", user->actor);
 778        xs *instance_url = xs_fmt("%s/instance", user->actor);
 779
 780        xs_html_add(top_nav,
 781            xs_html_tag("a",
 782                xs_html_attr("href", user->actor),
 783                xs_html_text(L("public"))),
 784            xs_html_text(" - "),
 785            xs_html_tag("a",
 786                xs_html_attr("href", admin_url),
 787                xs_html_text(L("private"))),
 788            xs_html_text(" - "),
 789            xs_html_tag("a",
 790                xs_html_attr("href", notify_url),
 791                xs_html_text(L("notifications"))),
 792            notify_count,
 793            xs_html_text(" - "),
 794            xs_html_tag("a",
 795                xs_html_attr("href", people_url),
 796                xs_html_text(L("people"))),
 797            xs_html_text(" - "),
 798            xs_html_tag("a",
 799                xs_html_attr("href", instance_url),
 800                xs_html_text(L("instance"))),
 801            xs_html_text(" "),
 802            xs_html_tag("form",
 803                xs_html_attr("style", "display: inline!important"),
 804                xs_html_attr("class", "snac-search-box"),
 805                xs_html_attr("action", admin_url),
 806                    xs_html_sctag("input",
 807                        xs_html_attr("type", "text"),
 808                        xs_html_attr("name", "q"),
 809                        xs_html_attr("title", L("Search posts by URL or content (regular expression), @user@host accounts, or #tag")),
 810                        xs_html_attr("placeholder", L("Content search")))));
 811    }
 812
 813    xs_html_add(body,
 814        top_nav);
 815
 816    /* user info */
 817    xs_html *top_user = xs_html_tag("div",
 818        xs_html_attr("class", "h-card snac-top-user"));
 819
 820    if (read_only) {
 821        const char *header = xs_dict_get(user->config, "header");
 822        if (header && *header) {
 823            xs_html_add(top_user,
 824                xs_html_tag("div",
 825                    xs_html_attr("class", "snac-top-user-banner"),
 826                    xs_html_attr("style", "clear: both"),
 827                    xs_html_sctag("br", NULL),
 828                    xs_html_sctag("img",
 829                        xs_html_attr("src", header))));
 830        }
 831    }
 832
 833    xs *handle = xs_fmt("@%s@%s",
 834        xs_dict_get(user->config, "uid"),
 835        xs_dict_get(srv_config, "host"));
 836
 837    xs_html_add(top_user,
 838        xs_html_tag("p",
 839            xs_html_attr("class", "p-name snac-top-user-name"),
 840            xs_html_text(xs_dict_get(user->config, "name"))),
 841        xs_html_tag("p",
 842            xs_html_attr("class", "snac-top-user-id"),
 843            xs_html_text(handle)));
 844
 845    /** instance announcement **/
 846
 847    if (!read_only) {
 848        double la = 0.0;
 849        xs *user_la = xs_dup(xs_dict_get(user->config, "last_announcement"));
 850        if (user_la != NULL)
 851            la = xs_number_get(user_la);
 852
 853        const t_announcement *an = announcement(la);
 854        if (an != NULL && (an->text != NULL)) {
 855            xs *s = xs_fmt("?da=%.0f", an->timestamp);
 856
 857            xs_html_add(top_user,  xs_html_tag("div",
 858                xs_html_attr("class", "snac-announcement"),
 859                    xs_html_text(an->text),
 860                    xs_html_text(" "),
 861                    xs_html_tag("a",
 862                            xs_html_attr("href", s),
 863                            xs_html_text("Dismiss"))));
 864        }
 865    }
 866
 867    if (read_only) {
 868        xs *tags = xs_list_new();
 869        xs *bio1 = not_really_markdown(xs_dict_get(user->config, "bio"), NULL, &tags);
 870        xs *bio2 = process_tags(user, bio1, &tags);
 871        xs *bio3 = sanitize(bio2);
 872
 873        bio3 = replace_shortnames(bio3, tags, 2, proxy);
 874
 875        xs_html *top_user_bio = xs_html_tag("div",
 876            xs_html_attr("class", "p-note snac-top-user-bio"),
 877            xs_html_raw(bio3)); /* already sanitized */
 878
 879        xs_html_add(top_user,
 880            top_user_bio);
 881
 882        xs *metadata = NULL;
 883        const xs_dict *md = xs_dict_get(user->config, "metadata");
 884
 885        if (xs_type(md) == XSTYPE_DICT)
 886            metadata = xs_dup(md);
 887        else
 888        if (xs_type(md) == XSTYPE_STRING) {
 889            /* convert to dict for easier iteration */
 890            metadata = xs_dict_new();
 891            xs *l = xs_split(md, "\n");
 892            const char *ll;
 893
 894            xs_list_foreach(l, ll) {
 895                xs *kv = xs_split_n(ll, "=", 1);
 896                const char *k = xs_list_get(kv, 0);
 897                const char *v = xs_list_get(kv, 1);
 898
 899                if (k && v) {
 900                    xs *kk = xs_strip_i(xs_dup(k));
 901                    xs *vv = xs_strip_i(xs_dup(v));
 902                    metadata = xs_dict_set(metadata, kk, vv);
 903                }
 904            }
 905        }
 906
 907        if (xs_type(metadata) == XSTYPE_DICT) {
 908            const xs_str *k;
 909            const xs_str *v;
 910
 911            xs_dict *val_links = user->links;
 912            if (xs_is_null(val_links))
 913                val_links = xs_stock(XSTYPE_DICT);
 914
 915            xs_html *snac_metadata = xs_html_tag("div",
 916                xs_html_attr("class", "snac-metadata"));
 917
 918            int c = 0;
 919            while (xs_dict_next(metadata, &k, &v, &c)) {
 920                xs_html *value;
 921
 922                if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
 923                    /* is this link validated? */
 924                    xs *verified_link = NULL;
 925                    const xs_number *val_time = xs_dict_get(val_links, v);
 926
 927                    if (xs_type(val_time) == XSTYPE_NUMBER) {
 928                        time_t t = xs_number_get(val_time);
 929
 930                        if (t > 0) {
 931                            xs *s1 = xs_str_utctime(t, ISO_DATE_SPEC);
 932                            verified_link = xs_fmt("%s (%s)", L("verified link"), s1);
 933                        }
 934                    }
 935
 936                    if (!xs_is_null(verified_link)) {
 937                        value = xs_html_tag("span",
 938                            xs_html_attr("title", verified_link),
 939                            xs_html_raw("✔ "),
 940                            xs_html_tag("a",
 941                                xs_html_attr("rel", "me"),
 942                                xs_html_attr("target", "_blank"),
 943                                xs_html_attr("href", v),
 944                                xs_html_text(v)));
 945                    }
 946                    else {
 947                        value = xs_html_tag("a",
 948                            xs_html_attr("rel", "me"),
 949                            xs_html_attr("href", v),
 950                            xs_html_text(v));
 951                    }
 952                }
 953                else
 954                if (xs_startswith(v, "gemini:/") || xs_startswith(v, "xmpp:")) {
 955                    value = xs_html_tag("a",
 956                        xs_html_attr("rel", "me"),
 957                        xs_html_attr("href", v),
 958                        xs_html_text(v));
 959                }
 960                else
 961                    value = xs_html_text(v);
 962
 963                xs_html_add(snac_metadata,
 964                    xs_html_tag("span",
 965                        xs_html_attr("class", "snac-property-name"),
 966                        xs_html_text(k)),
 967                    xs_html_text(":"),
 968                    xs_html_raw(" "),
 969                    xs_html_tag("span",
 970                        xs_html_attr("class", "snac-property-value"),
 971                        value),
 972                    xs_html_sctag("br", NULL));
 973            }
 974
 975            xs_html_add(top_user,
 976                snac_metadata);
 977        }
 978
 979        const char *latitude = xs_dict_get_def(user->config, "latitude", "");
 980        const char *longitude = xs_dict_get_def(user->config, "longitude", "");
 981
 982        if (*latitude && *longitude) {
 983            xs *label = xs_fmt(L("%s,%s"), latitude, longitude);
 984            xs *url   = xs_fmt(L("https://openstreetmap.org/search?query=%s,%s"),
 985                        latitude, longitude);
 986
 987            xs_html_add(top_user,
 988                xs_html_tag("p",
 989                    xs_html_text(L("Location: ")),
 990                    xs_html_tag("a",
 991                        xs_html_attr("href", url),
 992                        xs_html_attr("target", "_blank"),
 993                        xs_html_text(label))));
 994        }
 995
 996        if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
 997            xs *fwers = follower_list(user);
 998            xs *fwing = following_list(user);
 999
1000            xs *s1 = xs_fmt(L("%d following %d followers"),
1001                xs_list_len(fwing), xs_list_len(fwers));
1002
1003            xs_html_add(top_user,
1004                xs_html_tag("p",
1005                    xs_html_text(s1)));
1006        }
1007    }
1008
1009    xs_html_add(body,
1010        top_user);
1011
1012    return body;
1013}
1014
1015
1016xs_html *html_top_controls(snac *snac)
1017/* generates the top controls */
1018{
1019    xs *ops_action = xs_fmt("%s/admin/action", snac->actor);
1020
1021    xs_html *top_controls = xs_html_tag("div",
1022        xs_html_attr("class", "snac-top-controls"),
1023
1024        /** new post **/
1025        html_note(snac, L("New Post..."),
1026            "new_post_div", "new_post_form",
1027            L("What's on your mind?"), "",
1028            NULL, NULL,
1029            xs_stock(XSTYPE_FALSE), "",
1030            xs_stock(XSTYPE_FALSE), NULL,
1031            NULL, 1, "", "", 0),
1032
1033        /** operations **/
1034        xs_html_tag("details",
1035            xs_html_tag("summary",
1036                xs_html_text(L("Operations..."))),
1037            xs_html_tag("p", NULL),
1038            xs_html_tag("form",
1039                xs_html_attr("autocomplete", "off"),
1040                xs_html_attr("method",       "post"),
1041                xs_html_attr("action",       ops_action),
1042                xs_html_sctag("input",
1043                    xs_html_attr("type",     "text"),
1044                    xs_html_attr("name",     "actor"),
1045                    xs_html_attr("required", "required"),
1046                    xs_html_attr("placeholder", "bob@example.com")),
1047                xs_html_text(" "),
1048                xs_html_sctag("input",
1049                    xs_html_attr("type",    "submit"),
1050                    xs_html_attr("name",    "action"),
1051                    xs_html_attr("value",   L("Follow"))),
1052            xs_html_text(" "),
1053            xs_html_text(L("(by URL or user@host)"))),
1054            xs_html_tag("p", NULL),
1055            xs_html_tag("form",
1056                xs_html_attr("autocomplete", "off"),
1057                xs_html_attr("method",       "post"),
1058                xs_html_attr("action",       ops_action),
1059                xs_html_sctag("input",
1060                    xs_html_attr("type",     "url"),
1061                    xs_html_attr("name",     "id"),
1062                    xs_html_attr("required", "required"),
1063                    xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
1064                xs_html_text(" "),
1065                xs_html_sctag("input",
1066                    xs_html_attr("type",    "submit"),
1067                    xs_html_attr("name",    "action"),
1068                    xs_html_attr("value",   L("Boost"))),
1069                xs_html_text(" "),
1070                xs_html_text(L("(by URL)"))),
1071            xs_html_tag("p", NULL),
1072            xs_html_tag("form",
1073                xs_html_attr("autocomplete", "off"),
1074                xs_html_attr("method",       "post"),
1075                xs_html_attr("action",       ops_action),
1076                xs_html_sctag("input",
1077                    xs_html_attr("type",     "text"),
1078                    xs_html_attr("name",     "id"),
1079                    xs_html_attr("required", "required"),
1080                    xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
1081                xs_html_text(" "),
1082                xs_html_sctag("input",
1083                    xs_html_attr("type",    "submit"),
1084                    xs_html_attr("name",    "action"),
1085                    xs_html_attr("value",   L("Like"))),
1086                xs_html_text(" "),
1087                xs_html_text(L("(by URL)"))),
1088            xs_html_tag("p", NULL)));
1089
1090    /** user settings **/
1091
1092    const char *email = "[disabled by admin]";
1093
1094    if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
1095        email = xs_dict_get(snac->config_o, "email");
1096        if (xs_is_null(email)) {
1097            email = xs_dict_get(snac->config, "email");
1098
1099            if (xs_is_null(email))
1100                email = "";
1101        }
1102    }
1103
1104    const char *cw = xs_dict_get(snac->config, "cw");
1105    if (xs_is_null(cw))
1106        cw = "";
1107
1108    const char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
1109    if (xs_is_null(telegram_bot))
1110        telegram_bot = "";
1111
1112    const char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
1113    if (xs_is_null(telegram_chat_id))
1114        telegram_chat_id = "";
1115
1116    const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
1117    if (xs_is_null(ntfy_server))
1118        ntfy_server = "";
1119
1120    const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
1121    if (xs_is_null(ntfy_token))
1122        ntfy_token = "";
1123
1124    const char *purge_days = xs_dict_get(snac->config, "purge_days");
1125    if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
1126        purge_days = (char *)xs_number_str(purge_days);
1127    else
1128        purge_days = "0";
1129
1130    const xs_val *d_dm_f_u  = xs_dict_get(snac->config, "drop_dm_from_unknown");
1131    const xs_val *bot       = xs_dict_get(snac->config, "bot");
1132    const xs_val *a_private = xs_dict_get(snac->config, "private");
1133    const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
1134    const xs_val *coll_thrds = xs_dict_get(snac->config, "collapse_threads");
1135    const xs_val *pending    = xs_dict_get(snac->config, "approve_followers");
1136    const xs_val *show_foll  = xs_dict_get(snac->config, "show_contact_metrics");
1137    const char *latitude     = xs_dict_get_def(snac->config, "latitude", "");
1138    const char *longitude    = xs_dict_get_def(snac->config, "longitude", "");
1139
1140    xs *metadata = NULL;
1141    const xs_dict *md = xs_dict_get(snac->config, "metadata");
1142
1143    if (xs_type(md) == XSTYPE_DICT) {
1144        const xs_str *k;
1145        const xs_str *v;
1146
1147        metadata = xs_str_new(NULL);
1148
1149        xs_dict_foreach(md, k, v) {
1150            xs *kp = xs_fmt("%s=%s", k, v);
1151
1152            if (*metadata)
1153                metadata = xs_str_cat(metadata, "\n");
1154            metadata = xs_str_cat(metadata, kp);
1155        }
1156    }
1157    else
1158    if (xs_type(md) == XSTYPE_STRING)
1159        metadata = xs_dup(md);
1160    else
1161        metadata = xs_str_new(NULL);
1162
1163    xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
1164
1165    xs_html_add(top_controls,
1166        xs_html_tag("details",
1167        xs_html_tag("summary",
1168            xs_html_text(L("User Settings..."))),
1169        xs_html_tag("div",
1170            xs_html_attr("class", "snac-user-setup"),
1171            xs_html_tag("form",
1172                xs_html_attr("autocomplete", "off"),
1173                xs_html_attr("method",       "post"),
1174                xs_html_attr("action",       user_setup_action),
1175                xs_html_attr("enctype",      "multipart/form-data"),
1176                xs_html_tag("p",
1177                    xs_html_text(L("Display name:")),
1178                    xs_html_sctag("br", NULL),
1179                    xs_html_sctag("input",
1180                        xs_html_attr("type", "text"),
1181                        xs_html_attr("name", "name"),
1182                        xs_html_attr("value", xs_dict_get(snac->config, "name")),
1183                        xs_html_attr("placeholder", L("Your name")))),
1184                xs_html_tag("p",
1185                    xs_html_text(L("Avatar: ")),
1186                    xs_html_sctag("input",
1187                        xs_html_attr("type", "file"),
1188                        xs_html_attr("name", "avatar_file"))),
1189                xs_html_tag("p",
1190                    xs_html_sctag("input",
1191                        xs_html_attr("type", "checkbox"),
1192                        xs_html_attr("name", "avatar_delete")),
1193                        xs_html_text(L("Delete current avatar"))),
1194                xs_html_tag("p",
1195                    xs_html_text(L("Header image (banner): ")),
1196                    xs_html_sctag("input",
1197                        xs_html_attr("type", "file"),
1198                        xs_html_attr("name", "header_file"))),
1199                xs_html_tag("p",
1200                    xs_html_sctag("input",
1201                        xs_html_attr("type", "checkbox"),
1202                        xs_html_attr("name", "header_delete")),
1203                        xs_html_text(L("Delete current header image"))),
1204                xs_html_tag("p",
1205                    xs_html_text(L("Bio:")),
1206                    xs_html_sctag("br", NULL),
1207                    xs_html_tag("textarea",
1208                        xs_html_attr("name", "bio"),
1209                        xs_html_attr("cols", "40"),
1210                        xs_html_attr("rows", "4"),
1211                        xs_html_attr("placeholder", L("Write about yourself here...")),
1212                        xs_html_text(xs_dict_get(snac->config, "bio")))),
1213                xs_html_sctag("input",
1214                    xs_html_attr("type", "checkbox"),
1215                    xs_html_attr("name", "cw"),
1216                    xs_html_attr("id",   "cw"),
1217                    xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
1218                xs_html_tag("label",
1219                    xs_html_attr("for", "cw"),
1220                    xs_html_text(L("Always show sensitive content"))),
1221                xs_html_tag("p",
1222                    xs_html_text(L("Email address for notifications:")),
1223                    xs_html_sctag("br", NULL),
1224                    xs_html_sctag("input",
1225                        xs_html_attr("type", "text"),
1226                        xs_html_attr("name", "email"),
1227                        xs_html_attr("value", email),
1228                        xs_html_attr("placeholder", "bob@example.com"))),
1229                xs_html_tag("p",
1230                    xs_html_text(L("Telegram notifications (bot key and chat id):")),
1231                    xs_html_sctag("br", NULL),
1232                    xs_html_sctag("input",
1233                        xs_html_attr("type", "text"),
1234                        xs_html_attr("name", "telegram_bot"),
1235                        xs_html_attr("value", telegram_bot),
1236                        xs_html_attr("placeholder", "Bot API key")),
1237                    xs_html_text(" "),
1238                    xs_html_sctag("input",
1239                        xs_html_attr("type", "text"),
1240                        xs_html_attr("name", "telegram_chat_id"),
1241                        xs_html_attr("value", telegram_chat_id),
1242                        xs_html_attr("placeholder", "Chat id"))),
1243                xs_html_tag("p",
1244                    xs_html_text(L("ntfy notifications (ntfy server and token):")),
1245                    xs_html_sctag("br", NULL),
1246                    xs_html_sctag("input",
1247                        xs_html_attr("type", "text"),
1248                        xs_html_attr("name", "ntfy_server"),
1249                        xs_html_attr("value", ntfy_server),
1250                        xs_html_attr("placeholder", "ntfy server - full URL (example: https://ntfy.sh/YourTopic)")),
1251                    xs_html_text(" "),
1252                    xs_html_sctag("input",
1253                        xs_html_attr("type", "text"),
1254                        xs_html_attr("name", "ntfy_token"),
1255                        xs_html_attr("value", ntfy_token),
1256                        xs_html_attr("placeholder", "ntfy token - if needed"))),
1257                xs_html_tag("p",
1258                    xs_html_text(L("Maximum days to keep posts (0: server settings):")),
1259                    xs_html_sctag("br", NULL),
1260                    xs_html_sctag("input",
1261                        xs_html_attr("type", "number"),
1262                        xs_html_attr("name", "purge_days"),
1263                        xs_html_attr("value", purge_days))),
1264                xs_html_tag("p",
1265                    xs_html_sctag("input",
1266                        xs_html_attr("type", "checkbox"),
1267                        xs_html_attr("name", "drop_dm_from_unknown"),
1268                        xs_html_attr("id", "drop_dm_from_unknown"),
1269                        xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
1270                    xs_html_tag("label",
1271                        xs_html_attr("for", "drop_dm_from_unknown"),
1272                        xs_html_text(L("Drop direct messages from people you don't follow")))),
1273                xs_html_tag("p",
1274                    xs_html_sctag("input",
1275                        xs_html_attr("type", "checkbox"),
1276                        xs_html_attr("name", "bot"),
1277                        xs_html_attr("id", "bot"),
1278                        xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
1279                    xs_html_tag("label",
1280                        xs_html_attr("for", "bot"),
1281                        xs_html_text(L("This account is a bot")))),
1282                xs_html_tag("p",
1283                    xs_html_sctag("input",
1284                        xs_html_attr("type", "checkbox"),
1285                        xs_html_attr("name", "auto_boost"),
1286                        xs_html_attr("id", "auto_boost"),
1287                        xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
1288                    xs_html_tag("label",
1289                        xs_html_attr("for", "auto_boost"),
1290                        xs_html_text(L("Auto-boost all mentions to this account")))),
1291                xs_html_tag("p",
1292                    xs_html_sctag("input",
1293                        xs_html_attr("type", "checkbox"),
1294                        xs_html_attr("name", "private"),
1295                        xs_html_attr("id", "private"),
1296                        xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
1297                    xs_html_tag("label",
1298                        xs_html_attr("for", "private"),
1299                        xs_html_text(L("This account is private "
1300                            "(posts are not shown through the web)")))),
1301                xs_html_tag("p",
1302                    xs_html_sctag("input",
1303                        xs_html_attr("type", "checkbox"),
1304                        xs_html_attr("name", "collapse_threads"),
1305                        xs_html_attr("id",   "collapse_threads"),
1306                        xs_html_attr(xs_is_true(coll_thrds) ? "checked" : "", NULL)),
1307                    xs_html_tag("label",
1308                        xs_html_attr("for", "collapse_threads"),
1309                        xs_html_text(L("Collapse top threads by default")))),
1310                xs_html_tag("p",
1311                    xs_html_sctag("input",
1312                        xs_html_attr("type", "checkbox"),
1313                        xs_html_attr("name", "approve_followers"),
1314                        xs_html_attr("id",   "approve_followers"),
1315                        xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
1316                    xs_html_tag("label",
1317                        xs_html_attr("for", "approve_followers"),
1318                        xs_html_text(L("Follow requests must be approved")))),
1319                xs_html_tag("p",
1320                    xs_html_sctag("input",
1321                        xs_html_attr("type", "checkbox"),
1322                        xs_html_attr("name", "show_contact_metrics"),
1323                        xs_html_attr("id",   "show_contact_metrics"),
1324                        xs_html_attr(xs_is_true(show_foll) ? "checked" : "", NULL)),
1325                    xs_html_tag("label",
1326                        xs_html_attr("for", "show_contact_metrics"),
1327                        xs_html_text(L("Publish follower and following metrics")))),
1328                xs_html_tag("p",
1329                    xs_html_text(L("Current location:")),
1330                    xs_html_sctag("br", NULL),
1331                    xs_html_sctag("input",
1332                        xs_html_attr("type", "text"),
1333                        xs_html_attr("name", "latitude"),
1334                        xs_html_attr("value", latitude),
1335                        xs_html_attr("placeholder", "latitude")),
1336                    xs_html_text(" "),
1337                    xs_html_sctag("input",
1338                        xs_html_attr("type", "text"),
1339                        xs_html_attr("name", "longitude"),
1340                        xs_html_attr("value", longitude),
1341                        xs_html_attr("placeholder", "longitude"))),
1342                xs_html_tag("p",
1343                    xs_html_text(L("Profile metadata (key=value pairs in each line):")),
1344                    xs_html_sctag("br", NULL),
1345                    xs_html_tag("textarea",
1346                        xs_html_attr("name", "metadata"),
1347                        xs_html_attr("cols", "40"),
1348                        xs_html_attr("rows", "4"),
1349                        xs_html_attr("placeholder", "Blog=https:/"
1350                            "/example.com/my-blog\nGPG Key=1FA54\n..."),
1351                    xs_html_text(metadata))),
1352
1353                xs_html_tag("p",
1354                    xs_html_text(L("New password:")),
1355                    xs_html_sctag("br", NULL),
1356                    xs_html_sctag("input",
1357                        xs_html_attr("type", "password"),
1358                        xs_html_attr("name", "passwd1"),
1359                        xs_html_attr("value", ""))),
1360                xs_html_tag("p",
1361                    xs_html_text(L("Repeat new password:")),
1362                    xs_html_sctag("br", NULL),
1363                    xs_html_sctag("input",
1364                        xs_html_attr("type", "password"),
1365                        xs_html_attr("name", "passwd2"),
1366                        xs_html_attr("value", ""))),
1367
1368                xs_html_sctag("input",
1369                    xs_html_attr("type", "submit"),
1370                    xs_html_attr("class", "button"),
1371                    xs_html_attr("value", L("Update user info"))),
1372
1373                xs_html_tag("p", NULL)))));
1374
1375    xs *followed_hashtags_action = xs_fmt("%s/admin/followed-hashtags", snac->actor);
1376    xs *followed_hashtags = xs_join(xs_dict_get_def(snac->config,
1377                        "followed_hashtags", xs_stock(XSTYPE_LIST)), "\n");
1378
1379    xs_html_add(top_controls,
1380        xs_html_tag("details",
1381        xs_html_tag("summary",
1382            xs_html_text(L("Followed hashtags..."))),
1383        xs_html_tag("p",
1384            xs_html_text(L("One hashtag per line"))),
1385        xs_html_tag("div",
1386            xs_html_attr("class", "snac-followed-hashtags"),
1387            xs_html_tag("form",
1388                xs_html_attr("autocomplete", "off"),
1389                xs_html_attr("method", "post"),
1390                xs_html_attr("action", followed_hashtags_action),
1391                xs_html_attr("enctype", "multipart/form-data"),
1392
1393                xs_html_tag("textarea",
1394                    xs_html_attr("name", "followed_hashtags"),
1395                    xs_html_attr("cols", "40"),
1396                    xs_html_attr("rows", "4"),
1397                    xs_html_attr("placeholder", "#cats\n#windowfriday\n#classicalmusic"),
1398                    xs_html_text(followed_hashtags)),
1399
1400                xs_html_tag("br", NULL),
1401
1402                xs_html_sctag("input",
1403                    xs_html_attr("type", "submit"),
1404                    xs_html_attr("class", "button"),
1405                    xs_html_attr("value", L("Update hashtags")))))));
1406
1407    return top_controls;
1408}
1409
1410
1411static xs_html *html_button(char *clss, char *label, char *hint)
1412{
1413    xs *c = xs_fmt("snac-btn-%s", clss);
1414
1415    /* use an NULL tag to separate non-css-classed buttons from one another */
1416    return xs_html_container(
1417        xs_html_sctag("input",
1418            xs_html_attr("type",    "submit"),
1419            xs_html_attr("name",    "action"),
1420            xs_html_attr("class",   c),
1421            xs_html_attr("value",   label),
1422            xs_html_attr("title",   hint)),
1423        xs_html_text("\n"));
1424}
1425
1426
1427xs_str *build_mentions(snac *snac, const xs_dict *msg)
1428/* returns a string with the mentions in msg */
1429{
1430    xs_str *s = xs_str_new(NULL);
1431    const char *list = xs_dict_get(msg, "tag");
1432    const char *v;
1433    int c = 0;
1434
1435    while (xs_list_next(list, &v, &c)) {
1436        const char *type = xs_dict_get(v, "type");
1437        const char *href = xs_dict_get(v, "href");
1438        const char *name = xs_dict_get(v, "name");
1439
1440        if (type && strcmp(type, "Mention") == 0 &&
1441            href && strcmp(href, snac->actor) != 0 && name) {
1442            xs *s1 = NULL;
1443
1444            if (name[0] != '@') {
1445                s1 = xs_fmt("@%s", name);
1446                name = s1;
1447            }
1448
1449            xs *l = xs_split(name, "@");
1450
1451            /* is it a name without a host? */
1452            if (xs_list_len(l) < 3) {
1453                /* split the href and pick the host name LIKE AN ANIMAL */
1454                /* would be better to query the webfinger but *won't do that* here */
1455                xs *l2 = xs_split(href, "/");
1456
1457                if (xs_list_len(l2) >= 3) {
1458                    xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
1459
1460                    if (xs_str_in(s, s1) == -1)
1461                        s = xs_str_cat(s, s1);
1462                }
1463            }
1464            else {
1465                if (xs_str_in(s, name) == -1) {
1466                    s = xs_str_cat(s, name);
1467                    s = xs_str_cat(s, " ");
1468                }
1469            }
1470        }
1471    }
1472
1473    if (*s) {
1474        xs *s1 = s;
1475        s = xs_fmt("\n\n\nCC: %s", s1);
1476    }
1477
1478    return s;
1479}
1480
1481
1482xs_html *html_entry_controls(snac *snac, const char *actor,
1483                            const xs_dict *msg, const char *md5)
1484{
1485    const char *id    = xs_dict_get(msg, "id");
1486    const char *group = xs_dict_get(msg, "audience");
1487
1488    xs *likes   = object_likes(id);
1489    xs *boosts  = object_announces(id);
1490
1491    xs *action = xs_fmt("%s/admin/action", snac->actor);
1492    xs *redir  = xs_fmt("%s_entry", md5);
1493
1494    xs_html *form;
1495    xs_html *controls = xs_html_tag("div",
1496        xs_html_attr("class", "snac-controls"),
1497        form = xs_html_tag("form",
1498            xs_html_attr("autocomplete", "off"),
1499            xs_html_attr("method",       "post"),
1500            xs_html_attr("action",       action),
1501            xs_html_sctag("input",
1502                xs_html_attr("type",     "hidden"),
1503                xs_html_attr("name",     "id"),
1504                xs_html_attr("value",    id)),
1505            xs_html_sctag("input",
1506                xs_html_attr("type",     "hidden"),
1507                xs_html_attr("name",     "actor"),
1508                xs_html_attr("value",    actor)),
1509            xs_html_sctag("input",
1510                xs_html_attr("type",     "hidden"),
1511                xs_html_attr("name",     "group"),
1512                xs_html_attr("value",    xs_is_null(group) ? "" : group)),
1513            xs_html_sctag("input",
1514                xs_html_attr("type",     "hidden"),
1515                xs_html_attr("name",     "redir"),
1516                xs_html_attr("value",    redir))));
1517
1518    if (!xs_startswith(id, snac->actor)) {
1519        if (xs_list_in(likes, snac->md5) == -1) {
1520            /* not already liked; add button */
1521            xs_html_add(form,
1522                html_button("like", L("Like"), L("Say you like this post")));
1523        }
1524        else {
1525            /* not like it anymore */
1526            xs_html_add(form,
1527                html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
1528        }
1529    }
1530    else {
1531        if (is_pinned(snac, id))
1532            xs_html_add(form,
1533                html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
1534        else
1535            xs_html_add(form,
1536                html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
1537    }
1538
1539    if (is_msg_public(msg)) {
1540        if (xs_list_in(boosts, snac->md5) == -1) {
1541            /* not already boosted; add button */
1542            xs_html_add(form,
1543                html_button("boost", L("Boost"), L("Announce this post to your followers")));
1544        }
1545        else {
1546            /* already boosted; add button to regret */
1547            xs_html_add(form,
1548                html_button("unboost", L("Unboost"), L("I regret I boosted this")));
1549        }
1550    }
1551
1552    if (is_bookmarked(snac, id))
1553            xs_html_add(form,
1554                html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
1555        else
1556            xs_html_add(form,
1557                html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
1558
1559    if (strcmp(actor, snac->actor) != 0) {
1560        /* controls for other actors than this one */
1561        if (following_check(snac, actor)) {
1562            xs_html_add(form,
1563                html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
1564        }
1565        else {
1566            xs_html_add(form,
1567                html_button("follow", L("Follow"), L("Start following this user's activity")));
1568        }
1569
1570        if (!xs_is_null(group)) {
1571            if (following_check(snac, group)) {
1572                xs_html_add(form,
1573                    html_button("unfollow", L("Unfollow Group"),
1574                        L("Stop following this group or channel")));
1575            }
1576            else {
1577                xs_html_add(form,
1578                    html_button("follow", L("Follow Group"),
1579                        L("Start following this group or channel")));
1580            }
1581        }
1582
1583        xs_html_add(form,
1584            html_button("mute", L("MUTE"),
1585                L("Block any activity from this user forever")));
1586    }
1587
1588    if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
1589        xs_html_add(form,
1590            html_button("delete", L("Delete"), L("Delete this post")));
1591
1592    xs_html_add(form,
1593        html_button("hide",   L("Hide"), L("Hide this post and its children")));
1594
1595    const char *prev_src = xs_dict_get(msg, "sourceContent");
1596
1597    if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) { /** edit **/
1598        /* post can be edited */
1599        xs *div_id  = xs_fmt("%s_edit", md5);
1600        xs *form_id = xs_fmt("%s_edit_form", md5);
1601        xs *redir   = xs_fmt("%s_entry", md5);
1602
1603        const char *att_file = "";
1604        const char *att_alt_text = "";
1605        const xs_list *att_list = xs_dict_get(msg, "attachment");
1606
1607        /* does it have an attachment? */
1608        if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) {
1609            const xs_dict *d = xs_list_get(att_list, 0);
1610
1611            if (xs_type(d) == XSTYPE_DICT) {
1612                att_file = xs_dict_get_def(d, "url", "");
1613                att_alt_text = xs_dict_get_def(d, "name", "");
1614            }
1615        }
1616
1617        xs_html_add(controls, xs_html_tag("div",
1618            xs_html_tag("p", NULL),
1619            html_note(snac, L("Edit..."),
1620                div_id, form_id,
1621                "", prev_src,
1622                id, NULL,
1623                xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1624                xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1625                NULL, 0, att_file, att_alt_text, is_draft(snac, id))),
1626            xs_html_tag("p", NULL));
1627    }
1628
1629    { /** reply **/
1630        /* the post textarea */
1631        xs *ct      = build_mentions(snac, msg);
1632        xs *div_id  = xs_fmt("%s_reply", md5);
1633        xs *form_id = xs_fmt("%s_reply_form", md5);
1634        xs *redir   = xs_fmt("%s_entry", md5);
1635
1636        xs_html_add(controls, xs_html_tag("div",
1637            xs_html_tag("p", NULL),
1638            html_note(snac, L("Reply..."),
1639                div_id, form_id,
1640                "", ct,
1641                NULL, NULL,
1642                xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1643                xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1644                id, 0, "", "", 0)),
1645            xs_html_tag("p", NULL));
1646    }
1647
1648    return controls;
1649}
1650
1651
1652xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
1653                   int level, const char *md5, int hide_children)
1654{
1655    const char *id    = xs_dict_get(msg, "id");
1656    const char *type  = xs_dict_get(msg, "type");
1657    const char *actor;
1658    const char *v;
1659    int has_title = 0;
1660    int collapse_threads = 0;
1661    const char *proxy = NULL;
1662
1663    if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
1664        proxy = user->actor;
1665
1666    /* do not show non-public messages in the public timeline */
1667    if ((read_only || !user) && !is_msg_public(msg))
1668        return NULL;
1669
1670    if (id && is_instance_blocked(id))
1671        return NULL;
1672
1673    if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
1674        collapse_threads = 1;
1675
1676    /* hidden? do nothing more for this conversation */
1677    if (user && is_hidden(user, id)) {
1678        xs *s1 = xs_fmt("%s_entry", md5);
1679
1680        /* return just an dummy anchor, to keep position after hitting 'Hide' */
1681        return xs_html_tag("div",
1682            xs_html_tag("a",
1683                xs_html_attr("name", s1)));
1684    }
1685
1686    /* avoid too deep nesting, as it may be a loop */
1687    if (level >= MAX_CONVERSATION_LEVELS)
1688        return xs_html_tag("mark",
1689            xs_html_text(L("Truncated (too deep)")));
1690
1691    if (strcmp(type, "Follow") == 0) {
1692        return xs_html_tag("div",
1693            xs_html_attr("class", "snac-post"),
1694            xs_html_tag("div",
1695                xs_html_attr("class", "snac-post-header"),
1696                xs_html_tag("div",
1697                    xs_html_attr("class", "snac-origin"),
1698                    xs_html_text(L("follows you"))),
1699                html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy)));
1700    }
1701    else
1702    if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
1703        /* skip oddities */
1704        snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
1705        return NULL;
1706    }
1707
1708    /* ignore notes with "name", as they are votes to Questions */
1709    if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
1710        return NULL;
1711
1712    /* get the attributedTo */
1713    if ((actor = get_atto(msg)) == NULL)
1714        return NULL;
1715
1716    /* ignore muted morons immediately */
1717    if (user && is_muted(user, actor)) {
1718        xs *s1 = xs_fmt("%s_entry", md5);
1719
1720        /* return just an dummy anchor, to keep position after hitting 'MUTE' */
1721        return xs_html_tag("div",
1722            xs_html_tag("a",
1723                xs_html_attr("name", s1)));
1724    }
1725
1726    if ((user == NULL || strcmp(actor, user->actor) != 0)
1727        && !valid_status(actor_get(actor, NULL)))
1728        return NULL;
1729
1730    /** html_entry top tag **/
1731    xs_html *entry_top = xs_html_tag("div", NULL);
1732
1733    {
1734        xs *s1 = xs_fmt("%s_entry", md5);
1735        xs_html_add(entry_top,
1736            xs_html_tag("a",
1737                xs_html_attr("name", s1)));
1738    }
1739
1740    xs_html *entry = xs_html_tag("div",
1741        xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
1742
1743    xs_html_add(entry_top,
1744        entry);
1745
1746    /** post header **/
1747
1748    xs_html *score;
1749    xs_html *post_header = xs_html_tag("div",
1750        xs_html_attr("class", "snac-post-header"),
1751        score = xs_html_tag("div",
1752            xs_html_attr("class", "snac-score")));
1753
1754    xs_html_add(entry,
1755        post_header);
1756
1757    if (user && is_pinned(user, id)) {
1758        /* add a pin emoji */
1759        xs_html_add(score,
1760            xs_html_tag("span",
1761                xs_html_attr("title", L("Pinned")),
1762                xs_html_raw(" &#128204; ")));
1763    }
1764
1765    if (user && is_bookmarked(user, id)) {
1766        /* add a bookmark emoji */
1767        xs_html_add(score,
1768            xs_html_tag("span",
1769                xs_html_attr("title", L("Bookmarked")),
1770                xs_html_raw(" &#128278; ")));
1771    }
1772
1773    if (strcmp(type, "Question") == 0) {
1774        /* add the ballot box emoji */
1775        xs_html_add(score,
1776            xs_html_tag("span",
1777                xs_html_attr("title", L("Poll")),
1778                xs_html_raw(" &#128499; ")));
1779
1780        if (user && was_question_voted(user, id)) {
1781            /* add a check to show this poll was voted */
1782            xs_html_add(score,
1783                xs_html_tag("span",
1784                    xs_html_attr("title", L("Voted")),
1785                    xs_html_raw(" &#10003; ")));
1786        }
1787    }
1788
1789    if (strcmp(type, "Event") == 0) {
1790        /* add the calendar emoji */
1791        xs_html_add(score,
1792            xs_html_tag("span",
1793                xs_html_attr("title", L("Event")),
1794                xs_html_raw(" &#128197; ")));
1795    }
1796
1797    /* if it's a user from this same instance, add the score */
1798    if (xs_startswith(id, srv_baseurl)) {
1799        int n_likes  = object_likes_len(id);
1800        int n_boosts = object_announces_len(id);
1801
1802        /* alternate emojis: %d &#128077; %d &#128257; */
1803        xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
1804
1805        xs_html_add(score,
1806            xs_html_raw(s1));
1807    }
1808
1809    xs *boosts = object_announces(id);
1810
1811    if (xs_list_len(boosts)) {
1812        /* if somebody boosted this, show as origin */
1813        const char *p = xs_list_get(boosts, -1);
1814        xs *actor_r = NULL;
1815
1816        if (user && xs_list_in(boosts, user->md5) != -1) {
1817            /* we boosted this */
1818            xs_html_add(post_header,
1819                xs_html_tag("div",
1820                    xs_html_attr("class", "snac-origin"),
1821                    xs_html_tag("a",
1822                        xs_html_attr("href", user->actor),
1823                        xs_html_text(xs_dict_get(user->config, "name"))),
1824                        xs_html_text(" "),
1825                        xs_html_text(L("boosted"))));
1826        }
1827        else
1828        if (valid_status(object_get_by_md5(p, &actor_r))) {
1829            xs *name = actor_name(actor_r, proxy);
1830
1831            if (!xs_is_null(name)) {
1832                xs *href = NULL;
1833                const char *id = xs_dict_get(actor_r, "id");
1834                int fwers = 0;
1835                int fwing = 0;
1836
1837                if (user != NULL) {
1838                    fwers = follower_check(user, id);
1839                    fwing = following_check(user, id);
1840                }
1841
1842                if (!read_only && (fwers || fwing))
1843                    href = xs_fmt("%s/people#%s", user->actor, p);
1844                else
1845                    href = xs_dup(id);
1846
1847                xs_html_add(post_header,
1848                    xs_html_tag("div",
1849                        xs_html_attr("class", "snac-origin"),
1850                        xs_html_tag("a",
1851                            xs_html_attr("href", href),
1852                            xs_html_raw(name)), /* already sanitized */
1853                            xs_html_text(" "),
1854                            xs_html_text(L("boosted"))));
1855            }
1856        }
1857    }
1858
1859    if (user && strcmp(type, "Note") == 0) {
1860        /* is the parent not here? */
1861        const char *parent = get_in_reply_to(msg);
1862
1863        if (!xs_is_null(parent) && *parent) {
1864            xs *md5 = xs_md5_hex(parent, strlen(parent));
1865
1866            if (!timeline_here(user, md5)) {
1867                xs_html_add(post_header,
1868                    xs_html_tag("div",
1869                        xs_html_attr("class", "snac-origin"),
1870                        xs_html_text(L("in reply to")),
1871                        xs_html_text(" "),
1872                        xs_html_tag("a",
1873                            xs_html_attr("href", parent),
1874                            xs_html_text("ยป"))));
1875            }
1876        }
1877    }
1878
1879    xs_html_add(post_header,
1880        html_msg_icon(read_only ? NULL : user, actor, msg, proxy));
1881
1882    /** post content **/
1883
1884    xs_html *snac_content_wrap = xs_html_tag("div",
1885        xs_html_attr("class", "e-content snac-content"));
1886
1887    xs_html_add(entry,
1888        snac_content_wrap);
1889
1890    if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
1891        xs_html_add(snac_content_wrap,
1892            xs_html_tag("h3",
1893                xs_html_attr("class", "snac-entry-title"),
1894                xs_html_text(v)));
1895
1896        has_title = 1;
1897    }
1898
1899    xs_html *snac_content = NULL;
1900
1901    v = xs_dict_get(msg, "summary");
1902
1903    /* is it sensitive? */
1904    if (xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
1905        if (xs_is_null(v) || *v == '\0')
1906            v = "...";
1907
1908        const char *cw = "";
1909
1910        if (user) {
1911            /* only show it when not in the public timeline and the config setting is "open" */
1912            cw = xs_dict_get(user->config, "cw");
1913            if (xs_is_null(cw) || read_only)
1914                cw = "";
1915        }
1916
1917        snac_content = xs_html_tag("details",
1918            xs_html_attr(cw, NULL),
1919            xs_html_tag("summary",
1920                xs_html_text(v),
1921                xs_html_text(L(" [SENSITIVE CONTENT]"))));
1922    }
1923    else {
1924        /* print the summary as a header (sites like e.g. Friendica can contain one) */
1925        if (!has_title && !xs_is_null(v) && *v) {
1926            xs_html_add(snac_content_wrap,
1927                xs_html_tag("h3",
1928                    xs_html_attr("class", "snac-entry-title"),
1929                    xs_html_text(v)));
1930
1931            has_title = 1;
1932        }
1933
1934        snac_content = xs_html_tag("div", NULL);
1935    }
1936
1937    xs_html_add(snac_content_wrap,
1938        snac_content);
1939
1940    {
1941        /** build the content string **/
1942        const char *content = xs_dict_get(msg, "content");
1943
1944        if (xs_type(content) != XSTYPE_STRING) {
1945            if (!xs_is_null(content))
1946                srv_archive_error("unexpected_content_xstype",
1947                    "content field type", xs_stock(XSTYPE_DICT), msg);
1948
1949            content = "";
1950        }
1951
1952        /* skip ugly line breaks at the beginning */
1953        while (xs_startswith(content, "<br>"))
1954            content += 4;
1955
1956        xs *c = sanitize(content);
1957
1958        /* do some tweaks to the content */
1959        c = xs_replace_i(c, "\r", "");
1960
1961        while (xs_endswith(c, "<br><br>"))
1962            c = xs_crop_i(c, 0, -4);
1963
1964        c = xs_replace_i(c, "<br><br>", "<p>");
1965
1966        c = xs_str_cat(c, "<p>");
1967
1968        /* replace the :shortnames: */
1969        c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
1970
1971        /* Peertube videos content is in markdown */
1972        const char *mtype = xs_dict_get(msg, "mediaType");
1973        if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
1974            /* a full conversion could be better */
1975            c = xs_replace_i(c, "\r", "");
1976            c = xs_replace_i(c, "\n", "<br>");
1977        }
1978
1979        /* c contains sanitized HTML */
1980        xs_html_add(snac_content,
1981            xs_html_raw(c));
1982    }
1983
1984    if (strcmp(type, "Question") == 0) { /** question content **/
1985        const xs_list *oo = xs_dict_get(msg, "oneOf");
1986        const xs_list *ao = xs_dict_get(msg, "anyOf");
1987        const xs_list *p;
1988        const xs_dict *v;
1989        int closed = 0;
1990        const char *f_closed = NULL;
1991
1992        xs_html *poll = xs_html_tag("div", NULL);
1993
1994        if (read_only)
1995            closed = 1; /* non-identified page; show as closed */
1996        else
1997        if (user && xs_startswith(id, user->actor))
1998            closed = 1; /* we questioned; closed for us */
1999        else
2000        if (user && was_question_voted(user, id))
2001            closed = 1; /* we already voted; closed for us */
2002
2003        if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
2004            /* it has a closed date... but is it in the past? */
2005            time_t t0 = time(NULL);
2006            time_t t1 = xs_parse_iso_date(f_closed, 0);
2007
2008            if (t1 < t0)
2009                closed = 2;
2010        }
2011
2012        /* get the appropriate list of options */
2013        p = oo != NULL ? oo : ao;
2014
2015        if (closed || user == NULL) {
2016            /* closed poll */
2017            xs_html *poll_result = xs_html_tag("table",
2018                xs_html_attr("class", "snac-poll-result"));
2019            int c = 0;
2020
2021            while (xs_list_next(p, &v, &c)) {
2022                const char *name       = xs_dict_get(v, "name");
2023                const xs_dict *replies = xs_dict_get(v, "replies");
2024
2025                if (name && replies) {
2026                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
2027
2028                    xs_html_add(poll_result,
2029                        xs_html_tag("tr",
2030                            xs_html_tag("td",
2031                                xs_html_text(name),
2032                                xs_html_text(":")),
2033                            xs_html_tag("td",
2034                                xs_html_text(ti))));
2035                }
2036            }
2037
2038            xs_html_add(poll,
2039                poll_result);
2040        }
2041        else {
2042            /* poll still active */
2043            xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
2044            xs_html *form;
2045            xs_html *poll_form = xs_html_tag("div",
2046                xs_html_attr("class", "snac-poll-form"),
2047                form = xs_html_tag("form",
2048                    xs_html_attr("autocomplete", "off"),
2049                    xs_html_attr("method", "post"),
2050                    xs_html_attr("action", vote_action),
2051                    xs_html_sctag("input",
2052                        xs_html_attr("type", "hidden"),
2053                        xs_html_attr("name", "actor"),
2054                        xs_html_attr("value", actor)),
2055                    xs_html_sctag("input",
2056                        xs_html_attr("type", "hidden"),
2057                        xs_html_attr("name", "irt"),
2058                        xs_html_attr("value", id))));
2059
2060            int c = 0;
2061            while (xs_list_next(p, &v, &c)) {
2062                const char *name = xs_dict_get(v, "name");
2063                const xs_dict *replies = xs_dict_get(v, "replies");
2064
2065                if (name) {
2066                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
2067
2068                    xs_html *btn = xs_html_sctag("input",
2069                            xs_html_attr("id", name),
2070                            xs_html_attr("value", name),
2071                            xs_html_attr("name", "question"));
2072
2073                    if (!xs_is_null(oo)) {
2074                        xs_html_add(btn,
2075                            xs_html_attr("type", "radio"),
2076                            xs_html_attr("required", "required"));
2077                    }
2078                    else
2079                        xs_html_add(btn,
2080                            xs_html_attr("type", "checkbox"));
2081
2082                    xs_html_add(form,
2083                        btn,
2084                        xs_html_text(" "),
2085                        xs_html_tag("span",
2086                            xs_html_attr("title", ti),
2087                            xs_html_text(name)),
2088                        xs_html_sctag("br", NULL));
2089                }
2090            }
2091
2092            xs_html_add(form,
2093                xs_html_tag("p", NULL),
2094                xs_html_sctag("input",
2095                    xs_html_attr("type", "submit"),
2096                    xs_html_attr("class", "button"),
2097                    xs_html_attr("value", L("Vote"))));
2098
2099            xs_html_add(poll,
2100                poll_form);
2101        }
2102
2103        /* if it's *really* closed, say it */
2104        if (closed == 2) {
2105            xs_html_add(poll,
2106                xs_html_tag("p",
2107                    xs_html_text(L("Closed"))));
2108        }
2109        else {
2110            /* show when the poll closes */
2111            const char *end_time = xs_dict_get(msg, "endTime");
2112
2113            /* Pleroma does not have an endTime field;
2114               it has a closed time in the future */
2115            if (xs_is_null(end_time))
2116                end_time = xs_dict_get(msg, "closed");
2117
2118            if (!xs_is_null(end_time)) {
2119                time_t t0 = time(NULL);
2120                time_t t1 = xs_parse_iso_date(end_time, 0);
2121
2122                if (t1 > 0 && t1 > t0) {
2123                    time_t diff_time = t1 - t0;
2124                    xs *tf = xs_str_time_diff(diff_time);
2125                    char *p = tf;
2126
2127                    /* skip leading zeros */
2128                    for (; *p == '0' || *p == ':'; p++);
2129
2130                    xs_html_add(poll,
2131                        xs_html_tag("p",
2132                            xs_html_text(L("Closes in")),
2133                            xs_html_text(" "),
2134                            xs_html_text(p)));
2135                }
2136            }
2137        }
2138
2139        xs_html_add(snac_content,
2140            poll);
2141    }
2142
2143    /** attachments **/
2144    xs *attach = get_attachments(msg);
2145
2146    {
2147        /* make custom css for attachments easier */
2148        xs_html *content_attachments = xs_html_tag("div",
2149            xs_html_attr("class", "snac-content-attachments"));
2150
2151        xs_html_add(snac_content,
2152            content_attachments);
2153
2154        const char *content = xs_dict_get(msg, "content");
2155
2156        int c = 0;
2157        const xs_dict *a;
2158        while (xs_list_next(attach, &a, &c)) {
2159            const char *type = xs_dict_get(a, "type");
2160            const char *o_href = xs_dict_get(a, "href");
2161            const char *name = xs_dict_get(a, "name");
2162
2163            /* if this image is already in the post content, skip */
2164            if (content && xs_str_in(content, o_href) != -1)
2165                continue;
2166
2167            /* do this attachment include an icon? */
2168            const xs_dict *icon = xs_dict_get(a, "icon");
2169            if (xs_type(icon) == XSTYPE_DICT) {
2170                const char *icon_mtype = xs_dict_get(icon, "mediaType");
2171                const char *icon_url   = xs_dict_get(icon, "url");
2172
2173                if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
2174                    xs_html_add(content_attachments,
2175                        xs_html_tag("a",
2176                            xs_html_attr("href", icon_url),
2177                            xs_html_attr("target", "_blank"),
2178                            xs_html_sctag("img",
2179                                xs_html_attr("loading", "lazy"),
2180                                xs_html_attr("src", icon_url))));
2181                }
2182            }
2183
2184            xs *href = make_url(o_href, proxy, 0);
2185
2186            if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
2187                xs_html_add(content_attachments,
2188                    xs_html_tag("a",
2189                        xs_html_attr("href", href),
2190                        xs_html_attr("target", "_blank"),
2191                        xs_html_sctag("img",
2192                            xs_html_attr("loading", "lazy"),
2193                            xs_html_attr("src", href),
2194                            xs_html_attr("alt", name),
2195                            xs_html_attr("title", name))));
2196            }
2197            else
2198            if (xs_startswith(type, "video/")) {
2199                xs_html_add(content_attachments,
2200                    xs_html_tag("video",
2201                        xs_html_attr("preload", "none"),
2202                        xs_html_attr("style", "width: 100%"),
2203                        xs_html_attr("class", "snac-embedded-video"),
2204                        xs_html_attr("controls", NULL),
2205                        xs_html_attr("src", href),
2206                        xs_html_text(L("Video")),
2207                        xs_html_text(": "),
2208                        xs_html_tag("a",
2209                            xs_html_attr("href", href),
2210                            xs_html_text(name))));
2211            }
2212            else
2213            if (xs_startswith(type, "audio/")) {
2214                xs_html_add(content_attachments,
2215                    xs_html_tag("audio",
2216                        xs_html_attr("preload", "none"),
2217                        xs_html_attr("style", "width: 100%"),
2218                        xs_html_attr("class", "snac-embedded-audio"),
2219                        xs_html_attr("controls", NULL),
2220                        xs_html_attr("src", href),
2221                        xs_html_text(L("Audio")),
2222                        xs_html_text(": "),
2223                        xs_html_tag("a",
2224                            xs_html_attr("href", href),
2225                            xs_html_text(name))));
2226            }
2227            else
2228            if (strcmp(type, "Link") == 0) {
2229                xs_html_add(content_attachments,
2230                    xs_html_tag("p",
2231                        xs_html_tag("a",
2232                            xs_html_attr("href", o_href),
2233                            xs_html_text(href))));
2234
2235                /* do not generate an Alt... */
2236                name = NULL;
2237            }
2238            else {
2239                xs_html_add(content_attachments,
2240                    xs_html_tag("p",
2241                        xs_html_tag("a",
2242                            xs_html_attr("href", o_href),
2243                            xs_html_text(L("Attachment")),
2244                            xs_html_text(": "),
2245                            xs_html_text(o_href))));
2246
2247                /* do not generate an Alt... */
2248                name = NULL;
2249            }
2250
2251            if (name != NULL && *name) {
2252                xs_html_add(content_attachments,
2253                    xs_html_tag("p",
2254                        xs_html_attr("class", "snac-alt-text"),
2255                        xs_html_tag("details",
2256                            xs_html_tag("summary",
2257                                xs_html_text(L("Alt..."))),
2258                            xs_html_text(name))));
2259            }
2260        }
2261    }
2262
2263    /* has this message an audience (i.e., comes from a channel or community)? */
2264    const char *audience = xs_dict_get(msg, "audience");
2265    if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
2266        xs_html *au_tag = xs_html_tag("p",
2267            xs_html_text("("),
2268            xs_html_tag("a",
2269                xs_html_attr("href", audience),
2270                xs_html_attr("title", L("Source channel or community")),
2271                xs_html_text(audience)),
2272            xs_html_text(")"));
2273
2274        xs_html_add(snac_content_wrap,
2275            au_tag);
2276    }
2277
2278    /* does it have a location? */
2279    const xs_dict *location = xs_dict_get(msg, "location");
2280    if (xs_type(location) == XSTYPE_DICT) {
2281        const xs_number *latitude = xs_dict_get(location, "latitude");
2282        const xs_number *longitude = xs_dict_get(location, "longitude");
2283        const char *name = xs_dict_get(location, "name");
2284        const char *address = xs_dict_get(location, "address");
2285        xs *label_list = xs_list_new();
2286
2287        if (xs_type(name) == XSTYPE_STRING)
2288            label_list = xs_list_append(label_list, name);
2289        if (xs_type(address) == XSTYPE_STRING)
2290            label_list = xs_list_append(label_list, address);
2291
2292        if (xs_list_len(label_list)) {
2293            const char *url = xs_dict_get(location, "url");
2294            xs *label = xs_join(label_list, ", ");
2295
2296            if (xs_type(url) == XSTYPE_STRING) {
2297                xs_html_add(snac_content_wrap,
2298                    xs_html_tag("p",
2299                        xs_html_text(L("Location: ")),
2300                        xs_html_tag("a",
2301                            xs_html_attr("href", url),
2302                            xs_html_attr("target", "_blank"),
2303                            xs_html_text(label))));
2304            }
2305            else
2306            if (!xs_is_null(latitude) && !xs_is_null(longitude)) {
2307                xs *url = xs_fmt("https://openstreetmap.org/search/?query=%s,%s",
2308                    xs_number_str(latitude), xs_number_str(longitude));
2309
2310                xs_html_add(snac_content_wrap,
2311                    xs_html_tag("p",
2312                        xs_html_text(L("Location: ")),
2313                        xs_html_tag("a",
2314                            xs_html_attr("href", url),
2315                            xs_html_attr("target", "_blank"),
2316                            xs_html_text(label))));
2317            }
2318            else
2319                xs_html_add(snac_content_wrap,
2320                    xs_html_tag("p",
2321                        xs_html_text(L("Location: ")),
2322                        xs_html_text(label)));
2323        }
2324    }
2325
2326    if (strcmp(type, "Event") == 0) { /** Event start and end times **/
2327        const char *s_time = xs_dict_get(msg, "startTime");
2328
2329        if (xs_is_string(s_time) && strlen(s_time) > 20) {
2330            const char *e_time = xs_dict_get(msg, "endTime");
2331            const char *tz     = xs_dict_get(msg, "timezone");
2332
2333            xs *s = xs_replace_i(xs_dup(s_time), "T", " ");
2334            xs *e = NULL;
2335
2336            if (xs_is_string(e_time) && strlen(e_time) > 20)
2337                e = xs_replace_i(xs_dup(e_time), "T", " ");
2338
2339            /* if the event has a timezone, crop the offsets */
2340            if (xs_is_string(tz)) {
2341                s = xs_crop_i(s, 0, 19);
2342
2343                if (e)
2344                    e = xs_crop_i(e, 0, 19);
2345            }
2346            else
2347                tz = "";
2348
2349            /* if start and end share the same day, crop it from the end */
2350            if (e && memcmp(s, e, 11) == 0)
2351                e = xs_crop_i(e, 11, 0);
2352
2353            if (e)
2354                s = xs_str_cat(s, " / ", e);
2355
2356            if (*tz)
2357                s = xs_str_cat(s, " (", tz, ")");
2358
2359            /* replace ugly decimals */
2360            s = xs_replace_i(s, ".000", "");
2361
2362            xs_html_add(snac_content_wrap,
2363                xs_html_tag("p",
2364                    xs_html_text(L("Time: ")),
2365                    xs_html_text(s)));
2366        }
2367    }
2368
2369    /* show all hashtags that has not been shown previously in the content */
2370    const xs_list *tags = xs_dict_get(msg, "tag");
2371    const char *o_content = xs_dict_get_def(msg, "content", "");
2372
2373    if (xs_is_string(o_content) && xs_is_list(tags) && xs_list_len(tags)) {
2374        xs *content = xs_utf8_to_lower(o_content);
2375        const xs_dict *tag;
2376
2377        xs_html *add_hashtags = xs_html_tag("ul",
2378            xs_html_attr("class", "snac-more-hashtags"));
2379
2380        xs_list_foreach(tags, tag) {
2381            const char *type = xs_dict_get(tag, "type");
2382
2383            if (xs_is_string(type) && strcmp(type, "Hashtag") == 0) {
2384                const char *o_href = xs_dict_get(tag, "href");
2385                const char *name   = xs_dict_get(tag, "name");
2386
2387                if (xs_is_string(o_href) && xs_is_string(name)) {
2388                    xs *href = xs_utf8_to_lower(o_href);
2389
2390                    if (xs_str_in(content, href) == -1 && xs_str_in(content, name) == -1) {
2391                        /* not in the content: add here */
2392                        xs_html_add(add_hashtags,
2393                            xs_html_tag("li",
2394                                xs_html_tag("a",
2395                                    xs_html_attr("href", href),
2396                                    xs_html_text(name),
2397                                    xs_html_text(" "))));
2398                    }
2399                }
2400            }
2401        }
2402
2403        xs_html_add(snac_content_wrap,
2404            add_hashtags);
2405    }
2406
2407    /** controls **/
2408
2409    if (!read_only && user) {
2410        xs_html_add(entry,
2411            html_entry_controls(user, actor, msg, md5));
2412    }
2413
2414    /** children **/
2415    if (!hide_children) {
2416        xs *children = object_children(id);
2417        int left     = xs_list_len(children);
2418
2419        if (left) {
2420            xs_html *ch_details = xs_html_tag("details",
2421                xs_html_attr(collapse_threads ? "" : "open", NULL),
2422                xs_html_tag("summary",
2423                    xs_html_text("...")));
2424
2425            xs_html_add(entry,
2426                ch_details);
2427
2428            xs_html *ch_container = xs_html_tag("div",
2429                xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
2430
2431            xs_html_add(ch_details,
2432                ch_container);
2433
2434            xs_html *ch_older = NULL;
2435            if (left > 3) {
2436                xs_html_add(ch_container,
2437                    ch_older = xs_html_tag("details",
2438                        xs_html_tag("summary",
2439                            xs_html_text(L("Older...")))));
2440            }
2441
2442            xs_list *p = children;
2443            const char *cmd5;
2444            int cnt = 0;
2445            int o_cnt = 0;
2446
2447            while (xs_list_iter(&p, &cmd5)) {
2448                xs *chd = NULL;
2449
2450                if (user)
2451                    timeline_get_by_md5(user, cmd5, &chd);
2452                else
2453                    object_get_by_md5(cmd5, &chd);
2454
2455                if (chd != NULL) {
2456                    if (xs_is_null(xs_dict_get(chd, "name"))) {
2457                        xs_html *che = html_entry(user, chd, read_only,
2458                            level + 1, cmd5, hide_children);
2459
2460                        if (che != NULL) {
2461                            if (left > 3) {
2462                                xs_html_add(ch_older,
2463                                    che);
2464
2465                                o_cnt++;
2466                            }
2467                            else
2468                                xs_html_add(ch_container,
2469                                    che);
2470
2471                            cnt++;
2472                        }
2473                    }
2474
2475                    left--;
2476                }
2477                else
2478                    srv_debug(2, xs_fmt("cannot read child %s", cmd5));
2479            }
2480
2481            /* if no children were finally added, hide the details */
2482            if (cnt == 0)
2483                xs_html_add(ch_details,
2484                    xs_html_attr("style", "display: none"));
2485
2486            if (o_cnt == 0 && ch_older)
2487                xs_html_add(ch_older,
2488                    xs_html_attr("style", "display: none"));
2489        }
2490    }
2491
2492    return entry_top;
2493}
2494
2495
2496xs_html *html_footer(void)
2497{
2498    return xs_html_tag("div",
2499        xs_html_attr("class", "snac-footer"),
2500        xs_html_tag("a",
2501            xs_html_attr("href", srv_baseurl),
2502            xs_html_text(L("about this site"))),
2503        xs_html_text(" - "),
2504        xs_html_text(L("powered by ")),
2505        xs_html_tag("a",
2506            xs_html_attr("href", WHAT_IS_SNAC_URL),
2507            xs_html_tag("abbr",
2508                xs_html_attr("title", "Social Networks Are Crap"),
2509                xs_html_text("snac"))));
2510}
2511
2512
2513xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
2514                      int skip, int show, int show_more,
2515                      const char *title, const char *page,
2516                      int utl, const char *error)
2517/* returns the HTML for the timeline */
2518{
2519    xs_list *p = (xs_list *)list;
2520    const char *v;
2521    double t = ftime();
2522    int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
2523
2524    xs *desc = NULL;
2525    xs *alternate = NULL;
2526
2527    if (xs_list_len(list) == 1) {
2528        /* only one element? pick the description from the source */
2529        const char *id = xs_list_get(list, 0);
2530        xs *d = NULL;
2531        object_get_by_md5(id, &d);
2532        const char *sc = xs_dict_get(d, "sourceContent");
2533        if (d && sc != NULL)
2534            desc = xs_dup(sc);
2535
2536        alternate = xs_dup(xs_dict_get(d, "id"));
2537    }
2538
2539    xs_html *head;
2540    xs_html *body;
2541
2542    if (user) {
2543        head = html_user_head(user, desc, alternate);
2544        body = html_user_body(user, read_only);
2545    }
2546    else {
2547        head = html_instance_head();
2548        body = html_instance_body();
2549    }
2550
2551    xs_html *html = xs_html_tag("html",
2552        head,
2553        body);
2554
2555    if (user && !read_only)
2556        xs_html_add(body,
2557            html_top_controls(user));
2558
2559    if (error != NULL) {
2560        xs_html_add(body,
2561            xs_html_tag("dialog",
2562                xs_html_attr("open", NULL),
2563                xs_html_tag("p",
2564                    xs_html_text(error)),
2565                xs_html_tag("form",
2566                    xs_html_attr("method", "dialog"),
2567                    xs_html_sctag("input",
2568                        xs_html_attr("type", "submit"),
2569                        xs_html_attr("value", L("Dismiss"))))));
2570    }
2571
2572    /* show links to the available lists */
2573    if (user && !read_only) {
2574        xs_html *lol = xs_html_tag("ul",
2575            xs_html_attr("class", "snac-list-of-lists"));
2576        xs_html_add(body, lol);
2577
2578        xs *lists = list_maint(user, NULL, 0); /* get list of lists */
2579
2580        int ct = 0;
2581        const char *v;
2582
2583        while (xs_list_next(lists, &v, &ct)) {
2584            const char *lname = xs_list_get(v, 1);
2585            xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
2586            xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
2587
2588            xs_html_add(lol,
2589                xs_html_tag("li",
2590                    xs_html_tag("a",
2591                        xs_html_attr("href", url),
2592                        xs_html_attr("class", "snac-list-link"),
2593                        xs_html_attr("title", ttl),
2594                        xs_html_text(lname))));
2595        }
2596
2597        {
2598            /* show the list of pinned posts */
2599            xs *url = xs_fmt("%s/pinned", user->actor);
2600            xs_html_add(lol,
2601                xs_html_tag("li",
2602                    xs_html_tag("a",
2603                        xs_html_attr("href", url),
2604                        xs_html_attr("class", "snac-list-link"),
2605                        xs_html_attr("title", L("Pinned posts")),
2606                        xs_html_text("pinned"))));
2607        }
2608
2609        {
2610            /* show the list of bookmarked posts */
2611            xs *url = xs_fmt("%s/bookmarks", user->actor);
2612            xs_html_add(lol,
2613                xs_html_tag("li",
2614                    xs_html_tag("a",
2615                        xs_html_attr("href", url),
2616                        xs_html_attr("class", "snac-list-link"),
2617                        xs_html_attr("title", L("Bookmarked posts")),
2618                        xs_html_text("bookmarks"))));
2619        }
2620
2621        {
2622            /* show the list of drafts */
2623            xs *url = xs_fmt("%s/drafts", user->actor);
2624            xs_html_add(lol,
2625                xs_html_tag("li",
2626                    xs_html_tag("a",
2627                        xs_html_attr("href", url),
2628                        xs_html_attr("class", "snac-list-link"),
2629                        xs_html_attr("title", L("Post drafts")),
2630                        xs_html_text("drafts"))));
2631        }
2632    }
2633
2634    xs_html_add(body,
2635        xs_html_tag("a",
2636            xs_html_attr("name", "snac-posts")));
2637
2638    xs_html *posts = xs_html_tag("div",
2639        xs_html_attr("class", "snac-posts"));
2640
2641    if (title) {
2642        xs_html_add(posts,
2643            xs_html_tag("h2",
2644                xs_html_attr("class", "snac-header"),
2645                xs_html_text(title)));
2646    }
2647
2648    xs_html_add(body,
2649        posts);
2650
2651    int mark_shown = 0;
2652
2653    while (xs_list_iter(&p, &v)) {
2654        xs *msg = NULL;
2655        int status;
2656
2657        /* "already seen" mark? */
2658        if (strcmp(v, MD5_ALREADY_SEEN_MARK) == 0) {
2659            if (skip == 0 && !mark_shown) {
2660                xs *s = xs_fmt("%s/admin", user->actor);
2661
2662                xs_html_add(posts,
2663                    xs_html_tag("div",
2664                        xs_html_attr("class", "snac-no-more-unseen-posts"),
2665                        xs_html_text(L("No more unseen posts")),
2666                        xs_html_text(" - "),
2667                        xs_html_tag("a",
2668                            xs_html_attr("href", s),
2669                            xs_html_text(L("Back to top")))));
2670            }
2671
2672            mark_shown = 1;
2673
2674            continue;
2675        }
2676
2677        if (utl && user && !is_pinned_by_md5(user, v))
2678            status = timeline_get_by_md5(user, v, &msg);
2679        else
2680            status = object_get_by_md5(v, &msg);
2681
2682        if (!valid_status(status))
2683            continue;
2684
2685        /* if it's an instance page, discard messages from private users */
2686        if (user == NULL && is_msg_from_private_user(msg))
2687            continue;
2688
2689        /* is this message a non-public reply? */
2690        if (user != NULL && !is_msg_public(msg)) {
2691            const char *irt = get_in_reply_to(msg);
2692
2693            /* is it a reply to something not in the storage? */
2694            if (!xs_is_null(irt) && !object_here(irt)) {
2695                /* is it for me? */
2696                const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
2697                const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
2698
2699                if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
2700                    snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
2701                    continue;
2702                }
2703            }
2704        }
2705
2706        xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
2707
2708        if (entry != NULL)
2709            xs_html_add(posts,
2710                entry);
2711    }
2712
2713    if (list && user && read_only) {
2714        /** history **/
2715        if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
2716            xs_html *ul = xs_html_tag("ul", NULL);
2717
2718            xs_html *history = xs_html_tag("div",
2719                xs_html_attr("class", "snac-history"),
2720                xs_html_tag("p",
2721                    xs_html_attr("class", "snac-history-title"),
2722                    xs_html_text(L("History"))),
2723                    ul);
2724
2725            xs *list = history_list(user);
2726            xs_list *p = list;
2727            const char *v;
2728
2729            while (xs_list_iter(&p, &v)) {
2730                xs *fn  = xs_replace(v, ".html", "");
2731                xs *url = xs_fmt("%s/h/%s", user->actor, v);
2732
2733                xs_html_add(ul,
2734                    xs_html_tag("li",
2735                        xs_html_tag("a",
2736                            xs_html_attr("href", url),
2737                            xs_html_text(fn))));
2738            }
2739
2740            xs_html_add(body,
2741                history);
2742        }
2743    }
2744
2745    {
2746        xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
2747        xs_html_add(body,
2748            xs_html_raw(s1));
2749    }
2750
2751    if (show_more) {
2752        xs *m  = NULL;
2753        xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
2754
2755        xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
2756
2757        if (page != NULL)
2758            url = xs_str_cat(url, page);
2759
2760        if (xs_str_in(url, "?") != -1)
2761            m = xs_fmt("%s&%s", url, ss);
2762        else
2763            m = xs_fmt("%s?%s", url, ss);
2764
2765        xs_html *more_links = xs_html_tag("p",
2766            xs_html_tag("a",
2767                xs_html_attr("href", url),
2768                xs_html_attr("name", "snac-more"),
2769                xs_html_text(L("Back to top"))),
2770            xs_html_text(" - "),
2771            xs_html_tag("a",
2772                xs_html_attr("href", m),
2773                xs_html_attr("name", "snac-more"),
2774                xs_html_text(L("More..."))));
2775
2776        xs_html_add(body,
2777            more_links);
2778    }
2779
2780    xs_html_add(body,
2781        html_footer());
2782
2783    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2784}
2785
2786
2787xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, const char *proxy)
2788{
2789    xs_html *snac_posts;
2790    xs_html *people = xs_html_tag("div",
2791        xs_html_tag("h2",
2792            xs_html_attr("class", "snac-header"),
2793            xs_html_text(header)),
2794        snac_posts = xs_html_tag("details",
2795                xs_html_attr("open", NULL),
2796                xs_html_tag("summary",
2797                    xs_html_text("..."))));
2798
2799    const char *actor_id;
2800
2801    xs_list_foreach(list, actor_id) {
2802        xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
2803        xs *actor = NULL;
2804
2805        if (valid_status(actor_get(actor_id, &actor))) {
2806            xs_html *snac_post = xs_html_tag("div",
2807                xs_html_attr("class", "snac-post"),
2808                xs_html_tag("a",
2809                    xs_html_attr("name", md5)),
2810                xs_html_tag("div",
2811                    xs_html_attr("class", "snac-post-header"),
2812                    html_actor_icon(snac, actor, xs_dict_get(actor, "published"),
2813                                    NULL, NULL, 0, 1, proxy, NULL)));
2814
2815            /* content (user bio) */
2816            const char *c = xs_dict_get(actor, "summary");
2817
2818            if (!xs_is_null(c)) {
2819                xs *sc = sanitize(c);
2820
2821                xs_html *snac_content = xs_html_tag("div",
2822                    xs_html_attr("class", "snac-content"));
2823
2824                if (xs_startswith(sc, "<p>"))
2825                    xs_html_add(snac_content,
2826                        xs_html_raw(sc)); /* already sanitized */
2827                else
2828                    xs_html_add(snac_content,
2829                        xs_html_tag("p",
2830                            xs_html_raw(sc))); /* already sanitized */
2831
2832                xs_html_add(snac_post, snac_content);
2833            }
2834
2835            /* buttons */
2836            xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
2837
2838            xs_html *snac_controls = xs_html_tag("div",
2839                xs_html_attr("class",   "snac-controls"));
2840
2841            xs_html *form = xs_html_tag("form",
2842                xs_html_attr("autocomplete",    "off"),
2843                xs_html_attr("method",          "post"),
2844                xs_html_attr("action",          btn_form_action),
2845                xs_html_sctag("input",
2846                    xs_html_attr("type",    "hidden"),
2847                    xs_html_attr("name",    "actor"),
2848                    xs_html_attr("value",   actor_id)),
2849                xs_html_sctag("input",
2850                    xs_html_attr("type",    "hidden"),
2851                    xs_html_attr("name",    "actor-form"),
2852                    xs_html_attr("value",   "yes")));
2853
2854            xs_html_add(snac_controls, form);
2855
2856            if (following_check(snac, actor_id)) {
2857                xs_html_add(form,
2858                    html_button("unfollow", L("Unfollow"),
2859                                L("Stop following this user's activity")));
2860
2861                if (is_limited(snac, actor_id))
2862                    xs_html_add(form,
2863                        html_button("unlimit", L("Unlimit"),
2864                                L("Allow announces (boosts) from this user")));
2865                else
2866                    xs_html_add(form,
2867                        html_button("limit", L("Limit"),
2868                                L("Block announces (boosts) from this user")));
2869            }
2870            else {
2871                xs_html_add(form,
2872                    html_button("follow", L("Follow"),
2873                                L("Start following this user's activity")));
2874
2875                if (follower_check(snac, actor_id))
2876                    xs_html_add(form,
2877                        html_button("delete", L("Delete"), L("Delete this user")));
2878            }
2879
2880            if (pending_check(snac, actor_id)) {
2881                xs_html_add(form,
2882                    html_button("approve", L("Approve"),
2883                                L("Approve this follow request")));
2884
2885                xs_html_add(form,
2886                    html_button("discard", L("Discard"), L("Discard this follow request")));
2887            }
2888
2889            if (is_muted(snac, actor_id))
2890                xs_html_add(form,
2891                    html_button("unmute", L("Unmute"),
2892                                L("Stop blocking activities from this user")));
2893            else
2894                xs_html_add(form,
2895                    html_button("mute", L("MUTE"),
2896                                L("Block any activity from this user")));
2897
2898            /* the post textarea */
2899            xs *dm_div_id  = xs_fmt("%s_%s_dm", md5, t);
2900            xs *dm_form_id = xs_fmt("%s_reply_form", md5);
2901
2902            xs_html_add(snac_controls,
2903                xs_html_tag("p", NULL),
2904                html_note(snac, L("Direct Message..."),
2905                    dm_div_id, dm_form_id,
2906                    "", "",
2907                    NULL, actor_id,
2908                    xs_stock(XSTYPE_FALSE), "",
2909                    xs_stock(XSTYPE_FALSE), NULL,
2910                    NULL, 0, "", "", 0),
2911                xs_html_tag("p", NULL));
2912
2913            xs_html_add(snac_post, snac_controls);
2914
2915            xs_html_add(snac_posts, snac_post);
2916        }
2917    }
2918
2919    return people;
2920}
2921
2922
2923xs_str *html_people(snac *user)
2924{
2925    const char *proxy = NULL;
2926
2927    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
2928        proxy = user->actor;
2929
2930    xs *wing = following_list(user);
2931    xs *wers = follower_list(user);
2932
2933    xs_html *lists = xs_html_tag("div",
2934        xs_html_attr("class", "snac-posts"));
2935
2936    if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
2937        xs *pending = pending_list(user);
2938        xs_html_add(lists,
2939            html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
2940    }
2941
2942    xs_html_add(lists,
2943        html_people_list(user, wing, L("People you follow"), "i", proxy),
2944        html_people_list(user, wers, L("People that follow you"), "e", proxy));
2945
2946    xs_html *html = xs_html_tag("html",
2947        html_user_head(user, NULL, NULL),
2948        xs_html_add(html_user_body(user, 0),
2949            lists,
2950            html_footer()));
2951
2952    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2953}
2954
2955
2956xs_str *html_notifications(snac *user, int skip, int show)
2957{
2958    const char *proxy = NULL;
2959
2960    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
2961        proxy = user->actor;
2962
2963    xs *n_list = notify_list(user, skip, show);
2964    xs *n_time = notify_check_time(user, 0);
2965
2966    xs_html *body = html_user_body(user, 0);
2967
2968    xs_html *html = xs_html_tag("html",
2969        html_user_head(user, NULL, NULL),
2970        body);
2971
2972    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
2973
2974    xs_html_add(body,
2975        xs_html_tag("form",
2976            xs_html_attr("autocomplete", "off"),
2977            xs_html_attr("method",       "post"),
2978            xs_html_attr("action",       clear_all_action),
2979            xs_html_attr("id",           "clear"),
2980            xs_html_sctag("input",
2981                xs_html_attr("type",     "submit"),
2982                xs_html_attr("class",    "snac-btn-like"),
2983                xs_html_attr("value",    L("Clear all")))));
2984
2985    xs_html *noti_new = NULL;
2986    xs_html *noti_seen = NULL;
2987
2988    xs_html *posts = xs_html_tag("div",
2989        xs_html_attr("class", "snac-posts"));
2990    xs_html_add(body, posts);
2991
2992    xs_set rep;
2993    xs_set_init(&rep);
2994
2995    /* dict to store previous notification labels */
2996    xs *admiration_labels = xs_dict_new();
2997
2998    const xs_str *v;
2999
3000    xs_list_foreach(n_list, v) {
3001        xs *noti = notify_get(user, v);
3002
3003        if (noti == NULL)
3004            continue;
3005
3006        xs *obj = NULL;
3007        const char *type  = xs_dict_get(noti, "type");
3008        const char *utype = xs_dict_get(noti, "utype");
3009        const char *id    = xs_dict_get(noti, "objid");
3010        const char *date  = xs_dict_get(noti, "date");
3011        const char *id2   = xs_dict_get_path(noti, "msg.id");
3012        xs *wrk = NULL;
3013
3014        if (xs_is_null(id))
3015            continue;
3016
3017        if (is_hidden(user, id))
3018            continue;
3019
3020        if (xs_is_string(id2) && xs_set_add(&rep, id2) != 1)
3021            continue;
3022
3023        object_get(id, &obj);
3024
3025        const char *msg_id = NULL;
3026
3027        if (xs_is_dict(obj))
3028            msg_id = xs_dict_get(obj, "id");
3029
3030        const char *actor_id = xs_dict_get(noti, "actor");
3031        xs *actor = NULL;
3032
3033        if (!valid_status(actor_get(actor_id, &actor)))
3034            continue;
3035
3036        xs *a_name = actor_name(actor, proxy);
3037        const char *label = type;
3038
3039        if (strcmp(type, "Create") == 0)
3040            label = L("Mention");
3041        else
3042        if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
3043            label = L("Finished poll");
3044        else
3045        if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
3046            label = L("Unfollow");
3047        else
3048        if (strcmp(type, "EmojiReact") == 0) {
3049            const char *content = xs_dict_get_path(noti, "msg.content");
3050
3051            if (xs_type(content) == XSTYPE_STRING) {
3052                wrk = xs_fmt("%s (%s)", type, content);
3053                label = wrk;
3054            }
3055        }
3056        else
3057        if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
3058            label = L("Follow Request");
3059
3060        xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
3061
3062        xs_html *this_html_label = xs_html_container(
3063                xs_html_tag("b",
3064                    xs_html_text(label),
3065                    xs_html_text(" by "),
3066                    xs_html_tag("a",
3067                        xs_html_attr("href", actor_id),
3068                        xs_html_raw(a_name))), /* a_name is already sanitized */
3069                xs_html_text(" "),
3070                xs_html_tag("time",
3071                    xs_html_attr("class", "dt-published snac-pubdate"),
3072                    xs_html_attr("title", date),
3073                    xs_html_text(s_date)));
3074
3075        xs_html *html_label = NULL;
3076
3077        if (xs_is_string(msg_id)) {
3078            const xs_val *prev_label = xs_dict_get(admiration_labels, msg_id);
3079
3080            if (xs_type(prev_label) == XSTYPE_DATA) {
3081                /* there is a previous list of admiration labels! */
3082                xs_data_get(&html_label, prev_label);
3083
3084                xs_html_add(html_label,
3085                    xs_html_sctag("br", NULL),
3086                    this_html_label);
3087
3088                continue;
3089            }
3090        }
3091
3092        xs_html *entry = NULL;
3093
3094        html_label = xs_html_tag("p",
3095            this_html_label);
3096
3097        /* store in the admiration labels dict */
3098        xs *pl = xs_data_new(&html_label, sizeof(html_label));
3099
3100        if (xs_is_string(msg_id))
3101            admiration_labels = xs_dict_set(admiration_labels, msg_id, pl);
3102
3103        entry = xs_html_tag("div",
3104            xs_html_attr("class", "snac-post-with-desc"),
3105            html_label);
3106
3107        if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0 || strcmp(type, "Block") == 0) {
3108            xs_html_add(entry,
3109                xs_html_tag("div",
3110                    xs_html_attr("class", "snac-post"),
3111                    html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0, proxy, NULL)));
3112        }
3113        else
3114        if (strcmp(type, "Move") == 0) {
3115            const xs_dict *o_msg = xs_dict_get(noti, "msg");
3116            const char *target;
3117
3118            if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
3119                xs *old_actor = NULL;
3120
3121                if (valid_status(actor_get(target, &old_actor))) {
3122                    xs_html_add(entry,
3123                        xs_html_tag("div",
3124                            xs_html_attr("class", "snac-post"),
3125                            html_actor_icon(user, old_actor, NULL, NULL, NULL, 0, 0, proxy, NULL)));
3126                }
3127            }
3128        }
3129        else
3130        if (obj != NULL) {
3131            xs *md5 = xs_md5_hex(id, strlen(id));
3132
3133            xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
3134
3135            if (h != NULL) {
3136                xs_html_add(entry,
3137                    h);
3138            }
3139        }
3140
3141        if (strcmp(v, n_time) > 0) {
3142            /* unseen notification */
3143            if (noti_new == NULL) {
3144                noti_new = xs_html_tag("div",
3145                    xs_html_tag("h2",
3146                        xs_html_attr("class", "snac-header"),
3147                        xs_html_text(L("New"))));
3148
3149                xs_html_add(posts,
3150                    noti_new);
3151            }
3152
3153            xs_html_add(noti_new,
3154                entry);
3155        }
3156        else {
3157            /* already seen notification */
3158            if (noti_seen == NULL) {
3159                noti_seen = xs_html_tag("div",
3160                    xs_html_tag("h2",
3161                        xs_html_attr("class", "snac-header"),
3162                        xs_html_text(L("Already seen"))));
3163
3164                xs_html_add(posts,
3165                    noti_seen);
3166            }
3167
3168            xs_html_add(noti_seen,
3169                entry);
3170        }
3171    }
3172
3173    if (noti_new == NULL && noti_seen == NULL)
3174        xs_html_add(body,
3175            xs_html_tag("h2",
3176                xs_html_attr("class", "snac-header"),
3177                xs_html_text(L("None"))));
3178
3179    /* add the navigation footer */
3180    xs *next_p = notify_list(user, skip + show, 1);
3181    if (xs_list_len(next_p)) {
3182        xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
3183            user->actor, skip + show, show);
3184
3185        xs_html_add(body,
3186            xs_html_tag("p",
3187                xs_html_tag("a",
3188                    xs_html_attr("href", url),
3189                    xs_html_text(L("More...")))));
3190    }
3191
3192    xs_set_free(&rep);
3193
3194    xs_html_add(body,
3195        html_footer());
3196
3197    /* set the check time to now */
3198    xs *dummy = notify_check_time(user, 1);
3199    dummy = xs_free(dummy);
3200
3201    timeline_touch(user);
3202
3203    return xs_html_render_s(html, "<!DOCTYPE html>\n");
3204}
3205
3206
3207int html_get_handler(const xs_dict *req, const char *q_path,
3208                     char **body, int *b_size, char **ctype,
3209                     xs_str **etag, xs_str **last_modified)
3210{
3211    const char *accept = xs_dict_get(req, "accept");
3212    int status = HTTP_STATUS_NOT_FOUND;
3213    snac snac;
3214    xs *uid = NULL;
3215    const char *p_path;
3216    int cache = 1;
3217    int save = 1;
3218    int proxy = 0;
3219    const char *v;
3220
3221    const xs_dict *q_vars = xs_dict_get(req, "q_vars");
3222
3223    xs *l = xs_split_n(q_path, "/", 2);
3224    v = xs_list_get(l, 1);
3225
3226    if (xs_is_null(v)) {
3227        srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
3228        return HTTP_STATUS_NOT_FOUND;
3229    }
3230
3231    if (strcmp(v, "share-bridge") == 0) {
3232        /* temporary redirect for a post */
3233        const char *login = xs_dict_get(q_vars, "login");
3234        const char *content = xs_dict_get(q_vars, "content");
3235
3236        if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
3237            xs *b64 = xs_base64_enc(content, strlen(content));
3238
3239            srv_log(xs_fmt("share-bridge for user '%s'", login));
3240
3241            *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
3242            return HTTP_STATUS_SEE_OTHER;
3243        }
3244        else
3245            return HTTP_STATUS_NOT_FOUND;
3246    }
3247    else
3248    if (strcmp(v, "auth-int-bridge") == 0) {
3249        const char *login  = xs_dict_get(q_vars, "login");
3250        const char *id     = xs_dict_get(q_vars, "id");
3251        const char *action = xs_dict_get(q_vars, "action");
3252
3253        if (xs_is_string(login) && xs_is_string(id) && xs_is_string(action)) {
3254            *body = xs_fmt("%s/%s/authorize_interaction?action=%s&id=%s",
3255                srv_baseurl, login, action, id);
3256
3257            return HTTP_STATUS_SEE_OTHER;
3258        }
3259        else
3260            return HTTP_STATUS_NOT_FOUND;
3261    }
3262
3263    uid = xs_dup(v);
3264
3265    /* rss extension? */
3266    if (xs_endswith(uid, ".rss")) {
3267        uid = xs_crop_i(uid, 0, -4);
3268        p_path = ".rss";
3269    }
3270    else
3271        p_path = xs_list_get(l, 2);
3272
3273    if (!uid || !user_open(&snac, uid)) {
3274        /* invalid user */
3275        srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
3276        return HTTP_STATUS_NOT_FOUND;
3277    }
3278
3279    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
3280        proxy = 1;
3281
3282    /* return the RSS if requested by Accept header */
3283    if (accept != NULL) {
3284        if (xs_str_in(accept, "text/xml") != -1 ||
3285            xs_str_in(accept, "application/rss+xml") != -1)
3286            p_path = ".rss";
3287    }
3288
3289    /* check if server config variable 'disable_cache' is set */
3290    if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
3291        cache = 0;
3292
3293    int skip = 0;
3294    int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
3295    int show = def_show;
3296
3297    if ((v = xs_dict_get(q_vars, "skip")) != NULL)
3298        skip = atoi(v), cache = 0, save = 0;
3299    if ((v = xs_dict_get(q_vars, "show")) != NULL)
3300        show = atoi(v), cache = 0, save = 0;
3301    if ((v = xs_dict_get(q_vars, "da")) != NULL) {
3302        /* user dismissed an announcement */
3303        if (login(&snac, req)) {
3304            double ts = atof(v);
3305            xs *timestamp = xs_number_new(ts);
3306            srv_log(xs_fmt("user dismissed announcements until %d", ts));
3307            snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
3308            user_persist(&snac, 0);
3309        }
3310    }
3311
3312    /* get a possible error message */
3313    const char *error = xs_dict_get(q_vars, "error");
3314    if (error != NULL)
3315        cache = 0;
3316
3317    /* a show of 0 has no sense */
3318    if (show == 0)
3319        show = def_show;
3320
3321    if (p_path == NULL) { /** public timeline **/
3322        xs *h = xs_str_localtime(0, "%Y-%m.html");
3323
3324        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
3325            /** empty public timeline for private users **/
3326            *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error);
3327            *b_size = strlen(*body);
3328            status  = HTTP_STATUS_OK;
3329        }
3330        else
3331        if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
3332            snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
3333
3334            status = history_get(&snac, h, body, b_size,
3335                        xs_dict_get(req, "if-none-match"), etag);
3336        }
3337        else {
3338            xs *list = NULL;
3339            int more = 0;
3340
3341            if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")))
3342                list = timeline_simple_list(&snac, "public", skip, show, &more);
3343            else 
3344                list = timeline_list(&snac, "public", skip, show, &more);
3345
3346            xs *pins = pinned_list(&snac);
3347            pins = xs_list_cat(pins, list);
3348
3349            *body = html_timeline(&snac, pins, 1, skip, show, more, NULL, "", 1, error);
3350
3351            *b_size = strlen(*body);
3352            status  = HTTP_STATUS_OK;
3353
3354            if (save)
3355                history_add(&snac, h, *body, *b_size, etag);
3356        }
3357    }
3358    else
3359    if (strcmp(p_path, "admin") == 0) { /** private timeline **/
3360        if (!login(&snac, req)) {
3361            *body  = xs_dup(uid);
3362            status = HTTP_STATUS_UNAUTHORIZED;
3363        }
3364        else {
3365            const char *q = xs_dict_get(q_vars, "q");
3366            xs *url_acct = NULL;
3367
3368            /* searching for an URL? */
3369            if (q && xs_match(q, "https://*|http://*")) {
3370                /* may by an actor; try a webfinger */
3371                xs *actor_obj = NULL;
3372
3373                if (valid_status(webfinger_request(q, &actor_obj, &url_acct))) {
3374                    /* it's an actor; do the dirty trick of changing q to the account name */
3375                    q = url_acct;
3376                }
3377                else {
3378                    /* bring it to the user's timeline */
3379                    xs *object = NULL;
3380                    int status;
3381
3382                    status = activitypub_request(&snac, q, &object);
3383                    snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
3384
3385                    if (valid_status(status)) {
3386                        /* got it; also request the actor */
3387                        const char *attr_to = get_atto(object);
3388
3389                        if (!xs_is_null(attr_to)) {
3390                            status = actor_request(&snac, attr_to, &actor_obj);
3391
3392                            if (valid_status(status)) {
3393                                /* reset the query string to be the real id */
3394                                url_acct = xs_dup(xs_dict_get(object, "id"));
3395                                q = url_acct;
3396
3397                                /* add the post to the timeline */
3398                                xs *md5 = xs_md5_hex(q, strlen(q));
3399
3400                                if (!timeline_here(&snac, md5))
3401                                    timeline_add(&snac, q, object);
3402                            }
3403                        }
3404                    }
3405                }
3406
3407                /* fall through */
3408            }
3409
3410            if (q && *q) {
3411                if (xs_regex_match(q, "^@?[a-zA-Z0-9._]+@[a-zA-Z0-9-]+\\.")) {
3412                    /** search account **/
3413                    xs *actor = NULL;
3414                    xs *acct = NULL;
3415                    xs *l = xs_list_new();
3416                    xs_html *page = NULL;
3417
3418                    if (valid_status(webfinger_request(q, &actor, &acct))) {
3419                        xs *actor_obj = NULL;
3420
3421                        if (valid_status(actor_request(&snac, actor, &actor_obj))) {
3422                            actor_add(actor, actor_obj);
3423
3424                            /* create a people list with only one element */
3425                            l = xs_list_append(xs_list_new(), actor);
3426
3427                            xs *title = xs_fmt(L("Search results for account %s"), q);
3428
3429                            page = html_people_list(&snac, l, title, "wf", NULL);
3430                        }
3431                    }
3432
3433                    if (page == NULL) {
3434                        xs *title = xs_fmt(L("Account %s not found"), q);
3435
3436                        page = xs_html_tag("div",
3437                            xs_html_tag("h2",
3438                                xs_html_attr("class", "snac-header"),
3439                                xs_html_text(title)));
3440                    }
3441
3442                    xs_html *html = xs_html_tag("html",
3443                        html_user_head(&snac, NULL, NULL),
3444                        xs_html_add(html_user_body(&snac, 0),
3445                        page,
3446                        html_footer()));
3447
3448                    *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
3449                    *b_size = strlen(*body);
3450                    status = HTTP_STATUS_OK;
3451                }
3452                else
3453                if (*q == '#') {
3454                    /** search by tag **/
3455                    xs *tl = tag_search(q, skip, show + 1);
3456                    int more = 0;
3457                    if (xs_list_len(tl) >= show + 1) {
3458                        /* drop the last one */
3459                        tl = xs_list_del(tl, -1);
3460                        more = 1;
3461                    }
3462
3463                    xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
3464                    xs *title = xs_fmt(xs_list_len(tl) ?
3465                        L("Search results for tag %s") : L("Nothing found for tag %s"), q);
3466
3467                    *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error);
3468                    *b_size = strlen(*body);
3469                    status  = HTTP_STATUS_OK;
3470                }
3471                else {
3472                    /** search by content **/
3473                    int to = 0;
3474                    int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
3475                    xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
3476                    xs *title = NULL;
3477                    xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
3478                    int tl_len = xs_list_len(tl);
3479
3480                    if (to)
3481                        title = xs_fmt(L("Search results for '%s' (may be more)"), q);
3482                    else
3483                    if (tl_len)
3484                        title = xs_fmt(L("Search results for '%s'"), q);
3485                    else
3486                    if (skip)
3487                        title = xs_fmt(L("No more matches for '%s'"), q);
3488                    else
3489                        title = xs_fmt(L("Nothing found for '%s'"), q);
3490
3491                    *body   = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
3492                                            title, page, 0, error);
3493                    *b_size = strlen(*body);
3494                    status  = HTTP_STATUS_OK;
3495                }
3496            }
3497            else {
3498                /** the private timeline **/
3499                double t = history_mtime(&snac, "timeline.html_");
3500
3501                /* if enabled by admin, return a cached page if its timestamp is:
3502                   a) newer than the timeline timestamp
3503                   b) newer than the start time of the server
3504                */
3505                if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
3506                    snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
3507
3508                    status = history_get(&snac, "timeline.html_", body, b_size,
3509                                xs_dict_get(req, "if-none-match"), etag);
3510                }
3511                else {
3512                    int more = 0;
3513
3514                    snac_debug(&snac, 1, xs_fmt("building timeline"));
3515
3516                    xs *list = timeline_list(&snac, "private", skip, show, &more);
3517
3518                    *body = html_timeline(&snac, list, 0, skip, show,
3519                            more, NULL, "/admin", 1, error);
3520
3521                    *b_size = strlen(*body);
3522                    status  = HTTP_STATUS_OK;
3523
3524                    if (save)
3525                        history_add(&snac, "timeline.html_", *body, *b_size, etag);
3526
3527                    timeline_add_mark(&snac);
3528                }
3529            }
3530        }
3531    }
3532    else
3533    if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
3534        if (!login(&snac, req)) {
3535            *body  = xs_dup(uid);
3536            status = HTTP_STATUS_UNAUTHORIZED;
3537        }
3538        else {
3539            xs *l = xs_split(p_path, "/");
3540            const char *md5 = xs_list_get(l, -1);
3541
3542            if (md5 && *md5 && timeline_here(&snac, md5)) {
3543                xs *list = xs_list_append(xs_list_new(), md5);
3544
3545                *body   = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error);
3546                *b_size = strlen(*body);
3547                status  = HTTP_STATUS_OK;
3548            }
3549        }
3550    }
3551    else
3552    if (strcmp(p_path, "people") == 0) { /** the list of people **/
3553        if (!login(&snac, req)) {
3554            *body  = xs_dup(uid);
3555            status = HTTP_STATUS_UNAUTHORIZED;
3556        }
3557        else {
3558            *body   = html_people(&snac);
3559            *b_size = strlen(*body);
3560            status  = HTTP_STATUS_OK;
3561        }
3562    }
3563    else
3564    if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
3565        if (!login(&snac, req)) {
3566            *body  = xs_dup(uid);
3567            status = HTTP_STATUS_UNAUTHORIZED;
3568        }
3569        else {
3570            *body   = html_notifications(&snac, skip, show);
3571            *b_size = strlen(*body);
3572            status  = HTTP_STATUS_OK;
3573        }
3574    }
3575    else
3576    if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
3577        if (!login(&snac, req)) {
3578            *body  = xs_dup(uid);
3579            status = HTTP_STATUS_UNAUTHORIZED;
3580        }
3581        else {
3582            xs *list = timeline_instance_list(skip, show);
3583            xs *next = timeline_instance_list(skip + show, 1);
3584
3585            *body = html_timeline(&snac, list, 0, skip, show,
3586                xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error);
3587            *b_size = strlen(*body);
3588            status  = HTTP_STATUS_OK;
3589        }
3590    }
3591    else
3592    if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
3593        if (!login(&snac, req)) {
3594            *body  = xs_dup(uid);
3595            status = HTTP_STATUS_UNAUTHORIZED;
3596        }
3597        else {
3598            xs *list = pinned_list(&snac);
3599
3600            *body = html_timeline(&snac, list, 0, skip, show,
3601                0, L("Pinned posts"), "", 0, error);
3602            *b_size = strlen(*body);
3603            status  = HTTP_STATUS_OK;
3604        }
3605    }
3606    else
3607    if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
3608        if (!login(&snac, req)) {
3609            *body  = xs_dup(uid);
3610            status = HTTP_STATUS_UNAUTHORIZED;
3611        }
3612        else {
3613            xs *list = bookmark_list(&snac);
3614
3615            *body = html_timeline(&snac, list, 0, skip, show,
3616                0, L("Bookmarked posts"), "", 0, error);
3617            *b_size = strlen(*body);
3618            status  = HTTP_STATUS_OK;
3619        }
3620    }
3621    else
3622    if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
3623        if (!login(&snac, req)) {
3624            *body  = xs_dup(uid);
3625            status = HTTP_STATUS_UNAUTHORIZED;
3626        }
3627        else {
3628            xs *list = draft_list(&snac);
3629
3630            *body = html_timeline(&snac, list, 0, skip, show,
3631                0, L("Post drafts"), "", 0, error);
3632            *b_size = strlen(*body);
3633            status  = HTTP_STATUS_OK;
3634        }
3635    }
3636    else
3637    if (xs_startswith(p_path, "list/")) { /** list timelines **/
3638        if (!login(&snac, req)) {
3639            *body  = xs_dup(uid);
3640            status = HTTP_STATUS_UNAUTHORIZED;
3641        }
3642        else {
3643            xs *l = xs_split(p_path, "/");
3644            const char *lid = xs_list_get(l, -1);
3645
3646            xs *list = list_timeline(&snac, lid, skip, show);
3647            xs *next = list_timeline(&snac, lid, skip + show, 1);
3648
3649            if (list != NULL) {
3650                xs *ttl = timeline_top_level(&snac, list);
3651
3652                xs *base = xs_fmt("/list/%s", lid);
3653                xs *name = list_maint(&snac, lid, 3);
3654                xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
3655
3656                *body = html_timeline(&snac, ttl, 0, skip, show,
3657                    xs_list_len(next), title, base, 1, error);
3658                *b_size = strlen(*body);
3659                status  = HTTP_STATUS_OK;
3660            }
3661        }
3662    }
3663    else
3664    if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
3665        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3666            return HTTP_STATUS_FORBIDDEN;
3667
3668        xs *id  = xs_fmt("%s/%s", snac.actor, p_path);
3669        xs *msg = NULL;
3670
3671        if (valid_status(object_get(id, &msg))) {
3672            xs *md5  = xs_md5_hex(id, strlen(id));
3673            xs *list = xs_list_new();
3674
3675            list = xs_list_append(list, md5);
3676
3677            *body   = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error);
3678            *b_size = strlen(*body);
3679            status  = HTTP_STATUS_OK;
3680        }
3681    }
3682    else
3683    if (xs_startswith(p_path, "s/")) { /** a static file **/
3684        xs *l    = xs_split(p_path, "/");
3685        const char *id = xs_list_get(l, 1);
3686        int sz;
3687
3688        if (id && *id) {
3689            status = static_get(&snac, id, body, &sz,
3690                        xs_dict_get(req, "if-none-match"), etag);
3691
3692            if (valid_status(status)) {
3693                *b_size = sz;
3694                *ctype  = (char *)xs_mime_by_ext(id);
3695            }
3696        }
3697    }
3698    else
3699    if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
3700        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3701            return HTTP_STATUS_FORBIDDEN;
3702
3703        if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
3704            return HTTP_STATUS_FORBIDDEN;
3705
3706        xs *l = xs_split(p_path, "/");
3707        const char *id = xs_list_get(l, 1);
3708
3709        if (id && *id) {
3710            if (xs_endswith(id, "timeline.html_")) {
3711                /* Don't let them in */
3712                *b_size = 0;
3713                status = HTTP_STATUS_NOT_FOUND;
3714            }
3715            else
3716                status = history_get(&snac, id, body, b_size,
3717                            xs_dict_get(req, "if-none-match"), etag);
3718        }
3719    }
3720    else
3721    if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
3722        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3723            return HTTP_STATUS_FORBIDDEN;
3724
3725        int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
3726
3727        xs *elems = timeline_simple_list(&snac, "public", 0, cnt, NULL);
3728        xs *bio   = xs_dup(xs_dict_get(snac.config, "bio"));
3729
3730        xs *rss_title = xs_fmt("%s (@%s@%s)",
3731            xs_dict_get(snac.config, "name"),
3732            snac.uid,
3733            xs_dict_get(srv_config, "host"));
3734        xs *rss_link = xs_fmt("%s.rss", snac.actor);
3735
3736        *body   = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
3737        *b_size = strlen(*body);
3738        *ctype  = "application/rss+xml; charset=utf-8";
3739        status  = HTTP_STATUS_OK;
3740
3741        snac_debug(&snac, 1, xs_fmt("serving RSS"));
3742    }
3743    else
3744    if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
3745        xs *proxy_prefix = NULL;
3746
3747        if (xs_startswith(p_path, "x/")) {
3748            /* proxy usage authorized by http basic auth */
3749            if (login(&snac, req))
3750                proxy_prefix = xs_str_new("x/");
3751            else {
3752                *body  = xs_dup(uid);
3753                status = HTTP_STATUS_UNAUTHORIZED;
3754            }
3755        }
3756        else {
3757            /* proxy usage authorized by proxy_token */
3758            xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
3759            xs *tk = xs_md5_hex(tks, strlen(tks));
3760            xs *p = xs_fmt("y/%s/", tk);
3761
3762            if (xs_startswith(p_path, p))
3763                proxy_prefix = xs_dup(p);
3764        }
3765
3766        if (proxy_prefix) {
3767            /* pick the raw path (including optional ? arguments) */
3768            const char *raw_path = xs_dict_get(req, "raw_path");
3769
3770            /* skip to where the proxy/ string starts */
3771            raw_path += xs_str_in(raw_path, proxy_prefix);
3772
3773            xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
3774            xs *hdrs = xs_dict_new();
3775
3776            hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
3777
3778            const char *ims = xs_dict_get(req, "if-modified-since");
3779            const char *inm = xs_dict_get(req, "if-none-match");
3780
3781            if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
3782            if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
3783
3784            xs *rsp = xs_http_request("GET", url, hdrs,
3785                        NULL, 0, &status, body, b_size, 0);
3786
3787            if (valid_status(status)) {
3788                const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
3789                const char *lm = xs_dict_get(rsp, "last-modified");
3790                const char *et = xs_dict_get(rsp, "etag");
3791
3792                if (lm) *last_modified = xs_dup(lm);
3793                if (et) *etag = xs_dup(et);
3794
3795                /* find the content-type in the static mime types,
3796                   and return that value instead of ct, which will
3797                   be destroyed when out of scope */
3798                for (int n = 0; xs_mime_types[n]; n += 2) {
3799                    if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
3800                        *ctype = (char *)xs_mime_types[n + 1];
3801                        break;
3802                    }
3803                }
3804            }
3805
3806            snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
3807        }
3808    }
3809    else
3810    if (strcmp(p_path, "share") == 0) { /** direct post **/
3811        if (!login(&snac, req)) {
3812            *body  = xs_dup(uid);
3813            status = HTTP_STATUS_UNAUTHORIZED;
3814        }
3815        else {
3816            const char *b64 = xs_dict_get(q_vars, "content");
3817            int sz;
3818            xs *content = xs_base64_dec(b64, &sz);
3819            xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL);
3820            xs *c_msg = msg_create(&snac, msg);
3821
3822            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3823
3824            enqueue_message(&snac, c_msg);
3825
3826            snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
3827
3828            *body   = xs_fmt("%s/admin", snac.actor);
3829            *b_size = strlen(*body);
3830            status  = HTTP_STATUS_SEE_OTHER;
3831        }
3832    }
3833    else
3834    if (strcmp(p_path, "authorize_interaction") == 0) { /** follow, like or boost from Mastodon **/
3835        if (!login(&snac, req)) {
3836            *body  = xs_dup(uid);
3837            status = HTTP_STATUS_UNAUTHORIZED;
3838        }
3839        else {
3840            status = HTTP_STATUS_NOT_FOUND;
3841
3842            const char *id     = xs_dict_get(q_vars, "id");
3843            const char *action = xs_dict_get(q_vars, "action");
3844
3845            if (xs_is_string(id) && xs_is_string(action)) {
3846                if (strcmp(action, "Follow") == 0) {
3847                    xs *msg = msg_follow(&snac, id);
3848
3849                    if (msg != NULL) {
3850                        const char *actor = xs_dict_get(msg, "object");
3851
3852                        following_add(&snac, actor, msg);
3853
3854                        enqueue_output_by_actor(&snac, msg, actor, 0);
3855
3856                        status = HTTP_STATUS_SEE_OTHER;
3857                    }
3858                }
3859                else
3860                if (xs_match(action, "Like|Boost|Announce")) {
3861                    /* bring the post */
3862                    xs *msg = msg_admiration(&snac, id, *action == 'L' ? "Like" : "Announce");
3863
3864                    if (msg != NULL) {
3865                        enqueue_message(&snac, msg);
3866                        timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, *action == 'L' ? 1 : 0);
3867
3868                        status = HTTP_STATUS_SEE_OTHER;
3869                    }
3870                }
3871            }
3872
3873            if (status == HTTP_STATUS_SEE_OTHER) {
3874                *body   = xs_fmt("%s/admin", snac.actor);
3875                *b_size = strlen(*body);
3876            }
3877        }
3878    }
3879    else
3880        status = HTTP_STATUS_NOT_FOUND;
3881
3882    user_free(&snac);
3883
3884    if (valid_status(status) && *ctype == NULL) {
3885        *ctype = "text/html; charset=utf-8";
3886    }
3887
3888    return status;
3889}
3890
3891
3892int html_post_handler(const xs_dict *req, const char *q_path,
3893                      char *payload, int p_size,
3894                      char **body, int *b_size, char **ctype)
3895{
3896    (void)p_size;
3897    (void)ctype;
3898
3899    int status = 0;
3900    snac snac;
3901    const char *uid;
3902    const char *p_path;
3903    const xs_dict *p_vars;
3904
3905    xs *l = xs_split_n(q_path, "/", 2);
3906
3907    uid = xs_list_get(l, 1);
3908    if (!uid || !user_open(&snac, uid)) {
3909        /* invalid user */
3910        srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
3911        return HTTP_STATUS_NOT_FOUND;
3912    }
3913
3914    p_path = xs_list_get(l, 2);
3915
3916    /* all posts must be authenticated */
3917    if (!login(&snac, req)) {
3918        user_free(&snac);
3919        *body  = xs_dup(uid);
3920        return HTTP_STATUS_UNAUTHORIZED;
3921    }
3922
3923    p_vars = xs_dict_get(req, "p_vars");
3924
3925    if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
3926        snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
3927
3928        /* post note */
3929        const xs_str *content      = xs_dict_get(p_vars, "content");
3930        const xs_str *in_reply_to  = xs_dict_get(p_vars, "in_reply_to");
3931        const xs_str *attach_url   = xs_dict_get(p_vars, "attach_url");
3932        const xs_list *attach_file = xs_dict_get(p_vars, "attach");
3933        const xs_str *to           = xs_dict_get(p_vars, "to");
3934        const xs_str *sensitive    = xs_dict_get(p_vars, "sensitive");
3935        const xs_str *summary      = xs_dict_get(p_vars, "summary");
3936        const xs_str *edit_id      = xs_dict_get(p_vars, "edit_id");
3937        const xs_str *alt_text     = xs_dict_get(p_vars, "alt_text");
3938        int priv             = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
3939        int store_as_draft   = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
3940        xs *attach_list      = xs_list_new();
3941
3942        /* default alt text */
3943        if (xs_is_null(alt_text))
3944            alt_text = "";
3945
3946        /* is attach_url set? */
3947        if (!xs_is_null(attach_url) && *attach_url != '\0') {
3948            xs *l = xs_list_new();
3949
3950            l = xs_list_append(l, attach_url);
3951            l = xs_list_append(l, alt_text);
3952
3953            attach_list = xs_list_append(attach_list, l);
3954        }
3955
3956        /* is attach_file set? */
3957        if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
3958            const char *fn = xs_list_get(attach_file, 0);
3959
3960            if (*fn != '\0') {
3961                char *ext = strrchr(fn, '.');
3962                xs *hash  = xs_md5_hex(fn, strlen(fn));
3963                xs *id    = xs_fmt("%s%s", hash, ext);
3964                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
3965                int fo    = xs_number_get(xs_list_get(attach_file, 1));
3966                int fs    = xs_number_get(xs_list_get(attach_file, 2));
3967
3968                /* store */
3969                static_put(&snac, id, payload + fo, fs);
3970
3971                xs *l = xs_list_new();
3972
3973                l = xs_list_append(l, url);
3974                l = xs_list_append(l, alt_text);
3975
3976                attach_list = xs_list_append(attach_list, l);
3977            }
3978        }
3979
3980        if (content != NULL) {
3981            xs *msg       = NULL;
3982            xs *c_msg     = NULL;
3983            xs *content_2 = xs_replace(content, "\r", "");
3984            xs *poll_opts = NULL;
3985
3986            /* is there a valid set of poll options? */
3987            const char *v = xs_dict_get(p_vars, "poll_options");
3988            if (!xs_is_null(v) && *v) {
3989                xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
3990
3991                poll_opts = xs_split(v2, "\n");
3992            }
3993
3994            if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
3995                /* get the rest of poll configuration */
3996                const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
3997                const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
3998                int multiple = 0;
3999
4000                int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
4001
4002                if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
4003                    multiple = 1;
4004
4005                msg = msg_question(&snac, content_2, attach_list,
4006                                   poll_opts, multiple, end_secs);
4007
4008                enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
4009            }
4010            else
4011                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv, NULL);
4012
4013            if (sensitive != NULL) {
4014                msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
4015                msg = xs_dict_set(msg, "summary",   xs_is_null(summary) ? "..." : summary);
4016            }
4017
4018            if (xs_is_null(edit_id)) {
4019                /* new message */
4020                const char *id = xs_dict_get(msg, "id");
4021
4022                if (store_as_draft) {
4023                    draft_add(&snac, id, msg);
4024                }
4025                else {
4026                    c_msg = msg_create(&snac, msg);
4027                    timeline_add(&snac, id, msg);
4028                }
4029            }
4030            else {
4031                /* an edition of a previous message */
4032                xs *p_msg = NULL;
4033
4034                if (valid_status(object_get(edit_id, &p_msg))) {
4035                    /* copy relevant fields from previous version */
4036                    char *fields[] = { "id", "context", "url", "published",
4037                                       "to", "inReplyTo", NULL };
4038                    int n;
4039
4040                    for (n = 0; fields[n]; n++) {
4041                        const char *v = xs_dict_get(p_msg, fields[n]);
4042                        msg = xs_dict_set(msg, fields[n], v);
4043                    }
4044
4045                    if (store_as_draft) {
4046                        draft_add(&snac, edit_id, msg);
4047                    }
4048                    else
4049                    if (is_draft(&snac, edit_id)) {
4050                        /* message was previously a draft; it's a create activity */
4051
4052                        /* set the published field to now */
4053                        xs *published = xs_str_utctime(0, ISO_DATE_SPEC);
4054                        msg = xs_dict_set(msg, "published", published);
4055
4056                        /* overwrite object */
4057                        object_add_ow(edit_id, msg);
4058
4059                        c_msg = msg_create(&snac, msg);
4060                        timeline_add(&snac, edit_id, msg);
4061                        draft_del(&snac, edit_id);
4062                    }
4063                    else {
4064                        /* set the updated field */
4065                        xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
4066                        msg = xs_dict_set(msg, "updated", updated);
4067
4068                        /* overwrite object, not updating the indexes */
4069                        object_add_ow(edit_id, msg);
4070
4071                        /* update message */
4072                        c_msg = msg_update(&snac, msg);
4073                    }
4074                }
4075                else
4076                    snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
4077            }
4078
4079            if (c_msg != NULL)
4080                enqueue_message(&snac, c_msg);
4081
4082            history_del(&snac, "timeline.html_");
4083        }
4084
4085        status = HTTP_STATUS_SEE_OTHER;
4086    }
4087    else
4088    if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
4089        /* action on an entry */
4090        const char *id     = xs_dict_get(p_vars, "id");
4091        const char *actor  = xs_dict_get(p_vars, "actor");
4092        const char *action = xs_dict_get(p_vars, "action");
4093        const char *group  = xs_dict_get(p_vars, "group");
4094
4095        if (action == NULL)
4096            return HTTP_STATUS_NOT_FOUND;
4097
4098        snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
4099
4100        status = HTTP_STATUS_SEE_OTHER;
4101
4102        if (strcmp(action, L("Like")) == 0) { /** **/
4103            xs *msg = msg_admiration(&snac, id, "Like");
4104
4105            if (msg != NULL) {
4106                enqueue_message(&snac, msg);
4107                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
4108            }
4109        }
4110        else
4111        if (strcmp(action, L("Boost")) == 0) { /** **/
4112            xs *msg = msg_admiration(&snac, id, "Announce");
4113
4114            if (msg != NULL) {
4115                enqueue_message(&snac, msg);
4116                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
4117            }
4118        }
4119        else
4120        if (strcmp(action, L("Unlike")) == 0) { /** **/
4121            xs *msg = msg_repulsion(&snac, id, "Like");
4122
4123            if (msg != NULL) {
4124                enqueue_message(&snac, msg);
4125            }
4126        }
4127        else
4128        if (strcmp(action, L("Unboost")) == 0) { /** **/
4129            xs *msg = msg_repulsion(&snac, id, "Announce");
4130
4131            if (msg != NULL) {
4132                enqueue_message(&snac, msg);
4133            }
4134        }
4135        else
4136        if (strcmp(action, L("MUTE")) == 0) { /** **/
4137            mute(&snac, actor);
4138        }
4139        else
4140        if (strcmp(action, L("Unmute")) == 0) { /** **/
4141            unmute(&snac, actor);
4142        }
4143        else
4144        if (strcmp(action, L("Hide")) == 0) { /** **/
4145            if (is_draft(&snac, id))
4146                draft_del(&snac, id);
4147            else
4148                hide(&snac, id);
4149        }
4150        else
4151        if (strcmp(action, L("Limit")) == 0) { /** **/
4152            limit(&snac, actor);
4153        }
4154        else
4155        if (strcmp(action, L("Unlimit")) == 0) { /** **/
4156            unlimit(&snac, actor);
4157        }
4158        else
4159        if (strcmp(action, L("Follow")) == 0) { /** **/
4160            xs *msg = msg_follow(&snac, actor);
4161
4162            if (msg != NULL) {
4163                /* reload the actor from the message, in may be different */
4164                actor = xs_dict_get(msg, "object");
4165
4166                following_add(&snac, actor, msg);
4167
4168                enqueue_output_by_actor(&snac, msg, actor, 0);
4169            }
4170        }
4171        else
4172        if (strcmp(action, L("Unfollow")) == 0) { /** **/
4173            /* get the following object */
4174            xs *object = NULL;
4175
4176            if (valid_status(following_get(&snac, actor, &object))) {
4177                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
4178
4179                following_del(&snac, actor);
4180
4181                enqueue_output_by_actor(&snac, msg, actor, 0);
4182
4183                snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
4184            }
4185            else
4186                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
4187        }
4188        else
4189        if (strcmp(action, L("Follow Group")) == 0) { /** **/
4190            xs *msg = msg_follow(&snac, group);
4191
4192            if (msg != NULL) {
4193                /* reload the group from the message, in may be different */
4194                group = xs_dict_get(msg, "object");
4195
4196                following_add(&snac, group, msg);
4197
4198                enqueue_output_by_actor(&snac, msg, group, 0);
4199            }
4200        }
4201        else
4202        if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
4203            /* get the following object */
4204            xs *object = NULL;
4205
4206            if (valid_status(following_get(&snac, group, &object))) {
4207                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
4208
4209                following_del(&snac, group);
4210
4211                enqueue_output_by_actor(&snac, msg, group, 0);
4212
4213                snac_log(&snac, xs_fmt("unfollowed group %s", group));
4214            }
4215            else
4216                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
4217        }
4218        else
4219        if (strcmp(action, L("Delete")) == 0) { /** **/
4220            const char *actor_form = xs_dict_get(p_vars, "actor-form");
4221            if (actor_form != NULL) {
4222                /* delete follower */
4223                if (valid_status(follower_del(&snac, actor)))
4224                    snac_log(&snac, xs_fmt("deleted follower %s", actor));
4225                else
4226                    snac_log(&snac, xs_fmt("error deleting follower %s", actor));
4227            }
4228            else {
4229                /* delete an entry */
4230                if (xs_startswith(id, snac.actor) && !is_draft(&snac, id)) {
4231                    /* it's a post by us: generate a delete */
4232                    xs *msg = msg_delete(&snac, id);
4233
4234                    enqueue_message(&snac, msg);
4235
4236                    snac_log(&snac, xs_fmt("posted tombstone for %s", id));
4237                }
4238
4239                timeline_del(&snac, id);
4240
4241                draft_del(&snac, id);
4242
4243                snac_log(&snac, xs_fmt("deleted entry %s", id));
4244            }
4245        }
4246        else
4247        if (strcmp(action, L("Pin")) == 0) { /** **/
4248            pin(&snac, id);
4249            timeline_touch(&snac);
4250        }
4251        else
4252        if (strcmp(action, L("Unpin")) == 0) { /** **/
4253            unpin(&snac, id);
4254            timeline_touch(&snac);
4255        }
4256        else
4257        if (strcmp(action, L("Bookmark")) == 0) { /** **/
4258            bookmark(&snac, id);
4259            timeline_touch(&snac);
4260        }
4261        else
4262        if (strcmp(action, L("Unbookmark")) == 0) { /** **/
4263            unbookmark(&snac, id);
4264            timeline_touch(&snac);
4265        }
4266        else
4267        if (strcmp(action, L("Approve")) == 0) { /** **/
4268            xs *fwreq = pending_get(&snac, actor);
4269
4270            if (fwreq != NULL) {
4271                xs *reply = msg_accept(&snac, fwreq, actor);
4272
4273                enqueue_message(&snac, reply);
4274
4275                if (xs_is_null(xs_dict_get(fwreq, "published"))) {
4276                    /* add a date if it doesn't include one (Mastodon) */
4277                    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
4278                    fwreq = xs_dict_set(fwreq, "published", date);
4279                }
4280
4281                timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
4282
4283                follower_add(&snac, actor);
4284
4285                pending_del(&snac, actor);
4286
4287                snac_log(&snac, xs_fmt("new follower %s", actor));
4288            }
4289        }
4290        else
4291        if (strcmp(action, L("Discard")) == 0) { /** **/
4292            pending_del(&snac, actor);
4293        }
4294        else
4295            status = HTTP_STATUS_NOT_FOUND;
4296
4297        /* delete the cached timeline */
4298        if (status == HTTP_STATUS_SEE_OTHER)
4299            history_del(&snac, "timeline.html_");
4300    }
4301    else
4302    if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
4303        /* change of user data */
4304        const char *v;
4305        const char *p1, *p2;
4306
4307        if ((v = xs_dict_get(p_vars, "name")) != NULL)
4308            snac.config = xs_dict_set(snac.config, "name", v);
4309        if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
4310            snac.config = xs_dict_set(snac.config, "avatar", v);
4311        if ((v = xs_dict_get(p_vars, "bio")) != NULL)
4312            snac.config = xs_dict_set(snac.config, "bio", v);
4313        if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
4314            strcmp(v, "on") == 0) {
4315            snac.config = xs_dict_set(snac.config, "cw", "open");
4316        } else { /* if the checkbox is not set, the parameter is missing */
4317            snac.config = xs_dict_set(snac.config, "cw", "");
4318        }
4319        if ((v = xs_dict_get(p_vars, "email")) != NULL)
4320            snac.config = xs_dict_set(snac.config, "email", v);
4321        if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
4322            snac.config = xs_dict_set(snac.config, "telegram_bot", v);
4323        if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
4324            snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
4325        if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
4326            snac.config = xs_dict_set(snac.config, "ntfy_server", v);
4327        if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
4328            snac.config = xs_dict_set(snac.config, "ntfy_token", v);
4329        if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
4330            xs *days    = xs_number_new(atof(v));
4331            snac.config = xs_dict_set(snac.config, "purge_days", days);
4332        }
4333        if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
4334            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
4335        else
4336            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
4337        if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
4338            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
4339        else
4340            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
4341        if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
4342            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
4343        else
4344            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
4345        if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
4346            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
4347        else
4348            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
4349        if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
4350            snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
4351        else
4352            snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
4353        if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
4354            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
4355        else
4356            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
4357        if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
4358            snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
4359        else
4360            snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
4361
4362        snac.config = xs_dict_set(snac.config, "latitude", xs_dict_get_def(p_vars, "latitude", ""));
4363        snac.config = xs_dict_set(snac.config, "longitude", xs_dict_get_def(p_vars, "longitude", ""));
4364
4365        if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
4366            snac.config = xs_dict_set(snac.config, "metadata", v);
4367
4368        /* uploads */
4369        const char *uploads[] = { "avatar", "header", NULL };
4370        int n;
4371        for (n = 0; uploads[n]; n++) {
4372            xs *var_name = xs_fmt("%s_file", uploads[n]);
4373
4374            const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
4375            if (xs_type(uploaded_file) == XSTYPE_LIST) {
4376                const char *fn = xs_list_get(uploaded_file, 0);
4377
4378                if (fn && *fn) {
4379                    const char *mimetype = xs_mime_by_ext(fn);
4380
4381                    if (xs_startswith(mimetype, "image/")) {
4382                        const char *ext = strrchr(fn, '.');
4383                        xs *hash        = xs_md5_hex(fn, strlen(fn));
4384                        xs *id          = xs_fmt("%s%s", hash, ext);
4385                        xs *url         = xs_fmt("%s/s/%s", snac.actor, id);
4386                        int fo          = xs_number_get(xs_list_get(uploaded_file, 1));
4387                        int fs          = xs_number_get(xs_list_get(uploaded_file, 2));
4388
4389                        /* store */
4390                        static_put(&snac, id, payload + fo, fs);
4391
4392                        snac.config = xs_dict_set(snac.config, uploads[n], url);
4393                    }
4394                }
4395            }
4396        }
4397
4398        /* delete images by removing url from user.json */
4399        for (n = 0; uploads[n]; n++) {
4400            xs *var_name = xs_fmt("%s_delete", uploads[n]);
4401            const char *delete_var = xs_dict_get(p_vars, var_name);
4402
4403            if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
4404                    snac.config = xs_dict_set(snac.config, uploads[n], "");
4405            }
4406        }
4407
4408        /* password change? */
4409        if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
4410            (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
4411            *p1 && strcmp(p1, p2) == 0) {
4412            xs *pw = hash_password(snac.uid, p1, NULL);
4413            snac.config = xs_dict_set(snac.config, "passwd", pw);
4414        }
4415
4416        user_persist(&snac, 1);
4417
4418        status = HTTP_STATUS_SEE_OTHER;
4419    }
4420    else
4421    if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
4422        notify_clear(&snac);
4423        timeline_touch(&snac);
4424
4425        status = HTTP_STATUS_SEE_OTHER;
4426    }
4427    else
4428    if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
4429        const char *irt   = xs_dict_get(p_vars, "irt");
4430        const char *opt   = xs_dict_get(p_vars, "question");
4431        const char *actor = xs_dict_get(p_vars, "actor");
4432
4433        xs *ls = NULL;
4434
4435        /* multiple choices? */
4436        if (xs_type(opt) == XSTYPE_LIST)
4437            ls = xs_dup(opt);
4438        else
4439        if (xs_type(opt) == XSTYPE_STRING) {
4440            ls = xs_list_new();
4441            ls = xs_list_append(ls, opt);
4442        }
4443
4444        const xs_str *v;
4445        int c = 0;
4446
4447        while (xs_list_next(ls, &v, &c)) {
4448            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL);
4449
4450            /* set the option */
4451            msg = xs_dict_append(msg, "name", v);
4452
4453            xs *c_msg = msg_create(&snac, msg);
4454
4455            enqueue_message(&snac, c_msg);
4456
4457            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
4458        }
4459
4460        if (ls != NULL) {
4461            /* get the poll object */
4462            xs *poll = NULL;
4463
4464            if (valid_status(object_get(irt, &poll))) {
4465                const char *date = xs_dict_get(poll, "endTime");
4466                if (xs_is_null(date))
4467                    date = xs_dict_get(poll, "closed");
4468
4469                if (!xs_is_null(date)) {
4470                    time_t t = xs_parse_iso_date(date, 0) - time(NULL);
4471
4472                    /* request the poll when it's closed;
4473                       Pleroma does not send and update when the poll closes */
4474                    enqueue_object_request(&snac, irt, t + 2);
4475                }
4476            }
4477        }
4478
4479        status = HTTP_STATUS_SEE_OTHER;
4480    }
4481    else
4482    if (p_path && strcmp(p_path, "admin/followed-hashtags") == 0) { /** **/
4483        const char *followed_hashtags = xs_dict_get(p_vars, "followed_hashtags");
4484
4485        if (xs_is_string(followed_hashtags)) {
4486            xs *new_hashtags = xs_list_new();
4487            xs *l = xs_split(followed_hashtags, "\n");
4488            const char *v;
4489
4490            xs_list_foreach(l, v) {
4491                xs *s1 = xs_strip_i(xs_dup(v));
4492                s1 = xs_replace_i(s1, " ", "");
4493
4494                if (*s1 == '\0')
4495                    continue;
4496
4497                xs *s2 = xs_utf8_to_lower(s1);
4498                if (*s2 != '#')
4499                    s2 = xs_str_prepend_i(s2, "#");
4500
4501                new_hashtags = xs_list_append(new_hashtags, s2);
4502            }
4503
4504            snac.config = xs_dict_set(snac.config, "followed_hashtags", new_hashtags);
4505            user_persist(&snac, 0);
4506        }
4507
4508        status = HTTP_STATUS_SEE_OTHER;
4509    }
4510
4511    if (status == HTTP_STATUS_SEE_OTHER) {
4512        const char *redir = xs_dict_get(p_vars, "redir");
4513
4514        if (xs_is_null(redir))
4515            redir = "top";
4516
4517        *body   = xs_fmt("%s/admin#%s", snac.actor, redir);
4518        *b_size = strlen(*body);
4519    }
4520
4521    user_free(&snac);
4522
4523    return status;
4524}
4525
4526
4527xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
4528                        const char *title, const char *link, const char *desc)
4529/* converts a timeline to rss */
4530{
4531    xs_html *rss = xs_html_tag("rss",
4532        xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
4533        xs_html_attr("version",       "2.0"),
4534        xs_html_attr("xmlns:atom",    "http:/" "/www.w3.org/2005/Atom"));
4535
4536    xs_html *channel = xs_html_tag("channel",
4537        xs_html_tag("title",
4538            xs_html_text(title)),
4539        xs_html_tag("language",
4540            xs_html_text("en")),
4541        xs_html_tag("link",
4542            xs_html_text(link)),
4543        xs_html_sctag("atom:link",
4544            xs_html_attr("href", link),
4545            xs_html_attr("rel", "self"),
4546            xs_html_attr("type", "application/rss+xml")),
4547        xs_html_tag("generator",
4548            xs_html_text(USER_AGENT)),
4549        xs_html_tag("description",
4550            xs_html_text(desc)));
4551
4552    xs_html_add(rss, channel);
4553
4554    int cnt = 0;
4555    const char *v;
4556
4557    xs_list_foreach(timeline, v) {
4558        xs *msg = NULL;
4559
4560        if (user) {
4561            if (!valid_status(timeline_get_by_md5(user, v, &msg)))
4562                continue;
4563        }
4564        else {
4565            if (!valid_status(object_get_by_md5(v, &msg)))
4566                continue;
4567        }
4568
4569        const char *id = xs_dict_get(msg, "id");
4570        const char *content = xs_dict_get(msg, "content");
4571        const char *published = xs_dict_get(msg, "published");
4572
4573        if (user && !xs_startswith(id, user->actor))
4574            continue;
4575
4576        if (!id || !content || !published)
4577            continue;
4578
4579        /* create a title with the first line of the content */
4580        xs *title = xs_replace(content, "<br>", "\n");
4581        title = xs_regex_replace_i(title, "<[^>]+>", " ");
4582        title = xs_regex_replace_i(title, "&[^;]+;", " ");
4583        int i;
4584
4585        for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
4586
4587        if (title[i] != '\0') {
4588            title[i] = '\0';
4589            title = xs_str_cat(title, "...");
4590        }
4591
4592        title = xs_strip_i(title);
4593
4594        /* convert the date */
4595        time_t t = xs_parse_iso_date(published, 0);
4596        xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
4597
4598        /* if it's the first one, add it to the header */
4599        if (cnt == 0)
4600            xs_html_add(channel,
4601                xs_html_tag("lastBuildDate",
4602                    xs_html_text(rss_date)));
4603
4604        xs_html_add(channel,
4605            xs_html_tag("item",
4606                xs_html_tag("title",
4607                    xs_html_text(title)),
4608                xs_html_tag("link",
4609                    xs_html_text(id)),
4610                xs_html_tag("guid",
4611                    xs_html_text(id)),
4612                xs_html_tag("pubDate",
4613                    xs_html_text(rss_date)),
4614                xs_html_tag("description",
4615                    xs_html_text(content))));
4616
4617        cnt++;
4618    }
4619
4620    return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
4621}