2.70 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
 82static xs_str *format_line(const char *line, xs_list **attach)
 83/* formats a line */
 84{
 85    xs_str *s = xs_str_new(NULL);
 86    char *p;
 87    const char *v;
 88
 89    /* split by markup */
 90    xs *sm = xs_regex_split(line,
 91        "("
 92            "`[^`]+`"                           "|"
 93            "~~[^~]+~~"                         "|"
 94            "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*"     "|"
 95            "_[^_]+_"                           "|" //anzu
 96            "__[^_]+__"                         "|" //anzu
 97            "!\\[[^]]+\\]\\([^\\)]+\\)"         "|"
 98            "\\[[^]]+\\]\\([^\\)]+\\)"          "|"
 99            "[a-z]+:/" "/[^[:space:]]+"         "|"
100            "(mailto|xmpp):[^@[:space:]]+@[^[:space:]]+"
101        ")");
102    int n = 0;
103
104    p = sm;
105    while (xs_list_iter(&p, &v)) {
106        if ((n & 0x1)) {
107            /* markup */
108            if (xs_startswith(v, "`")) {
109                xs *s1 = xs_strip_chars_i(xs_dup(v), "`");
110                xs *e1 = encode_html(s1);
111                xs *s2 = xs_fmt("<code>%s</code>", e1);
112                s = xs_str_cat(s, s2);
113            }
114            else
115            if (xs_startswith(v, "***")) {
116                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
117                xs *s2 = xs_fmt("<b><i>%s</i></b>", s1);
118                s = xs_str_cat(s, s2);
119            }
120            else
121            if (xs_startswith(v, "**")) {
122                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
123                xs *s2 = xs_fmt("<b>%s</b>", s1);
124                s = xs_str_cat(s, s2);
125            }
126            else
127            if (xs_startswith(v, "*")) {
128                xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
129                xs *s2 = xs_fmt("<i>%s</i>", s1);
130                s = xs_str_cat(s, s2);
131            }
132            //anzu - begin
133            else
134            if (xs_startswith(v, "__")) {
135                xs *s1 = xs_strip_chars_i(xs_dup(v), "_");
136                xs *s2 = xs_fmt("<u>%s</u>", s1);
137                s = xs_str_cat(s, s2);
138            }
139            else
140            if (xs_startswith(v, "_")) {
141                xs *s1 = xs_strip_chars_i(xs_dup(v), "_");
142                xs *s2 = xs_fmt("<i>%s</i>", s1);
143                s = xs_str_cat(s, s2);
144            }
145            //anzu - end
146            else
147            if (xs_startswith(v, "~~")) {
148                xs *s1 = xs_strip_chars_i(xs_dup(v), "~");
149                xs *e1 = encode_html(s1);
150                xs *s2 = xs_fmt("<s>%s</s>", e1);
151                s = xs_str_cat(s, s2);
152            }
153            else
154            if (*v == '[') {
155                /* markdown-like links [label](url) */
156                xs *w = xs_strip_chars_i(
157                    xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
158                "![)");
159                xs *l = xs_split_n(w, "](", 1);
160
161                if (xs_list_len(l) == 2) {
162                    xs *link = xs_fmt("<a href=\"%s\">%s</a>",
163                            xs_list_get(l, 1), xs_list_get(l, 0));
164
165                    s = xs_str_cat(s, link);
166                }
167                else
168                    s = xs_str_cat(s, v);
169            }
170            else
171            if (*v == '!') {
172                /* markdown-like images ![alt text](url to image) */
173                xs *w = xs_strip_chars_i(
174                    xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
175                "![)");
176                xs *l = xs_split_n(w, "](", 1);
177
178                if (xs_list_len(l) == 2) {
179                    const char *alt_text = xs_list_get(l, 0);
180                    const char *img_url  = xs_list_get(l, 1);
181                    const char *mime     = xs_mime_by_ext(img_url);
182
183                    if (attach != NULL && xs_startswith(mime, "image/")) {
184                        const xs_dict *ad;
185                        int add = 1;
186
187                        xs_list_foreach(*attach, ad) {
188                            if (strcmp(xs_dict_get_def(ad, "url", ""), img_url) == 0) {
189                                add = 0;
190                                break;
191                            }
192                        }
193
194                        if (add) {
195                            xs *d = xs_dict_new();
196
197                            d = xs_dict_append(d, "mediaType", mime);
198                            d = xs_dict_append(d, "url",       img_url);
199                            d = xs_dict_append(d, "name",      alt_text);
200                            d = xs_dict_append(d, "type",      "Image");
201
202                            *attach = xs_list_append(*attach, d);
203                        }
204                    }
205                    else {
206                        xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
207
208                        s = xs_str_cat(s, link);
209                    }
210                }
211                else
212                    s = xs_str_cat(s, v);
213            }
214            else
215            if (xs_str_in(v, ":/" "/") != -1) {
216                xs *u  = xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;");
217
218                xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
219
220                const char *mime = xs_mime_by_ext(v2);
221
222                if (attach != NULL && xs_startswith(mime, "image/")) {
223                    /* if it's a link to an image, insert it as an attachment */
224                    const xs_dict *ad;
225                    int add = 1;
226
227                    xs_list_foreach(*attach, ad) {
228                        if (strcmp(xs_dict_get_def(ad, "url", ""), v2) == 0) {
229                            add = 0;
230                            break;
231                        }
232                    }
233
234                    if (add) {
235                        xs *d = xs_dict_new();
236
237                        d = xs_dict_append(d, "mediaType", mime);
238                        d = xs_dict_append(d, "url",       v2);
239                        d = xs_dict_append(d, "name",      "");
240                        d = xs_dict_append(d, "type",      "Image");
241
242                        *attach = xs_list_append(*attach, d);
243                    }
244                }
245                else {
246                    xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
247                    s = xs_str_cat(s, s1);
248                }
249            }
250            else
251            if (xs_match(v, "mailto*|xmpp*")) {
252                xs *u  = xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;");
253
254                xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
255
256                xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
257                s = xs_str_cat(s, s1);
258            }
259            else
260                s = xs_str_cat(s, v);
261        }
262        else
263            /* surrounded text, copy directly */
264            s = xs_str_cat(s, v);
265
266        n++;
267    }
268
269    return s;
270}
271
272
273xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag)
274/* formats a content using some Markdown rules */
275{
276    xs_str *s  = xs_str_new(NULL);
277    int in_pre = 0;
278    int in_blq = 0;
279    xs *list;
280    char *p;
281    const char *v;
282
283    /* work by lines */
284    list = xs_split(content, "\n");
285
286    p = list;
287    while (xs_list_iter(&p, &v)) {
288        xs *ss = NULL;
289
290        if (strcmp(v, "```") == 0) {
291            if (!in_pre)
292                s = xs_str_cat(s, "<pre>");
293            else
294                s = xs_str_cat(s, "</pre>");
295
296            in_pre = !in_pre;
297            continue;
298        }
299
300        if (in_pre) {
301            // Encode all HTML characters when we're in pre element until we are out.
302            ss = encode_html(v);
303
304            s = xs_str_cat(s, ss);
305            s = xs_str_cat(s, "<br>");
306            continue;
307        }
308
309        else
310            ss = xs_strip_i(format_line(v, attach));
311
312        if (xs_startswith(ss, "---")) {
313            /* delete the --- */
314            ss = xs_strip_i(xs_crop_i(ss, 3, 0));
315            s = xs_str_cat(s, "<hr>");
316
317            s = xs_str_cat(s, ss);
318
319            continue;
320        }
321
322        //anzu - begin
323        // h1 reserved for snac?
324        if (xs_startswith(ss, "# ")) {
325            ss = xs_strip_i(xs_crop_i(ss, 2, 0));
326            s = xs_str_cat(s, "<h2>");
327            s = xs_str_cat(s, ss);
328            s = xs_str_cat(s, "</h2>");
329            continue;
330        }
331        if (xs_startswith(ss, "## ")) {
332            ss = xs_strip_i(xs_crop_i(ss, 3, 0));
333            s = xs_str_cat(s, "<h2>");
334            s = xs_str_cat(s, ss);
335            s = xs_str_cat(s, "</h2>");
336            continue;
337        }
338        if (xs_startswith(ss, "### ")) {
339            ss = xs_strip_i(xs_crop_i(ss, 4, 0));
340            s = xs_str_cat(s, "<h3>");
341            s = xs_str_cat(s, ss);
342            s = xs_str_cat(s, "</h3>");
343            continue;
344        }
345        //anzu - end
346
347        if (xs_startswith(ss, ">")) {
348            /* delete the > and subsequent spaces */
349            ss = xs_strip_i(xs_crop_i(ss, 1, 0));
350
351            if (!in_blq) {
352                s = xs_str_cat(s, "<blockquote>");
353                in_blq = 1;
354            }
355
356            s = xs_str_cat(s, ss);
357            s = xs_str_cat(s, "<br>");
358
359            continue;
360        }
361
362        if (in_blq) {
363            s = xs_str_cat(s, "</blockquote>");
364            in_blq = 0;
365        }
366
367        s = xs_str_cat(s, ss);
368        s = xs_str_cat(s, "<br>");
369    }
370
371    if (in_blq)
372        s = xs_str_cat(s, "</blockquote>");
373    if (in_pre)
374        s = xs_str_cat(s, "</pre>");
375
376    /* some beauty fixes */
377    s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
378    s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
379    s = xs_replace_i(s, "</pre><br>", "</pre>");
380    s = xs_replace_i(s, "</h2><br>", "</h2>"); //anzu ???
381    s = xs_replace_i(s, "</h3><br>", "</h3>"); //anzu ???
382
383    {
384        /* traditional emoticons */
385        xs *d = emojis();
386        int c = 0;
387        const char *k, *v;
388
389        while (xs_dict_next(d, &k, &v, &c)) {
390            const char *t = NULL;
391
392            /* is it an URL to an image? */
393            if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) {
394                if (tag && xs_str_in(s, k) != -1) {
395                    /* add the emoji to the tag list */
396                    xs *e = xs_dict_new();
397                    xs *i = xs_dict_new();
398                    xs *u = xs_str_utctime(0, ISO_DATE_SPEC);
399
400                    e = xs_dict_append(e, "id", v);
401                    e = xs_dict_append(e, "type", "Emoji");
402                    e = xs_dict_append(e, "name", k);
403                    e = xs_dict_append(e, "updated", u);
404
405                    i = xs_dict_append(i, "type", "Image");
406                    i = xs_dict_append(i, "mediaType", t);
407                    i = xs_dict_append(i, "url", v);
408                    e = xs_dict_append(e, "icon", i);
409
410                    *tag = xs_list_append(*tag, e);
411                }
412            }
413            else
414                s = xs_replace_i(s, k, v);
415        }
416    }
417
418    return s;
419}
420
421
422const char *valid_tags[] = {
423    "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
424    "span", "i", "b", "u", "s", "pre", "code", "em", "strong", "hr", "img", "del", "bdi",
425    "h2","h3", //anzu
426    NULL
427};
428
429xs_str *sanitize(const char *content)
430/* cleans dangerous HTML output */
431{
432    xs_str *s = xs_str_new(NULL);
433    xs *sl;
434    int n = 0;
435    char *p;
436    const char *v;
437
438    sl = xs_regex_split(content, "</?[^>]+>");
439
440    p = sl;
441
442    n = 0;
443    while (xs_list_iter(&p, &v)) {
444        if (n & 0x1) {
445            xs *s1  = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
446            xs *l1  = xs_split_n(s1, " ", 1);
447            xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
448            xs *s2  = NULL;
449            int i;
450
451            /* check if it's one of the valid tags */
452            for (i = 0; valid_tags[i]; i++) {
453                if (strcmp(tag, valid_tags[i]) == 0)
454                    break;
455            }
456
457            if (valid_tags[i]) {
458                /* accepted tag: rebuild it with only the accepted elements */
459                xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\"");
460                xs *s3 = xs_join(el, " ");
461
462                s2 = xs_fmt("<%s%s%s%s>",
463                    v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
464
465                s = xs_str_cat(s, s2);
466            } else {
467                /* treat end of divs as paragraph breaks */
468                if (strcmp(v, "</div>"))
469                    s = xs_str_cat(s, "<p>");
470            }
471        }
472        else {
473            /* non-tag */
474            s = xs_str_cat(s, v);
475        }
476
477        n++;
478    }
479
480    return s;
481}
482
483
484xs_str *encode_html(const char *str)
485/* escapes html characters */
486{
487    xs_str *encoded = xs_html_encode((char *)str);
488
489    /* Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. */
490    encoded = xs_replace_i(encoded, "&lt;br&gt;", "<br>");
491
492    return encoded;
493}