2.65 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
 943    xs_html_add(body,
 944        top_user);
 945
 946    return body;
 947}
 948
 949
 950xs_html *html_top_controls(snac *snac)
 951/* generates the top controls */
 952{
 953    xs *ops_action = xs_fmt("%s/admin/action", snac->actor);
 954
 955    xs_html *top_controls = xs_html_tag("div",
 956        xs_html_attr("class", "snac-top-controls"),
 957
 958        /** new post **/
 959        html_note(snac, L("New Post..."),
 960            "new_post_div", "new_post_form",
 961            L("What's on your mind?"), "",
 962            NULL, NULL,
 963            xs_stock(XSTYPE_FALSE), "",
 964            xs_stock(XSTYPE_FALSE), NULL,
 965            NULL, 1, "", "", 0),
 966
 967        /** operations **/
 968        xs_html_tag("details",
 969            xs_html_tag("summary",
 970                xs_html_text(L("Operations..."))),
 971            xs_html_tag("p", NULL),
 972            xs_html_tag("form",
 973                xs_html_attr("autocomplete", "off"),
 974                xs_html_attr("method",       "post"),
 975                xs_html_attr("action",       ops_action),
 976                xs_html_sctag("input",
 977                    xs_html_attr("type",     "text"),
 978                    xs_html_attr("name",     "actor"),
 979                    xs_html_attr("required", "required"),
 980                    xs_html_attr("placeholder", "bob@example.com")),
 981                xs_html_text(" "),
 982                xs_html_sctag("input",
 983                    xs_html_attr("type",    "submit"),
 984                    xs_html_attr("name",    "action"),
 985                    xs_html_attr("value",   L("Follow"))),
 986            xs_html_text(" "),
 987            xs_html_text(L("(by URL or user@host)"))),
 988            xs_html_tag("p", NULL),
 989            xs_html_tag("form",
 990                xs_html_attr("autocomplete", "off"),
 991                xs_html_attr("method",       "post"),
 992                xs_html_attr("action",       ops_action),
 993                xs_html_sctag("input",
 994                    xs_html_attr("type",     "url"),
 995                    xs_html_attr("name",     "id"),
 996                    xs_html_attr("required", "required"),
 997                    xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
 998                xs_html_text(" "),
 999                xs_html_sctag("input",
1000                    xs_html_attr("type",    "submit"),
1001                    xs_html_attr("name",    "action"),
1002                    xs_html_attr("value",   L("Boost"))),
1003                xs_html_text(" "),
1004                xs_html_text(L("(by URL)"))),
1005            xs_html_tag("p", NULL)));
1006
1007    /** user settings **/
1008
1009    const char *email = "[disabled by admin]";
1010
1011    if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
1012        email = xs_dict_get(snac->config_o, "email");
1013        if (xs_is_null(email)) {
1014            email = xs_dict_get(snac->config, "email");
1015
1016            if (xs_is_null(email))
1017                email = "";
1018        }
1019    }
1020
1021    const char *cw = xs_dict_get(snac->config, "cw");
1022    if (xs_is_null(cw))
1023        cw = "";
1024
1025    const char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
1026    if (xs_is_null(telegram_bot))
1027        telegram_bot = "";
1028
1029    const char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
1030    if (xs_is_null(telegram_chat_id))
1031        telegram_chat_id = "";
1032
1033    const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
1034    if (xs_is_null(ntfy_server))
1035        ntfy_server = "";
1036
1037    const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
1038    if (xs_is_null(ntfy_token))
1039        ntfy_token = "";
1040
1041    const char *purge_days = xs_dict_get(snac->config, "purge_days");
1042    if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
1043        purge_days = (char *)xs_number_str(purge_days);
1044    else
1045        purge_days = "0";
1046
1047    const xs_val *d_dm_f_u  = xs_dict_get(snac->config, "drop_dm_from_unknown");
1048    const xs_val *bot       = xs_dict_get(snac->config, "bot");
1049    const xs_val *a_private = xs_dict_get(snac->config, "private");
1050    const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
1051    const xs_val *coll_thrds = xs_dict_get(snac->config, "collapse_threads");
1052    const xs_val *pending    = xs_dict_get(snac->config, "approve_followers");
1053
1054    xs *metadata = NULL;
1055    const xs_dict *md = xs_dict_get(snac->config, "metadata");
1056
1057    if (xs_type(md) == XSTYPE_DICT) {
1058        const xs_str *k;
1059        const xs_str *v;
1060
1061        metadata = xs_str_new(NULL);
1062
1063        xs_dict_foreach(md, k, v) {
1064            xs *kp = xs_fmt("%s=%s", k, v);
1065
1066            if (*metadata)
1067                metadata = xs_str_cat(metadata, "\n");
1068            metadata = xs_str_cat(metadata, kp);
1069        }
1070    }
1071    else
1072    if (xs_type(md) == XSTYPE_STRING)
1073        metadata = xs_dup(md);
1074    else
1075        metadata = xs_str_new(NULL);
1076
1077    xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
1078
1079    xs_html_add(top_controls,
1080        xs_html_tag("details",
1081        xs_html_tag("summary",
1082            xs_html_text(L("User Settings..."))),
1083        xs_html_tag("div",
1084            xs_html_attr("class", "snac-user-setup"),
1085            xs_html_tag("form",
1086                xs_html_attr("autocomplete", "off"),
1087                xs_html_attr("method",       "post"),
1088                xs_html_attr("action",       user_setup_action),
1089                xs_html_attr("enctype",      "multipart/form-data"),
1090                xs_html_tag("p",
1091                    xs_html_text(L("Display name:")),
1092                    xs_html_sctag("br", NULL),
1093                    xs_html_sctag("input",
1094                        xs_html_attr("type", "text"),
1095                        xs_html_attr("name", "name"),
1096                        xs_html_attr("value", xs_dict_get(snac->config, "name")),
1097                        xs_html_attr("placeholder", L("Your name")))),
1098                xs_html_tag("p",
1099                    xs_html_text(L("Avatar: ")),
1100                    xs_html_sctag("input",
1101                        xs_html_attr("type", "file"),
1102                        xs_html_attr("name", "avatar_file"))),
1103                xs_html_tag("p",
1104                    xs_html_sctag("input",
1105                        xs_html_attr("type", "checkbox"),
1106                        xs_html_attr("name", "avatar_delete")),
1107                        xs_html_text(L("Delete current avatar"))),
1108                xs_html_tag("p",
1109                    xs_html_text(L("Header image (banner): ")),
1110                    xs_html_sctag("input",
1111                        xs_html_attr("type", "file"),
1112                        xs_html_attr("name", "header_file"))),
1113                xs_html_tag("p",
1114                    xs_html_sctag("input",
1115                        xs_html_attr("type", "checkbox"),
1116                        xs_html_attr("name", "header_delete")),
1117                        xs_html_text(L("Delete current header image"))),
1118                xs_html_tag("p",
1119                    xs_html_text(L("Bio:")),
1120                    xs_html_sctag("br", NULL),
1121                    xs_html_tag("textarea",
1122                        xs_html_attr("name", "bio"),
1123                        xs_html_attr("cols", "40"),
1124                        xs_html_attr("rows", "4"),
1125                        xs_html_attr("placeholder", L("Write about yourself here...")),
1126                        xs_html_text(xs_dict_get(snac->config, "bio")))),
1127                xs_html_sctag("input",
1128                    xs_html_attr("type", "checkbox"),
1129                    xs_html_attr("name", "cw"),
1130                    xs_html_attr("id",   "cw"),
1131                    xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
1132                xs_html_tag("label",
1133                    xs_html_attr("for", "cw"),
1134                    xs_html_text(L("Always show sensitive content"))),
1135                xs_html_tag("p",
1136                    xs_html_text(L("Email address for notifications:")),
1137                    xs_html_sctag("br", NULL),
1138                    xs_html_sctag("input",
1139                        xs_html_attr("type", "text"),
1140                        xs_html_attr("name", "email"),
1141                        xs_html_attr("value", email),
1142                        xs_html_attr("placeholder", "bob@example.com"))),
1143                xs_html_tag("p",
1144                    xs_html_text(L("Telegram notifications (bot key and chat id):")),
1145                    xs_html_sctag("br", NULL),
1146                    xs_html_sctag("input",
1147                        xs_html_attr("type", "text"),
1148                        xs_html_attr("name", "telegram_bot"),
1149                        xs_html_attr("value", telegram_bot),
1150                        xs_html_attr("placeholder", "Bot API key")),
1151                    xs_html_text(" "),
1152                    xs_html_sctag("input",
1153                        xs_html_attr("type", "text"),
1154                        xs_html_attr("name", "telegram_chat_id"),
1155                        xs_html_attr("value", telegram_chat_id),
1156                        xs_html_attr("placeholder", "Chat id"))),
1157                xs_html_tag("p",
1158                    xs_html_text(L("ntfy notifications (ntfy server and token):")),
1159                    xs_html_sctag("br", NULL),
1160                    xs_html_sctag("input",
1161                        xs_html_attr("type", "text"),
1162                        xs_html_attr("name", "ntfy_server"),
1163                        xs_html_attr("value", ntfy_server),
1164                        xs_html_attr("placeholder", "ntfy server - full URL (example: https://ntfy.sh/YourTopic)")),
1165                    xs_html_text(" "),
1166                    xs_html_sctag("input",
1167                        xs_html_attr("type", "text"),
1168                        xs_html_attr("name", "ntfy_token"),
1169                        xs_html_attr("value", ntfy_token),
1170                        xs_html_attr("placeholder", "ntfy token - if needed"))),
1171                xs_html_tag("p",
1172                    xs_html_text(L("Maximum days to keep posts (0: server settings):")),
1173                    xs_html_sctag("br", NULL),
1174                    xs_html_sctag("input",
1175                        xs_html_attr("type", "number"),
1176                        xs_html_attr("name", "purge_days"),
1177                        xs_html_attr("value", purge_days))),
1178                xs_html_tag("p",
1179                    xs_html_sctag("input",
1180                        xs_html_attr("type", "checkbox"),
1181                        xs_html_attr("name", "drop_dm_from_unknown"),
1182                        xs_html_attr("id", "drop_dm_from_unknown"),
1183                        xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
1184                    xs_html_tag("label",
1185                        xs_html_attr("for", "drop_dm_from_unknown"),
1186                        xs_html_text(L("Drop direct messages from people you don't follow")))),
1187                xs_html_tag("p",
1188                    xs_html_sctag("input",
1189                        xs_html_attr("type", "checkbox"),
1190                        xs_html_attr("name", "bot"),
1191                        xs_html_attr("id", "bot"),
1192                        xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
1193                    xs_html_tag("label",
1194                        xs_html_attr("for", "bot"),
1195                        xs_html_text(L("This account is a bot")))),
1196                xs_html_tag("p",
1197                    xs_html_sctag("input",
1198                        xs_html_attr("type", "checkbox"),
1199                        xs_html_attr("name", "auto_boost"),
1200                        xs_html_attr("id", "auto_boost"),
1201                        xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
1202                    xs_html_tag("label",
1203                        xs_html_attr("for", "auto_boost"),
1204                        xs_html_text(L("Auto-boost all mentions to this account")))),
1205                xs_html_tag("p",
1206                    xs_html_sctag("input",
1207                        xs_html_attr("type", "checkbox"),
1208                        xs_html_attr("name", "private"),
1209                        xs_html_attr("id", "private"),
1210                        xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
1211                    xs_html_tag("label",
1212                        xs_html_attr("for", "private"),
1213                        xs_html_text(L("This account is private "
1214                            "(posts are not shown through the web)")))),
1215                xs_html_tag("p",
1216                    xs_html_sctag("input",
1217                        xs_html_attr("type", "checkbox"),
1218                        xs_html_attr("name", "collapse_threads"),
1219                        xs_html_attr("id",   "collapse_threads"),
1220                        xs_html_attr(xs_is_true(coll_thrds) ? "checked" : "", NULL)),
1221                    xs_html_tag("label",
1222                        xs_html_attr("for", "collapse_threads"),
1223                        xs_html_text(L("Collapse top threads by default")))),
1224                xs_html_tag("p",
1225                    xs_html_sctag("input",
1226                        xs_html_attr("type", "checkbox"),
1227                        xs_html_attr("name", "approve_followers"),
1228                        xs_html_attr("id",   "approve_followers"),
1229                        xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
1230                    xs_html_tag("label",
1231                        xs_html_attr("for", "approve_followers"),
1232                        xs_html_text(L("Follow requests must be approved")))),
1233                xs_html_tag("p",
1234                    xs_html_text(L("Profile metadata (key=value pairs in each line):")),
1235                    xs_html_sctag("br", NULL),
1236                    xs_html_tag("textarea",
1237                        xs_html_attr("name", "metadata"),
1238                        xs_html_attr("cols", "40"),
1239                        xs_html_attr("rows", "4"),
1240                        xs_html_attr("placeholder", "Blog=https:/"
1241                            "/example.com/my-blog\nGPG Key=1FA54\n..."),
1242                    xs_html_text(metadata))),
1243
1244                xs_html_tag("p",
1245                    xs_html_text(L("New password:")),
1246                    xs_html_sctag("br", NULL),
1247                    xs_html_sctag("input",
1248                        xs_html_attr("type", "password"),
1249                        xs_html_attr("name", "passwd1"),
1250                        xs_html_attr("value", ""))),
1251                xs_html_tag("p",
1252                    xs_html_text(L("Repeat new password:")),
1253                    xs_html_sctag("br", NULL),
1254                    xs_html_sctag("input",
1255                        xs_html_attr("type", "password"),
1256                        xs_html_attr("name", "passwd2"),
1257                        xs_html_attr("value", ""))),
1258
1259                xs_html_sctag("input",
1260                    xs_html_attr("type", "submit"),
1261                    xs_html_attr("class", "button"),
1262                    xs_html_attr("value", L("Update user info")))))));
1263
1264    return top_controls;
1265}
1266
1267
1268static xs_html *html_button(char *clss, char *label, char *hint)
1269{
1270    xs *c = xs_fmt("snac-btn-%s", clss);
1271
1272    /* use an NULL tag to separate non-css-classed buttons from one another */
1273    return xs_html_container(
1274        xs_html_sctag("input",
1275            xs_html_attr("type",    "submit"),
1276            xs_html_attr("name",    "action"),
1277            xs_html_attr("class",   c),
1278            xs_html_attr("value",   label),
1279            xs_html_attr("title",   hint)),
1280        xs_html_text("\n"));
1281}
1282
1283
1284xs_str *build_mentions(snac *snac, const xs_dict *msg)
1285/* returns a string with the mentions in msg */
1286{
1287    xs_str *s = xs_str_new(NULL);
1288    const char *list = xs_dict_get(msg, "tag");
1289    const char *v;
1290    int c = 0;
1291
1292    while (xs_list_next(list, &v, &c)) {
1293        const char *type = xs_dict_get(v, "type");
1294        const char *href = xs_dict_get(v, "href");
1295        const char *name = xs_dict_get(v, "name");
1296
1297        if (type && strcmp(type, "Mention") == 0 &&
1298            href && strcmp(href, snac->actor) != 0 && name) {
1299            xs *s1 = NULL;
1300
1301            if (name[0] != '@') {
1302                s1 = xs_fmt("@%s", name);
1303                name = s1;
1304            }
1305
1306            xs *l = xs_split(name, "@");
1307
1308            /* is it a name without a host? */
1309            if (xs_list_len(l) < 3) {
1310                /* split the href and pick the host name LIKE AN ANIMAL */
1311                /* would be better to query the webfinger but *won't do that* here */
1312                xs *l2 = xs_split(href, "/");
1313
1314                if (xs_list_len(l2) >= 3) {
1315                    xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
1316
1317                    if (xs_str_in(s, s1) == -1)
1318                        s = xs_str_cat(s, s1);
1319                }
1320            }
1321            else {
1322                if (xs_str_in(s, name) == -1) {
1323                    s = xs_str_cat(s, name);
1324                    s = xs_str_cat(s, " ");
1325                }
1326            }
1327        }
1328    }
1329
1330    if (*s) {
1331        xs *s1 = s;
1332        s = xs_fmt("\n\n\nCC: %s", s1);
1333    }
1334
1335    return s;
1336}
1337
1338
1339xs_html *html_entry_controls(snac *snac, const char *actor,
1340                            const xs_dict *msg, const char *md5)
1341{
1342    const char *id    = xs_dict_get(msg, "id");
1343    const char *group = xs_dict_get(msg, "audience");
1344
1345    xs *likes   = object_likes(id);
1346    xs *boosts  = object_announces(id);
1347
1348    xs *action = xs_fmt("%s/admin/action", snac->actor);
1349    xs *redir  = xs_fmt("%s_entry", md5);
1350
1351    xs_html *form;
1352    xs_html *controls = xs_html_tag("div",
1353        xs_html_attr("class", "snac-controls"),
1354        form = xs_html_tag("form",
1355            xs_html_attr("autocomplete", "off"),
1356            xs_html_attr("method",       "post"),
1357            xs_html_attr("action",       action),
1358            xs_html_sctag("input",
1359                xs_html_attr("type",     "hidden"),
1360                xs_html_attr("name",     "id"),
1361                xs_html_attr("value",    id)),
1362            xs_html_sctag("input",
1363                xs_html_attr("type",     "hidden"),
1364                xs_html_attr("name",     "actor"),
1365                xs_html_attr("value",    actor)),
1366            xs_html_sctag("input",
1367                xs_html_attr("type",     "hidden"),
1368                xs_html_attr("name",     "group"),
1369                xs_html_attr("value",    xs_is_null(group) ? "" : group)),
1370            xs_html_sctag("input",
1371                xs_html_attr("type",     "hidden"),
1372                xs_html_attr("name",     "redir"),
1373                xs_html_attr("value",    redir))));
1374
1375    if (!xs_startswith(id, snac->actor)) {
1376        if (xs_list_in(likes, snac->md5) == -1) {
1377            /* not already liked; add button */
1378            xs_html_add(form,
1379                html_button("like", L("Like"), L("Say you like this post")));
1380        }
1381        else {
1382            /* not like it anymore */
1383            xs_html_add(form,
1384                html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
1385        }
1386    }
1387    else {
1388        if (is_pinned(snac, id))
1389            xs_html_add(form,
1390                html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
1391        else
1392            xs_html_add(form,
1393                html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
1394    }
1395
1396    if (is_msg_public(msg)) {
1397        if (xs_list_in(boosts, snac->md5) == -1) {
1398            /* not already boosted; add button */
1399            xs_html_add(form,
1400                html_button("boost", L("Boost"), L("Announce this post to your followers")));
1401        }
1402        else {
1403            /* already boosted; add button to regret */
1404            xs_html_add(form,
1405                html_button("unboost", L("Unboost"), L("I regret I boosted this")));
1406        }
1407    }
1408
1409    if (is_bookmarked(snac, id))
1410            xs_html_add(form,
1411                html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
1412        else
1413            xs_html_add(form,
1414                html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
1415
1416    if (strcmp(actor, snac->actor) != 0) {
1417        /* controls for other actors than this one */
1418        if (following_check(snac, actor)) {
1419            xs_html_add(form,
1420                html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
1421        }
1422        else {
1423            xs_html_add(form,
1424                html_button("follow", L("Follow"), L("Start following this user's activity")));
1425        }
1426
1427        if (!xs_is_null(group)) {
1428            if (following_check(snac, group)) {
1429                xs_html_add(form,
1430                    html_button("unfollow", L("Unfollow Group"),
1431                        L("Stop following this group or channel")));
1432            }
1433            else {
1434                xs_html_add(form,
1435                    html_button("follow", L("Follow Group"),
1436                        L("Start following this group or channel")));
1437            }
1438        }
1439
1440        xs_html_add(form,
1441            html_button("mute", L("MUTE"),
1442                L("Block any activity from this user forever")));
1443    }
1444
1445    if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
1446        xs_html_add(form,
1447            html_button("delete", L("Delete"), L("Delete this post")));
1448
1449    xs_html_add(form,
1450        html_button("hide",   L("Hide"), L("Hide this post and its children")));
1451
1452    const char *prev_src = xs_dict_get(msg, "sourceContent");
1453
1454    if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) { /** edit **/
1455        /* post can be edited */
1456        xs *div_id  = xs_fmt("%s_edit", md5);
1457        xs *form_id = xs_fmt("%s_edit_form", md5);
1458        xs *redir   = xs_fmt("%s_entry", md5);
1459
1460        const char *att_file = "";
1461        const char *att_alt_text = "";
1462        const xs_list *att_list = xs_dict_get(msg, "attachment");
1463
1464        /* does it have an attachment? */
1465        if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) {
1466            const xs_dict *d = xs_list_get(att_list, 0);
1467
1468            if (xs_type(d) == XSTYPE_DICT) {
1469                att_file = xs_dict_get_def(d, "url", "");
1470                att_alt_text = xs_dict_get_def(d, "name", "");
1471            }
1472        }
1473
1474        xs_html_add(controls, xs_html_tag("div",
1475            xs_html_tag("p", NULL),
1476            html_note(snac, L("Edit..."),
1477                div_id, form_id,
1478                "", prev_src,
1479                id, NULL,
1480                xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1481                xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1482                NULL, 0, att_file, att_alt_text, is_draft(snac, id))),
1483            xs_html_tag("p", NULL));
1484    }
1485
1486    { /** reply **/
1487        /* the post textarea */
1488        xs *ct      = build_mentions(snac, msg);
1489        xs *div_id  = xs_fmt("%s_reply", md5);
1490        xs *form_id = xs_fmt("%s_reply_form", md5);
1491        xs *redir   = xs_fmt("%s_entry", md5);
1492
1493        xs_html_add(controls, xs_html_tag("div",
1494            xs_html_tag("p", NULL),
1495            html_note(snac, L("Reply..."),
1496                div_id, form_id,
1497                "", ct,
1498                NULL, NULL,
1499                xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
1500                xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
1501                id, 0, "", "", 0)),
1502            xs_html_tag("p", NULL));
1503    }
1504
1505    return controls;
1506}
1507
1508
1509xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
1510                   int level, const char *md5, int hide_children)
1511{
1512    const char *id    = xs_dict_get(msg, "id");
1513    const char *type  = xs_dict_get(msg, "type");
1514    const char *actor;
1515    const char *v;
1516    int has_title = 0;
1517    int collapse_threads = 0;
1518    const char *proxy = NULL;
1519
1520    if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
1521        proxy = user->actor;
1522
1523    /* do not show non-public messages in the public timeline */
1524    if ((read_only || !user) && !is_msg_public(msg))
1525        return NULL;
1526
1527    if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
1528        collapse_threads = 1;
1529
1530    /* hidden? do nothing more for this conversation */
1531    if (user && is_hidden(user, id)) {
1532        xs *s1 = xs_fmt("%s_entry", md5);
1533
1534        /* return just an dummy anchor, to keep position after hitting 'Hide' */
1535        return xs_html_tag("div",
1536            xs_html_tag("a",
1537                xs_html_attr("name", s1)));
1538    }
1539
1540    /* avoid too deep nesting, as it may be a loop */
1541    if (level >= MAX_CONVERSATION_LEVELS)
1542        return xs_html_tag("mark",
1543            xs_html_text(L("Truncated (too deep)")));
1544
1545    if (strcmp(type, "Follow") == 0) {
1546        return xs_html_tag("div",
1547            xs_html_attr("class", "snac-post"),
1548            xs_html_tag("div",
1549                xs_html_attr("class", "snac-post-header"),
1550                xs_html_tag("div",
1551                    xs_html_attr("class", "snac-origin"),
1552                    xs_html_text(L("follows you"))),
1553                html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy)));
1554    }
1555    else
1556    if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
1557        /* skip oddities */
1558        snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
1559        return NULL;
1560    }
1561
1562    /* ignore notes with "name", as they are votes to Questions */
1563    if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
1564        return NULL;
1565
1566    /* get the attributedTo */
1567    if ((actor = get_atto(msg)) == NULL)
1568        return NULL;
1569
1570    /* ignore muted morons immediately */
1571    if (user && is_muted(user, actor)) {
1572        xs *s1 = xs_fmt("%s_entry", md5);
1573
1574        /* return just an dummy anchor, to keep position after hitting 'MUTE' */
1575        return xs_html_tag("div",
1576            xs_html_tag("a",
1577                xs_html_attr("name", s1)));
1578    }
1579
1580    if ((user == NULL || strcmp(actor, user->actor) != 0)
1581        && !valid_status(actor_get(actor, NULL)))
1582        return NULL;
1583
1584    /** html_entry top tag **/
1585    xs_html *entry_top = xs_html_tag("div", NULL);
1586
1587    {
1588        xs *s1 = xs_fmt("%s_entry", md5);
1589        xs_html_add(entry_top,
1590            xs_html_tag("a",
1591                xs_html_attr("name", s1)));
1592    }
1593
1594    xs_html *entry = xs_html_tag("div",
1595        xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
1596
1597    xs_html_add(entry_top,
1598        entry);
1599
1600    /** post header **/
1601
1602    xs_html *score;
1603    xs_html *post_header = xs_html_tag("div",
1604        xs_html_attr("class", "snac-post-header"),
1605        score = xs_html_tag("div",
1606            xs_html_attr("class", "snac-score")));
1607
1608    xs_html_add(entry,
1609        post_header);
1610
1611    if (user && is_pinned(user, id)) {
1612        /* add a pin emoji */
1613        xs_html_add(score,
1614            xs_html_tag("span",
1615                xs_html_attr("title", L("Pinned")),
1616                xs_html_raw(" &#128204; ")));
1617    }
1618
1619    if (user && is_bookmarked(user, id)) {
1620        /* add a bookmark emoji */
1621        xs_html_add(score,
1622            xs_html_tag("span",
1623                xs_html_attr("title", L("Bookmarked")),
1624                xs_html_raw(" &#128278; ")));
1625    }
1626
1627    if (strcmp(type, "Question") == 0) {
1628        /* add the ballot box emoji */
1629        xs_html_add(score,
1630            xs_html_tag("span",
1631                xs_html_attr("title", L("Poll")),
1632                xs_html_raw(" &#128499; ")));
1633
1634        if (user && was_question_voted(user, id)) {
1635            /* add a check to show this poll was voted */
1636            xs_html_add(score,
1637                xs_html_tag("span",
1638                    xs_html_attr("title", L("Voted")),
1639                    xs_html_raw(" &#10003; ")));
1640        }
1641    }
1642
1643    if (strcmp(type, "Event") == 0) {
1644        /* add the calendar emoji */
1645        xs_html_add(score,
1646            xs_html_tag("span",
1647                xs_html_attr("title", L("Event")),
1648                xs_html_raw(" &#128197; ")));
1649    }
1650
1651    /* if it's a user from this same instance, add the score */
1652    if (xs_startswith(id, srv_baseurl)) {
1653        int n_likes  = object_likes_len(id);
1654        int n_boosts = object_announces_len(id);
1655
1656        /* alternate emojis: %d &#128077; %d &#128257; */
1657        xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
1658
1659        xs_html_add(score,
1660            xs_html_raw(s1));
1661    }
1662
1663    xs *boosts = object_announces(id);
1664
1665    if (xs_list_len(boosts)) {
1666        /* if somebody boosted this, show as origin */
1667        const char *p = xs_list_get(boosts, -1);
1668        xs *actor_r = NULL;
1669
1670        if (user && xs_list_in(boosts, user->md5) != -1) {
1671            /* we boosted this */
1672            xs_html_add(post_header,
1673                xs_html_tag("div",
1674                    xs_html_attr("class", "snac-origin"),
1675                    xs_html_tag("a",
1676                        xs_html_attr("href", user->actor),
1677                        xs_html_text(xs_dict_get(user->config, "name"))),
1678                        xs_html_text(" "),
1679                        xs_html_text(L("boosted"))));
1680        }
1681        else
1682        if (valid_status(object_get_by_md5(p, &actor_r))) {
1683            xs *name = actor_name(actor_r, proxy);
1684
1685            if (!xs_is_null(name)) {
1686                xs *href = NULL;
1687                const char *id = xs_dict_get(actor_r, "id");
1688                int fwers = 0;
1689                int fwing = 0;
1690
1691                if (user != NULL) {
1692                    fwers = follower_check(user, id);
1693                    fwing = following_check(user, id);
1694                }
1695
1696                if (!read_only && (fwers || fwing))
1697                    href = xs_fmt("%s/people#%s", user->actor, p);
1698                else
1699                    href = xs_dup(id);
1700
1701                xs_html_add(post_header,
1702                    xs_html_tag("div",
1703                        xs_html_attr("class", "snac-origin"),
1704                        xs_html_tag("a",
1705                            xs_html_attr("href", href),
1706                            xs_html_raw(name)), /* already sanitized */
1707                            xs_html_text(" "),
1708                            xs_html_text(L("boosted"))));
1709            }
1710        }
1711    }
1712    else
1713    if (strcmp(type, "Note") == 0) {
1714        if (level == 0) {
1715            /* is the parent not here? */
1716            const char *parent = get_in_reply_to(msg);
1717
1718            if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
1719                xs_html_add(post_header,
1720                    xs_html_tag("div",
1721                        xs_html_attr("class", "snac-origin"),
1722                        xs_html_text(L("in reply to")),
1723                        xs_html_text(" "),
1724                        xs_html_tag("a",
1725                            xs_html_attr("href", parent),
1726                            xs_html_text("ยป"))));
1727            }
1728        }
1729    }
1730
1731    xs_html_add(post_header,
1732        html_msg_icon(read_only ? NULL : user, actor, msg, proxy));
1733
1734    /** post content **/
1735
1736    xs_html *snac_content_wrap = xs_html_tag("div",
1737        xs_html_attr("class", "e-content snac-content"));
1738
1739    xs_html_add(entry,
1740        snac_content_wrap);
1741
1742    if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
1743        xs_html_add(snac_content_wrap,
1744            xs_html_tag("h3",
1745                xs_html_attr("class", "snac-entry-title"),
1746                xs_html_text(v)));
1747
1748        has_title = 1;
1749    }
1750
1751    xs_html *snac_content = NULL;
1752
1753    v = xs_dict_get(msg, "summary");
1754
1755    /* is it sensitive? */
1756    if (xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
1757        if (xs_is_null(v) || *v == '\0')
1758            v = "...";
1759
1760        const char *cw = "";
1761
1762        if (user) {
1763            /* only show it when not in the public timeline and the config setting is "open" */
1764            cw = xs_dict_get(user->config, "cw");
1765            if (xs_is_null(cw) || read_only)
1766                cw = "";
1767        }
1768
1769        snac_content = xs_html_tag("details",
1770            xs_html_attr(cw, NULL),
1771            xs_html_tag("summary",
1772                xs_html_text(v),
1773                xs_html_text(L(" [SENSITIVE CONTENT]"))));
1774    }
1775    else {
1776        /* print the summary as a header (sites like e.g. Friendica can contain one) */
1777        if (!has_title && !xs_is_null(v) && *v) {
1778            xs_html_add(snac_content_wrap,
1779                xs_html_tag("h3",
1780                    xs_html_attr("class", "snac-entry-title"),
1781                    xs_html_text(v)));
1782
1783            has_title = 1;
1784        }
1785
1786        snac_content = xs_html_tag("div", NULL);
1787    }
1788
1789    xs_html_add(snac_content_wrap,
1790        snac_content);
1791
1792    {
1793        /** build the content string **/
1794        const char *content = xs_dict_get(msg, "content");
1795
1796        if (xs_type(content) != XSTYPE_STRING) {
1797            if (!xs_is_null(content))
1798                srv_archive_error("unexpected_content_xstype",
1799                    "content field type", xs_stock(XSTYPE_DICT), msg);
1800
1801            content = "";
1802        }
1803
1804        /* skip ugly line breaks at the beginning */
1805        while (xs_startswith(content, "<br>"))
1806            content += 4;
1807
1808        xs *c = sanitize(content);
1809
1810        /* do some tweaks to the content */
1811        c = xs_replace_i(c, "\r", "");
1812
1813        while (xs_endswith(c, "<br><br>"))
1814            c = xs_crop_i(c, 0, -4);
1815
1816        c = xs_replace_i(c, "<br><br>", "<p>");
1817
1818        c = xs_str_cat(c, "<p>");
1819
1820        /* replace the :shortnames: */
1821        c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
1822
1823        /* Peertube videos content is in markdown */
1824        const char *mtype = xs_dict_get(msg, "mediaType");
1825        if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
1826            /* a full conversion could be better */
1827            c = xs_replace_i(c, "\r", "");
1828            c = xs_replace_i(c, "\n", "<br>");
1829        }
1830
1831        /* c contains sanitized HTML */
1832        xs_html_add(snac_content,
1833            xs_html_raw(c));
1834    }
1835
1836    if (strcmp(type, "Question") == 0) { /** question content **/
1837        const xs_list *oo = xs_dict_get(msg, "oneOf");
1838        const xs_list *ao = xs_dict_get(msg, "anyOf");
1839        const xs_list *p;
1840        const xs_dict *v;
1841        int closed = 0;
1842        const char *f_closed = NULL;
1843
1844        xs_html *poll = xs_html_tag("div", NULL);
1845
1846        if (read_only)
1847            closed = 1; /* non-identified page; show as closed */
1848        else
1849        if (user && xs_startswith(id, user->actor))
1850            closed = 1; /* we questioned; closed for us */
1851        else
1852        if (user && was_question_voted(user, id))
1853            closed = 1; /* we already voted; closed for us */
1854
1855        if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
1856            /* it has a closed date... but is it in the past? */
1857            time_t t0 = time(NULL);
1858            time_t t1 = xs_parse_iso_date(f_closed, 0);
1859
1860            if (t1 < t0)
1861                closed = 2;
1862        }
1863
1864        /* get the appropriate list of options */
1865        p = oo != NULL ? oo : ao;
1866
1867        if (closed || user == NULL) {
1868            /* closed poll */
1869            xs_html *poll_result = xs_html_tag("table",
1870                xs_html_attr("class", "snac-poll-result"));
1871            int c = 0;
1872
1873            while (xs_list_next(p, &v, &c)) {
1874                const char *name       = xs_dict_get(v, "name");
1875                const xs_dict *replies = xs_dict_get(v, "replies");
1876
1877                if (name && replies) {
1878                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1879
1880                    xs_html_add(poll_result,
1881                        xs_html_tag("tr",
1882                            xs_html_tag("td",
1883                                xs_html_text(name),
1884                                xs_html_text(":")),
1885                            xs_html_tag("td",
1886                                xs_html_text(ti))));
1887                }
1888            }
1889
1890            xs_html_add(poll,
1891                poll_result);
1892        }
1893        else {
1894            /* poll still active */
1895            xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
1896            xs_html *form;
1897            xs_html *poll_form = xs_html_tag("div",
1898                xs_html_attr("class", "snac-poll-form"),
1899                form = xs_html_tag("form",
1900                    xs_html_attr("autocomplete", "off"),
1901                    xs_html_attr("method", "post"),
1902                    xs_html_attr("action", vote_action),
1903                    xs_html_sctag("input",
1904                        xs_html_attr("type", "hidden"),
1905                        xs_html_attr("name", "actor"),
1906                        xs_html_attr("value", actor)),
1907                    xs_html_sctag("input",
1908                        xs_html_attr("type", "hidden"),
1909                        xs_html_attr("name", "irt"),
1910                        xs_html_attr("value", id))));
1911
1912            int c = 0;
1913            while (xs_list_next(p, &v, &c)) {
1914                const char *name = xs_dict_get(v, "name");
1915                const xs_dict *replies = xs_dict_get(v, "replies");
1916
1917                if (name) {
1918                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1919
1920                    xs_html *btn = xs_html_sctag("input",
1921                            xs_html_attr("id", name),
1922                            xs_html_attr("value", name),
1923                            xs_html_attr("name", "question"));
1924
1925                    if (!xs_is_null(oo)) {
1926                        xs_html_add(btn,
1927                            xs_html_attr("type", "radio"),
1928                            xs_html_attr("required", "required"));
1929                    }
1930                    else
1931                        xs_html_add(btn,
1932                            xs_html_attr("type", "checkbox"));
1933
1934                    xs_html_add(form,
1935                        btn,
1936                        xs_html_text(" "),
1937                        xs_html_tag("span",
1938                            xs_html_attr("title", ti),
1939                            xs_html_text(name)),
1940                        xs_html_sctag("br", NULL));
1941                }
1942            }
1943
1944            xs_html_add(form,
1945                xs_html_tag("p", NULL),
1946                xs_html_sctag("input",
1947                    xs_html_attr("type", "submit"),
1948                    xs_html_attr("class", "button"),
1949                    xs_html_attr("value", L("Vote"))));
1950
1951            xs_html_add(poll,
1952                poll_form);
1953        }
1954
1955        /* if it's *really* closed, say it */
1956        if (closed == 2) {
1957            xs_html_add(poll,
1958                xs_html_tag("p",
1959                    xs_html_text(L("Closed"))));
1960        }
1961        else {
1962            /* show when the poll closes */
1963            const char *end_time = xs_dict_get(msg, "endTime");
1964
1965            /* Pleroma does not have an endTime field;
1966               it has a closed time in the future */
1967            if (xs_is_null(end_time))
1968                end_time = xs_dict_get(msg, "closed");
1969
1970            if (!xs_is_null(end_time)) {
1971                time_t t0 = time(NULL);
1972                time_t t1 = xs_parse_iso_date(end_time, 0);
1973
1974                if (t1 > 0 && t1 > t0) {
1975                    time_t diff_time = t1 - t0;
1976                    xs *tf = xs_str_time_diff(diff_time);
1977                    char *p = tf;
1978
1979                    /* skip leading zeros */
1980                    for (; *p == '0' || *p == ':'; p++);
1981
1982                    xs_html_add(poll,
1983                        xs_html_tag("p",
1984                            xs_html_text(L("Closes in")),
1985                            xs_html_text(" "),
1986                            xs_html_text(p)));
1987                }
1988            }
1989        }
1990
1991        xs_html_add(snac_content,
1992            poll);
1993    }
1994
1995    /** attachments **/
1996    xs *attach = get_attachments(msg);
1997
1998    {
1999        /* make custom css for attachments easier */
2000        xs_html *content_attachments = xs_html_tag("div",
2001            xs_html_attr("class", "snac-content-attachments"));
2002
2003        xs_html_add(snac_content,
2004            content_attachments);
2005
2006        const char *content = xs_dict_get(msg, "content");
2007
2008        int c = 0;
2009        const xs_dict *a;
2010        while (xs_list_next(attach, &a, &c)) {
2011            const char *type = xs_dict_get(a, "type");
2012            const char *o_href = xs_dict_get(a, "href");
2013            const char *name = xs_dict_get(a, "name");
2014
2015            /* if this image is already in the post content, skip */
2016            if (content && xs_str_in(content, o_href) != -1)
2017                continue;
2018
2019            xs *href = make_url(o_href, proxy, 0);
2020
2021            if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
2022                xs_html_add(content_attachments,
2023                    xs_html_tag("a",
2024                        xs_html_attr("href", href),
2025                        xs_html_attr("target", "_blank"),
2026                        xs_html_sctag("img",
2027                            xs_html_attr("loading", "lazy"),
2028                            xs_html_attr("src", href),
2029                            xs_html_attr("alt", name),
2030                            xs_html_attr("title", name))));
2031            }
2032            else
2033            if (xs_startswith(type, "video/")) {
2034                xs_html_add(content_attachments,
2035                    xs_html_tag("video",
2036                        xs_html_attr("preload", "none"),
2037                        xs_html_attr("style", "width: 100%"),
2038                        xs_html_attr("class", "snac-embedded-video"),
2039                        xs_html_attr("controls", NULL),
2040                        xs_html_attr("src", href),
2041                        xs_html_text(L("Video")),
2042                        xs_html_text(": "),
2043                        xs_html_tag("a",
2044                            xs_html_attr("href", href),
2045                            xs_html_text(name))));
2046            }
2047            else
2048            if (xs_startswith(type, "audio/")) {
2049                xs_html_add(content_attachments,
2050                    xs_html_tag("audio",
2051                        xs_html_attr("preload", "none"),
2052                        xs_html_attr("style", "width: 100%"),
2053                        xs_html_attr("class", "snac-embedded-audio"),
2054                        xs_html_attr("controls", NULL),
2055                        xs_html_attr("src", href),
2056                        xs_html_text(L("Audio")),
2057                        xs_html_text(": "),
2058                        xs_html_tag("a",
2059                            xs_html_attr("href", href),
2060                            xs_html_text(name))));
2061            }
2062            else
2063            if (strcmp(type, "Link") == 0) {
2064                xs_html_add(content_attachments,
2065                    xs_html_tag("p",
2066                        xs_html_tag("a",
2067                            xs_html_attr("href", o_href),
2068                            xs_html_text(href))));
2069
2070                /* do not generate an Alt... */
2071                name = NULL;
2072            }
2073            else {
2074                xs_html_add(content_attachments,
2075                    xs_html_tag("p",
2076                        xs_html_tag("a",
2077                            xs_html_attr("href", o_href),
2078                            xs_html_text(L("Attachment")),
2079                            xs_html_text(": "),
2080                            xs_html_text(o_href))));
2081
2082                /* do not generate an Alt... */
2083                name = NULL;
2084            }
2085
2086            if (name != NULL && *name) {
2087                xs_html_add(content_attachments,
2088                    xs_html_tag("p",
2089                        xs_html_attr("class", "snac-alt-text"),
2090                        xs_html_tag("details",
2091                            xs_html_tag("summary",
2092                                xs_html_text(L("Alt..."))),
2093                            xs_html_text(name))));
2094            }
2095        }
2096    }
2097
2098    /* has this message an audience (i.e., comes from a channel or community)? */
2099    const char *audience = xs_dict_get(msg, "audience");
2100    if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
2101        xs_html *au_tag = xs_html_tag("p",
2102            xs_html_text("("),
2103            xs_html_tag("a",
2104                xs_html_attr("href", audience),
2105                xs_html_attr("title", L("Source channel or community")),
2106                xs_html_text(audience)),
2107            xs_html_text(")"));
2108
2109        xs_html_add(snac_content_wrap,
2110            au_tag);
2111    }
2112
2113    /** controls **/
2114
2115    if (!read_only && user) {
2116        xs_html_add(entry,
2117            html_entry_controls(user, actor, msg, md5));
2118    }
2119
2120    /** children **/
2121    if (!hide_children) {
2122        xs *children = object_children(id);
2123        int left     = xs_list_len(children);
2124
2125        if (left) {
2126            xs_html *ch_details = xs_html_tag("details",
2127                xs_html_attr(collapse_threads ? "" : "open", NULL),
2128                xs_html_tag("summary",
2129                    xs_html_text("...")));
2130
2131            xs_html_add(entry,
2132                ch_details);
2133
2134            xs_html *ch_container = xs_html_tag("div",
2135                xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
2136
2137            xs_html_add(ch_details,
2138                ch_container);
2139
2140            xs_html *ch_older = NULL;
2141            if (left > 3) {
2142                xs_html_add(ch_container,
2143                    ch_older = xs_html_tag("details",
2144                        xs_html_tag("summary",
2145                            xs_html_text(L("Older...")))));
2146            }
2147
2148            xs_list *p = children;
2149            const char *cmd5;
2150            int cnt = 0;
2151            int o_cnt = 0;
2152
2153            while (xs_list_iter(&p, &cmd5)) {
2154                xs *chd = NULL;
2155
2156                if (user)
2157                    timeline_get_by_md5(user, cmd5, &chd);
2158                else
2159                    object_get_by_md5(cmd5, &chd);
2160
2161                if (chd != NULL) {
2162                    if (xs_is_null(xs_dict_get(chd, "name"))) {
2163                        xs_html *che = html_entry(user, chd, read_only,
2164                            level + 1, cmd5, hide_children);
2165
2166                        if (che != NULL) {
2167                            if (left > 3) {
2168                                xs_html_add(ch_older,
2169                                    che);
2170
2171                                o_cnt++;
2172                            }
2173                            else
2174                                xs_html_add(ch_container,
2175                                    che);
2176
2177                            cnt++;
2178                        }
2179                    }
2180
2181                    left--;
2182                }
2183                else
2184                    srv_debug(2, xs_fmt("cannot read child %s", cmd5));
2185            }
2186
2187            /* if no children were finally added, hide the details */
2188            if (cnt == 0)
2189                xs_html_add(ch_details,
2190                    xs_html_attr("style", "display: none"));
2191
2192            if (o_cnt == 0 && ch_older)
2193                xs_html_add(ch_older,
2194                    xs_html_attr("style", "display: none"));
2195        }
2196    }
2197
2198    return entry_top;
2199}
2200
2201
2202xs_html *html_footer(void)
2203{
2204    return xs_html_tag("div",
2205        xs_html_attr("class", "snac-footer"),
2206        xs_html_tag("a",
2207            xs_html_attr("href", srv_baseurl),
2208            xs_html_text(L("about this site"))),
2209        xs_html_text(" - "),
2210        xs_html_text(L("powered by ")),
2211        xs_html_tag("a",
2212            xs_html_attr("href", WHAT_IS_SNAC_URL),
2213            xs_html_tag("abbr",
2214                xs_html_attr("title", "Social Networks Are Crap"),
2215                xs_html_text("snac"))));
2216}
2217
2218
2219xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
2220                      int skip, int show, int show_more,
2221                      const char *title, const char *page,
2222                      int utl, const char *error)
2223/* returns the HTML for the timeline */
2224{
2225    xs_list *p = (xs_list *)list;
2226    const char *v;
2227    double t = ftime();
2228    int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
2229
2230    xs *desc = NULL;
2231    xs *alternate = NULL;
2232
2233    if (xs_list_len(list) == 1) {
2234        /* only one element? pick the description from the source */
2235        const char *id = xs_list_get(list, 0);
2236        xs *d = NULL;
2237        object_get_by_md5(id, &d);
2238        const char *sc = xs_dict_get(d, "sourceContent");
2239        if (d && sc != NULL)
2240            desc = xs_dup(sc);
2241
2242        alternate = xs_dup(xs_dict_get(d, "id"));
2243    }
2244
2245    xs_html *head;
2246    xs_html *body;
2247
2248    if (user) {
2249        head = html_user_head(user, desc, alternate);
2250        body = html_user_body(user, read_only);
2251    }
2252    else {
2253        head = html_instance_head();
2254        body = html_instance_body();
2255    }
2256
2257    xs_html *html = xs_html_tag("html",
2258        head,
2259        body);
2260
2261    if (user && !read_only)
2262        xs_html_add(body,
2263            html_top_controls(user));
2264
2265    if (error != NULL) {
2266        xs_html_add(body,
2267            xs_html_tag("dialog",
2268                xs_html_attr("open", NULL),
2269                xs_html_tag("p",
2270                    xs_html_text(error)),
2271                xs_html_tag("form",
2272                    xs_html_attr("method", "dialog"),
2273                    xs_html_sctag("input",
2274                        xs_html_attr("type", "submit"),
2275                        xs_html_attr("value", L("Dismiss"))))));
2276    }
2277
2278    /* show links to the available lists */
2279    if (user && !read_only) {
2280        xs_html *lol = xs_html_tag("ul",
2281            xs_html_attr("class", "snac-list-of-lists"));
2282        xs_html_add(body, lol);
2283
2284        xs *lists = list_maint(user, NULL, 0); /* get list of lists */
2285
2286        int ct = 0;
2287        const char *v;
2288
2289        while (xs_list_next(lists, &v, &ct)) {
2290            const char *lname = xs_list_get(v, 1);
2291            xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
2292            xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
2293
2294            xs_html_add(lol,
2295                xs_html_tag("li",
2296                    xs_html_tag("a",
2297                        xs_html_attr("href", url),
2298                        xs_html_attr("class", "snac-list-link"),
2299                        xs_html_attr("title", ttl),
2300                        xs_html_text(lname))));
2301        }
2302
2303        {
2304            /* show the list of pinned posts */
2305            xs *url = xs_fmt("%s/pinned", user->actor);
2306            xs_html_add(lol,
2307                xs_html_tag("li",
2308                    xs_html_tag("a",
2309                        xs_html_attr("href", url),
2310                        xs_html_attr("class", "snac-list-link"),
2311                        xs_html_attr("title", L("Pinned posts")),
2312                        xs_html_text("pinned"))));
2313        }
2314
2315        {
2316            /* show the list of bookmarked posts */
2317            xs *url = xs_fmt("%s/bookmarks", user->actor);
2318            xs_html_add(lol,
2319                xs_html_tag("li",
2320                    xs_html_tag("a",
2321                        xs_html_attr("href", url),
2322                        xs_html_attr("class", "snac-list-link"),
2323                        xs_html_attr("title", L("Bookmarked posts")),
2324                        xs_html_text("bookmarks"))));
2325        }
2326
2327        {
2328            /* show the list of drafts */
2329            xs *url = xs_fmt("%s/drafts", user->actor);
2330            xs_html_add(lol,
2331                xs_html_tag("li",
2332                    xs_html_tag("a",
2333                        xs_html_attr("href", url),
2334                        xs_html_attr("class", "snac-list-link"),
2335                        xs_html_attr("title", L("Post drafts")),
2336                        xs_html_text("drafts"))));
2337        }
2338    }
2339
2340    xs_html_add(body,
2341        xs_html_tag("a",
2342            xs_html_attr("name", "snac-posts")));
2343
2344    xs_html *posts = xs_html_tag("div",
2345        xs_html_attr("class", "snac-posts"));
2346
2347    if (title) {
2348        xs_html_add(posts,
2349            xs_html_tag("h2",
2350                xs_html_attr("class", "snac-header"),
2351                xs_html_text(title)));
2352    }
2353
2354    xs_html_add(body,
2355        posts);
2356
2357    while (xs_list_iter(&p, &v)) {
2358        xs *msg = NULL;
2359        int status;
2360
2361        if (utl && user && !is_pinned_by_md5(user, v))
2362            status = timeline_get_by_md5(user, v, &msg);
2363        else
2364            status = object_get_by_md5(v, &msg);
2365
2366        if (!valid_status(status))
2367            continue;
2368
2369        /* if it's an instance page, discard messages from private users */
2370        if (user == NULL && is_msg_from_private_user(msg))
2371            continue;
2372
2373        /* is this message a non-public reply? */
2374        if (user != NULL && !is_msg_public(msg)) {
2375            const char *irt = get_in_reply_to(msg);
2376
2377            /* is it a reply to something not in the storage? */
2378            if (!xs_is_null(irt) && !object_here(irt)) {
2379                /* is it for me? */
2380                const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
2381                const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
2382
2383                if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
2384                    snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
2385                    continue;
2386                }
2387            }
2388        }
2389
2390        xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
2391
2392        if (entry != NULL)
2393            xs_html_add(posts,
2394                entry);
2395    }
2396
2397    if (list && user && read_only) {
2398        /** history **/
2399        if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
2400            xs_html *ul = xs_html_tag("ul", NULL);
2401
2402            xs_html *history = xs_html_tag("div",
2403                xs_html_attr("class", "snac-history"),
2404                xs_html_tag("p",
2405                    xs_html_attr("class", "snac-history-title"),
2406                    xs_html_text(L("History"))),
2407                    ul);
2408
2409            xs *list = history_list(user);
2410            xs_list *p = list;
2411            const char *v;
2412
2413            while (xs_list_iter(&p, &v)) {
2414                xs *fn  = xs_replace(v, ".html", "");
2415                xs *url = xs_fmt("%s/h/%s", user->actor, v);
2416
2417                xs_html_add(ul,
2418                    xs_html_tag("li",
2419                        xs_html_tag("a",
2420                            xs_html_attr("href", url),
2421                            xs_html_text(fn))));
2422            }
2423
2424            xs_html_add(body,
2425                history);
2426        }
2427    }
2428
2429    {
2430        xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
2431        xs_html_add(body,
2432            xs_html_raw(s1));
2433    }
2434
2435    if (show_more) {
2436        xs *m  = NULL;
2437        xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
2438
2439        xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
2440
2441        if (page != NULL)
2442            url = xs_str_cat(url, page);
2443
2444        if (xs_str_in(url, "?") != -1)
2445            m = xs_fmt("%s&%s", url, ss);
2446        else
2447            m = xs_fmt("%s?%s", url, ss);
2448
2449        xs_html *more_links = xs_html_tag("p",
2450            xs_html_tag("a",
2451                xs_html_attr("href", url),
2452                xs_html_attr("name", "snac-more"),
2453                xs_html_text(L("Back to top"))),
2454            xs_html_text(" - "),
2455            xs_html_tag("a",
2456                xs_html_attr("href", m),
2457                xs_html_attr("name", "snac-more"),
2458                xs_html_text(L("More..."))));
2459
2460        xs_html_add(body,
2461            more_links);
2462    }
2463
2464    xs_html_add(body,
2465        html_footer());
2466
2467    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2468}
2469
2470
2471xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, const char *proxy)
2472{
2473    xs_html *snac_posts;
2474    xs_html *people = xs_html_tag("div",
2475        xs_html_tag("h2",
2476            xs_html_attr("class", "snac-header"),
2477            xs_html_text(header)),
2478        snac_posts = xs_html_tag("details",
2479                xs_html_attr("open", NULL),
2480                xs_html_tag("summary",
2481                    xs_html_text("..."))));
2482
2483    const char *actor_id;
2484
2485    xs_list_foreach(list, actor_id) {
2486        xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
2487        xs *actor = NULL;
2488
2489        if (valid_status(actor_get(actor_id, &actor))) {
2490            xs_html *snac_post = xs_html_tag("div",
2491                xs_html_attr("class", "snac-post"),
2492                xs_html_tag("a",
2493                    xs_html_attr("name", md5)),
2494                xs_html_tag("div",
2495                    xs_html_attr("class", "snac-post-header"),
2496                    html_actor_icon(snac, actor, xs_dict_get(actor, "published"),
2497                                    NULL, NULL, 0, 1, proxy)));
2498
2499            /* content (user bio) */
2500            const char *c = xs_dict_get(actor, "summary");
2501
2502            if (!xs_is_null(c)) {
2503                xs *sc = sanitize(c);
2504
2505                xs_html *snac_content = xs_html_tag("div",
2506                    xs_html_attr("class", "snac-content"));
2507
2508                if (xs_startswith(sc, "<p>"))
2509                    xs_html_add(snac_content,
2510                        xs_html_raw(sc)); /* already sanitized */
2511                else
2512                    xs_html_add(snac_content,
2513                        xs_html_tag("p",
2514                            xs_html_raw(sc))); /* already sanitized */
2515
2516                xs_html_add(snac_post, snac_content);
2517            }
2518
2519            /* buttons */
2520            xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
2521
2522            xs_html *snac_controls = xs_html_tag("div",
2523                xs_html_attr("class",   "snac-controls"));
2524
2525            xs_html *form = xs_html_tag("form",
2526                xs_html_attr("autocomplete",    "off"),
2527                xs_html_attr("method",          "post"),
2528                xs_html_attr("action",          btn_form_action),
2529                xs_html_sctag("input",
2530                    xs_html_attr("type",    "hidden"),
2531                    xs_html_attr("name",    "actor"),
2532                    xs_html_attr("value",   actor_id)),
2533                xs_html_sctag("input",
2534                    xs_html_attr("type",    "hidden"),
2535                    xs_html_attr("name",    "actor-form"),
2536                    xs_html_attr("value",   "yes")));
2537
2538            xs_html_add(snac_controls, form);
2539
2540            if (following_check(snac, actor_id)) {
2541                xs_html_add(form,
2542                    html_button("unfollow", L("Unfollow"),
2543                                L("Stop following this user's activity")));
2544
2545                if (is_limited(snac, actor_id))
2546                    xs_html_add(form,
2547                        html_button("unlimit", L("Unlimit"),
2548                                L("Allow announces (boosts) from this user")));
2549                else
2550                    xs_html_add(form,
2551                        html_button("limit", L("Limit"),
2552                                L("Block announces (boosts) from this user")));
2553            }
2554            else
2555            if (pending_check(snac, actor_id)) {
2556                xs_html_add(form,
2557                    html_button("approve", L("Approve"),
2558                                L("Approve this follow request")));
2559
2560                xs_html_add(form,
2561                    html_button("discard", L("Discard"), L("Discard this follow request")));
2562            }
2563            else {
2564                xs_html_add(form,
2565                    html_button("follow", L("Follow"),
2566                                L("Start following this user's activity")));
2567
2568                if (follower_check(snac, actor_id))
2569                    xs_html_add(form,
2570                        html_button("delete", L("Delete"), L("Delete this user")));
2571            }
2572
2573            if (is_muted(snac, actor_id))
2574                xs_html_add(form,
2575                    html_button("unmute", L("Unmute"),
2576                                L("Stop blocking activities from this user")));
2577            else
2578                xs_html_add(form,
2579                    html_button("mute", L("MUTE"),
2580                                L("Block any activity from this user")));
2581
2582            /* the post textarea */
2583            xs *dm_div_id  = xs_fmt("%s_%s_dm", md5, t);
2584            xs *dm_form_id = xs_fmt("%s_reply_form", md5);
2585
2586            xs_html_add(snac_controls,
2587                xs_html_tag("p", NULL),
2588                html_note(snac, L("Direct Message..."),
2589                    dm_div_id, dm_form_id,
2590                    "", "",
2591                    NULL, actor_id,
2592                    xs_stock(XSTYPE_FALSE), "",
2593                    xs_stock(XSTYPE_FALSE), NULL,
2594                    NULL, 0, "", "", 0),
2595                xs_html_tag("p", NULL));
2596
2597            xs_html_add(snac_post, snac_controls);
2598
2599            xs_html_add(snac_posts, snac_post);
2600        }
2601    }
2602
2603    return people;
2604}
2605
2606
2607xs_str *html_people(snac *user)
2608{
2609    const char *proxy = NULL;
2610
2611    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
2612        proxy = user->actor;
2613
2614    xs *wing = following_list(user);
2615    xs *wers = follower_list(user);
2616
2617    xs_html *lists = xs_html_tag("div",
2618        xs_html_attr("class", "snac-posts"));
2619
2620    if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
2621        xs *pending = pending_list(user);
2622        xs_html_add(lists,
2623            html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
2624    }
2625
2626    xs_html_add(lists,
2627        html_people_list(user, wing, L("People you follow"), "i", proxy),
2628        html_people_list(user, wers, L("People that follow you"), "e", proxy));
2629
2630    xs_html *html = xs_html_tag("html",
2631        html_user_head(user, NULL, NULL),
2632        xs_html_add(html_user_body(user, 0),
2633            lists,
2634            html_footer()));
2635
2636    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2637}
2638
2639
2640xs_str *html_notifications(snac *user, int skip, int show)
2641{
2642    const char *proxy = NULL;
2643
2644    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
2645        proxy = user->actor;
2646
2647    xs *n_list = notify_list(user, skip, show);
2648    xs *n_time = notify_check_time(user, 0);
2649
2650    xs_html *body = html_user_body(user, 0);
2651
2652    xs_html *html = xs_html_tag("html",
2653        html_user_head(user, NULL, NULL),
2654        body);
2655
2656    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
2657
2658    xs_html_add(body,
2659        xs_html_tag("form",
2660            xs_html_attr("autocomplete", "off"),
2661            xs_html_attr("method",       "post"),
2662            xs_html_attr("action",       clear_all_action),
2663            xs_html_attr("id",           "clear"),
2664            xs_html_sctag("input",
2665                xs_html_attr("type",     "submit"),
2666                xs_html_attr("class",    "snac-btn-like"),
2667                xs_html_attr("value",    L("Clear all")))));
2668
2669    xs_html *noti_new = NULL;
2670    xs_html *noti_seen = NULL;
2671
2672    xs_html *posts = xs_html_tag("div",
2673        xs_html_attr("class", "snac-posts"));
2674    xs_html_add(body, posts);
2675
2676    xs_list *p = n_list;
2677    const xs_str *v;
2678    while (xs_list_iter(&p, &v)) {
2679        xs *noti = notify_get(user, v);
2680
2681        if (noti == NULL)
2682            continue;
2683
2684        xs *obj = NULL;
2685        const char *type  = xs_dict_get(noti, "type");
2686        const char *utype = xs_dict_get(noti, "utype");
2687        const char *id    = xs_dict_get(noti, "objid");
2688        const char *date  = xs_dict_get(noti, "date");
2689        xs *wrk = NULL;
2690
2691        if (xs_is_null(id))
2692            continue;
2693
2694        if (is_hidden(user, id))
2695            continue;
2696
2697        object_get(id, &obj);
2698
2699        const char *actor_id = xs_dict_get(noti, "actor");
2700        xs *actor = NULL;
2701
2702        if (!valid_status(actor_get(actor_id, &actor)))
2703            continue;
2704
2705        xs *a_name = actor_name(actor, proxy);
2706        const char *label = type;
2707
2708        if (strcmp(type, "Create") == 0)
2709            label = L("Mention");
2710        else
2711        if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
2712            label = L("Finished poll");
2713        else
2714        if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
2715            label = L("Unfollow");
2716        else
2717        if (strcmp(type, "EmojiReact") == 0) {
2718            const char *content = xs_dict_get_path(noti, "msg.content");
2719
2720            if (xs_type(content) == XSTYPE_STRING) {
2721                wrk = xs_fmt("%s (%s)", type, content);
2722                label = wrk;
2723            }
2724        }
2725        else
2726        if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
2727            label = L("Follow Request");
2728
2729        xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
2730
2731        xs_html *entry = xs_html_tag("div",
2732            xs_html_attr("class", "snac-post-with-desc"),
2733            xs_html_tag("p",
2734                xs_html_tag("b",
2735                    xs_html_text(label),
2736                    xs_html_text(" by "),
2737                    xs_html_tag("a",
2738                        xs_html_attr("href", actor_id),
2739                        xs_html_raw(a_name))), /* a_name is already sanitized */
2740                xs_html_text(" "),
2741                xs_html_tag("time",
2742                    xs_html_attr("class", "dt-published snac-pubdate"),
2743                    xs_html_attr("title", date),
2744                    xs_html_text(s_date))));
2745
2746        if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0 || strcmp(type, "Block") == 0) {
2747            xs_html_add(entry,
2748                xs_html_tag("div",
2749                    xs_html_attr("class", "snac-post"),
2750                    html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0, proxy)));
2751        }
2752        else
2753        if (strcmp(type, "Move") == 0) {
2754            const xs_dict *o_msg = xs_dict_get(noti, "msg");
2755            const char *target;
2756
2757            if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
2758                xs *old_actor = NULL;
2759
2760                if (valid_status(actor_get(target, &old_actor))) {
2761                    xs_html_add(entry,
2762                        xs_html_tag("div",
2763                            xs_html_attr("class", "snac-post"),
2764                            html_actor_icon(user, old_actor, NULL, NULL, NULL, 0, 0, proxy)));
2765                }
2766            }
2767        }
2768        else
2769        if (obj != NULL) {
2770            xs *md5 = xs_md5_hex(id, strlen(id));
2771
2772            xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
2773
2774            if (h != NULL) {
2775                xs_html_add(entry,
2776                    h);
2777            }
2778        }
2779
2780        if (strcmp(v, n_time) > 0) {
2781            /* unseen notification */
2782            if (noti_new == NULL) {
2783                noti_new = xs_html_tag("div",
2784                    xs_html_tag("h2",
2785                        xs_html_attr("class", "snac-header"),
2786                        xs_html_text(L("New"))));
2787
2788                xs_html_add(posts,
2789                    noti_new);
2790            }
2791
2792            xs_html_add(noti_new,
2793                entry);
2794        }
2795        else {
2796            /* already seen notification */
2797            if (noti_seen == NULL) {
2798                noti_seen = xs_html_tag("div",
2799                    xs_html_tag("h2",
2800                        xs_html_attr("class", "snac-header"),
2801                        xs_html_text(L("Already seen"))));
2802
2803                xs_html_add(posts,
2804                    noti_seen);
2805            }
2806
2807            xs_html_add(noti_seen,
2808                entry);
2809        }
2810    }
2811
2812    if (noti_new == NULL && noti_seen == NULL)
2813        xs_html_add(body,
2814            xs_html_tag("h2",
2815                xs_html_attr("class", "snac-header"),
2816                xs_html_text(L("None"))));
2817
2818    /* add the navigation footer */
2819    xs *next_p = notify_list(user, skip + show, 1);
2820    if (xs_list_len(next_p)) {
2821        xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
2822            user->actor, skip + show, show);
2823
2824        xs_html_add(body,
2825            xs_html_tag("p",
2826                xs_html_tag("a",
2827                    xs_html_attr("href", url),
2828                    xs_html_text(L("More...")))));
2829    }
2830
2831    xs_html_add(body,
2832        html_footer());
2833
2834    /* set the check time to now */
2835    xs *dummy = notify_check_time(user, 1);
2836    dummy = xs_free(dummy);
2837
2838    timeline_touch(user);
2839
2840    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2841}
2842
2843
2844int html_get_handler(const xs_dict *req, const char *q_path,
2845                     char **body, int *b_size, char **ctype,
2846                     xs_str **etag, xs_str **last_modified)
2847{
2848    const char *accept = xs_dict_get(req, "accept");
2849    int status = HTTP_STATUS_NOT_FOUND;
2850    snac snac;
2851    xs *uid = NULL;
2852    const char *p_path;
2853    int cache = 1;
2854    int save = 1;
2855    int proxy = 0;
2856    const char *v;
2857
2858    xs *l = xs_split_n(q_path, "/", 2);
2859    v = xs_list_get(l, 1);
2860
2861    if (xs_is_null(v)) {
2862        srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
2863        return HTTP_STATUS_NOT_FOUND;
2864    }
2865
2866    uid = xs_dup(v);
2867
2868    /* rss extension? */
2869    if (xs_endswith(uid, ".rss")) {
2870        uid = xs_crop_i(uid, 0, -4);
2871        p_path = ".rss";
2872    }
2873    else
2874        p_path = xs_list_get(l, 2);
2875
2876    if (!uid || !user_open(&snac, uid)) {
2877        /* invalid user */
2878        srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
2879        return HTTP_STATUS_NOT_FOUND;
2880    }
2881
2882    if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
2883        proxy = 1;
2884
2885    /* return the RSS if requested by Accept header */
2886    if (accept != NULL) {
2887        if (xs_str_in(accept, "text/xml") != -1 ||
2888            xs_str_in(accept, "application/rss+xml") != -1)
2889            p_path = ".rss";
2890    }
2891
2892    /* check if server config variable 'disable_cache' is set */
2893    if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
2894        cache = 0;
2895
2896    int skip = 0;
2897    int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
2898    int show = def_show;
2899
2900    const xs_dict *q_vars = xs_dict_get(req, "q_vars");
2901    if ((v = xs_dict_get(q_vars, "skip")) != NULL)
2902        skip = atoi(v), cache = 0, save = 0;
2903    if ((v = xs_dict_get(q_vars, "show")) != NULL)
2904        show = atoi(v), cache = 0, save = 0;
2905    if ((v = xs_dict_get(q_vars, "da")) != NULL) {
2906        /* user dismissed an announcement */
2907        if (login(&snac, req)) {
2908            double ts = atof(v);
2909            xs *timestamp = xs_number_new(ts);
2910            srv_log(xs_fmt("user dismissed announcements until %d", ts));
2911            snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
2912            user_persist(&snac, 0);
2913        }
2914    }
2915
2916    /* get a possible error message */
2917    const char *error = xs_dict_get(q_vars, "error");
2918    if (error != NULL)
2919        cache = 0;
2920
2921    /* a show of 0 has no sense */
2922    if (show == 0)
2923        show = def_show;
2924
2925    if (p_path == NULL) { /** public timeline **/
2926        xs *h = xs_str_localtime(0, "%Y-%m.html");
2927
2928        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
2929            /** empty public timeline for private users **/
2930            *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error);
2931            *b_size = strlen(*body);
2932            status  = HTTP_STATUS_OK;
2933        }
2934        else
2935        if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
2936            snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
2937
2938            status = history_get(&snac, h, body, b_size,
2939                        xs_dict_get(req, "if-none-match"), etag);
2940        }
2941        else {
2942            xs *list = NULL;
2943            xs *next = NULL;
2944
2945            if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines"))) {
2946                list = timeline_simple_list(&snac, "public", skip, show);
2947                next = timeline_simple_list(&snac, "public", skip + show, 1);
2948            }
2949            else {
2950                list = timeline_list(&snac, "public", skip, show);
2951                next = timeline_list(&snac, "public", skip + show, 1);
2952            }
2953
2954            xs *pins = pinned_list(&snac);
2955            pins = xs_list_cat(pins, list);
2956
2957            *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1, error);
2958
2959            *b_size = strlen(*body);
2960            status  = HTTP_STATUS_OK;
2961
2962            if (save)
2963                history_add(&snac, h, *body, *b_size, etag);
2964        }
2965    }
2966    else
2967    if (strcmp(p_path, "admin") == 0) { /** private timeline **/
2968        if (!login(&snac, req)) {
2969            *body  = xs_dup(uid);
2970            status = HTTP_STATUS_UNAUTHORIZED;
2971        }
2972        else {
2973            const char *q = xs_dict_get(q_vars, "q");
2974
2975            if (q && *q) {
2976                if (xs_regex_match(q, "^@?[a-zA-Z0-9_]+@[a-zA-Z0-9-]+\\.")) {
2977                    /** search account **/
2978                    xs *actor = NULL;
2979                    xs *acct = NULL;
2980                    xs *l = xs_list_new();
2981                    xs_html *page = NULL;
2982
2983                    if (valid_status(webfinger_request(q, &actor, &acct))) {
2984                        xs *actor_obj = NULL;
2985
2986                        if (valid_status(actor_request(&snac, actor, &actor_obj))) {
2987                            actor_add(actor, actor_obj);
2988
2989                            /* create a people list with only one element */
2990                            l = xs_list_append(xs_list_new(), actor);
2991
2992                            xs *title = xs_fmt(L("Search results for account %s"), q);
2993
2994                            page = html_people_list(&snac, l, title, "wf", NULL);
2995                        }
2996                    }
2997
2998                    if (page == NULL) {
2999                        xs *title = xs_fmt(L("Account %s not found"), q);
3000
3001                        page = xs_html_tag("div",
3002                            xs_html_tag("h2",
3003                                xs_html_attr("class", "snac-header"),
3004                                xs_html_text(title)));
3005                    }
3006
3007                    xs_html *html = xs_html_tag("html",
3008                        html_user_head(&snac, NULL, NULL),
3009                        xs_html_add(html_user_body(&snac, 0),
3010                        page,
3011                        html_footer()));
3012
3013                    *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
3014                    *b_size = strlen(*body);
3015                    status = HTTP_STATUS_OK;
3016                }
3017                else
3018                if (*q == '#') {
3019                    /** search by tag **/
3020                    xs *tl = tag_search(q, skip, show + 1);
3021                    int more = 0;
3022                    if (xs_list_len(tl) >= show + 1) {
3023                        /* drop the last one */
3024                        tl = xs_list_del(tl, -1);
3025                        more = 1;
3026                    }
3027
3028                    xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
3029                    xs *title = xs_fmt(xs_list_len(tl) ?
3030                        L("Search results for tag %s") : L("Nothing found for tag %s"), q);
3031
3032                    *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error);
3033                    *b_size = strlen(*body);
3034                    status  = HTTP_STATUS_OK;
3035                }
3036                else {
3037                    /** search by content **/
3038                    int to = 0;
3039                    int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
3040                    xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
3041                    xs *title = NULL;
3042                    xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
3043                    int tl_len = xs_list_len(tl);
3044
3045                    if (to)
3046                        title = xs_fmt(L("Search results for '%s' (may be more)"), q);
3047                    else
3048                    if (tl_len)
3049                        title = xs_fmt(L("Search results for '%s'"), q);
3050                    else
3051                    if (skip)
3052                        title = xs_fmt(L("No more matches for '%s'"), q);
3053                    else
3054                        title = xs_fmt(L("Nothing found for '%s'"), q);
3055
3056                    *body   = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
3057                                            title, page, 0, error);
3058                    *b_size = strlen(*body);
3059                    status  = HTTP_STATUS_OK;
3060                }
3061            }
3062            else {
3063                double t = history_mtime(&snac, "timeline.html_");
3064
3065                /* if enabled by admin, return a cached page if its timestamp is:
3066                   a) newer than the timeline timestamp
3067                   b) newer than the start time of the server
3068                */
3069                if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
3070                    snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
3071
3072                    status = history_get(&snac, "timeline.html_", body, b_size,
3073                                xs_dict_get(req, "if-none-match"), etag);
3074                }
3075                else {
3076                    snac_debug(&snac, 1, xs_fmt("building timeline"));
3077
3078                    xs *list = timeline_list(&snac, "private", skip, show);
3079                    xs *next = timeline_list(&snac, "private", skip + show, 1);
3080
3081                    *body = html_timeline(&snac, list, 0, skip, show,
3082                            xs_list_len(next), NULL, "/admin", 1, error);
3083
3084                    *b_size = strlen(*body);
3085                    status  = HTTP_STATUS_OK;
3086
3087                    if (save)
3088                        history_add(&snac, "timeline.html_", *body, *b_size, etag);
3089                }
3090            }
3091        }
3092    }
3093    else
3094    if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
3095        if (!login(&snac, req)) {
3096            *body  = xs_dup(uid);
3097            status = HTTP_STATUS_UNAUTHORIZED;
3098        }
3099        else {
3100            xs *l = xs_split(p_path, "/");
3101            const char *md5 = xs_list_get(l, -1);
3102
3103            if (md5 && *md5 && timeline_here(&snac, md5)) {
3104                xs *list = xs_list_append(xs_list_new(), md5);
3105
3106                *body   = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error);
3107                *b_size = strlen(*body);
3108                status  = HTTP_STATUS_OK;
3109            }
3110        }
3111    }
3112    else
3113    if (strcmp(p_path, "people") == 0) { /** the list of people **/
3114        if (!login(&snac, req)) {
3115            *body  = xs_dup(uid);
3116            status = HTTP_STATUS_UNAUTHORIZED;
3117        }
3118        else {
3119            *body   = html_people(&snac);
3120            *b_size = strlen(*body);
3121            status  = HTTP_STATUS_OK;
3122        }
3123    }
3124    else
3125    if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
3126        if (!login(&snac, req)) {
3127            *body  = xs_dup(uid);
3128            status = HTTP_STATUS_UNAUTHORIZED;
3129        }
3130        else {
3131            *body   = html_notifications(&snac, skip, show);
3132            *b_size = strlen(*body);
3133            status  = HTTP_STATUS_OK;
3134        }
3135    }
3136    else
3137    if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
3138        if (!login(&snac, req)) {
3139            *body  = xs_dup(uid);
3140            status = HTTP_STATUS_UNAUTHORIZED;
3141        }
3142        else {
3143            xs *list = timeline_instance_list(skip, show);
3144            xs *next = timeline_instance_list(skip + show, 1);
3145
3146            *body = html_timeline(&snac, list, 0, skip, show,
3147                xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error);
3148            *b_size = strlen(*body);
3149            status  = HTTP_STATUS_OK;
3150        }
3151    }
3152    else
3153    if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
3154        if (!login(&snac, req)) {
3155            *body  = xs_dup(uid);
3156            status = HTTP_STATUS_UNAUTHORIZED;
3157        }
3158        else {
3159            xs *list = pinned_list(&snac);
3160
3161            *body = html_timeline(&snac, list, 0, skip, show,
3162                0, L("Pinned posts"), "", 0, error);
3163            *b_size = strlen(*body);
3164            status  = HTTP_STATUS_OK;
3165        }
3166    }
3167    else
3168    if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
3169        if (!login(&snac, req)) {
3170            *body  = xs_dup(uid);
3171            status = HTTP_STATUS_UNAUTHORIZED;
3172        }
3173        else {
3174            xs *list = bookmark_list(&snac);
3175
3176            *body = html_timeline(&snac, list, 0, skip, show,
3177                0, L("Bookmarked posts"), "", 0, error);
3178            *b_size = strlen(*body);
3179            status  = HTTP_STATUS_OK;
3180        }
3181    }
3182    else
3183    if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
3184        if (!login(&snac, req)) {
3185            *body  = xs_dup(uid);
3186            status = HTTP_STATUS_UNAUTHORIZED;
3187        }
3188        else {
3189            xs *list = draft_list(&snac);
3190
3191            *body = html_timeline(&snac, list, 0, skip, show,
3192                0, L("Post drafts"), "", 0, error);
3193            *b_size = strlen(*body);
3194            status  = HTTP_STATUS_OK;
3195        }
3196    }
3197    else
3198    if (xs_startswith(p_path, "list/")) { /** list timelines **/
3199        if (!login(&snac, req)) {
3200            *body  = xs_dup(uid);
3201            status = HTTP_STATUS_UNAUTHORIZED;
3202        }
3203        else {
3204            xs *l = xs_split(p_path, "/");
3205            const char *lid = xs_list_get(l, -1);
3206
3207            xs *list = list_timeline(&snac, lid, skip, show);
3208            xs *next = list_timeline(&snac, lid, skip + show, 1);
3209
3210            if (list != NULL) {
3211                xs *ttl = timeline_top_level(&snac, list);
3212
3213                xs *base = xs_fmt("/list/%s", lid);
3214                xs *name = list_maint(&snac, lid, 3);
3215                xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
3216
3217                *body = html_timeline(&snac, ttl, 0, skip, show,
3218                    xs_list_len(next), title, base, 1, error);
3219                *b_size = strlen(*body);
3220                status  = HTTP_STATUS_OK;
3221            }
3222        }
3223    }
3224    else
3225    if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
3226        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3227            return HTTP_STATUS_FORBIDDEN;
3228
3229        xs *id  = xs_fmt("%s/%s", snac.actor, p_path);
3230        xs *msg = NULL;
3231
3232        if (valid_status(object_get(id, &msg))) {
3233            xs *md5  = xs_md5_hex(id, strlen(id));
3234            xs *list = xs_list_new();
3235
3236            list = xs_list_append(list, md5);
3237
3238            *body   = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error);
3239            *b_size = strlen(*body);
3240            status  = HTTP_STATUS_OK;
3241        }
3242    }
3243    else
3244    if (xs_startswith(p_path, "s/")) { /** a static file **/
3245        xs *l    = xs_split(p_path, "/");
3246        const char *id = xs_list_get(l, 1);
3247        int sz;
3248
3249        if (id && *id) {
3250            status = static_get(&snac, id, body, &sz,
3251                        xs_dict_get(req, "if-none-match"), etag);
3252
3253            if (valid_status(status)) {
3254                *b_size = sz;
3255                *ctype  = (char *)xs_mime_by_ext(id);
3256            }
3257        }
3258    }
3259    else
3260    if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
3261        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3262            return HTTP_STATUS_FORBIDDEN;
3263
3264        if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
3265            return HTTP_STATUS_FORBIDDEN;
3266
3267        xs *l = xs_split(p_path, "/");
3268        const char *id = xs_list_get(l, 1);
3269
3270        if (id && *id) {
3271            if (xs_endswith(id, "timeline.html_")) {
3272                /* Don't let them in */
3273                *b_size = 0;
3274                status = HTTP_STATUS_NOT_FOUND;
3275            }
3276            else
3277                status = history_get(&snac, id, body, b_size,
3278                            xs_dict_get(req, "if-none-match"), etag);
3279        }
3280    }
3281    else
3282    if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
3283        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
3284            return HTTP_STATUS_FORBIDDEN;
3285
3286        xs *elems = timeline_simple_list(&snac, "public", 0, 20);
3287        xs *bio   = xs_dup(xs_dict_get(snac.config, "bio"));
3288
3289        xs *rss_title = xs_fmt("%s (@%s@%s)",
3290            xs_dict_get(snac.config, "name"),
3291            snac.uid,
3292            xs_dict_get(srv_config, "host"));
3293        xs *rss_link = xs_fmt("%s.rss", snac.actor);
3294
3295        *body   = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
3296        *b_size = strlen(*body);
3297        *ctype  = "application/rss+xml; charset=utf-8";
3298        status  = HTTP_STATUS_OK;
3299
3300        snac_debug(&snac, 1, xs_fmt("serving RSS"));
3301    }
3302    else
3303    if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
3304        xs *proxy_prefix = NULL;
3305
3306        if (xs_startswith(p_path, "x/")) {
3307            /* proxy usage authorized by http basic auth */
3308            if (login(&snac, req))
3309                proxy_prefix = xs_str_new("x/");
3310            else {
3311                *body  = xs_dup(uid);
3312                status = HTTP_STATUS_UNAUTHORIZED;
3313            }
3314        }
3315        else {
3316            /* proxy usage authorized by proxy_token */
3317            xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
3318            xs *tk = xs_md5_hex(tks, strlen(tks));
3319            xs *p = xs_fmt("y/%s/", tk);
3320
3321            if (xs_startswith(p_path, p))
3322                proxy_prefix = xs_dup(p);
3323        }
3324
3325        if (proxy_prefix) {
3326            /* pick the raw path (including optional ? arguments) */
3327            const char *raw_path = xs_dict_get(req, "raw_path");
3328
3329            /* skip to where the proxy/ string starts */
3330            raw_path += xs_str_in(raw_path, proxy_prefix);
3331
3332            xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
3333            xs *hdrs = xs_dict_new();
3334
3335            hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
3336
3337            const char *ims = xs_dict_get(req, "if-modified-since");
3338            const char *inm = xs_dict_get(req, "if-none-match");
3339
3340            if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
3341            if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
3342
3343            xs *rsp = xs_http_request("GET", url, hdrs,
3344                        NULL, 0, &status, body, b_size, 0);
3345
3346            if (valid_status(status)) {
3347                const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
3348                const char *lm = xs_dict_get(rsp, "last-modified");
3349                const char *et = xs_dict_get(rsp, "etag");
3350
3351                if (lm) *last_modified = xs_dup(lm);
3352                if (et) *etag = xs_dup(et);
3353
3354                /* find the content-type in the static mime types,
3355                   and return that value instead of ct, which will
3356                   be destroyed when out of scope */
3357                for (int n = 0; xs_mime_types[n]; n += 2) {
3358                    if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
3359                        *ctype = (char *)xs_mime_types[n + 1];
3360                        break;
3361                    }
3362                }
3363            }
3364
3365            snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
3366        }
3367    }
3368    else
3369        status = HTTP_STATUS_NOT_FOUND;
3370
3371    user_free(&snac);
3372
3373    if (valid_status(status) && *ctype == NULL) {
3374        *ctype = "text/html; charset=utf-8";
3375    }
3376
3377    return status;
3378}
3379
3380
3381int html_post_handler(const xs_dict *req, const char *q_path,
3382                      char *payload, int p_size,
3383                      char **body, int *b_size, char **ctype)
3384{
3385    (void)p_size;
3386    (void)ctype;
3387
3388    int status = 0;
3389    snac snac;
3390    const char *uid;
3391    const char *p_path;
3392    const xs_dict *p_vars;
3393
3394    xs *l = xs_split_n(q_path, "/", 2);
3395
3396    uid = xs_list_get(l, 1);
3397    if (!uid || !user_open(&snac, uid)) {
3398        /* invalid user */
3399        srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
3400        return HTTP_STATUS_NOT_FOUND;
3401    }
3402
3403    p_path = xs_list_get(l, 2);
3404
3405    /* all posts must be authenticated */
3406    if (!login(&snac, req)) {
3407        user_free(&snac);
3408        *body  = xs_dup(uid);
3409        return HTTP_STATUS_UNAUTHORIZED;
3410    }
3411
3412    p_vars = xs_dict_get(req, "p_vars");
3413
3414    if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
3415        snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
3416
3417        /* post note */
3418        const xs_str *content      = xs_dict_get(p_vars, "content");
3419        const xs_str *in_reply_to  = xs_dict_get(p_vars, "in_reply_to");
3420        const xs_str *attach_url   = xs_dict_get(p_vars, "attach_url");
3421        const xs_list *attach_file = xs_dict_get(p_vars, "attach");
3422        const xs_str *to           = xs_dict_get(p_vars, "to");
3423        const xs_str *sensitive    = xs_dict_get(p_vars, "sensitive");
3424        const xs_str *summary      = xs_dict_get(p_vars, "summary");
3425        const xs_str *edit_id      = xs_dict_get(p_vars, "edit_id");
3426        const xs_str *alt_text     = xs_dict_get(p_vars, "alt_text");
3427        int priv             = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
3428        int store_as_draft   = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
3429        xs *attach_list      = xs_list_new();
3430
3431        /* default alt text */
3432        if (xs_is_null(alt_text))
3433            alt_text = "";
3434
3435        /* is attach_url set? */
3436        if (!xs_is_null(attach_url) && *attach_url != '\0') {
3437            xs *l = xs_list_new();
3438
3439            l = xs_list_append(l, attach_url);
3440            l = xs_list_append(l, alt_text);
3441
3442            attach_list = xs_list_append(attach_list, l);
3443        }
3444
3445        /* is attach_file set? */
3446        if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
3447            const char *fn = xs_list_get(attach_file, 0);
3448
3449            if (*fn != '\0') {
3450                char *ext = strrchr(fn, '.');
3451                xs *hash  = xs_md5_hex(fn, strlen(fn));
3452                xs *id    = xs_fmt("%s%s", hash, ext);
3453                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
3454                int fo    = xs_number_get(xs_list_get(attach_file, 1));
3455                int fs    = xs_number_get(xs_list_get(attach_file, 2));
3456
3457                /* store */
3458                static_put(&snac, id, payload + fo, fs);
3459
3460                xs *l = xs_list_new();
3461
3462                l = xs_list_append(l, url);
3463                l = xs_list_append(l, alt_text);
3464
3465                attach_list = xs_list_append(attach_list, l);
3466            }
3467        }
3468
3469        if (content != NULL) {
3470            xs *msg       = NULL;
3471            xs *c_msg     = NULL;
3472            xs *content_2 = xs_replace(content, "\r", "");
3473            xs *poll_opts = NULL;
3474
3475            /* is there a valid set of poll options? */
3476            const char *v = xs_dict_get(p_vars, "poll_options");
3477            if (!xs_is_null(v) && *v) {
3478                xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
3479
3480                poll_opts = xs_split(v2, "\n");
3481            }
3482
3483            if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
3484                /* get the rest of poll configuration */
3485                const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
3486                const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
3487                int multiple = 0;
3488
3489                int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
3490
3491                if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
3492                    multiple = 1;
3493
3494                msg = msg_question(&snac, content_2, attach_list,
3495                                   poll_opts, multiple, end_secs);
3496
3497                enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
3498            }
3499            else
3500                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
3501
3502            if (sensitive != NULL) {
3503                msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
3504                msg = xs_dict_set(msg, "summary",   xs_is_null(summary) ? "..." : summary);
3505            }
3506
3507            if (xs_is_null(edit_id)) {
3508                /* new message */
3509                const char *id = xs_dict_get(msg, "id");
3510
3511                if (store_as_draft) {
3512                    draft_add(&snac, id, msg);
3513                }
3514                else {
3515                    c_msg = msg_create(&snac, msg);
3516                    timeline_add(&snac, id, msg);
3517                }
3518            }
3519            else {
3520                /* an edition of a previous message */
3521                xs *p_msg = NULL;
3522
3523                if (valid_status(object_get(edit_id, &p_msg))) {
3524                    /* copy relevant fields from previous version */
3525                    char *fields[] = { "id", "context", "url", "published",
3526                                       "to", "inReplyTo", NULL };
3527                    int n;
3528
3529                    for (n = 0; fields[n]; n++) {
3530                        const char *v = xs_dict_get(p_msg, fields[n]);
3531                        msg = xs_dict_set(msg, fields[n], v);
3532                    }
3533
3534                    if (store_as_draft) {
3535                        draft_add(&snac, edit_id, msg);
3536                    }
3537                    else
3538                    if (is_draft(&snac, edit_id)) {
3539                        /* message was previously a draft; it's a create activity */
3540
3541                        /* set the published field to now */
3542                        xs *published = xs_str_utctime(0, ISO_DATE_SPEC);
3543                        msg = xs_dict_set(msg, "published", published);
3544
3545                        /* overwrite object */
3546                        object_add_ow(edit_id, msg);
3547
3548                        c_msg = msg_create(&snac, msg);
3549                        timeline_add(&snac, edit_id, msg);
3550                        draft_del(&snac, edit_id);
3551                    }
3552                    else {
3553                        /* set the updated field */
3554                        xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
3555                        msg = xs_dict_set(msg, "updated", updated);
3556
3557                        /* overwrite object, not updating the indexes */
3558                        object_add_ow(edit_id, msg);
3559
3560                        /* update message */
3561                        c_msg = msg_update(&snac, msg);
3562                    }
3563                }
3564                else
3565                    snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
3566            }
3567
3568            if (c_msg != NULL)
3569                enqueue_message(&snac, c_msg);
3570
3571            history_del(&snac, "timeline.html_");
3572        }
3573
3574        status = HTTP_STATUS_SEE_OTHER;
3575    }
3576    else
3577    if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
3578        /* action on an entry */
3579        const char *id     = xs_dict_get(p_vars, "id");
3580        const char *actor  = xs_dict_get(p_vars, "actor");
3581        const char *action = xs_dict_get(p_vars, "action");
3582        const char *group  = xs_dict_get(p_vars, "group");
3583
3584        if (action == NULL)
3585            return HTTP_STATUS_NOT_FOUND;
3586
3587        snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
3588
3589        status = HTTP_STATUS_SEE_OTHER;
3590
3591        if (strcmp(action, L("Like")) == 0) { /** **/
3592            xs *msg = msg_admiration(&snac, id, "Like");
3593
3594            if (msg != NULL) {
3595                enqueue_message(&snac, msg);
3596                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
3597            }
3598        }
3599        else
3600        if (strcmp(action, L("Boost")) == 0) { /** **/
3601            xs *msg = msg_admiration(&snac, id, "Announce");
3602
3603            if (msg != NULL) {
3604                enqueue_message(&snac, msg);
3605                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
3606            }
3607        }
3608        else
3609        if (strcmp(action, L("Unlike")) == 0) { /** **/
3610            xs *msg = msg_repulsion(&snac, id, "Like");
3611
3612            if (msg != NULL) {
3613                enqueue_message(&snac, msg);
3614            }
3615        }
3616        else
3617        if (strcmp(action, L("Unboost")) == 0) { /** **/
3618            xs *msg = msg_repulsion(&snac, id, "Announce");
3619
3620            if (msg != NULL) {
3621                enqueue_message(&snac, msg);
3622            }
3623        }
3624        else
3625        if (strcmp(action, L("MUTE")) == 0) { /** **/
3626            mute(&snac, actor);
3627        }
3628        else
3629        if (strcmp(action, L("Unmute")) == 0) { /** **/
3630            unmute(&snac, actor);
3631        }
3632        else
3633        if (strcmp(action, L("Hide")) == 0) { /** **/
3634            if (is_draft(&snac, id))
3635                draft_del(&snac, id);
3636            else
3637                hide(&snac, id);
3638        }
3639        else
3640        if (strcmp(action, L("Limit")) == 0) { /** **/
3641            limit(&snac, actor);
3642        }
3643        else
3644        if (strcmp(action, L("Unlimit")) == 0) { /** **/
3645            unlimit(&snac, actor);
3646        }
3647        else
3648        if (strcmp(action, L("Follow")) == 0) { /** **/
3649            xs *msg = msg_follow(&snac, actor);
3650
3651            if (msg != NULL) {
3652                /* reload the actor from the message, in may be different */
3653                actor = xs_dict_get(msg, "object");
3654
3655                following_add(&snac, actor, msg);
3656
3657                enqueue_output_by_actor(&snac, msg, actor, 0);
3658            }
3659        }
3660        else
3661        if (strcmp(action, L("Unfollow")) == 0) { /** **/
3662            /* get the following object */
3663            xs *object = NULL;
3664
3665            if (valid_status(following_get(&snac, actor, &object))) {
3666                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3667
3668                following_del(&snac, actor);
3669
3670                enqueue_output_by_actor(&snac, msg, actor, 0);
3671
3672                snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
3673            }
3674            else
3675                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3676        }
3677        else
3678        if (strcmp(action, L("Follow Group")) == 0) { /** **/
3679            xs *msg = msg_follow(&snac, group);
3680
3681            if (msg != NULL) {
3682                /* reload the group from the message, in may be different */
3683                group = xs_dict_get(msg, "object");
3684
3685                following_add(&snac, group, msg);
3686
3687                enqueue_output_by_actor(&snac, msg, group, 0);
3688            }
3689        }
3690        else
3691        if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
3692            /* get the following object */
3693            xs *object = NULL;
3694
3695            if (valid_status(following_get(&snac, group, &object))) {
3696                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3697
3698                following_del(&snac, group);
3699
3700                enqueue_output_by_actor(&snac, msg, group, 0);
3701
3702                snac_log(&snac, xs_fmt("unfollowed group %s", group));
3703            }
3704            else
3705                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3706        }
3707        else
3708        if (strcmp(action, L("Delete")) == 0) { /** **/
3709            const char *actor_form = xs_dict_get(p_vars, "actor-form");
3710            if (actor_form != NULL) {
3711                /* delete follower */
3712                if (valid_status(follower_del(&snac, actor)))
3713                    snac_log(&snac, xs_fmt("deleted follower %s", actor));
3714                else
3715                    snac_log(&snac, xs_fmt("error deleting follower %s", actor));
3716            }
3717            else {
3718                /* delete an entry */
3719                if (xs_startswith(id, snac.actor) && !is_draft(&snac, id)) {
3720                    /* it's a post by us: generate a delete */
3721                    xs *msg = msg_delete(&snac, id);
3722
3723                    enqueue_message(&snac, msg);
3724
3725                    snac_log(&snac, xs_fmt("posted tombstone for %s", id));
3726                }
3727
3728                timeline_del(&snac, id);
3729
3730                draft_del(&snac, id);
3731
3732                snac_log(&snac, xs_fmt("deleted entry %s", id));
3733            }
3734        }
3735        else
3736        if (strcmp(action, L("Pin")) == 0) { /** **/
3737            pin(&snac, id);
3738            timeline_touch(&snac);
3739        }
3740        else
3741        if (strcmp(action, L("Unpin")) == 0) { /** **/
3742            unpin(&snac, id);
3743            timeline_touch(&snac);
3744        }
3745        else
3746        if (strcmp(action, L("Bookmark")) == 0) { /** **/
3747            bookmark(&snac, id);
3748            timeline_touch(&snac);
3749        }
3750        else
3751        if (strcmp(action, L("Unbookmark")) == 0) { /** **/
3752            unbookmark(&snac, id);
3753            timeline_touch(&snac);
3754        }
3755        else
3756        if (strcmp(action, L("Approve")) == 0) { /** **/
3757            xs *fwreq = pending_get(&snac, actor);
3758
3759            if (fwreq != NULL) {
3760                xs *reply = msg_accept(&snac, fwreq, actor);
3761
3762                enqueue_message(&snac, reply);
3763
3764                if (xs_is_null(xs_dict_get(fwreq, "published"))) {
3765                    /* add a date if it doesn't include one (Mastodon) */
3766                    xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
3767                    fwreq = xs_dict_set(fwreq, "published", date);
3768                }
3769
3770                timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
3771
3772                follower_add(&snac, actor);
3773
3774                pending_del(&snac, actor);
3775
3776                snac_log(&snac, xs_fmt("new follower %s", actor));
3777            }
3778        }
3779        else
3780        if (strcmp(action, L("Discard")) == 0) { /** **/
3781            pending_del(&snac, actor);
3782        }
3783        else
3784            status = HTTP_STATUS_NOT_FOUND;
3785
3786        /* delete the cached timeline */
3787        if (status == HTTP_STATUS_SEE_OTHER)
3788            history_del(&snac, "timeline.html_");
3789    }
3790    else
3791    if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
3792        /* change of user data */
3793        const char *v;
3794        const char *p1, *p2;
3795
3796        if ((v = xs_dict_get(p_vars, "name")) != NULL)
3797            snac.config = xs_dict_set(snac.config, "name", v);
3798        if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
3799            snac.config = xs_dict_set(snac.config, "avatar", v);
3800        if ((v = xs_dict_get(p_vars, "bio")) != NULL)
3801            snac.config = xs_dict_set(snac.config, "bio", v);
3802        if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
3803            strcmp(v, "on") == 0) {
3804            snac.config = xs_dict_set(snac.config, "cw", "open");
3805        } else { /* if the checkbox is not set, the parameter is missing */
3806            snac.config = xs_dict_set(snac.config, "cw", "");
3807        }
3808        if ((v = xs_dict_get(p_vars, "email")) != NULL)
3809            snac.config = xs_dict_set(snac.config, "email", v);
3810        if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
3811            snac.config = xs_dict_set(snac.config, "telegram_bot", v);
3812        if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
3813            snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
3814        if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
3815            snac.config = xs_dict_set(snac.config, "ntfy_server", v);
3816        if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
3817            snac.config = xs_dict_set(snac.config, "ntfy_token", v);
3818        if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
3819            xs *days    = xs_number_new(atof(v));
3820            snac.config = xs_dict_set(snac.config, "purge_days", days);
3821        }
3822        if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
3823            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
3824        else
3825            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
3826        if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
3827            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
3828        else
3829            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
3830        if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
3831            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
3832        else
3833            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
3834        if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
3835            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
3836        else
3837            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
3838        if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
3839            snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
3840        else
3841            snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
3842        if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
3843            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
3844        else
3845            snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
3846
3847        if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
3848            snac.config = xs_dict_set(snac.config, "metadata", v);
3849
3850        /* uploads */
3851        const char *uploads[] = { "avatar", "header", NULL };
3852        int n;
3853        for (n = 0; uploads[n]; n++) {
3854            xs *var_name = xs_fmt("%s_file", uploads[n]);
3855
3856            const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
3857            if (xs_type(uploaded_file) == XSTYPE_LIST) {
3858                const char *fn = xs_list_get(uploaded_file, 0);
3859
3860                if (fn && *fn) {
3861                    const char *mimetype = xs_mime_by_ext(fn);
3862
3863                    if (xs_startswith(mimetype, "image/")) {
3864                        const char *ext = strrchr(fn, '.');
3865                        xs *hash        = xs_md5_hex(fn, strlen(fn));
3866                        xs *id          = xs_fmt("%s%s", hash, ext);
3867                        xs *url         = xs_fmt("%s/s/%s", snac.actor, id);
3868                        int fo          = xs_number_get(xs_list_get(uploaded_file, 1));
3869                        int fs          = xs_number_get(xs_list_get(uploaded_file, 2));
3870
3871                        /* store */
3872                        static_put(&snac, id, payload + fo, fs);
3873
3874                        snac.config = xs_dict_set(snac.config, uploads[n], url);
3875                    }
3876                }
3877            }
3878        }
3879
3880        /* delete images by removing url from user.json */
3881        for (n = 0; uploads[n]; n++) {
3882            xs *var_name = xs_fmt("%s_delete", uploads[n]);
3883            const char *delete_var = xs_dict_get(p_vars, var_name);
3884
3885            if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
3886                    snac.config = xs_dict_set(snac.config, uploads[n], "");
3887            }
3888        }
3889
3890        /* password change? */
3891        if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
3892            (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
3893            *p1 && strcmp(p1, p2) == 0) {
3894            xs *pw = hash_password(snac.uid, p1, NULL);
3895            snac.config = xs_dict_set(snac.config, "passwd", pw);
3896        }
3897
3898        user_persist(&snac, 1);
3899
3900        status = HTTP_STATUS_SEE_OTHER;
3901    }
3902    else
3903    if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
3904        notify_clear(&snac);
3905        timeline_touch(&snac);
3906
3907        status = HTTP_STATUS_SEE_OTHER;
3908    }
3909    else
3910    if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
3911        const char *irt   = xs_dict_get(p_vars, "irt");
3912        const char *opt   = xs_dict_get(p_vars, "question");
3913        const char *actor = xs_dict_get(p_vars, "actor");
3914
3915        xs *ls = NULL;
3916
3917        /* multiple choices? */
3918        if (xs_type(opt) == XSTYPE_LIST)
3919            ls = xs_dup(opt);
3920        else
3921        if (xs_type(opt) == XSTYPE_STRING) {
3922            ls = xs_list_new();
3923            ls = xs_list_append(ls, opt);
3924        }
3925
3926        const xs_str *v;
3927        int c = 0;
3928
3929        while (xs_list_next(ls, &v, &c)) {
3930            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
3931
3932            /* set the option */
3933            msg = xs_dict_append(msg, "name", v);
3934
3935            xs *c_msg = msg_create(&snac, msg);
3936
3937            enqueue_message(&snac, c_msg);
3938
3939            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3940        }
3941
3942        if (ls != NULL) {
3943            /* get the poll object */
3944            xs *poll = NULL;
3945
3946            if (valid_status(object_get(irt, &poll))) {
3947                const char *date = xs_dict_get(poll, "endTime");
3948                if (xs_is_null(date))
3949                    date = xs_dict_get(poll, "closed");
3950
3951                if (!xs_is_null(date)) {
3952                    time_t t = xs_parse_iso_date(date, 0) - time(NULL);
3953
3954                    /* request the poll when it's closed;
3955                       Pleroma does not send and update when the poll closes */
3956                    enqueue_object_request(&snac, irt, t + 2);
3957                }
3958            }
3959        }
3960
3961        status = HTTP_STATUS_SEE_OTHER;
3962    }
3963
3964    if (status == HTTP_STATUS_SEE_OTHER) {
3965        const char *redir = xs_dict_get(p_vars, "redir");
3966
3967        if (xs_is_null(redir))
3968            redir = "top";
3969
3970        *body   = xs_fmt("%s/admin#%s", snac.actor, redir);
3971        *b_size = strlen(*body);
3972    }
3973
3974    user_free(&snac);
3975
3976    return status;
3977}
3978
3979
3980xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
3981                        const char *title, const char *link, const char *desc)
3982/* converts a timeline to rss */
3983{
3984    xs_html *rss = xs_html_tag("rss",
3985        xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
3986        xs_html_attr("version",       "2.0"),
3987        xs_html_attr("xmlns:atom",    "http:/" "/www.w3.org/2005/Atom"));
3988
3989    xs_html *channel = xs_html_tag("channel",
3990        xs_html_tag("title",
3991            xs_html_text(title)),
3992        xs_html_tag("language",
3993            xs_html_text("en")),
3994        xs_html_tag("link",
3995            xs_html_text(link)),
3996        xs_html_sctag("atom:link",
3997            xs_html_attr("href", link),
3998            xs_html_attr("rel", "self"),
3999            xs_html_attr("type", "application/rss+xml")),
4000        xs_html_tag("generator",
4001            xs_html_text(USER_AGENT)),
4002        xs_html_tag("description",
4003            xs_html_text(desc)));
4004
4005    xs_html_add(rss, channel);
4006
4007    int cnt = 0;
4008    const char *v;
4009
4010    xs_list_foreach(timeline, v) {
4011        xs *msg = NULL;
4012
4013        if (user) {
4014            if (!valid_status(timeline_get_by_md5(user, v, &msg)))
4015                continue;
4016        }
4017        else {
4018            if (!valid_status(object_get_by_md5(v, &msg)))
4019                continue;
4020        }
4021
4022        const char *id = xs_dict_get(msg, "id");
4023        const char *content = xs_dict_get(msg, "content");
4024        const char *published = xs_dict_get(msg, "published");
4025
4026        if (user && !xs_startswith(id, user->actor))
4027            continue;
4028
4029        if (!id || !content || !published)
4030            continue;
4031
4032        /* create a title with the first line of the content */
4033        xs *title = xs_replace(content, "<br>", "\n");
4034        title = xs_regex_replace_i(title, "<[^>]+>", " ");
4035        title = xs_regex_replace_i(title, "&[^;]+;", " ");
4036        int i;
4037
4038        for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
4039
4040        if (title[i] != '\0') {
4041            title[i] = '\0';
4042            title = xs_str_cat(title, "...");
4043        }
4044
4045        title = xs_strip_i(title);
4046
4047        /* convert the date */
4048        time_t t = xs_parse_iso_date(published, 0);
4049        xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
4050
4051        /* if it's the first one, add it to the header */
4052        if (cnt == 0)
4053            xs_html_add(channel,
4054                xs_html_tag("lastBuildDate",
4055                    xs_html_text(rss_date)));
4056
4057        xs_html_add(channel,
4058            xs_html_tag("item",
4059                xs_html_tag("title",
4060                    xs_html_text(title)),
4061                xs_html_tag("link",
4062                    xs_html_text(id)),
4063                xs_html_tag("guid",
4064                    xs_html_text(id)),
4065                xs_html_tag("pubDate",
4066                    xs_html_text(rss_date)),
4067                xs_html_tag("description",
4068                    xs_html_text(content))));
4069
4070        cnt++;
4071    }
4072
4073    return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
4074}