html.c 43 KB

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