format.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 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 "snac.h"
  8. /* emoticons, people laughing and such */
  9. const char *smileys[] = {
  10. ":-)", "🙂",
  11. ":-D", "😀",
  12. "X-D", "😆",
  13. ";-)", "😉",
  14. "B-)", "😎",
  15. ">:-(", "😡",
  16. ":-(", "😞",
  17. ":-*", "😘",
  18. ":-/", "😕",
  19. "8-o", "😲",
  20. "%-)", "🤪",
  21. ":_(", "😢",
  22. ":-|", "😐",
  23. "<3", "&#10084;&#65039;",
  24. ":facepalm:", "&#129318;",
  25. ":shrug:", "&#129335;",
  26. ":shrug2:", "&#175;\\_(&#12484;)_/&#175;",
  27. ":eyeroll:", "&#128580;",
  28. ":beer:", "&#127866;",
  29. ":beers:", "&#127867;",
  30. ":munch:", "&#128561;",
  31. ":thumb:", "&#128077;",
  32. NULL, NULL
  33. };
  34. static xs_str *format_line(const char *line, xs_list **attach)
  35. /* formats a line */
  36. {
  37. xs_str *s = xs_str_new(NULL);
  38. char *p, *v;
  39. /* split by markup */
  40. xs *sm = xs_regex_split(line,
  41. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
  42. int n = 0;
  43. p = sm;
  44. while (xs_list_iter(&p, &v)) {
  45. if ((n & 0x1)) {
  46. /* markup */
  47. if (xs_startswith(v, "`")) {
  48. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  49. xs *e1 = encode_html(s1);
  50. xs *s2 = xs_fmt("<code>%s</code>", e1);
  51. s = xs_str_cat(s, s2);
  52. }
  53. else
  54. if (xs_startswith(v, "**")) {
  55. xs *s1 = xs_crop_i(xs_dup(v), 2, -2);
  56. xs *s2 = xs_fmt("<b>%s</b>", s1);
  57. s = xs_str_cat(s, s2);
  58. }
  59. else
  60. if (xs_startswith(v, "*")) {
  61. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  62. xs *s2 = xs_fmt("<i>%s</i>", s1);
  63. s = xs_str_cat(s, s2);
  64. }
  65. else
  66. if (xs_startswith(v, "http")) {
  67. xs *u = xs_replace(v, "#", "&#35;");
  68. xs *v2 = xs_strip_chars_i(xs_dup(u), ".");
  69. const char *mime = xs_mime_by_ext(v2);
  70. if (attach != NULL && xs_startswith(mime, "image/")) {
  71. /* if it's a link to an image, insert it as an attachment */
  72. xs *d = xs_dict_new();
  73. d = xs_dict_append(d, "mediaType", mime);
  74. d = xs_dict_append(d, "url", v2);
  75. d = xs_dict_append(d, "name", "");
  76. d = xs_dict_append(d, "type", "Image");
  77. *attach = xs_list_append(*attach, d);
  78. }
  79. else {
  80. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
  81. s = xs_str_cat(s, s1);
  82. }
  83. }
  84. else
  85. s = xs_str_cat(s, v);
  86. }
  87. else
  88. /* surrounded text, copy directly */
  89. s = xs_str_cat(s, v);
  90. n++;
  91. }
  92. return s;
  93. }
  94. xs_str *not_really_markdown(const char *content, xs_list **attach)
  95. /* formats a content using some Markdown rules */
  96. {
  97. xs_str *s = xs_str_new(NULL);
  98. int in_pre = 0;
  99. int in_blq = 0;
  100. xs *list;
  101. char *p, *v;
  102. /* work by lines */
  103. list = xs_split(content, "\n");
  104. p = list;
  105. while (xs_list_iter(&p, &v)) {
  106. xs *ss = NULL;
  107. if (strcmp(v, "```") == 0) {
  108. if (!in_pre)
  109. s = xs_str_cat(s, "<pre>");
  110. else
  111. s = xs_str_cat(s, "</pre>");
  112. in_pre = !in_pre;
  113. continue;
  114. }
  115. if (in_pre) {
  116. // Encode all HTML characters when we're in pre element until we are out.
  117. ss = encode_html(v);
  118. s = xs_str_cat(s, ss);
  119. s = xs_str_cat(s, "<br>");
  120. continue;
  121. }
  122. else
  123. ss = xs_strip_i(format_line(v, attach));
  124. if (xs_startswith(ss, "---")) {
  125. /* delete the --- */
  126. ss = xs_strip_i(xs_crop_i(ss, 3, 0));
  127. s = xs_str_cat(s, "<hr>");
  128. s = xs_str_cat(s, ss);
  129. continue;
  130. }
  131. if (xs_startswith(ss, ">")) {
  132. /* delete the > and subsequent spaces */
  133. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  134. if (!in_blq) {
  135. s = xs_str_cat(s, "<blockquote>");
  136. in_blq = 1;
  137. }
  138. s = xs_str_cat(s, ss);
  139. s = xs_str_cat(s, "<br>");
  140. continue;
  141. }
  142. if (in_blq) {
  143. s = xs_str_cat(s, "</blockquote>");
  144. in_blq = 0;
  145. }
  146. s = xs_str_cat(s, ss);
  147. s = xs_str_cat(s, "<br>");
  148. }
  149. if (in_blq)
  150. s = xs_str_cat(s, "</blockquote>");
  151. if (in_pre)
  152. s = xs_str_cat(s, "</pre>");
  153. /* some beauty fixes */
  154. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  155. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  156. s = xs_replace_i(s, "</pre><br>", "</pre>");
  157. {
  158. /* traditional emoticons */
  159. const char **emo = smileys;
  160. while (*emo) {
  161. s = xs_replace_i(s, emo[0], emo[1]);
  162. emo += 2;
  163. }
  164. }
  165. return s;
  166. }
  167. const char *valid_tags[] = {
  168. "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
  169. "span", "i", "b", "u", "s", "pre", "code", "em", "strong", "hr", "img", "del", NULL
  170. };
  171. xs_str *sanitize(const char *content)
  172. /* cleans dangerous HTML output */
  173. {
  174. xs_str *s = xs_str_new(NULL);
  175. xs *sl;
  176. int n = 0;
  177. char *p, *v;
  178. sl = xs_regex_split(content, "</?[^>]+>");
  179. p = sl;
  180. n = 0;
  181. while (xs_list_iter(&p, &v)) {
  182. if (n & 0x1) {
  183. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  184. xs *l1 = xs_split_n(s1, " ", 1);
  185. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  186. xs *s2 = NULL;
  187. int i;
  188. /* check if it's one of the valid tags */
  189. for (i = 0; valid_tags[i]; i++) {
  190. if (strcmp(tag, valid_tags[i]) == 0)
  191. break;
  192. }
  193. if (valid_tags[i]) {
  194. /* accepted tag: rebuild it with only the accepted elements */
  195. xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\"");
  196. xs *s3 = xs_join(el, " ");
  197. s2 = xs_fmt("<%s%s%s%s>",
  198. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  199. s = xs_str_cat(s, s2);
  200. } else {
  201. /* else? just show it with encoded code.. that's it. */
  202. xs *el = encode_html(v);
  203. s = xs_str_cat(s, el);
  204. }
  205. }
  206. else {
  207. /* non-tag */
  208. s = xs_str_cat(s, v);
  209. }
  210. n++;
  211. }
  212. return s;
  213. }
  214. xs_str *encode_html(const char *str)
  215. /* escapes html characters */
  216. {
  217. xs_str *encoded = xs_html_encode((char *)str);
  218. /* Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. */
  219. encoded = xs_replace_i(encoded, "&lt;br&gt;", "<br>");
  220. return encoded;
  221. }