html.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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 "xs_openssl.h"
  10. #include "xs_time.h"
  11. #include "snac.h"
  12. d_char *not_really_markdown(char *content, d_char **f_content)
  13. /* formats a content using some Markdown rules */
  14. {
  15. d_char *s = NULL;
  16. int in_pre = 0;
  17. int in_blq = 0;
  18. xs *list;
  19. char *p, *v;
  20. xs *wrk = xs_str_new(NULL);
  21. {
  22. /* split by special markup */
  23. xs *sm = xs_regex_split(content,
  24. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
  25. int n = 0;
  26. p = sm;
  27. while (xs_list_iter(&p, &v)) {
  28. if ((n & 0x1)) {
  29. /* markup */
  30. if (xs_startswith(v, "`")) {
  31. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  32. xs *s2 = xs_fmt("<code>%s</code>", s1);
  33. wrk = xs_str_cat(wrk, s2);
  34. }
  35. else
  36. if (xs_startswith(v, "**")) {
  37. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  38. xs *s2 = xs_fmt("<b>%s</b>", s1);
  39. wrk = xs_str_cat(wrk, s2);
  40. }
  41. else
  42. if (xs_startswith(v, "*")) {
  43. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  44. xs *s2 = xs_fmt("<i>%s</i>", s1);
  45. wrk = xs_str_cat(wrk, s2);
  46. }
  47. else
  48. if (xs_startswith(v, "http")) {
  49. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  50. wrk = xs_str_cat(wrk, s1);
  51. }
  52. else
  53. /* what the hell is this */
  54. wrk = xs_str_cat(wrk, v);
  55. }
  56. else
  57. /* surrounded text, copy directly */
  58. wrk = xs_str_cat(wrk, v);
  59. n++;
  60. }
  61. }
  62. /* now work by lines */
  63. p = list = xs_split(wrk, "\n");
  64. s = xs_str_new(NULL);
  65. while (xs_list_iter(&p, &v)) {
  66. xs *ss = xs_strip(xs_dup(v));
  67. if (xs_startswith(ss, "```")) {
  68. if (!in_pre)
  69. s = xs_str_cat(s, "<pre>");
  70. else
  71. s = xs_str_cat(s, "</pre>");
  72. in_pre = !in_pre;
  73. continue;
  74. }
  75. if (xs_startswith(ss, ">")) {
  76. /* delete the > and subsequent spaces */
  77. ss = xs_strip(xs_crop(ss, 1, 0));
  78. if (!in_blq) {
  79. s = xs_str_cat(s, "<blockquote>");
  80. in_blq = 1;
  81. }
  82. s = xs_str_cat(s, ss);
  83. s = xs_str_cat(s, "<br>");
  84. continue;
  85. }
  86. if (in_blq) {
  87. s = xs_str_cat(s, "</blockquote>");
  88. in_blq = 0;
  89. }
  90. s = xs_str_cat(s, ss);
  91. s = xs_str_cat(s, "<br>");
  92. }
  93. if (in_blq)
  94. s = xs_str_cat(s, "</blockquote>");
  95. if (in_pre)
  96. s = xs_str_cat(s, "</pre>");
  97. /* some beauty fixes */
  98. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  99. *f_content = s;
  100. return *f_content;
  101. }
  102. int login(snac *snac, char *headers)
  103. /* tries a login */
  104. {
  105. int logged_in = 0;
  106. char *auth = xs_dict_get(headers, "authorization");
  107. if (auth && xs_startswith(auth, "Basic ")) {
  108. int sz;
  109. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  110. xs *s2 = xs_base64_dec(s1, &sz);
  111. xs *l1 = xs_split_n(s2, ":", 1);
  112. if (xs_list_len(l1) == 2) {
  113. logged_in = check_password(
  114. xs_list_get(l1, 0), xs_list_get(l1, 1),
  115. xs_dict_get(snac->config, "passwd"));
  116. }
  117. }
  118. return logged_in;
  119. }
  120. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  121. {
  122. char *actor_id;
  123. xs *actor = NULL;
  124. xs *s = xs_str_new(NULL);
  125. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  126. actor_id = xs_dict_get(msg, "actor");
  127. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  128. xs *name = NULL;
  129. xs *avatar = NULL;
  130. char *v;
  131. /* get the name */
  132. if ((v = xs_dict_get(actor, "name")) == NULL || *v == '\0') {
  133. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  134. v = "user";
  135. }
  136. }
  137. name = xs_dup(v);
  138. /* get the avatar */
  139. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  140. (v = xs_dict_get(v, "url")) != NULL) {
  141. avatar = xs_dup(v);
  142. }
  143. if (avatar == NULL)
  144. avatar = xs_fmt("data:image/png;base64, %s", susie);
  145. {
  146. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  147. s = xs_str_cat(s, s1);
  148. }
  149. {
  150. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  151. actor_id, name);
  152. s = xs_str_cat(s, s1);
  153. }
  154. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  155. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  156. s = xs_str_cat(s, s1);
  157. }
  158. if (!is_msg_public(snac, msg))
  159. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  160. if ((v = xs_dict_get(msg, "published")) == NULL) {
  161. s = xs_str_cat(s, "<br>\n<time>&nbsp;</time>\n");
  162. }
  163. else {
  164. xs *s1 = xs_fmt(
  165. "<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  166. s = xs_str_cat(s, s1);
  167. }
  168. }
  169. return xs_str_cat(os, s);
  170. }
  171. d_char *html_user_header(snac *snac, d_char *s, int local)
  172. /* creates the HTML header */
  173. {
  174. char *p, *v;
  175. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  176. s = xs_str_cat(s, "<meta name=\"viewport\" "
  177. "content=\"width=device-width, initial-scale=1\"/>\n");
  178. s = xs_str_cat(s, "<meta name=\"generator\" "
  179. "content=\"" USER_AGENT "\"/>\n");
  180. /* add server CSS */
  181. p = xs_dict_get(srv_config, "cssurls");
  182. while (xs_list_iter(&p, &v)) {
  183. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  184. s = xs_str_cat(s, s1);
  185. }
  186. /* add the user CSS */
  187. {
  188. xs *css = NULL;
  189. int size;
  190. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  191. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  192. s = xs_str_cat(s, s1);
  193. }
  194. }
  195. {
  196. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  197. s = xs_str_cat(s, s1);
  198. }
  199. s = xs_str_cat(s, "</head>\n<body>\n");
  200. /* top nav */
  201. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  202. {
  203. xs *s1;
  204. if (local)
  205. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  206. else
  207. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  208. s = xs_str_cat(s, s1);
  209. }
  210. /* user info */
  211. {
  212. xs *bio = NULL;
  213. char *_tmpl =
  214. "<div class=\"h-card snac-top-user\">\n"
  215. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  216. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  217. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  218. "</div>\n";
  219. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  220. xs *s1 = xs_fmt(_tmpl,
  221. xs_dict_get(snac->config, "name"),
  222. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  223. bio
  224. );
  225. s = xs_str_cat(s, s1);
  226. }
  227. return s;
  228. }
  229. d_char *html_top_controls(snac *snac, d_char *s)
  230. /* generates the top controls */
  231. {
  232. char *_tmpl =
  233. "<div class=\"snac-top-controls\">\n"
  234. "<div class=\"snac-note\">\n"
  235. "<form method=\"post\" action=\"%s/admin/note\">\n"
  236. "<textarea class=\"snac-textarea\" name=\"content\" "
  237. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  238. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  239. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  240. "</form><p>\n"
  241. "</div>\n"
  242. "<div class=\"snac-top-controls-more\">\n"
  243. "<details><summary>%s</summary>\n"
  244. "<form method=\"post\" action=\"%s/admin/action\">\n"
  245. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  246. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  247. "</form></p>\n"
  248. "<form method=\"post\" action=\"%s\">\n"
  249. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  250. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  251. "</form></p>\n"
  252. "<details><summary>%s</summary>\n"
  253. "<div class=\"snac-user-setup\">\n"
  254. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  255. "<p>%s:<br>\n"
  256. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  257. "<p>%s:<br>\n"
  258. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  259. "<p>%s:<br>\n"
  260. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  261. "<p>%s:<br>\n"
  262. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  263. "<p>%s:<br>\n"
  264. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  265. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  266. "</form>\n"
  267. "</div>\n"
  268. "</details>\n"
  269. "</details>\n"
  270. "</div>\n"
  271. "</div>\n";
  272. xs *s1 = xs_fmt(_tmpl,
  273. snac->actor,
  274. L("Post"),
  275. L("More options..."),
  276. snac->actor,
  277. L("Follow"), L("(by URL or user@host)"),
  278. snac->actor,
  279. L("Boost"), L("(by URL)"),
  280. L("User setup..."),
  281. snac->actor,
  282. L("User name"),
  283. xs_dict_get(snac->config, "name"),
  284. L("Avatar URL"),
  285. xs_dict_get(snac->config, "avatar"),
  286. L("Bio"),
  287. xs_dict_get(snac->config, "bio"),
  288. L("Password (only to change it)"),
  289. L("Repeat Password"),
  290. L("Update user info")
  291. );
  292. s = xs_str_cat(s, s1);
  293. return s;
  294. }
  295. d_char *html_button(d_char *s, char *clss, char *label)
  296. {
  297. xs *s1 = xs_fmt(
  298. "<input type=\"submit\" name=\"action\" "
  299. "class=\"snac-btn-%s\" value=\"%s\">\n",
  300. clss, label);
  301. return xs_str_cat(s, s1);
  302. }
  303. d_char *html_entry_controls(snac *snac, d_char *os, char *msg)
  304. {
  305. char *id = xs_dict_get(msg, "id");
  306. char *actor = xs_dict_get(msg, "attributedTo");
  307. char *meta = xs_dict_get(msg, "_snac");
  308. xs *s = xs_str_new(NULL);
  309. xs *md5 = xs_md5_hex(id, strlen(id));
  310. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  311. {
  312. xs *s1 = xs_fmt(
  313. "<form method=\"post\" action=\"%s/admin/action\">\n"
  314. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  315. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  316. "<input type=\"button\" name=\"action\" "
  317. "value=\"%s\" onclick=\""
  318. "x = document.getElementById('%s_reply'); "
  319. "if (x.style.display == 'block') "
  320. " x.style.display = 'none'; else "
  321. " x.style.display = 'block';"
  322. "\">\n",
  323. snac->actor, id, actor,
  324. L("Reply"),
  325. md5
  326. );
  327. s = xs_str_cat(s, s1);
  328. }
  329. if (strcmp(actor, snac->actor) != 0) {
  330. /* controls for other actors than this one */
  331. char *l;
  332. l = xs_dict_get(meta, "liked_by");
  333. if (xs_list_in(l, snac->actor) == -1) {
  334. /* not already liked; add button */
  335. s = html_button(s, "like", L("Like"));
  336. }
  337. l = xs_dict_get(meta, "announced_by");
  338. if (xs_list_in(l, snac->actor) == -1) {
  339. /* not already boosted; add button */
  340. s = html_button(s, "boost", L("Boost"));
  341. }
  342. if (following_check(snac, actor)) {
  343. s = html_button(s, "unfollow", L("Unfollow"));
  344. }
  345. else {
  346. s = html_button(s, "follow", L("Follow"));
  347. s = html_button(s, "mute", L("MUTE"));
  348. }
  349. }
  350. s = html_button(s, "delete", L("Delete"));
  351. s = xs_str_cat(s, "</form>\n");
  352. {
  353. /* the post textarea */
  354. xs *ct = xs_str_new("");
  355. xs *s1 = xs_fmt(
  356. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  357. "<form method=\"post\" action=\"%s/admin/note\" id=\"%s_reply_form\">\n"
  358. "<textarea class=\"snac-textarea\" name=\"content\" "
  359. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  360. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  361. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  362. "</form><p></div>\n",
  363. md5,
  364. snac->actor, md5,
  365. ct,
  366. id,
  367. L("Post")
  368. );
  369. s = xs_str_cat(s, s1);
  370. }
  371. s = xs_str_cat(s, "</div>\n");
  372. return xs_str_cat(os, s);
  373. }
  374. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  375. {
  376. char *id = xs_dict_get(msg, "id");
  377. char *type = xs_dict_get(msg, "type");
  378. char *meta = xs_dict_get(msg, "_snac");
  379. xs *actor_o = NULL;
  380. char *actor;
  381. /* do not show non-public messages in the public timeline */
  382. if (local && !is_msg_public(snac, msg))
  383. return os;
  384. /* return if already seen */
  385. if (xs_set_add(seen, id) == 0)
  386. return os;
  387. xs *s = xs_str_new(NULL);
  388. if (strcmp(type, "Follow") == 0) {
  389. actor = xs_dict_get(msg, "actor");
  390. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  391. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  392. s = xs_str_cat(s, s1);
  393. s = html_msg_icon(snac, s, msg);
  394. s = xs_str_cat(s, "</div>\n");
  395. return xs_str_cat(os, s);
  396. }
  397. /* bring the main actor */
  398. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  399. return os;
  400. /* ignore muted morons immediately */
  401. if (is_muted(snac, actor))
  402. return os;
  403. if (!valid_status(actor_get(snac, actor, &actor_o)))
  404. return os;
  405. /* if this is our post, add the score */
  406. if (xs_startswith(id, snac->actor)) {
  407. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  408. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  409. xs *s1 = xs_fmt(
  410. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  411. likes, boosts);
  412. s = xs_str_cat(s, s1);
  413. }
  414. if (level == 0) {
  415. char *p;
  416. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  417. /* print the origin of the post, if any */
  418. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  419. xs *actor_r = NULL;
  420. if (valid_status(actor_get(snac, p, &actor_r))) {
  421. char *name;
  422. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  423. name = xs_dict_get(actor_r, "preferredUsername");
  424. xs *s1 = xs_fmt(
  425. "<div class=\"snac-origin\">"
  426. "<a href=\"%s\">%s</a> %s</div>\n",
  427. xs_dict_get(actor_r, "id"),
  428. name,
  429. L("boosted")
  430. );
  431. s = xs_str_cat(s, s1);
  432. }
  433. }
  434. else
  435. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  436. /* this may happen if any of the autor or the parent aren't here */
  437. xs *s1 = xs_fmt(
  438. "<div class=\"snac-origin\">%s "
  439. "<a href=\"%s\">»</a></div>\n",
  440. L("in reply to"), p
  441. );
  442. s = xs_str_cat(s, s1);
  443. }
  444. else
  445. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  446. xs_list_in(p, snac->actor) != -1) {
  447. /* we boosted this */
  448. xs *s1 = xs_fmt(
  449. "<div class=\"snac-origin\">"
  450. "<a href=\"%s\">%s</a> %s</a></div>",
  451. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  452. );
  453. s = xs_str_cat(s, s1);
  454. }
  455. else
  456. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  457. xs_list_in(p, snac->actor) != -1) {
  458. /* we liked this */
  459. xs *s1 = xs_fmt(
  460. "<div class=\"snac-origin\">"
  461. "<a href=\"%s\">%s</a> %s</a></div>",
  462. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  463. );
  464. s = xs_str_cat(s, s1);
  465. }
  466. }
  467. else
  468. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  469. s = html_msg_icon(snac, s, msg);
  470. /* add the content */
  471. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  472. {
  473. xs *c = xs_dup(xs_dict_get(msg, "content"));
  474. /* do some tweaks to the content */
  475. c = xs_replace_i(c, "\r", "");
  476. while (xs_endswith(c, "<br><br>"))
  477. c = xs_crop(c, 0, -4);
  478. c = xs_replace_i(c, "<br><br>", "<p>");
  479. if (!xs_startswith(c, "<p>")) {
  480. xs *s1 = c;
  481. c = xs_fmt("<p>%s</p>", s1);
  482. }
  483. s = xs_str_cat(s, c);
  484. }
  485. s = xs_str_cat(s, "\n");
  486. /* add the attachments */
  487. char *attach;
  488. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  489. char *v;
  490. while (xs_list_iter(&attach, &v)) {
  491. char *t = xs_dict_get(v, "mediaType");
  492. if (t && xs_startswith(t, "image/")) {
  493. char *url = xs_dict_get(v, "url");
  494. char *name = xs_dict_get(v, "name");
  495. if (url != NULL) {
  496. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  497. url, xs_is_null(name) ? "" : name);
  498. s = xs_str_cat(s, s1);
  499. }
  500. }
  501. }
  502. }
  503. s = xs_str_cat(s, "</div>\n");
  504. /** controls **/
  505. if (!local)
  506. s = html_entry_controls(snac, s, msg);
  507. /** children **/
  508. char *children = xs_dict_get(meta, "children");
  509. if (xs_list_len(children)) {
  510. int left = xs_list_len(children);
  511. char *id;
  512. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  513. if (left > 3)
  514. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  515. while (xs_list_iter(&children, &id)) {
  516. xs *chd = timeline_find(snac, id);
  517. if (left == 0)
  518. s = xs_str_cat(s, "</details>\n");
  519. if (chd != NULL)
  520. s = html_entry(snac, s, chd, seen, local, level + 1);
  521. else
  522. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  523. left--;
  524. }
  525. s = xs_str_cat(s, "</div>\n");
  526. }
  527. s = xs_str_cat(s, "</div>\n");
  528. return xs_str_cat(os, s);
  529. }
  530. d_char *html_user_footer(snac *snac, d_char *s)
  531. {
  532. xs *s1 = xs_fmt(
  533. "<div class=\"snac-footer\">\n"
  534. "<a href=\"%s\">%s</a> - "
  535. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  536. srv_baseurl,
  537. L("about this site")
  538. );
  539. return xs_str_cat(s, s1);
  540. }
  541. d_char *html_timeline(snac *snac, char *list, int local)
  542. /* returns the HTML for the timeline */
  543. {
  544. d_char *s = xs_str_new(NULL);
  545. xs_set *seen = xs_set_new(4096);
  546. char *v;
  547. double t = ftime();
  548. s = html_user_header(snac, s, local);
  549. if (!local)
  550. s = html_top_controls(snac, s);
  551. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  552. while (xs_list_iter(&list, &v)) {
  553. xs *msg = timeline_get(snac, v);
  554. s = html_entry(snac, s, msg, seen, local, 0);
  555. }
  556. s = xs_str_cat(s, "</div>\n");
  557. if (local) {
  558. xs *s1 = xs_fmt(
  559. "<div class=\"snac-history\">\n"
  560. "<p class=\"snac-history-title\">%s</p><ul>\n",
  561. L("History")
  562. );
  563. s = xs_str_cat(s, s1);
  564. xs *list = history_list(snac);
  565. char *p, *v;
  566. p = list;
  567. while (xs_list_iter(&p, &v)) {
  568. xs *fn = xs_replace(v, ".html", "");
  569. xs *s1 = xs_fmt(
  570. "<li><a href=\"%s/h/%s\">%s</li>\n",
  571. snac->actor, v, fn);
  572. s = xs_str_cat(s, s1);
  573. }
  574. s = xs_str_cat(s, "</ul></div>\n");
  575. }
  576. s = html_user_footer(snac, s);
  577. {
  578. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  579. s = xs_str_cat(s, s1);
  580. }
  581. s = xs_str_cat(s, "</body>\n</html>\n");
  582. xs_set_free(seen);
  583. return s;
  584. }
  585. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  586. {
  587. int status = 404;
  588. snac snac;
  589. char *uid, *p_path;
  590. xs *l = xs_split_n(q_path, "/", 2);
  591. uid = xs_list_get(l, 1);
  592. if (!uid || !user_open(&snac, uid)) {
  593. /* invalid user */
  594. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  595. return 404;
  596. }
  597. p_path = xs_list_get(l, 2);
  598. if (p_path == NULL) {
  599. /* public timeline */
  600. xs *h = xs_str_localtime(0, "%Y-%m.html");
  601. if (history_mtime(&snac, h) > timeline_mtime(&snac)) {
  602. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  603. *body = history_get(&snac, h);
  604. *b_size = strlen(*body);
  605. status = 200;
  606. }
  607. else {
  608. xs *list = local_list(&snac, 0xfffffff);
  609. *body = html_timeline(&snac, list, 1);
  610. *b_size = strlen(*body);
  611. status = 200;
  612. history_add(&snac, h, *body, *b_size);
  613. }
  614. }
  615. else
  616. if (strcmp(p_path, "admin") == 0) {
  617. /* private timeline */
  618. if (!login(&snac, req))
  619. status = 401;
  620. else {
  621. if (history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  622. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  623. *body = history_get(&snac, "timeline.html_");
  624. *b_size = strlen(*body);
  625. status = 200;
  626. }
  627. else {
  628. snac_debug(&snac, 1, xs_fmt("building timeline"));
  629. xs *list = timeline_list(&snac, 0xfffffff);
  630. *body = html_timeline(&snac, list, 0);
  631. *b_size = strlen(*body);
  632. status = 200;
  633. history_add(&snac, "timeline.html_", *body, *b_size);
  634. }
  635. }
  636. }
  637. else
  638. if (xs_startswith(p_path, "p/")) {
  639. /* a timeline with just one entry */
  640. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  641. xs *fn = _timeline_find_fn(&snac, id);
  642. if (fn != NULL) {
  643. xs *list = xs_list_new();
  644. list = xs_list_append(list, fn);
  645. *body = html_timeline(&snac, list, 1);
  646. *b_size = strlen(*body);
  647. status = 200;
  648. }
  649. }
  650. else
  651. if (xs_startswith(p_path, "s/")) {
  652. /* a static file */
  653. }
  654. else
  655. if (xs_startswith(p_path, "h/")) {
  656. /* an entry from the history */
  657. xs *id = xs_replace(p_path, "h/", "");
  658. if ((*body = history_get(&snac, id)) != NULL) {
  659. *b_size = strlen(*body);
  660. status = 200;
  661. }
  662. }
  663. else
  664. status = 404;
  665. user_free(&snac);
  666. if (valid_status(status)) {
  667. *ctype = "text/html; charset=utf-8";
  668. }
  669. return status;
  670. }
  671. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  672. char **body, int *b_size, char **ctype)
  673. {
  674. int status = 0;
  675. snac snac;
  676. char *uid, *p_path;
  677. char *p_vars;
  678. xs *l = xs_split_n(q_path, "/", 2);
  679. uid = xs_list_get(l, 1);
  680. if (!uid || !user_open(&snac, uid)) {
  681. /* invalid user */
  682. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  683. return 404;
  684. }
  685. p_path = xs_list_get(l, 2);
  686. /* all posts must be authenticated */
  687. if (!login(&snac, req))
  688. return 401;
  689. p_vars = xs_dict_get(req, "p_vars");
  690. if (p_path && strcmp(p_path, "admin/note") == 0) {
  691. /* post note */
  692. char *content = xs_dict_get(p_vars, "content");
  693. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  694. if (content != NULL) {
  695. xs *msg = NULL;
  696. xs *c_msg = NULL;
  697. msg = msg_note(&snac, content, NULL, in_reply_to);
  698. c_msg = msg_create(&snac, msg);
  699. post(&snac, c_msg);
  700. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  701. }
  702. status = 303;
  703. }
  704. else
  705. if (p_path && strcmp(p_path, "admin/action") == 0) {
  706. /* action on an entry */
  707. char *id = xs_dict_get(p_vars, "id");
  708. char *actor = xs_dict_get(p_vars, "actor");
  709. char *action = xs_dict_get(p_vars, "action");
  710. if (action == NULL)
  711. return 404;
  712. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  713. status = 303;
  714. if (strcmp(action, L("Like")) == 0) {
  715. xs *msg = msg_admiration(&snac, id, "Like");
  716. post(&snac, msg);
  717. timeline_admire(&snac, id, snac.actor, 1);
  718. }
  719. else
  720. if (strcmp(action, L("Boost")) == 0) {
  721. xs *msg = msg_admiration(&snac, id, "Announce");
  722. post(&snac, msg);
  723. timeline_admire(&snac, id, snac.actor, 0);
  724. }
  725. else
  726. if (strcmp(action, L("MUTE")) == 0) {
  727. mute(&snac, actor);
  728. }
  729. else
  730. if (strcmp(action, L("Follow")) == 0) {
  731. xs *msg = msg_follow(&snac, actor);
  732. /* reload the actor from the message, in may be different */
  733. actor = xs_dict_get(msg, "object");
  734. following_add(&snac, actor, msg);
  735. enqueue_output(&snac, msg, actor, 0);
  736. }
  737. else
  738. if (strcmp(action, L("Unfollow")) == 0) {
  739. /* get the following object */
  740. xs *object = NULL;
  741. if (valid_status(following_get(&snac, actor, &object))) {
  742. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  743. following_del(&snac, actor);
  744. enqueue_output(&snac, msg, actor, 0);
  745. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  746. }
  747. else
  748. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  749. }
  750. else
  751. if (strcmp(action, L("Delete")) == 0) {
  752. /* delete an entry */
  753. if (xs_startswith(id, snac.actor)) {
  754. /* it's a post by us: generate a delete */
  755. xs *msg = msg_delete(&snac, id);
  756. post(&snac, msg);
  757. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  758. }
  759. timeline_del(&snac, id);
  760. snac_log(&snac, xs_fmt("deleted entry %s", id));
  761. }
  762. else
  763. status = 404;
  764. /* delete the cached timeline */
  765. if (status == 303)
  766. history_del(&snac, "timeline.html_");
  767. }
  768. else
  769. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  770. /* change of user data */
  771. char *v;
  772. char *p1, *p2;
  773. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  774. snac.config = xs_dict_set(snac.config, "name", v);
  775. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  776. snac.config = xs_dict_set(snac.config, "avatar", v);
  777. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  778. snac.config = xs_dict_set(snac.config, "bio", v);
  779. /* password change? */
  780. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  781. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  782. *p1 && strcmp(p1, p2) == 0) {
  783. xs *pw = hash_password(snac.uid, p1, NULL);
  784. snac.config = xs_dict_set(snac.config, "passwd", pw);
  785. }
  786. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  787. xs *bfn = xs_fmt("%s.bak", fn);
  788. FILE *f;
  789. rename(fn, bfn);
  790. if ((f = fopen(fn, "w")) != NULL) {
  791. xs *j = xs_json_dumps_pp(snac.config, 4);
  792. fwrite(j, strlen(j), 1, f);
  793. fclose(f);
  794. }
  795. else
  796. rename(bfn, fn);
  797. history_del(&snac, "timeline.html_");
  798. xs *a_msg = msg_actor(&snac);
  799. xs *u_msg = msg_update(&snac, a_msg);
  800. post(&snac, u_msg);
  801. status = 303;
  802. }
  803. if (status == 303) {
  804. *body = xs_fmt("%s/admin", snac.actor);
  805. *b_size = strlen(*body);
  806. }
  807. return status;
  808. }