html.c 27 KB

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