html.c 48 KB

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