html.c 36 KB

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