html.c 37 KB

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