format.c 13 KB

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