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