format.c 15 KB

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