html.c 27 KB

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