format.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_regex.h"
  5. #include "xs_mime.h"
  6. #include "snac.h"
  7. /* emoticons, people laughing and such */
  8. struct {
  9. const char *key;
  10. const char *value;
  11. } smileys[] = {
  12. { ":-)", "🙂" },
  13. { ":-D", "😀" },
  14. { "X-D", "😆" },
  15. { ";-)", "😉" },
  16. { "B-)", "😎" },
  17. { ">:-(", "😡" },
  18. { ":-(", "😞" },
  19. { ":-*", "😘" },
  20. { ":-/", "😕" },
  21. { "8-o", "😲" },
  22. { "%-)", "🤪" },
  23. { ":_(", "😢" },
  24. { ":-|", "😐" },
  25. { "<3", "&#128147;" },
  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. static xs_str *format_line(const char *line, xs_list **attach)
  37. /* formats a line */
  38. {
  39. xs_str *s = xs_str_new(NULL);
  40. char *p, *v;
  41. /* split by markup */
  42. xs *sm = xs_regex_split(line,
  43. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
  44. int n = 0;
  45. p = sm;
  46. while (xs_list_iter(&p, &v)) {
  47. if ((n & 0x1)) {
  48. /* markup */
  49. if (xs_startswith(v, "`")) {
  50. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  51. xs *s2 = xs_fmt("<code>%s</code>", s1);
  52. s = xs_str_cat(s, s2);
  53. }
  54. else
  55. if (xs_startswith(v, "**")) {
  56. xs *s1 = xs_crop_i(xs_dup(v), 2, -2);
  57. xs *s2 = xs_fmt("<b>%s</b>", s1);
  58. s = xs_str_cat(s, s2);
  59. }
  60. else
  61. if (xs_startswith(v, "*")) {
  62. xs *s1 = xs_crop_i(xs_dup(v), 1, -1);
  63. xs *s2 = xs_fmt("<i>%s</i>", s1);
  64. s = xs_str_cat(s, s2);
  65. }
  66. else
  67. if (xs_startswith(v, "http")) {
  68. xs *u = xs_replace(v, "#", "&#35;");
  69. xs *v2 = xs_strip_chars_i(xs_dup(u), ".");
  70. const char *mime = xs_mime_by_ext(v2);
  71. if (attach != NULL && xs_startswith(mime, "image/")) {
  72. /* if it's a link to an image, insert it as an attachment */
  73. xs *d = xs_dict_new();
  74. d = xs_dict_append(d, "mediaType", mime);
  75. d = xs_dict_append(d, "url", v2);
  76. d = xs_dict_append(d, "name", "");
  77. d = xs_dict_append(d, "type", "Image");
  78. *attach = xs_list_append(*attach, d);
  79. }
  80. else {
  81. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
  82. s = xs_str_cat(s, s1);
  83. }
  84. }
  85. else
  86. s = xs_str_cat(s, v);
  87. }
  88. else
  89. /* surrounded text, copy directly */
  90. s = xs_str_cat(s, v);
  91. n++;
  92. }
  93. return s;
  94. }
  95. xs_str *not_really_markdown(const char *content, xs_list **attach)
  96. /* formats a content using some Markdown rules */
  97. {
  98. xs_str *s = xs_str_new(NULL);
  99. int in_pre = 0;
  100. int in_blq = 0;
  101. xs *list;
  102. char *p, *v;
  103. /* work by lines */
  104. list = xs_split(content, "\n");
  105. p = list;
  106. while (xs_list_iter(&p, &v)) {
  107. xs *ss = NULL;
  108. if (strcmp(v, "```") == 0) {
  109. if (!in_pre)
  110. s = xs_str_cat(s, "<pre>");
  111. else
  112. s = xs_str_cat(s, "</pre>");
  113. in_pre = !in_pre;
  114. continue;
  115. }
  116. if (in_pre)
  117. ss = xs_dup(v);
  118. else
  119. ss = xs_strip_i(format_line(v, attach));
  120. if (xs_startswith(ss, ">")) {
  121. /* delete the > and subsequent spaces */
  122. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  123. if (!in_blq) {
  124. s = xs_str_cat(s, "<blockquote>");
  125. in_blq = 1;
  126. }
  127. s = xs_str_cat(s, ss);
  128. s = xs_str_cat(s, "<br>");
  129. continue;
  130. }
  131. if (in_blq) {
  132. s = xs_str_cat(s, "</blockquote>");
  133. in_blq = 0;
  134. }
  135. s = xs_str_cat(s, ss);
  136. s = xs_str_cat(s, "<br>");
  137. }
  138. if (in_blq)
  139. s = xs_str_cat(s, "</blockquote>");
  140. if (in_pre)
  141. s = xs_str_cat(s, "</pre>");
  142. /* some beauty fixes */
  143. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  144. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  145. s = xs_replace_i(s, "</pre><br>", "</pre>");
  146. {
  147. /* traditional emoticons */
  148. int n;
  149. for (n = 0; smileys[n].key; n++)
  150. s = xs_replace_i(s, smileys[n].key, smileys[n].value);
  151. }
  152. return s;
  153. }
  154. const char *valid_tags[] = {
  155. "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite",
  156. "span", "i", "b", "u", "pre", "code", "em", "strong", NULL
  157. };
  158. xs_str *sanitize(const char *content)
  159. /* cleans dangerous HTML output */
  160. {
  161. xs_str *s = xs_str_new(NULL);
  162. xs *sl;
  163. int n = 0;
  164. char *p, *v;
  165. sl = xs_regex_split(content, "</?[^>]+>");
  166. p = sl;
  167. n = 0;
  168. while (xs_list_iter(&p, &v)) {
  169. if (n & 0x1) {
  170. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  171. xs *l1 = xs_split_n(s1, " ", 1);
  172. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  173. xs *s2 = NULL;
  174. int i;
  175. /* check if it's one of the valid tags */
  176. for (i = 0; valid_tags[i]; i++) {
  177. if (strcmp(tag, valid_tags[i]) == 0)
  178. break;
  179. }
  180. if (valid_tags[i]) {
  181. /* accepted tag: rebuild it with only the accepted elements */
  182. xs *el = xs_regex_match(v, "(href|rel|class|target)=\"[^\"]*\"");
  183. xs *s3 = xs_join(el, " ");
  184. s2 = xs_fmt("<%s%s%s%s>",
  185. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  186. s = xs_str_cat(s, s2);
  187. }
  188. }
  189. else {
  190. /* non-tag */
  191. s = xs_str_cat(s, v);
  192. }
  193. n++;
  194. }
  195. return s;
  196. }