html.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  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. "</form><p>\n"
  155. "</div>\n"
  156. "<div class=\"snac-top-controls-more\">\n"
  157. "<details><summary>%s</summary>\n"
  158. "<form method=\"post\" action=\"%s/admin/action\">\n"
  159. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  160. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  161. "</form></p>\n"
  162. "<form method=\"post\" action=\"%s/admin/action\">\n"
  163. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  164. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  165. "</form></p>\n"
  166. "<details><summary>%s</summary>\n"
  167. "<div class=\"snac-user-setup\">\n"
  168. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  169. "<p>%s:<br>\n"
  170. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  171. "<p>%s:<br>\n"
  172. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  173. "<p>%s:<br>\n"
  174. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  175. "<p>%s:<br>\n"
  176. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  177. "<p>%s:<br>\n"
  178. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  179. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  180. "</form>\n"
  181. "</div>\n"
  182. "</details>\n"
  183. "</details>\n"
  184. "</div>\n"
  185. "</div>\n";
  186. xs *s1 = xs_fmt(_tmpl,
  187. snac->actor,
  188. L("Post"),
  189. L("More options..."),
  190. snac->actor,
  191. L("Follow"), L("(by URL or user@host)"),
  192. snac->actor,
  193. L("Boost"), L("(by URL)"),
  194. L("User setup..."),
  195. snac->actor,
  196. L("User name"),
  197. xs_dict_get(snac->config, "name"),
  198. L("Avatar URL"),
  199. xs_dict_get(snac->config, "avatar"),
  200. L("Bio"),
  201. xs_dict_get(snac->config, "bio"),
  202. L("Password (only to change it)"),
  203. L("Repeat Password"),
  204. L("Update user info")
  205. );
  206. s = xs_str_cat(s, s1);
  207. return s;
  208. }
  209. d_char *html_button(d_char *s, char *clss, char *label)
  210. {
  211. xs *s1 = xs_fmt(
  212. "<input type=\"submit\" name=\"action\" "
  213. "class=\"snac-btn-%s\" value=\"%s\">\n",
  214. clss, label);
  215. return xs_str_cat(s, s1);
  216. }
  217. d_char *build_mentions(snac *snac, char *msg)
  218. /* returns a string with the mentions in msg */
  219. {
  220. d_char *s = xs_str_new(NULL);
  221. char *list = xs_dict_get(msg, "tag");
  222. char *v;
  223. while (xs_list_iter(&list, &v)) {
  224. char *type = xs_dict_get(v, "type");
  225. char *href = xs_dict_get(v, "href");
  226. char *name = xs_dict_get(v, "name");
  227. if (type && strcmp(type, "Mention") == 0 &&
  228. href && strcmp(href, snac->actor) != 0 && name) {
  229. xs *l = xs_split(name, "@");
  230. /* if it's a name without host, query the webfinger */
  231. if (xs_list_len(l) < 3) {
  232. #if 0
  233. xs *actor = NULL;
  234. xs *user = NULL;
  235. if (valid_status(webfinger_request(href, &actor, &user))) {
  236. s = xs_str_cat(s, user);
  237. s = xs_str_cat(s, " ");
  238. }
  239. #endif
  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. actor = xs_dict_get(msg, "actor");
  336. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  337. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  338. s = xs_str_cat(s, s1);
  339. s = html_msg_icon(snac, s, msg);
  340. s = xs_str_cat(s, "</div>\n");
  341. return xs_str_cat(os, s);
  342. }
  343. /* bring the main actor */
  344. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  345. return os;
  346. /* ignore muted morons immediately */
  347. if (is_muted(snac, actor))
  348. return os;
  349. if (!valid_status(actor_get(snac, actor, &actor_o)))
  350. return os;
  351. /* if this is our post, add the score */
  352. if (xs_startswith(id, snac->actor)) {
  353. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  354. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  355. xs *s1 = xs_fmt(
  356. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  357. likes, boosts);
  358. s = xs_str_cat(s, s1);
  359. }
  360. if (level == 0) {
  361. char *p;
  362. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  363. /* print the origin of the post, if any */
  364. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  365. xs *actor_r = NULL;
  366. if (valid_status(actor_get(snac, p, &actor_r))) {
  367. char *name;
  368. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  369. name = xs_dict_get(actor_r, "preferredUsername");
  370. xs *s1 = xs_fmt(
  371. "<div class=\"snac-origin\">"
  372. "<a href=\"%s\">%s</a> %s</div>\n",
  373. xs_dict_get(actor_r, "id"),
  374. name,
  375. L("boosted")
  376. );
  377. s = xs_str_cat(s, s1);
  378. }
  379. }
  380. else
  381. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  382. /* this may happen if any of the autor or the parent aren't here */
  383. xs *s1 = xs_fmt(
  384. "<div class=\"snac-origin\">%s "
  385. "<a href=\"%s\">»</a></div>\n",
  386. L("in reply to"), p
  387. );
  388. s = xs_str_cat(s, s1);
  389. }
  390. else
  391. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  392. xs_list_in(p, snac->actor) != -1) {
  393. /* we boosted this */
  394. xs *s1 = xs_fmt(
  395. "<div class=\"snac-origin\">"
  396. "<a href=\"%s\">%s</a> %s</a></div>",
  397. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  398. );
  399. s = xs_str_cat(s, s1);
  400. }
  401. else
  402. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  403. xs_list_in(p, snac->actor) != -1) {
  404. /* we liked this */
  405. xs *s1 = xs_fmt(
  406. "<div class=\"snac-origin\">"
  407. "<a href=\"%s\">%s</a> %s</a></div>",
  408. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  409. );
  410. s = xs_str_cat(s, s1);
  411. }
  412. }
  413. else
  414. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  415. s = html_msg_icon(snac, s, msg);
  416. /* add the content */
  417. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  418. {
  419. xs *c = xs_dup(xs_dict_get(msg, "content"));
  420. /* do some tweaks to the content */
  421. c = xs_replace_i(c, "\r", "");
  422. while (xs_endswith(c, "<br><br>"))
  423. c = xs_crop(c, 0, -4);
  424. c = xs_replace_i(c, "<br><br>", "<p>");
  425. if (!xs_startswith(c, "<p>")) {
  426. xs *s1 = c;
  427. c = xs_fmt("<p>%s</p>", s1);
  428. }
  429. s = xs_str_cat(s, c);
  430. }
  431. s = xs_str_cat(s, "\n");
  432. /* add the attachments */
  433. char *attach;
  434. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  435. char *v;
  436. while (xs_list_iter(&attach, &v)) {
  437. char *t = xs_dict_get(v, "mediaType");
  438. if (xs_is_null(t))
  439. continue;
  440. if (xs_startswith(t, "image/")) {
  441. char *url = xs_dict_get(v, "url");
  442. char *name = xs_dict_get(v, "name");
  443. if (url != NULL) {
  444. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  445. url, xs_is_null(name) ? "" : name);
  446. s = xs_str_cat(s, s1);
  447. }
  448. }
  449. else
  450. if (xs_startswith(t, "video/")) {
  451. char *url = xs_dict_get(v, "url");
  452. if (url != NULL) {
  453. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  454. s = xs_str_cat(s, s1);
  455. }
  456. }
  457. }
  458. }
  459. s = xs_str_cat(s, "</div>\n");
  460. /** controls **/
  461. if (!local)
  462. s = html_entry_controls(snac, s, msg);
  463. /** children **/
  464. char *children = xs_dict_get(meta, "children");
  465. if (xs_list_len(children)) {
  466. int left = xs_list_len(children);
  467. char *id;
  468. if (level < 4)
  469. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  470. else
  471. s = xs_str_cat(s, "<div>\n");
  472. if (left > 3)
  473. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  474. while (xs_list_iter(&children, &id)) {
  475. xs *chd = timeline_find(snac, id);
  476. if (left == 3)
  477. s = xs_str_cat(s, "</details>\n");
  478. if (chd != NULL)
  479. s = html_entry(snac, s, chd, seen, local, level + 1);
  480. else
  481. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  482. left--;
  483. }
  484. s = xs_str_cat(s, "</div>\n");
  485. }
  486. s = xs_str_cat(s, "</div>\n");
  487. return xs_str_cat(os, s);
  488. }
  489. d_char *html_user_footer(snac *snac, d_char *s)
  490. {
  491. xs *s1 = xs_fmt(
  492. "<div class=\"snac-footer\">\n"
  493. "<a href=\"%s\">%s</a> - "
  494. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  495. srv_baseurl,
  496. L("about this site")
  497. );
  498. return xs_str_cat(s, s1);
  499. }
  500. d_char *html_timeline(snac *snac, char *list, int local)
  501. /* returns the HTML for the timeline */
  502. {
  503. d_char *s = xs_str_new(NULL);
  504. xs_set *seen = xs_set_new(4096);
  505. char *v;
  506. double t = ftime();
  507. s = html_user_header(snac, s, local);
  508. if (!local)
  509. s = html_top_controls(snac, s);
  510. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  511. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  512. while (xs_list_iter(&list, &v)) {
  513. xs *msg = timeline_get(snac, v);
  514. s = html_entry(snac, s, msg, seen, local, 0);
  515. }
  516. s = xs_str_cat(s, "</div>\n");
  517. if (local) {
  518. xs *s1 = xs_fmt(
  519. "<div class=\"snac-history\">\n"
  520. "<p class=\"snac-history-title\">%s</p><ul>\n",
  521. L("History")
  522. );
  523. s = xs_str_cat(s, s1);
  524. xs *list = history_list(snac);
  525. char *p, *v;
  526. p = list;
  527. while (xs_list_iter(&p, &v)) {
  528. xs *fn = xs_replace(v, ".html", "");
  529. xs *s1 = xs_fmt(
  530. "<li><a href=\"%s/h/%s\">%s</li>\n",
  531. snac->actor, v, fn);
  532. s = xs_str_cat(s, s1);
  533. }
  534. s = xs_str_cat(s, "</ul></div>\n");
  535. }
  536. s = html_user_footer(snac, s);
  537. {
  538. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  539. s = xs_str_cat(s, s1);
  540. }
  541. s = xs_str_cat(s, "</body>\n</html>\n");
  542. xs_set_free(seen);
  543. return s;
  544. }
  545. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  546. {
  547. int status = 404;
  548. snac snac;
  549. char *uid, *p_path;
  550. int cache = 1;
  551. char *v;
  552. xs *l = xs_split_n(q_path, "/", 2);
  553. uid = xs_list_get(l, 1);
  554. if (!uid || !user_open(&snac, uid)) {
  555. /* invalid user */
  556. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  557. return 404;
  558. }
  559. /* check if server config variable 'disable_cache' is set */
  560. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  561. cache = 0;
  562. p_path = xs_list_get(l, 2);
  563. if (p_path == NULL) {
  564. /* public timeline */
  565. xs *h = xs_str_localtime(0, "%Y-%m.html");
  566. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  567. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  568. *body = history_get(&snac, h);
  569. *b_size = strlen(*body);
  570. status = 200;
  571. }
  572. else {
  573. xs *list = local_list(&snac, 0xfffffff);
  574. *body = html_timeline(&snac, list, 1);
  575. *b_size = strlen(*body);
  576. status = 200;
  577. history_add(&snac, h, *body, *b_size);
  578. }
  579. }
  580. else
  581. if (strcmp(p_path, "admin") == 0) {
  582. /* private timeline */
  583. if (!login(&snac, req))
  584. status = 401;
  585. else {
  586. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  587. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  588. *body = history_get(&snac, "timeline.html_");
  589. *b_size = strlen(*body);
  590. status = 200;
  591. }
  592. else {
  593. snac_debug(&snac, 1, xs_fmt("building timeline"));
  594. xs *list = timeline_list(&snac, 0xfffffff);
  595. *body = html_timeline(&snac, list, 0);
  596. *b_size = strlen(*body);
  597. status = 200;
  598. history_add(&snac, "timeline.html_", *body, *b_size);
  599. }
  600. }
  601. }
  602. else
  603. if (xs_startswith(p_path, "p/")) {
  604. /* a timeline with just one entry */
  605. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  606. xs *fn = _timeline_find_fn(&snac, id);
  607. if (fn != NULL) {
  608. xs *list = xs_list_new();
  609. list = xs_list_append(list, fn);
  610. *body = html_timeline(&snac, list, 1);
  611. *b_size = strlen(*body);
  612. status = 200;
  613. }
  614. }
  615. else
  616. if (xs_startswith(p_path, "s/")) {
  617. /* a static file */
  618. }
  619. else
  620. if (xs_startswith(p_path, "h/")) {
  621. /* an entry from the history */
  622. xs *id = xs_replace(p_path, "h/", "");
  623. if ((*body = history_get(&snac, id)) != NULL) {
  624. *b_size = strlen(*body);
  625. status = 200;
  626. }
  627. }
  628. else
  629. status = 404;
  630. user_free(&snac);
  631. if (valid_status(status)) {
  632. *ctype = "text/html; charset=utf-8";
  633. }
  634. return status;
  635. }
  636. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  637. char **body, int *b_size, char **ctype)
  638. {
  639. int status = 0;
  640. snac snac;
  641. char *uid, *p_path;
  642. char *p_vars;
  643. xs *l = xs_split_n(q_path, "/", 2);
  644. uid = xs_list_get(l, 1);
  645. if (!uid || !user_open(&snac, uid)) {
  646. /* invalid user */
  647. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  648. return 404;
  649. }
  650. p_path = xs_list_get(l, 2);
  651. /* all posts must be authenticated */
  652. if (!login(&snac, req))
  653. return 401;
  654. p_vars = xs_dict_get(req, "p_vars");
  655. if (p_path && strcmp(p_path, "admin/note") == 0) {
  656. /* post note */
  657. char *content = xs_dict_get(p_vars, "content");
  658. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  659. if (content != NULL) {
  660. xs *msg = NULL;
  661. xs *c_msg = NULL;
  662. msg = msg_note(&snac, content, NULL, in_reply_to);
  663. c_msg = msg_create(&snac, msg);
  664. post(&snac, c_msg);
  665. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  666. }
  667. status = 303;
  668. }
  669. else
  670. if (p_path && strcmp(p_path, "admin/action") == 0) {
  671. /* action on an entry */
  672. char *id = xs_dict_get(p_vars, "id");
  673. char *actor = xs_dict_get(p_vars, "actor");
  674. char *action = xs_dict_get(p_vars, "action");
  675. if (action == NULL)
  676. return 404;
  677. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  678. status = 303;
  679. if (strcmp(action, L("Like")) == 0) {
  680. xs *msg = msg_admiration(&snac, id, "Like");
  681. post(&snac, msg);
  682. timeline_admire(&snac, id, snac.actor, 1);
  683. }
  684. else
  685. if (strcmp(action, L("Boost")) == 0) {
  686. xs *msg = msg_admiration(&snac, id, "Announce");
  687. post(&snac, msg);
  688. timeline_admire(&snac, id, snac.actor, 0);
  689. }
  690. else
  691. if (strcmp(action, L("MUTE")) == 0) {
  692. mute(&snac, actor);
  693. }
  694. else
  695. if (strcmp(action, L("Follow")) == 0) {
  696. xs *msg = msg_follow(&snac, actor);
  697. /* reload the actor from the message, in may be different */
  698. actor = xs_dict_get(msg, "object");
  699. following_add(&snac, actor, msg);
  700. enqueue_output(&snac, msg, actor, 0);
  701. }
  702. else
  703. if (strcmp(action, L("Unfollow")) == 0) {
  704. /* get the following object */
  705. xs *object = NULL;
  706. if (valid_status(following_get(&snac, actor, &object))) {
  707. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  708. following_del(&snac, actor);
  709. enqueue_output(&snac, msg, actor, 0);
  710. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  711. }
  712. else
  713. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  714. }
  715. else
  716. if (strcmp(action, L("Delete")) == 0) {
  717. /* delete an entry */
  718. if (xs_startswith(id, snac.actor)) {
  719. /* it's a post by us: generate a delete */
  720. xs *msg = msg_delete(&snac, id);
  721. post(&snac, msg);
  722. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  723. }
  724. timeline_del(&snac, id);
  725. snac_log(&snac, xs_fmt("deleted entry %s", id));
  726. }
  727. else
  728. status = 404;
  729. /* delete the cached timeline */
  730. if (status == 303)
  731. history_del(&snac, "timeline.html_");
  732. }
  733. else
  734. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  735. /* change of user data */
  736. char *v;
  737. char *p1, *p2;
  738. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  739. snac.config = xs_dict_set(snac.config, "name", v);
  740. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  741. snac.config = xs_dict_set(snac.config, "avatar", v);
  742. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  743. snac.config = xs_dict_set(snac.config, "bio", v);
  744. /* password change? */
  745. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  746. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  747. *p1 && strcmp(p1, p2) == 0) {
  748. xs *pw = hash_password(snac.uid, p1, NULL);
  749. snac.config = xs_dict_set(snac.config, "passwd", pw);
  750. }
  751. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  752. xs *bfn = xs_fmt("%s.bak", fn);
  753. FILE *f;
  754. rename(fn, bfn);
  755. if ((f = fopen(fn, "w")) != NULL) {
  756. xs *j = xs_json_dumps_pp(snac.config, 4);
  757. fwrite(j, strlen(j), 1, f);
  758. fclose(f);
  759. }
  760. else
  761. rename(bfn, fn);
  762. history_del(&snac, "timeline.html_");
  763. xs *a_msg = msg_actor(&snac);
  764. xs *u_msg = msg_update(&snac, a_msg);
  765. post(&snac, u_msg);
  766. status = 303;
  767. }
  768. if (status == 303) {
  769. *body = xs_fmt("%s/admin#snac-posts", snac.actor);
  770. *b_size = strlen(*body);
  771. }
  772. return status;
  773. }