html.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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\"/>\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. s = xs_str_cat(s, "</div>\n");
  164. }
  165. return 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 style=\"snac-top-nav\">");
  198. {
  199. xs *s1;
  200. if (local)
  201. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>", snac->actor, L("admin"));
  202. else
  203. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>", 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 *s, 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 s;
  301. if (strcmp(type, "Follow") == 0)
  302. return s;
  303. /* bring the main actor */
  304. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  305. return s;
  306. if (!valid_status(actor_get(snac, actor, &actor_o)))
  307. return s;
  308. if (level == 0) {
  309. char *referrer;
  310. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  311. /* print the origin of the post, if any */
  312. if ((referrer = xs_dict_get(meta, "referrer")) != NULL) {
  313. xs *actor_r = NULL;
  314. if (valid_status(actor_get(snac, referrer, &actor_r))) {
  315. char *name;
  316. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  317. name = xs_dict_get(actor_r, "preferredUsername");
  318. xs *s1 = xs_fmt(
  319. "<div class=\"snac-origin\">\n"
  320. "<a href=\"%s\">%s</a> %s</div>",
  321. xs_dict_get(actor_r, "id"),
  322. name,
  323. "boosted"
  324. );
  325. s = xs_str_cat(s, s1);
  326. }
  327. }
  328. }
  329. else
  330. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  331. s = html_msg_icon(snac, s, msg);
  332. /* add the content */
  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. xs *s1 = xs_fmt("<div class=\"e-content snac-content\">\n%s", c);
  345. s = xs_str_cat(s, s1);
  346. s = xs_str_cat(s, "</div>\n");
  347. }
  348. s = xs_str_cat(s, "</div>\n");
  349. return s;
  350. }
  351. d_char *html_timeline(snac *snac, char *list, int local)
  352. /* returns the HTML for the timeline */
  353. {
  354. d_char *s = xs_str_new(NULL);
  355. xs_set *seen = xs_set_new(4096);
  356. char *v;
  357. s = html_user_header(snac, s, local);
  358. if (!local)
  359. s = html_top_controls(snac, s);
  360. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  361. while (xs_list_iter(&list, &v)) {
  362. xs *msg = timeline_get(snac, v);
  363. s = html_entry(snac, s, msg, seen, 0);
  364. }
  365. s = xs_str_cat(s, "</div>\n");
  366. #if 0
  367. s = xs_str_cat(s, "<h1>HI</h1>\n");
  368. s = xs_str_cat(s, xs_fmt("len() == %d\n", xs_list_len(list)));
  369. {
  370. char *i = xs_list_get(list, 0);
  371. xs *msg = timeline_get(snac, i);
  372. s = html_msg_icon(snac, s, msg);
  373. }
  374. s = xs_str_cat(s, "</html>\n");
  375. #endif
  376. {
  377. /* footer */
  378. xs *s1 = xs_fmt(
  379. "<div class=\"snac-footer\">\n"
  380. "<a href=\"%s\">%s</a> - "
  381. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  382. srv_baseurl,
  383. L("about this site")
  384. );
  385. s = xs_str_cat(s, s1);
  386. }
  387. s = xs_str_cat(s, "</body>\n</html>\n");
  388. xs_set_free(seen);
  389. return s;
  390. }
  391. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  392. {
  393. int status = 404;
  394. snac snac;
  395. char *uid, *p_path;
  396. xs *l = xs_split_n(q_path, "/", 2);
  397. uid = xs_list_get(l, 1);
  398. if (!uid || !user_open(&snac, uid)) {
  399. /* invalid user */
  400. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  401. return 404;
  402. }
  403. p_path = xs_list_get(l, 2);
  404. if (p_path == NULL) {
  405. /* public timeline */
  406. xs *list = local_list(&snac, 0xfffffff);
  407. *body = html_timeline(&snac, list, 1);
  408. *b_size = strlen(*body);
  409. status = 200;
  410. }
  411. else
  412. if (strcmp(p_path, "admin") == 0) {
  413. /* private timeline */
  414. if (!login(&snac, req))
  415. status = 401;
  416. else {
  417. xs *list = timeline_list(&snac, 0xfffffff);
  418. *body = html_timeline(&snac, list, 0);
  419. *b_size = strlen(*body);
  420. status = 200;
  421. }
  422. }
  423. else
  424. if (xs_startswith(p_path, "p/") == 0) {
  425. /* a timeline with just one entry */
  426. }
  427. else
  428. if (xs_startswith(p_path, "s/") == 0) {
  429. /* a static file */
  430. }
  431. else
  432. if (xs_startswith(p_path, "h/") == 0) {
  433. /* an entry from the history */
  434. }
  435. else
  436. status = 404;
  437. user_free(&snac);
  438. if (valid_status(status)) {
  439. *ctype = "text/html; charset=utf-8";
  440. }
  441. return status;
  442. }
  443. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  444. char **body, int *b_size, char **ctype)
  445. {
  446. int status = 0;
  447. return status;
  448. }