2.52 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; vertical-align: middle;", 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, "Note|Question|Page|Article|Video"))
 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(char *tag)
 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    {
 564        xs *l = tag ? xs_fmt(L("Search results for #%s"), tag) :
 565            xs_dup(L("Recent posts by users in this instance"));
 566
 567        xs_html_add(body,
 568            xs_html_tag("h2",
 569                xs_html_attr("class", "snac-header"),
 570                xs_html_text(l)));
 571    }
 572
 573    return body;
 574}
 575
 576
 577xs_html *html_user_head(snac *user, char *desc, char *url)
 578{
 579    xs_html *head = html_base_head();
 580
 581    /* add the user CSS */
 582    {
 583        xs *css = NULL;
 584        int size;
 585
 586        /* try to open the user css */
 587        if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
 588            /* it's not there; try to open the server-wide css */
 589            FILE *f;
 590            xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
 591
 592            if ((f = fopen(g_css_fn, "r")) != NULL) {
 593                css = xs_readall(f);
 594                fclose(f);
 595            }
 596        }
 597
 598        if (css != NULL) {
 599            xs_html_add(head,
 600                xs_html_tag("style",
 601                    xs_html_raw(css)));
 602        }
 603    }
 604
 605    /* title */
 606    xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
 607        user->uid, xs_dict_get(srv_config, "host"));
 608
 609    xs_html_add(head,
 610        xs_html_tag("title",
 611            xs_html_text(title)));
 612
 613    xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
 614
 615    if (avatar == NULL || *avatar == '\0') {
 616        xs_free(avatar);
 617        avatar = xs_fmt("%s/susie.png", srv_baseurl);
 618    }
 619
 620    /* create a description field */
 621    xs *s_desc = NULL;
 622    int n;
 623
 624    if (desc == NULL)
 625        s_desc = xs_dup(xs_dict_get(user->config, "bio"));
 626    else
 627        s_desc = xs_dup(desc);
 628
 629    /* shorten desc to a reasonable size */
 630    for (n = 0; s_desc[n]; n++) {
 631        if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
 632            break;
 633    }
 634
 635    s_desc[n] = '\0';
 636
 637    /* og properties */
 638    xs_html_add(head,
 639        xs_html_sctag("meta",
 640            xs_html_attr("property", "og:site_name"),
 641            xs_html_attr("content", xs_dict_get(srv_config, "host"))),
 642        xs_html_sctag("meta",
 643            xs_html_attr("property", "og:title"),
 644            xs_html_attr("content", title)),
 645        xs_html_sctag("meta",
 646            xs_html_attr("property", "og:description"),
 647            xs_html_attr("content", s_desc)),
 648        xs_html_sctag("meta",
 649            xs_html_attr("property", "og:image"),
 650            xs_html_attr("content", avatar)),
 651        xs_html_sctag("meta",
 652            xs_html_attr("property", "og:width"),
 653            xs_html_attr("content", "300")),
 654        xs_html_sctag("meta",
 655            xs_html_attr("property", "og:height"),
 656            xs_html_attr("content", "300")));
 657
 658    /* RSS link */
 659    xs *rss_url = xs_fmt("%s.rss", user->actor);
 660    xs_html_add(head,
 661        xs_html_sctag("link",
 662            xs_html_attr("rel", "alternate"),
 663            xs_html_attr("type", "application/rss+xml"),
 664            xs_html_attr("title", "RSS"),
 665            xs_html_attr("href", rss_url)));
 666
 667    /* ActivityPub alternate link (actor id) */
 668    xs_html_add(head,
 669        xs_html_sctag("link",
 670            xs_html_attr("rel", "alternate"),
 671            xs_html_attr("type", "application/activity+json"),
 672            xs_html_attr("href", url ? url : user->actor)));
 673
 674    return head;
 675}
 676
 677
 678static xs_html *html_user_body(snac *user, int read_only)
 679{
 680    xs_html *body = xs_html_tag("body", NULL);
 681
 682    /* top nav */
 683    xs_html *top_nav = xs_html_tag("nav",
 684        xs_html_attr("class", "snac-top-nav"));
 685
 686    xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
 687
 688    if (avatar == NULL || *avatar == '\0') {
 689        xs_free(avatar);
 690        avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
 691    }
 692
 693    xs_html_add(top_nav,
 694        xs_html_sctag("img",
 695            xs_html_attr("src", avatar),
 696            xs_html_attr("class", "snac-avatar"),
 697            xs_html_attr("alt", "")));
 698
 699    if (read_only) {
 700        xs *rss_url = xs_fmt("%s.rss", user->actor);
 701        xs *admin_url = xs_fmt("%s/admin", user->actor);
 702
 703        xs_html_add(top_nav,
 704            xs_html_tag("a",
 705                xs_html_attr("href", rss_url),
 706                xs_html_text(L("RSS"))),
 707            xs_html_text(" - "),
 708            xs_html_tag("a",
 709                xs_html_attr("href", admin_url),
 710                xs_html_attr("rel", "nofollow"),
 711                xs_html_text(L("private"))));
 712    }
 713    else {
 714        int n_len = notify_new_num(user);
 715        xs_html *notify_count = NULL;
 716
 717        /* show the number of new notifications, if there are any */
 718        if (n_len) {
 719            xs *n_len_str = xs_fmt(" %d ", n_len);
 720            notify_count = xs_html_tag("sup",
 721                xs_html_attr("style", "background-color: red; color: white;"),
 722                xs_html_text(n_len_str));
 723        }
 724        else
 725            notify_count = xs_html_text("");
 726
 727        xs *admin_url = xs_fmt("%s/admin", user->actor);
 728        xs *notify_url = xs_fmt("%s/notifications", user->actor);
 729        xs *people_url = xs_fmt("%s/people", user->actor);
 730        xs *instance_url = xs_fmt("%s/instance", user->actor);
 731
 732        xs_html_add(top_nav,
 733            xs_html_tag("a",
 734                xs_html_attr("href", user->actor),
 735                xs_html_text(L("public"))),
 736            xs_html_text(" - "),
 737            xs_html_tag("a",
 738                xs_html_attr("href", admin_url),
 739                xs_html_text(L("private"))),
 740            xs_html_text(" - "),
 741            xs_html_tag("a",
 742                xs_html_attr("href", notify_url),
 743                xs_html_text(L("notifications"))),
 744            notify_count,
 745            xs_html_text(" - "),
 746            xs_html_tag("a",
 747                xs_html_attr("href", people_url),
 748                xs_html_text(L("people"))),
 749            xs_html_text(" - "),
 750            xs_html_tag("a",
 751                xs_html_attr("href", instance_url),
 752                xs_html_text(L("instance"))));
 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 (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) {
1269            /* not already boosted or us; 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
1378    /* do not show non-public messages in the public timeline */
1379    if ((read_only || !user) && !is_msg_public(msg))
1380        return NULL;
1381
1382    /* hidden? do nothing more for this conversation */
1383    if (user && is_hidden(user, id)) {
1384        xs *s1 = xs_fmt("%s_entry", md5);
1385
1386        /* return just an dummy anchor, to keep position after hitting 'Hide' */
1387        return xs_html_tag("div",
1388            xs_html_tag("a",
1389                xs_html_attr("name", s1)));
1390    }
1391
1392    /* avoid too deep nesting, as it may be a loop */
1393    if (level >= MAX_CONVERSATION_LEVELS)
1394        return xs_html_tag("mark",
1395            xs_html_text(L("Truncated (too deep)")));
1396
1397    if (strcmp(type, "Follow") == 0) {
1398        return xs_html_tag("div",
1399            xs_html_attr("class", "snac-post"),
1400            xs_html_tag("div",
1401                xs_html_attr("class", "snac-post-header"),
1402                xs_html_tag("div",
1403                    xs_html_attr("class", "snac-origin"),
1404                    xs_html_text(L("follows you"))),
1405                html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg)));
1406    }
1407    else
1408    if (!xs_match(type, "Note|Question|Page|Article|Video")) {
1409        /* skip oddities */
1410        return NULL;
1411    }
1412
1413    /* ignore notes with "name", as they are votes to Questions */
1414    if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
1415        return NULL;
1416
1417    /* get the attributedTo */
1418    if ((actor = get_atto(msg)) == NULL)
1419        return NULL;
1420
1421    /* ignore muted morons immediately */
1422    if (user && is_muted(user, actor)) {
1423        xs *s1 = xs_fmt("%s_entry", md5);
1424
1425        /* return just an dummy anchor, to keep position after hitting 'MUTE' */
1426        return xs_html_tag("div",
1427            xs_html_tag("a",
1428                xs_html_attr("name", s1)));
1429    }
1430
1431    if ((user == NULL || strcmp(actor, user->actor) != 0)
1432        && !valid_status(actor_get(actor, NULL)))
1433        return NULL;
1434
1435    /** html_entry top tag **/
1436    xs_html *entry_top = xs_html_tag("div", NULL);
1437
1438    {
1439        xs *s1 = xs_fmt("%s_entry", md5);
1440        xs_html_add(entry_top,
1441            xs_html_tag("a",
1442                xs_html_attr("name", s1)));
1443    }
1444
1445    xs_html *entry = xs_html_tag("div",
1446        xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
1447
1448    xs_html_add(entry_top,
1449        entry);
1450
1451    /** post header **/
1452
1453    xs_html *score;
1454    xs_html *post_header = xs_html_tag("div",
1455        xs_html_attr("class", "snac-post-header"),
1456        score = xs_html_tag("div",
1457            xs_html_attr("class", "snac-score")));
1458
1459    xs_html_add(entry,
1460        post_header);
1461
1462    if (user && is_pinned(user, id)) {
1463        /* add a pin emoji */
1464        xs_html_add(score,
1465            xs_html_tag("span",
1466                xs_html_attr("title", L("Pinned")),
1467                xs_html_raw(" &#128204; ")));
1468    }
1469
1470    if (strcmp(type, "Question") == 0) {
1471        /* add the ballot box emoji */
1472        xs_html_add(score,
1473            xs_html_tag("span",
1474                xs_html_attr("title", L("Poll")),
1475                xs_html_raw(" &#128499; ")));
1476
1477        if (user && was_question_voted(user, id)) {
1478            /* add a check to show this poll was voted */
1479            xs_html_add(score,
1480                xs_html_tag("span",
1481                    xs_html_attr("title", L("Voted")),
1482                    xs_html_raw(" &#10003; ")));
1483        }
1484    }
1485
1486    /* if it's a user from this same instance, add the score */
1487    if (xs_startswith(id, srv_baseurl)) {
1488        int n_likes  = object_likes_len(id);
1489        int n_boosts = object_announces_len(id);
1490
1491        /* alternate emojis: %d &#128077; %d &#128257; */
1492        xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
1493
1494        xs_html_add(score,
1495            xs_html_raw(s1));
1496    }
1497
1498    xs *boosts = object_announces(id);
1499
1500    if (xs_list_len(boosts)) {
1501        /* if somebody boosted this, show as origin */
1502        char *p = xs_list_get(boosts, -1);
1503        xs *actor_r = NULL;
1504
1505        if (user && xs_list_in(boosts, user->md5) != -1) {
1506            /* we boosted this */
1507            xs_html_add(post_header,
1508                xs_html_tag("div",
1509                    xs_html_attr("class", "snac-origin"),
1510                    xs_html_tag("a",
1511                        xs_html_attr("href", user->actor),
1512                        xs_html_text(xs_dict_get(user->config, "name"))),
1513                        xs_html_text(" "),
1514                        xs_html_text(L("boosted"))));
1515        }
1516        else
1517        if (valid_status(object_get_by_md5(p, &actor_r))) {
1518            xs *name = actor_name(actor_r);
1519
1520            if (!xs_is_null(name)) {
1521                xs *href = NULL;
1522                char *id = xs_dict_get(actor_r, "id");
1523                int fwers = 0;
1524                int fwing = 0;
1525
1526                if (user != NULL) {
1527                    fwers = follower_check(user, id);
1528                    fwing = following_check(user, id);
1529                }
1530
1531                if (!read_only && (fwers || fwing))
1532                    href = xs_fmt("%s/people#%s", user->actor, p);
1533                else
1534                    href = xs_dup(id);
1535
1536                xs_html_add(post_header,
1537                    xs_html_tag("div",
1538                        xs_html_attr("class", "snac-origin"),
1539                        xs_html_tag("a",
1540                            xs_html_attr("href", href),
1541                            xs_html_raw(name)), /* already sanitized */
1542                            xs_html_text(" "),
1543                            xs_html_text(L("boosted"))));
1544            }
1545        }
1546    }
1547    else
1548    if (strcmp(type, "Note") == 0) {
1549        if (level == 0) {
1550            /* is the parent not here? */
1551            char *parent = xs_dict_get(msg, "inReplyTo");
1552
1553            if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
1554                xs_html_add(post_header,
1555                    xs_html_tag("div",
1556                        xs_html_attr("class", "snac-origin"),
1557                        xs_html_text(L("in reply to")),
1558                        xs_html_text(" "),
1559                        xs_html_tag("a",
1560                            xs_html_attr("href", parent),
1561                            xs_html_text("ยป"))));
1562            }
1563        }
1564    }
1565
1566    xs_html_add(post_header,
1567        html_msg_icon(read_only ? NULL : user, actor, msg));
1568
1569    /** post content **/
1570
1571    xs_html *snac_content_wrap = xs_html_tag("div",
1572        xs_html_attr("class", "e-content snac-content"));
1573
1574    xs_html_add(entry,
1575        snac_content_wrap);
1576
1577    if (!xs_is_null(v = xs_dict_get(msg, "name"))) {
1578        xs_html_add(snac_content_wrap,
1579            xs_html_tag("h3",
1580                xs_html_attr("class", "snac-entry-title"),
1581                xs_html_text(v)));
1582    }
1583
1584    xs_html *snac_content = NULL;
1585
1586    v = xs_dict_get(msg, "summary");
1587
1588    /* is it sensitive? */
1589    if (user && xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
1590        if (xs_is_null(v) || *v == '\0')
1591            v = "...";
1592
1593        /* only show it when not in the public timeline and the config setting is "open" */
1594        char *cw = xs_dict_get(user->config, "cw");
1595        if (xs_is_null(cw) || read_only)
1596            cw = "";
1597
1598        snac_content = xs_html_tag("details",
1599            xs_html_attr(cw, NULL),
1600            xs_html_tag("summary",
1601                xs_html_text(v),
1602                xs_html_text(L(" [SENSITIVE CONTENT]"))));
1603    }
1604    else {
1605        /* print the summary as a header (sites like e.g. Friendica can contain one) */
1606        if (!xs_is_null(v) && *v)
1607            xs_html_add(snac_content_wrap,
1608                xs_html_tag("h3",
1609                    xs_html_attr("class", "snac-entry-title"),
1610                    xs_html_text(v)));
1611
1612        snac_content = xs_html_tag("div", NULL);
1613    }
1614
1615    xs_html_add(snac_content_wrap,
1616        snac_content);
1617
1618    {
1619        /** build the content string **/
1620        char *content = xs_dict_get(msg, "content");
1621
1622        xs *c = sanitize(xs_is_null(content) ? "" : content);
1623
1624        /* do some tweaks to the content */
1625        c = xs_replace_i(c, "\r", "");
1626
1627        while (xs_endswith(c, "<br><br>"))
1628            c = xs_crop_i(c, 0, -4);
1629
1630        c = xs_replace_i(c, "<br><br>", "<p>");
1631
1632        c = xs_str_cat(c, "<p>");
1633
1634        /* replace the :shortnames: */
1635        c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2);
1636
1637        /* Peertube videos content is in markdown */
1638        char *mtype = xs_dict_get(msg, "mediaType");
1639        if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
1640            /* a full conversion could be better */
1641            c = xs_replace_i(c, "\r", "");
1642            c = xs_replace_i(c, "\n", "<br>");
1643        }
1644
1645        /* c contains sanitized HTML */
1646        xs_html_add(snac_content,
1647            xs_html_raw(c));
1648    }
1649
1650    if (strcmp(type, "Question") == 0) { /** question content **/
1651        xs_list *oo = xs_dict_get(msg, "oneOf");
1652        xs_list *ao = xs_dict_get(msg, "anyOf");
1653        xs_list *p;
1654        xs_dict *v;
1655        int closed = 0;
1656        char *f_closed = NULL;
1657
1658        xs_html *poll = xs_html_tag("div", NULL);
1659
1660        if (read_only)
1661            closed = 1; /* non-identified page; show as closed */
1662        else
1663        if (user && xs_startswith(id, user->actor))
1664            closed = 1; /* we questioned; closed for us */
1665        else
1666        if (user && was_question_voted(user, id))
1667            closed = 1; /* we already voted; closed for us */
1668
1669        if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
1670            /* it has a closed date... but is it in the past? */
1671            time_t t0 = time(NULL);
1672            time_t t1 = xs_parse_iso_date(f_closed, 0);
1673
1674            if (t1 < t0)
1675                closed = 2;
1676        }
1677
1678        /* get the appropriate list of options */
1679        p = oo != NULL ? oo : ao;
1680
1681        if (closed || user == NULL) {
1682            /* closed poll */
1683            xs_html *poll_result = xs_html_tag("table",
1684                xs_html_attr("class", "snac-poll-result"));
1685
1686            while (xs_list_iter(&p, &v)) {
1687                char *name       = xs_dict_get(v, "name");
1688                xs_dict *replies = xs_dict_get(v, "replies");
1689
1690                if (name && replies) {
1691                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1692
1693                    xs_html_add(poll_result,
1694                        xs_html_tag("tr",
1695                            xs_html_tag("td",
1696                                xs_html_text(name),
1697                                xs_html_text(":")),
1698                            xs_html_tag("td",
1699                                xs_html_text(ti))));
1700                }
1701            }
1702
1703            xs_html_add(poll,
1704                poll_result);
1705        }
1706        else {
1707            /* poll still active */
1708            xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
1709            xs_html *form;
1710            xs_html *poll_form = xs_html_tag("div",
1711                xs_html_attr("class", "snac-poll-form"),
1712                form = xs_html_tag("form",
1713                    xs_html_attr("autocomplete", "off"),
1714                    xs_html_attr("method", "post"),
1715                    xs_html_attr("action", vote_action),
1716                    xs_html_sctag("input",
1717                        xs_html_attr("type", "hidden"),
1718                        xs_html_attr("name", "actor"),
1719                        xs_html_attr("value", actor)),
1720                    xs_html_sctag("input",
1721                        xs_html_attr("type", "hidden"),
1722                        xs_html_attr("name", "irt"),
1723                        xs_html_attr("value", id))));
1724
1725            while (xs_list_iter(&p, &v)) {
1726                char *name = xs_dict_get(v, "name");
1727                xs_dict *replies = xs_dict_get(v, "replies");
1728
1729                if (name) {
1730                    char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
1731
1732                    xs_html_add(form,
1733                        xs_html_sctag("input",
1734                            xs_html_attr("type", !xs_is_null(oo) ? "radio" : "checkbox"),
1735                            xs_html_attr("id", name),
1736                            xs_html_attr("value", name),
1737                            xs_html_attr("name", "question")),
1738                        xs_html_text(" "),
1739                        xs_html_tag("span",
1740                            xs_html_attr("title", ti),
1741                            xs_html_text(name)),
1742                        xs_html_sctag("br", NULL));
1743                }
1744            }
1745
1746            xs_html_add(form,
1747                xs_html_tag("p", NULL),
1748                xs_html_sctag("input",
1749                    xs_html_attr("type", "submit"),
1750                    xs_html_attr("class", "button"),
1751                    xs_html_attr("value", L("Vote"))));
1752
1753            xs_html_add(poll,
1754                poll_form);
1755        }
1756
1757        /* if it's *really* closed, say it */
1758        if (closed == 2) {
1759            xs_html_add(poll,
1760                xs_html_tag("p",
1761                    xs_html_text(L("Closed"))));
1762        }
1763        else {
1764            /* show when the poll closes */
1765            char *end_time = xs_dict_get(msg, "endTime");
1766
1767            /* Pleroma does not have an endTime field;
1768               it has a closed time in the future */
1769            if (xs_is_null(end_time))
1770                end_time = xs_dict_get(msg, "closed");
1771
1772            if (!xs_is_null(end_time)) {
1773                time_t t0 = time(NULL);
1774                time_t t1 = xs_parse_iso_date(end_time, 0);
1775
1776                if (t1 > 0 && t1 > t0) {
1777                    time_t diff_time = t1 - t0;
1778                    xs *tf = xs_str_time_diff(diff_time);
1779                    char *p = tf;
1780
1781                    /* skip leading zeros */
1782                    for (; *p == '0' || *p == ':'; p++);
1783
1784                    xs_html_add(poll,
1785                        xs_html_tag("p",
1786                            xs_html_text(L("Closes in")),
1787                            xs_html_text(" "),
1788                            xs_html_text(p)));
1789                }
1790            }
1791        }
1792
1793        xs_html_add(snac_content,
1794            poll);
1795    }
1796
1797    /** attachments **/
1798    xs *attach = get_attachments(msg);
1799
1800    {
1801        /* make custom css for attachments easier */
1802        xs_html *content_attachments = xs_html_tag("div",
1803            xs_html_attr("class", "snac-content-attachments"));
1804
1805        xs_html_add(snac_content,
1806            content_attachments);
1807
1808        xs_list *p = attach;
1809
1810        while (xs_list_iter(&p, &v)) {
1811            char *type = xs_dict_get(v, "type");
1812            char *href = xs_dict_get(v, "href");
1813            char *name = xs_dict_get(v, "name");
1814
1815            if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
1816                xs_html_add(content_attachments,
1817                    xs_html_tag("a",
1818                        xs_html_attr("href", href),
1819                        xs_html_attr("target", "_blank"),
1820                        xs_html_sctag("img",
1821                            xs_html_attr("loading", "lazy"),
1822                            xs_html_attr("src", href),
1823                            xs_html_attr("alt", name),
1824                            xs_html_attr("title", name))));
1825            }
1826            else
1827            if (xs_startswith(type, "video/")) {
1828                xs_html_add(content_attachments,
1829                    xs_html_tag("video",
1830                        xs_html_attr("preload", "none"),
1831                        xs_html_attr("style", "width: 100%"),
1832                        xs_html_attr("class", "snac-embedded-video"),
1833                        xs_html_attr("controls", NULL),
1834                        xs_html_attr("src", href),
1835                        xs_html_text(L("Video")),
1836                        xs_html_text(": "),
1837                        xs_html_tag("a",
1838                            xs_html_attr("href", href),
1839                            xs_html_text(name))));
1840            }
1841            else
1842            if (xs_startswith(type, "audio/")) {
1843                xs_html_add(content_attachments,
1844                    xs_html_tag("audio",
1845                        xs_html_attr("preload", "none"),
1846                        xs_html_attr("style", "width: 100%"),
1847                        xs_html_attr("class", "snac-embedded-audio"),
1848                        xs_html_attr("controls", NULL),
1849                        xs_html_attr("src", href),
1850                        xs_html_text(L("Audio")),
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 (strcmp(type, "Link") == 0) {
1858                xs_html_add(content_attachments,
1859                    xs_html_tag("p",
1860                        xs_html_tag("a",
1861                            xs_html_attr("href", href),
1862                            xs_html_text(href))));
1863            }
1864            else {
1865                xs_html_add(content_attachments,
1866                    xs_html_tag("p",
1867                        xs_html_tag("a",
1868                            xs_html_attr("href", href),
1869                            xs_html_text(L("Attachment")),
1870                            xs_html_text(": "),
1871                            xs_html_text(href))));
1872            }
1873        }
1874    }
1875
1876    /* has this message an audience (i.e., comes from a channel or community)? */
1877    char *audience = xs_dict_get(msg, "audience");
1878    if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
1879        xs_html *au_tag = xs_html_tag("p",
1880            xs_html_text("("),
1881            xs_html_tag("a",
1882                xs_html_attr("href", audience),
1883                xs_html_attr("title", L("Source channel or community")),
1884                xs_html_text(audience)),
1885            xs_html_text(")"));
1886
1887        xs_html_add(snac_content_wrap,
1888            au_tag);
1889    }
1890
1891    /** controls **/
1892
1893    if (!read_only && user) {
1894        xs_html_add(entry,
1895            html_entry_controls(user, actor, msg, md5));
1896    }
1897
1898    /** children **/
1899    if (!hide_children) {
1900        xs *children = object_children(id);
1901        int left     = xs_list_len(children);
1902
1903        if (left) {
1904            xs_html *ch_details = xs_html_tag("details",
1905                xs_html_attr("open", NULL),
1906                xs_html_tag("summary",
1907                    xs_html_text("...")));
1908
1909            xs_html_add(entry,
1910                ch_details);
1911
1912            xs_html *ch_container = xs_html_tag("div",
1913                xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
1914
1915            xs_html_add(ch_details,
1916                ch_container);
1917
1918            xs_html *ch_older = NULL;
1919            if (left > 3) {
1920                xs_html_add(ch_container,
1921                    ch_older = xs_html_tag("details",
1922                        xs_html_tag("summary",
1923                            xs_html_text(L("Older...")))));
1924            }
1925
1926            xs_list *p = children;
1927            char *cmd5;
1928            int cnt = 0;
1929            int o_cnt = 0;
1930
1931            while (xs_list_iter(&p, &cmd5)) {
1932                xs *chd = NULL;
1933
1934                if (user)
1935                    timeline_get_by_md5(user, cmd5, &chd);
1936                else
1937                    object_get_by_md5(cmd5, &chd);
1938
1939                if (chd != NULL) {
1940                    if (xs_is_null(xs_dict_get(chd, "name"))) {
1941                        xs_html *che = html_entry(user, chd, read_only,
1942                            level + 1, cmd5, hide_children);
1943
1944                        if (che != NULL) {
1945                            if (left > 3) {
1946                                xs_html_add(ch_older,
1947                                    che);
1948
1949                                o_cnt++;
1950                            }
1951                            else
1952                                xs_html_add(ch_container,
1953                                    che);
1954
1955                            cnt++;
1956                        }
1957                    }
1958
1959                    left--;
1960                }
1961                else
1962                    srv_debug(2, xs_fmt("cannot read child %s", cmd5));
1963            }
1964
1965            /* if no children were finally added, hide the details */
1966            if (cnt == 0)
1967                xs_html_add(ch_details,
1968                    xs_html_attr("style", "display: none"));
1969
1970            if (o_cnt == 0 && ch_older)
1971                xs_html_add(ch_older,
1972                    xs_html_attr("style", "display: none"));
1973        }
1974    }
1975
1976    return entry_top;
1977}
1978
1979
1980xs_html *html_footer(void)
1981{
1982    return xs_html_tag("div",
1983        xs_html_attr("class", "snac-footer"),
1984        xs_html_tag("a",
1985            xs_html_attr("href", srv_baseurl),
1986            xs_html_text(L("about this site"))),
1987        xs_html_text(" - "),
1988        xs_html_text(L("powered by ")),
1989        xs_html_tag("a",
1990            xs_html_attr("href", WHAT_IS_SNAC_URL),
1991            xs_html_tag("abbr",
1992                xs_html_attr("title", "Social Network Are Crap"),
1993                xs_html_text("snac"))));
1994}
1995
1996
1997xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
1998                      int skip, int show, int show_more,
1999                      char *tag, char *page, int utl)
2000/* returns the HTML for the timeline */
2001{
2002    xs_list *p = (xs_list *)list;
2003    char *v;
2004    double t = ftime();
2005
2006    xs *desc = NULL;
2007    xs *alternate = NULL;
2008
2009    if (xs_list_len(list) == 1) {
2010        /* only one element? pick the description from the source */
2011        char *id = xs_list_get(list, 0);
2012        xs *d = NULL;
2013        object_get_by_md5(id, &d);
2014        if (d && (v = xs_dict_get(d, "sourceContent")) != NULL)
2015            desc = xs_dup(v);
2016
2017        alternate = xs_dup(xs_dict_get(d, "id"));
2018    }
2019
2020    xs_html *head;
2021    xs_html *body;
2022
2023    if (user) {
2024        head = html_user_head(user, desc, alternate);
2025        body = html_user_body(user, read_only);
2026    }
2027    else {
2028        head = html_instance_head();
2029        body = html_instance_body(tag);
2030    }
2031
2032    xs_html *html = xs_html_tag("html",
2033        head,
2034        body);
2035
2036    if (user && !read_only)
2037        xs_html_add(body,
2038            html_top_controls(user));
2039
2040    xs_html_add(body,
2041        xs_html_tag("a",
2042            xs_html_attr("name", "snac-posts")));
2043
2044    xs_html *posts = xs_html_tag("div",
2045        xs_html_attr("class", "snac-posts"));
2046
2047    xs_html_add(body,
2048        posts);
2049
2050    while (xs_list_iter(&p, &v)) {
2051        xs *msg = NULL;
2052        int status;
2053
2054        if (utl && user && !is_pinned_by_md5(user, v))
2055            status = timeline_get_by_md5(user, v, &msg);
2056        else
2057            status = object_get_by_md5(v, &msg);
2058
2059        if (!valid_status(status))
2060            continue;
2061
2062        /* if it's an instance page, discard messages from private users */
2063        if (user == NULL && is_msg_from_private_user(msg))
2064            continue;
2065
2066        /* is this message a non-public reply? */
2067        if (user != NULL && !is_msg_public(msg)) {
2068            char *irt = xs_dict_get(msg, "inReplyTo");
2069
2070            /* is it a reply to something not in the storage? */
2071            if (!xs_is_null(irt) && !object_here(irt)) {
2072                /* is it for me? */
2073                xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
2074                xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
2075
2076                if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
2077                    snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
2078                    continue;
2079                }
2080            }
2081        }
2082
2083        xs_html *entry = html_entry(user, msg, read_only, 0, v, user ? 0 : 1);
2084
2085        if (entry != NULL)
2086            xs_html_add(posts,
2087                entry);
2088    }
2089
2090    if (list && user && read_only) {
2091        /** history **/
2092        if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
2093            xs_html *ul = xs_html_tag("ul", NULL);
2094
2095            xs_html *history = xs_html_tag("div",
2096                xs_html_attr("class", "snac-history"),
2097                xs_html_tag("p",
2098                    xs_html_attr("class", "snac-history-title"),
2099                    xs_html_text(L("History"))),
2100                    ul);
2101
2102            xs *list = history_list(user);
2103            xs_list *p = list;
2104            char *v;
2105
2106            while (xs_list_iter(&p, &v)) {
2107                xs *fn  = xs_replace(v, ".html", "");
2108                xs *url = xs_fmt("%s/h/%s", user->actor, v);
2109
2110                xs_html_add(ul,
2111                    xs_html_tag("li",
2112                        xs_html_tag("a",
2113                            xs_html_attr("href", url),
2114                            xs_html_text(fn))));
2115            }
2116
2117            xs_html_add(body,
2118                history);
2119        }
2120    }
2121
2122    {
2123        xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
2124        xs_html_add(body,
2125            xs_html_raw(s1));
2126    }
2127
2128    if (show_more) {
2129        xs *t  = NULL;
2130        xs *m  = NULL;
2131        xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
2132
2133        xs *url = page == NULL || user == NULL ?
2134            xs_dup(srv_baseurl) : xs_fmt("%s%s", user->actor, page);
2135
2136        if (tag) {
2137            t = xs_fmt("%s?t=%s", url, tag);
2138            m = xs_fmt("%s&%s", t, ss);
2139        }
2140        else {
2141            t = xs_dup(url);
2142            m = xs_fmt("%s?%s", t, ss);
2143        }
2144
2145        xs_html *more_links = xs_html_tag("p",
2146            xs_html_tag("a",
2147                xs_html_attr("href", t),
2148                xs_html_attr("name", "snac-more"),
2149                xs_html_text(L("Back to top"))),
2150            xs_html_text(" - "),
2151            xs_html_tag("a",
2152                xs_html_attr("href", m),
2153                xs_html_attr("name", "snac-more"),
2154                xs_html_text(L("More..."))));
2155
2156        xs_html_add(body,
2157            more_links);
2158    }
2159
2160    xs_html_add(body,
2161        html_footer());
2162
2163    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2164}
2165
2166
2167xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t)
2168{
2169    xs_html *snac_posts;
2170    xs_html *people = xs_html_tag("div",
2171        xs_html_tag("h2",
2172            xs_html_attr("class", "snac-header"),
2173            xs_html_text(header)),
2174        snac_posts = xs_html_tag("details",
2175                xs_html_attr("open", NULL),
2176                xs_html_tag("summary",
2177                    xs_html_text("..."))));
2178
2179    xs_list *p = list;
2180    char *actor_id;
2181
2182    while (xs_list_iter(&p, &actor_id)) {
2183        xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
2184        xs *actor = NULL;
2185
2186        if (valid_status(actor_get(actor_id, &actor))) {
2187            xs_html *snac_post = xs_html_tag("div",
2188                xs_html_attr("class", "snac-post"),
2189                xs_html_tag("a",
2190                    xs_html_attr("name", md5)),
2191                xs_html_tag("div",
2192                    xs_html_attr("class", "snac-post-header"),
2193                    html_actor_icon(snac, actor, xs_dict_get(actor, "published"), NULL, NULL, 0, 1)));
2194
2195            /* content (user bio) */
2196            char *c = xs_dict_get(actor, "summary");
2197
2198            if (!xs_is_null(c)) {
2199                xs *sc = sanitize(c);
2200
2201                xs_html *snac_content = xs_html_tag("div",
2202                    xs_html_attr("class", "snac-content"));
2203
2204                if (xs_startswith(sc, "<p>"))
2205                    xs_html_add(snac_content,
2206                        xs_html_raw(sc)); /* already sanitized */
2207                else
2208                    xs_html_add(snac_content,
2209                        xs_html_tag("p",
2210                            xs_html_raw(sc))); /* already sanitized */
2211
2212                xs_html_add(snac_post, snac_content);
2213            }
2214
2215            /* buttons */
2216            xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
2217
2218            xs_html *snac_controls = xs_html_tag("div",
2219                xs_html_attr("class",   "snac-controls"));
2220
2221            xs_html *form = xs_html_tag("form",
2222                xs_html_attr("autocomplete",    "off"),
2223                xs_html_attr("method",          "post"),
2224                xs_html_attr("action",          btn_form_action),
2225                xs_html_sctag("input",
2226                    xs_html_attr("type",    "hidden"),
2227                    xs_html_attr("name",    "actor"),
2228                    xs_html_attr("value",   actor_id)),
2229                xs_html_sctag("input",
2230                    xs_html_attr("type",    "hidden"),
2231                    xs_html_attr("name",    "actor-form"),
2232                    xs_html_attr("value",   "yes")));
2233
2234            xs_html_add(snac_controls, form);
2235
2236            if (following_check(snac, actor_id)) {
2237                xs_html_add(form,
2238                    html_button("unfollow", L("Unfollow"),
2239                                L("Stop following this user's activity")));
2240
2241                if (is_limited(snac, actor_id))
2242                    xs_html_add(form,
2243                        html_button("unlimit", L("Unlimit"),
2244                                L("Allow announces (boosts) from this user")));
2245                else
2246                    xs_html_add(form,
2247                        html_button("limit", L("Limit"),
2248                                L("Block announces (boosts) from this user")));
2249            }
2250            else {
2251                xs_html_add(form,
2252                    html_button("follow", L("Follow"),
2253                                L("Start following this user's activity")));
2254
2255                if (follower_check(snac, actor_id))
2256                    xs_html_add(form,
2257                        html_button("delete", L("Delete"), L("Delete this user")));
2258            }
2259
2260            if (is_muted(snac, actor_id))
2261                xs_html_add(form,
2262                    html_button("unmute", L("Unmute"),
2263                                L("Stop blocking activities from this user")));
2264            else
2265                xs_html_add(form,
2266                    html_button("mute", L("MUTE"),
2267                                L("Block any activity from this user")));
2268
2269            /* the post textarea */
2270            xs *dm_div_id  = xs_fmt("%s_%s_dm", md5, t);
2271            xs *dm_form_id = xs_fmt("%s_reply_form", md5);
2272
2273            xs_html_add(snac_controls,
2274                xs_html_tag("p", NULL),
2275                html_note(snac, L("Direct Message..."),
2276                    dm_div_id, dm_form_id,
2277                    "", "",
2278                    NULL, actor_id,
2279                    xs_stock(XSTYPE_FALSE), "",
2280                    xs_stock(XSTYPE_FALSE), NULL,
2281                    NULL, 0, "", ""),
2282                xs_html_tag("p", NULL));
2283
2284            xs_html_add(snac_post, snac_controls);
2285
2286            xs_html_add(snac_posts, snac_post);
2287        }
2288    }
2289
2290    return people;
2291}
2292
2293
2294xs_str *html_people(snac *user)
2295{
2296    xs *wing = following_list(user);
2297    xs *wers = follower_list(user);
2298
2299    xs_html *html = xs_html_tag("html",
2300        html_user_head(user, NULL, NULL),
2301        xs_html_add(html_user_body(user, 0),
2302            xs_html_tag("div",
2303                xs_html_attr("class", "snac-posts"),
2304                html_people_list(user, wing, L("People you follow"), "i"),
2305                html_people_list(user, wers, L("People that follow you"), "e")),
2306            html_footer()));
2307
2308    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2309}
2310
2311
2312xs_str *html_notifications(snac *user, int skip, int show)
2313{
2314    xs *n_list = notify_list(user, skip, show);
2315    xs *n_time = notify_check_time(user, 0);
2316
2317    xs_html *body = html_user_body(user, 0);
2318
2319    xs_html *html = xs_html_tag("html",
2320        html_user_head(user, NULL, NULL),
2321        body);
2322
2323    xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
2324
2325    xs_html_add(body,
2326        xs_html_tag("form",
2327            xs_html_attr("autocomplete", "off"),
2328            xs_html_attr("method",       "post"),
2329            xs_html_attr("action",       clear_all_action),
2330            xs_html_attr("id",           "clear"),
2331            xs_html_sctag("input",
2332                xs_html_attr("type",     "submit"),
2333                xs_html_attr("class",    "snac-btn-like"),
2334                xs_html_attr("value",    L("Clear all")))));
2335
2336    xs_html *noti_new = NULL;
2337    xs_html *noti_seen = NULL;
2338
2339    xs_list *p = n_list;
2340    xs_str *v;
2341    while (xs_list_iter(&p, &v)) {
2342        xs *noti = notify_get(user, v);
2343
2344        if (noti == NULL)
2345            continue;
2346
2347        xs *obj = NULL;
2348        char *type  = xs_dict_get(noti, "type");
2349        char *utype = xs_dict_get(noti, "utype");
2350        char *id    = xs_dict_get(noti, "objid");
2351        char *date  = xs_dict_get(noti, "date");
2352
2353        if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
2354            continue;
2355
2356        if (is_hidden(user, id))
2357            continue;
2358
2359        char *actor_id = xs_dict_get(noti, "actor");
2360        xs *actor = NULL;
2361
2362        if (!valid_status(actor_get(actor_id, &actor)))
2363            continue;
2364
2365        xs *a_name = actor_name(actor);
2366        char *label = type;
2367
2368        if (strcmp(type, "Create") == 0)
2369            label = L("Mention");
2370        else
2371        if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
2372            label = L("Finished poll");
2373        else
2374        if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
2375            label = L("Unfollow");
2376
2377        xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
2378
2379        xs_html *entry = xs_html_tag("div",
2380            xs_html_attr("class", "snac-post-with-desc"),
2381            xs_html_tag("p",
2382                xs_html_tag("b",
2383                    xs_html_text(label),
2384                    xs_html_text(" by "),
2385                    xs_html_tag("a",
2386                        xs_html_attr("href", actor_id),
2387                        xs_html_raw(a_name))), /* a_name is already sanitized */
2388                xs_html_text(" "),
2389                xs_html_tag("time",
2390                    xs_html_attr("class", "dt-published snac-pubdate"),
2391                    xs_html_attr("title", date),
2392                    xs_html_text(s_date))));
2393
2394        if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
2395            xs_html_add(entry,
2396                xs_html_tag("div",
2397                    xs_html_attr("class", "snac-post"),
2398                    html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0)));
2399        }
2400        else {
2401            xs *md5 = xs_md5_hex(id, strlen(id));
2402
2403            xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
2404
2405            if (h != NULL) {
2406                xs_html_add(entry,
2407                    h);
2408            }
2409        }
2410
2411        if (strcmp(v, n_time) > 0) {
2412            /* unseen notification */
2413            if (noti_new == NULL) {
2414                noti_new = xs_html_tag("div",
2415                    xs_html_tag("h2",
2416                        xs_html_attr("class", "snac-header"),
2417                        xs_html_text(L("New"))));
2418
2419                xs_html_add(body,
2420                    noti_new);
2421            }
2422
2423            xs_html_add(noti_new,
2424                entry);
2425        }
2426        else {
2427            /* already seen notification */
2428            if (noti_seen == NULL) {
2429                noti_seen = xs_html_tag("div",
2430                    xs_html_tag("h2",
2431                        xs_html_attr("class", "snac-header"),
2432                        xs_html_text(L("Already seen"))));
2433
2434                xs_html_add(body,
2435                    noti_seen);
2436            }
2437
2438            xs_html_add(noti_seen,
2439                entry);
2440        }
2441    }
2442
2443    if (noti_new == NULL && noti_seen == NULL)
2444        xs_html_add(body,
2445            xs_html_tag("h2",
2446                xs_html_attr("class", "snac-header"),
2447                xs_html_text(L("None"))));
2448
2449    /* add the navigation footer */
2450    xs *next_p = notify_list(user, skip + show, 1);
2451    if (xs_list_len(next_p)) {
2452        xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
2453            user->actor, skip + show, show);
2454
2455        xs_html_add(body,
2456            xs_html_tag("p",
2457                xs_html_tag("a",
2458                    xs_html_attr("href", url),
2459                    xs_html_text(L("More...")))));
2460    }
2461
2462    xs_html_add(body,
2463        html_footer());
2464
2465    /* set the check time to now */
2466    xs *dummy = notify_check_time(user, 1);
2467    dummy = xs_free(dummy);
2468
2469    timeline_touch(user);
2470
2471    return xs_html_render_s(html, "<!DOCTYPE html>\n");
2472}
2473
2474
2475int html_get_handler(const xs_dict *req, const char *q_path,
2476                     char **body, int *b_size, char **ctype, xs_str **etag)
2477{
2478    char *accept = xs_dict_get(req, "accept");
2479    int status = 404;
2480    snac snac;
2481    xs *uid = NULL;
2482    char *p_path;
2483    int cache = 1;
2484    int save = 1;
2485    char *v;
2486
2487    xs *l = xs_split_n(q_path, "/", 2);
2488    v = xs_list_get(l, 1);
2489
2490    if (xs_is_null(v)) {
2491        srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
2492        return 404;
2493    }
2494
2495    uid = xs_dup(v);
2496
2497    /* rss extension? */
2498    if (xs_endswith(uid, ".rss")) {
2499        uid = xs_crop_i(uid, 0, -4);
2500        p_path = ".rss";
2501    }
2502    else
2503        p_path = xs_list_get(l, 2);
2504
2505    if (!uid || !user_open(&snac, uid)) {
2506        /* invalid user */
2507        srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
2508        return 404;
2509    }
2510
2511    /* return the RSS if requested by Accept header */
2512    if (accept != NULL) {
2513        if (xs_str_in(accept, "text/xml") != -1 ||
2514            xs_str_in(accept, "application/rss+xml") != -1)
2515            p_path = ".rss";
2516    }
2517
2518    /* check if server config variable 'disable_cache' is set */
2519    if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
2520        cache = 0;
2521
2522    int skip = 0;
2523    int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
2524    char *q_vars = xs_dict_get(req, "q_vars");
2525    if ((v = xs_dict_get(q_vars, "skip")) != NULL)
2526        skip = atoi(v), cache = 0, save = 0;
2527    if ((v = xs_dict_get(q_vars, "show")) != NULL)
2528        show = atoi(v), cache = 0, save = 0;
2529
2530    if (p_path == NULL) { /** public timeline **/
2531        xs *h = xs_str_localtime(0, "%Y-%m.html");
2532
2533        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
2534            /** empty public timeline for private users **/
2535            *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1);
2536            *b_size = strlen(*body);
2537            status  = 200;
2538        }
2539        else
2540        if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
2541            snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
2542
2543            status = history_get(&snac, h, body, b_size,
2544                        xs_dict_get(req, "if-none-match"), etag);
2545        }
2546        else {
2547            xs *list = timeline_list(&snac, "public", skip, show);
2548            xs *next = timeline_list(&snac, "public", skip + show, 1);
2549
2550            xs *pins = pinned_list(&snac);
2551            pins = xs_list_cat(pins, list);
2552
2553            *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1);
2554
2555            *b_size = strlen(*body);
2556            status  = 200;
2557
2558            if (save)
2559                history_add(&snac, h, *body, *b_size, etag);
2560        }
2561    }
2562    else
2563    if (strcmp(p_path, "admin") == 0) { /** private timeline **/
2564        if (!login(&snac, req)) {
2565            *body  = xs_dup(uid);
2566            status = 401;
2567        }
2568        else {
2569            double t = history_mtime(&snac, "timeline.html_");
2570
2571            /* if enabled by admin, return a cached page if its timestamp is:
2572               a) newer than the timeline timestamp
2573               b) newer than the start time of the server
2574            */
2575            if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
2576                snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
2577
2578                status = history_get(&snac, "timeline.html_", body, b_size,
2579                            xs_dict_get(req, "if-none-match"), etag);
2580            }
2581            else {
2582                snac_debug(&snac, 1, xs_fmt("building timeline"));
2583
2584                xs *list = timeline_list(&snac, "private", skip, show);
2585                xs *next = timeline_list(&snac, "private", skip + show, 1);
2586
2587                xs *pins = pinned_list(&snac);
2588                pins = xs_list_cat(pins, list);
2589
2590                *body = html_timeline(&snac, pins, 0, skip, show,
2591                        xs_list_len(next), NULL, "/admin", 1);
2592
2593                *b_size = strlen(*body);
2594                status  = 200;
2595
2596                if (save)
2597                    history_add(&snac, "timeline.html_", *body, *b_size, etag);
2598            }
2599        }
2600    }
2601    else
2602    if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
2603        if (!login(&snac, req)) {
2604            *body  = xs_dup(uid);
2605            status = 401;
2606        }
2607        else {
2608            xs *l = xs_split(p_path, "/");
2609            char *md5 = xs_list_get(l, -1);
2610
2611            if (md5 && *md5 && timeline_here(&snac, md5)) {
2612                xs *list = xs_list_append(xs_list_new(), md5);
2613
2614                *body   = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1);
2615                *b_size = strlen(*body);
2616                status  = 200;
2617            }
2618        }
2619    }
2620    else
2621    if (strcmp(p_path, "people") == 0) { /** the list of people **/
2622        if (!login(&snac, req)) {
2623            *body  = xs_dup(uid);
2624            status = 401;
2625        }
2626        else {
2627            *body   = html_people(&snac);
2628            *b_size = strlen(*body);
2629            status  = 200;
2630        }
2631    }
2632    else
2633    if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
2634        if (!login(&snac, req)) {
2635            *body  = xs_dup(uid);
2636            status = 401;
2637        }
2638        else {
2639            *body   = html_notifications(&snac, skip, show);
2640            *b_size = strlen(*body);
2641            status  = 200;
2642        }
2643    }
2644    else
2645    if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
2646        if (!login(&snac, req)) {
2647            *body  = xs_dup(uid);
2648            status = 401;
2649        }
2650        else {
2651            xs *list = timeline_instance_list(skip, show);
2652            xs *next = timeline_instance_list(skip + show, 1);
2653
2654            *body = html_timeline(&snac, list, 0, skip, show,
2655                xs_list_len(next), NULL, "/instance", 0);
2656            *b_size = strlen(*body);
2657            status  = 200;
2658        }
2659    }
2660    else
2661    if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
2662        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2663            return 403;
2664
2665        xs *id  = xs_fmt("%s/%s", snac.actor, p_path);
2666        xs *msg = NULL;
2667
2668        if (valid_status(object_get(id, &msg))) {
2669            xs *md5  = xs_md5_hex(id, strlen(id));
2670            xs *list = xs_list_new();
2671
2672            list = xs_list_append(list, md5);
2673
2674            *body   = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1);
2675            *b_size = strlen(*body);
2676            status  = 200;
2677        }
2678    }
2679    else
2680    if (xs_startswith(p_path, "s/")) { /** a static file **/
2681        xs *l    = xs_split(p_path, "/");
2682        char *id = xs_list_get(l, 1);
2683        int sz;
2684
2685        if (id && *id) {
2686            status = static_get(&snac, id, body, &sz,
2687                        xs_dict_get(req, "if-none-match"), etag);
2688
2689            if (valid_status(status)) {
2690                *b_size = sz;
2691                *ctype  = (char *)xs_mime_by_ext(id);
2692            }
2693        }
2694    }
2695    else
2696    if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
2697        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2698            return 403;
2699
2700        if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
2701            return 403;
2702
2703        xs *l    = xs_split(p_path, "/");
2704        char *id = xs_list_get(l, 1);
2705
2706        if (id && *id) {
2707            if (xs_endswith(id, "timeline.html_")) {
2708                /* Don't let them in */
2709                *b_size = 0;
2710                status = 404;
2711            }
2712            else
2713                status = history_get(&snac, id, body, b_size,
2714                            xs_dict_get(req, "if-none-match"), etag);
2715        }
2716    }
2717    else
2718    if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
2719        if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
2720            return 403;
2721
2722        xs *elems = timeline_simple_list(&snac, "public", 0, 20);
2723        xs *bio   = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL, NULL);
2724
2725        xs *rss_title = xs_fmt("%s (@%s@%s)",
2726            xs_dict_get(snac.config, "name"),
2727            snac.uid,
2728            xs_dict_get(srv_config, "host"));
2729        xs *rss_link = xs_fmt("%s.rss", snac.actor);
2730
2731        *body   = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
2732        *b_size = strlen(*body);
2733        *ctype  = "application/rss+xml; charset=utf-8";
2734        status  = 200;
2735
2736        snac_debug(&snac, 1, xs_fmt("serving RSS"));
2737    }
2738    else
2739        status = 404;
2740
2741    user_free(&snac);
2742
2743    if (valid_status(status) && *ctype == NULL) {
2744        *ctype = "text/html; charset=utf-8";
2745    }
2746
2747    return status;
2748}
2749
2750
2751int html_post_handler(const xs_dict *req, const char *q_path,
2752                      char *payload, int p_size,
2753                      char **body, int *b_size, char **ctype)
2754{
2755    (void)p_size;
2756    (void)ctype;
2757
2758    int status = 0;
2759    snac snac;
2760    char *uid, *p_path;
2761    xs_dict *p_vars;
2762
2763    xs *l = xs_split_n(q_path, "/", 2);
2764
2765    uid = xs_list_get(l, 1);
2766    if (!uid || !user_open(&snac, uid)) {
2767        /* invalid user */
2768        srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
2769        return 404;
2770    }
2771
2772    p_path = xs_list_get(l, 2);
2773
2774    /* all posts must be authenticated */
2775    if (!login(&snac, req)) {
2776        user_free(&snac);
2777        *body  = xs_dup(uid);
2778        return 401;
2779    }
2780
2781    p_vars = xs_dict_get(req, "p_vars");
2782
2783#if 0
2784    xs_json_dump(p_vars, 4, stdout);
2785#endif
2786
2787    if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
2788        /* post note */
2789        xs_str *content      = xs_dict_get(p_vars, "content");
2790        xs_str *in_reply_to  = xs_dict_get(p_vars, "in_reply_to");
2791        xs_str *attach_url   = xs_dict_get(p_vars, "attach_url");
2792        xs_list *attach_file = xs_dict_get(p_vars, "attach");
2793        xs_str *to           = xs_dict_get(p_vars, "to");
2794        xs_str *sensitive    = xs_dict_get(p_vars, "sensitive");
2795        xs_str *summary      = xs_dict_get(p_vars, "summary");
2796        xs_str *edit_id      = xs_dict_get(p_vars, "edit_id");
2797        xs_str *alt_text     = xs_dict_get(p_vars, "alt_text");
2798        int priv             = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
2799        xs *attach_list      = xs_list_new();
2800
2801        /* default alt text */
2802        if (xs_is_null(alt_text))
2803            alt_text = "";
2804
2805        /* is attach_url set? */
2806        if (!xs_is_null(attach_url) && *attach_url != '\0') {
2807            xs *l = xs_list_new();
2808
2809            l = xs_list_append(l, attach_url);
2810            l = xs_list_append(l, alt_text);
2811
2812            attach_list = xs_list_append(attach_list, l);
2813        }
2814
2815        /* is attach_file set? */
2816        if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
2817            char *fn = xs_list_get(attach_file, 0);
2818
2819            if (*fn != '\0') {
2820                char *ext = strrchr(fn, '.');
2821                xs *hash  = xs_md5_hex(fn, strlen(fn));
2822                xs *id    = xs_fmt("%s%s", hash, ext);
2823                xs *url   = xs_fmt("%s/s/%s", snac.actor, id);
2824                int fo    = xs_number_get(xs_list_get(attach_file, 1));
2825                int fs    = xs_number_get(xs_list_get(attach_file, 2));
2826
2827                /* store */
2828                static_put(&snac, id, payload + fo, fs);
2829
2830                xs *l = xs_list_new();
2831
2832                l = xs_list_append(l, url);
2833                l = xs_list_append(l, alt_text);
2834
2835                attach_list = xs_list_append(attach_list, l);
2836            }
2837        }
2838
2839        if (content != NULL) {
2840            xs *msg       = NULL;
2841            xs *c_msg     = NULL;
2842            xs *content_2 = xs_replace(content, "\r", "");
2843            xs *poll_opts = NULL;
2844
2845            /* is there a valid set of poll options? */
2846            const char *v = xs_dict_get(p_vars, "poll_options");
2847            if (!xs_is_null(v) && *v) {
2848                xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
2849
2850                poll_opts = xs_split(v2, "\n");
2851            }
2852
2853            if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
2854                /* get the rest of poll configuration */
2855                const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
2856                const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
2857                int multiple = 0;
2858
2859                int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
2860
2861                if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
2862                    multiple = 1;
2863
2864                msg = msg_question(&snac, content_2, attach_list,
2865                                   poll_opts, multiple, end_secs);
2866
2867                enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
2868            }
2869            else
2870                msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
2871
2872            if (sensitive != NULL) {
2873                msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
2874                msg = xs_dict_set(msg, "summary",   xs_is_null(summary) ? "..." : summary);
2875            }
2876
2877            if (xs_is_null(edit_id)) {
2878                /* new message */
2879                c_msg = msg_create(&snac, msg);
2880                timeline_add(&snac, xs_dict_get(msg, "id"), msg);
2881            }
2882            else {
2883                /* an edition of a previous message */
2884                xs *p_msg = NULL;
2885
2886                if (valid_status(object_get(edit_id, &p_msg))) {
2887                    /* copy relevant fields from previous version */
2888                    char *fields[] = { "id", "context", "url", "published",
2889                                       "to", "inReplyTo", NULL };
2890                    int n;
2891
2892                    for (n = 0; fields[n]; n++) {
2893                        char *v = xs_dict_get(p_msg, fields[n]);
2894                        msg = xs_dict_set(msg, fields[n], v);
2895                    }
2896
2897                    /* set the updated field */
2898                    xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
2899                    msg = xs_dict_set(msg, "updated", updated);
2900
2901                    /* overwrite object, not updating the indexes */
2902                    object_add_ow(edit_id, msg);
2903
2904                    /* update message */
2905                    c_msg = msg_update(&snac, msg);
2906                }
2907                else
2908                    snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
2909            }
2910
2911            if (c_msg != NULL)
2912                enqueue_message(&snac, c_msg);
2913
2914            history_del(&snac, "timeline.html_");
2915        }
2916
2917        status = 303;
2918    }
2919    else
2920    if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
2921        /* action on an entry */
2922        char *id     = xs_dict_get(p_vars, "id");
2923        char *actor  = xs_dict_get(p_vars, "actor");
2924        char *action = xs_dict_get(p_vars, "action");
2925        char *group  = xs_dict_get(p_vars, "group");
2926
2927        if (action == NULL)
2928            return 404;
2929
2930        snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
2931
2932        status = 303;
2933
2934        if (strcmp(action, L("Like")) == 0) { /** **/
2935            xs *msg = msg_admiration(&snac, id, "Like");
2936
2937            if (msg != NULL) {
2938                enqueue_message(&snac, msg);
2939                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
2940            }
2941        }
2942        else
2943        if (strcmp(action, L("Boost")) == 0) { /** **/
2944            xs *msg = msg_admiration(&snac, id, "Announce");
2945
2946            if (msg != NULL) {
2947                enqueue_message(&snac, msg);
2948                timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
2949            }
2950        }
2951        else
2952        if (strcmp(action, L("Unlike")) == 0) { /** **/
2953            xs *msg = msg_repulsion(&snac, id, "Like");
2954
2955            if (msg != NULL) {
2956                enqueue_message(&snac, msg);
2957            }
2958        }
2959        else
2960        if (strcmp(action, L("Unboost")) == 0) { /** **/
2961            xs *msg = msg_repulsion(&snac, id, "Announce");
2962
2963            if (msg != NULL) {
2964                enqueue_message(&snac, msg);
2965            }
2966        }
2967        else
2968        if (strcmp(action, L("MUTE")) == 0) { /** **/
2969            mute(&snac, actor);
2970        }
2971        else
2972        if (strcmp(action, L("Unmute")) == 0) { /** **/
2973            unmute(&snac, actor);
2974        }
2975        else
2976        if (strcmp(action, L("Hide")) == 0) { /** **/
2977            hide(&snac, id);
2978        }
2979        else
2980        if (strcmp(action, L("Limit")) == 0) { /** **/
2981            limit(&snac, actor);
2982        }
2983        else
2984        if (strcmp(action, L("Unlimit")) == 0) { /** **/
2985            unlimit(&snac, actor);
2986        }
2987        else
2988        if (strcmp(action, L("Follow")) == 0) { /** **/
2989            xs *msg = msg_follow(&snac, actor);
2990
2991            if (msg != NULL) {
2992                /* reload the actor from the message, in may be different */
2993                actor = xs_dict_get(msg, "object");
2994
2995                following_add(&snac, actor, msg);
2996
2997                enqueue_output_by_actor(&snac, msg, actor, 0);
2998            }
2999        }
3000        else
3001        if (strcmp(action, L("Unfollow")) == 0) { /** **/
3002            /* get the following object */
3003            xs *object = NULL;
3004
3005            if (valid_status(following_get(&snac, actor, &object))) {
3006                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3007
3008                following_del(&snac, actor);
3009
3010                enqueue_output_by_actor(&snac, msg, actor, 0);
3011
3012                snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
3013            }
3014            else
3015                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3016        }
3017        else
3018        if (strcmp(action, L("Follow Group")) == 0) { /** **/
3019            xs *msg = msg_follow(&snac, group);
3020
3021            if (msg != NULL) {
3022                /* reload the group from the message, in may be different */
3023                group = xs_dict_get(msg, "object");
3024
3025                following_add(&snac, group, msg);
3026
3027                enqueue_output_by_actor(&snac, msg, group, 0);
3028            }
3029        }
3030        else
3031        if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
3032            /* get the following object */
3033            xs *object = NULL;
3034
3035            if (valid_status(following_get(&snac, group, &object))) {
3036                xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
3037
3038                following_del(&snac, group);
3039
3040                enqueue_output_by_actor(&snac, msg, group, 0);
3041
3042                snac_log(&snac, xs_fmt("unfollowed group %s", group));
3043            }
3044            else
3045                snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
3046        }
3047        else
3048        if (strcmp(action, L("Delete")) == 0) { /** **/
3049            char *actor_form = xs_dict_get(p_vars, "actor-form");
3050            if (actor_form != NULL) {
3051                /* delete follower */
3052                if (valid_status(follower_del(&snac, actor)))
3053                    snac_log(&snac, xs_fmt("deleted follower %s", actor));
3054                else
3055                    snac_log(&snac, xs_fmt("error deleting follower %s", actor));
3056            }
3057            else {
3058                /* delete an entry */
3059                if (xs_startswith(id, snac.actor)) {
3060                    /* it's a post by us: generate a delete */
3061                    xs *msg = msg_delete(&snac, id);
3062
3063                    enqueue_message(&snac, msg);
3064
3065                    snac_log(&snac, xs_fmt("posted tombstone for %s", id));
3066                }
3067
3068                timeline_del(&snac, id);
3069
3070                snac_log(&snac, xs_fmt("deleted entry %s", id));
3071            }
3072        }
3073        else
3074        if (strcmp(action, L("Pin")) == 0) { /** **/
3075            pin(&snac, id);
3076            timeline_touch(&snac);
3077        }
3078        else
3079        if (strcmp(action, L("Unpin")) == 0) { /** **/
3080            unpin(&snac, id);
3081            timeline_touch(&snac);
3082        }
3083        else
3084            status = 404;
3085
3086        /* delete the cached timeline */
3087        if (status == 303)
3088            history_del(&snac, "timeline.html_");
3089    }
3090    else
3091    if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
3092        /* change of user data */
3093        char *v;
3094        char *p1, *p2;
3095
3096        if ((v = xs_dict_get(p_vars, "name")) != NULL)
3097            snac.config = xs_dict_set(snac.config, "name", v);
3098        if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
3099            snac.config = xs_dict_set(snac.config, "avatar", v);
3100        if ((v = xs_dict_get(p_vars, "bio")) != NULL)
3101            snac.config = xs_dict_set(snac.config, "bio", v);
3102        if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
3103            strcmp(v, "on") == 0) {
3104            snac.config = xs_dict_set(snac.config, "cw", "open");
3105        } else { /* if the checkbox is not set, the parameter is missing */
3106            snac.config = xs_dict_set(snac.config, "cw", "");
3107        }
3108        if ((v = xs_dict_get(p_vars, "email")) != NULL)
3109            snac.config = xs_dict_set(snac.config, "email", v);
3110        if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
3111            snac.config = xs_dict_set(snac.config, "telegram_bot", v);
3112        if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
3113            snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
3114        if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
3115            snac.config = xs_dict_set(snac.config, "ntfy_server", v);
3116        if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
3117            snac.config = xs_dict_set(snac.config, "ntfy_token", v);
3118        if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
3119            xs *days    = xs_number_new(atof(v));
3120            snac.config = xs_dict_set(snac.config, "purge_days", days);
3121        }
3122        if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
3123            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
3124        else
3125            snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
3126        if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
3127            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
3128        else
3129            snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
3130        if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
3131            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
3132        else
3133            snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
3134        if ((v = xs_dict_get(p_vars, "metadata")) != NULL) {
3135            /* split the metadata and store it as a dict */
3136            xs_dict *md = xs_dict_new();
3137            xs *l = xs_split(v, "\n");
3138            xs_list *p = l;
3139            xs_str *kp;
3140
3141            while (xs_list_iter(&p, &kp)) {
3142                xs *kpl = xs_split_n(kp, "=", 1);
3143                if (xs_list_len(kpl) == 2) {
3144                    xs *k2 = xs_strip_i(xs_dup(xs_list_get(kpl, 0)));
3145                    xs *v2 = xs_strip_i(xs_dup(xs_list_get(kpl, 1)));
3146
3147                    md = xs_dict_set(md, k2, v2);
3148                }
3149            }
3150
3151            snac.config = xs_dict_set(snac.config, "metadata", md);
3152        }
3153
3154        /* uploads */
3155        const char *uploads[] = { "avatar", "header", NULL };
3156        int n;
3157        for (n = 0; uploads[n]; n++) {
3158            xs *var_name = xs_fmt("%s_file", uploads[n]);
3159
3160            xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
3161            if (xs_type(uploaded_file) == XSTYPE_LIST) {
3162                const char *fn = xs_list_get(uploaded_file, 0);
3163
3164                if (fn && *fn) {
3165                    const char *mimetype = xs_mime_by_ext(fn);
3166
3167                    if (xs_startswith(mimetype, "image/")) {
3168                        const char *ext = strrchr(fn, '.');
3169                        xs *hash        = xs_md5_hex(fn, strlen(fn));
3170                        xs *id          = xs_fmt("%s%s", hash, ext);
3171                        xs *url         = xs_fmt("%s/s/%s", snac.actor, id);
3172                        int fo          = xs_number_get(xs_list_get(uploaded_file, 1));
3173                        int fs          = xs_number_get(xs_list_get(uploaded_file, 2));
3174
3175                        /* store */
3176                        static_put(&snac, id, payload + fo, fs);
3177
3178                        snac.config = xs_dict_set(snac.config, uploads[n], url);
3179                    }
3180                }
3181            }
3182        }
3183
3184        /* password change? */
3185        if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
3186            (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
3187            *p1 && strcmp(p1, p2) == 0) {
3188            xs *pw = hash_password(snac.uid, p1, NULL);
3189            snac.config = xs_dict_set(snac.config, "passwd", pw);
3190        }
3191
3192        xs *fn  = xs_fmt("%s/user.json", snac.basedir);
3193        xs *bfn = xs_fmt("%s.bak", fn);
3194        FILE *f;
3195
3196        rename(fn, bfn);
3197
3198        if ((f = fopen(fn, "w")) != NULL) {
3199            xs_json_dump(snac.config, 4, f);
3200            fclose(f);
3201        }
3202        else
3203            rename(bfn, fn);
3204
3205        history_del(&snac, "timeline.html_");
3206
3207        xs *a_msg = msg_actor(&snac);
3208        xs *u_msg = msg_update(&snac, a_msg);
3209
3210        enqueue_message(&snac, u_msg);
3211
3212        enqueue_verify_links(&snac);
3213
3214        status = 303;
3215    }
3216    else
3217    if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
3218        notify_clear(&snac);
3219        timeline_touch(&snac);
3220
3221        status = 303;
3222    }
3223    else
3224    if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
3225        char *irt         = xs_dict_get(p_vars, "irt");
3226        const char *opt   = xs_dict_get(p_vars, "question");
3227        const char *actor = xs_dict_get(p_vars, "actor");
3228
3229        xs *ls = NULL;
3230
3231        /* multiple choices? */
3232        if (xs_type(opt) == XSTYPE_LIST)
3233            ls = xs_dup(opt);
3234        else {
3235            ls = xs_list_new();
3236            ls = xs_list_append(ls, opt);
3237        }
3238
3239        xs_list *p = ls;
3240        xs_str *v;
3241
3242        while (xs_list_iter(&p, &v)) {
3243            xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
3244
3245            /* set the option */
3246            msg = xs_dict_append(msg, "name", v);
3247
3248            xs *c_msg = msg_create(&snac, msg);
3249
3250            enqueue_message(&snac, c_msg);
3251
3252            timeline_add(&snac, xs_dict_get(msg, "id"), msg);
3253        }
3254
3255        {
3256            /* get the poll object */
3257            xs *poll = NULL;
3258
3259            if (valid_status(object_get(irt, &poll))) {
3260                char *date = xs_dict_get(poll, "endTime");
3261                if (xs_is_null(date))
3262                    date = xs_dict_get(poll, "closed");
3263
3264                if (!xs_is_null(date)) {
3265                    time_t t = xs_parse_iso_date(date, 0) - time(NULL);
3266
3267                    /* request the poll when it's closed;
3268                       Pleroma does not send and update when the poll closes */
3269                    enqueue_object_request(&snac, irt, t + 2);
3270                }
3271            }
3272        }
3273
3274        status = 303;
3275    }
3276
3277    if (status == 303) {
3278        char *redir = xs_dict_get(p_vars, "redir");
3279
3280        if (xs_is_null(redir))
3281            redir = "top";
3282
3283        *body   = xs_fmt("%s/admin#%s", snac.actor, redir);
3284        *b_size = strlen(*body);
3285    }
3286
3287    user_free(&snac);
3288
3289    return status;
3290}
3291
3292
3293xs_str *timeline_to_rss(snac *user, const xs_list *timeline, char *title, char *link, char *desc)
3294/* converts a timeline to rss */
3295{
3296    xs_html *rss = xs_html_tag("rss",
3297        xs_html_attr("version", "0.91"));
3298
3299    xs_html *channel = xs_html_tag("channel",
3300        xs_html_tag("title",
3301            xs_html_text(title)),
3302        xs_html_tag("language",
3303            xs_html_text("en")),
3304        xs_html_tag("link",
3305            xs_html_text(link)),
3306        xs_html_tag("description",
3307            xs_html_text(desc)));
3308
3309    xs_html_add(rss, channel);
3310
3311    int c = 0;
3312    char *v;
3313
3314    while (xs_list_next(timeline, &v, &c)) {
3315        xs *msg = NULL;
3316
3317        if (user) {
3318            if (!valid_status(timeline_get_by_md5(user, v, &msg)))
3319                continue;
3320        }
3321        else {
3322            if (!valid_status(object_get_by_md5(v, &msg)))
3323                continue;
3324        }
3325
3326        char *id = xs_dict_get(msg, "id");
3327        char *content = xs_dict_get(msg, "content");
3328
3329        if (user && !xs_startswith(id, user->actor))
3330            continue;
3331
3332        /* create a title with the first line of the content */
3333        xs *title = xs_replace(content, "<br>", "\n");
3334        title = xs_regex_replace_i(title, "<[^>]+>", " ");
3335        title = xs_regex_replace_i(title, "&[^;]+;", " ");
3336        int i;
3337
3338        for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
3339
3340        if (title[i] != '\0') {
3341            title[i] = '\0';
3342            title = xs_str_cat(title, "...");
3343        }
3344
3345        title = xs_strip_i(title);
3346
3347        xs_html_add(channel,
3348            xs_html_tag("item",
3349                xs_html_tag("title",
3350                    xs_html_text(title)),
3351                xs_html_tag("link",
3352                    xs_html_text(id)),
3353                xs_html_tag("description",
3354                    xs_html_text(content))));
3355    }
3356
3357    return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
3358}