html.c 51 KB

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