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