html.c 29 KB

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