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