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