html.c 37 KB

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