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