html.c 27 KB

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