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