html.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  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, "delete", L("Delete"));
  341. s = xs_str_cat(s, "</form>\n");
  342. {
  343. /* the post textarea */
  344. xs *ct = build_mentions(snac, msg);
  345. xs *s1 = xs_fmt(
  346. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  347. "<form method=\"post\" action=\"%s/admin/note\" "
  348. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  349. "<textarea class=\"snac-textarea\" name=\"content\" "
  350. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  351. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  352. "<p><input type=\"file\" name=\"attach\">\n"
  353. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  354. "</form><p></div>\n",
  355. md5,
  356. snac->actor, md5,
  357. ct,
  358. id,
  359. L("Post")
  360. );
  361. s = xs_str_cat(s, s1);
  362. }
  363. s = xs_str_cat(s, "</div>\n");
  364. return xs_str_cat(os, s);
  365. }
  366. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level, int *num)
  367. {
  368. char *id = xs_dict_get(msg, "id");
  369. char *type = xs_dict_get(msg, "type");
  370. char *meta = xs_dict_get(msg, "_snac");
  371. char *actor;
  372. int sensitive = 0;
  373. char *v;
  374. /* do not show non-public messages in the public timeline */
  375. if (local && !is_msg_public(snac, msg))
  376. return os;
  377. /* return if already seen */
  378. if (xs_set_add(seen, id) == 0)
  379. return os;
  380. xs *s = xs_str_new(NULL);
  381. if (level == 0) {
  382. xs *s1 = xs_fmt("<a name=\"%d_entry\"></a>\n", *num);
  383. *num = *num + 1;
  384. s = xs_str_cat(s, s1);
  385. }
  386. if (strcmp(type, "Follow") == 0) {
  387. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  388. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  389. s = xs_str_cat(s, s1);
  390. s = html_msg_icon(snac, s, msg);
  391. s = xs_str_cat(s, "</div>\n");
  392. return xs_str_cat(os, s);
  393. }
  394. /* bring the main actor */
  395. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  396. return os;
  397. /* ignore muted morons immediately */
  398. if (is_muted(snac, actor))
  399. return os;
  400. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  401. return os;
  402. /* if this is our post, add the score */
  403. if (xs_startswith(id, snac->actor)) {
  404. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  405. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  406. /* alternate emojis: %d &#128077; %d &#128257; */
  407. xs *s1 = xs_fmt(
  408. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  409. likes, boosts);
  410. s = xs_str_cat(s, s1);
  411. }
  412. if (level == 0) {
  413. char *p;
  414. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  415. /* print the origin of the post, if any */
  416. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  417. xs *actor_r = NULL;
  418. if (valid_status(actor_get(snac, p, &actor_r))) {
  419. char *name;
  420. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  421. name = xs_dict_get(actor_r, "preferredUsername");
  422. xs *s1 = xs_fmt(
  423. "<div class=\"snac-origin\">"
  424. "<a href=\"%s\">%s</a> %s</div>\n",
  425. xs_dict_get(actor_r, "id"),
  426. name,
  427. L("boosted")
  428. );
  429. s = xs_str_cat(s, s1);
  430. }
  431. }
  432. else
  433. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  434. /* this may happen if any of the autor or the parent aren't here */
  435. xs *s1 = xs_fmt(
  436. "<div class=\"snac-origin\">%s "
  437. "<a href=\"%s\">»</a></div>\n",
  438. L("in reply to"), p
  439. );
  440. s = xs_str_cat(s, s1);
  441. }
  442. else
  443. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  444. xs_list_in(p, snac->actor) != -1) {
  445. /* we boosted this */
  446. xs *s1 = xs_fmt(
  447. "<div class=\"snac-origin\">"
  448. "<a href=\"%s\">%s</a> %s</a></div>",
  449. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  450. );
  451. s = xs_str_cat(s, s1);
  452. }
  453. else
  454. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  455. xs_list_in(p, snac->actor) != -1) {
  456. /* we liked this */
  457. xs *s1 = xs_fmt(
  458. "<div class=\"snac-origin\">"
  459. "<a href=\"%s\">%s</a> %s</a></div>",
  460. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  461. );
  462. s = xs_str_cat(s, s1);
  463. }
  464. }
  465. else
  466. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  467. s = html_msg_icon(snac, s, msg);
  468. /* add the content */
  469. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  470. /* is it sensitive? */
  471. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  472. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  473. v = "...";
  474. xs *s1 = xs_fmt("<details><summary>%s [%s]</summary>\n", v, L("SENSITIVE CONTENT"));
  475. s = xs_str_cat(s, s1);
  476. sensitive = 1;
  477. }
  478. {
  479. xs *c = xs_dup(xs_dict_get(msg, "content"));
  480. char *p, *v;
  481. /* do some tweaks to the content */
  482. c = xs_replace_i(c, "\r", "");
  483. while (xs_endswith(c, "<br><br>"))
  484. c = xs_crop(c, 0, -4);
  485. c = xs_replace_i(c, "<br><br>", "<p>");
  486. if (!xs_startswith(c, "<p>")) {
  487. xs *s1 = c;
  488. c = xs_fmt("<p>%s</p>", s1);
  489. }
  490. /* replace the :shortnames: */
  491. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  492. /* iterate the tags */
  493. while (xs_list_iter(&p, &v)) {
  494. char *t = xs_dict_get(v, "type");
  495. if (t && strcmp(t, "Emoji") == 0) {
  496. char *n = xs_dict_get(v, "name");
  497. char *i = xs_dict_get(v, "icon");
  498. if (n && i) {
  499. char *u = xs_dict_get(i, "url");
  500. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\"/>", u);
  501. c = xs_replace_i(c, n, img);
  502. }
  503. }
  504. }
  505. }
  506. xs *sc = sanitize(c);
  507. s = xs_str_cat(s, sc);
  508. }
  509. s = xs_str_cat(s, "\n");
  510. /* add the attachments */
  511. char *attach;
  512. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  513. char *v;
  514. while (xs_list_iter(&attach, &v)) {
  515. char *t = xs_dict_get(v, "mediaType");
  516. if (xs_is_null(t))
  517. continue;
  518. if (xs_startswith(t, "image/")) {
  519. char *url = xs_dict_get(v, "url");
  520. char *name = xs_dict_get(v, "name");
  521. if (url != NULL) {
  522. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  523. url, xs_is_null(name) ? "" : name);
  524. s = xs_str_cat(s, s1);
  525. }
  526. }
  527. else
  528. if (xs_startswith(t, "video/")) {
  529. char *url = xs_dict_get(v, "url");
  530. if (url != NULL) {
  531. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  532. s = xs_str_cat(s, s1);
  533. }
  534. }
  535. }
  536. }
  537. if (sensitive)
  538. s = xs_str_cat(s, "</details><p>\n");
  539. s = xs_str_cat(s, "</div>\n");
  540. /** controls **/
  541. if (!local)
  542. s = html_entry_controls(snac, s, msg, *num);
  543. /** children **/
  544. char *children = xs_dict_get(meta, "children");
  545. int left = xs_list_len(children);
  546. if (left) {
  547. char *id;
  548. if (level < 4)
  549. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  550. else
  551. s = xs_str_cat(s, "<div>\n");
  552. if (left > 3)
  553. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  554. while (xs_list_iter(&children, &id)) {
  555. xs *chd = timeline_find(snac, id);
  556. if (left == 3)
  557. s = xs_str_cat(s, "</details>\n");
  558. if (chd != NULL)
  559. s = html_entry(snac, s, chd, seen, local, level + 1, num);
  560. else
  561. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  562. left--;
  563. }
  564. s = xs_str_cat(s, "</div>\n");
  565. }
  566. s = xs_str_cat(s, "</div>\n");
  567. return xs_str_cat(os, s);
  568. }
  569. d_char *html_user_footer(snac *snac, d_char *s)
  570. {
  571. xs *s1 = xs_fmt(
  572. "<div class=\"snac-footer\">\n"
  573. "<a href=\"%s\">%s</a> - "
  574. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  575. srv_baseurl,
  576. L("about this site")
  577. );
  578. return xs_str_cat(s, s1);
  579. }
  580. d_char *html_timeline(snac *snac, char *list, int local)
  581. /* returns the HTML for the timeline */
  582. {
  583. d_char *s = xs_str_new(NULL);
  584. xs_set *seen = xs_set_new(4096);
  585. char *v;
  586. double t = ftime();
  587. int num = 0;
  588. s = html_user_header(snac, s, local);
  589. if (!local)
  590. s = html_top_controls(snac, s);
  591. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  592. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  593. while (xs_list_iter(&list, &v)) {
  594. xs *msg = timeline_get(snac, v);
  595. s = html_entry(snac, s, msg, seen, local, 0, &num);
  596. }
  597. s = xs_str_cat(s, "</div>\n");
  598. if (local) {
  599. xs *s1 = xs_fmt(
  600. "<div class=\"snac-history\">\n"
  601. "<p class=\"snac-history-title\">%s</p><ul>\n",
  602. L("History")
  603. );
  604. s = xs_str_cat(s, s1);
  605. xs *list = history_list(snac);
  606. char *p, *v;
  607. p = list;
  608. while (xs_list_iter(&p, &v)) {
  609. xs *fn = xs_replace(v, ".html", "");
  610. xs *s1 = xs_fmt(
  611. "<li><a href=\"%s/h/%s\">%s</li>\n",
  612. snac->actor, v, fn);
  613. s = xs_str_cat(s, s1);
  614. }
  615. s = xs_str_cat(s, "</ul></div>\n");
  616. }
  617. s = html_user_footer(snac, s);
  618. {
  619. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  620. s = xs_str_cat(s, s1);
  621. }
  622. s = xs_str_cat(s, "</body>\n</html>\n");
  623. xs_set_free(seen);
  624. return s;
  625. }
  626. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  627. {
  628. xs *s = xs_str_new(NULL);
  629. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  630. char *p, *v;
  631. s = xs_str_cat(s, h);
  632. p = list;
  633. while (xs_list_iter(&p, &v)) {
  634. char *actor_id = xs_dict_get(v, "actor");
  635. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  636. xs *actor = NULL;
  637. if (valid_status(actor_get(snac, actor_id, &actor))) {
  638. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  639. s = html_actor_icon(snac, s, actor, NULL, NULL, 0);
  640. /* content (user bio) */
  641. char *c = xs_dict_get(actor, "summary");
  642. if (!xs_is_null(c)) {
  643. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  644. xs *sc = sanitize(c);
  645. if (xs_startswith(sc, "<p>"))
  646. s = xs_str_cat(s, sc);
  647. else {
  648. xs *s1 = xs_fmt("<p>%s</p>", sc);
  649. s = xs_str_cat(s, s1);
  650. }
  651. s = xs_str_cat(s, "</div>\n");
  652. }
  653. /* buttons */
  654. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  655. xs *s1 = xs_fmt(
  656. "<form method=\"post\" action=\"%s/admin/action\">\n"
  657. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  658. "<input type=\"button\" name=\"action\" "
  659. "value=\"%s\" onclick=\""
  660. "x = document.getElementById('%s_%s_dm'); "
  661. "if (x.style.display == 'block') "
  662. " x.style.display = 'none'; else "
  663. " x.style.display = 'block';"
  664. "\">\n",
  665. snac->actor, actor_id,
  666. L("DM"),
  667. md5, t
  668. );
  669. s = xs_str_cat(s, s1);
  670. if (following_check(snac, actor_id))
  671. s = html_button(s, "unfollow", L("Unfollow"));
  672. else
  673. s = html_button(s, "follow", L("Follow"));
  674. if (is_muted(snac, actor_id))
  675. s = html_button(s, "unmute", L("Unmute"));
  676. else
  677. s = html_button(s, "mute", L("MUTE"));
  678. s = xs_str_cat(s, "</form>\n");
  679. /* the post textarea */
  680. xs *s2 = xs_fmt(
  681. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_%s_dm\">\n"
  682. "<form method=\"post\" action=\"%s/admin/note\" "
  683. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  684. "<textarea class=\"snac-textarea\" name=\"content\" "
  685. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  686. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  687. "<p><input type=\"file\" name=\"attach\">\n"
  688. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  689. "</form><p></div>\n",
  690. md5, t,
  691. snac->actor, md5,
  692. actor_id,
  693. L("Post")
  694. );
  695. s = xs_str_cat(s, s2);
  696. s = xs_str_cat(s, "</div>\n");
  697. s = xs_str_cat(s, "</div>\n");
  698. }
  699. }
  700. return xs_str_cat(os, s);
  701. }
  702. d_char *html_people(snac *snac)
  703. {
  704. d_char *s = xs_str_new(NULL);
  705. xs *wing = following_list(snac);
  706. xs *wers = follower_list(snac);
  707. s = html_user_header(snac, s, 0);
  708. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  709. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  710. s = html_user_footer(snac, s);
  711. s = xs_str_cat(s, "</body>\n</html>\n");
  712. return s;
  713. }
  714. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  715. {
  716. int status = 404;
  717. snac snac;
  718. char *uid, *p_path;
  719. int cache = 1;
  720. char *v;
  721. xs *l = xs_split_n(q_path, "/", 2);
  722. uid = xs_list_get(l, 1);
  723. if (!uid || !user_open(&snac, uid)) {
  724. /* invalid user */
  725. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  726. return 404;
  727. }
  728. /* check if server config variable 'disable_cache' is set */
  729. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  730. cache = 0;
  731. p_path = xs_list_get(l, 2);
  732. if (p_path == NULL) {
  733. /* public timeline */
  734. xs *h = xs_str_localtime(0, "%Y-%m.html");
  735. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  736. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  737. *body = history_get(&snac, h);
  738. *b_size = strlen(*body);
  739. status = 200;
  740. }
  741. else {
  742. xs *list = local_list(&snac, 0xfffffff);
  743. *body = html_timeline(&snac, list, 1);
  744. *b_size = strlen(*body);
  745. status = 200;
  746. history_add(&snac, h, *body, *b_size);
  747. }
  748. }
  749. else
  750. if (strcmp(p_path, "admin") == 0) {
  751. /* private timeline */
  752. if (!login(&snac, req))
  753. status = 401;
  754. else {
  755. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  756. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  757. *body = history_get(&snac, "timeline.html_");
  758. *b_size = strlen(*body);
  759. status = 200;
  760. }
  761. else {
  762. snac_debug(&snac, 1, xs_fmt("building timeline"));
  763. xs *list = timeline_list(&snac, 0xfffffff);
  764. *body = html_timeline(&snac, list, 0);
  765. *b_size = strlen(*body);
  766. status = 200;
  767. history_add(&snac, "timeline.html_", *body, *b_size);
  768. }
  769. }
  770. }
  771. else
  772. if (strcmp(p_path, "people") == 0) {
  773. /* the list of people */
  774. if (!login(&snac, req))
  775. status = 401;
  776. else {
  777. *body = html_people(&snac);
  778. *b_size = strlen(*body);
  779. status = 200;
  780. }
  781. }
  782. else
  783. if (xs_startswith(p_path, "p/")) {
  784. /* a timeline with just one entry */
  785. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  786. xs *fn = _timeline_find_fn(&snac, id);
  787. if (fn != NULL) {
  788. xs *list = xs_list_new();
  789. list = xs_list_append(list, fn);
  790. *body = html_timeline(&snac, list, 1);
  791. *b_size = strlen(*body);
  792. status = 200;
  793. }
  794. }
  795. else
  796. if (xs_startswith(p_path, "s/")) {
  797. /* a static file */
  798. xs *l = xs_split(p_path, "/");
  799. char *id = xs_list_get(l, 1);
  800. int sz;
  801. if (valid_status(static_get(&snac, id, body, &sz))) {
  802. *b_size = sz;
  803. *ctype = xs_mime_by_ext(id);
  804. status = 200;
  805. }
  806. }
  807. else
  808. if (xs_startswith(p_path, "h/")) {
  809. /* an entry from the history */
  810. xs *l = xs_split(p_path, "/");
  811. char *id = xs_list_get(l, 1);
  812. if ((*body = history_get(&snac, id)) != NULL) {
  813. *b_size = strlen(*body);
  814. status = 200;
  815. }
  816. }
  817. else
  818. status = 404;
  819. user_free(&snac);
  820. if (valid_status(status) && *ctype == NULL) {
  821. *ctype = "text/html; charset=utf-8";
  822. }
  823. return status;
  824. }
  825. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  826. char **body, int *b_size, char **ctype)
  827. {
  828. int status = 0;
  829. snac snac;
  830. char *uid, *p_path;
  831. char *p_vars;
  832. xs *l = xs_split_n(q_path, "/", 2);
  833. uid = xs_list_get(l, 1);
  834. if (!uid || !user_open(&snac, uid)) {
  835. /* invalid user */
  836. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  837. return 404;
  838. }
  839. p_path = xs_list_get(l, 2);
  840. /* all posts must be authenticated */
  841. if (!login(&snac, req)) {
  842. user_free(&snac);
  843. return 401;
  844. }
  845. p_vars = xs_dict_get(req, "p_vars");
  846. #if 0
  847. {
  848. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  849. printf("%s\n", j1);
  850. }
  851. #endif
  852. if (p_path && strcmp(p_path, "admin/note") == 0) {
  853. /* post note */
  854. char *content = xs_dict_get(p_vars, "content");
  855. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  856. char *attach_url = xs_dict_get(p_vars, "attach_url");
  857. char *attach_file = xs_dict_get(p_vars, "attach");
  858. char *to = xs_dict_get(p_vars, "to");
  859. xs *attach_list = xs_list_new();
  860. /* is attach_url set? */
  861. if (!xs_is_null(attach_url) && *attach_url != '\0')
  862. attach_list = xs_list_append(attach_list, attach_url);
  863. /* is attach_file set? */
  864. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  865. char *fn = xs_list_get(attach_file, 0);
  866. if (*fn != '\0') {
  867. char *ext = strrchr(fn, '.');
  868. xs *ntid = tid(0);
  869. xs *id = xs_fmt("%s%s", ntid, ext);
  870. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  871. int fo = xs_number_get(xs_list_get(attach_file, 1));
  872. int fs = xs_number_get(xs_list_get(attach_file, 2));
  873. /* store */
  874. static_put(&snac, id, payload + fo, fs);
  875. attach_list = xs_list_append(attach_list, url);
  876. }
  877. }
  878. if (content != NULL) {
  879. xs *msg = NULL;
  880. xs *c_msg = NULL;
  881. xs *content_2 = xs_replace(content, "\r", "");
  882. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list);
  883. c_msg = msg_create(&snac, msg);
  884. post(&snac, c_msg);
  885. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  886. }
  887. status = 303;
  888. }
  889. else
  890. if (p_path && strcmp(p_path, "admin/action") == 0) {
  891. /* action on an entry */
  892. char *id = xs_dict_get(p_vars, "id");
  893. char *actor = xs_dict_get(p_vars, "actor");
  894. char *action = xs_dict_get(p_vars, "action");
  895. if (action == NULL)
  896. return 404;
  897. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  898. status = 303;
  899. if (strcmp(action, L("Like")) == 0) {
  900. xs *msg = msg_admiration(&snac, id, "Like");
  901. post(&snac, msg);
  902. timeline_admire(&snac, id, snac.actor, 1);
  903. }
  904. else
  905. if (strcmp(action, L("Boost")) == 0) {
  906. xs *msg = msg_admiration(&snac, id, "Announce");
  907. post(&snac, msg);
  908. timeline_admire(&snac, id, snac.actor, 0);
  909. }
  910. else
  911. if (strcmp(action, L("MUTE")) == 0) {
  912. mute(&snac, actor);
  913. }
  914. else
  915. if (strcmp(action, L("Unmute")) == 0) {
  916. unmute(&snac, actor);
  917. }
  918. else
  919. if (strcmp(action, L("Follow")) == 0) {
  920. xs *msg = msg_follow(&snac, actor);
  921. /* reload the actor from the message, in may be different */
  922. actor = xs_dict_get(msg, "object");
  923. following_add(&snac, actor, msg);
  924. enqueue_output(&snac, msg, actor, 0);
  925. }
  926. else
  927. if (strcmp(action, L("Unfollow")) == 0) {
  928. /* get the following object */
  929. xs *object = NULL;
  930. if (valid_status(following_get(&snac, actor, &object))) {
  931. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  932. following_del(&snac, actor);
  933. enqueue_output(&snac, msg, actor, 0);
  934. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  935. }
  936. else
  937. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  938. }
  939. else
  940. if (strcmp(action, L("Delete")) == 0) {
  941. /* delete an entry */
  942. if (xs_startswith(id, snac.actor)) {
  943. /* it's a post by us: generate a delete */
  944. xs *msg = msg_delete(&snac, id);
  945. post(&snac, msg);
  946. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  947. }
  948. timeline_del(&snac, id);
  949. snac_log(&snac, xs_fmt("deleted entry %s", id));
  950. }
  951. else
  952. status = 404;
  953. /* delete the cached timeline */
  954. if (status == 303)
  955. history_del(&snac, "timeline.html_");
  956. }
  957. else
  958. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  959. /* change of user data */
  960. char *v;
  961. char *p1, *p2;
  962. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  963. snac.config = xs_dict_set(snac.config, "name", v);
  964. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  965. snac.config = xs_dict_set(snac.config, "avatar", v);
  966. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  967. snac.config = xs_dict_set(snac.config, "bio", v);
  968. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  969. snac.config = xs_dict_set(snac.config, "email", v);
  970. /* password change? */
  971. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  972. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  973. *p1 && strcmp(p1, p2) == 0) {
  974. xs *pw = hash_password(snac.uid, p1, NULL);
  975. snac.config = xs_dict_set(snac.config, "passwd", pw);
  976. }
  977. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  978. xs *bfn = xs_fmt("%s.bak", fn);
  979. FILE *f;
  980. rename(fn, bfn);
  981. if ((f = fopen(fn, "w")) != NULL) {
  982. xs *j = xs_json_dumps_pp(snac.config, 4);
  983. fwrite(j, strlen(j), 1, f);
  984. fclose(f);
  985. }
  986. else
  987. rename(bfn, fn);
  988. history_del(&snac, "timeline.html_");
  989. xs *a_msg = msg_actor(&snac);
  990. xs *u_msg = msg_update(&snac, a_msg);
  991. post(&snac, u_msg);
  992. status = 303;
  993. }
  994. if (status == 303) {
  995. char *redir = xs_dict_get(p_vars, "redir");
  996. if (xs_is_null(redir))
  997. redir = "snac-posts";
  998. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  999. *b_size = strlen(*body);
  1000. }
  1001. user_free(&snac);
  1002. return status;
  1003. }