format.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 *v2 = xs_strip_chars_i(xs_dup(v), ".");
  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, v);
  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. ss = xs_dup(v);
  117. else
  118. ss = xs_strip_i(format_line(v, attach));
  119. if (xs_startswith(ss, ">")) {
  120. /* delete the > and subsequent spaces */
  121. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  122. if (!in_blq) {
  123. s = xs_str_cat(s, "<blockquote>");
  124. in_blq = 1;
  125. }
  126. s = xs_str_cat(s, ss);
  127. s = xs_str_cat(s, "<br>");
  128. continue;
  129. }
  130. if (in_blq) {
  131. s = xs_str_cat(s, "</blockquote>");
  132. in_blq = 0;
  133. }
  134. s = xs_str_cat(s, ss);
  135. s = xs_str_cat(s, "<br>");
  136. }
  137. if (in_blq)
  138. s = xs_str_cat(s, "</blockquote>");
  139. if (in_pre)
  140. s = xs_str_cat(s, "</pre>");
  141. /* some beauty fixes */
  142. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  143. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  144. s = xs_replace_i(s, "</pre><br>", "</pre>");
  145. {
  146. /* traditional emoticons */
  147. int n;
  148. for (n = 0; smileys[n].key; n++)
  149. s = xs_replace_i(s, smileys[n].key, smileys[n].value);
  150. }
  151. return s;
  152. }
  153. const char *valid_tags[] = {
  154. "a", "p", "br", "br/", "blockquote", "ul", "li", "cite",
  155. "span", "i", "b", "u", "pre", "code", "em", "strong", NULL
  156. };
  157. xs_str *sanitize(const char *content)
  158. /* cleans dangerous HTML output */
  159. {
  160. xs_str *s = xs_str_new(NULL);
  161. xs *sl;
  162. int n = 0;
  163. char *p, *v;
  164. sl = xs_regex_split(content, "</?[^>]+>");
  165. p = sl;
  166. n = 0;
  167. while (xs_list_iter(&p, &v)) {
  168. if (n & 0x1) {
  169. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  170. xs *l1 = xs_split_n(s1, " ", 1);
  171. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  172. xs *s2 = NULL;
  173. int i;
  174. /* check if it's one of the valid tags */
  175. for (i = 0; valid_tags[i]; i++) {
  176. if (strcmp(tag, valid_tags[i]) == 0)
  177. break;
  178. }
  179. if (valid_tags[i]) {
  180. /* accepted tag: rebuild it with only the accepted elements */
  181. xs *el = xs_regex_match(v, "(href|rel|class|target)=\"[^\"]*\"");
  182. xs *s3 = xs_join(el, " ");
  183. s2 = xs_fmt("<%s%s%s%s>",
  184. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  185. }
  186. else {
  187. /* bad tag: escape it */
  188. s2 = xs_replace(v, "<", "&lt;");
  189. }
  190. s = xs_str_cat(s, s2);
  191. }
  192. else {
  193. /* non-tag */
  194. s = xs_str_cat(s, v);
  195. }
  196. n++;
  197. }
  198. return s;
  199. }