html.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_json.h"
  7. #include "xs_regex.h"
  8. #include "xs_set.h"
  9. #include "snac.h"
  10. d_char *not_really_markdown(char *content, d_char **f_content)
  11. /* formats a content using some Markdown rules */
  12. {
  13. d_char *s = NULL;
  14. int in_pre = 0;
  15. int in_blq = 0;
  16. xs *list;
  17. char *p, *v;
  18. xs *wrk = xs_str_new(NULL);
  19. {
  20. /* split by special markup */
  21. xs *sm = xs_regex_split(content,
  22. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
  23. int n = 0;
  24. p = sm;
  25. while (xs_list_iter(&p, &v)) {
  26. if ((n & 0x1)) {
  27. /* markup */
  28. if (xs_startswith(v, "`")) {
  29. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  30. xs *s2 = xs_fmt("<code>%s</code>", s1);
  31. wrk = xs_str_cat(wrk, s2);
  32. }
  33. else
  34. if (xs_startswith(v, "**")) {
  35. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  36. xs *s2 = xs_fmt("<b>%s</b>", s1);
  37. wrk = xs_str_cat(wrk, s2);
  38. }
  39. else
  40. if (xs_startswith(v, "*")) {
  41. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  42. xs *s2 = xs_fmt("<i>%s</i>", s1);
  43. wrk = xs_str_cat(wrk, s2);
  44. }
  45. else
  46. if (xs_startswith(v, "http")) {
  47. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  48. wrk = xs_str_cat(wrk, s1);
  49. }
  50. else
  51. /* what the hell is this */
  52. wrk = xs_str_cat(wrk, v);
  53. }
  54. else
  55. /* surrounded text, copy directly */
  56. wrk = xs_str_cat(wrk, v);
  57. n++;
  58. }
  59. }
  60. /* now work by lines */
  61. p = list = xs_split(wrk, "\n");
  62. s = xs_str_new(NULL);
  63. while (xs_list_iter(&p, &v)) {
  64. xs *ss = xs_strip(xs_dup(v));
  65. if (xs_startswith(ss, "```")) {
  66. if (!in_pre)
  67. s = xs_str_cat(s, "<pre>");
  68. else
  69. s = xs_str_cat(s, "</pre>");
  70. in_pre = !in_pre;
  71. continue;
  72. }
  73. if (xs_startswith(ss, ">")) {
  74. /* delete the > and subsequent spaces */
  75. ss = xs_strip(xs_crop(ss, 1, 0));
  76. if (!in_blq) {
  77. s = xs_str_cat(s, "<blockquote>");
  78. in_blq = 1;
  79. }
  80. s = xs_str_cat(s, ss);
  81. s = xs_str_cat(s, "<br>");
  82. continue;
  83. }
  84. if (in_blq) {
  85. s = xs_str_cat(s, "</blockquote>");
  86. in_blq = 0;
  87. }
  88. s = xs_str_cat(s, ss);
  89. s = xs_str_cat(s, "<br>");
  90. }
  91. if (in_blq)
  92. s = xs_str_cat(s, "</blockquote>");
  93. if (in_pre)
  94. s = xs_str_cat(s, "</pre>");
  95. /* some beauty fixes */
  96. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  97. *f_content = s;
  98. return *f_content;
  99. }
  100. int login(snac *snac, char *headers)
  101. /* tries a login */
  102. {
  103. int logged_in = 0;
  104. char *auth = xs_dict_get(headers, "authorization");
  105. if (auth && xs_startswith(auth, "Basic ")) {
  106. int sz;
  107. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  108. xs *s2 = xs_base64_dec(s1, &sz);
  109. xs *l1 = xs_split_n(s2, ":", 1);
  110. if (xs_list_len(l1) == 2) {
  111. logged_in = check_password(
  112. xs_list_get(l1, 0), xs_list_get(l1, 1),
  113. xs_dict_get(snac->config, "passwd"));
  114. }
  115. }
  116. return logged_in;
  117. }
  118. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  119. {
  120. char *actor_id;
  121. xs *actor = NULL;
  122. xs *s = xs_str_new(NULL);
  123. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  124. actor_id = xs_dict_get(msg, "actor");
  125. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  126. xs *name = NULL;
  127. xs *avatar = NULL;
  128. char *v;
  129. /* get the name */
  130. if ((v = xs_dict_get(actor, "name")) == NULL) {
  131. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  132. v = "user";
  133. }
  134. }
  135. name = xs_dup(v);
  136. /* get the avatar */
  137. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  138. (v = xs_dict_get(v, "url")) != NULL) {
  139. avatar = xs_dup(v);
  140. }
  141. if (avatar == NULL)
  142. avatar = xs_fmt("data:image/png;base64, %s", susie);
  143. {
  144. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  145. s = xs_str_cat(s, s1);
  146. }
  147. {
  148. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  149. actor_id, name);
  150. s = xs_str_cat(s, s1);
  151. }
  152. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  153. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  154. s = xs_str_cat(s, s1);
  155. }
  156. if (!is_msg_public(snac, msg))
  157. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  158. if ((v = xs_dict_get(msg, "published")) == NULL)
  159. v = "&nbsp;";
  160. {
  161. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  162. s = xs_str_cat(s, s1);
  163. }
  164. }
  165. return xs_str_cat(os, s);
  166. }
  167. d_char *html_user_header(snac *snac, d_char *s, int local)
  168. /* creates the HTML header */
  169. {
  170. char *p, *v;
  171. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  172. s = xs_str_cat(s, "<meta name=\"viewport\" "
  173. "content=\"width=device-width, initial-scale=1\"/>\n");
  174. s = xs_str_cat(s, "<meta name=\"generator\" "
  175. "content=\"" USER_AGENT "\"/>\n");
  176. /* add server CSS */
  177. p = xs_dict_get(srv_config, "cssurls");
  178. while (xs_list_iter(&p, &v)) {
  179. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  180. s = xs_str_cat(s, s1);
  181. }
  182. /* add the user CSS */
  183. {
  184. xs *css = NULL;
  185. int size;
  186. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  187. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  188. s = xs_str_cat(s, s1);
  189. }
  190. }
  191. {
  192. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  193. s = xs_str_cat(s, s1);
  194. }
  195. s = xs_str_cat(s, "</head>\n<body>\n");
  196. /* top nav */
  197. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  198. {
  199. xs *s1;
  200. if (local)
  201. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  202. else
  203. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  204. s = xs_str_cat(s, s1);
  205. }
  206. /* user info */
  207. {
  208. xs *bio = NULL;
  209. char *_tmpl =
  210. "<div class=\"h-card snac-top-user\">\n"
  211. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  212. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  213. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  214. "</div>\n";
  215. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  216. xs *s1 = xs_fmt(_tmpl,
  217. xs_dict_get(snac->config, "name"),
  218. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  219. bio
  220. );
  221. s = xs_str_cat(s, s1);
  222. }
  223. return s;
  224. }
  225. d_char *html_top_controls(snac *snac, d_char *s)
  226. /* generates the top controls */
  227. {
  228. char *_tmpl =
  229. "<div class=\"snac-top-controls\">\n"
  230. "<div class=\"snac-note\">\n"
  231. "<form method=\"post\" action=\"%s/admin/note\">\n"
  232. "<textarea class=\"snac-textarea\" name=\"content\" "
  233. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  234. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  235. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  236. "</form><p>\n"
  237. "</div>\n"
  238. "<div class=\"snac-top-controls-more\">\n"
  239. "<details><summary>%s</summary>\n"
  240. "<form method=\"post\" action=\"%s/admin/action\">\n"
  241. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  242. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  243. "</form></p>\n"
  244. "<form method=\"post\" action=\"%s\">\n"
  245. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  246. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  247. "</form></p>\n"
  248. "<details><summary>%s</summary>\n"
  249. "<div class=\"snac-user-setup\">\n"
  250. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  251. "<p>%s:<br>\n"
  252. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  253. "<p>%s:<br>\n"
  254. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  255. "<p>%s:<br>\n"
  256. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  257. "<p>%s:<br>\n"
  258. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  259. "<p>%s:<br>\n"
  260. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  261. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  262. "</form>\n"
  263. "</div>\n"
  264. "</details>\n"
  265. "</details>\n"
  266. "</div>\n"
  267. "</div>\n";
  268. xs *s1 = xs_fmt(_tmpl,
  269. snac->actor,
  270. L("Post"),
  271. L("More options..."),
  272. snac->actor,
  273. L("Follow"), L("(by URL or user@host)"),
  274. snac->actor,
  275. L("Boost"), L("(by URL)"),
  276. L("User setup..."),
  277. snac->actor,
  278. L("User name"),
  279. xs_dict_get(snac->config, "name"),
  280. L("Avatar URL"),
  281. xs_dict_get(snac->config, "avatar"),
  282. L("Bio"),
  283. xs_dict_get(snac->config, "bio"),
  284. L("Password (only to change it)"),
  285. L("Repeat Password"),
  286. L("Update user info")
  287. );
  288. s = xs_str_cat(s, s1);
  289. return s;
  290. }
  291. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int level)
  292. {
  293. char *id = xs_dict_get(msg, "id");
  294. char *type = xs_dict_get(msg, "type");
  295. char *meta = xs_dict_get(msg, "_snac");
  296. xs *actor_o = NULL;
  297. char *actor;
  298. /* return if already seen */
  299. if (xs_set_add(seen, id) == 0)
  300. return os;
  301. if (strcmp(type, "Follow") == 0)
  302. return os;
  303. /* bring the main actor */
  304. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  305. return os;
  306. if (!valid_status(actor_get(snac, actor, &actor_o)))
  307. return os;
  308. xs *s = xs_str_new(NULL);
  309. /* if this is our post, add the score */
  310. if (xs_startswith(id, snac->actor)) {
  311. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  312. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  313. xs *s1 = xs_fmt(
  314. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  315. likes, boosts);
  316. s = xs_str_cat(s, s1);
  317. }
  318. if (level == 0) {
  319. char *referrer;
  320. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  321. /* print the origin of the post, if any */
  322. if ((referrer = xs_dict_get(meta, "referrer")) != NULL) {
  323. xs *actor_r = NULL;
  324. if (valid_status(actor_get(snac, referrer, &actor_r))) {
  325. char *name;
  326. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  327. name = xs_dict_get(actor_r, "preferredUsername");
  328. xs *s1 = xs_fmt(
  329. "<div class=\"snac-origin\">"
  330. "<a href=\"%s\">%s</a> %s</div>\n",
  331. xs_dict_get(actor_r, "id"),
  332. name,
  333. L("boosted")
  334. );
  335. s = xs_str_cat(s, s1);
  336. }
  337. }
  338. }
  339. else
  340. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  341. s = html_msg_icon(snac, s, msg);
  342. /* add the content */
  343. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  344. {
  345. xs *c = xs_dup(xs_dict_get(msg, "content"));
  346. /* do some tweaks to the content */
  347. c = xs_replace_i(c, "\r", "");
  348. while (xs_endswith(c, "<br><br>"))
  349. c = xs_crop(c, 0, -4);
  350. c = xs_replace_i(c, "<br><br>", "<p>");
  351. if (!xs_startswith(c, "<p>")) {
  352. xs *s1 = c;
  353. c = xs_fmt("<p>%s</p>", s1);
  354. }
  355. s = xs_str_cat(s, c);
  356. }
  357. /* add the attachments */
  358. char *attach;
  359. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  360. char *v;
  361. while (xs_list_iter(&attach, &v)) {
  362. char *t = xs_dict_get(v, "mediaType");
  363. if (t && xs_startswith(t, "image/")) {
  364. char *url = xs_dict_get(v, "url");
  365. char *name = xs_dict_get(v, "name");
  366. if (url != NULL) {
  367. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  368. url, xs_is_null(name) ? "" : name);
  369. s = xs_str_cat(s, s1);
  370. }
  371. }
  372. }
  373. }
  374. s = xs_str_cat(s, "</div>\n");
  375. char *children = xs_dict_get(meta, "children");
  376. if (xs_list_len(children)) {
  377. int left = xs_list_len(children);
  378. char *id;
  379. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  380. if (left > 3)
  381. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  382. while (xs_list_iter(&children, &id)) {
  383. xs *chd = timeline_find(snac, id);
  384. if (left == 0)
  385. s = xs_str_cat(s, "</details>\n");
  386. if (chd != NULL)
  387. s = html_entry(snac, s, chd, seen, level + 1);
  388. else
  389. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  390. left--;
  391. }
  392. s = xs_str_cat(s, "</div>\n");
  393. }
  394. s = xs_str_cat(s, "</div>\n");
  395. return xs_str_cat(os, s);
  396. }
  397. d_char *html_user_footer(snac *snac, d_char *s)
  398. {
  399. xs *s1 = xs_fmt(
  400. "<div class=\"snac-footer\">\n"
  401. "<a href=\"%s\">%s</a> - "
  402. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  403. srv_baseurl,
  404. L("about this site")
  405. );
  406. return xs_str_cat(s, s1);
  407. }
  408. d_char *html_timeline(snac *snac, char *list, int local)
  409. /* returns the HTML for the timeline */
  410. {
  411. d_char *s = xs_str_new(NULL);
  412. xs_set *seen = xs_set_new(4096);
  413. char *v;
  414. double t = ftime();
  415. s = html_user_header(snac, s, local);
  416. if (!local)
  417. s = html_top_controls(snac, s);
  418. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  419. while (xs_list_iter(&list, &v)) {
  420. xs *msg = timeline_get(snac, v);
  421. s = html_entry(snac, s, msg, seen, 0);
  422. }
  423. s = xs_str_cat(s, "</div>\n");
  424. s = html_user_footer(snac, s);
  425. {
  426. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  427. s = xs_str_cat(s, s1);
  428. }
  429. s = xs_str_cat(s, "</body>\n</html>\n");
  430. xs_set_free(seen);
  431. return s;
  432. }
  433. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  434. {
  435. int status = 404;
  436. snac snac;
  437. char *uid, *p_path;
  438. xs *l = xs_split_n(q_path, "/", 2);
  439. uid = xs_list_get(l, 1);
  440. if (!uid || !user_open(&snac, uid)) {
  441. /* invalid user */
  442. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  443. return 404;
  444. }
  445. p_path = xs_list_get(l, 2);
  446. if (p_path == NULL) {
  447. /* public timeline */
  448. xs *list = local_list(&snac, 0xfffffff);
  449. *body = html_timeline(&snac, list, 1);
  450. *b_size = strlen(*body);
  451. status = 200;
  452. }
  453. else
  454. if (strcmp(p_path, "admin") == 0) {
  455. /* private timeline */
  456. if (!login(&snac, req))
  457. status = 401;
  458. else {
  459. xs *list = timeline_list(&snac, 0xfffffff);
  460. *body = html_timeline(&snac, list, 0);
  461. *b_size = strlen(*body);
  462. status = 200;
  463. }
  464. }
  465. else
  466. if (xs_startswith(p_path, "p/") == 0) {
  467. /* a timeline with just one entry */
  468. }
  469. else
  470. if (xs_startswith(p_path, "s/") == 0) {
  471. /* a static file */
  472. }
  473. else
  474. if (xs_startswith(p_path, "h/") == 0) {
  475. /* an entry from the history */
  476. }
  477. else
  478. status = 404;
  479. user_free(&snac);
  480. if (valid_status(status)) {
  481. *ctype = "text/html; charset=utf-8";
  482. }
  483. return status;
  484. }
  485. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  486. char **body, int *b_size, char **ctype)
  487. {
  488. int status = 0;
  489. return status;
  490. }