html.c 32 KB

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