html.c 27 KB

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