html.c 28 KB

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