format.c 13 KB

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