html.c 50 KB

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