html.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015
  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. /* is it a name without a host? */
  232. if (xs_list_len(l) < 3) {
  233. /* split the href and pick the host name LIKE AN ANIMAL */
  234. /* would be better to query the webfinger but *won't do that* here */
  235. xs *l2 = xs_split(href, "/");
  236. if (xs_list_len(l2) >= 3) {
  237. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  238. s = xs_str_cat(s, s1);
  239. }
  240. }
  241. else {
  242. s = xs_str_cat(s, name);
  243. s = xs_str_cat(s, " ");
  244. }
  245. }
  246. }
  247. return s;
  248. }
  249. d_char *html_entry_controls(snac *snac, d_char *os, char *msg)
  250. {
  251. char *id = xs_dict_get(msg, "id");
  252. char *actor = xs_dict_get(msg, "attributedTo");
  253. char *meta = xs_dict_get(msg, "_snac");
  254. xs *s = xs_str_new(NULL);
  255. xs *md5 = xs_md5_hex(id, strlen(id));
  256. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  257. {
  258. xs *s1 = xs_fmt(
  259. "<form method=\"post\" action=\"%s/admin/action\">\n"
  260. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  261. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  262. "<input type=\"button\" name=\"action\" "
  263. "value=\"%s\" onclick=\""
  264. "x = document.getElementById('%s_reply'); "
  265. "if (x.style.display == 'block') "
  266. " x.style.display = 'none'; else "
  267. " x.style.display = 'block';"
  268. "\">\n",
  269. snac->actor, id, actor,
  270. L("Reply"),
  271. md5
  272. );
  273. s = xs_str_cat(s, s1);
  274. }
  275. if (strcmp(actor, snac->actor) != 0) {
  276. /* controls for other actors than this one */
  277. char *l;
  278. l = xs_dict_get(meta, "liked_by");
  279. if (xs_list_in(l, snac->actor) == -1) {
  280. /* not already liked; add button */
  281. s = html_button(s, "like", L("Like"));
  282. }
  283. l = xs_dict_get(meta, "announced_by");
  284. if (xs_list_in(l, snac->actor) == -1) {
  285. /* not already boosted; add button */
  286. s = html_button(s, "boost", L("Boost"));
  287. }
  288. if (following_check(snac, actor)) {
  289. s = html_button(s, "unfollow", L("Unfollow"));
  290. }
  291. else {
  292. s = html_button(s, "follow", L("Follow"));
  293. s = html_button(s, "mute", L("MUTE"));
  294. }
  295. }
  296. s = html_button(s, "delete", L("Delete"));
  297. s = xs_str_cat(s, "</form>\n");
  298. {
  299. /* the post textarea */
  300. xs *ct = build_mentions(snac, msg);
  301. xs *s1 = xs_fmt(
  302. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  303. "<form method=\"post\" action=\"%s/admin/note\" id=\"%s_reply_form\">\n"
  304. "<textarea class=\"snac-textarea\" name=\"content\" "
  305. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  306. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  307. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  308. "</form><p></div>\n",
  309. md5,
  310. snac->actor, md5,
  311. ct,
  312. id,
  313. L("Post")
  314. );
  315. s = xs_str_cat(s, s1);
  316. }
  317. s = xs_str_cat(s, "</div>\n");
  318. return xs_str_cat(os, s);
  319. }
  320. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  321. {
  322. char *id = xs_dict_get(msg, "id");
  323. char *type = xs_dict_get(msg, "type");
  324. char *meta = xs_dict_get(msg, "_snac");
  325. xs *actor_o = NULL;
  326. char *actor;
  327. /* do not show non-public messages in the public timeline */
  328. if (local && !is_msg_public(snac, msg))
  329. return os;
  330. /* return if already seen */
  331. if (xs_set_add(seen, id) == 0)
  332. return os;
  333. xs *s = xs_str_new(NULL);
  334. if (strcmp(type, "Follow") == 0) {
  335. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  336. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  337. s = xs_str_cat(s, s1);
  338. s = html_msg_icon(snac, s, msg);
  339. s = xs_str_cat(s, "</div>\n");
  340. return xs_str_cat(os, s);
  341. }
  342. /* bring the main actor */
  343. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  344. return os;
  345. /* ignore muted morons immediately */
  346. if (is_muted(snac, actor))
  347. return os;
  348. if (!valid_status(actor_get(snac, actor, &actor_o)))
  349. return os;
  350. /* if this is our post, add the score */
  351. if (xs_startswith(id, snac->actor)) {
  352. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  353. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  354. xs *s1 = xs_fmt(
  355. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  356. likes, boosts);
  357. s = xs_str_cat(s, s1);
  358. }
  359. if (level == 0) {
  360. char *p;
  361. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  362. /* print the origin of the post, if any */
  363. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  364. xs *actor_r = NULL;
  365. if (valid_status(actor_get(snac, p, &actor_r))) {
  366. char *name;
  367. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  368. name = xs_dict_get(actor_r, "preferredUsername");
  369. xs *s1 = xs_fmt(
  370. "<div class=\"snac-origin\">"
  371. "<a href=\"%s\">%s</a> %s</div>\n",
  372. xs_dict_get(actor_r, "id"),
  373. name,
  374. L("boosted")
  375. );
  376. s = xs_str_cat(s, s1);
  377. }
  378. }
  379. else
  380. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  381. /* this may happen if any of the autor or the parent aren't here */
  382. xs *s1 = xs_fmt(
  383. "<div class=\"snac-origin\">%s "
  384. "<a href=\"%s\">»</a></div>\n",
  385. L("in reply to"), p
  386. );
  387. s = xs_str_cat(s, s1);
  388. }
  389. else
  390. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  391. xs_list_in(p, snac->actor) != -1) {
  392. /* we boosted this */
  393. xs *s1 = xs_fmt(
  394. "<div class=\"snac-origin\">"
  395. "<a href=\"%s\">%s</a> %s</a></div>",
  396. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  397. );
  398. s = xs_str_cat(s, s1);
  399. }
  400. else
  401. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  402. xs_list_in(p, snac->actor) != -1) {
  403. /* we liked this */
  404. xs *s1 = xs_fmt(
  405. "<div class=\"snac-origin\">"
  406. "<a href=\"%s\">%s</a> %s</a></div>",
  407. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  408. );
  409. s = xs_str_cat(s, s1);
  410. }
  411. }
  412. else
  413. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  414. s = html_msg_icon(snac, s, msg);
  415. /* add the content */
  416. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  417. {
  418. xs *c = xs_dup(xs_dict_get(msg, "content"));
  419. /* do some tweaks to the content */
  420. c = xs_replace_i(c, "\r", "");
  421. while (xs_endswith(c, "<br><br>"))
  422. c = xs_crop(c, 0, -4);
  423. c = xs_replace_i(c, "<br><br>", "<p>");
  424. if (!xs_startswith(c, "<p>")) {
  425. xs *s1 = c;
  426. c = xs_fmt("<p>%s</p>", s1);
  427. }
  428. s = xs_str_cat(s, c);
  429. }
  430. s = xs_str_cat(s, "\n");
  431. /* add the attachments */
  432. char *attach;
  433. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  434. char *v;
  435. while (xs_list_iter(&attach, &v)) {
  436. char *t = xs_dict_get(v, "mediaType");
  437. if (xs_is_null(t))
  438. continue;
  439. if (xs_startswith(t, "image/")) {
  440. char *url = xs_dict_get(v, "url");
  441. char *name = xs_dict_get(v, "name");
  442. if (url != NULL) {
  443. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  444. url, xs_is_null(name) ? "" : name);
  445. s = xs_str_cat(s, s1);
  446. }
  447. }
  448. else
  449. if (xs_startswith(t, "video/")) {
  450. char *url = xs_dict_get(v, "url");
  451. if (url != NULL) {
  452. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  453. s = xs_str_cat(s, s1);
  454. }
  455. }
  456. }
  457. }
  458. s = xs_str_cat(s, "</div>\n");
  459. /** controls **/
  460. if (!local)
  461. s = html_entry_controls(snac, s, msg);
  462. /** children **/
  463. char *children = xs_dict_get(meta, "children");
  464. if (xs_list_len(children)) {
  465. int left = xs_list_len(children);
  466. char *id;
  467. if (level < 4)
  468. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  469. else
  470. s = xs_str_cat(s, "<div>\n");
  471. if (left > 3)
  472. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  473. while (xs_list_iter(&children, &id)) {
  474. xs *chd = timeline_find(snac, id);
  475. if (left == 3)
  476. s = xs_str_cat(s, "</details>\n");
  477. if (chd != NULL)
  478. s = html_entry(snac, s, chd, seen, local, level + 1);
  479. else
  480. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  481. left--;
  482. }
  483. s = xs_str_cat(s, "</div>\n");
  484. }
  485. s = xs_str_cat(s, "</div>\n");
  486. return xs_str_cat(os, s);
  487. }
  488. d_char *html_user_footer(snac *snac, d_char *s)
  489. {
  490. xs *s1 = xs_fmt(
  491. "<div class=\"snac-footer\">\n"
  492. "<a href=\"%s\">%s</a> - "
  493. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  494. srv_baseurl,
  495. L("about this site")
  496. );
  497. return xs_str_cat(s, s1);
  498. }
  499. d_char *html_timeline(snac *snac, char *list, int local)
  500. /* returns the HTML for the timeline */
  501. {
  502. d_char *s = xs_str_new(NULL);
  503. xs_set *seen = xs_set_new(4096);
  504. char *v;
  505. double t = ftime();
  506. s = html_user_header(snac, s, local);
  507. if (!local)
  508. s = html_top_controls(snac, s);
  509. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  510. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  511. while (xs_list_iter(&list, &v)) {
  512. xs *msg = timeline_get(snac, v);
  513. s = html_entry(snac, s, msg, seen, local, 0);
  514. }
  515. s = xs_str_cat(s, "</div>\n");
  516. if (local) {
  517. xs *s1 = xs_fmt(
  518. "<div class=\"snac-history\">\n"
  519. "<p class=\"snac-history-title\">%s</p><ul>\n",
  520. L("History")
  521. );
  522. s = xs_str_cat(s, s1);
  523. xs *list = history_list(snac);
  524. char *p, *v;
  525. p = list;
  526. while (xs_list_iter(&p, &v)) {
  527. xs *fn = xs_replace(v, ".html", "");
  528. xs *s1 = xs_fmt(
  529. "<li><a href=\"%s/h/%s\">%s</li>\n",
  530. snac->actor, v, fn);
  531. s = xs_str_cat(s, s1);
  532. }
  533. s = xs_str_cat(s, "</ul></div>\n");
  534. }
  535. s = html_user_footer(snac, s);
  536. {
  537. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  538. s = xs_str_cat(s, s1);
  539. }
  540. s = xs_str_cat(s, "</body>\n</html>\n");
  541. xs_set_free(seen);
  542. return s;
  543. }
  544. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  545. {
  546. int status = 404;
  547. snac snac;
  548. char *uid, *p_path;
  549. int cache = 1;
  550. char *v;
  551. xs *l = xs_split_n(q_path, "/", 2);
  552. uid = xs_list_get(l, 1);
  553. if (!uid || !user_open(&snac, uid)) {
  554. /* invalid user */
  555. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  556. return 404;
  557. }
  558. /* check if server config variable 'disable_cache' is set */
  559. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  560. cache = 0;
  561. p_path = xs_list_get(l, 2);
  562. if (p_path == NULL) {
  563. /* public timeline */
  564. xs *h = xs_str_localtime(0, "%Y-%m.html");
  565. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  566. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  567. *body = history_get(&snac, h);
  568. *b_size = strlen(*body);
  569. status = 200;
  570. }
  571. else {
  572. xs *list = local_list(&snac, 0xfffffff);
  573. *body = html_timeline(&snac, list, 1);
  574. *b_size = strlen(*body);
  575. status = 200;
  576. history_add(&snac, h, *body, *b_size);
  577. }
  578. }
  579. else
  580. if (strcmp(p_path, "admin") == 0) {
  581. /* private timeline */
  582. if (!login(&snac, req))
  583. status = 401;
  584. else {
  585. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  586. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  587. *body = history_get(&snac, "timeline.html_");
  588. *b_size = strlen(*body);
  589. status = 200;
  590. }
  591. else {
  592. snac_debug(&snac, 1, xs_fmt("building timeline"));
  593. xs *list = timeline_list(&snac, 0xfffffff);
  594. *body = html_timeline(&snac, list, 0);
  595. *b_size = strlen(*body);
  596. status = 200;
  597. history_add(&snac, "timeline.html_", *body, *b_size);
  598. }
  599. }
  600. }
  601. else
  602. if (xs_startswith(p_path, "p/")) {
  603. /* a timeline with just one entry */
  604. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  605. xs *fn = _timeline_find_fn(&snac, id);
  606. if (fn != NULL) {
  607. xs *list = xs_list_new();
  608. list = xs_list_append(list, fn);
  609. *body = html_timeline(&snac, list, 1);
  610. *b_size = strlen(*body);
  611. status = 200;
  612. }
  613. }
  614. else
  615. if (xs_startswith(p_path, "s/")) {
  616. /* a static file */
  617. }
  618. else
  619. if (xs_startswith(p_path, "h/")) {
  620. /* an entry from the history */
  621. xs *id = xs_replace(p_path, "h/", "");
  622. if ((*body = history_get(&snac, id)) != NULL) {
  623. *b_size = strlen(*body);
  624. status = 200;
  625. }
  626. }
  627. else
  628. status = 404;
  629. user_free(&snac);
  630. if (valid_status(status)) {
  631. *ctype = "text/html; charset=utf-8";
  632. }
  633. return status;
  634. }
  635. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  636. char **body, int *b_size, char **ctype)
  637. {
  638. int status = 0;
  639. snac snac;
  640. char *uid, *p_path;
  641. char *p_vars;
  642. xs *l = xs_split_n(q_path, "/", 2);
  643. uid = xs_list_get(l, 1);
  644. if (!uid || !user_open(&snac, uid)) {
  645. /* invalid user */
  646. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  647. return 404;
  648. }
  649. p_path = xs_list_get(l, 2);
  650. /* all posts must be authenticated */
  651. if (!login(&snac, req))
  652. return 401;
  653. p_vars = xs_dict_get(req, "p_vars");
  654. #if 0
  655. {
  656. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  657. printf("%s\n", j1);
  658. }
  659. #endif
  660. if (p_path && strcmp(p_path, "admin/note") == 0) {
  661. /* post note */
  662. char *content = xs_dict_get(p_vars, "content");
  663. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  664. char *attach_url = xs_dict_get(p_vars, "attach_url");
  665. if (content != NULL) {
  666. xs *msg = NULL;
  667. xs *c_msg = NULL;
  668. msg = msg_note(&snac, content, NULL, in_reply_to, attach_url);
  669. c_msg = msg_create(&snac, msg);
  670. post(&snac, c_msg);
  671. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  672. }
  673. status = 303;
  674. }
  675. else
  676. if (p_path && strcmp(p_path, "admin/action") == 0) {
  677. /* action on an entry */
  678. char *id = xs_dict_get(p_vars, "id");
  679. char *actor = xs_dict_get(p_vars, "actor");
  680. char *action = xs_dict_get(p_vars, "action");
  681. if (action == NULL)
  682. return 404;
  683. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  684. status = 303;
  685. if (strcmp(action, L("Like")) == 0) {
  686. xs *msg = msg_admiration(&snac, id, "Like");
  687. post(&snac, msg);
  688. timeline_admire(&snac, id, snac.actor, 1);
  689. }
  690. else
  691. if (strcmp(action, L("Boost")) == 0) {
  692. xs *msg = msg_admiration(&snac, id, "Announce");
  693. post(&snac, msg);
  694. timeline_admire(&snac, id, snac.actor, 0);
  695. }
  696. else
  697. if (strcmp(action, L("MUTE")) == 0) {
  698. mute(&snac, actor);
  699. }
  700. else
  701. if (strcmp(action, L("Follow")) == 0) {
  702. xs *msg = msg_follow(&snac, actor);
  703. /* reload the actor from the message, in may be different */
  704. actor = xs_dict_get(msg, "object");
  705. following_add(&snac, actor, msg);
  706. enqueue_output(&snac, msg, actor, 0);
  707. }
  708. else
  709. if (strcmp(action, L("Unfollow")) == 0) {
  710. /* get the following object */
  711. xs *object = NULL;
  712. if (valid_status(following_get(&snac, actor, &object))) {
  713. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  714. following_del(&snac, actor);
  715. enqueue_output(&snac, msg, actor, 0);
  716. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  717. }
  718. else
  719. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  720. }
  721. else
  722. if (strcmp(action, L("Delete")) == 0) {
  723. /* delete an entry */
  724. if (xs_startswith(id, snac.actor)) {
  725. /* it's a post by us: generate a delete */
  726. xs *msg = msg_delete(&snac, id);
  727. post(&snac, msg);
  728. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  729. }
  730. timeline_del(&snac, id);
  731. snac_log(&snac, xs_fmt("deleted entry %s", id));
  732. }
  733. else
  734. status = 404;
  735. /* delete the cached timeline */
  736. if (status == 303)
  737. history_del(&snac, "timeline.html_");
  738. }
  739. else
  740. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  741. /* change of user data */
  742. char *v;
  743. char *p1, *p2;
  744. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  745. snac.config = xs_dict_set(snac.config, "name", v);
  746. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  747. snac.config = xs_dict_set(snac.config, "avatar", v);
  748. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  749. snac.config = xs_dict_set(snac.config, "bio", v);
  750. /* password change? */
  751. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  752. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  753. *p1 && strcmp(p1, p2) == 0) {
  754. xs *pw = hash_password(snac.uid, p1, NULL);
  755. snac.config = xs_dict_set(snac.config, "passwd", pw);
  756. }
  757. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  758. xs *bfn = xs_fmt("%s.bak", fn);
  759. FILE *f;
  760. rename(fn, bfn);
  761. if ((f = fopen(fn, "w")) != NULL) {
  762. xs *j = xs_json_dumps_pp(snac.config, 4);
  763. fwrite(j, strlen(j), 1, f);
  764. fclose(f);
  765. }
  766. else
  767. rename(bfn, fn);
  768. history_del(&snac, "timeline.html_");
  769. xs *a_msg = msg_actor(&snac);
  770. xs *u_msg = msg_update(&snac, a_msg);
  771. post(&snac, u_msg);
  772. status = 303;
  773. }
  774. if (status == 303) {
  775. *body = xs_fmt("%s/admin#snac-posts", snac.actor);
  776. *b_size = strlen(*body);
  777. }
  778. return status;
  779. }