2.64 format.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_regex.h"
  6#include "xs_mime.h"
  7#include "xs_html.h"
  8#include "xs_json.h"
  9#include "xs_time.h"
 10
 11#include "snac.h"
 12
 13/* emoticons, people laughing and such */
 14const char *smileys[] = {
 15    ":-)",        "🙂",
 16    ":-D",        "😀",
 17    "X-D",        "😆",
 18    ";-)",        "😉",
 19    "B-)",        "😎",
 20    ">:-(",       "😡",
 21    ":-(",        "😞",
 22    ":-*",        "😘",
 23    ":-/",        "😕",
 24    "8-o",        "😲",
 25    "%-)",        "🤪",
 26    ":_(",        "😢",
 27    ":-|",        "😐",
 28    "<3",         "&#10084;&#65039;",
 29    ":facepalm:", "&#129318;",
 30    ":shrug:",    "&#129335;",
 31    ":shrug2:",   "&#175;\\_(&#12484;)_/&#175;",
 32    ":eyeroll:",  "&#128580;",
 33    ":beer:",     "&#127866;",
 34    ":beers:",    "&#127867;",
 35    ":munch:",    "&#128561;",
 36    ":thumb:",    "&#128077;",
 37    NULL,         NULL
 38};
 39
 40
 41xs_dict *emojis(void)
 42/* returns a dict with the emojis */
 43{
 44    xs *fn = xs_fmt("%s/emojis.json", srv_basedir);
 45    FILE *f;
 46
 47    if (mtime(fn) == 0) {
 48        /* file does not exist; create it with the defaults */
 49        xs *d = xs_dict_new();
 50        const char **emo = smileys;
 51
 52        while (*emo) {
 53            d = xs_dict_append(d, emo[0], emo[1]);
 54            emo += 2;
 55        }
 56
 57        if ((f = fopen(fn, "w")) != NULL) {
 58            xs_json_dump(d, 4, f);
 59            fclose(f);
 60        }
 61        else
 62            srv_log(xs_fmt("Error creating '%s'", fn));
 63    }
 64
 65    xs_dict *d = NULL;
 66
 67    if ((f = fopen(fn, "r")) != NULL) {
 68        d = xs_json_load(f);
 69        fclose(f);
 70
 71        if (d == NULL)
 72            srv_log(xs_fmt("JSON parse error in '%s'", fn));
 73    }
 74    else
 75        srv_log(xs_fmt("Error opening '%s'", fn));
 76
 77    return d;
 78}
 79
 80
 81static xs_str *format_line(const char *line, xs_list **attach)
 82/* formats a line */
 83{
 84    xs_str *s = xs_str_new(NULL);
 85    char *p;
 86    const char *v;
 87
 88    /* split by markup */
 89    xs *sm = xs_regex_split(line,
 90        "("
 91            "`[^`]+`"                           "|"
 92            "~~[^~]+~~"                         "|"
 93            "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*"     "|"
 94            "!\\[[^]]+\\]\\([^\\)]+\\)"         "|"
 95            "\\[[^]]+\\]\\([^\\)]+\\)"          "|"
 96            "[a-z]+:/" "/[^[:space:]]+"
 97        ")");
 98    int n = 0;
 99
100    p = sm;
101    while (xs_list_iter(&p, &v)) {
102        if ((n & 0x1)) {
103            /* markup */
104            if (xs_startswith(v, "`")) {
105                xs *s1 = xs_strip_chars_i(xs_dup(v), "`");
106                xs *e1 = encode_html(s1);
107                xs *s2 = xs_fmt("<code>%s</code>", e1);
108                s = xs_str_cat(s, s2);
109            }
110            else
111            if (xs_startswith(v, "***")) {
112                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
113                xs *s2 = xs_fmt("<b><i>%s</i></b>", s1);
114                s = xs_str_cat(s, s2);
115            }
116            else
117            if (xs_startswith(v, "**")) {
118                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
119                xs *s2 = xs_fmt("<b>%s</b>", s1);
120                s = xs_str_cat(s, s2);
121            }
122            else
123            if (xs_startswith(v, "*")) {
124                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
125                xs *s2 = xs_fmt("<i>%s</i>", s1);
126                s = xs_str_cat(s, s2);
127            }
128            else
129            if (xs_startswith(v, "~~")) {
130                xs *s1 = xs_strip_chars_i(xs_dup(v), "~");
131                xs *e1 = encode_html(s1);
132                xs *s2 = xs_fmt("<s>%s</s>", e1);
133                s = xs_str_cat(s, s2);
134            }
135            else
136            if (*v == '[') {
137                /* markdown-like links [label](url) */
138                xs *w = xs_strip_chars_i(
139                    xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
140                "![)");
141                xs *l = xs_split_n(w, "](", 1);
142
143                if (xs_list_len(l) == 2) {
144                    xs *link = xs_fmt("<a href=\"%s\">%s</a>",
145                            xs_list_get(l, 1), xs_list_get(l, 0));
146
147                    s = xs_str_cat(s, link);
148                }
149                else
150                    s = xs_str_cat(s, v);
151            }
152            else
153            if (*v == '!') {
154                /* markdown-like images ![alt text](url to image) */
155                xs *w = xs_strip_chars_i(
156                    xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
157                "![)");
158                xs *l = xs_split_n(w, "](", 1);
159
160                if (xs_list_len(l) == 2) {
161                    const char *alt_text = xs_list_get(l, 0);
162                    const char *img_url  = xs_list_get(l, 1);
163                    const char *mime     = xs_mime_by_ext(img_url);
164
165                    if (attach != NULL && xs_startswith(mime, "image/")) {
166                        xs *d = xs_dict_new();
167
168                        d = xs_dict_append(d, "mediaType", mime);
169                        d = xs_dict_append(d, "url",       img_url);
170                        d = xs_dict_append(d, "name",      alt_text);
171                        d = xs_dict_append(d, "type",      "Image");
172
173                        *attach = xs_list_append(*attach, d);
174                    }
175                    else {
176                        xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
177
178                        s = xs_str_cat(s, link);
179                    }
180                }
181                else
182                    s = xs_str_cat(s, v);
183            }
184            else
185            if (xs_str_in(v, ":/" "/") != -1) {
186                xs *u  = xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;");
187
188                xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
189
190                const char *mime = xs_mime_by_ext(v2);
191
192                if (attach != NULL && xs_startswith(mime, "image/")) {
193                    /* if it's a link to an image, insert it as an attachment */
194                    xs *d = xs_dict_new();
195
196                    d = xs_dict_append(d, "mediaType", mime);
197                    d = xs_dict_append(d, "url",       v2);
198                    d = xs_dict_append(d, "name",      "");
199                    d = xs_dict_append(d, "type",      "Image");
200
201                    *attach = xs_list_append(*attach, d);
202                }
203                else {
204                    xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
205                    s = xs_str_cat(s, s1);
206                }
207            }
208            else
209                s = xs_str_cat(s, v);
210        }
211        else
212            /* surrounded text, copy directly */
213            s = xs_str_cat(s, v);
214
215        n++;
216    }
217
218    return s;
219}
220
221
222xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag)
223/* formats a content using some Markdown rules */
224{
225    xs_str *s  = xs_str_new(NULL);
226    int in_pre = 0;
227    int in_blq = 0;
228    xs *list;
229    char *p;
230    const char *v;
231
232    /* work by lines */
233    list = xs_split(content, "\n");
234
235    p = list;
236    while (xs_list_iter(&p, &v)) {
237        xs *ss = NULL;
238
239        if (strcmp(v, "```") == 0) {
240            if (!in_pre)
241                s = xs_str_cat(s, "<pre>");
242            else
243                s = xs_str_cat(s, "</pre>");
244
245            in_pre = !in_pre;
246            continue;
247        }
248
249        if (in_pre) {
250            // Encode all HTML characters when we're in pre element until we are out.
251            ss = encode_html(v);
252
253            s = xs_str_cat(s, ss);
254            s = xs_str_cat(s, "<br>");
255            continue;
256        }
257
258        else
259            ss = xs_strip_i(format_line(v, attach));
260
261        if (xs_startswith(ss, "---")) {
262            /* delete the --- */
263            ss = xs_strip_i(xs_crop_i(ss, 3, 0));
264            s = xs_str_cat(s, "<hr>");
265
266            s = xs_str_cat(s, ss);
267
268            continue;
269        }
270
271        if (xs_startswith(ss, ">")) {
272            /* delete the > and subsequent spaces */
273            ss = xs_strip_i(xs_crop_i(ss, 1, 0));
274
275            if (!in_blq) {
276                s = xs_str_cat(s, "<blockquote>");
277                in_blq = 1;
278            }
279
280            s = xs_str_cat(s, ss);
281            s = xs_str_cat(s, "<br>");
282
283            continue;
284        }
285
286        if (in_blq) {
287            s = xs_str_cat(s, "</blockquote>");
288            in_blq = 0;
289        }
290
291        s = xs_str_cat(s, ss);
292        s = xs_str_cat(s, "<br>");
293    }
294
295    if (in_blq)
296        s = xs_str_cat(s, "</blockquote>");
297    if (in_pre)
298        s = xs_str_cat(s, "</pre>");
299
300    /* some beauty fixes */
301    s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
302    s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
303    s = xs_replace_i(s, "</pre><br>", "</pre>");
304
305    {
306        /* traditional emoticons */
307        xs *d = emojis();
308        int c = 0;
309        const char *k, *v;
310
311        while (xs_dict_next(d, &k, &v, &c)) {
312            const char *t = NULL;
313
314            /* is it an URL to an image? */
315            if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) {
316                if (tag && xs_str_in(s, k) != -1) {
317                    /* add the emoji to the tag list */
318                    xs *e = xs_dict_new();
319                    xs *i = xs_dict_new();
320                    xs *u = xs_str_utctime(0, ISO_DATE_SPEC);
321
322                    e = xs_dict_append(e, "id", v);
323                    e = xs_dict_append(e, "type", "Emoji");
324                    e = xs_dict_append(e, "name", k);
325                    e = xs_dict_append(e, "updated", u);
326
327                    i = xs_dict_append(i, "type", "Image");
328                    i = xs_dict_append(i, "mediaType", t);
329                    i = xs_dict_append(i, "url", v);
330                    e = xs_dict_append(e, "icon", i);
331
332                    *tag = xs_list_append(*tag, e);
333                }
334            }
335            else
336                s = xs_replace_i(s, k, v);
337        }
338    }
339
340    return s;
341}
342
343
344const char *valid_tags[] = {
345    "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
346    "span", "i", "b", "u", "s", "pre", "code", "em", "strong", "hr", "img", "del", "bdi", NULL
347};
348
349xs_str *sanitize(const char *content)
350/* cleans dangerous HTML output */
351{
352    xs_str *s = xs_str_new(NULL);
353    xs *sl;
354    int n = 0;
355    char *p;
356    const char *v;
357
358    sl = xs_regex_split(content, "</?[^>]+>");
359
360    p = sl;
361
362    n = 0;
363    while (xs_list_iter(&p, &v)) {
364        if (n & 0x1) {
365            xs *s1  = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
366            xs *l1  = xs_split_n(s1, " ", 1);
367            xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
368            xs *s2  = NULL;
369            int i;
370
371            /* check if it's one of the valid tags */
372            for (i = 0; valid_tags[i]; i++) {
373                if (strcmp(tag, valid_tags[i]) == 0)
374                    break;
375            }
376
377            if (valid_tags[i]) {
378                /* accepted tag: rebuild it with only the accepted elements */
379                xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\"");
380                xs *s3 = xs_join(el, " ");
381
382                s2 = xs_fmt("<%s%s%s%s>",
383                    v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
384
385                s = xs_str_cat(s, s2);
386            } else {
387                /* treat end of divs as paragraph breaks */
388                if (strcmp(v, "</div>"))
389                    s = xs_str_cat(s, "<p>");
390            }
391        }
392        else {
393            /* non-tag */
394            s = xs_str_cat(s, v);
395        }
396
397        n++;
398    }
399
400    return s;
401}
402
403
404xs_str *encode_html(const char *str)
405/* escapes html characters */
406{
407    xs_str *encoded = xs_html_encode((char *)str);
408
409    /* Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. */
410    encoded = xs_replace_i(encoded, "&lt;br&gt;", "<br>");
411
412    return encoded;
413}