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