html.c 30 KB

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