html.c 31 KB

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