html.c 36 KB

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