format.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_regex.h"
  5. #include "xs_mime.h"
  6. #include "xs_html.h"
  7. #include "xs_json.h"
  8. #include "xs_time.h"
  9. #include "snac.h"
  10. /* emoticons, people laughing and such */
  11. const char *smileys[] = {
  12. ":-)", "🙂",
  13. ":-D", "😀",
  14. "X-D", "😆",
  15. ";-)", "😉",
  16. "B-)", "😎",
  17. ">:-(", "😡",
  18. ":-(", "😞",
  19. ":-*", "😘",
  20. ":-/", "😕",
  21. "8-o", "😲",
  22. "%-)", "🤪",
  23. ":_(", "😢",
  24. ":-|", "😐",
  25. "<3", "&#10084;&#65039;",
  26. ":facepalm:", "&#129318;",
  27. ":shrug:", "&#129335;",
  28. ":shrug2:", "&#175;\\_(&#12484;)_/&#175;",
  29. ":eyeroll:", "&#128580;",
  30. ":beer:", "&#127866;",
  31. ":beers:", "&#127867;",
  32. ":munch:", "&#128561;",
  33. ":thumb:", "&#128077;",
  34. NULL, NULL
  35. };
  36. xs_dict *emojis(void)
  37. /* returns a dict with the emojis */
  38. {
  39. xs *fn = xs_fmt("%s/emojis.json", srv_basedir);
  40. FILE *f;
  41. if (mtime(fn) == 0) {
  42. /* file does not exist; create it with the defaults */
  43. xs *d = xs_dict_new();
  44. const char **emo = smileys;
  45. while (*emo) {
  46. d = xs_dict_append(d, emo[0], emo[1]);
  47. emo += 2;
  48. }
  49. if ((f = fopen(fn, "w")) != NULL) {
  50. xs_json_dump(d, 4, f);
  51. fclose(f);
  52. }
  53. else
  54. srv_log(xs_fmt("Error creating '%s'", fn));
  55. }
  56. xs_dict *d = NULL;
  57. if ((f = fopen(fn, "r")) != NULL) {
  58. d = xs_json_load(f);
  59. fclose(f);
  60. if (d == NULL)
  61. srv_log(xs_fmt("JSON parse error in '%s'", fn));
  62. }
  63. else
  64. srv_log(xs_fmt("Error opening '%s'", fn));
  65. return d;
  66. }
  67. static xs_str *format_line(const char *line, xs_list **attach)
  68. /* formats a line */
  69. {
  70. xs_str *s = xs_str_new(NULL);
  71. char *p;
  72. const char *v;
  73. /* split by markup */
  74. xs *sm = xs_regex_split(line,
  75. "("
  76. "`[^`]+`" "|"
  77. "~~[^~]+~~" "|"
  78. "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*" "|"
  79. "\\[[^]]+\\]\\([^\\)]+\\)" "|"
  80. "https?:/" "/[^[:space:]]+"
  81. ")");
  82. int n = 0;
  83. p = sm;
  84. while (xs_list_iter(&p, &v)) {
  85. if ((n & 0x1)) {
  86. /* markup */
  87. if (xs_startswith(v, "`")) {
  88. xs *s1 = xs_strip_chars_i(xs_dup(v), "`");
  89. xs *e1 = encode_html(s1);
  90. xs *s2 = xs_fmt("<code>%s</code>", e1);
  91. s = xs_str_cat(s, s2);
  92. }
  93. else
  94. if (xs_startswith(v, "***")) {
  95. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  96. xs *s2 = xs_fmt("<b><i>%s</i></b>", s1);
  97. s = xs_str_cat(s, s2);
  98. }
  99. else
  100. if (xs_startswith(v, "**")) {
  101. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  102. xs *s2 = xs_fmt("<b>%s</b>", s1);
  103. s = xs_str_cat(s, s2);
  104. }
  105. else
  106. if (xs_startswith(v, "*")) {
  107. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  108. xs *s2 = xs_fmt("<i>%s</i>", s1);
  109. s = xs_str_cat(s, s2);
  110. }
  111. else
  112. if (xs_startswith(v, "~~")) {
  113. xs *s1 = xs_strip_chars_i(xs_dup(v), "~");
  114. xs *e1 = encode_html(s1);
  115. xs *s2 = xs_fmt("<s>%s</s>", e1);
  116. s = xs_str_cat(s, s2);
  117. }
  118. else
  119. if (xs_startswith(v, "http")) {
  120. xs *u = xs_replace(v, "#", "&#35;");
  121. xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
  122. const char *mime = xs_mime_by_ext(v2);
  123. if (attach != NULL && xs_startswith(mime, "image/")) {
  124. /* if it's a link to an image, insert it as an attachment */
  125. xs *d = xs_dict_new();
  126. d = xs_dict_append(d, "mediaType", mime);
  127. d = xs_dict_append(d, "url", v2);
  128. d = xs_dict_append(d, "name", "");
  129. d = xs_dict_append(d, "type", "Image");
  130. *attach = xs_list_append(*attach, d);
  131. }
  132. else {
  133. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
  134. s = xs_str_cat(s, s1);
  135. }
  136. }
  137. else
  138. if (*v == '[') {
  139. /* markdown-like links [label](url) */
  140. xs *w = xs_strip_chars_i(xs_replace(v, "#", "&#35;"), "[)");
  141. xs *l = xs_split_n(w, "](", 1);
  142. if (xs_list_len(l) == 2) {
  143. xs *link = xs_fmt("<a href=\"%s\">%s</a>",
  144. xs_list_get(l, 1), xs_list_get(l, 0));
  145. s = xs_str_cat(s, link);
  146. }
  147. else
  148. s = xs_str_cat(s, v);
  149. }
  150. else
  151. s = xs_str_cat(s, v);
  152. }
  153. else
  154. /* surrounded text, copy directly */
  155. s = xs_str_cat(s, v);
  156. n++;
  157. }
  158. return s;
  159. }
  160. xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag)
  161. /* formats a content using some Markdown rules */
  162. {
  163. xs_str *s = xs_str_new(NULL);
  164. int in_pre = 0;
  165. int in_blq = 0;
  166. xs *list;
  167. char *p;
  168. const char *v;
  169. /* work by lines */
  170. list = xs_split(content, "\n");
  171. p = list;
  172. while (xs_list_iter(&p, &v)) {
  173. xs *ss = NULL;
  174. if (strcmp(v, "```") == 0) {
  175. if (!in_pre)
  176. s = xs_str_cat(s, "<pre>");
  177. else
  178. s = xs_str_cat(s, "</pre>");
  179. in_pre = !in_pre;
  180. continue;
  181. }
  182. if (in_pre) {
  183. // Encode all HTML characters when we're in pre element until we are out.
  184. ss = encode_html(v);
  185. s = xs_str_cat(s, ss);
  186. s = xs_str_cat(s, "<br>");
  187. continue;
  188. }
  189. else
  190. ss = xs_strip_i(format_line(v, attach));
  191. if (xs_startswith(ss, "---")) {
  192. /* delete the --- */
  193. ss = xs_strip_i(xs_crop_i(ss, 3, 0));
  194. s = xs_str_cat(s, "<hr>");
  195. s = xs_str_cat(s, ss);
  196. continue;
  197. }
  198. if (xs_startswith(ss, ">")) {
  199. /* delete the > and subsequent spaces */
  200. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  201. if (!in_blq) {
  202. s = xs_str_cat(s, "<blockquote>");
  203. in_blq = 1;
  204. }
  205. s = xs_str_cat(s, ss);
  206. s = xs_str_cat(s, "<br>");
  207. continue;
  208. }
  209. if (in_blq) {
  210. s = xs_str_cat(s, "</blockquote>");
  211. in_blq = 0;
  212. }
  213. s = xs_str_cat(s, ss);
  214. s = xs_str_cat(s, "<br>");
  215. }
  216. if (in_blq)
  217. s = xs_str_cat(s, "</blockquote>");
  218. if (in_pre)
  219. s = xs_str_cat(s, "</pre>");
  220. /* some beauty fixes */
  221. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  222. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  223. s = xs_replace_i(s, "</pre><br>", "</pre>");
  224. {
  225. /* traditional emoticons */
  226. xs *d = emojis();
  227. int c = 0;
  228. const char *k, *v;
  229. while (xs_dict_next(d, &k, &v, &c)) {
  230. const char *t = NULL;
  231. /* is it an URL to an image? */
  232. if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) {
  233. if (tag && xs_str_in(s, k) != -1) {
  234. /* add the emoji to the tag list */
  235. xs *e = xs_dict_new();
  236. xs *i = xs_dict_new();
  237. xs *u = xs_str_utctime(0, ISO_DATE_SPEC);
  238. e = xs_dict_append(e, "id", v);
  239. e = xs_dict_append(e, "type", "Emoji");
  240. e = xs_dict_append(e, "name", k);
  241. e = xs_dict_append(e, "updated", u);
  242. i = xs_dict_append(i, "type", "Image");
  243. i = xs_dict_append(i, "mediaType", t);
  244. i = xs_dict_append(i, "url", v);
  245. e = xs_dict_append(e, "icon", i);
  246. *tag = xs_list_append(*tag, e);
  247. }
  248. }
  249. else
  250. s = xs_replace_i(s, k, v);
  251. }
  252. }
  253. return s;
  254. }
  255. const char *valid_tags[] = {
  256. "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
  257. "span", "i", "b", "u", "s", "pre", "code", "em", "strong", "hr", "img", "del", "bdi", NULL
  258. };
  259. xs_str *sanitize(const char *content)
  260. /* cleans dangerous HTML output */
  261. {
  262. xs_str *s = xs_str_new(NULL);
  263. xs *sl;
  264. int n = 0;
  265. char *p;
  266. const char *v;
  267. sl = xs_regex_split(content, "</?[^>]+>");
  268. p = sl;
  269. n = 0;
  270. while (xs_list_iter(&p, &v)) {
  271. if (n & 0x1) {
  272. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  273. xs *l1 = xs_split_n(s1, " ", 1);
  274. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  275. xs *s2 = NULL;
  276. int i;
  277. /* check if it's one of the valid tags */
  278. for (i = 0; valid_tags[i]; i++) {
  279. if (strcmp(tag, valid_tags[i]) == 0)
  280. break;
  281. }
  282. if (valid_tags[i]) {
  283. /* accepted tag: rebuild it with only the accepted elements */
  284. xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\"");
  285. xs *s3 = xs_join(el, " ");
  286. s2 = xs_fmt("<%s%s%s%s>",
  287. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  288. s = xs_str_cat(s, s2);
  289. } else {
  290. /* treat end of divs as paragraph breaks */
  291. if (strcmp(v, "</div>"))
  292. s = xs_str_cat(s, "<p>");
  293. }
  294. }
  295. else {
  296. /* non-tag */
  297. s = xs_str_cat(s, v);
  298. }
  299. n++;
  300. }
  301. return s;
  302. }
  303. xs_str *encode_html(const char *str)
  304. /* escapes html characters */
  305. {
  306. xs_str *encoded = xs_html_encode((char *)str);
  307. /* Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. */
  308. encoded = xs_replace_i(encoded, "&lt;br&gt;", "<br>");
  309. return encoded;
  310. }