html.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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 "snac.h"
  12. d_char *not_really_markdown(char *content, d_char **f_content)
  13. /* formats a content using some Markdown rules */
  14. {
  15. d_char *s = NULL;
  16. int in_pre = 0;
  17. int in_blq = 0;
  18. xs *list;
  19. char *p, *v;
  20. xs *wrk = xs_str_new(NULL);
  21. {
  22. /* split by special markup */
  23. xs *sm = xs_regex_split(content,
  24. "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
  25. int n = 0;
  26. p = sm;
  27. while (xs_list_iter(&p, &v)) {
  28. if ((n & 0x1)) {
  29. /* markup */
  30. if (xs_startswith(v, "`")) {
  31. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  32. xs *s2 = xs_fmt("<code>%s</code>", s1);
  33. wrk = xs_str_cat(wrk, s2);
  34. }
  35. else
  36. if (xs_startswith(v, "**")) {
  37. xs *s1 = xs_crop(xs_dup(v), 2, -2);
  38. xs *s2 = xs_fmt("<b>%s</b>", s1);
  39. wrk = xs_str_cat(wrk, s2);
  40. }
  41. else
  42. if (xs_startswith(v, "*")) {
  43. xs *s1 = xs_crop(xs_dup(v), 1, -1);
  44. xs *s2 = xs_fmt("<i>%s</i>", s1);
  45. wrk = xs_str_cat(wrk, s2);
  46. }
  47. else
  48. if (xs_startswith(v, "http")) {
  49. xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
  50. wrk = xs_str_cat(wrk, s1);
  51. }
  52. else
  53. /* what the hell is this */
  54. wrk = xs_str_cat(wrk, v);
  55. }
  56. else
  57. /* surrounded text, copy directly */
  58. wrk = xs_str_cat(wrk, v);
  59. n++;
  60. }
  61. }
  62. /* now work by lines */
  63. p = list = xs_split(wrk, "\n");
  64. s = xs_str_new(NULL);
  65. while (xs_list_iter(&p, &v)) {
  66. xs *ss = xs_strip(xs_dup(v));
  67. if (xs_startswith(ss, "```")) {
  68. if (!in_pre)
  69. s = xs_str_cat(s, "<pre>");
  70. else
  71. s = xs_str_cat(s, "</pre>");
  72. in_pre = !in_pre;
  73. continue;
  74. }
  75. if (xs_startswith(ss, ">")) {
  76. /* delete the > and subsequent spaces */
  77. ss = xs_strip(xs_crop(ss, 1, 0));
  78. if (!in_blq) {
  79. s = xs_str_cat(s, "<blockquote>");
  80. in_blq = 1;
  81. }
  82. s = xs_str_cat(s, ss);
  83. s = xs_str_cat(s, "<br>");
  84. continue;
  85. }
  86. if (in_blq) {
  87. s = xs_str_cat(s, "</blockquote>");
  88. in_blq = 0;
  89. }
  90. s = xs_str_cat(s, ss);
  91. s = xs_str_cat(s, "<br>");
  92. }
  93. if (in_blq)
  94. s = xs_str_cat(s, "</blockquote>");
  95. if (in_pre)
  96. s = xs_str_cat(s, "</pre>");
  97. /* some beauty fixes */
  98. s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
  99. *f_content = s;
  100. return *f_content;
  101. }
  102. int login(snac *snac, char *headers)
  103. /* tries a login */
  104. {
  105. int logged_in = 0;
  106. char *auth = xs_dict_get(headers, "authorization");
  107. if (auth && xs_startswith(auth, "Basic ")) {
  108. int sz;
  109. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  110. xs *s2 = xs_base64_dec(s1, &sz);
  111. /* copy to asciiz it */
  112. xs *s3 = calloc(sz + 1, 1);
  113. memcpy(s3, s2, sz);
  114. xs *l1 = xs_split_n(s3, ":", 1);
  115. if (xs_list_len(l1) == 2) {
  116. logged_in = check_password(
  117. xs_list_get(l1, 0), xs_list_get(l1, 1),
  118. xs_dict_get(snac->config, "passwd"));
  119. }
  120. }
  121. return logged_in;
  122. }
  123. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  124. {
  125. char *actor_id;
  126. xs *actor = NULL;
  127. xs *s = xs_str_new(NULL);
  128. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  129. actor_id = xs_dict_get(msg, "actor");
  130. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  131. xs *name = NULL;
  132. xs *avatar = NULL;
  133. char *v;
  134. /* get the name */
  135. if ((v = xs_dict_get(actor, "name")) == NULL || *v == '\0') {
  136. if ((v = xs_dict_get(actor, "preferredUsername")) == NULL) {
  137. v = "user";
  138. }
  139. }
  140. name = xs_dup(v);
  141. /* get the avatar */
  142. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  143. (v = xs_dict_get(v, "url")) != NULL) {
  144. avatar = xs_dup(v);
  145. }
  146. if (avatar == NULL)
  147. avatar = xs_fmt("data:image/png;base64, %s", susie);
  148. {
  149. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\"/>\n", avatar);
  150. s = xs_str_cat(s, s1);
  151. }
  152. {
  153. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  154. actor_id, name);
  155. s = xs_str_cat(s, s1);
  156. }
  157. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0) {
  158. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", xs_dict_get(msg, "id"));
  159. s = xs_str_cat(s, s1);
  160. }
  161. if (!is_msg_public(snac, msg))
  162. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  163. if ((v = xs_dict_get(msg, "published")) == NULL) {
  164. s = xs_str_cat(s, "<br>\n<time>&nbsp;</time>\n");
  165. }
  166. else {
  167. xs *s1 = xs_fmt(
  168. "<br>\n<time class=\"dt-published snac-pubdate\">%s</time>\n", v);
  169. s = xs_str_cat(s, s1);
  170. }
  171. }
  172. return xs_str_cat(os, s);
  173. }
  174. d_char *html_user_header(snac *snac, d_char *s, int local)
  175. /* creates the HTML header */
  176. {
  177. char *p, *v;
  178. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  179. s = xs_str_cat(s, "<meta name=\"viewport\" "
  180. "content=\"width=device-width, initial-scale=1\"/>\n");
  181. s = xs_str_cat(s, "<meta name=\"generator\" "
  182. "content=\"" USER_AGENT "\"/>\n");
  183. /* add server CSS */
  184. p = xs_dict_get(srv_config, "cssurls");
  185. while (xs_list_iter(&p, &v)) {
  186. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  187. s = xs_str_cat(s, s1);
  188. }
  189. /* add the user CSS */
  190. {
  191. xs *css = NULL;
  192. int size;
  193. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  194. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  195. s = xs_str_cat(s, s1);
  196. }
  197. }
  198. {
  199. xs *s1 = xs_fmt("<title>%s</title>\n", xs_dict_get(snac->config, "name"));
  200. s = xs_str_cat(s, s1);
  201. }
  202. s = xs_str_cat(s, "</head>\n<body>\n");
  203. /* top nav */
  204. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  205. {
  206. xs *s1;
  207. if (local)
  208. s1 = xs_fmt("<a href=\"%s/admin\">%s</a></nav>\n", snac->actor, L("admin"));
  209. else
  210. s1 = xs_fmt("<a href=\"%s\">%s</a></nav>\n", snac->actor, L("public"));
  211. s = xs_str_cat(s, s1);
  212. }
  213. /* user info */
  214. {
  215. xs *bio = NULL;
  216. char *_tmpl =
  217. "<div class=\"h-card snac-top-user\">\n"
  218. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  219. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  220. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  221. "</div>\n";
  222. not_really_markdown(xs_dict_get(snac->config, "bio"), &bio);
  223. xs *s1 = xs_fmt(_tmpl,
  224. xs_dict_get(snac->config, "name"),
  225. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  226. bio
  227. );
  228. s = xs_str_cat(s, s1);
  229. }
  230. return s;
  231. }
  232. d_char *html_top_controls(snac *snac, d_char *s)
  233. /* generates the top controls */
  234. {
  235. char *_tmpl =
  236. "<div class=\"snac-top-controls\">\n"
  237. "<div class=\"snac-note\">\n"
  238. "<form method=\"post\" action=\"%s/admin/note\">\n"
  239. "<textarea class=\"snac-textarea\" name=\"content\" "
  240. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  241. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  242. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  243. "</form><p>\n"
  244. "</div>\n"
  245. "<div class=\"snac-top-controls-more\">\n"
  246. "<details><summary>%s</summary>\n"
  247. "<form method=\"post\" action=\"%s/admin/action\">\n"
  248. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  249. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  250. "</form></p>\n"
  251. "<form method=\"post\" action=\"%s/admin/action\">\n"
  252. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  253. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  254. "</form></p>\n"
  255. "<details><summary>%s</summary>\n"
  256. "<div class=\"snac-user-setup\">\n"
  257. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  258. "<p>%s:<br>\n"
  259. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  260. "<p>%s:<br>\n"
  261. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  262. "<p>%s:<br>\n"
  263. "<textarea name=\"bio\" cols=60 rows=4>%s</textarea></p>\n"
  264. "<p>%s:<br>\n"
  265. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  266. "<p>%s:<br>\n"
  267. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  268. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  269. "</form>\n"
  270. "</div>\n"
  271. "</details>\n"
  272. "</details>\n"
  273. "</div>\n"
  274. "</div>\n";
  275. xs *s1 = xs_fmt(_tmpl,
  276. snac->actor,
  277. L("Post"),
  278. L("More options..."),
  279. snac->actor,
  280. L("Follow"), L("(by URL or user@host)"),
  281. snac->actor,
  282. L("Boost"), L("(by URL)"),
  283. L("User setup..."),
  284. snac->actor,
  285. L("User name"),
  286. xs_dict_get(snac->config, "name"),
  287. L("Avatar URL"),
  288. xs_dict_get(snac->config, "avatar"),
  289. L("Bio"),
  290. xs_dict_get(snac->config, "bio"),
  291. L("Password (only to change it)"),
  292. L("Repeat Password"),
  293. L("Update user info")
  294. );
  295. s = xs_str_cat(s, s1);
  296. return s;
  297. }
  298. d_char *html_button(d_char *s, char *clss, char *label)
  299. {
  300. xs *s1 = xs_fmt(
  301. "<input type=\"submit\" name=\"action\" "
  302. "class=\"snac-btn-%s\" value=\"%s\">\n",
  303. clss, label);
  304. return xs_str_cat(s, s1);
  305. }
  306. d_char *build_mentions(snac *snac, char *msg)
  307. /* returns a string with the mentions in msg */
  308. {
  309. d_char *s = xs_str_new(NULL);
  310. char *list = xs_dict_get(msg, "tag");
  311. char *v;
  312. while (xs_list_iter(&list, &v)) {
  313. char *type = xs_dict_get(v, "type");
  314. char *href = xs_dict_get(v, "href");
  315. char *name = xs_dict_get(v, "name");
  316. if (type && strcmp(type, "Mention") == 0 &&
  317. href && strcmp(href, snac->actor) != 0 && name) {
  318. xs *l = xs_split(name, "@");
  319. /* if it's a name without host, query the webfinger */
  320. if (xs_list_len(l) < 3) {
  321. #if 0
  322. xs *actor = NULL;
  323. xs *user = NULL;
  324. if (valid_status(webfinger_request(href, &actor, &user))) {
  325. s = xs_str_cat(s, user);
  326. s = xs_str_cat(s, " ");
  327. }
  328. #endif
  329. }
  330. else {
  331. s = xs_str_cat(s, name);
  332. s = xs_str_cat(s, " ");
  333. }
  334. }
  335. }
  336. return s;
  337. }
  338. d_char *html_entry_controls(snac *snac, d_char *os, char *msg)
  339. {
  340. char *id = xs_dict_get(msg, "id");
  341. char *actor = xs_dict_get(msg, "attributedTo");
  342. char *meta = xs_dict_get(msg, "_snac");
  343. xs *s = xs_str_new(NULL);
  344. xs *md5 = xs_md5_hex(id, strlen(id));
  345. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  346. {
  347. xs *s1 = xs_fmt(
  348. "<form method=\"post\" action=\"%s/admin/action\">\n"
  349. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  350. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  351. "<input type=\"button\" name=\"action\" "
  352. "value=\"%s\" onclick=\""
  353. "x = document.getElementById('%s_reply'); "
  354. "if (x.style.display == 'block') "
  355. " x.style.display = 'none'; else "
  356. " x.style.display = 'block';"
  357. "\">\n",
  358. snac->actor, id, actor,
  359. L("Reply"),
  360. md5
  361. );
  362. s = xs_str_cat(s, s1);
  363. }
  364. if (strcmp(actor, snac->actor) != 0) {
  365. /* controls for other actors than this one */
  366. char *l;
  367. l = xs_dict_get(meta, "liked_by");
  368. if (xs_list_in(l, snac->actor) == -1) {
  369. /* not already liked; add button */
  370. s = html_button(s, "like", L("Like"));
  371. }
  372. l = xs_dict_get(meta, "announced_by");
  373. if (xs_list_in(l, snac->actor) == -1) {
  374. /* not already boosted; add button */
  375. s = html_button(s, "boost", L("Boost"));
  376. }
  377. if (following_check(snac, actor)) {
  378. s = html_button(s, "unfollow", L("Unfollow"));
  379. }
  380. else {
  381. s = html_button(s, "follow", L("Follow"));
  382. s = html_button(s, "mute", L("MUTE"));
  383. }
  384. }
  385. s = html_button(s, "delete", L("Delete"));
  386. s = xs_str_cat(s, "</form>\n");
  387. {
  388. /* the post textarea */
  389. xs *ct = build_mentions(snac, msg);
  390. xs *s1 = xs_fmt(
  391. "<p><div class=\"snac-note\" style=\"display: none\" id=\"%s_reply\">\n"
  392. "<form method=\"post\" action=\"%s/admin/note\" id=\"%s_reply_form\">\n"
  393. "<textarea class=\"snac-textarea\" name=\"content\" "
  394. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  395. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  396. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  397. "</form><p></div>\n",
  398. md5,
  399. snac->actor, md5,
  400. ct,
  401. id,
  402. L("Post")
  403. );
  404. s = xs_str_cat(s, s1);
  405. }
  406. s = xs_str_cat(s, "</div>\n");
  407. return xs_str_cat(os, s);
  408. }
  409. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level)
  410. {
  411. char *id = xs_dict_get(msg, "id");
  412. char *type = xs_dict_get(msg, "type");
  413. char *meta = xs_dict_get(msg, "_snac");
  414. xs *actor_o = NULL;
  415. char *actor;
  416. /* do not show non-public messages in the public timeline */
  417. if (local && !is_msg_public(snac, msg))
  418. return os;
  419. /* return if already seen */
  420. if (xs_set_add(seen, id) == 0)
  421. return os;
  422. xs *s = xs_str_new(NULL);
  423. if (strcmp(type, "Follow") == 0) {
  424. actor = xs_dict_get(msg, "actor");
  425. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  426. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  427. s = xs_str_cat(s, s1);
  428. s = html_msg_icon(snac, s, msg);
  429. s = xs_str_cat(s, "</div>\n");
  430. return xs_str_cat(os, s);
  431. }
  432. /* bring the main actor */
  433. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  434. return os;
  435. /* ignore muted morons immediately */
  436. if (is_muted(snac, actor))
  437. return os;
  438. if (!valid_status(actor_get(snac, actor, &actor_o)))
  439. return os;
  440. /* if this is our post, add the score */
  441. if (xs_startswith(id, snac->actor)) {
  442. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  443. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  444. xs *s1 = xs_fmt(
  445. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  446. likes, boosts);
  447. s = xs_str_cat(s, s1);
  448. }
  449. if (level == 0) {
  450. char *p;
  451. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  452. /* print the origin of the post, if any */
  453. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  454. xs *actor_r = NULL;
  455. if (valid_status(actor_get(snac, p, &actor_r))) {
  456. char *name;
  457. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  458. name = xs_dict_get(actor_r, "preferredUsername");
  459. xs *s1 = xs_fmt(
  460. "<div class=\"snac-origin\">"
  461. "<a href=\"%s\">%s</a> %s</div>\n",
  462. xs_dict_get(actor_r, "id"),
  463. name,
  464. L("boosted")
  465. );
  466. s = xs_str_cat(s, s1);
  467. }
  468. }
  469. else
  470. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  471. /* this may happen if any of the autor or the parent aren't here */
  472. xs *s1 = xs_fmt(
  473. "<div class=\"snac-origin\">%s "
  474. "<a href=\"%s\">»</a></div>\n",
  475. L("in reply to"), p
  476. );
  477. s = xs_str_cat(s, s1);
  478. }
  479. else
  480. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  481. xs_list_in(p, snac->actor) != -1) {
  482. /* we boosted this */
  483. xs *s1 = xs_fmt(
  484. "<div class=\"snac-origin\">"
  485. "<a href=\"%s\">%s</a> %s</a></div>",
  486. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  487. );
  488. s = xs_str_cat(s, s1);
  489. }
  490. else
  491. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  492. xs_list_in(p, snac->actor) != -1) {
  493. /* we liked this */
  494. xs *s1 = xs_fmt(
  495. "<div class=\"snac-origin\">"
  496. "<a href=\"%s\">%s</a> %s</a></div>",
  497. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  498. );
  499. s = xs_str_cat(s, s1);
  500. }
  501. }
  502. else
  503. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  504. s = html_msg_icon(snac, s, msg);
  505. /* add the content */
  506. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  507. {
  508. xs *c = xs_dup(xs_dict_get(msg, "content"));
  509. /* do some tweaks to the content */
  510. c = xs_replace_i(c, "\r", "");
  511. while (xs_endswith(c, "<br><br>"))
  512. c = xs_crop(c, 0, -4);
  513. c = xs_replace_i(c, "<br><br>", "<p>");
  514. if (!xs_startswith(c, "<p>")) {
  515. xs *s1 = c;
  516. c = xs_fmt("<p>%s</p>", s1);
  517. }
  518. s = xs_str_cat(s, c);
  519. }
  520. s = xs_str_cat(s, "\n");
  521. /* add the attachments */
  522. char *attach;
  523. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  524. char *v;
  525. while (xs_list_iter(&attach, &v)) {
  526. char *t = xs_dict_get(v, "mediaType");
  527. if (t && xs_startswith(t, "image/")) {
  528. char *url = xs_dict_get(v, "url");
  529. char *name = xs_dict_get(v, "name");
  530. if (url != NULL) {
  531. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  532. url, xs_is_null(name) ? "" : name);
  533. s = xs_str_cat(s, s1);
  534. }
  535. }
  536. }
  537. }
  538. s = xs_str_cat(s, "</div>\n");
  539. /** controls **/
  540. if (!local)
  541. s = html_entry_controls(snac, s, msg);
  542. /** children **/
  543. char *children = xs_dict_get(meta, "children");
  544. if (xs_list_len(children)) {
  545. int left = xs_list_len(children);
  546. char *id;
  547. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  548. if (left > 3)
  549. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  550. while (xs_list_iter(&children, &id)) {
  551. xs *chd = timeline_find(snac, id);
  552. if (left == 3)
  553. s = xs_str_cat(s, "</details>\n");
  554. if (chd != NULL)
  555. s = html_entry(snac, s, chd, seen, local, level + 1);
  556. else
  557. snac_debug(snac, 1, xs_fmt("cannot read from timeline child %s", id));
  558. left--;
  559. }
  560. s = xs_str_cat(s, "</div>\n");
  561. }
  562. s = xs_str_cat(s, "</div>\n");
  563. return xs_str_cat(os, s);
  564. }
  565. d_char *html_user_footer(snac *snac, d_char *s)
  566. {
  567. xs *s1 = xs_fmt(
  568. "<div class=\"snac-footer\">\n"
  569. "<a href=\"%s\">%s</a> - "
  570. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  571. srv_baseurl,
  572. L("about this site")
  573. );
  574. return xs_str_cat(s, s1);
  575. }
  576. d_char *html_timeline(snac *snac, char *list, int local)
  577. /* returns the HTML for the timeline */
  578. {
  579. d_char *s = xs_str_new(NULL);
  580. xs_set *seen = xs_set_new(4096);
  581. char *v;
  582. double t = ftime();
  583. s = html_user_header(snac, s, local);
  584. if (!local)
  585. s = html_top_controls(snac, s);
  586. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  587. while (xs_list_iter(&list, &v)) {
  588. xs *msg = timeline_get(snac, v);
  589. s = html_entry(snac, s, msg, seen, local, 0);
  590. }
  591. s = xs_str_cat(s, "</div>\n");
  592. if (local) {
  593. xs *s1 = xs_fmt(
  594. "<div class=\"snac-history\">\n"
  595. "<p class=\"snac-history-title\">%s</p><ul>\n",
  596. L("History")
  597. );
  598. s = xs_str_cat(s, s1);
  599. xs *list = history_list(snac);
  600. char *p, *v;
  601. p = list;
  602. while (xs_list_iter(&p, &v)) {
  603. xs *fn = xs_replace(v, ".html", "");
  604. xs *s1 = xs_fmt(
  605. "<li><a href=\"%s/h/%s\">%s</li>\n",
  606. snac->actor, v, fn);
  607. s = xs_str_cat(s, s1);
  608. }
  609. s = xs_str_cat(s, "</ul></div>\n");
  610. }
  611. s = html_user_footer(snac, s);
  612. {
  613. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  614. s = xs_str_cat(s, s1);
  615. }
  616. s = xs_str_cat(s, "</body>\n</html>\n");
  617. xs_set_free(seen);
  618. return s;
  619. }
  620. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  621. {
  622. int status = 404;
  623. snac snac;
  624. char *uid, *p_path;
  625. xs *l = xs_split_n(q_path, "/", 2);
  626. uid = xs_list_get(l, 1);
  627. if (!uid || !user_open(&snac, uid)) {
  628. /* invalid user */
  629. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  630. return 404;
  631. }
  632. p_path = xs_list_get(l, 2);
  633. if (p_path == NULL) {
  634. /* public timeline */
  635. xs *h = xs_str_localtime(0, "%Y-%m.html");
  636. if (history_mtime(&snac, h) > timeline_mtime(&snac)) {
  637. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  638. *body = history_get(&snac, h);
  639. *b_size = strlen(*body);
  640. status = 200;
  641. }
  642. else {
  643. xs *list = local_list(&snac, 0xfffffff);
  644. *body = html_timeline(&snac, list, 1);
  645. *b_size = strlen(*body);
  646. status = 200;
  647. history_add(&snac, h, *body, *b_size);
  648. }
  649. }
  650. else
  651. if (strcmp(p_path, "admin") == 0) {
  652. /* private timeline */
  653. if (!login(&snac, req))
  654. status = 401;
  655. else {
  656. if (history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  657. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  658. *body = history_get(&snac, "timeline.html_");
  659. *b_size = strlen(*body);
  660. status = 200;
  661. }
  662. else {
  663. snac_debug(&snac, 1, xs_fmt("building timeline"));
  664. xs *list = timeline_list(&snac, 0xfffffff);
  665. *body = html_timeline(&snac, list, 0);
  666. *b_size = strlen(*body);
  667. status = 200;
  668. history_add(&snac, "timeline.html_", *body, *b_size);
  669. }
  670. }
  671. }
  672. else
  673. if (xs_startswith(p_path, "p/")) {
  674. /* a timeline with just one entry */
  675. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  676. xs *fn = _timeline_find_fn(&snac, id);
  677. if (fn != NULL) {
  678. xs *list = xs_list_new();
  679. list = xs_list_append(list, fn);
  680. *body = html_timeline(&snac, list, 1);
  681. *b_size = strlen(*body);
  682. status = 200;
  683. }
  684. }
  685. else
  686. if (xs_startswith(p_path, "s/")) {
  687. /* a static file */
  688. }
  689. else
  690. if (xs_startswith(p_path, "h/")) {
  691. /* an entry from the history */
  692. xs *id = xs_replace(p_path, "h/", "");
  693. if ((*body = history_get(&snac, id)) != NULL) {
  694. *b_size = strlen(*body);
  695. status = 200;
  696. }
  697. }
  698. else
  699. status = 404;
  700. user_free(&snac);
  701. if (valid_status(status)) {
  702. *ctype = "text/html; charset=utf-8";
  703. }
  704. return status;
  705. }
  706. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  707. char **body, int *b_size, char **ctype)
  708. {
  709. int status = 0;
  710. snac snac;
  711. char *uid, *p_path;
  712. char *p_vars;
  713. xs *l = xs_split_n(q_path, "/", 2);
  714. uid = xs_list_get(l, 1);
  715. if (!uid || !user_open(&snac, uid)) {
  716. /* invalid user */
  717. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  718. return 404;
  719. }
  720. p_path = xs_list_get(l, 2);
  721. /* all posts must be authenticated */
  722. if (!login(&snac, req))
  723. return 401;
  724. p_vars = xs_dict_get(req, "p_vars");
  725. if (p_path && strcmp(p_path, "admin/note") == 0) {
  726. /* post note */
  727. char *content = xs_dict_get(p_vars, "content");
  728. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  729. if (content != NULL) {
  730. xs *msg = NULL;
  731. xs *c_msg = NULL;
  732. msg = msg_note(&snac, content, NULL, in_reply_to);
  733. c_msg = msg_create(&snac, msg);
  734. post(&snac, c_msg);
  735. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  736. }
  737. status = 303;
  738. }
  739. else
  740. if (p_path && strcmp(p_path, "admin/action") == 0) {
  741. /* action on an entry */
  742. char *id = xs_dict_get(p_vars, "id");
  743. char *actor = xs_dict_get(p_vars, "actor");
  744. char *action = xs_dict_get(p_vars, "action");
  745. if (action == NULL)
  746. return 404;
  747. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  748. status = 303;
  749. if (strcmp(action, L("Like")) == 0) {
  750. xs *msg = msg_admiration(&snac, id, "Like");
  751. post(&snac, msg);
  752. timeline_admire(&snac, id, snac.actor, 1);
  753. }
  754. else
  755. if (strcmp(action, L("Boost")) == 0) {
  756. xs *msg = msg_admiration(&snac, id, "Announce");
  757. post(&snac, msg);
  758. timeline_admire(&snac, id, snac.actor, 0);
  759. }
  760. else
  761. if (strcmp(action, L("MUTE")) == 0) {
  762. mute(&snac, actor);
  763. }
  764. else
  765. if (strcmp(action, L("Follow")) == 0) {
  766. xs *msg = msg_follow(&snac, actor);
  767. /* reload the actor from the message, in may be different */
  768. actor = xs_dict_get(msg, "object");
  769. following_add(&snac, actor, msg);
  770. enqueue_output(&snac, msg, actor, 0);
  771. }
  772. else
  773. if (strcmp(action, L("Unfollow")) == 0) {
  774. /* get the following object */
  775. xs *object = NULL;
  776. if (valid_status(following_get(&snac, actor, &object))) {
  777. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  778. following_del(&snac, actor);
  779. enqueue_output(&snac, msg, actor, 0);
  780. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  781. }
  782. else
  783. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  784. }
  785. else
  786. if (strcmp(action, L("Delete")) == 0) {
  787. /* delete an entry */
  788. if (xs_startswith(id, snac.actor)) {
  789. /* it's a post by us: generate a delete */
  790. xs *msg = msg_delete(&snac, id);
  791. post(&snac, msg);
  792. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  793. }
  794. timeline_del(&snac, id);
  795. snac_log(&snac, xs_fmt("deleted entry %s", id));
  796. }
  797. else
  798. status = 404;
  799. /* delete the cached timeline */
  800. if (status == 303)
  801. history_del(&snac, "timeline.html_");
  802. }
  803. else
  804. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  805. /* change of user data */
  806. char *v;
  807. char *p1, *p2;
  808. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  809. snac.config = xs_dict_set(snac.config, "name", v);
  810. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  811. snac.config = xs_dict_set(snac.config, "avatar", v);
  812. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  813. snac.config = xs_dict_set(snac.config, "bio", v);
  814. /* password change? */
  815. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  816. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  817. *p1 && strcmp(p1, p2) == 0) {
  818. xs *pw = hash_password(snac.uid, p1, NULL);
  819. snac.config = xs_dict_set(snac.config, "passwd", pw);
  820. }
  821. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  822. xs *bfn = xs_fmt("%s.bak", fn);
  823. FILE *f;
  824. rename(fn, bfn);
  825. if ((f = fopen(fn, "w")) != NULL) {
  826. xs *j = xs_json_dumps_pp(snac.config, 4);
  827. fwrite(j, strlen(j), 1, f);
  828. fclose(f);
  829. }
  830. else
  831. rename(bfn, fn);
  832. history_del(&snac, "timeline.html_");
  833. xs *a_msg = msg_actor(&snac);
  834. xs *u_msg = msg_update(&snac, a_msg);
  835. post(&snac, u_msg);
  836. status = 303;
  837. }
  838. if (status == 303) {
  839. *body = xs_fmt("%s/admin", snac.actor);
  840. *b_size = strlen(*body);
  841. }
  842. return status;
  843. }