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