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