html.c 28 KB

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