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