html.c 42 KB

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