format.c 9.8 KB

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