html.c 30 KB

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