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