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