html.c 32 KB

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