1
0

format.c 15 KB

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