html.c 31 KB

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