html.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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. bio = not_really_markdown(xs_dict_get(snac->config, "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. "\n",
  307. snac->actor, id, actor, num,
  308. L("Reply"),
  309. md5
  310. );
  311. s = xs_str_cat(s, s1);
  312. }
  313. {
  314. char *l;
  315. l = xs_dict_get(meta, "liked_by");
  316. if (xs_list_in(l, snac->actor) == -1) {
  317. /* not already liked; add button */
  318. s = html_button(s, "like", L("Like"));
  319. }
  320. l = xs_dict_get(meta, "announced_by");
  321. if (strcmp(actor, snac->actor) == 0 || xs_list_in(l, snac->actor) == -1) {
  322. /* not already boosted or us; add button */
  323. s = html_button(s, "boost", L("Boost"));
  324. }
  325. }
  326. if (strcmp(actor, snac->actor) != 0) {
  327. /* controls for other actors than this one */
  328. if (following_check(snac, actor)) {
  329. s = html_button(s, "unfollow", L("Unfollow"));
  330. }
  331. else {
  332. s = html_button(s, "follow", L("Follow"));
  333. }
  334. s = html_button(s, "mute", L("MUTE"));
  335. }
  336. s = html_button(s, "delete", L("Delete"));
  337. s = html_button(s, "hide", L("Hide"));
  338. s = xs_str_cat(s, "</form>\n");
  339. {
  340. /* the post textarea */
  341. xs *ct = build_mentions(snac, msg);
  342. xs *s1 = xs_fmt(
  343. "<p><details><summary>%s</summary>\n"
  344. "<p><div class=\"snac-note\" id=\"%s_reply\">\n"
  345. "<form method=\"post\" action=\"%s/admin/note\" "
  346. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  347. "<textarea class=\"snac-textarea\" name=\"content\" "
  348. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  349. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  350. "<p><input type=\"file\" name=\"attach\">\n"
  351. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  352. "</form><p></div>\n"
  353. "</details><p>"
  354. "\n",
  355. L("Reply..."),
  356. md5,
  357. snac->actor, md5,
  358. ct,
  359. id,
  360. L("Post")
  361. );
  362. s = xs_str_cat(s, s1);
  363. }
  364. s = xs_str_cat(s, "</div>\n");
  365. return xs_str_cat(os, s);
  366. }
  367. d_char *html_entry(snac *snac, d_char *os, char *msg, xs_set *seen, int local, int level, int *num)
  368. {
  369. char *id = xs_dict_get(msg, "id");
  370. char *type = xs_dict_get(msg, "type");
  371. char *meta = xs_dict_get(msg, "_snac");
  372. char *actor;
  373. int sensitive = 0;
  374. char *v;
  375. /* do not show non-public messages in the public timeline */
  376. if (local && !is_msg_public(snac, msg))
  377. return os;
  378. /* return if already seen */
  379. if (xs_set_add(seen, id) == 0)
  380. return os;
  381. xs *s = xs_str_new(NULL);
  382. /* top wrap */
  383. if ((v = xs_dict_get(meta, "hidden")) && xs_type(v) == XSTYPE_TRUE)
  384. s = xs_str_cat(s, "<div style=\"display: none\">\n");
  385. else
  386. s = xs_str_cat(s, "<div>\n");
  387. if (level == 0) {
  388. xs *s1 = xs_fmt("<a name=\"%d_entry\"></a>\n", *num);
  389. *num = *num + 1;
  390. s = xs_str_cat(s, s1);
  391. }
  392. if (strcmp(type, "Follow") == 0) {
  393. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  394. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  395. s = xs_str_cat(s, s1);
  396. s = html_msg_icon(snac, s, msg);
  397. s = xs_str_cat(s, "</div>\n");
  398. return xs_str_cat(os, s);
  399. }
  400. /* bring the main actor */
  401. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  402. return os;
  403. /* ignore muted morons immediately */
  404. if (is_muted(snac, actor))
  405. return os;
  406. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  407. return os;
  408. /* if this is our post, add the score */
  409. if (xs_startswith(id, snac->actor)) {
  410. int likes = xs_list_len(xs_dict_get(meta, "liked_by"));
  411. int boosts = xs_list_len(xs_dict_get(meta, "announced_by"));
  412. /* alternate emojis: %d &#128077; %d &#128257; */
  413. xs *s1 = xs_fmt(
  414. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  415. likes, boosts);
  416. s = xs_str_cat(s, s1);
  417. }
  418. if (level == 0) {
  419. char *p;
  420. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  421. /* print the origin of the post, if any */
  422. if (!xs_is_null(p = xs_dict_get(meta, "referrer"))) {
  423. xs *actor_r = NULL;
  424. if (valid_status(actor_get(snac, p, &actor_r))) {
  425. char *name;
  426. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  427. name = xs_dict_get(actor_r, "preferredUsername");
  428. xs *s1 = xs_fmt(
  429. "<div class=\"snac-origin\">"
  430. "<a href=\"%s\">%s</a> %s</div>\n",
  431. xs_dict_get(actor_r, "id"),
  432. name,
  433. L("boosted")
  434. );
  435. s = xs_str_cat(s, s1);
  436. }
  437. }
  438. else
  439. if (!xs_is_null((p = xs_dict_get(meta, "parent"))) && *p) {
  440. /* this may happen if any of the autor or the parent aren't here */
  441. xs *s1 = xs_fmt(
  442. "<div class=\"snac-origin\">%s "
  443. "<a href=\"%s\">»</a></div>\n",
  444. L("in reply to"), p
  445. );
  446. s = xs_str_cat(s, s1);
  447. }
  448. else
  449. if (!xs_is_null((p = xs_dict_get(meta, "announced_by"))) &&
  450. xs_list_in(p, snac->actor) != -1) {
  451. /* we boosted this */
  452. xs *s1 = xs_fmt(
  453. "<div class=\"snac-origin\">"
  454. "<a href=\"%s\">%s</a> %s</a></div>",
  455. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  456. );
  457. s = xs_str_cat(s, s1);
  458. }
  459. else
  460. if (!xs_is_null((p = xs_dict_get(meta, "liked_by"))) &&
  461. xs_list_in(p, snac->actor) != -1) {
  462. /* we liked this */
  463. xs *s1 = xs_fmt(
  464. "<div class=\"snac-origin\">"
  465. "<a href=\"%s\">%s</a> %s</a></div>",
  466. snac->actor, xs_dict_get(snac->config, "name"), L("liked")
  467. );
  468. s = xs_str_cat(s, s1);
  469. }
  470. }
  471. else
  472. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  473. s = html_msg_icon(snac, s, msg);
  474. /* add the content */
  475. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  476. /* is it sensitive? */
  477. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  478. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  479. v = "...";
  480. xs *s1 = xs_fmt("<details><summary>%s [%s]</summary>\n", v, L("SENSITIVE CONTENT"));
  481. s = xs_str_cat(s, s1);
  482. sensitive = 1;
  483. }
  484. {
  485. xs *c = xs_dup(xs_dict_get(msg, "content"));
  486. char *p, *v;
  487. /* do some tweaks to the content */
  488. c = xs_replace_i(c, "\r", "");
  489. while (xs_endswith(c, "<br><br>"))
  490. c = xs_crop(c, 0, -4);
  491. c = xs_replace_i(c, "<br><br>", "<p>");
  492. if (!xs_startswith(c, "<p>")) {
  493. xs *s1 = c;
  494. c = xs_fmt("<p>%s</p>", s1);
  495. }
  496. /* replace the :shortnames: */
  497. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  498. /* iterate the tags */
  499. while (xs_list_iter(&p, &v)) {
  500. char *t = xs_dict_get(v, "type");
  501. if (t && strcmp(t, "Emoji") == 0) {
  502. char *n = xs_dict_get(v, "name");
  503. char *i = xs_dict_get(v, "icon");
  504. if (n && i) {
  505. char *u = xs_dict_get(i, "url");
  506. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\"/>", u);
  507. c = xs_replace_i(c, n, img);
  508. }
  509. }
  510. }
  511. }
  512. xs *sc = sanitize(c);
  513. s = xs_str_cat(s, sc);
  514. }
  515. s = xs_str_cat(s, "\n");
  516. /* add the attachments */
  517. char *attach;
  518. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  519. char *v;
  520. while (xs_list_iter(&attach, &v)) {
  521. char *t = xs_dict_get(v, "mediaType");
  522. if (xs_is_null(t))
  523. continue;
  524. if (xs_startswith(t, "image/")) {
  525. char *url = xs_dict_get(v, "url");
  526. char *name = xs_dict_get(v, "name");
  527. if (url != NULL) {
  528. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\"/></p>\n",
  529. url, xs_is_null(name) ? "" : name);
  530. s = xs_str_cat(s, s1);
  531. }
  532. }
  533. else
  534. if (xs_startswith(t, "video/")) {
  535. char *url = xs_dict_get(v, "url");
  536. if (url != NULL) {
  537. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  538. s = xs_str_cat(s, s1);
  539. }
  540. }
  541. }
  542. }
  543. if (sensitive)
  544. s = xs_str_cat(s, "</details><p>\n");
  545. s = xs_str_cat(s, "</div>\n");
  546. /** controls **/
  547. if (!local)
  548. s = html_entry_controls(snac, s, msg, *num);
  549. /** children **/
  550. char *children = xs_dict_get(meta, "children");
  551. int left = xs_list_len(children);
  552. if (left) {
  553. char *id;
  554. if (level < 4)
  555. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  556. else
  557. s = xs_str_cat(s, "<div>\n");
  558. if (left > 3)
  559. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  560. while (xs_list_iter(&children, &id)) {
  561. xs *chd = timeline_find(snac, id);
  562. if (left == 3)
  563. s = xs_str_cat(s, "</details>\n");
  564. if (chd != NULL)
  565. s = html_entry(snac, s, chd, seen, local, level + 1, num);
  566. else
  567. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", id));
  568. left--;
  569. }
  570. s = xs_str_cat(s, "</div>\n");
  571. }
  572. s = xs_str_cat(s, "</div>\n</div>\n");
  573. return xs_str_cat(os, s);
  574. }
  575. d_char *html_user_footer(snac *snac, d_char *s)
  576. {
  577. xs *s1 = xs_fmt(
  578. "<div class=\"snac-footer\">\n"
  579. "<a href=\"%s\">%s</a> - "
  580. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  581. srv_baseurl,
  582. L("about this site")
  583. );
  584. return xs_str_cat(s, s1);
  585. }
  586. d_char *html_timeline(snac *snac, char *list, int local)
  587. /* returns the HTML for the timeline */
  588. {
  589. d_char *s = xs_str_new(NULL);
  590. xs_set *seen = xs_set_new(4096);
  591. char *v;
  592. double t = ftime();
  593. int num = 0;
  594. s = html_user_header(snac, s, local);
  595. if (!local)
  596. s = html_top_controls(snac, s);
  597. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  598. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  599. while (xs_list_iter(&list, &v)) {
  600. xs *msg = timeline_get(snac, v);
  601. s = html_entry(snac, s, msg, seen, local, 0, &num);
  602. }
  603. s = xs_str_cat(s, "</div>\n");
  604. if (local) {
  605. xs *s1 = xs_fmt(
  606. "<div class=\"snac-history\">\n"
  607. "<p class=\"snac-history-title\">%s</p><ul>\n",
  608. L("History")
  609. );
  610. s = xs_str_cat(s, s1);
  611. xs *list = history_list(snac);
  612. char *p, *v;
  613. p = list;
  614. while (xs_list_iter(&p, &v)) {
  615. xs *fn = xs_replace(v, ".html", "");
  616. xs *s1 = xs_fmt(
  617. "<li><a href=\"%s/h/%s\">%s</li>\n",
  618. snac->actor, v, fn);
  619. s = xs_str_cat(s, s1);
  620. }
  621. s = xs_str_cat(s, "</ul></div>\n");
  622. }
  623. s = html_user_footer(snac, s);
  624. {
  625. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  626. s = xs_str_cat(s, s1);
  627. }
  628. s = xs_str_cat(s, "</body>\n</html>\n");
  629. xs_set_free(seen);
  630. return s;
  631. }
  632. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  633. {
  634. xs *s = xs_str_new(NULL);
  635. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  636. char *p, *v;
  637. s = xs_str_cat(s, h);
  638. p = list;
  639. while (xs_list_iter(&p, &v)) {
  640. char *actor_id = xs_dict_get(v, "actor");
  641. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  642. xs *actor = NULL;
  643. if (valid_status(actor_get(snac, actor_id, &actor))) {
  644. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  645. s = html_actor_icon(snac, s, actor, xs_dict_get(actor, "published"), NULL, 0);
  646. /* content (user bio) */
  647. char *c = xs_dict_get(actor, "summary");
  648. if (!xs_is_null(c)) {
  649. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  650. xs *sc = sanitize(c);
  651. if (xs_startswith(sc, "<p>"))
  652. s = xs_str_cat(s, sc);
  653. else {
  654. xs *s1 = xs_fmt("<p>%s</p>", sc);
  655. s = xs_str_cat(s, s1);
  656. }
  657. s = xs_str_cat(s, "</div>\n");
  658. }
  659. /* buttons */
  660. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  661. xs *s1 = xs_fmt(
  662. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  663. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n",
  664. snac->actor, actor_id,
  665. md5, t
  666. );
  667. s = xs_str_cat(s, s1);
  668. if (following_check(snac, actor_id))
  669. s = html_button(s, "unfollow", L("Unfollow"));
  670. else
  671. s = html_button(s, "follow", L("Follow"));
  672. if (is_muted(snac, actor_id))
  673. s = html_button(s, "unmute", L("Unmute"));
  674. else
  675. s = html_button(s, "mute", L("MUTE"));
  676. s = xs_str_cat(s, "</form>\n");
  677. /* the post textarea */
  678. xs *s2 = xs_fmt(
  679. "<p><details><summary>%s</summary>\n"
  680. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  681. "<form method=\"post\" action=\"%s/admin/note\" "
  682. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  683. "<textarea class=\"snac-textarea\" name=\"content\" "
  684. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  685. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  686. "<p><input type=\"file\" name=\"attach\">\n"
  687. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  688. "</form><p></div>\n"
  689. "</details><p>\n",
  690. L("Direct Message..."),
  691. md5, t,
  692. snac->actor, md5,
  693. actor_id,
  694. L("Post")
  695. );
  696. s = xs_str_cat(s, s2);
  697. s = xs_str_cat(s, "</div>\n");
  698. s = xs_str_cat(s, "</div>\n");
  699. }
  700. }
  701. return xs_str_cat(os, s);
  702. }
  703. d_char *html_people(snac *snac)
  704. {
  705. d_char *s = xs_str_new(NULL);
  706. xs *wing = following_list(snac);
  707. xs *wers = follower_list(snac);
  708. s = html_user_header(snac, s, 0);
  709. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  710. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  711. s = html_user_footer(snac, s);
  712. s = xs_str_cat(s, "</body>\n</html>\n");
  713. return s;
  714. }
  715. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  716. {
  717. int status = 404;
  718. snac snac;
  719. char *uid, *p_path;
  720. int cache = 1;
  721. char *v;
  722. xs *l = xs_split_n(q_path, "/", 2);
  723. uid = xs_list_get(l, 1);
  724. if (!uid || !user_open(&snac, uid)) {
  725. /* invalid user */
  726. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  727. return 404;
  728. }
  729. /* check if server config variable 'disable_cache' is set */
  730. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  731. cache = 0;
  732. p_path = xs_list_get(l, 2);
  733. if (p_path == NULL) {
  734. /* public timeline */
  735. xs *h = xs_str_localtime(0, "%Y-%m.html");
  736. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  737. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  738. *body = history_get(&snac, h);
  739. *b_size = strlen(*body);
  740. status = 200;
  741. }
  742. else {
  743. xs *list = local_list(&snac, 0xfffffff);
  744. *body = html_timeline(&snac, list, 1);
  745. *b_size = strlen(*body);
  746. status = 200;
  747. history_add(&snac, h, *body, *b_size);
  748. }
  749. }
  750. else
  751. if (strcmp(p_path, "admin") == 0) {
  752. /* private timeline */
  753. if (!login(&snac, req))
  754. status = 401;
  755. else {
  756. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  757. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  758. *body = history_get(&snac, "timeline.html_");
  759. *b_size = strlen(*body);
  760. status = 200;
  761. }
  762. else {
  763. snac_debug(&snac, 1, xs_fmt("building timeline"));
  764. xs *list = timeline_list(&snac, 0xfffffff);
  765. *body = html_timeline(&snac, list, 0);
  766. *b_size = strlen(*body);
  767. status = 200;
  768. history_add(&snac, "timeline.html_", *body, *b_size);
  769. }
  770. }
  771. }
  772. else
  773. if (strcmp(p_path, "people") == 0) {
  774. /* the list of people */
  775. if (!login(&snac, req))
  776. status = 401;
  777. else {
  778. *body = html_people(&snac);
  779. *b_size = strlen(*body);
  780. status = 200;
  781. }
  782. }
  783. else
  784. if (xs_startswith(p_path, "p/")) {
  785. /* a timeline with just one entry */
  786. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  787. xs *fn = _timeline_find_fn(&snac, id);
  788. if (fn != NULL) {
  789. xs *list = xs_list_new();
  790. list = xs_list_append(list, fn);
  791. *body = html_timeline(&snac, list, 1);
  792. *b_size = strlen(*body);
  793. status = 200;
  794. }
  795. }
  796. else
  797. if (xs_startswith(p_path, "s/")) {
  798. /* a static file */
  799. xs *l = xs_split(p_path, "/");
  800. char *id = xs_list_get(l, 1);
  801. int sz;
  802. if (valid_status(static_get(&snac, id, body, &sz))) {
  803. *b_size = sz;
  804. *ctype = xs_mime_by_ext(id);
  805. status = 200;
  806. }
  807. }
  808. else
  809. if (xs_startswith(p_path, "h/")) {
  810. /* an entry from the history */
  811. xs *l = xs_split(p_path, "/");
  812. char *id = xs_list_get(l, 1);
  813. if ((*body = history_get(&snac, id)) != NULL) {
  814. *b_size = strlen(*body);
  815. status = 200;
  816. }
  817. }
  818. else
  819. status = 404;
  820. user_free(&snac);
  821. if (valid_status(status) && *ctype == NULL) {
  822. *ctype = "text/html; charset=utf-8";
  823. }
  824. return status;
  825. }
  826. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  827. char **body, int *b_size, char **ctype)
  828. {
  829. int status = 0;
  830. snac snac;
  831. char *uid, *p_path;
  832. char *p_vars;
  833. xs *l = xs_split_n(q_path, "/", 2);
  834. uid = xs_list_get(l, 1);
  835. if (!uid || !user_open(&snac, uid)) {
  836. /* invalid user */
  837. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  838. return 404;
  839. }
  840. p_path = xs_list_get(l, 2);
  841. /* all posts must be authenticated */
  842. if (!login(&snac, req)) {
  843. user_free(&snac);
  844. return 401;
  845. }
  846. p_vars = xs_dict_get(req, "p_vars");
  847. #if 0
  848. {
  849. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  850. printf("%s\n", j1);
  851. }
  852. #endif
  853. if (p_path && strcmp(p_path, "admin/note") == 0) {
  854. /* post note */
  855. char *content = xs_dict_get(p_vars, "content");
  856. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  857. char *attach_url = xs_dict_get(p_vars, "attach_url");
  858. char *attach_file = xs_dict_get(p_vars, "attach");
  859. char *to = xs_dict_get(p_vars, "to");
  860. xs *attach_list = xs_list_new();
  861. /* is attach_url set? */
  862. if (!xs_is_null(attach_url) && *attach_url != '\0')
  863. attach_list = xs_list_append(attach_list, attach_url);
  864. /* is attach_file set? */
  865. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  866. char *fn = xs_list_get(attach_file, 0);
  867. if (*fn != '\0') {
  868. char *ext = strrchr(fn, '.');
  869. xs *ntid = tid(0);
  870. xs *id = xs_fmt("%s%s", ntid, ext);
  871. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  872. int fo = xs_number_get(xs_list_get(attach_file, 1));
  873. int fs = xs_number_get(xs_list_get(attach_file, 2));
  874. /* store */
  875. static_put(&snac, id, payload + fo, fs);
  876. attach_list = xs_list_append(attach_list, url);
  877. }
  878. }
  879. if (content != NULL) {
  880. xs *msg = NULL;
  881. xs *c_msg = NULL;
  882. xs *content_2 = xs_replace(content, "\r", "");
  883. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list);
  884. c_msg = msg_create(&snac, msg);
  885. post(&snac, c_msg);
  886. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  887. }
  888. status = 303;
  889. }
  890. else
  891. if (p_path && strcmp(p_path, "admin/action") == 0) {
  892. /* action on an entry */
  893. char *id = xs_dict_get(p_vars, "id");
  894. char *actor = xs_dict_get(p_vars, "actor");
  895. char *action = xs_dict_get(p_vars, "action");
  896. if (action == NULL)
  897. return 404;
  898. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  899. status = 303;
  900. if (strcmp(action, L("Like")) == 0) {
  901. xs *msg = msg_admiration(&snac, id, "Like");
  902. post(&snac, msg);
  903. timeline_admire(&snac, id, snac.actor, 1);
  904. }
  905. else
  906. if (strcmp(action, L("Boost")) == 0) {
  907. xs *msg = msg_admiration(&snac, id, "Announce");
  908. post(&snac, msg);
  909. timeline_admire(&snac, id, snac.actor, 0);
  910. }
  911. else
  912. if (strcmp(action, L("MUTE")) == 0) {
  913. mute(&snac, actor);
  914. }
  915. else
  916. if (strcmp(action, L("Unmute")) == 0) {
  917. unmute(&snac, actor);
  918. }
  919. else
  920. if (strcmp(action, L("Hide")) == 0) {
  921. timeline_hide(&snac, id, 1);
  922. }
  923. else
  924. if (strcmp(action, L("Follow")) == 0) {
  925. xs *msg = msg_follow(&snac, actor);
  926. if (msg != NULL) {
  927. /* reload the actor from the message, in may be different */
  928. actor = xs_dict_get(msg, "object");
  929. following_add(&snac, actor, msg);
  930. enqueue_output(&snac, msg, actor, 0);
  931. }
  932. }
  933. else
  934. if (strcmp(action, L("Unfollow")) == 0) {
  935. /* get the following object */
  936. xs *object = NULL;
  937. if (valid_status(following_get(&snac, actor, &object))) {
  938. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  939. following_del(&snac, actor);
  940. enqueue_output(&snac, msg, actor, 0);
  941. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  942. }
  943. else
  944. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  945. }
  946. else
  947. if (strcmp(action, L("Delete")) == 0) {
  948. /* delete an entry */
  949. if (xs_startswith(id, snac.actor)) {
  950. /* it's a post by us: generate a delete */
  951. xs *msg = msg_delete(&snac, id);
  952. post(&snac, msg);
  953. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  954. }
  955. timeline_del(&snac, id);
  956. snac_log(&snac, xs_fmt("deleted entry %s", id));
  957. }
  958. else
  959. status = 404;
  960. /* delete the cached timeline */
  961. if (status == 303)
  962. history_del(&snac, "timeline.html_");
  963. }
  964. else
  965. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  966. /* change of user data */
  967. char *v;
  968. char *p1, *p2;
  969. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  970. snac.config = xs_dict_set(snac.config, "name", v);
  971. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  972. snac.config = xs_dict_set(snac.config, "avatar", v);
  973. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  974. snac.config = xs_dict_set(snac.config, "bio", v);
  975. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  976. snac.config = xs_dict_set(snac.config, "email", v);
  977. /* password change? */
  978. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  979. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  980. *p1 && strcmp(p1, p2) == 0) {
  981. xs *pw = hash_password(snac.uid, p1, NULL);
  982. snac.config = xs_dict_set(snac.config, "passwd", pw);
  983. }
  984. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  985. xs *bfn = xs_fmt("%s.bak", fn);
  986. FILE *f;
  987. rename(fn, bfn);
  988. if ((f = fopen(fn, "w")) != NULL) {
  989. xs *j = xs_json_dumps_pp(snac.config, 4);
  990. fwrite(j, strlen(j), 1, f);
  991. fclose(f);
  992. }
  993. else
  994. rename(bfn, fn);
  995. history_del(&snac, "timeline.html_");
  996. xs *a_msg = msg_actor(&snac);
  997. xs *u_msg = msg_update(&snac, a_msg);
  998. post(&snac, u_msg);
  999. status = 303;
  1000. }
  1001. if (status == 303) {
  1002. char *redir = xs_dict_get(p_vars, "redir");
  1003. if (xs_is_null(redir))
  1004. redir = "snac-posts";
  1005. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1006. *b_size = strlen(*body);
  1007. }
  1008. user_free(&snac);
  1009. return status;
  1010. }