html.c 41 KB

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