html.c 30 KB

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