html.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. v = "&nbsp;";
  161. {
  162. xs *s1 = xs_fmt("<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  163. s = xs_str_cat(s, s1);
  164. }
  165. }
  166. return xs_str_cat(os, s);
  167. }
  168. d_char *html_user_header(snac *snac, d_char *s, int local)
  169. /* creates the HTML header */
  170. {
  171. char *p, *v;
  172. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  173. s = xs_str_cat(s, "<meta name=\"viewport\" "
  174. "content=\"width=device-width, initial-scale=1\"/>\n");
  175. s = xs_str_cat(s, "<meta name=\"generator\" "
  176. "content=\"" USER_AGENT "\"/>\n");
  177. /* add server CSS */
  178. p = xs_dict_get(srv_config, "cssurls");
  179. while (xs_list_iter(&p, &v)) {
  180. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  181. s = xs_str_cat(s, s1);
  182. }
  183. /* add the user CSS */
  184. {
  185. xs *css = NULL;
  186. int size;
  187. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  188. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  189. s = xs_str_cat(s, s1);
  190. }
  191. }
  192. {
  193. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  194. s = xs_str_cat(s, s1);
  195. }
  196. s = xs_str_cat(s, "</head>\n<body>\n");
  197. /* top nav */
  198. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  199. {
  200. xs *s1;
  201. if (local)
  202. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  203. else
  204. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  205. s = xs_str_cat(s, s1);
  206. }
  207. /* user info */
  208. {
  209. xs *bio = NULL;
  210. char *_tmpl =
  211. "<div class=\"h-card snac-top-user\">\n"
  212. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  213. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  214. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  215. "</div>\n";
  216. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  217. xs *s1 = xs_fmt(_tmpl,
  218. xs_dict_get(snac->config, "name"),
  219. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  220. bio
  221. );
  222. s = xs_str_cat(s, s1);
  223. }
  224. return s;
  225. }
  226. d_char *html_top_controls(snac *snac, d_char *s)
  227. /* generates the top controls */
  228. {
  229. char *_tmpl =
  230. "<div class=\"snac-top-controls\">\n"
  231. "<div class=\"snac-note\">\n"
  232. "<form method=\"post\" action=\"%s/admin/note\">\n"
  233. "<textarea class=\"snac-textarea\" name=\"content\" "
  234. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  235. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  236. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  237. "</form><p>\n"
  238. "</div>\n"
  239. "<div class=\"snac-top-controls-more\">\n"
  240. "<details><summary>%s</summary>\n"
  241. "<form method=\"post\" action=\"%s/admin/action\">\n"
  242. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  243. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  244. "</form></p>\n"
  245. "<form method=\"post\" action=\"%s\">\n"
  246. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  247. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  248. "</form></p>\n"
  249. "<details><summary>%s</summary>\n"
  250. "<div class=\"snac-user-setup\">\n"
  251. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  252. "<p>%s:<br>\n"
  253. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  254. "<p>%s:<br>\n"
  255. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  256. "<p>%s:<br>\n"
  257. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  258. "<p>%s:<br>\n"
  259. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  260. "<p>%s:<br>\n"
  261. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  262. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  263. "</form>\n"
  264. "</div>\n"
  265. "</details>\n"
  266. "</details>\n"
  267. "</div>\n"
  268. "</div>\n";
  269. xs *s1 = xs_fmt(_tmpl,
  270. snac->actor,
  271. L("Post"),
  272. L("More options..."),
  273. snac->actor,
  274. L("Follow"), L("(by URL or user@host)"),
  275. snac->actor,
  276. L("Boost"), L("(by URL)"),
  277. L("User setup..."),
  278. snac->actor,
  279. L("User name"),
  280. xs_dict_get(snac->config, "name"),
  281. L("Avatar URL"),
  282. xs_dict_get(snac->config, "avatar"),
  283. L("Bio"),
  284. xs_dict_get(snac->config, "bio"),
  285. L("Password (only to change it)"),
  286. L("Repeat Password"),
  287. L("Update user info")
  288. );
  289. s = xs_str_cat(s, s1);
  290. return s;
  291. }
  292. d_char *html_button(d_char *s, char *clss, char *label)
  293. {
  294. xs *s1 = xs_fmt(
  295. "<input type=\"submit\" name=\"action\" "
  296. "class=\"snac-btn-%s\" value=\"%s\">\n",
  297. clss, label);
  298. return xs_str_cat(s, s1);
  299. }
  300. d_char *html_entry_controls(snac *snac, d_char *os, char *msg)
  301. {
  302. char *id = xs_dict_get(msg, "id");
  303. char *actor = xs_dict_get(msg, "attributedTo");
  304. char *meta = xs_dict_get(msg, "_snac");
  305. xs *s = xs_str_new(NULL);
  306. xs *md5 = xs_md5_hex(id, strlen(id));
  307. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  308. {
  309. xs *s1 = xs_fmt(
  310. "<form method=\"post\" action=\"%s/admin/action\">\n"
  311. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  312. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  313. "<input type=\"button\" name=\"action\" "
  314. "value=\"%s\" onclick=\""
  315. "x = document.getElementById('%s_reply'); "
  316. "if (x.style.display == 'block') "
  317. " x.style.display = 'none'; else "
  318. " x.style.display = 'block';"
  319. "\">\n",
  320. snac->actor, id, actor,
  321. L("Reply"),
  322. md5
  323. );
  324. s = xs_str_cat(s, s1);
  325. }
  326. if (strcmp(actor, snac->actor) != 0) {
  327. /* controls for other actors than this one */
  328. char *l;
  329. l = xs_dict_get(meta, "liked_by");
  330. if (xs_list_in(l, snac->actor) == -1) {
  331. /* not already liked; add button */
  332. s = html_button(s, "like", L("Like"));
  333. }
  334. l = xs_dict_get(meta, "announced_by");
  335. if (xs_list_in(l, snac->actor) == -1) {
  336. /* not already boosted; add button */
  337. s = html_button(s, "boost", L("Boost"));
  338. }
  339. if (following_check(snac, actor)) {
  340. s = html_button(s, "unfollow", L("Unfollow"));
  341. }
  342. else {
  343. s = html_button(s, "follow", L("Follow"));
  344. s = html_button(s, "mute", L("MUTE"));
  345. }
  346. }
  347. s = html_button(s, "delete", L("Delete"));
  348. s = xs_str_cat(s, "</form>\n");
  349. {
  350. /* the post textarea */
  351. xs *ct = xs_str_new("");
  352. xs *s1 = xs_fmt(
  353. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  354. "<form method=\"post\" action=\"%s/admin/note\" id=\"%s_reply_form\">\n"
  355. "<textarea class=\"snac-textarea\" name=\"content\" "
  356. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  357. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  358. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  359. "</form><p></div>\n",
  360. md5,
  361. snac->actor, md5,
  362. ct,
  363. id,
  364. L("Post")
  365. );
  366. s = xs_str_cat(s, s1);
  367. }
  368. s = xs_str_cat(s, "</div>\n");
  369. return xs_str_cat(os, s);
  370. }
  371. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  372. {
  373. char *id = xs_dict_get(msg, "id");
  374. char *type = xs_dict_get(msg, "type");
  375. char *meta = xs_dict_get(msg, "_snac");
  376. xs *actor_o = NULL;
  377. char *actor;
  378. /* return if already seen */
  379. if (xs_set_add(seen, id) == 0)
  380. return os;
  381. xs *s = xs_str_new(NULL);
  382. if (strcmp(type, "Follow") == 0) {
  383. actor = xs_dict_get(msg, "actor");
  384. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  385. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  386. s = xs_str_cat(s, s1);
  387. s = html_msg_icon(snac, s, msg);
  388. s = xs_str_cat(s, "</div>\n");
  389. return xs_str_cat(os, s);
  390. }
  391. /* bring the main actor */
  392. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  393. return os;
  394. /* ignore muted morons immediately */
  395. if (is_muted(snac, actor))
  396. return os;
  397. if (!valid_status(actor_get(snac, actor, &actor_o)))
  398. return os;
  399. /* if this is our post, add the score */
  400. if (xs_startswith(id, snac->actor)) {
  401. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  402. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  403. xs *s1 = xs_fmt(
  404. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  405. likes, boosts);
  406. s = xs_str_cat(s, s1);
  407. }
  408. if (level == 0) {
  409. char *referrer;
  410. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  411. /* print the origin of the post, if any */
  412. if ((referrer = xs_dict_get(meta, "referrer")) != NULL) {
  413. xs *actor_r = NULL;
  414. if (valid_status(actor_get(snac, referrer, &actor_r))) {
  415. char *name;
  416. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  417. name = xs_dict_get(actor_r, "preferredUsername");
  418. xs *s1 = xs_fmt(
  419. "<div class=\"snac-origin\">"
  420. "<a href=\"%s\">%s</a> %s</div>\n",
  421. xs_dict_get(actor_r, "id"),
  422. name,
  423. L("boosted")
  424. );
  425. s = xs_str_cat(s, s1);
  426. }
  427. }
  428. }
  429. else
  430. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  431. s = html_msg_icon(snac, s, msg);
  432. /* add the content */
  433. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  434. {
  435. xs *c = xs_dup(xs_dict_get(msg, "content"));
  436. /* do some tweaks to the content */
  437. c = xs_replace_i(c, "\r", "");
  438. while (xs_endswith(c, "<br><br>"))
  439. c = xs_crop(c, 0, -4);
  440. c = xs_replace_i(c, "<br><br>", "<p>");
  441. if (!xs_startswith(c, "<p>")) {
  442. xs *s1 = c;
  443. c = xs_fmt("<p>%s</p>", s1);
  444. }
  445. s = xs_str_cat(s, c);
  446. }
  447. s = xs_str_cat(s, "\n");
  448. /* add the attachments */
  449. char *attach;
  450. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  451. char *v;
  452. while (xs_list_iter(&attach, &v)) {
  453. char *t = xs_dict_get(v, "mediaType");
  454. if (t && xs_startswith(t, "image/")) {
  455. char *url = xs_dict_get(v, "url");
  456. char *name = xs_dict_get(v, "name");
  457. if (url != NULL) {
  458. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  459. url, xs_is_null(name) ? "" : name);
  460. s = xs_str_cat(s, s1);
  461. }
  462. }
  463. }
  464. }
  465. s = xs_str_cat(s, "</div>\n");
  466. /** controls **/
  467. if (!local)
  468. s = html_entry_controls(snac, s, msg);
  469. /** children **/
  470. char *children = xs_dict_get(meta, "children");
  471. if (xs_list_len(children)) {
  472. int left = xs_list_len(children);
  473. char *id;
  474. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  475. if (left > 3)
  476. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  477. while (xs_list_iter(&children, &id)) {
  478. xs *chd = timeline_find(snac, id);
  479. if (left == 0)
  480. s = xs_str_cat(s, "</details>\n");
  481. if (chd != NULL)
  482. s = html_entry(snac, s, chd, seen, local, level + 1);
  483. else
  484. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  485. left--;
  486. }
  487. s = xs_str_cat(s, "</div>\n");
  488. }
  489. s = xs_str_cat(s, "</div>\n");
  490. return xs_str_cat(os, s);
  491. }
  492. d_char *html_user_footer(snac *snac, d_char *s)
  493. {
  494. xs *s1 = xs_fmt(
  495. "<div class=\"snac-footer\">\n"
  496. "<a href=\"%s\">%s</a> - "
  497. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  498. srv_baseurl,
  499. L("about this site")
  500. );
  501. return xs_str_cat(s, s1);
  502. }
  503. d_char *html_timeline(snac *snac, char *list, int local)
  504. /* returns the HTML for the timeline */
  505. {
  506. d_char *s = xs_str_new(NULL);
  507. xs_set *seen = xs_set_new(4096);
  508. char *v;
  509. double t = ftime();
  510. s = html_user_header(snac, s, local);
  511. if (!local)
  512. s = html_top_controls(snac, s);
  513. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  514. while (xs_list_iter(&list, &v)) {
  515. xs *msg = timeline_get(snac, v);
  516. s = html_entry(snac, s, msg, seen, local, 0);
  517. }
  518. s = xs_str_cat(s, "</div>\n");
  519. s = html_user_footer(snac, s);
  520. {
  521. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  522. s = xs_str_cat(s, s1);
  523. }
  524. s = xs_str_cat(s, "</body>\n</html>\n");
  525. xs_set_free(seen);
  526. return s;
  527. }
  528. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  529. {
  530. int status = 404;
  531. snac snac;
  532. char *uid, *p_path;
  533. xs *l = xs_split_n(q_path, "/", 2);
  534. uid = xs_list_get(l, 1);
  535. if (!uid || !user_open(&snac, uid)) {
  536. /* invalid user */
  537. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  538. return 404;
  539. }
  540. p_path = xs_list_get(l, 2);
  541. if (p_path == NULL) {
  542. /* public timeline */
  543. xs *list = local_list(&snac, 0xfffffff);
  544. *body = html_timeline(&snac, list, 1);
  545. *b_size = strlen(*body);
  546. status = 200;
  547. }
  548. else
  549. if (strcmp(p_path, "admin") == 0) {
  550. /* private timeline */
  551. if (!login(&snac, req))
  552. status = 401;
  553. else {
  554. if (history_mtime(&snac, "_timeline.html") > timeline_mtime(&snac)) {
  555. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  556. *body = history_get(&snac, "_timeline.html");
  557. *b_size = strlen(*body);
  558. status = 200;
  559. }
  560. else {
  561. snac_debug(&snac, 1, xs_fmt("building timeline"));
  562. xs *list = timeline_list(&snac, 0xfffffff);
  563. *body = html_timeline(&snac, list, 0);
  564. *b_size = strlen(*body);
  565. status = 200;
  566. history_add(&snac, "_timeline.html", *body, *b_size);
  567. }
  568. }
  569. }
  570. else
  571. if (xs_startswith(p_path, "p/")) {
  572. /* a timeline with just one entry */
  573. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  574. xs *fn = _timeline_find_fn(&snac, id);
  575. if (fn != NULL) {
  576. xs *list = xs_list_new();
  577. list = xs_list_append(list, fn);
  578. *body = html_timeline(&snac, list, 1);
  579. *b_size = strlen(*body);
  580. status = 200;
  581. }
  582. }
  583. else
  584. if (xs_startswith(p_path, "s/")) {
  585. /* a static file */
  586. }
  587. else
  588. if (xs_startswith(p_path, "h/")) {
  589. /* an entry from the history */
  590. }
  591. else
  592. status = 404;
  593. user_free(&snac);
  594. if (valid_status(status)) {
  595. *ctype = "text/html; charset=utf-8";
  596. }
  597. return status;
  598. }
  599. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  600. char **body, int *b_size, char **ctype)
  601. {
  602. int status = 0;
  603. snac snac;
  604. char *uid, *p_path;
  605. char *p_vars;
  606. xs *l = xs_split_n(q_path, "/", 2);
  607. uid = xs_list_get(l, 1);
  608. if (!uid || !user_open(&snac, uid)) {
  609. /* invalid user */
  610. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  611. return 404;
  612. }
  613. p_path = xs_list_get(l, 2);
  614. /* all posts must be authenticated */
  615. if (!login(&snac, req))
  616. return 401;
  617. p_vars = xs_dict_get(req, "p_vars");
  618. {
  619. xs *j1 = xs_json_dumps_pp(req, 4);
  620. printf("%s\n", j1);
  621. printf("[%s]\n", p_path);
  622. }
  623. if (p_path && strcmp(p_path, "admin/note") == 0) {
  624. /* post note */
  625. char *content = xs_dict_get(p_vars, "content");
  626. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  627. if (content != NULL) {
  628. xs *msg = NULL;
  629. xs *c_msg = NULL;
  630. msg = msg_note(&snac, content, NULL, in_reply_to);
  631. c_msg = msg_create(&snac, msg);
  632. post(&snac, c_msg);
  633. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  634. }
  635. status = 303;
  636. }
  637. else
  638. if (p_path && strcmp(p_path, "admin/action") == 0) {
  639. /* action on an entry */
  640. char *id = xs_dict_get(p_vars, "id");
  641. char *actor = xs_dict_get(p_vars, "actor");
  642. char *action = xs_dict_get(p_vars, "action");
  643. if (action == NULL)
  644. return 404;
  645. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  646. status = 303;
  647. if (strcmp(action, L("Like")) == 0) {
  648. xs *msg = msg_admiration(&snac, id, "Like");
  649. post(&snac, msg);
  650. timeline_admire(&snac, id, snac.actor, 1);
  651. }
  652. else
  653. if (strcmp(action, L("Boost")) == 0) {
  654. xs *msg = msg_admiration(&snac, id, "Announce");
  655. post(&snac, msg);
  656. timeline_admire(&snac, id, snac.actor, 0);
  657. }
  658. else
  659. if (strcmp(action, L("MUTE")) == 0) {
  660. mute(&snac, actor);
  661. }
  662. else
  663. if (strcmp(action, L("Follow")) == 0) {
  664. char *msg = msg_follow(&snac, actor);
  665. /* reload the actor from the message, in may be different */
  666. actor = xs_dict_get(msg, "object");
  667. following_add(&snac, actor, msg);
  668. enqueue_output(&snac, msg, actor, 0);
  669. }
  670. else
  671. if (strcmp(action, L("Unfollow")) == 0) {
  672. }
  673. else
  674. if (strcmp(action, L("Delete")) == 0) {
  675. }
  676. else
  677. status = 404;
  678. /* delete the cached timeline */
  679. if (status == 303)
  680. history_del(&snac, "_timeline.html");
  681. }
  682. else
  683. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  684. /* change of user data */
  685. }
  686. if (status == 303) {
  687. *body = xs_fmt("%s/admin", snac.actor);
  688. *b_size = strlen(*body);
  689. }
  690. return status;
  691. }