/* snac - A simple, minimalistic ActivityPub instance */
/* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
#include "xs.h"
#include "xs_io.h"
#include "xs_json.h"
#include "xs_regex.h"
#include "xs_set.h"
#include "xs_openssl.h"
#include "xs_time.h"
#include "xs_mime.h"
#include "snac.h"
int login(snac *snac, const xs_dict *headers)
/* tries a login */
{
int logged_in = 0;
const char *auth = xs_dict_get(headers, "authorization");
if (auth && xs_startswith(auth, "Basic ")) {
int sz;
xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
xs *s2 = xs_base64_dec(s1, &sz);
xs *l1 = xs_split_n(s2, ":", 1);
if (xs_list_len(l1) == 2) {
logged_in = check_password(
xs_list_get(l1, 0), xs_list_get(l1, 1),
xs_dict_get(snac->config, "passwd"));
}
}
if (logged_in)
lastlog_write(snac, "web");
return logged_in;
}
xs_str *actor_name(xs_dict *actor)
/* gets the actor name */
{
xs_list *p;
char *v;
xs_str *name;
if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
v = "anonymous";
}
}
name = encode_html(v);
/* replace the :shortnames: */
if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
xs *tag = NULL;
if (xs_type(p) == XSTYPE_DICT) {
/* not a list */
tag = xs_list_new();
tag = xs_list_append(tag, p);
} else {
/* is a list */
tag = xs_dup(p);
}
xs_list *tags = tag;
/* iterate the tags */
while (xs_list_iter(&tags, &v)) {
char *t = xs_dict_get(v, "type");
if (t && strcmp(t, "Emoji") == 0) {
char *n = xs_dict_get(v, "name");
char *i = xs_dict_get(v, "icon");
if (n && i) {
char *u = xs_dict_get(i, "url");
xs *img = xs_fmt("", u);
name = xs_replace_i(name, n, img);
}
}
}
}
return name;
}
xs_str *html_actor_icon(xs_str *os, char *actor,
const char *date, const char *udate, const char *url, int priv)
{
xs *s = xs_str_new(NULL);
xs *avatar = NULL;
char *v;
xs *name = actor_name(actor);
/* get the avatar */
if ((v = xs_dict_get(actor, "icon")) != NULL &&
(v = xs_dict_get(v, "url")) != NULL) {
avatar = xs_dup(v);
}
if (avatar == NULL)
avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
{
xs *s1 = xs_fmt("
\n", avatar);
s = xs_str_cat(s, s1);
}
{
xs *s1 = xs_fmt("%s",
xs_dict_get(actor, "id"), name);
s = xs_str_cat(s, s1);
}
if (!xs_is_null(url)) {
xs *s1 = xs_fmt(" »", url);
s = xs_str_cat(s, s1);
}
if (priv)
s = xs_str_cat(s, " 🔒");
if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
s = xs_str_cat(s, " 🤖");
if (xs_is_null(date)) {
s = xs_str_cat(s, "\n \n");
}
else {
xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
xs *date_title = xs_dup(date);
if (!xs_is_null(udate)) {
xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
date_label = xs_str_cat(date_label, " / ");
date_label = xs_str_cat(date_label, sd);
date_title = xs_str_cat(date_title, " / ");
date_title = xs_str_cat(date_title, udate);
}
xs *edt = encode_html(date_title);
xs *edl = encode_html(date_label);
xs *s1 = xs_fmt(
"\n\n",
edt, edl);
s = xs_str_cat(s, s1);
}
{
char *username, *id;
xs *s1;
if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
/* This should never be reached */
username = "anonymous";
}
if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
/* This should never be reached */
id = "https://social.example.org/anonymous";
}
/* "LIKE AN ANIMAL" */
xs *domain = xs_split(id, "/");
xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
xs *u1 = encode_html(user);
s1 = xs_fmt(
" %s",
xs_dict_get(actor, "id"), u1);
s = xs_str_cat(s, s1);
}
return xs_str_cat(os, s);
}
xs_str *html_msg_icon(xs_str *os, const xs_dict *msg)
{
char *actor_id;
xs *actor = NULL;
if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
actor_id = xs_dict_get(msg, "actor");
if (actor_id && valid_status(actor_get(actor_id, &actor))) {
char *date = NULL;
char *udate = NULL;
char *url = NULL;
int priv = 0;
const char *type = xs_dict_get(msg, "type");
if (strcmp(type, "Note") == 0 || strcmp(type, "Question") == 0 || strcmp(type, "Page") == 0)
url = xs_dict_get(msg, "id");
priv = !is_msg_public(msg);
date = xs_dict_get(msg, "published");
udate = xs_dict_get(msg, "updated");
os = html_actor_icon(os, actor, date, udate, url, priv);
}
return os;
}
xs_str *html_base_header(xs_str *s)
{
xs_list *p;
xs_str *v;
s = xs_str_cat(s, "\n\n
\n");
s = xs_str_cat(s, "\n");
s = xs_str_cat(s, "\n");
/* add server CSS */
p = xs_dict_get(srv_config, "cssurls");
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_fmt("\n", v);
s = xs_str_cat(s, s1);
}
return s;
}
xs_str *html_instance_header(xs_str *s)
{
s = html_base_header(s);
{
FILE *f;
xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
if ((f = fopen(g_css_fn, "r")) != NULL) {
xs *css = xs_readall(f);
fclose(f);
xs *s1 = xs_fmt("\n", css);
s = xs_str_cat(s, s1);
}
}
const char *host = xs_dict_get(srv_config, "host");
const char *title = xs_dict_get(srv_config, "title");
const char *sdesc = xs_dict_get(srv_config, "short_description");
const char *email = xs_dict_get(srv_config, "admin_email");
const char *acct = xs_dict_get(srv_config, "admin_account");
{
xs *s1 = xs_fmt("%s\n", title && *title ? title : host);
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "\n\n");
s = xs_str_cat(s, "
\n");
{
xs *s1 = xs_fmt(
"
%s is a "
"Fediverse "
"instance that uses the "
"ActivityPub "
"protocol. In other words, users at this host can communicate with people "
"that use software like Mastodon, Pleroma, Friendica, etc. "
"all around the world.
\n"
"
This server runs the "
"snac software and there is no "
"automatic sign-up process.
\n",
host);
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "
\n");
if (sdesc && *sdesc) {
xs *s1 = xs_fmt("
%s
%s
\n", L("Site description"), sdesc);
s = xs_str_cat(s, s1);
}
if (email && *email) {
xs *s1 = xs_fmt("
\n");
return xs_str_cat(os, s);
}
xs_str *html_entry(snac *user, xs_str *os, const xs_dict *msg, int local,
int level, const char *md5, int hide_children)
{
char *id = xs_dict_get(msg, "id");
char *type = xs_dict_get(msg, "type");
char *actor;
int sensitive = 0;
char *v;
xs *boosts = NULL;
/* do not show non-public messages in the public timeline */
if (local && !is_msg_public(msg))
return os;
/* hidden? do nothing more for this conversation */
if (user && is_hidden(user, id))
return os;
/* avoid too deep nesting, as it may be a loop */
if (level >= 256)
return os;
xs *s = xs_str_new("
\n");
{
xs *s1 = xs_fmt("\n", md5);
s = xs_str_cat(s, s1);
}
if (strcmp(type, "Follow") == 0) {
s = xs_str_cat(s, "
\n
\n");
xs *s1 = xs_fmt("
%s
\n", L("follows you"));
s = xs_str_cat(s, s1);
s = html_msg_icon(s, msg);
s = xs_str_cat(s, "
\n
\n");
return xs_str_cat(os, s);
}
else
if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0 && strcmp(type, "Page") != 0) {
/* skip oddities */
return os;
}
/* ignore notes with "name", as they are votes to Questions */
if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
return os;
/* bring the main actor */
if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
return os;
/* ignore muted morons immediately */
if (user && is_muted(user, actor))
return os;
if ((user == NULL || strcmp(actor, user->actor) != 0)
&& !valid_status(actor_get(actor, NULL)))
return os;
if (level == 0)
s = xs_str_cat(s, "
\n"); /** **/
else
s = xs_str_cat(s, "
\n"); /** **/
s = xs_str_cat(s, "
\n
"); /** **/
if (user && is_pinned(user, id)) {
/* add a pin emoji */
xs *f = xs_fmt(" 📌 ", L("Pinned"));
s = xs_str_cat(s, f);
}
if (strcmp(type, "Question") == 0) {
/* add the ballot box emoji */
xs *f = xs_fmt(" 🗳 ", L("Poll"));
s = xs_str_cat(s, f);
if (user && was_question_voted(user, id)) {
/* add a check to show this poll was voted */
xs *f2 = xs_fmt(" ✓ ", L("Voted"));
s = xs_str_cat(s, f2);
}
}
/* if it's a user from this same instance, add the score */
if (xs_startswith(id, srv_baseurl)) {
int n_likes = object_likes_len(id);
int n_boosts = object_announces_len(id);
/* alternate emojis: %d 👍 %d 🔁 */
xs *s1 = xs_fmt("%d ★ %d ↺\n", n_likes, n_boosts);
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "
\n");
if (boosts == NULL)
boosts = object_announces(id);
if (xs_list_len(boosts)) {
/* if somebody boosted this, show as origin */
char *p = xs_list_get(boosts, -1);
xs *actor_r = NULL;
if (user && xs_list_in(boosts, user->md5) != -1) {
/* we boosted this */
xs *es1 = encode_html(xs_dict_get(user->config, "name"));
xs *s1 = xs_fmt(
"
\n", es1);
s = xs_str_cat(s, s1);
}
/* is it sensitive? */
if (user && xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
v = "...";
/* only show it when not in the public timeline and the config setting is "open" */
char *cw = xs_dict_get(user->config, "cw");
if (xs_is_null(cw) || local)
cw = "";
xs *es1 = encode_html(v);
xs *s1 = xs_fmt("%s [%s]\n", cw, es1, L("SENSITIVE CONTENT"));
s = xs_str_cat(s, s1);
sensitive = 1;
}
#if 0
{
xs *md5 = xs_md5_hex(id, strlen(id));
xs *s1 = xs_fmt("
%s
\n", md5);
s = xs_str_cat(s, s1);
}
#endif
{
const char *content = xs_dict_get(msg, "content");
xs *c = sanitize(xs_is_null(content) ? "" : content);
char *p, *v;
/* do some tweaks to the content */
c = xs_replace_i(c, "\r", "");
while (xs_endswith(c, "
"))
c = xs_crop_i(c, 0, -4);
c = xs_replace_i(c, "
", "
");
if (!xs_startswith(c, "
")) {
xs *s1 = c;
c = xs_fmt("
%s
", s1);
}
/* replace the :shortnames: */
if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
xs *tag = NULL;
if (xs_type(p) == XSTYPE_DICT) {
/* not a list */
tag = xs_list_new();
tag = xs_list_append(tag, p);
} else {
/* is a list */
tag = xs_dup(p);
}
xs_list *tags = tag;
/* iterate the tags */
while (xs_list_iter(&tags, &v)) {
char *t = xs_dict_get(v, "type");
if (t && strcmp(t, "Emoji") == 0) {
char *n = xs_dict_get(v, "name");
char *i = xs_dict_get(v, "icon");
if (n && i) {
char *u = xs_dict_get(i, "url");
xs *img = xs_fmt("", u, n);
c = xs_replace_i(c, n, img);
}
}
}
}
if (strcmp(type, "Question") == 0) { /** question content **/
xs_list *oo = xs_dict_get(msg, "oneOf");
xs_list *ao = xs_dict_get(msg, "anyOf");
xs_list *p;
xs_dict *v;
int closed = 0;
if (xs_dict_get(msg, "closed"))
closed = 2;
else
if (user && xs_startswith(id, user->actor))
closed = 1; /* we questioned; closed for us */
else
if (user && was_question_voted(user, id))
closed = 1; /* we already voted; closed for us */
/* get the appropriate list of options */
p = oo != NULL ? oo : ao;
if (closed || user == NULL) {
/* closed poll */
c = xs_str_cat(c, "
\n");
while (xs_list_iter(&p, &v)) {
const char *name = xs_dict_get(v, "name");
const xs_dict *replies = xs_dict_get(v, "replies");
if (name && replies) {
int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
xs *es1 = encode_html(name);
xs *l = xs_fmt("
%s:
%d
\n", es1, nr);
c = xs_str_cat(c, l);
}
}
c = xs_str_cat(c, "
\n");
}
else {
/* poll still active */
xs *s1 = xs_fmt("
\n"
"\n
\n\n", L("Vote"));
s1 = xs_str_cat(s1, s2);
c = xs_str_cat(c, s1);
}
/* if it's *really* closed, say it */
if (closed == 2) {
xs *s1 = xs_fmt("
%s
\n", L("Closed"));
c = xs_str_cat(c, s1);
}
else {
/* show when the poll closes */
const char *end_time = xs_dict_get(msg, "endTime");
if (!xs_is_null(end_time)) {
time_t t0 = time(NULL);
time_t t1 = xs_parse_iso_date(end_time, 0);
if (t1 > 0 && t1 > t0) {
time_t diff_time = t1 - t0;
xs *tf = xs_str_time_diff(diff_time);
char *p = tf;
/* skip leading zeros */
for (; *p == '0' || *p == ':'; p++);
xs *es1 = encode_html(p);
xs *s1 = xs_fmt("
%s %s
", L("Closes in"), es1);
c = xs_str_cat(c, s1);
}
}
}
}
s = xs_str_cat(s, c);
}
s = xs_str_cat(s, "\n");
/* add the attachments */
v = xs_dict_get(msg, "attachment");
if (!xs_is_null(v)) { /** attachments **/
xs *attach = NULL;
/* ensure it's a list */
if (xs_type(v) == XSTYPE_DICT) {
attach = xs_list_new();
attach = xs_list_append(attach, v);
}
else
attach = xs_dup(v);
/* does the message have an image? */
if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
/* add it to the attachment list */
attach = xs_list_append(attach, v);
}
/* make custom css for attachments easier */
s = xs_str_cat(s, "
\n");
xs_list *p = attach;
while (xs_list_iter(&p, &v)) {
const char *t = xs_dict_get(v, "mediaType");
if (xs_is_null(t))
t = xs_dict_get(v, "type");
if (xs_is_null(t))
continue;
const char *url = xs_dict_get(v, "url");
if (xs_is_null(url))
url = xs_dict_get(v, "href");
if (xs_is_null(url))
continue;
const char *name = xs_dict_get(v, "name");
if (xs_is_null(name))
name = xs_dict_get(msg, "name");
if (xs_is_null(name))
name = L("No description");
xs *es1 = encode_html(name);
xs *s1 = NULL;
if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
s1 = xs_fmt(
""
"\n",
url, url, es1, es1);
}
else
if (xs_startswith(t, "video/")) {
s1 = xs_fmt("\n", url, url, es1);
}
else
if (xs_startswith(t, "audio/")) {
s1 = xs_fmt("\n", url, url, es1);
}
else
if (strcmp(t, "Link") == 0) {
xs *es2 = encode_html(url);
s1 = xs_fmt("
\n", url, es1);
}
if (!xs_is_null(s1))
s = xs_str_cat(s, s1);
}
s = xs_str_cat(s, "
\n");
}
/* has this message an audience (i.e., comes from a channel or community)? */
const char *audience = xs_dict_get(msg, "audience");
if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
xs *es1 = encode_html(audience);
xs *s1 = xs_fmt("
\n",
audience, L("Source channel or community"), es1);
s = xs_str_cat(s, s1);
}
if (sensitive)
s = xs_str_cat(s, "
\n");
s = xs_str_cat(s, "
\n");
/** controls **/
if (!local && user)
s = html_entry_controls(user, s, msg, md5);
/** children **/
if (!hide_children) {
xs *children = object_children(id);
int left = xs_list_len(children);
if (left) {
char *p, *cmd5;
int older_open = 0;
xs *ss = xs_str_new(NULL);
int n_children = 0;
ss = xs_str_cat(ss, "...
\n");
if (level < 4)
ss = xs_str_cat(ss, "
\n");
else
ss = xs_str_cat(ss, "
\n");
if (left > 3) {
xs *s1 = xs_fmt("%s\n", L("Older..."));
ss = xs_str_cat(ss, s1);
older_open = 1;
}
p = children;
while (xs_list_iter(&p, &cmd5)) {
xs *chd = NULL;
if (user)
timeline_get_by_md5(user, cmd5, &chd);
else
object_get_by_md5(cmd5, &chd);
if (older_open && left <= 3) {
ss = xs_str_cat(ss, "\n");
older_open = 0;
}
if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
ss = html_entry(user, ss, chd, local, level + 1, cmd5, hide_children);
n_children++;
}
else
srv_debug(2, xs_fmt("cannot read child %s", cmd5));
left--;
}
if (older_open)
ss = xs_str_cat(ss, "\n");
ss = xs_str_cat(ss, "
\n");
ss = xs_str_cat(ss, "\n");
if (n_children)
s = xs_str_cat(s, ss);
}
}
s = xs_str_cat(s, "
\n
\n");
return xs_str_cat(os, s);
}
xs_str *html_footer(xs_str *s)
{
xs *s1 = xs_fmt(
"\n",
srv_baseurl,
L("about this site"),
WHAT_IS_SNAC_URL
);
return xs_str_cat(s, s1);
}
xs_str *html_timeline(snac *user, const xs_list *list, int local, int skip, int show, int show_more)
/* returns the HTML for the timeline */
{
xs_str *s = xs_str_new(NULL);
xs_list *p = (xs_list *)list;
char *v;
double t = ftime();
if (user)
s = html_user_header(user, s, local);
else
s = html_instance_header(s);
if (user && !local)
s = html_top_controls(user, s);
s = xs_str_cat(s, "\n");
s = xs_str_cat(s, "
\n");
while (xs_list_iter(&p, &v)) {
xs *msg = NULL;
int status;
if (user)
status = timeline_get_by_md5(user, v, &msg);
else
status = object_get_by_md5(v, &msg);
if (!valid_status(status))
continue;
s = html_entry(user, s, msg, local, 0, v, 0);
}
s = xs_str_cat(s, "