html.c 29 KB

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