format.c 9.1 KB

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