html.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 *s, char *msg)
  119. {
  120. char *actor_id;
  121. xs *actor = NULL;
  122. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  123. actor_id = xs_dict_get(msg, "actor");
  124. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  125. xs *name = NULL;
  126. xs *avatar = NULL;
  127. char *v;
  128. /* get the name */
  129. if ((v = xs_dict_get(actor, "name")) == NULL) {
  130. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  131. v = "user";
  132. }
  133. }
  134. name = xs_dup(v);
  135. /* get the avatar */
  136. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  137. (v = xs_dict_get(v, "url")) != NULL) {
  138. avatar = xs_dup(v);
  139. }
  140. if (avatar == NULL)
  141. avatar = xs_fmt("data:image/png;base64, %s", susie);
  142. {
  143. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  144. s = xs_str_cat(s, s1);
  145. }
  146. {
  147. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  148. actor_id, name);
  149. s = xs_str_cat(s, s1);
  150. }
  151. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  152. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  153. s = xs_str_cat(s, s1);
  154. }
  155. if (!is_msg_public(snac, msg))
  156. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  157. if ((v = xs_dict_get(msg, "published")) == NULL)
  158. v = "&nbsp;";
  159. {
  160. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  161. s = xs_str_cat(s, s1);
  162. }
  163. }
  164. return s;
  165. }
  166. d_char *html_user_header(snac *snac, d_char *s, int local)
  167. /* creates the HTML header */
  168. {
  169. char *p, *v;
  170. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  171. s = xs_str_cat(s, "<meta name=\"viewport\" "
  172. "content=\"width=device-width, initial-scale=1\"/>\n");
  173. s = xs_str_cat(s, "<meta name=\"generator\" "
  174. "content=\"" USER_AGENT "\"/>\n");
  175. /* add server CSS */
  176. p = xs_dict_get(srv_config, "cssurls");
  177. while (xs_list_iter(&p, &v)) {
  178. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  179. s = xs_str_cat(s, s1);
  180. }
  181. /* add the user CSS */
  182. {
  183. xs *css = NULL;
  184. int size;
  185. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  186. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  187. s = xs_str_cat(s, s1);
  188. }
  189. }
  190. {
  191. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  192. s = xs_str_cat(s, s1);
  193. }
  194. s = xs_str_cat(s, "</head>\n<body>\n");
  195. /* top nav */
  196. s = xs_str_cat(s, "<nav style=\"snac-top-nav\">");
  197. {
  198. xs *s1;
  199. if (local)
  200. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  201. else
  202. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  203. s = xs_str_cat(s, s1);
  204. }
  205. /* user info */
  206. {
  207. xs *bio = NULL;
  208. char *_tmpl =
  209. "<div class=\"h-card snac-top-user\">\n"
  210. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  211. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  212. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  213. "</div>\n";
  214. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  215. xs *s1 = xs_fmt(_tmpl,
  216. xs_dict_get(snac->config, "name"),
  217. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  218. bio
  219. );
  220. s = xs_str_cat(s, s1);
  221. }
  222. return s;
  223. }
  224. d_char *html_top_controls(snac *snac, d_char *s)
  225. /* generates the top controls */
  226. {
  227. char *_tmpl =
  228. "<div class=\"snac-top-controls\">\n"
  229. "<div class=\"snac-note\">\n"
  230. "<form method=\"post\" action=\"%s/admin/note\">\n"
  231. "<textarea class=\"snac-textarea\" name=\"content\" "
  232. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  233. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  234. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  235. "</form><p>\n"
  236. "</div>\n"
  237. "<div class=\"snac-top-controls-more\">\n"
  238. "<details><summary>%s</summary>\n"
  239. "<form method=\"post\" action=\"%s/admin/action\">\n"
  240. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  241. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  242. "</form></p>\n"
  243. "<form method=\"post\" action=\"%s\">\n"
  244. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  245. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  246. "</form></p>\n"
  247. "<details><summary>%s</summary>\n"
  248. "<div class=\"snac-user-setup\">\n"
  249. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  250. "<p>%s:<br>\n"
  251. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  252. "<p>%s:<br>\n"
  253. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  254. "<p>%s:<br>\n"
  255. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  256. "<p>%s:<br>\n"
  257. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  258. "<p>%s:<br>\n"
  259. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  260. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  261. "</form>\n"
  262. "</div>\n"
  263. "</details>\n"
  264. "</details>\n"
  265. "</div>\n"
  266. "</div>\n";
  267. xs *s1 = xs_fmt(_tmpl,
  268. snac->actor,
  269. L("Post"),
  270. L("More options..."),
  271. snac->actor,
  272. L("Follow"), L("(by URL or user@host)"),
  273. snac->actor,
  274. L("Boost"), L("(by URL)"),
  275. L("User setup..."),
  276. snac->actor,
  277. L("User name"),
  278. xs_dict_get(snac->config, "name"),
  279. L("Avatar URL"),
  280. xs_dict_get(snac->config, "avatar"),
  281. L("Bio"),
  282. xs_dict_get(snac->config, "bio"),
  283. L("Password (only to change it)"),
  284. L("Repeat Password"),
  285. L("Update user info")
  286. );
  287. s = xs_str_cat(s, s1);
  288. return s;
  289. }
  290. d_char *html_entry(snac *snac, d_char *s, char *msg, xs_set *seen, int level)
  291. {
  292. char *id = xs_dict_get(msg, "id");
  293. char *type = xs_dict_get(msg, "type");
  294. char *meta = xs_dict_get(msg, "_snac");
  295. xs *actor_o = NULL;
  296. char *actor;
  297. /* return if already seen */
  298. if (xs_set_add(seen, id) == 0)
  299. return s;
  300. if (strcmp(type, "Follow") == 0)
  301. return s;
  302. /* bring the main actor */
  303. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  304. return s;
  305. if (!valid_status(actor_get(snac, actor, &actor_o)))
  306. return s;
  307. if (level == 0) {
  308. char *referrer;
  309. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  310. /* print the origin of the post, if any */
  311. if ((referrer = xs_dict_get(meta, "referrer")) != NULL) {
  312. xs *actor_r = NULL;
  313. if (valid_status(actor_get(snac, referrer, &actor_r))) {
  314. char *name;
  315. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  316. name = xs_dict_get(actor_r, "preferredUsername");
  317. xs *s1 = xs_fmt(
  318. "<div class=\"snac-origin\">"
  319. "<a href=\"%s\">%s</a> %s</div>\n",
  320. xs_dict_get(actor_r, "id"),
  321. name,
  322. "boosted"
  323. );
  324. s = xs_str_cat(s, s1);
  325. }
  326. }
  327. }
  328. else
  329. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  330. s = html_msg_icon(snac, s, msg);
  331. /* add the content */
  332. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  333. {
  334. xs *c = xs_dup(xs_dict_get(msg, "content"));
  335. /* do some tweaks to the content */
  336. c = xs_replace_i(c, "\r", "");
  337. while (xs_endswith(c, "<br><br>"))
  338. c = xs_crop(c, 0, -4);
  339. c = xs_replace_i(c, "<br><br>", "<p>");
  340. if (!xs_startswith(c, "<p>")) {
  341. xs *s1 = c;
  342. c = xs_fmt("<p>%s</p>", s1);
  343. }
  344. s = xs_str_cat(s, c);
  345. }
  346. /* add the attachments */
  347. char *attach;
  348. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  349. char *v;
  350. while (xs_list_iter(&attach, &v)) {
  351. char *t = xs_dict_get(v, "mediaType");
  352. if (t && xs_startswith(t, "image/")) {
  353. char *url = xs_dict_get(v, "url");
  354. char *name = xs_dict_get(v, "name");
  355. if (url != NULL) {
  356. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  357. url, xs_is_null(name) ? "" : name);
  358. s = xs_str_cat(s, s1);
  359. }
  360. }
  361. }
  362. }
  363. s = xs_str_cat(s, "</div> <!-- e-content -->\n");
  364. char *children = xs_dict_get(meta, "children");
  365. if (xs_list_len(children)) {
  366. int left = xs_list_len(children);
  367. char *id;
  368. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  369. if (left > 3)
  370. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  371. while (xs_list_iter(&children, &id)) {
  372. xs *chd = timeline_find(snac, id);
  373. if (left == 0)
  374. s = xs_str_cat(s, "</details>\n");
  375. if (chd != NULL)
  376. s = html_entry(snac, s, chd, seen, level + 1);
  377. else
  378. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  379. left--;
  380. }
  381. s = xs_str_cat(s, "</div> <!-- snac-children -->\n");
  382. }
  383. s = xs_str_cat(s, "</div> <!-- post or child -->\n");
  384. return s;
  385. }
  386. d_char *html_user_footer(snac *snac, d_char *s)
  387. {
  388. xs *s1 = xs_fmt(
  389. "<div class=\"snac-footer\">\n"
  390. "<a href=\"%s\">%s</a> - "
  391. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  392. srv_baseurl,
  393. L("about this site")
  394. );
  395. return xs_str_cat(s, s1);
  396. }
  397. d_char *html_timeline(snac *snac, char *list, int local)
  398. /* returns the HTML for the timeline */
  399. {
  400. d_char *s = xs_str_new(NULL);
  401. xs_set *seen = xs_set_new(4096);
  402. char *v;
  403. double t = ftime();
  404. s = html_user_header(snac, s, local);
  405. if (!local)
  406. s = html_top_controls(snac, s);
  407. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  408. while (xs_list_iter(&list, &v)) {
  409. xs *msg = timeline_get(snac, v);
  410. s = html_entry(snac, s, msg, seen, 0);
  411. }
  412. s = xs_str_cat(s, "</div> <!-- snac-posts -->\n");
  413. s = html_user_footer(snac, s);
  414. {
  415. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  416. s = xs_str_cat(s, s1);
  417. }
  418. s = xs_str_cat(s, "</body>\n</html>\n");
  419. xs_set_free(seen);
  420. return s;
  421. }
  422. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  423. {
  424. int status = 404;
  425. snac snac;
  426. char *uid, *p_path;
  427. xs *l = xs_split_n(q_path, "/", 2);
  428. uid = xs_list_get(l, 1);
  429. if (!uid || !user_open(&snac, uid)) {
  430. /* invalid user */
  431. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  432. return 404;
  433. }
  434. p_path = xs_list_get(l, 2);
  435. if (p_path == NULL) {
  436. /* public timeline */
  437. xs *list = local_list(&snac, 0xfffffff);
  438. *body = html_timeline(&snac, list, 1);
  439. *b_size = strlen(*body);
  440. status = 200;
  441. }
  442. else
  443. if (strcmp(p_path, "admin") == 0) {
  444. /* private timeline */
  445. if (!login(&snac, req))
  446. status = 401;
  447. else {
  448. xs *list = timeline_list(&snac, 0xfffffff);
  449. *body = html_timeline(&snac, list, 0);
  450. *b_size = strlen(*body);
  451. status = 200;
  452. }
  453. }
  454. else
  455. if (xs_startswith(p_path, "p/") == 0) {
  456. /* a timeline with just one entry */
  457. }
  458. else
  459. if (xs_startswith(p_path, "s/") == 0) {
  460. /* a static file */
  461. }
  462. else
  463. if (xs_startswith(p_path, "h/") == 0) {
  464. /* an entry from the history */
  465. }
  466. else
  467. status = 404;
  468. user_free(&snac);
  469. if (valid_status(status)) {
  470. *ctype = "text/html; charset=utf-8";
  471. }
  472. return status;
  473. }
  474. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  475. char **body, int *b_size, char **ctype)
  476. {
  477. int status = 0;
  478. return status;
  479. }