2.56 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_add(form,
1808                        xs_html_sctag("input",
1809                            xs_html_attr("type", !xs_is_null(oo) ? "radio" : "checkbox"),
1810                            xs_html_attr("id", name),
1811                            xs_html_attr("value", name),
1812                            xs_html_attr("name", "question")),
1813                        xs_html_text(" "),
1814                        xs_html_tag("span",
1815                            xs_html_attr("title", ti),
1816                            xs_html_text(name)),
1817                        xs_html_sctag("br", NULL));
1818                }
1819            }
1820
1821            xs_html_add(form,
1822                xs_html_tag("p", NULL),
1823                xs_html_sctag("input",
1824                    xs_html_attr("type", "submit"),
1825                    xs_html_attr("class", "button"),
1826                    xs_html_attr("value", L("Vote"))));
1827
1828            xs_html_add(poll,
1829                poll_form);
1830        }
1831
1832        /* if it's *really* closed, say it */
1833        if (closed == 2) {
1834            xs_html_add(poll,
1835                xs_html_tag("p",
1836                    xs_html_text(L("Closed"))));
1837        }
1838        else {
1839            /* show when the poll closes */
1840            const char *end_time = xs_dict_get(msg, "endTime");
1841
1842            /* Pleroma does not have an endTime field;
1843               it has a closed time in the future */
1844            if (xs_is_null(end_time))
1845                end_time = xs_dict_get(msg, "closed");
1846
1847            if (!xs_is_null(end_time)) {
1848                time_t t0 = time(NULL);
1849                time_t t1 = xs_parse_iso_date(end_time, 0);
1850
1851                if (t1 > 0 && t1 > t0) {
1852                    time_t diff_time = t1 - t0;
1853                    xs *tf = xs_str_time_diff(diff_time);
1854                    char *p = tf;
1855
1856                    /* skip leading zeros */
1857                    for (; *p == '0' || *p == ':'; p++);
1858
1859                    xs_html_add(poll,
1860                        xs_html_tag("p",
1861                            xs_html_text(L("Closes in")),
1862                            xs_html_text(" "),
1863                            xs_html_text(p)));
1864                }
1865            }
1866        }
1867
1868        xs_html_add(snac_content,
1869            poll);
1870    }
1871
1872    /** attachments **/
1873    xs *attach = get_attachments(msg);
1874
1875    {
1876        /* make custom css for attachments easier */
1877        xs_html *content_attachments = xs_html_tag("div",
1878            xs_html_attr("class", "snac-content-attachments"));
1879
1880        xs_html_add(snac_content,
1881            content_attachments);
1882
1883        int c = 0;
1884        const xs_dict *a;
1885        while (xs_list_next(attach, &a, &c)) {
1886            const char *type = xs_dict_get(a, "type");
1887            const char *href = xs_dict_get(a, "href");
1888            const char *name = xs_dict_get(a, "name");
1889
1890            if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
1891                xs_html_add(content_attachments,
1892                    xs_html_tag("a",
1893                        xs_html_attr("href", href),
1894                        xs_html_attr("target", "_blank"),
1895                        xs_html_sctag("img",
1896                            xs_html_attr("loading", "lazy"),
1897                            xs_html_attr("src", href),
1898                            xs_html_attr("alt", name),
1899                            xs_html_attr("title", name))));
1900            }
1901            else
1902            if (xs_startswith(type, "video/")) {
1903                xs_html_add(content_attachments,
1904                    xs_html_tag("video",
1905                        xs_html_attr("preload", "none"),
1906                        xs_html_attr("style", "width: 100%"),
1907                        xs_html_attr("class", "snac-embedded-video"),
1908                        xs_html_attr("controls", NULL),
1909                        xs_html_attr("src", href),
1910                        xs_html_text(L("Video")),
1911                        xs_html_text(": "),
1912                        xs_html_tag("a",
1913                            xs_html_attr("href", href),
1914                            xs_html_text(name))));
1915            }
1916            else
1917            if (xs_startswith(type, "audio/")) {
1918                xs_html_add(content_attachments,
1919                    xs_html_tag("audio",
1920                        xs_html_attr("preload", "none"),
1921                        xs_html_attr("style", "width: 100%"),
1922                        xs_html_attr("class", "snac-embedded-audio"),
1923                        xs_html_attr("controls", NULL),
1924                        xs_html_attr("src", href),
1925                        xs_html_text(L("Audio")),
1926                        xs_html_text(": "),
1927                        xs_html_tag("a",
1928                            xs_html_attr("href", href),
1929                            xs_html_text(name))));
1930            }
1931            else
1932            if (strcmp(type, "Link") == 0) {
1933                xs_html_add(content_attachments,
1934                    xs_html_tag("p",
1935                        xs_html_tag("a",
1936                            xs_html_attr("href", href),
1937                            xs_html_text(href))));
1938
1939                /* do not generate an Alt... */
1940                name = NULL;
1941            }
1942            else {
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(L("Attachment")),
1948                            xs_html_text(": "),
1949                            xs_html_text(href))));
1950
1951                /* do not generate an Alt... */
1952                name = NULL;
1953            }
1954
1955            if (name != NULL && *name) {
1956                xs_html_add(content_attachments,
1957                    xs_html_tag("p",
1958                        xs_html_attr("class", "snac-alt-text"),
1959                        xs_html_tag("details",
1960                            xs_html_tag("summary",
1961                                xs_html_text(L("Alt..."))),
1962                            xs_html_text(name))));
1963            }
1964        }
1965    }
1966
1967    /* has this message an audience (i.e., comes from a channel or community)? */
1968    const char *audience = xs_dict_get(msg, "audience");
1969    if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
1970        xs_html *au_tag = xs_html_tag("p",
1971            xs_html_text("("),
1972            xs_html_tag("a",
1973                xs_html_attr("href", audience),
1974                xs_html_attr("title", L("Source channel or community")),
1975                xs_html_text(audience)),
1976            xs_html_text(")"));
1977
1978        xs_html_add(snac_content_wrap,
1979            au_tag);
1980    }
1981
1982    /** controls **/
1983
1984    if (!read_only && user) {
1985        xs_html_add(entry,
1986            html_entry_controls(user, actor, msg, md5));
1987    }
1988
1989    /** children **/
1990    if (!hide_children) {
1991        xs *children = object_children(id);
1992        int left     = xs_list_len(children);
1993
1994        if (left) {
1995            xs_html *ch_details = xs_html_tag("details",
1996                xs_html_attr("open", NULL),
1997                xs_html_tag("summary",
1998                    xs_html_text("...")));
1999
2000            xs_html_add(entry,
2001                ch_details);
2002
2003            xs_html *ch_container = xs_html_tag("div",
2004                xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
2005
2006            xs_html_add(ch_details,
2007                ch_container);
2008
2009            xs_html *ch_older = NULL;
2010            if (left > 3) {
2011                xs_html_add(ch_container,
2012                    ch_older = xs_html_tag("details",
2013                        xs_html_tag("summary",
2014                            xs_html_text(L("Older...")))));
2015            }
2016
2017            xs_list *p = children;
2018            const char *cmd5;
2019            int cnt = 0;
2020            int o_cnt = 0;
2021
2022            while (xs_list_iter(&p, &cmd5)) {
2023                xs *chd = NULL;
2024
2025                if (user)
2026                    timeline_get_by_md5(user, cmd5, &chd);
2027                else
2028                    object_get_by_md5(cmd5, &chd);
2029
2030                if (chd != NULL) {
2031                    if (xs_is_null(xs_dict_get(chd, "name"))) {
2032                        xs_html *che = html_entry(user, chd, read_only,
2033                            level + 1, cmd5, hide_children);
2034
2035                        if (che != NULL) {
2036                            if (left > 3) {
2037                                xs_html_add(ch_older,
2038                                    che);
2039
2040                                o_cnt++;
2041                            }
2042                            else
2043                                xs_html_add(ch_container,
2044                                    che);
2045
2046                            cnt++;
2047                        }
2048                    }
2049
2050                    left--;
2051                }
2052                else
2053                    srv_debug(2, xs_fmt("cannot read child %s", cmd5));
2054            }
2055
2056            /* if no children were finally added, hide the details */
2057            if (cnt == 0)
2058                xs_html_add(ch_details,
2059                    xs_html_attr("style", "display: none"));
2060
2061            if (o_cnt == 0 && ch_older)
2062                xs_html_add(ch_older,
2063                    xs_html_attr("style", "display: none"));
2064        }
2065    }
2066
2067    return entry_top;
2068}
2069
2070
2071xs_html *html_footer(void)
2072{
2073    return xs_html_tag("div",
2074        xs_html_attr("class", "snac-footer"),
2075        xs_html_tag("a",
2076            xs_html_attr("href", srv_baseurl),
2077            xs_html_text(L("about this site"))),
2078        xs_html_text(" - "),
2079        xs_html_text(L("powered by ")),
2080        xs_html_tag("a",
2081            xs_html_attr("href", WHAT_IS_SNAC_URL),
2082            xs_html_tag("abbr",
2083                xs_html_attr("title", "Social Network Are Crap"),
2084                xs_html_text("snac"))));
2085}
2086
2087
2088xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
2089                      int skip, int show, int show_more,
2090                      char *title, char *page, int utl)
2091/* returns the HTML for the timeline */
2092{
2093    xs_list *p = (xs_list *)list;
2094    const char *v;
2095    double t = ftime();
2096
2097    xs *desc = NULL;
2098    xs *alternate = NULL;
2099
2100    if (xs_list_len(list) == 1) {
2101        /* only one element? pick the description from the source */
2102        const char *id = xs_list_get(list, 0);
2103        xs *d = NULL;
2104        object_get_by_md5(id, &d);
2105        const char *sc = xs_dict_get(d, "sourceContent");
2106        if (d && sc != NULL)
2107            desc = xs_dup(sc);
2108
2109        alternate = xs_dup(xs_dict_get(d, "id"));
2110    }
2111
2112    xs_html *head;
2113    xs_html *body;
2114
2115    if (user) {
2116        head = html_user_head(user, desc, alternate);
2117        body = html_user_body(user, read_only);
2118    }
2119    else {
2120        head = html_instance_head();
2121        body = html_instance_body();
2122    }
2123
2124    xs_html *html = xs_html_tag("html",
2125        head,
2126        body);
2127
2128    if (user && !read_only)
2129        xs_html_add(body,
2130            html_top_controls(user));
2131
2132    /* show links to the available lists */
2133    if (user && !read_only) {
2134        xs *lists = list_maint(user, NULL, 0); /* get list of lists */
2135
2136        if (xs_list_len(lists)) {
2137            int ct = 0;
2138            const char *v;
2139
2140            xs_html *lol = xs_html_tag("ul",
2141                xs_html_attr("class", "snac-list-of-lists"));
2142            xs_html_add(body, lol);
2143
2144            while (xs_list_next(lists, &v, &ct)) {
2145                const char *lname = xs_list_get(v, 1);
2146                xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
2147                xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
2148
2149                xs_html_add(lol,
2150                    xs_html_tag("li",
2151                        xs_html_tag("a",
2152                            xs_html_attr("href", url),
2153                            xs_html_attr("class", "snac-list-link"),
2154                            xs_html_attr("title", ttl),
2155                            xs_html_text(lname))));
2156            }
2157        }
2158    }
2159
2160    if (title) {
2161        xs_html_add(body,
2162            xs_html_tag("h2",
2163                xs_html_attr("class", "snac-header"),
2164                xs_html_text(title)));
2165    }
2166
2167    xs_html_add(body,
2168        xs_html_tag("a",
2169            xs_html_attr("name", "snac-posts")));
2170
2171    xs_html *posts = xs_html_tag("div",
2172        xs_html_attr("class", "snac-posts"));
2173
2174    xs_html_add(body,
2175        posts);
2176
2177    while (xs_list_iter(&p, &v)) {
2178        xs *msg = NULL;
2179        int status;
2180
2181        if (utl && user && !is_pinned_by_md5(user, v))
2182            status = timeline_get_by_md5(user, v, &msg);
2183        else
2184            status = object_get_by_md5(v, &msg);
2185
2186        if (!valid_status(status))
2187            continue;
2188
2189        /* if it's an instance page, discard messages from private users */
2190        if (user == NULL && is_msg_from_private_user(msg))
2191            continue;
2192
2193        /* is this message a non-public reply? */
2194        if (user != NULL && !is_msg_public(msg)) {
2195            const char *irt = xs_dict_get(msg, "inReplyTo");
2196
2197            /* is it a reply to something not in the storage? */
2198            if (!xs_is_null(irt) && !object_here(irt)) {
2199                /* is it for me? */
2200                const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
2201                const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
2202
2203                if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
2204                    snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
2205                    continue;
2206                }
2207            }
2208        }
2209
2210        xs_html *entry = html_entry(user, msg, read_only, 0, v, user ? 0 : 1);
2211
2212        if (entry != NULL)
2213            xs_html_add(posts,
2214                entry);
2215    }
2216
2217    if (list && user && read_only) {
2218        /** history **/
2219        if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
2220            xs_html *ul = xs_html_tag("ul", NULL);
2221
2222            xs_html *history = xs_html_tag("div",
2223                xs_html_attr("class", "snac-history"),
2224                xs_html_tag("p",
2225                    xs_html_attr("class", "snac-history-title"),
2226                    xs_html_text(L("History"))),
2227                    ul);
2228
2229            xs *list = history_list(user);
2230            xs_list *p = list;
2231            const char *v;
2232
2233            while (xs_list_iter(&p, &v)) {
2234                xs *fn  = xs_replace(v, ".html", "");
2235                xs *url = xs_fmt("%s/h/%s", user->actor, v);
2236
2237                xs_html_add(ul,
2238                    xs_html_tag("li",
2239                        xs_html_tag("a",
2240                            xs_html_attr("href", url),
2241                            xs_html_text(fn))));
2242            }
2243
2244            xs_html_add(body,
2245                history);
2246        }
2247    }
2248
2249    {
2250        xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
2251        xs_html_add(body,
2252            xs_html_raw(s1));
2253    }
2254
2255    if (show_more) {
2256        xs *m  = NULL;
2257        xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
2258
2259        xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
2260
2261        if (page != NULL)
2262            url = xs_str_cat(url, page);
2263
2264        if (xs_str_in(url, "?") != -1)
2265            m = xs_fmt("%s&%s", url, ss);
2266        else
2267            m = xs_fmt("%s?%s", url, ss);
2268
2269        xs_html *more_links = xs_html_tag("p",
2270            xs_html_tag("a",
2271                xs_html_attr("href", url),
2272                xs_html_attr("name", "snac-more"),
2273                xs_html_text(L("Back to top"))),
2274            xs_html_text(" - "),
2275            xs_html_tag("a",
2276                xs_html_attr("href", m),
2277                xs_html_attr("name", "snac-more"),
2278                xs_html_text(L("More..."))));
2279
2280        xs_html_add(body,
2281            more_links);
2282    }
2283
2284    xs_html_add(body,
2285        html_footer());
2286
2287    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2288}
2289
2290
2291xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t)
2292{
2293    xs_html *snac_posts;
2294    xs_html *people = xs_html_tag("div",
2295        xs_html_tag("h2",
2296            xs_html_attr("class", "snac-header"),
2297            xs_html_text(header)),
2298        snac_posts = xs_html_tag("details",
2299                xs_html_attr("open", NULL),
2300                xs_html_tag("summary",
2301                    xs_html_text("..."))));
2302
2303    xs_list *p = list;
2304    const char *actor_id;
2305
2306    while (xs_list_iter(&p, &actor_id)) {
2307        xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
2308        xs *actor = NULL;
2309
2310        if (valid_status(actor_get(actor_id, &actor))) {
2311            xs_html *snac_post = xs_html_tag("div",
2312                xs_html_attr("class", "snac-post"),
2313                xs_html_tag("a",
2314                    xs_html_attr("name", md5)),
2315                xs_html_tag("div",
2316                    xs_html_attr("class", "snac-post-header"),
2317                    html_actor_icon(snac, actor, xs_dict_get(actor, "published"), NULL, NULL, 0, 1)));
2318
2319            /* content (user bio) */
2320            const char *c = xs_dict_get(actor, "summary");
2321
2322            if (!xs_is_null(c)) {
2323                xs *sc = sanitize(c);
2324
2325                xs_html *snac_content = xs_html_tag("div",
2326                    xs_html_attr("class", "snac-content"));
2327
2328                if (xs_startswith(sc, "<p>"))
2329                    xs_html_add(snac_content,
2330                        xs_html_raw(sc)); /* already sanitized */
2331                else
2332                    xs_html_add(snac_content,
2333                        xs_html_tag("p",
2334                            xs_html_raw(sc))); /* already sanitized */
2335
2336                xs_html_add(snac_post, snac_content);
2337            }
2338
2339            /* buttons */
2340            xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
2341
2342            xs_html *snac_controls = xs_html_tag("div",
2343                xs_html_attr("class",   "snac-controls"));
2344
2345            xs_html *form = xs_html_tag("form",
2346                xs_html_attr("autocomplete",    "off"),
2347                xs_html_attr("method",          "post"),
2348                xs_html_attr("action",          btn_form_action),
2349                xs_html_sctag("input",
2350                    xs_html_attr("type",    "hidden"),
2351                    xs_html_attr("name",    "actor"),
2352                    xs_html_attr("value",   actor_id)),
2353                xs_html_sctag("input",
2354                    xs_html_attr("type",    "hidden"),
2355                    xs_html_attr("name",    "actor-form"),
2356                    xs_html_attr("value",   "yes")));
2357
2358            xs_html_add(snac_controls, form);
2359
2360            if (following_check(snac, actor_id)) {
2361                xs_html_add(form,
2362                    html_button("unfollow", L("Unfollow"),
2363                                L("Stop following this user's activity")));
2364
2365                if (is_limited(snac, actor_id))
2366                    xs_html_add(form,
2367                        html_button("unlimit", L("Unlimit"),
2368                                L("Allow announces (boosts) from this user")));
2369                else
2370                    xs_html_add(form,
2371                        html_button("limit", L("Limit"),
2372                                L("Block announces (boosts) from this user")));
2373            }
2374            else {
2375                xs_html_add(form,
2376                    html_button("follow", L("Follow"),
2377                                L("Start following this user's activity")));
2378
2379                if (follower_check(snac, actor_id))
2380                    xs_html_add(form,
2381                        html_button("delete", L("Delete"), L("Delete this user")));
2382            }
2383
2384            if (is_muted(snac, actor_id))
2385                xs_html_add(form,
2386                    html_button("unmute", L("Unmute"),
2387                                L("Stop blocking activities from this user")));
2388            else
2389                xs_html_add(form,
2390                    html_button("mute", L("MUTE"),
2391                                L("Block any activity from this user")));
2392
2393            /* the post textarea */
2394            xs *dm_div_id  = xs_fmt("%s_%s_dm", md5, t);
2395            xs *dm_form_id = xs_fmt("%s_reply_form", md5);
2396
2397            xs_html_add(snac_controls,
2398                xs_html_tag("p", NULL),
2399                html_note(snac, L("Direct Message..."),
2400                    dm_div_id, dm_form_id,
2401                    "", "",
2402                    NULL, actor_id,
2403                    xs_stock(XSTYPE_FALSE), "",
2404                    xs_stock(XSTYPE_FALSE), NULL,
2405                    NULL, 0, "", ""),
2406                xs_html_tag("p", NULL));
2407
2408            xs_html_add(snac_post, snac_controls);
2409
2410            xs_html_add(snac_posts, snac_post);
2411        }
2412    }
2413
2414    return people;
2415}
2416
2417
2418xs_str *html_people(snac *user)
2419{
2420    xs *wing = following_list(user);
2421    xs *wers = follower_list(user);
2422
2423    xs_html *html = xs_html_tag("html",
2424        html_user_head(user, NULL, NULL),
2425        xs_html_add(html_user_body(user, 0),
2426            xs_html_tag("div",
2427                xs_html_attr("class", "snac-posts"),
2428                html_people_list(user, wing, L("People you follow"), "i"),
2429                html_people_list(user, wers, L("People that follow you"), "e")),
2430            html_footer()));
2431
2432    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2433}
2434
2435
2436xs_str *html_notifications(snac *user, int skip, int show)
2437{
2438    xs *n_list = notify_list(user, skip, show);
2439    xs *n_time = notify_check_time(user, 0);
2440
2441    xs_html *body = html_user_body(user, 0);
2442
2443    xs_html *html = xs_html_tag("html",
2444        html_user_head(user, NULL, NULL),
2445        body);
2446
2447    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
2448
2449    xs_html_add(body,
2450        xs_html_tag("form",
2451            xs_html_attr("autocomplete", "off"),
2452            xs_html_attr("method",       "post"),
2453            xs_html_attr("action",       clear_all_action),
2454            xs_html_attr("id",           "clear"),
2455            xs_html_sctag("input",
2456                xs_html_attr("type",     "submit"),
2457                xs_html_attr("class",    "snac-btn-like"),
2458                xs_html_attr("value",    L("Clear all")))));
2459
2460    xs_html *noti_new = NULL;
2461    xs_html *noti_seen = NULL;
2462
2463    xs_list *p = n_list;
2464    const xs_str *v;
2465    while (xs_list_iter(&p, &v)) {
2466        xs *noti = notify_get(user, v);
2467
2468        if (noti == NULL)
2469            continue;
2470
2471        xs *obj = NULL;
2472        const char *type  = xs_dict_get(noti, "type");
2473        const char *utype = xs_dict_get(noti, "utype");
2474        const char *id    = xs_dict_get(noti, "objid");
2475        const char *date  = xs_dict_get(noti, "date");
2476
2477        if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
2478            continue;
2479
2480        if (is_hidden(user, id))
2481            continue;
2482
2483        const char *actor_id = xs_dict_get(noti, "actor");
2484        xs *actor = NULL;
2485
2486        if (!valid_status(actor_get(actor_id, &actor)))
2487            continue;
2488
2489        xs *a_name = actor_name(actor);
2490        const char *label = type;
2491
2492        if (strcmp(type, "Create") == 0)
2493            label = L("Mention");
2494        else
2495        if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
2496            label = L("Finished poll");
2497        else
2498        if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
2499            label = L("Unfollow");
2500
2501        xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
2502
2503        xs_html *entry = xs_html_tag("div",
2504            xs_html_attr("class", "snac-post-with-desc"),
2505            xs_html_tag("p",
2506                xs_html_tag("b",
2507                    xs_html_text(label),
2508                    xs_html_text(" by "),
2509                    xs_html_tag("a",
2510                        xs_html_attr("href", actor_id),
2511                        xs_html_raw(a_name))), /* a_name is already sanitized */
2512                xs_html_text(" "),
2513                xs_html_tag("time",
2514                    xs_html_attr("class", "dt-published snac-pubdate"),
2515                    xs_html_attr("title", date),
2516                    xs_html_text(s_date))));
2517
2518        if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
2519            xs_html_add(entry,
2520                xs_html_tag("div",
2521                    xs_html_attr("class", "snac-post"),
2522                    html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0)));
2523        }
2524        else {
2525            xs *md5 = xs_md5_hex(id, strlen(id));
2526
2527            xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
2528
2529            if (h != NULL) {
2530                xs_html_add(entry,
2531                    h);
2532            }
2533        }
2534
2535        if (strcmp(v, n_time) > 0) {
2536            /* unseen notification */
2537            if (noti_new == NULL) {
2538                noti_new = xs_html_tag("div",
2539                    xs_html_tag("h2",
2540                        xs_html_attr("class", "snac-header"),
2541                        xs_html_text(L("New"))));
2542
2543                xs_html_add(body,
2544                    noti_new);
2545            }
2546
2547            xs_html_add(noti_new,
2548                entry);
2549        }
2550        else {
2551            /* already seen notification */
2552            if (noti_seen == NULL) {
2553                noti_seen = xs_html_tag("div",
2554                    xs_html_tag("h2",
2555                        xs_html_attr("class", "snac-header"),
2556                        xs_html_text(L("Already seen"))));
2557
2558                xs_html_add(body,
2559                    noti_seen);
2560            }
2561
2562            xs_html_add(noti_seen,
2563                entry);
2564        }
2565    }
2566
2567    if (noti_new == NULL && noti_seen == NULL)
2568        xs_html_add(body,
2569            xs_html_tag("h2",
2570                xs_html_attr("class", "snac-header"),
2571                xs_html_text(L("None"))));
2572
2573    /* add the navigation footer */
2574    xs *next_p = notify_list(user, skip + show, 1);
2575    if (xs_list_len(next_p)) {
2576        xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
2577            user->actor, skip + show, show);
2578
2579        xs_html_add(body,
2580            xs_html_tag("p",
2581                xs_html_tag("a",
2582                    xs_html_attr("href", url),
2583                    xs_html_text(L("More...")))));
2584    }
2585
2586    xs_html_add(body,
2587        html_footer());
2588
2589    /* set the check time to now */
2590    xs *dummy = notify_check_time(user, 1);
2591    dummy = xs_free(dummy);
2592
2593    timeline_touch(user);
2594
2595    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2596}
2597
2598
2599int html_get_handler(const xs_dict *req, const char *q_path,
2600                     char **body, int *b_size, char **ctype, xs_str **etag)
2601{
2602    const char *accept = xs_dict_get(req, "accept");
2603    int status = HTTP_STATUS_NOT_FOUND;
2604    snac snac;
2605    xs *uid = NULL;
2606    const char *p_path;
2607    int cache = 1;
2608    int save = 1;
2609    const char *v;
2610
2611    xs *l = xs_split_n(q_path, "/", 2);
2612    v = xs_list_get(l, 1);
2613
2614    if (xs_is_null(v)) {
2615        srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
2616        return HTTP_STATUS_NOT_FOUND;
2617    }
2618
2619    uid = xs_dup(v);
2620
2621    /* rss extension? */
2622    if (xs_endswith(uid, ".rss")) {
2623        uid = xs_crop_i(uid, 0, -4);
2624        p_path = ".rss";
2625    }
2626    else
2627        p_path = xs_list_get(l, 2);
2628
2629    if (!uid || !user_open(&snac, uid)) {
2630        /* invalid user */
2631        srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
2632        return HTTP_STATUS_NOT_FOUND;
2633    }
2634
2635    /* return the RSS if requested by Accept header */
2636    if (accept != NULL) {
2637        if (xs_str_in(accept, "text/xml") != -1 ||
2638            xs_str_in(accept, "application/rss+xml") != -1)
2639            p_path = ".rss";
2640    }
2641
2642    /* check if server config variable 'disable_cache' is set */
2643    if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
2644        cache = 0;
2645
2646    int skip = 0;
2647    int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
2648    int show = def_show;
2649
2650    const xs_dict *q_vars = xs_dict_get(req, "q_vars");
2651    if ((v = xs_dict_get(q_vars, "skip")) != NULL)
2652        skip = atoi(v), cache = 0, save = 0;
2653    if ((v = xs_dict_get(q_vars, "show")) != NULL)
2654        show = atoi(v), cache = 0, save = 0;
2655    if ((v = xs_dict_get(q_vars, "da")) != NULL) {
2656        /* user dismissed an announcement */
2657        if (login(&snac, req)) {
2658            double ts = atof(v);
2659            xs *timestamp = xs_number_new(ts);
2660            srv_log(xs_fmt("user dismissed announcements until %d", ts));
2661            snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
2662            user_persist(&snac, 0);
2663        }
2664    }
2665
2666    /* a show of 0 has no sense */
2667    if (show == 0)
2668        show = def_show;
2669
2670    if (p_path == NULL) { /** public timeline **/
2671        xs *h = xs_str_localtime(0, "%Y-%m.html");
2672
2673        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
2674            /** empty public timeline for private users **/
2675            *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1);
2676            *b_size = strlen(*body);
2677            status  = HTTP_STATUS_OK;
2678        }
2679        else
2680        if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
2681            snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
2682
2683            status = history_get(&snac, h, body, b_size,
2684                        xs_dict_get(req, "if-none-match"), etag);
2685        }
2686        else {
2687            xs *list = timeline_list(&snac, "public", skip, show);
2688            xs *next = timeline_list(&snac, "public", skip + show, 1);
2689
2690            xs *pins = pinned_list(&snac);
2691            pins = xs_list_cat(pins, list);
2692
2693            *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1);
2694
2695            *b_size = strlen(*body);
2696            status  = HTTP_STATUS_OK;
2697
2698            if (save)
2699                history_add(&snac, h, *body, *b_size, etag);
2700        }
2701    }
2702    else
2703    if (strcmp(p_path, "admin") == 0) { /** private timeline **/
2704        if (!login(&snac, req)) {
2705            *body  = xs_dup(uid);
2706            status = HTTP_STATUS_UNAUTHORIZED;
2707        }
2708        else {
2709            const char *q = xs_dict_get(q_vars, "q");
2710
2711            if (q && *q) {
2712                if (*q == '#') {
2713                    /** search by tag **/
2714                    xs *tl = tag_search(q, skip, show + 1);
2715                    int more = 0;
2716                    if (xs_list_len(tl) >= show + 1) {
2717                        /* drop the last one */
2718                        tl = xs_list_del(tl, -1);
2719                        more = 1;
2720                    }
2721
2722                    xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
2723                    xs *title = xs_fmt(xs_list_len(tl) ?
2724                        L("Search results for tag %s") : L("Nothing found for tag %s"), q);
2725
2726                    *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0);
2727                    *b_size = strlen(*body);
2728                    status  = HTTP_STATUS_OK;
2729                }
2730                else {
2731                    /** search by content **/
2732                    int to = 0;
2733                    int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
2734                    xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
2735                    xs *title = NULL;
2736                    xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
2737                    int tl_len = xs_list_len(tl);
2738
2739                    if (to)
2740                        title = xs_fmt(L("Search results for '%s' (may be more)"), q);
2741                    else
2742                    if (tl_len)
2743                        title = xs_fmt(L("Search results for '%s'"), q);
2744                    else
2745                    if (skip)
2746                        title = xs_fmt(L("No more matches for '%s'"), q);
2747                    else
2748                        title = xs_fmt(L("Nothing found for '%s'"), q);
2749
2750                    *body   = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show, title, page, 0);
2751                    *b_size = strlen(*body);
2752                    status  = HTTP_STATUS_OK;
2753                }
2754            }
2755            else {
2756                double t = history_mtime(&snac, "timeline.html_");
2757
2758                /* if enabled by admin, return a cached page if its timestamp is:
2759                   a) newer than the timeline timestamp
2760                   b) newer than the start time of the server
2761                */
2762                if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
2763                    snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
2764
2765                    status = history_get(&snac, "timeline.html_", body, b_size,
2766                                xs_dict_get(req, "if-none-match"), etag);
2767                }
2768                else {
2769                    snac_debug(&snac, 1, xs_fmt("building timeline"));
2770
2771                    xs *list = timeline_list(&snac, "private", skip, show);
2772                    xs *next = timeline_list(&snac, "private", skip + show, 1);
2773
2774                    xs *pins = pinned_list(&snac);
2775                    pins = xs_list_cat(pins, list);
2776
2777                    *body = html_timeline(&snac, pins, 0, skip, show,
2778                            xs_list_len(next), NULL, "/admin", 1);
2779
2780                    *b_size = strlen(*body);
2781                    status  = HTTP_STATUS_OK;
2782
2783                    if (save)
2784                        history_add(&snac, "timeline.html_", *body, *b_size, etag);
2785                }
2786            }
2787        }
2788    }
2789    else
2790    if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
2791        if (!login(&snac, req)) {
2792            *body  = xs_dup(uid);
2793            status = HTTP_STATUS_UNAUTHORIZED;
2794        }
2795        else {
2796            xs *l = xs_split(p_path, "/");
2797            const char *md5 = xs_list_get(l, -1);
2798
2799            if (md5 && *md5 && timeline_here(&snac, md5)) {
2800                xs *list = xs_list_append(xs_list_new(), md5);
2801
2802                *body   = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1);
2803                *b_size = strlen(*body);
2804                status  = HTTP_STATUS_OK;
2805            }
2806        }
2807    }
2808    else
2809    if (strcmp(p_path, "people") == 0) { /** the list of people **/
2810        if (!login(&snac, req)) {
2811            *body  = xs_dup(uid);
2812            status = HTTP_STATUS_UNAUTHORIZED;
2813        }
2814        else {
2815            *body   = html_people(&snac);
2816            *b_size = strlen(*body);
2817            status  = HTTP_STATUS_OK;
2818        }
2819    }
2820    else
2821    if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
2822        if (!login(&snac, req)) {
2823            *body  = xs_dup(uid);
2824            status = HTTP_STATUS_UNAUTHORIZED;
2825        }
2826        else {
2827            *body   = html_notifications(&snac, skip, show);
2828            *b_size = strlen(*body);
2829            status  = HTTP_STATUS_OK;
2830        }
2831    }
2832    else
2833    if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
2834        if (!login(&snac, req)) {
2835            *body  = xs_dup(uid);
2836            status = HTTP_STATUS_UNAUTHORIZED;
2837        }
2838        else {
2839            xs *list = timeline_instance_list(skip, show);
2840            xs *next = timeline_instance_list(skip + show, 1);
2841
2842            *body = html_timeline(&snac, list, 0, skip, show,
2843                xs_list_len(next), L("Showing instance timeline"), "/instance", 0);
2844            *b_size = strlen(*body);
2845            status  = HTTP_STATUS_OK;
2846        }
2847    }
2848    else
2849    if (xs_startswith(p_path, "list/")) { /** list timelines **/
2850        if (!login(&snac, req)) {
2851            *body  = xs_dup(uid);
2852            status = HTTP_STATUS_UNAUTHORIZED;
2853        }
2854        else {
2855            xs *l = xs_split(p_path, "/");
2856            const char *lid = xs_list_get(l, -1);
2857
2858            xs *list = list_timeline(&snac, lid, skip, show);
2859            xs *next = list_timeline(&snac, lid, skip + show, 1);
2860
2861            if (list != NULL) {
2862                xs *ttl = timeline_top_level(&snac, list);
2863
2864                xs *base = xs_fmt("/list/%s", lid);
2865                xs *name = list_maint(&snac, lid, 3);
2866                xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
2867
2868                *body = html_timeline(&snac, ttl, 0, skip, show,
2869                    xs_list_len(next), title, base, 1);
2870                *b_size = strlen(*body);
2871                status  = HTTP_STATUS_OK;
2872            }
2873        }
2874    }
2875    else
2876    if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
2877        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2878            return HTTP_STATUS_FORBIDDEN;
2879
2880        xs *id  = xs_fmt("%s/%s", snac.actor, p_path);
2881        xs *msg = NULL;
2882
2883        if (valid_status(object_get(id, &msg))) {
2884            xs *md5  = xs_md5_hex(id, strlen(id));
2885            xs *list = xs_list_new();
2886
2887            list = xs_list_append(list, md5);
2888
2889            *body   = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1);
2890            *b_size = strlen(*body);
2891            status  = HTTP_STATUS_OK;
2892        }
2893    }
2894    else
2895    if (xs_startswith(p_path, "s/")) { /** a static file **/
2896        xs *l    = xs_split(p_path, "/");
2897        const char *id = xs_list_get(l, 1);
2898        int sz;
2899
2900        if (id && *id) {
2901            status = static_get(&snac, id, body, &sz,
2902                        xs_dict_get(req, "if-none-match"), etag);
2903
2904            if (valid_status(status)) {
2905                *b_size = sz;
2906                *ctype  = (char *)xs_mime_by_ext(id);
2907            }
2908        }
2909    }
2910    else
2911    if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
2912        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2913            return HTTP_STATUS_FORBIDDEN;
2914
2915        if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
2916            return HTTP_STATUS_FORBIDDEN;
2917
2918        xs *l = xs_split(p_path, "/");
2919        const char *id = xs_list_get(l, 1);
2920
2921        if (id && *id) {
2922            if (xs_endswith(id, "timeline.html_")) {
2923                /* Don't let them in */
2924                *b_size = 0;
2925                status = HTTP_STATUS_NOT_FOUND;
2926            }
2927            else
2928                status = history_get(&snac, id, body, b_size,
2929                            xs_dict_get(req, "if-none-match"), etag);
2930        }
2931    }
2932    else
2933    if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
2934        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2935            return HTTP_STATUS_FORBIDDEN;
2936
2937        xs *elems = timeline_simple_list(&snac, "public", 0, 20);
2938        xs *bio   = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL, NULL);
2939
2940        xs *rss_title = xs_fmt("%s (@%s@%s)",
2941            xs_dict_get(snac.config, "name"),
2942            snac.uid,
2943            xs_dict_get(srv_config, "host"));
2944        xs *rss_link = xs_fmt("%s.rss", snac.actor);
2945
2946        *body   = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
2947        *b_size = strlen(*body);
2948        *ctype  = "application/rss+xml; charset=utf-8";
2949        status  = HTTP_STATUS_OK;
2950
2951        snac_debug(&snac, 1, xs_fmt("serving RSS"));
2952    }
2953    else
2954        status = HTTP_STATUS_NOT_FOUND;
2955
2956    user_free(&snac);
2957
2958    if (valid_status(status) && *ctype == NULL) {
2959        *ctype = "text/html; charset=utf-8";
2960    }
2961
2962    return status;
2963}
2964
2965
2966int html_post_handler(const xs_dict *req, const char *q_path,
2967                      char *payload, int p_size,
2968                      char **body, int *b_size, char **ctype)
2969{
2970    (void)p_size;
2971    (void)ctype;
2972
2973    int status = 0;
2974    snac snac;
2975    const char *uid;
2976    const char *p_path;
2977    const xs_dict *p_vars;
2978
2979    xs *l = xs_split_n(q_path, "/", 2);
2980
2981    uid = xs_list_get(l, 1);
2982    if (!uid || !user_open(&snac, uid)) {
2983        /* invalid user */
2984        srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
2985        return HTTP_STATUS_NOT_FOUND;
2986    }
2987
2988    p_path = xs_list_get(l, 2);
2989
2990    /* all posts must be authenticated */
2991    if (!login(&snac, req)) {
2992        user_free(&snac);
2993        *body  = xs_dup(uid);
2994        return HTTP_STATUS_UNAUTHORIZED;
2995    }
2996
2997    p_vars = xs_dict_get(req, "p_vars");
2998
2999#if 0
3000    xs_json_dump(p_vars, 4, stdout);
3001#endif
3002
3003    if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
3004        /* post note */
3005        const xs_str *content      = xs_dict_get(p_vars, "content");
3006        const xs_str *in_reply_to  = xs_dict_get(p_vars, "in_reply_to");
3007        const xs_str *attach_url   = xs_dict_get(p_vars, "attach_url");
3008        const xs_list *attach_file = xs_dict_get(p_vars, "attach");
3009        const xs_str *to           = xs_dict_get(p_vars, "to");
3010        const xs_str *sensitive    = xs_dict_get(p_vars, "sensitive");
3011        const xs_str *summary      = xs_dict_get(p_vars, "summary");
3012        const xs_str *edit_id      = xs_dict_get(p_vars, "edit_id");
3013        const xs_str *alt_text     = xs_dict_get(p_vars, "alt_text");
3014        int priv             = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
3015        xs *attach_list      = xs_list_new();
3016
3017        /* default alt text */
3018        if (xs_is_null(alt_text))
3019            alt_text = "";
3020
3021        /* is attach_url set? */
3022        if (!xs_is_null(attach_url) && *attach_url != '\0') {
3023            xs *l = xs_list_new();
3024
3025            l = xs_list_append(l, attach_url);
3026            l = xs_list_append(l, alt_text);
3027
3028            attach_list = xs_list_append(attach_list, l);
3029        }
3030
3031        /* is attach_file set? */
3032        if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
3033            const char *fn = xs_list_get(attach_file, 0);
3034
3035            if (*fn != '\0') {
3036                char *ext = strrchr(fn, '.');
3037                xs *hash  = xs_md5_hex(fn, strlen(fn));
3038                xs *id    = xs_fmt("%s%s", hash, ext);
3039                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
3040                int fo    = xs_number_get(xs_list_get(attach_file, 1));
3041                int fs    = xs_number_get(xs_list_get(attach_file, 2));
3042
3043                /* store */
3044                static_put(&snac, id, payload + fo, fs);
3045
3046                xs *l = xs_list_new();
3047
3048                l = xs_list_append(l, url);
3049                l = xs_list_append(l, alt_text);
3050
3051                attach_list = xs_list_append(attach_list, l);
3052            }
3053        }
3054
3055        if (content != NULL) {
3056            xs *msg       = NULL;
3057            xs *c_msg     = NULL;
3058            xs *content_2 = xs_replace(content, "\r", "");
3059            xs *poll_opts = NULL;
3060
3061            /* is there a valid set of poll options? */
3062            const char *v = xs_dict_get(p_vars, "poll_options");
3063            if (!xs_is_null(v) && *v) {
3064                xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
3065
3066                poll_opts = xs_split(v2, "\n");
3067            }
3068
3069            if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
3070                /* get the rest of poll configuration */
3071                const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
3072                const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
3073                int multiple = 0;
3074
3075                int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
3076
3077                if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
3078                    multiple = 1;
3079
3080                msg = msg_question(&snac, content_2, attach_list,
3081                                   poll_opts, multiple, end_secs);
3082
3083                enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
3084            }
3085            else
3086                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
3087
3088            if (sensitive != NULL) {
3089                msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
3090                msg = xs_dict_set(msg, "summary",   xs_is_null(summary) ? "..." : summary);
3091            }
3092
3093            if (xs_is_null(edit_id)) {
3094                /* new message */
3095                c_msg = msg_create(&snac, msg);
3096                timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3097            }
3098            else {
3099                /* an edition of a previous message */
3100                xs *p_msg = NULL;
3101
3102                if (valid_status(object_get(edit_id, &p_msg))) {
3103                    /* copy relevant fields from previous version */
3104                    char *fields[] = { "id", "context", "url", "published",
3105                                       "to", "inReplyTo", NULL };
3106                    int n;
3107
3108                    for (n = 0; fields[n]; n++) {
3109                        const char *v = xs_dict_get(p_msg, fields[n]);
3110                        msg = xs_dict_set(msg, fields[n], v);
3111                    }
3112
3113                    /* set the updated field */
3114                    xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
3115                    msg = xs_dict_set(msg, "updated", updated);
3116
3117                    /* overwrite object, not updating the indexes */
3118                    object_add_ow(edit_id, msg);
3119
3120                    /* update message */
3121                    c_msg = msg_update(&snac, msg);
3122                }
3123                else
3124                    snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
3125            }
3126
3127            if (c_msg != NULL)
3128                enqueue_message(&snac, c_msg);
3129
3130            history_del(&snac, "timeline.html_");
3131        }
3132
3133        status = HTTP_STATUS_SEE_OTHER;
3134    }
3135    else
3136    if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
3137        /* action on an entry */
3138        const char *id     = xs_dict_get(p_vars, "id");
3139        const char *actor  = xs_dict_get(p_vars, "actor");
3140        const char *action = xs_dict_get(p_vars, "action");
3141        const char *group  = xs_dict_get(p_vars, "group");
3142
3143        if (action == NULL)
3144            return HTTP_STATUS_NOT_FOUND;
3145
3146        snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
3147
3148        status = HTTP_STATUS_SEE_OTHER;
3149
3150        if (strcmp(action, L("Like")) == 0) { /** **/
3151            xs *msg = msg_admiration(&snac, id, "Like");
3152
3153            if (msg != NULL) {
3154                enqueue_message(&snac, msg);
3155                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
3156            }
3157        }
3158        else
3159        if (strcmp(action, L("Boost")) == 0) { /** **/
3160            xs *msg = msg_admiration(&snac, id, "Announce");
3161
3162            if (msg != NULL) {
3163                enqueue_message(&snac, msg);
3164                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
3165            }
3166        }
3167        else
3168        if (strcmp(action, L("Unlike")) == 0) { /** **/
3169            xs *msg = msg_repulsion(&snac, id, "Like");
3170
3171            if (msg != NULL) {
3172                enqueue_message(&snac, msg);
3173            }
3174        }
3175        else
3176        if (strcmp(action, L("Unboost")) == 0) { /** **/
3177            xs *msg = msg_repulsion(&snac, id, "Announce");
3178
3179            if (msg != NULL) {
3180                enqueue_message(&snac, msg);
3181            }
3182        }
3183        else
3184        if (strcmp(action, L("MUTE")) == 0) { /** **/
3185            mute(&snac, actor);
3186        }
3187        else
3188        if (strcmp(action, L("Unmute")) == 0) { /** **/
3189            unmute(&snac, actor);
3190        }
3191        else
3192        if (strcmp(action, L("Hide")) == 0) { /** **/
3193            hide(&snac, id);
3194        }
3195        else
3196        if (strcmp(action, L("Limit")) == 0) { /** **/
3197            limit(&snac, actor);
3198        }
3199        else
3200        if (strcmp(action, L("Unlimit")) == 0) { /** **/
3201            unlimit(&snac, actor);
3202        }
3203        else
3204        if (strcmp(action, L("Follow")) == 0) { /** **/
3205            xs *msg = msg_follow(&snac, actor);
3206
3207            if (msg != NULL) {
3208                /* reload the actor from the message, in may be different */
3209                actor = xs_dict_get(msg, "object");
3210
3211                following_add(&snac, actor, msg);
3212
3213                enqueue_output_by_actor(&snac, msg, actor, 0);
3214            }
3215        }
3216        else
3217        if (strcmp(action, L("Unfollow")) == 0) { /** **/
3218            /* get the following object */
3219            xs *object = NULL;
3220
3221            if (valid_status(following_get(&snac, actor, &object))) {
3222                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3223
3224                following_del(&snac, actor);
3225
3226                enqueue_output_by_actor(&snac, msg, actor, 0);
3227
3228                snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
3229            }
3230            else
3231                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3232        }
3233        else
3234        if (strcmp(action, L("Follow Group")) == 0) { /** **/
3235            xs *msg = msg_follow(&snac, group);
3236
3237            if (msg != NULL) {
3238                /* reload the group from the message, in may be different */
3239                group = xs_dict_get(msg, "object");
3240
3241                following_add(&snac, group, msg);
3242
3243                enqueue_output_by_actor(&snac, msg, group, 0);
3244            }
3245        }
3246        else
3247        if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
3248            /* get the following object */
3249            xs *object = NULL;
3250
3251            if (valid_status(following_get(&snac, group, &object))) {
3252                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3253
3254                following_del(&snac, group);
3255
3256                enqueue_output_by_actor(&snac, msg, group, 0);
3257
3258                snac_log(&snac, xs_fmt("unfollowed group %s", group));
3259            }
3260            else
3261                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3262        }
3263        else
3264        if (strcmp(action, L("Delete")) == 0) { /** **/
3265            const char *actor_form = xs_dict_get(p_vars, "actor-form");
3266            if (actor_form != NULL) {
3267                /* delete follower */
3268                if (valid_status(follower_del(&snac, actor)))
3269                    snac_log(&snac, xs_fmt("deleted follower %s", actor));
3270                else
3271                    snac_log(&snac, xs_fmt("error deleting follower %s", actor));
3272            }
3273            else {
3274                /* delete an entry */
3275                if (xs_startswith(id, snac.actor)) {
3276                    /* it's a post by us: generate a delete */
3277                    xs *msg = msg_delete(&snac, id);
3278
3279                    enqueue_message(&snac, msg);
3280
3281                    snac_log(&snac, xs_fmt("posted tombstone for %s", id));
3282                }
3283
3284                timeline_del(&snac, id);
3285
3286                snac_log(&snac, xs_fmt("deleted entry %s", id));
3287            }
3288        }
3289        else
3290        if (strcmp(action, L("Pin")) == 0) { /** **/
3291            pin(&snac, id);
3292            timeline_touch(&snac);
3293        }
3294        else
3295        if (strcmp(action, L("Unpin")) == 0) { /** **/
3296            unpin(&snac, id);
3297            timeline_touch(&snac);
3298        }
3299        else
3300            status = HTTP_STATUS_NOT_FOUND;
3301
3302        /* delete the cached timeline */
3303        if (status == HTTP_STATUS_SEE_OTHER)
3304            history_del(&snac, "timeline.html_");
3305    }
3306    else
3307    if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
3308        /* change of user data */
3309        const char *v;
3310        const char *p1, *p2;
3311
3312        if ((v = xs_dict_get(p_vars, "name")) != NULL)
3313            snac.config = xs_dict_set(snac.config, "name", v);
3314        if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
3315            snac.config = xs_dict_set(snac.config, "avatar", v);
3316        if ((v = xs_dict_get(p_vars, "bio")) != NULL)
3317            snac.config = xs_dict_set(snac.config, "bio", v);
3318        if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
3319            strcmp(v, "on") == 0) {
3320            snac.config = xs_dict_set(snac.config, "cw", "open");
3321        } else { /* if the checkbox is not set, the parameter is missing */
3322            snac.config = xs_dict_set(snac.config, "cw", "");
3323        }
3324        if ((v = xs_dict_get(p_vars, "email")) != NULL)
3325            snac.config = xs_dict_set(snac.config, "email", v);
3326        if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
3327            snac.config = xs_dict_set(snac.config, "telegram_bot", v);
3328        if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
3329            snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
3330        if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
3331            snac.config = xs_dict_set(snac.config, "ntfy_server", v);
3332        if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
3333            snac.config = xs_dict_set(snac.config, "ntfy_token", v);
3334        if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
3335            xs *days    = xs_number_new(atof(v));
3336            snac.config = xs_dict_set(snac.config, "purge_days", days);
3337        }
3338        if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
3339            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
3340        else
3341            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
3342        if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
3343            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
3344        else
3345            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
3346        if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
3347            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
3348        else
3349            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
3350        if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
3351            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
3352        else
3353            snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
3354
3355        if ((v = xs_dict_get(p_vars, "metadata")) != NULL) {
3356            /* split the metadata and store it as a dict */
3357            xs_dict *md = xs_dict_new();
3358            xs *l = xs_split(v, "\n");
3359            xs_list *p = l;
3360            const xs_str *kp;
3361
3362            while (xs_list_iter(&p, &kp)) {
3363                xs *kpl = xs_split_n(kp, "=", 1);
3364                if (xs_list_len(kpl) == 2) {
3365                    xs *k2 = xs_strip_i(xs_dup(xs_list_get(kpl, 0)));
3366                    xs *v2 = xs_strip_i(xs_dup(xs_list_get(kpl, 1)));
3367
3368                    md = xs_dict_set(md, k2, v2);
3369                }
3370            }
3371
3372            snac.config = xs_dict_set(snac.config, "metadata", md);
3373        }
3374
3375        /* uploads */
3376        const char *uploads[] = { "avatar", "header", NULL };
3377        int n;
3378        for (n = 0; uploads[n]; n++) {
3379            xs *var_name = xs_fmt("%s_file", uploads[n]);
3380
3381            const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
3382            if (xs_type(uploaded_file) == XSTYPE_LIST) {
3383                const char *fn = xs_list_get(uploaded_file, 0);
3384
3385                if (fn && *fn) {
3386                    const char *mimetype = xs_mime_by_ext(fn);
3387
3388                    if (xs_startswith(mimetype, "image/")) {
3389                        const char *ext = strrchr(fn, '.');
3390                        xs *hash        = xs_md5_hex(fn, strlen(fn));
3391                        xs *id          = xs_fmt("%s%s", hash, ext);
3392                        xs *url         = xs_fmt("%s/s/%s", snac.actor, id);
3393                        int fo          = xs_number_get(xs_list_get(uploaded_file, 1));
3394                        int fs          = xs_number_get(xs_list_get(uploaded_file, 2));
3395
3396                        /* store */
3397                        static_put(&snac, id, payload + fo, fs);
3398
3399                        snac.config = xs_dict_set(snac.config, uploads[n], url);
3400                    }
3401                }
3402            }
3403        }
3404
3405        /* delete images by removing url from user.json */
3406        for (n = 0; uploads[n]; n++) {
3407            xs *var_name = xs_fmt("%s_delete", uploads[n]);
3408            const char *delete_var = xs_dict_get(p_vars, var_name);
3409
3410            if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
3411                    snac.config = xs_dict_set(snac.config, uploads[n], "");
3412            }
3413        }
3414
3415        /* password change? */
3416        if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
3417            (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
3418            *p1 && strcmp(p1, p2) == 0) {
3419            xs *pw = hash_password(snac.uid, p1, NULL);
3420            snac.config = xs_dict_set(snac.config, "passwd", pw);
3421        }
3422
3423        user_persist(&snac, 1);
3424
3425        status = HTTP_STATUS_SEE_OTHER;
3426    }
3427    else
3428    if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
3429        notify_clear(&snac);
3430        timeline_touch(&snac);
3431
3432        status = HTTP_STATUS_SEE_OTHER;
3433    }
3434    else
3435    if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
3436        const char *irt   = xs_dict_get(p_vars, "irt");
3437        const char *opt   = xs_dict_get(p_vars, "question");
3438        const char *actor = xs_dict_get(p_vars, "actor");
3439
3440        xs *ls = NULL;
3441
3442        /* multiple choices? */
3443        if (xs_type(opt) == XSTYPE_LIST)
3444            ls = xs_dup(opt);
3445        else {
3446            ls = xs_list_new();
3447            ls = xs_list_append(ls, opt);
3448        }
3449
3450        xs_list *p = ls;
3451        const xs_str *v;
3452
3453        while (xs_list_iter(&p, &v)) {
3454            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
3455
3456            /* set the option */
3457            msg = xs_dict_append(msg, "name", v);
3458
3459            xs *c_msg = msg_create(&snac, msg);
3460
3461            enqueue_message(&snac, c_msg);
3462
3463            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3464        }
3465
3466        {
3467            /* get the poll object */
3468            xs *poll = NULL;
3469
3470            if (valid_status(object_get(irt, &poll))) {
3471                const char *date = xs_dict_get(poll, "endTime");
3472                if (xs_is_null(date))
3473                    date = xs_dict_get(poll, "closed");
3474
3475                if (!xs_is_null(date)) {
3476                    time_t t = xs_parse_iso_date(date, 0) - time(NULL);
3477
3478                    /* request the poll when it's closed;
3479                       Pleroma does not send and update when the poll closes */
3480                    enqueue_object_request(&snac, irt, t + 2);
3481                }
3482            }
3483        }
3484
3485        status = HTTP_STATUS_SEE_OTHER;
3486    }
3487
3488    if (status == HTTP_STATUS_SEE_OTHER) {
3489        const char *redir = xs_dict_get(p_vars, "redir");
3490
3491        if (xs_is_null(redir))
3492            redir = "top";
3493
3494        *body   = xs_fmt("%s/admin#%s", snac.actor, redir);
3495        *b_size = strlen(*body);
3496    }
3497
3498    user_free(&snac);
3499
3500    return status;
3501}
3502
3503
3504xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc)
3505/* converts a timeline to rss */
3506{
3507    xs_html *rss = xs_html_tag("rss",
3508        xs_html_attr("version", "0.91"));
3509
3510    xs_html *channel = xs_html_tag("channel",
3511        xs_html_tag("title",
3512            xs_html_text(title)),
3513        xs_html_tag("language",
3514            xs_html_text("en")),
3515        xs_html_tag("link",
3516            xs_html_text(link)),
3517        xs_html_tag("description",
3518            xs_html_text(desc)));
3519
3520    xs_html_add(rss, channel);
3521
3522    int c = 0;
3523    const char *v;
3524
3525    while (xs_list_next(timeline, &v, &c)) {
3526        xs *msg = NULL;
3527
3528        if (user) {
3529            if (!valid_status(timeline_get_by_md5(user, v, &msg)))
3530                continue;
3531        }
3532        else {
3533            if (!valid_status(object_get_by_md5(v, &msg)))
3534                continue;
3535        }
3536
3537        const char *id = xs_dict_get(msg, "id");
3538        const char *content = xs_dict_get(msg, "content");
3539
3540        if (user && !xs_startswith(id, user->actor))
3541            continue;
3542
3543        /* create a title with the first line of the content */
3544        xs *title = xs_replace(content, "<br>", "\n");
3545        title = xs_regex_replace_i(title, "<[^>]+>", " ");
3546        title = xs_regex_replace_i(title, "&[^;]+;", " ");
3547        int i;
3548
3549        for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
3550
3551        if (title[i] != '\0') {
3552            title[i] = '\0';
3553            title = xs_str_cat(title, "...");
3554        }
3555
3556        title = xs_strip_i(title);
3557
3558        xs_html_add(channel,
3559            xs_html_tag("item",
3560                xs_html_tag("title",
3561                    xs_html_text(title)),
3562                xs_html_tag("link",
3563                    xs_html_text(id)),
3564                xs_html_tag("description",
3565                    xs_html_text(content))));
3566    }
3567
3568    return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
3569}