format.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 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 "xs_match.h"
  10. #include "snac.h"
  11. /* emoticons, people laughing and such */
  12. const char *smileys[] = {
  13. ":-)", "🙂",
  14. ":-D", "😀",
  15. "X-D", "😆",
  16. ";-)", "😉",
  17. "B-)", "😎",
  18. ">:-(", "😡",
  19. ":-(", "😞",
  20. ":-*", "😘",
  21. ":-/", "😕",
  22. "8-o", "😲",
  23. "%-)", "🤪",
  24. ":_(", "😢",
  25. ":-|", "😐",
  26. "<3", "&#10084;&#65039;",
  27. ":facepalm:", "&#129318;",
  28. ":shrug:", "&#129335;",
  29. ":shrug2:", "&#175;\\_(&#12484;)_/&#175;",
  30. ":eyeroll:", "&#128580;",
  31. ":beer:", "&#127866;",
  32. ":beers:", "&#127867;",
  33. ":munch:", "&#128561;",
  34. ":thumb:", "&#128077;",
  35. NULL, NULL
  36. };
  37. xs_dict *emojis(void)
  38. /* returns a dict with the emojis */
  39. {
  40. xs *fn = xs_fmt("%s/emojis.json", srv_basedir);
  41. FILE *f;
  42. if (mtime(fn) == 0) {
  43. /* file does not exist; create it with the defaults */
  44. xs *d = xs_dict_new();
  45. const char **emo = smileys;
  46. while (*emo) {
  47. d = xs_dict_append(d, emo[0], emo[1]);
  48. emo += 2;
  49. }
  50. if ((f = fopen(fn, "w")) != NULL) {
  51. xs_json_dump(d, 4, f);
  52. fclose(f);
  53. }
  54. else
  55. srv_log(xs_fmt("Error creating '%s'", fn));
  56. }
  57. xs_dict *d = NULL;
  58. if ((f = fopen(fn, "r")) != NULL) {
  59. d = xs_json_load(f);
  60. fclose(f);
  61. if (d == NULL)
  62. srv_log(xs_fmt("JSON parse error in '%s'", fn));
  63. }
  64. else
  65. srv_log(xs_fmt("Error opening '%s'", fn));
  66. return d;
  67. }
  68. static xs_str *format_line(const char *line, xs_list **attach)
  69. /* formats a line */
  70. {
  71. xs_str *s = xs_str_new(NULL);
  72. char *p;
  73. const char *v;
  74. /* split by markup */
  75. xs *sm = xs_regex_split(line,
  76. "("
  77. "`[^`]+`" "|"
  78. "~~[^~]+~~" "|"
  79. "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*" "|"
  80. "_[^_]+_" "|" //anzu
  81. "__[^_]+__" "|" //anzu
  82. "!\\[[^]]+\\]\\([^\\)]+\\)" "|"
  83. "\\[[^]]+\\]\\([^\\)]+\\)" "|"
  84. "[a-z]+:/" "/[^[:space:]]+" "|"
  85. "(mailto|xmpp):[^@[:space:]]+@[^[:space:]]+"
  86. ")");
  87. int n = 0;
  88. p = sm;
  89. while (xs_list_iter(&p, &v)) {
  90. if ((n & 0x1)) {
  91. /* markup */
  92. if (xs_startswith(v, "`")) {
  93. xs *s1 = xs_strip_chars_i(xs_dup(v), "`");
  94. xs *e1 = encode_html(s1);
  95. xs *s2 = xs_fmt("<code>%s</code>", e1);
  96. s = xs_str_cat(s, s2);
  97. }
  98. else
  99. if (xs_startswith(v, "***")) {
  100. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  101. xs *s2 = xs_fmt("<b><i>%s</i></b>", s1);
  102. s = xs_str_cat(s, s2);
  103. }
  104. else
  105. if (xs_startswith(v, "**")) {
  106. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  107. xs *s2 = xs_fmt("<b>%s</b>", s1);
  108. s = xs_str_cat(s, s2);
  109. }
  110. else
  111. if (xs_startswith(v, "*")) {
  112. xs *s1 = xs_strip_chars_i(xs_dup(v), "*");
  113. xs *s2 = xs_fmt("<i>%s</i>", s1);
  114. s = xs_str_cat(s, s2);
  115. }
  116. //anzu - begin
  117. else
  118. if (xs_startswith(v, "__")) {
  119. xs *s1 = xs_strip_chars_i(xs_dup(v), "_");
  120. xs *s2 = xs_fmt("<u>%s</u>", s1);
  121. s = xs_str_cat(s, s2);
  122. }
  123. else
  124. if (xs_startswith(v, "_")) {
  125. xs *s1 = xs_strip_chars_i(xs_dup(v), "_");
  126. xs *s2 = xs_fmt("<i>%s</i>", s1);
  127. s = xs_str_cat(s, s2);
  128. }
  129. //anzu - end
  130. else
  131. if (xs_startswith(v, "~~")) {
  132. xs *s1 = xs_strip_chars_i(xs_dup(v), "~");
  133. xs *e1 = encode_html(s1);
  134. xs *s2 = xs_fmt("<s>%s</s>", e1);
  135. s = xs_str_cat(s, s2);
  136. }
  137. else
  138. if (*v == '[') {
  139. /* markdown-like links [label](url) */
  140. xs *w = xs_strip_chars_i(
  141. xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
  142. "![)");
  143. xs *l = xs_split_n(w, "](", 1);
  144. if (xs_list_len(l) == 2) {
  145. xs *link = xs_fmt("<a href=\"%s\">%s</a>",
  146. xs_list_get(l, 1), xs_list_get(l, 0));
  147. s = xs_str_cat(s, link);
  148. }
  149. else
  150. s = xs_str_cat(s, v);
  151. }
  152. else
  153. if (*v == '!') {
  154. /* markdown-like images ![alt text](url to image) */
  155. xs *w = xs_strip_chars_i(
  156. xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;"),
  157. "![)");
  158. xs *l = xs_split_n(w, "](", 1);
  159. if (xs_list_len(l) == 2) {
  160. const char *alt_text = xs_list_get(l, 0);
  161. const char *img_url = xs_list_get(l, 1);
  162. const char *mime = xs_mime_by_ext(img_url);
  163. if (attach != NULL && xs_startswith(mime, "image/")) {
  164. const xs_dict *ad;
  165. int add = 1;
  166. xs_list_foreach(*attach, ad) {
  167. if (strcmp(xs_dict_get_def(ad, "url", ""), img_url) == 0) {
  168. add = 0;
  169. break;
  170. }
  171. }
  172. if (add) {
  173. xs *d = xs_dict_new();
  174. d = xs_dict_append(d, "mediaType", mime);
  175. d = xs_dict_append(d, "url", img_url);
  176. d = xs_dict_append(d, "name", alt_text);
  177. d = xs_dict_append(d, "type", "Image");
  178. *attach = xs_list_append(*attach, d);
  179. }
  180. }
  181. else {
  182. xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
  183. s = xs_str_cat(s, link);
  184. }
  185. }
  186. else
  187. s = xs_str_cat(s, v);
  188. }
  189. else
  190. if (xs_str_in(v, ":/" "/") != -1) {
  191. xs *u = xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;");
  192. xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
  193. const char *mime = xs_mime_by_ext(v2);
  194. if (attach != NULL && xs_startswith(mime, "image/")) {
  195. /* if it's a link to an image, insert it as an attachment */
  196. const xs_dict *ad;
  197. int add = 1;
  198. xs_list_foreach(*attach, ad) {
  199. if (strcmp(xs_dict_get_def(ad, "url", ""), v2) == 0) {
  200. add = 0;
  201. break;
  202. }
  203. }
  204. if (add) {
  205. xs *d = xs_dict_new();
  206. d = xs_dict_append(d, "mediaType", mime);
  207. d = xs_dict_append(d, "url", v2);
  208. d = xs_dict_append(d, "name", "");
  209. d = xs_dict_append(d, "type", "Image");
  210. *attach = xs_list_append(*attach, d);
  211. }
  212. }
  213. else {
  214. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
  215. s = xs_str_cat(s, s1);
  216. }
  217. }
  218. else
  219. if (xs_match(v, "mailto*|xmpp*")) {
  220. xs *u = xs_replace_i(xs_replace(v, "#", "&#35;"), "@", "&#64;");
  221. xs *v2 = xs_strip_chars_i(xs_dup(u), ".,)");
  222. xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v2, u);
  223. s = xs_str_cat(s, s1);
  224. }
  225. else
  226. s = xs_str_cat(s, v);
  227. }
  228. else
  229. /* surrounded text, copy directly */
  230. s = xs_str_cat(s, v);
  231. n++;
  232. }
  233. return s;
  234. }
  235. xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag)
  236. /* formats a content using some Markdown rules */
  237. {
  238. xs_str *s = xs_str_new(NULL);
  239. int in_pre = 0;
  240. int in_blq = 0;
  241. xs *list;
  242. char *p;
  243. const char *v;
  244. /* work by lines */
  245. list = xs_split(content, "\n");
  246. p = list;
  247. while (xs_list_iter(&p, &v)) {
  248. xs *ss = NULL;
  249. if (strcmp(v, "```") == 0) {
  250. if (!in_pre)
  251. s = xs_str_cat(s, "<pre>");
  252. else
  253. s = xs_str_cat(s, "</pre>");
  254. in_pre = !in_pre;
  255. continue;
  256. }
  257. if (in_pre) {
  258. // Encode all HTML characters when we're in pre element until we are out.
  259. ss = encode_html(v);
  260. s = xs_str_cat(s, ss);
  261. s = xs_str_cat(s, "<br>");
  262. continue;
  263. }
  264. else
  265. ss = xs_strip_i(format_line(v, attach));
  266. if (xs_startswith(ss, "---")) {
  267. /* delete the --- */
  268. ss = xs_strip_i(xs_crop_i(ss, 3, 0));
  269. s = xs_str_cat(s, "<hr>");
  270. s = xs_str_cat(s, ss);
  271. continue;
  272. }
  273. //anzu - begin
  274. // h1 reserved for snac?
  275. if (xs_startswith(ss, "# ")) {
  276. ss = xs_strip_i(xs_crop_i(ss, 2, 0));
  277. s = xs_str_cat(s, "<h2>");
  278. s = xs_str_cat(s, ss);
  279. s = xs_str_cat(s, "</h2>");
  280. continue;
  281. }
  282. if (xs_startswith(ss, "## ")) {
  283. ss = xs_strip_i(xs_crop_i(ss, 3, 0));
  284. s = xs_str_cat(s, "<h2>");
  285. s = xs_str_cat(s, ss);
  286. s = xs_str_cat(s, "</h2>");
  287. continue;
  288. }
  289. if (xs_startswith(ss, "### ")) {
  290. ss = xs_strip_i(xs_crop_i(ss, 4, 0));
  291. s = xs_str_cat(s, "<h3>");
  292. s = xs_str_cat(s, ss);
  293. s = xs_str_cat(s, "</h3>");
  294. continue;
  295. }
  296. //anzu - end
  297. if (xs_startswith(ss, ">")) {
  298. /* delete the > and subsequent spaces */
  299. ss = xs_strip_i(xs_crop_i(ss, 1, 0));
  300. if (!in_blq) {
  301. s = xs_str_cat(s, "<blockquote>");
  302. in_blq = 1;
  303. }
  304. s = xs_str_cat(s, ss);
  305. s = xs_str_cat(s, "<br>");
  306. continue;
  307. }
  308. if (in_blq) {
  309. s = xs_str_cat(s, "</blockquote>");
  310. in_blq = 0;
  311. }
  312. s = xs_str_cat(s, ss);
  313. s = xs_str_cat(s, "<br>");
  314. }
  315. if (in_blq)
  316. s = xs_str_cat(s, "</blockquote>");
  317. if (in_pre)
  318. s = xs_str_cat(s, "</pre>");
  319. /* some beauty fixes */
  320. s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
  321. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  322. s = xs_replace_i(s, "</pre><br>", "</pre>");
  323. s = xs_replace_i(s, "</h2><br>", "</h2>"); //anzu ???
  324. s = xs_replace_i(s, "</h3><br>", "</h3>"); //anzu ???
  325. {
  326. /* traditional emoticons */
  327. xs *d = emojis();
  328. int c = 0;
  329. const char *k, *v;
  330. while (xs_dict_next(d, &k, &v, &c)) {
  331. const char *t = NULL;
  332. /* is it an URL to an image? */
  333. if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) {
  334. if (tag && xs_str_in(s, k) != -1) {
  335. /* add the emoji to the tag list */
  336. xs *e = xs_dict_new();
  337. xs *i = xs_dict_new();
  338. xs *u = xs_str_utctime(0, ISO_DATE_SPEC);
  339. e = xs_dict_append(e, "id", v);
  340. e = xs_dict_append(e, "type", "Emoji");
  341. e = xs_dict_append(e, "name", k);
  342. e = xs_dict_append(e, "updated", u);
  343. i = xs_dict_append(i, "type", "Image");
  344. i = xs_dict_append(i, "mediaType", t);
  345. i = xs_dict_append(i, "url", v);
  346. e = xs_dict_append(e, "icon", i);
  347. *tag = xs_list_append(*tag, e);
  348. }
  349. }
  350. else
  351. s = xs_replace_i(s, k, v);
  352. }
  353. }
  354. return s;
  355. }
  356. const char *valid_tags[] = {
  357. "a", "p", "br", "br/", "blockquote", "ul", "ol", "li", "cite", "small",
  358. "span", "i", "b", "u", "s", "pre", "code", "em", "strong", "hr", "img", "del", "bdi",
  359. "h2","h3", //anzu
  360. NULL
  361. };
  362. xs_str *sanitize(const char *content)
  363. /* cleans dangerous HTML output */
  364. {
  365. xs_str *s = xs_str_new(NULL);
  366. xs *sl;
  367. int n = 0;
  368. char *p;
  369. const char *v;
  370. sl = xs_regex_split(content, "</?[^>]+>");
  371. p = sl;
  372. n = 0;
  373. while (xs_list_iter(&p, &v)) {
  374. if (n & 0x1) {
  375. xs *s1 = xs_strip_i(xs_crop_i(xs_dup(v), v[1] == '/' ? 2 : 1, -1));
  376. xs *l1 = xs_split_n(s1, " ", 1);
  377. xs *tag = xs_tolower_i(xs_dup(xs_list_get(l1, 0)));
  378. xs *s2 = NULL;
  379. int i;
  380. /* check if it's one of the valid tags */
  381. for (i = 0; valid_tags[i]; i++) {
  382. if (strcmp(tag, valid_tags[i]) == 0)
  383. break;
  384. }
  385. if (valid_tags[i]) {
  386. /* accepted tag: rebuild it with only the accepted elements */
  387. xs *el = xs_regex_select(v, "(src|href|rel|class|target)=\"[^\"]*\"");
  388. xs *s3 = xs_join(el, " ");
  389. s2 = xs_fmt("<%s%s%s%s>",
  390. v[1] == '/' ? "/" : "", tag, xs_list_len(el) ? " " : "", s3);
  391. s = xs_str_cat(s, s2);
  392. } else {
  393. /* treat end of divs as paragraph breaks */
  394. if (strcmp(v, "</div>"))
  395. s = xs_str_cat(s, "<p>");
  396. }
  397. }
  398. else {
  399. /* non-tag */
  400. s = xs_str_cat(s, v);
  401. }
  402. n++;
  403. }
  404. return s;
  405. }
  406. xs_str *encode_html(const char *str)
  407. /* escapes html characters */
  408. {
  409. xs_str *encoded = xs_html_encode((char *)str);
  410. /* Restore only <br>. Probably safe. Let's hope nothing goes wrong with this. */
  411. encoded = xs_replace_i(encoded, "&lt;br&gt;", "<br>");
  412. return encoded;
  413. }