html.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  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_json.h"
  6. #include "xs_regex.h"
  7. #include "xs_set.h"
  8. #include "xs_openssl.h"
  9. #include "xs_time.h"
  10. #include "xs_mime.h"
  11. #include "snac.h"
  12. int login(snac *snac, const xs_dict *headers)
  13. /* tries a login */
  14. {
  15. int logged_in = 0;
  16. const char *auth = xs_dict_get(headers, "authorization");
  17. if (auth && xs_startswith(auth, "Basic ")) {
  18. int sz;
  19. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  20. xs *s2 = xs_base64_dec(s1, &sz);
  21. xs *l1 = xs_split_n(s2, ":", 1);
  22. if (xs_list_len(l1) == 2) {
  23. logged_in = check_password(
  24. xs_list_get(l1, 0), xs_list_get(l1, 1),
  25. xs_dict_get(snac->config, "passwd"));
  26. }
  27. }
  28. if (logged_in)
  29. lastlog_write(snac, "web");
  30. return logged_in;
  31. }
  32. xs_str *actor_name(xs_dict *actor)
  33. /* gets the actor name */
  34. {
  35. xs_list *p;
  36. char *v;
  37. xs_str *name;
  38. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  39. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  40. v = "anonymous";
  41. }
  42. }
  43. name = encode_html(v);
  44. /* replace the :shortnames: */
  45. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  46. /* iterate the tags */
  47. while (xs_list_iter(&p, &v)) {
  48. char *t = xs_dict_get(v, "type");
  49. if (t && strcmp(t, "Emoji") == 0) {
  50. char *n = xs_dict_get(v, "name");
  51. char *i = xs_dict_get(v, "icon");
  52. if (n && i) {
  53. char *u = xs_dict_get(i, "url");
  54. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\" loading=\"lazy\"/>", u);
  55. name = xs_replace_i(name, n, img);
  56. }
  57. }
  58. }
  59. }
  60. return name;
  61. }
  62. xs_str *html_actor_icon(xs_str *os, char *actor,
  63. const char *date, const char *udate, const char *url, int priv)
  64. {
  65. xs *s = xs_str_new(NULL);
  66. xs *avatar = NULL;
  67. char *v;
  68. xs *name = actor_name(actor);
  69. /* get the avatar */
  70. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  71. (v = xs_dict_get(v, "url")) != NULL) {
  72. avatar = xs_dup(v);
  73. }
  74. if (avatar == NULL)
  75. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  76. {
  77. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\" "
  78. "loading=\"lazy\"/>\n", avatar);
  79. s = xs_str_cat(s, s1);
  80. }
  81. {
  82. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  83. xs_dict_get(actor, "id"), name);
  84. s = xs_str_cat(s, s1);
  85. }
  86. if (!xs_is_null(url)) {
  87. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", url);
  88. s = xs_str_cat(s, s1);
  89. }
  90. if (priv)
  91. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  92. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  93. s = xs_str_cat(s, " <span title=\"bot\">&#129302;</span>");
  94. if (xs_is_null(date)) {
  95. s = xs_str_cat(s, "\n&nbsp;\n");
  96. }
  97. else {
  98. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  99. xs *date_title = xs_dup(date);
  100. if (!xs_is_null(udate)) {
  101. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  102. date_label = xs_str_cat(date_label, " / ");
  103. date_label = xs_str_cat(date_label, sd);
  104. date_title = xs_str_cat(date_title, " / ");
  105. date_title = xs_str_cat(date_title, udate);
  106. }
  107. xs *edt = encode_html(date_title);
  108. xs *edl = encode_html(date_label);
  109. xs *s1 = xs_fmt(
  110. "\n<time class=\"dt-published snac-pubdate\" title=\"%s\">%s</time>\n",
  111. edt, edl);
  112. s = xs_str_cat(s, s1);
  113. }
  114. {
  115. char *username, *id;
  116. xs *s1;
  117. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  118. /* This should never be reached */
  119. username = "anonymous";
  120. }
  121. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  122. /* This should never be reached */
  123. id = "https://social.example.org/anonymous";
  124. }
  125. /* "LIKE AN ANIMAL" */
  126. xs *domain = xs_split(id, "/");
  127. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  128. xs *u1 = encode_html(user);
  129. s1 = xs_fmt(
  130. "<br><a href=\"%s\" class=\"p-author-tag h-card snac-author-tag\">%s</a>",
  131. xs_dict_get(actor, "id"), u1);
  132. s = xs_str_cat(s, s1);
  133. }
  134. return xs_str_cat(os, s);
  135. }
  136. xs_str *html_msg_icon(snac *snac, xs_str *os, const xs_dict *msg)
  137. {
  138. char *actor_id;
  139. xs *actor = NULL;
  140. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  141. actor_id = xs_dict_get(msg, "actor");
  142. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  143. char *date = NULL;
  144. char *udate = NULL;
  145. char *url = NULL;
  146. int priv = 0;
  147. const char *type = xs_dict_get(msg, "type");
  148. if (strcmp(type, "Note") == 0 || strcmp(type, "Question") == 0 || strcmp(type, "Page") == 0)
  149. url = xs_dict_get(msg, "id");
  150. priv = !is_msg_public(snac, msg);
  151. date = xs_dict_get(msg, "published");
  152. udate = xs_dict_get(msg, "updated");
  153. os = html_actor_icon(os, actor, date, udate, url, priv);
  154. }
  155. return os;
  156. }
  157. d_char *html_user_header(snac *snac, d_char *s, int local)
  158. /* creates the HTML header */
  159. {
  160. char *p, *v;
  161. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  162. s = xs_str_cat(s, "<meta name=\"viewport\" "
  163. "content=\"width=device-width, initial-scale=1\"/>\n");
  164. s = xs_str_cat(s, "<meta name=\"generator\" "
  165. "content=\"" USER_AGENT "\"/>\n");
  166. /* add server CSS */
  167. p = xs_dict_get(srv_config, "cssurls");
  168. while (xs_list_iter(&p, &v)) {
  169. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  170. s = xs_str_cat(s, s1);
  171. }
  172. /* add the user CSS */
  173. {
  174. xs *css = NULL;
  175. int size;
  176. /* try to open the user css */
  177. if (!valid_status(static_get(snac, "style.css", &css, &size, NULL, NULL))) {
  178. /* it's not there; try to open the server-wide css */
  179. FILE *f;
  180. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  181. if ((f = fopen(g_css_fn, "r")) != NULL) {
  182. css = xs_readall(f);
  183. fclose(f);
  184. }
  185. }
  186. if (css != NULL) {
  187. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  188. s = xs_str_cat(s, s1);
  189. }
  190. }
  191. {
  192. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  193. xs *es2 = encode_html(snac->uid);
  194. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  195. xs *s1 = xs_fmt("<title>%s (@%s@%s)</title>\n", es1, es2, es3);
  196. s = xs_str_cat(s, s1);
  197. }
  198. xs *avatar = xs_dup(xs_dict_get(snac->config, "avatar"));
  199. if (avatar == NULL || *avatar == '\0') {
  200. xs_free(avatar);
  201. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  202. }
  203. {
  204. xs *s_bio = xs_dup(xs_dict_get(snac->config, "bio"));
  205. int n;
  206. /* shorten the bio */
  207. for (n = 0; s_bio[n] && s_bio[n] != '&' && s_bio[n] != '.' &&
  208. s_bio[n] != '\r' && s_bio[n] != '\n' && n < 128; n++);
  209. s_bio[n] = '\0';
  210. xs *s_avatar = xs_dup(avatar);
  211. /* don't inline an empty avatar: create a real link */
  212. if (xs_startswith(s_avatar, "data:")) {
  213. xs_free(s_avatar);
  214. s_avatar = xs_fmt("%s/susie.png", srv_baseurl);
  215. }
  216. /* og properties */
  217. xs *es1 = encode_html(xs_dict_get(srv_config, "host"));
  218. xs *es2 = encode_html(xs_dict_get(snac->config, "name"));
  219. xs *es3 = encode_html(snac->uid);
  220. xs *es4 = encode_html(xs_dict_get(srv_config, "host"));
  221. xs *es5 = encode_html(s_bio);
  222. xs *es6 = encode_html(s_avatar);
  223. xs *s1 = xs_fmt(
  224. "<meta property=\"og:site_name\" content=\"%s\"/>\n"
  225. "<meta property=\"og:title\" content=\"%s (@%s@%s)\"/>\n"
  226. "<meta property=\"og:description\" content=\"%s\"/>\n"
  227. "<meta property=\"og:image\" content=\"%s\"/>\n"
  228. "<meta property=\"og:image:width\" content=\"300\"/>\n"
  229. "<meta property=\"og:image:height\" content=\"300\"/>\n",
  230. es1, es2, es3, es4, es5, es6);
  231. s = xs_str_cat(s, s1);
  232. }
  233. {
  234. xs *s1 = xs_fmt("<link rel=\"alternate\" type=\"application/rss+xml\" "
  235. "title=\"RSS\" href=\"%s.rss\" />\n", snac->actor); /* snac->actor is likely need to be URLEncoded. */
  236. s = xs_str_cat(s, s1);
  237. }
  238. s = xs_str_cat(s, "</head>\n<body>\n");
  239. /* top nav */
  240. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  241. {
  242. xs *s1;
  243. s1 = xs_fmt("<img src=\"%s\" class=\"snac-avatar\" alt=\"\"/>&nbsp;", avatar);
  244. s = xs_str_cat(s, s1);
  245. }
  246. {
  247. xs *s1;
  248. if (local)
  249. s1 = xs_fmt(
  250. "<a href=\"%s.rss\">%s</a> - "
  251. "<a href=\"%s/admin\" rel=\"nofollow\">%s</a></nav>\n",
  252. snac->actor, L("RSS"),
  253. snac->actor, L("private"));
  254. else {
  255. xs *n_list = notify_list(snac, 1);
  256. int n_len = xs_list_len(n_list);
  257. xs *n_str = NULL;
  258. /* show the number of new notifications, if there are any */
  259. if (n_len)
  260. n_str = xs_fmt("<sup style=\"background-color: red; "
  261. "color: white;\"> %d </sup> ", n_len);
  262. else
  263. n_str = xs_str_new("");
  264. s1 = xs_fmt(
  265. "<a href=\"%s\">%s</a> - "
  266. "<a href=\"%s/admin\">%s</a> - "
  267. "<a href=\"%s/notifications\">%s</a>%s - "
  268. "<a href=\"%s/people\">%s</a></nav>\n",
  269. snac->actor, L("public"),
  270. snac->actor, L("private"),
  271. snac->actor, L("notifications"), n_str,
  272. snac->actor, L("people"));
  273. }
  274. s = xs_str_cat(s, s1);
  275. }
  276. /* user info */
  277. {
  278. char *_tmpl =
  279. "<div class=\"h-card snac-top-user\">\n"
  280. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  281. "<p class=\"snac-top-user-id\">@%s@%s</p>\n";
  282. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  283. xs *es2 = encode_html(xs_dict_get(snac->config, "uid"));
  284. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  285. xs *s1 = xs_fmt(_tmpl, es1, es2, es3);
  286. s = xs_str_cat(s, s1);
  287. if (local) {
  288. xs *es1 = encode_html(xs_dict_get(snac->config, "bio"));
  289. xs *bio1 = not_really_markdown(es1, NULL);
  290. xs *tags = xs_list_new();
  291. xs *bio2 = process_tags(snac, bio1, &tags);
  292. xs *s1 = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio2);
  293. s = xs_str_cat(s, s1);
  294. }
  295. s = xs_str_cat(s, "</div>\n");
  296. }
  297. return s;
  298. }
  299. d_char *html_top_controls(snac *snac, d_char *s)
  300. /* generates the top controls */
  301. {
  302. char *_tmpl =
  303. "<div class=\"snac-top-controls\">\n"
  304. "<div class=\"snac-note\">\n"
  305. "<form autocomplete=\"off\" method=\"post\" "
  306. "action=\"%s/admin/note\" enctype=\"multipart/form-data\">\n"
  307. "<textarea class=\"snac-textarea\" name=\"content\" "
  308. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  309. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  310. "<p>%s: <input type=\"checkbox\" name=\"sensitive\"> "
  311. "<input type=\"text\" name=\"summary\" placeholder=\"%s\">\n"
  312. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  313. "<details><summary>%s</summary>\n" /** attach **/
  314. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  315. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  316. "</details>\n"
  317. "<p>"
  318. "<details><summary>%s</summary>\n" /** poll **/
  319. "<p>%s:<br>\n"
  320. "<textarea class=\"snac-textarea\" name=\"poll_options\" "
  321. "rows=\"6\" wrap=\"virtual\"></textarea>\n"
  322. "<p><select name=\"poll_multiple\">\n"
  323. "<option value=\"off\">%s</option>\n"
  324. "<option value=\"on\">%s</option>\n"
  325. "</select>\n"
  326. "<select name=\"poll_end_secs\" id=\"poll_end_secs\">\n"
  327. "<option value=\"300\">%s</option>\n"
  328. "<option value=\"3600\">%s</option>\n"
  329. "<option value=\"86400\">%s</option>\n"
  330. "</select>\n"
  331. "</details>\n"
  332. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  333. "</form><p>\n"
  334. "</div>\n"
  335. "<div class=\"snac-top-controls-more\">\n"
  336. "<details><summary>%s</summary>\n"
  337. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n" /** follow **/
  338. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  339. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  340. "</form><p>\n"
  341. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n" /** boost **/
  342. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  343. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  344. "</form><p>\n"
  345. "<details><summary>%s</summary>\n"
  346. "<div class=\"snac-user-setup\">\n" /** user setup **/
  347. "<form autocomplete=\"off\" method=\"post\" "
  348. "action=\"%s/admin/user-setup\" enctype=\"multipart/form-data\">\n"
  349. "<p>%s:<br>\n"
  350. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  351. "<p>%s: <input type=\"file\" name=\"avatar_file\"></p>\n"
  352. "<p>%s:<br>\n"
  353. "<textarea name=\"bio\" cols=\"40\" rows=\"4\">%s</textarea></p>\n"
  354. "<p><input type=\"checkbox\" name=\"cw\" id=\"cw\" %s>\n"
  355. "<label for=\"cw\">%s</label></p>\n"
  356. "<p>%s:<br>\n"
  357. "<input type=\"text\" name=\"email\" value=\"%s\"></p>\n"
  358. "<p>%s:<br>\n"
  359. "<input type=\"text\" name=\"telegram_bot\" placeholder=\"Bot API key\" value=\"%s\"> "
  360. "<input type=\"text\" name=\"telegram_chat_id\" placeholder=\"Chat id\" value=\"%s\"></p>\n"
  361. "<p>%s:<br>\n"
  362. "<input type=\"number\" name=\"purge_days\" value=\"%s\"></p>\n"
  363. "<p><input type=\"checkbox\" name=\"drop_dm_from_unknown\" id=\"drop_dm_from_unknown\" %s>\n"
  364. "<label for=\"drop_dm_from_unknown\">%s</label></p>\n"
  365. "<p><input type=\"checkbox\" name=\"bot\" id=\"bot\" %s>\n"
  366. "<label for=\"bot\">%s</label></p>\n"
  367. "<p>%s:<br>\n"
  368. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  369. "<p>%s:<br>\n"
  370. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  371. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  372. "</form>\n"
  373. "</div>\n"
  374. "</details>\n"
  375. "</details>\n"
  376. "</div>\n"
  377. "</div>\n";
  378. const char *email = "[disabled by admin]";
  379. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  380. email = xs_dict_get(snac->config_o, "email");
  381. if (xs_is_null(email)) {
  382. email = xs_dict_get(snac->config, "email");
  383. if (xs_is_null(email))
  384. email = "";
  385. }
  386. }
  387. char *cw = xs_dict_get(snac->config, "cw");
  388. if (xs_is_null(cw))
  389. cw = "";
  390. char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
  391. if (xs_is_null(telegram_bot))
  392. telegram_bot = "";
  393. char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  394. if (xs_is_null(telegram_chat_id))
  395. telegram_chat_id = "";
  396. const char *purge_days = xs_dict_get(snac->config, "purge_days");
  397. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  398. purge_days = xs_number_str(purge_days);
  399. else
  400. purge_days = "0";
  401. const char *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
  402. const char *bot = xs_dict_get(snac->config, "bot");
  403. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  404. xs *es2 = encode_html(xs_dict_get(snac->config, "bio"));
  405. xs *es3 = encode_html(email);
  406. xs *es4 = encode_html(telegram_bot);
  407. xs *es5 = encode_html(telegram_chat_id);
  408. xs *es6 = encode_html(purge_days);
  409. xs *s1 = xs_fmt(_tmpl,
  410. snac->actor,
  411. L("Sensitive content"),
  412. L("Sensitive content description"),
  413. L("Only for mentioned people"),
  414. L("Attach..."),
  415. L("File"),
  416. L("File description"),
  417. L("Poll..."),
  418. L("Poll options (one per line, up to 8)"),
  419. L("One choice"),
  420. L("Multiple choices"),
  421. L("End in 5 minutes"),
  422. L("End in 1 hour"),
  423. L("End in 1 day"),
  424. L("Post"),
  425. L("More options..."),
  426. snac->actor,
  427. L("Follow"), L("(by URL or user@host)"),
  428. snac->actor,
  429. L("Boost"), L("(by URL)"),
  430. L("User setup..."),
  431. snac->actor,
  432. L("Display name"),
  433. es1,
  434. L("Avatar"),
  435. L("Bio"),
  436. es2,
  437. strcmp(cw, "open") == 0 ? "checked" : "",
  438. L("Always show sensitive content"),
  439. L("Email address for notifications"),
  440. es3,
  441. L("Telegram notifications (bot key and chat id)"),
  442. es4,
  443. es5,
  444. L("Maximum days to keep posts (0: server settings)"),
  445. es6,
  446. xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "",
  447. L("Drop direct messages from people you don't follow"),
  448. xs_type(bot) == XSTYPE_TRUE ? "checked" : "",
  449. L("This account is a bot"),
  450. L("New Password"),
  451. L("Repeat New Password"),
  452. L("Update user info")
  453. );
  454. s = xs_str_cat(s, s1);
  455. return s;
  456. }
  457. d_char *html_button(d_char *s, char *clss, char *label)
  458. {
  459. xs *s1 = xs_fmt(
  460. "<input type=\"submit\" name=\"action\" "
  461. "class=\"snac-btn-%s\" value=\"%s\">\n",
  462. clss, label);
  463. return xs_str_cat(s, s1);
  464. }
  465. xs_str *build_mentions(snac *snac, const xs_dict *msg)
  466. /* returns a string with the mentions in msg */
  467. {
  468. xs_str *s = xs_str_new(NULL);
  469. char *list = xs_dict_get(msg, "tag");
  470. char *v;
  471. while (xs_list_iter(&list, &v)) {
  472. char *type = xs_dict_get(v, "type");
  473. char *href = xs_dict_get(v, "href");
  474. char *name = xs_dict_get(v, "name");
  475. if (type && strcmp(type, "Mention") == 0 &&
  476. href && strcmp(href, snac->actor) != 0 && name) {
  477. xs *s1 = NULL;
  478. if (name[0] != '@') {
  479. s1 = xs_fmt("@%s", name);
  480. name = s1;
  481. }
  482. xs *l = xs_split(name, "@");
  483. /* is it a name without a host? */
  484. if (xs_list_len(l) < 3) {
  485. /* split the href and pick the host name LIKE AN ANIMAL */
  486. /* would be better to query the webfinger but *won't do that* here */
  487. xs *l2 = xs_split(href, "/");
  488. if (xs_list_len(l2) >= 3) {
  489. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  490. s = xs_str_cat(s, s1);
  491. }
  492. }
  493. else {
  494. s = xs_str_cat(s, name);
  495. s = xs_str_cat(s, " ");
  496. }
  497. }
  498. }
  499. if (*s) {
  500. xs *s1 = s;
  501. s = xs_fmt("\n\n\nCC: %s", s1);
  502. }
  503. return s;
  504. }
  505. xs_str *html_entry_controls(snac *snac, xs_str *os, const xs_dict *msg, const char *md5)
  506. {
  507. char *id = xs_dict_get(msg, "id");
  508. char *actor = xs_dict_get(msg, "attributedTo");
  509. xs *likes = object_likes(id);
  510. xs *boosts = object_announces(id);
  511. xs *s = xs_str_new(NULL);
  512. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  513. {
  514. xs *s1 = xs_fmt(
  515. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n"
  516. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  517. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  518. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  519. "\n",
  520. snac->actor, id, actor, md5
  521. );
  522. s = xs_str_cat(s, s1);
  523. }
  524. if (!xs_startswith(id, snac->actor)) {
  525. if (xs_list_in(likes, snac->md5) == -1) {
  526. /* not already liked; add button */
  527. s = html_button(s, "like", L("Like"));
  528. }
  529. }
  530. else {
  531. if (is_pinned(snac, id))
  532. s = html_button(s, "unpin", L("Unpin"));
  533. else
  534. s = html_button(s, "pin", L("Pin"));
  535. }
  536. if (is_msg_public(snac, msg)) {
  537. if (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) {
  538. /* not already boosted or us; add button */
  539. s = html_button(s, "boost", L("Boost"));
  540. }
  541. }
  542. if (strcmp(actor, snac->actor) != 0) {
  543. /* controls for other actors than this one */
  544. if (following_check(snac, actor)) {
  545. s = html_button(s, "unfollow", L("Unfollow"));
  546. }
  547. else {
  548. s = html_button(s, "follow", L("Follow"));
  549. }
  550. s = html_button(s, "mute", L("MUTE"));
  551. }
  552. s = html_button(s, "delete", L("Delete"));
  553. s = html_button(s, "hide", L("Hide"));
  554. s = xs_str_cat(s, "</form>\n");
  555. const char *prev_src1 = xs_dict_get(msg, "sourceContent");
  556. if (!xs_is_null(prev_src1) && strcmp(actor, snac->actor) == 0) { /** edit **/
  557. xs *prev_src = xs_replace(prev_src1, "<", "&lt;");
  558. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  559. const char *summary = xs_dict_get(msg, "summary");
  560. /* post can be edited */
  561. xs *s1 = xs_fmt(
  562. "<p><details><summary>%s</summary>\n"
  563. "<p><div class=\"snac-note\" id=\"%s_edit\">\n"
  564. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/note\" "
  565. "enctype=\"multipart/form-data\" id=\"%s_edit_form\">\n"
  566. "<textarea class=\"snac-textarea\" name=\"content\" "
  567. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  568. "<input type=\"hidden\" name=\"edit_id\" value=\"%s\">\n"
  569. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  570. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  571. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  572. "<details><summary>%s</summary>\n"
  573. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  574. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  575. "</details>\n"
  576. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  577. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  578. "</form><p></div>\n"
  579. "</details><p>"
  580. "\n",
  581. L("Edit..."),
  582. md5,
  583. snac->actor, md5,
  584. prev_src,
  585. id,
  586. L("Sensitive content"),
  587. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  588. L("Sensitive content description"),
  589. xs_is_null(summary) ? "" : summary,
  590. L("Only for mentioned people"),
  591. L("Attach..."),
  592. L("File"),
  593. L("File description"),
  594. md5,
  595. L("Post")
  596. );
  597. s = xs_str_cat(s, s1);
  598. }
  599. { /** reply **/
  600. /* the post textarea */
  601. xs *ct = build_mentions(snac, msg);
  602. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  603. const char *summary = xs_dict_get(msg, "summary");
  604. xs *s1 = xs_fmt(
  605. "<p><details><summary>%s</summary>\n"
  606. "<p><div class=\"snac-note\" id=\"%s_reply\">\n"
  607. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/note\" "
  608. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  609. "<textarea class=\"snac-textarea\" name=\"content\" "
  610. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  611. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  612. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  613. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  614. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  615. "<details><summary>%s</summary>\n"
  616. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  617. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  618. "</details>\n"
  619. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  620. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  621. "</form><p></div>\n"
  622. "</details><p>"
  623. "\n",
  624. L("Reply..."),
  625. md5,
  626. snac->actor, md5,
  627. ct,
  628. id,
  629. L("Sensitive content"),
  630. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  631. L("Sensitive content description"),
  632. xs_is_null(summary) ? "" : summary,
  633. L("Only for mentioned people"),
  634. L("Attach..."),
  635. L("File"),
  636. L("File description"),
  637. md5,
  638. L("Post")
  639. );
  640. s = xs_str_cat(s, s1);
  641. }
  642. s = xs_str_cat(s, "</div>\n");
  643. return xs_str_cat(os, s);
  644. }
  645. xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
  646. int level, const char *md5, int hide_children)
  647. {
  648. char *id = xs_dict_get(msg, "id");
  649. char *type = xs_dict_get(msg, "type");
  650. char *actor;
  651. int sensitive = 0;
  652. char *v;
  653. xs *boosts = NULL;
  654. /* do not show non-public messages in the public timeline */
  655. if (local && !is_msg_public(snac, msg))
  656. return os;
  657. /* hidden? do nothing more for this conversation */
  658. if (is_hidden(snac, id))
  659. return os;
  660. xs *s = xs_str_new("<div>\n");
  661. {
  662. xs *s1 = xs_fmt("<a name=\"%s_entry\"></a>\n", md5);
  663. s = xs_str_cat(s, s1);
  664. }
  665. if (strcmp(type, "Follow") == 0) {
  666. s = xs_str_cat(s, "<div class=\"snac-post\">\n<div class=\"snac-post-header\">\n");
  667. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  668. s = xs_str_cat(s, s1);
  669. s = html_msg_icon(snac, s, msg);
  670. s = xs_str_cat(s, "</div>\n</div>\n");
  671. return xs_str_cat(os, s);
  672. }
  673. else
  674. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0 && strcmp(type, "Page") != 0) {
  675. /* skip oddities */
  676. return os;
  677. }
  678. /* ignore notes with "name", as they are votes to Questions */
  679. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  680. return os;
  681. /* bring the main actor */
  682. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  683. return os;
  684. /* ignore muted morons immediately */
  685. if (is_muted(snac, actor))
  686. return os;
  687. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  688. return os;
  689. if (level == 0)
  690. s = xs_str_cat(s, "<div class=\"snac-post\">\n"); /** **/
  691. else
  692. s = xs_str_cat(s, "<div class=\"snac-child\">\n"); /** **/
  693. s = xs_str_cat(s, "<div class=\"snac-post-header\">\n<div class=\"snac-score\">"); /** **/
  694. if (is_pinned(snac, id)) {
  695. /* add a pin emoji */
  696. xs *f = xs_fmt("<span title=\"%s\"> &#128204; </span>", L("Pinned"));
  697. s = xs_str_cat(s, f);
  698. }
  699. if (strcmp(type, "Question") == 0) {
  700. /* add the ballot box emoji */
  701. xs *f = xs_fmt("<span title=\"%s\"> &#128499; </span>", L("Poll"));
  702. s = xs_str_cat(s, f);
  703. if (was_question_voted(snac, id)) {
  704. /* add a check to show this poll was voted */
  705. xs *f2 = xs_fmt("<span title=\"%s\"> &#10003; </span>", L("Voted"));
  706. s = xs_str_cat(s, f2);
  707. }
  708. }
  709. /* if this is our post, add the score */
  710. if (xs_startswith(id, snac->actor)) {
  711. int n_likes = object_likes_len(id);
  712. int n_boosts = object_announces_len(id);
  713. /* alternate emojis: %d &#128077; %d &#128257; */
  714. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  715. s = xs_str_cat(s, s1);
  716. }
  717. s = xs_str_cat(s, "</div>\n");
  718. if (boosts == NULL)
  719. boosts = object_announces(id);
  720. if (xs_list_len(boosts)) {
  721. /* if somebody boosted this, show as origin */
  722. char *p = xs_list_get(boosts, -1);
  723. xs *actor_r = NULL;
  724. if (xs_list_in(boosts, snac->md5) != -1) {
  725. /* we boosted this */
  726. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  727. xs *s1 = xs_fmt(
  728. "<div class=\"snac-origin\">"
  729. "<a href=\"%s\">%s</a> %s</a></div>",
  730. snac->actor, es1, L("boosted")
  731. );
  732. s = xs_str_cat(s, s1);
  733. }
  734. else
  735. if (valid_status(object_get_by_md5(p, &actor_r))) {
  736. xs *name = actor_name(actor_r);
  737. if (!xs_is_null(name)) {
  738. xs *s1 = xs_fmt(
  739. "<div class=\"snac-origin\">"
  740. "<a href=\"%s\">%s</a> %s</div>\n",
  741. xs_dict_get(actor_r, "id"),
  742. name,
  743. L("boosted")
  744. );
  745. s = xs_str_cat(s, s1);
  746. }
  747. }
  748. }
  749. else
  750. if (strcmp(type, "Note") == 0) {
  751. if (level == 0) {
  752. /* is the parent not here? */
  753. char *parent = xs_dict_get(msg, "inReplyTo");
  754. if (!xs_is_null(parent) && *parent && !timeline_here(snac, parent)) {
  755. xs *s1 = xs_fmt(
  756. "<div class=\"snac-origin\">%s "
  757. "<a href=\"%s\">»</a></div>\n",
  758. L("in reply to"), parent
  759. );
  760. s = xs_str_cat(s, s1);
  761. }
  762. }
  763. }
  764. s = html_msg_icon(snac, s, msg);
  765. /* add the content */
  766. s = xs_str_cat(s, "</div>\n<div class=\"e-content snac-content\">\n"); /** **/
  767. if (!xs_is_null(v = xs_dict_get(msg, "name"))) {
  768. xs *es1 = encode_html(v);
  769. xs *s1 = xs_fmt("<h3 class=\"snac-entry-title\">%s</h3>\n", es1);
  770. s = xs_str_cat(s, s1);
  771. }
  772. /* is it sensitive? */
  773. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  774. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  775. v = "...";
  776. /* only show it when not in the public timeline and the config setting is "open" */
  777. char *cw = xs_dict_get(snac->config, "cw");
  778. if (xs_is_null(cw) || local)
  779. cw = "";
  780. xs *es1 = encode_html(v);
  781. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, es1, L("SENSITIVE CONTENT"));
  782. s = xs_str_cat(s, s1);
  783. sensitive = 1;
  784. }
  785. #if 0
  786. {
  787. xs *md5 = xs_md5_hex(id, strlen(id));
  788. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  789. s = xs_str_cat(s, s1);
  790. }
  791. #endif
  792. {
  793. const char *content = xs_dict_get(msg, "content");
  794. xs *c = sanitize(xs_is_null(content) ? "" : content);
  795. char *p, *v;
  796. /* do some tweaks to the content */
  797. c = xs_replace_i(c, "\r", "");
  798. while (xs_endswith(c, "<br><br>"))
  799. c = xs_crop_i(c, 0, -4);
  800. c = xs_replace_i(c, "<br><br>", "<p>");
  801. if (!xs_startswith(c, "<p>")) {
  802. xs *s1 = c;
  803. c = xs_fmt("<p>%s</p>", s1);
  804. }
  805. /* replace the :shortnames: */
  806. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  807. /* iterate the tags */
  808. while (xs_list_iter(&p, &v)) {
  809. char *t = xs_dict_get(v, "type");
  810. if (t && strcmp(t, "Emoji") == 0) {
  811. char *n = xs_dict_get(v, "name");
  812. char *i = xs_dict_get(v, "icon");
  813. if (n && i) {
  814. char *u = xs_dict_get(i, "url");
  815. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\" "
  816. "loading=\"lazy\" title=\"%s\"/>", u, n);
  817. c = xs_replace_i(c, n, img);
  818. }
  819. }
  820. }
  821. }
  822. if (strcmp(type, "Question") == 0) { /** question content **/
  823. xs_list *oo = xs_dict_get(msg, "oneOf");
  824. xs_list *ao = xs_dict_get(msg, "anyOf");
  825. xs_list *p;
  826. xs_dict *v;
  827. int closed = 0;
  828. if (xs_dict_get(msg, "closed"))
  829. closed = 2;
  830. else
  831. if (xs_startswith(id, snac->actor))
  832. closed = 1; /* we questioned; closed for us */
  833. else
  834. if (was_question_voted(snac, id))
  835. closed = 1; /* we already voted; closed for us */
  836. /* get the appropriate list of options */
  837. p = oo != NULL ? oo : ao;
  838. if (closed) {
  839. /* closed poll */
  840. c = xs_str_cat(c, "<table class=\"snac-poll-result\">\n");
  841. while (xs_list_iter(&p, &v)) {
  842. const char *name = xs_dict_get(v, "name");
  843. const xs_dict *replies = xs_dict_get(v, "replies");
  844. if (name && replies) {
  845. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  846. xs *es1 = encode_html(name);
  847. xs *l = xs_fmt("<tr><td>%s:</td><td>%d</td></tr>\n", es1, nr);
  848. c = xs_str_cat(c, l);
  849. }
  850. }
  851. c = xs_str_cat(c, "</table>\n");
  852. }
  853. else {
  854. /* poll still active */
  855. xs *s1 = xs_fmt("<div class=\"snac-poll-form\">\n"
  856. "<form autocomplete=\"off\" "
  857. "method=\"post\" action=\"%s/admin/vote\">\n"
  858. "<input type=\"hidden\" name=\"actor\" value= \"%s\">\n"
  859. "<input type=\"hidden\" name=\"irt\" value=\"%s\">\n",
  860. snac->actor, actor, id);
  861. while (xs_list_iter(&p, &v)) {
  862. const char *name = xs_dict_get(v, "name");
  863. if (name) {
  864. xs *es1 = encode_html(name);
  865. xs *opt = xs_fmt("<input type=\"%s\""
  866. " id=\"%s\" value=\"%s\" name=\"question\"> %s<br>\n",
  867. !xs_is_null(oo) ? "radio" : "checkbox",
  868. es1, es1, es1);
  869. s1 = xs_str_cat(s1, opt);
  870. }
  871. }
  872. xs *s2 = xs_fmt("<p><input type=\"submit\" "
  873. "class=\"button\" value=\"%s\">\n</form>\n</div>\n\n", L("Vote"));
  874. s1 = xs_str_cat(s1, s2);
  875. c = xs_str_cat(c, s1);
  876. }
  877. /* if it's *really* closed, say it */
  878. if (closed == 2) {
  879. xs *s1 = xs_fmt("<p>%s</p>\n", L("Closed"));
  880. c = xs_str_cat(c, s1);
  881. }
  882. else {
  883. /* show when the poll closes */
  884. const char *end_time = xs_dict_get(msg, "endTime");
  885. if (!xs_is_null(end_time)) {
  886. time_t t0 = time(NULL);
  887. time_t t1 = xs_parse_iso_date(end_time, 0);
  888. if (t1 > 0 && t1 > t0) {
  889. time_t diff_time = t1 - t0;
  890. xs *tf = xs_str_time_diff(diff_time);
  891. char *p = tf;
  892. /* skip leading zeros */
  893. for (; *p == '0' || *p == ':'; p++);
  894. xs *es1 = encode_html(p);
  895. xs *s1 = xs_fmt("<p>%s %s</p>", L("Closes in"), es1);
  896. c = xs_str_cat(c, s1);
  897. }
  898. }
  899. }
  900. }
  901. s = xs_str_cat(s, c);
  902. }
  903. s = xs_str_cat(s, "\n");
  904. /* add the attachments */
  905. xs_list *attach = xs_dict_get(msg, "attachment");
  906. xs_dict *image = xs_dict_get(msg, "image");
  907. if (!xs_is_null(attach) || !xs_is_null(image)) { /** **/
  908. char *v;
  909. /* make custom css for attachments easier */
  910. s = xs_str_cat(s, "<p class=\"snac-content-attachments\">\n");
  911. while (xs_list_iter(&attach, &v)) {
  912. const char *t = xs_dict_get(v, "mediaType");
  913. if (xs_is_null(t))
  914. t = xs_dict_get(v, "type");
  915. if (xs_is_null(t))
  916. continue;
  917. if (xs_startswith(t, "image/")) {
  918. char *url = xs_dict_get(v, "url");
  919. char *name = xs_dict_get(v, "name");
  920. if (url != NULL) {
  921. if (xs_is_null(name))
  922. name = "";
  923. xs *es1 = encode_html(name);
  924. xs *s1 = xs_fmt(
  925. "<a href=\"%s\" target=\"_blank\">"
  926. "<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
  927. url, url, es1, es1);
  928. s = xs_str_cat(s, s1);
  929. }
  930. }
  931. else
  932. if (xs_startswith(t, "video/")) {
  933. char *url = xs_dict_get(v, "url");
  934. char *name = xs_dict_get(v, "name");
  935. if (url != NULL) {
  936. if (xs_is_null(name))
  937. name = "No description";
  938. xs *es1 = encode_html(name);
  939. xs *s1 = xs_fmt("<video style=\"width: 100%\" class=\"snac-embedded-video\" "
  940. "controls src=\"%s\">Video: "
  941. "<a href=\"%s\">%s</a></video>\n", url, url, es1);
  942. s = xs_str_cat(s, s1);
  943. }
  944. }
  945. else
  946. if (xs_startswith(t, "audio/")) {
  947. char *url = xs_dict_get(v, "url");
  948. char *name = xs_dict_get(v, "name");
  949. if (url != NULL) {
  950. if (xs_is_null(name))
  951. name = "No description";
  952. xs *es1 = encode_html(name);
  953. xs *s1 = xs_fmt("<audio style=\"width: 100%\" class=\"snac-embedded-audio\" "
  954. "controls src=\"%s\">Audio: "
  955. "<a href=\"%s\">%s</a></audio>\n", url, url, es1);
  956. s = xs_str_cat(s, s1);
  957. }
  958. }
  959. else
  960. if (strcmp(t, "Link") == 0) {
  961. const char *url = xs_dict_get(v, "href");
  962. if (!xs_is_null(url)) {
  963. xs *es1 = encode_html(url);
  964. xs *s1 = xs_fmt("<p><a href=\"%s\">%s</a></p>\n", url, es1);
  965. s = xs_str_cat(s, s1);
  966. }
  967. }
  968. else {
  969. char *url = xs_dict_get(v, "url");
  970. char *name = xs_dict_get(v, "name");
  971. if (url != NULL) {
  972. if (xs_is_null(name))
  973. name = "No description";
  974. xs *es1 = encode_html(name);
  975. xs *s1 = xs_fmt("<a href=\"%s\">Attachment: %s</a>", url, es1);
  976. s = xs_str_cat(s, s1);
  977. }
  978. }
  979. }
  980. /* if the message has an image, add it */
  981. if (!xs_is_null(image)) {
  982. if (!xs_is_null(image = xs_dict_get(image, "url"))) {
  983. xs *es1;
  984. if (!xs_is_null(v = xs_dict_get(msg, "name")))
  985. es1 = encode_html(v);
  986. else
  987. es1 = xs_str_new(NULL);
  988. xs *s1 = xs_fmt(
  989. "<a href=\"%s\" target=\"_blank\">"
  990. "<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
  991. image, image, es1, es1);
  992. s = xs_str_cat(s, s1);
  993. }
  994. }
  995. s = xs_str_cat(s, "</p>\n");
  996. }
  997. if (sensitive)
  998. s = xs_str_cat(s, "</details><p>\n");
  999. s = xs_str_cat(s, "</div>\n");
  1000. /** controls **/
  1001. if (!local)
  1002. s = html_entry_controls(snac, s, msg, md5);
  1003. /** children **/
  1004. if (!hide_children) {
  1005. xs *children = object_children(id);
  1006. int left = xs_list_len(children);
  1007. if (left) {
  1008. char *p, *cmd5;
  1009. int older_open = 0;
  1010. xs *ss = xs_str_new(NULL);
  1011. int n_children = 0;
  1012. ss = xs_str_cat(ss, "<details open><summary>...</summary><p>\n");
  1013. if (level < 4)
  1014. ss = xs_str_cat(ss, "<div class=\"snac-children\">\n");
  1015. else
  1016. ss = xs_str_cat(ss, "<div>\n");
  1017. if (left > 3) {
  1018. xs *s1 = xs_fmt("<details><summary>%s</summary>\n", L("Older..."));
  1019. ss = xs_str_cat(ss, s1);
  1020. older_open = 1;
  1021. }
  1022. p = children;
  1023. while (xs_list_iter(&p, &cmd5)) {
  1024. xs *chd = NULL;
  1025. timeline_get_by_md5(snac, cmd5, &chd);
  1026. if (older_open && left <= 3) {
  1027. ss = xs_str_cat(ss, "</details>\n");
  1028. older_open = 0;
  1029. }
  1030. if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
  1031. ss = html_entry(snac, ss, chd, local, level + 1, cmd5, hide_children);
  1032. n_children++;
  1033. }
  1034. else
  1035. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", cmd5));
  1036. left--;
  1037. }
  1038. if (older_open)
  1039. ss = xs_str_cat(ss, "</details>\n");
  1040. ss = xs_str_cat(ss, "</div>\n");
  1041. ss = xs_str_cat(ss, "</details>\n");
  1042. if (n_children)
  1043. s = xs_str_cat(s, ss);
  1044. }
  1045. }
  1046. s = xs_str_cat(s, "</div>\n</div>\n");
  1047. return xs_str_cat(os, s);
  1048. }
  1049. xs_str *html_user_footer(xs_str *s)
  1050. {
  1051. xs *s1 = xs_fmt(
  1052. "<div class=\"snac-footer\">\n"
  1053. "<a href=\"%s\">%s</a> - "
  1054. "powered by <a href=\"%s\">"
  1055. "<abbr title=\"Social Networks Are Crap\">snac</abbr></a></div>\n",
  1056. srv_baseurl,
  1057. L("about this site"),
  1058. WHAT_IS_SNAC_URL
  1059. );
  1060. return xs_str_cat(s, s1);
  1061. }
  1062. xs_str *html_timeline(snac *snac, const xs_list *list, int local, int skip, int show, int show_more)
  1063. /* returns the HTML for the timeline */
  1064. {
  1065. xs_str *s = xs_str_new(NULL);
  1066. xs_list *p = (xs_list *)list;
  1067. char *v;
  1068. double t = ftime();
  1069. s = html_user_header(snac, s, local);
  1070. if (!local)
  1071. s = html_top_controls(snac, s);
  1072. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  1073. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1074. while (xs_list_iter(&p, &v)) {
  1075. xs *msg = NULL;
  1076. if (!valid_status(timeline_get_by_md5(snac, v, &msg)))
  1077. continue;
  1078. s = html_entry(snac, s, msg, local, 0, v, 0);
  1079. }
  1080. s = xs_str_cat(s, "</div>\n");
  1081. if (local) {
  1082. xs *s1 = xs_fmt(
  1083. "<div class=\"snac-history\">\n"
  1084. "<p class=\"snac-history-title\">%s</p><ul>\n",
  1085. L("History")
  1086. );
  1087. s = xs_str_cat(s, s1);
  1088. xs *list = history_list(snac);
  1089. char *p, *v;
  1090. p = list;
  1091. while (xs_list_iter(&p, &v)) {
  1092. xs *fn = xs_replace(v, ".html", "");
  1093. xs *s1 = xs_fmt(
  1094. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  1095. snac->actor, v, fn);
  1096. s = xs_str_cat(s, s1);
  1097. }
  1098. s = xs_str_cat(s, "</ul></div>\n");
  1099. }
  1100. {
  1101. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  1102. s = xs_str_cat(s, s1);
  1103. }
  1104. if (show_more) {
  1105. xs *s1 = xs_fmt(
  1106. "<p>"
  1107. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  1108. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("Load more..."));
  1109. s = xs_str_cat(s, s1);
  1110. }
  1111. s = html_user_footer(s);
  1112. s = xs_str_cat(s, "</body>\n</html>\n");
  1113. return s;
  1114. }
  1115. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  1116. {
  1117. xs *s = xs_str_new(NULL);
  1118. xs *es1 = encode_html(header);
  1119. xs *h = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", es1);
  1120. char *p, *actor_id;
  1121. s = xs_str_cat(s, h);
  1122. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1123. p = list;
  1124. while (xs_list_iter(&p, &actor_id)) {
  1125. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  1126. xs *actor = NULL;
  1127. if (valid_status(actor_get(snac, actor_id, &actor))) {
  1128. s = xs_str_cat(s, "<div class=\"snac-post\">\n<div class=\"snac-post-header\">\n");
  1129. s = html_actor_icon(s, actor, xs_dict_get(actor, "published"), NULL, NULL, 0);
  1130. s = xs_str_cat(s, "</div>\n");
  1131. /* content (user bio) */
  1132. char *c = xs_dict_get(actor, "summary");
  1133. if (!xs_is_null(c)) {
  1134. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  1135. xs *sc = sanitize(c);
  1136. if (xs_startswith(sc, "<p>"))
  1137. s = xs_str_cat(s, sc);
  1138. else {
  1139. xs *s1 = xs_fmt("<p>%s</p>", sc);
  1140. s = xs_str_cat(s, s1);
  1141. }
  1142. s = xs_str_cat(s, "</div>\n");
  1143. }
  1144. /* buttons */
  1145. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  1146. xs *s1 = xs_fmt(
  1147. "<p><form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n"
  1148. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  1149. "<input type=\"hidden\" name=\"actor-form\" value=\"yes\">\n",
  1150. snac->actor, actor_id
  1151. );
  1152. s = xs_str_cat(s, s1);
  1153. if (following_check(snac, actor_id))
  1154. s = html_button(s, "unfollow", L("Unfollow"));
  1155. else {
  1156. s = html_button(s, "follow", L("Follow"));
  1157. if (follower_check(snac, actor_id))
  1158. s = html_button(s, "delete", L("Delete"));
  1159. }
  1160. if (is_muted(snac, actor_id))
  1161. s = html_button(s, "unmute", L("Unmute"));
  1162. else
  1163. s = html_button(s, "mute", L("MUTE"));
  1164. s = xs_str_cat(s, "</form>\n");
  1165. /* the post textarea */
  1166. xs *s2 = xs_fmt(
  1167. "<p><details><summary>%s</summary>\n"
  1168. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  1169. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/note\" "
  1170. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  1171. "<textarea class=\"snac-textarea\" name=\"content\" "
  1172. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  1173. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  1174. "<p><input type=\"file\" name=\"attach\">\n"
  1175. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  1176. "</form><p></div>\n"
  1177. "</details><p>\n",
  1178. L("Direct Message..."),
  1179. md5, t,
  1180. snac->actor, md5,
  1181. actor_id,
  1182. L("Post")
  1183. );
  1184. s = xs_str_cat(s, s2);
  1185. s = xs_str_cat(s, "</div>\n");
  1186. s = xs_str_cat(s, "</div>\n");
  1187. }
  1188. }
  1189. s = xs_str_cat(s, "</div>\n");
  1190. return xs_str_cat(os, s);
  1191. }
  1192. d_char *html_people(snac *snac)
  1193. {
  1194. d_char *s = xs_str_new(NULL);
  1195. xs *wing = following_list(snac);
  1196. xs *wers = follower_list(snac);
  1197. s = html_user_header(snac, s, 0);
  1198. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  1199. s = html_people_list(snac, s, wers, L("People that follow you"), "e");
  1200. s = html_user_footer(s);
  1201. s = xs_str_cat(s, "</body>\n</html>\n");
  1202. return s;
  1203. }
  1204. xs_str *html_notifications(snac *snac)
  1205. {
  1206. xs_str *s = xs_str_new(NULL);
  1207. xs *n_list = notify_list(snac, 0);
  1208. xs *n_time = notify_check_time(snac, 0);
  1209. xs_list *p = n_list;
  1210. xs_str *v;
  1211. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  1212. s = html_user_header(snac, s, 0);
  1213. xs *s1 = xs_fmt(
  1214. "<form autocomplete=\"off\" "
  1215. "method=\"post\" action=\"%s/admin/clear-notifications\" id=\"clear\">\n"
  1216. "<input type=\"submit\" class=\"snac-btn-like\" value=\"%s\">\n"
  1217. "</form><p>\n", snac->actor, L("Clear all"));
  1218. s = xs_str_cat(s, s1);
  1219. while (xs_list_iter(&p, &v)) {
  1220. xs *noti = notify_get(snac, v);
  1221. if (noti == NULL)
  1222. continue;
  1223. xs *obj = NULL;
  1224. const char *type = xs_dict_get(noti, "type");
  1225. const char *utype = xs_dict_get(noti, "utype");
  1226. const char *id = xs_dict_get(noti, "objid");
  1227. if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
  1228. continue;
  1229. if (is_hidden(snac, id))
  1230. continue;
  1231. const char *actor_id = xs_dict_get(noti, "actor");
  1232. xs *actor = NULL;
  1233. if (!valid_status(actor_get(snac, actor_id, &actor)))
  1234. continue;
  1235. xs *a_name = actor_name(actor);
  1236. if (strcmp(v, n_time) > 0) {
  1237. /* unseen notification */
  1238. if (stage == NHDR_NONE) {
  1239. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("New"));
  1240. s = xs_str_cat(s, s1);
  1241. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1242. stage = NHDR_NEW;
  1243. }
  1244. }
  1245. else {
  1246. /* already seen notification */
  1247. if (stage != NHDR_OLD) {
  1248. if (stage == NHDR_NEW)
  1249. s = xs_str_cat(s, "</div>\n");
  1250. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("Already seen"));
  1251. s = xs_str_cat(s, s1);
  1252. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1253. stage = NHDR_OLD;
  1254. }
  1255. }
  1256. const char *label = type;
  1257. if (strcmp(type, "Create") == 0)
  1258. label = L("Mention");
  1259. else
  1260. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1261. label = L("Finished poll");
  1262. else
  1263. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  1264. label = L("Unfollow");
  1265. xs *es1 = encode_html(label);
  1266. xs *s1 = xs_fmt("<div class=\"snac-post-with-desc\">\n"
  1267. "<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  1268. es1, actor_id, a_name);
  1269. s = xs_str_cat(s, s1);
  1270. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  1271. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  1272. s = html_actor_icon(s, actor, NULL, NULL, NULL, 0);
  1273. s = xs_str_cat(s, "</div>\n");
  1274. }
  1275. else {
  1276. xs *md5 = xs_md5_hex(id, strlen(id));
  1277. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  1278. }
  1279. s = xs_str_cat(s, "</div>\n");
  1280. }
  1281. if (stage == NHDR_NONE) {
  1282. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("None"));
  1283. s = xs_str_cat(s, s1);
  1284. }
  1285. else
  1286. s = xs_str_cat(s, "</div>\n");
  1287. s = html_user_footer(s);
  1288. s = xs_str_cat(s, "</body>\n</html>\n");
  1289. /* set the check time to now */
  1290. xs *dummy = notify_check_time(snac, 1);
  1291. dummy = xs_free(dummy);
  1292. timeline_touch(snac);
  1293. return s;
  1294. }
  1295. int html_get_handler(const xs_dict *req, const char *q_path,
  1296. char **body, int *b_size, char **ctype, xs_str **etag)
  1297. {
  1298. char *accept = xs_dict_get(req, "accept");
  1299. int status = 404;
  1300. snac snac;
  1301. xs *uid = NULL;
  1302. char *p_path;
  1303. int cache = 1;
  1304. int save = 1;
  1305. char *v;
  1306. xs *l = xs_split_n(q_path, "/", 2);
  1307. v = xs_list_get(l, 1);
  1308. if (xs_is_null(v)) {
  1309. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1310. return 404;
  1311. }
  1312. uid = xs_dup(v);
  1313. /* rss extension? */
  1314. if (xs_endswith(uid, ".rss")) {
  1315. uid = xs_crop_i(uid, 0, -4);
  1316. p_path = ".rss";
  1317. }
  1318. else
  1319. p_path = xs_list_get(l, 2);
  1320. if (!uid || !user_open(&snac, uid)) {
  1321. /* invalid user */
  1322. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1323. return 404;
  1324. }
  1325. /* return the RSS if requested by Accept header */
  1326. if (accept != NULL) {
  1327. if (xs_str_in(accept, "text/xml") != -1 ||
  1328. xs_str_in(accept, "application/rss+xml") != -1)
  1329. p_path = ".rss";
  1330. }
  1331. /* check if server config variable 'disable_cache' is set */
  1332. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1333. cache = 0;
  1334. int skip = 0;
  1335. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1336. char *q_vars = xs_dict_get(req, "q_vars");
  1337. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1338. skip = atoi(v), cache = 0, save = 0;
  1339. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1340. show = atoi(v), cache = 0, save = 0;
  1341. if (p_path == NULL) { /** public timeline **/
  1342. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1343. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1344. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1345. *body = history_get(&snac, h);
  1346. *b_size = strlen(*body);
  1347. status = 200;
  1348. }
  1349. else {
  1350. xs *list = timeline_list(&snac, "public", skip, show);
  1351. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1352. xs *pins = pinned_list(&snac);
  1353. pins = xs_list_cat(pins, list);
  1354. *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next));
  1355. *b_size = strlen(*body);
  1356. status = 200;
  1357. if (save)
  1358. history_add(&snac, h, *body, *b_size);
  1359. }
  1360. }
  1361. else
  1362. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  1363. if (!login(&snac, req)) {
  1364. *body = xs_dup(uid);
  1365. status = 401;
  1366. }
  1367. else {
  1368. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1369. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1370. *body = history_get(&snac, "timeline.html_");
  1371. *b_size = strlen(*body);
  1372. status = 200;
  1373. }
  1374. else {
  1375. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1376. xs *list = timeline_list(&snac, "private", skip, show);
  1377. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1378. xs *pins = pinned_list(&snac);
  1379. pins = xs_list_cat(pins, list);
  1380. *body = html_timeline(&snac, pins, 0, skip, show, xs_list_len(next));
  1381. *b_size = strlen(*body);
  1382. status = 200;
  1383. if (save)
  1384. history_add(&snac, "timeline.html_", *body, *b_size);
  1385. }
  1386. }
  1387. }
  1388. else
  1389. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  1390. if (!login(&snac, req)) {
  1391. *body = xs_dup(uid);
  1392. status = 401;
  1393. }
  1394. else {
  1395. *body = html_people(&snac);
  1396. *b_size = strlen(*body);
  1397. status = 200;
  1398. }
  1399. }
  1400. else
  1401. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  1402. if (!login(&snac, req)) {
  1403. *body = xs_dup(uid);
  1404. status = 401;
  1405. }
  1406. else {
  1407. *body = html_notifications(&snac);
  1408. *b_size = strlen(*body);
  1409. status = 200;
  1410. }
  1411. }
  1412. else
  1413. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  1414. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1415. xs *msg = NULL;
  1416. if (valid_status(object_get(id, &msg))) {
  1417. xs *md5 = xs_md5_hex(id, strlen(id));
  1418. xs *list = xs_list_new();
  1419. list = xs_list_append(list, md5);
  1420. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  1421. *b_size = strlen(*body);
  1422. status = 200;
  1423. }
  1424. }
  1425. else
  1426. if (xs_startswith(p_path, "s/")) { /** a static file **/
  1427. xs *l = xs_split(p_path, "/");
  1428. char *id = xs_list_get(l, 1);
  1429. int sz;
  1430. status = static_get(&snac, id, body, &sz,
  1431. xs_dict_get(req, "if-none-match"), etag);
  1432. if (valid_status(status)) {
  1433. *b_size = sz;
  1434. *ctype = xs_mime_by_ext(id);
  1435. }
  1436. }
  1437. else
  1438. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  1439. xs *l = xs_split(p_path, "/");
  1440. char *id = xs_list_get(l, 1);
  1441. if ((*body = history_get(&snac, id)) != NULL) {
  1442. *b_size = strlen(*body);
  1443. status = 200;
  1444. }
  1445. }
  1446. else
  1447. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  1448. d_char *rss;
  1449. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1450. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  1451. char *p, *v;
  1452. xs *es1 = encode_html(xs_dict_get(snac.config, "name"));
  1453. xs *es2 = encode_html(snac.uid);
  1454. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  1455. xs *es4 = encode_html(bio);
  1456. rss = xs_fmt(
  1457. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1458. "<rss version=\"0.91\">\n"
  1459. "<channel>\n"
  1460. "<title>%s (@%s@%s)</title>\n"
  1461. "<language>en</language>\n"
  1462. "<link>%s.rss</link>\n"
  1463. "<description>%s</description>\n",
  1464. es1,
  1465. es2,
  1466. es3,
  1467. snac.actor,
  1468. es4
  1469. );
  1470. p = elems;
  1471. while (xs_list_iter(&p, &v)) {
  1472. xs *msg = NULL;
  1473. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1474. continue;
  1475. char *id = xs_dict_get(msg, "id");
  1476. if (!xs_startswith(id, snac.actor))
  1477. continue;
  1478. xs *es1 = sanitize(xs_dict_get(msg, "content"));
  1479. xs *content = encode_html(es1);
  1480. xs *title = xs_str_new(NULL);
  1481. int i;
  1482. for (i = 0; content[i] && content[i] != '<' && content[i] != '&' && i < 40; i++)
  1483. title = xs_append_m(title, &content[i], 1);
  1484. xs *es11 = encode_html(title);
  1485. xs *es12 = encode_html(content);
  1486. xs *s = xs_fmt(
  1487. "<item>\n"
  1488. "<title>%s...</title>\n"
  1489. "<link>%s</link>\n"
  1490. "<description>%s</description>\n"
  1491. "</item>\n",
  1492. es11, id, es12
  1493. );
  1494. rss = xs_str_cat(rss, s);
  1495. }
  1496. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  1497. *body = rss;
  1498. *b_size = strlen(rss);
  1499. *ctype = "application/rss+xml; charset=utf-8";
  1500. status = 200;
  1501. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1502. }
  1503. else
  1504. status = 404;
  1505. user_free(&snac);
  1506. if (valid_status(status) && *ctype == NULL) {
  1507. *ctype = "text/html; charset=utf-8";
  1508. }
  1509. return status;
  1510. }
  1511. int html_post_handler(const xs_dict *req, const char *q_path,
  1512. char *payload, int p_size,
  1513. char **body, int *b_size, char **ctype)
  1514. {
  1515. (void)p_size;
  1516. (void)ctype;
  1517. int status = 0;
  1518. snac snac;
  1519. char *uid, *p_path;
  1520. xs_dict *p_vars;
  1521. xs *l = xs_split_n(q_path, "/", 2);
  1522. uid = xs_list_get(l, 1);
  1523. if (!uid || !user_open(&snac, uid)) {
  1524. /* invalid user */
  1525. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1526. return 404;
  1527. }
  1528. p_path = xs_list_get(l, 2);
  1529. /* all posts must be authenticated */
  1530. if (!login(&snac, req)) {
  1531. user_free(&snac);
  1532. *body = xs_dup(uid);
  1533. return 401;
  1534. }
  1535. p_vars = xs_dict_get(req, "p_vars");
  1536. #if 0
  1537. {
  1538. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  1539. printf("%s\n", j1);
  1540. }
  1541. #endif
  1542. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  1543. /* post note */
  1544. xs_str *content = xs_dict_get(p_vars, "content");
  1545. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1546. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1547. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1548. xs_str *to = xs_dict_get(p_vars, "to");
  1549. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1550. xs_str *summary = xs_dict_get(p_vars, "summary");
  1551. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1552. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1553. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1554. xs *attach_list = xs_list_new();
  1555. /* default alt text */
  1556. if (xs_is_null(alt_text))
  1557. alt_text = "";
  1558. /* is attach_url set? */
  1559. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  1560. xs *l = xs_list_new();
  1561. l = xs_list_append(l, attach_url);
  1562. l = xs_list_append(l, alt_text);
  1563. attach_list = xs_list_append(attach_list, l);
  1564. }
  1565. /* is attach_file set? */
  1566. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  1567. char *fn = xs_list_get(attach_file, 0);
  1568. if (*fn != '\0') {
  1569. char *ext = strrchr(fn, '.');
  1570. xs *hash = xs_md5_hex(fn, strlen(fn));
  1571. xs *id = xs_fmt("%s%s", hash, ext);
  1572. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1573. int fo = xs_number_get(xs_list_get(attach_file, 1));
  1574. int fs = xs_number_get(xs_list_get(attach_file, 2));
  1575. /* store */
  1576. static_put(&snac, id, payload + fo, fs);
  1577. xs *l = xs_list_new();
  1578. l = xs_list_append(l, url);
  1579. l = xs_list_append(l, alt_text);
  1580. attach_list = xs_list_append(attach_list, l);
  1581. }
  1582. }
  1583. if (content != NULL) {
  1584. xs *msg = NULL;
  1585. xs *c_msg = NULL;
  1586. xs *content_2 = xs_replace(content, "\r", "");
  1587. xs *poll_opts = NULL;
  1588. /* is there a valid set of poll options? */
  1589. const char *v = xs_dict_get(p_vars, "poll_options");
  1590. if (!xs_is_null(v) && *v) {
  1591. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  1592. poll_opts = xs_split(v2, "\n");
  1593. }
  1594. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  1595. /* get the rest of poll configuration */
  1596. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  1597. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  1598. int multiple = 0;
  1599. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  1600. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  1601. multiple = 1;
  1602. msg = msg_question(&snac, content_2, attach_list,
  1603. poll_opts, multiple, end_secs);
  1604. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  1605. }
  1606. else
  1607. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  1608. if (sensitive != NULL) {
  1609. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1610. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  1611. }
  1612. if (xs_is_null(edit_id)) {
  1613. /* new message */
  1614. c_msg = msg_create(&snac, msg);
  1615. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1616. }
  1617. else {
  1618. /* an edition of a previous message */
  1619. xs *p_msg = NULL;
  1620. if (valid_status(object_get(edit_id, &p_msg))) {
  1621. /* copy relevant fields from previous version */
  1622. char *fields[] = { "id", "context", "url", "published",
  1623. "to", "inReplyTo", NULL };
  1624. int n;
  1625. for (n = 0; fields[n]; n++) {
  1626. char *v = xs_dict_get(p_msg, fields[n]);
  1627. msg = xs_dict_set(msg, fields[n], v);
  1628. }
  1629. /* set the updated field */
  1630. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  1631. msg = xs_dict_set(msg, "updated", updated);
  1632. /* overwrite object, not updating the indexes */
  1633. object_add_ow(edit_id, msg);
  1634. /* update message */
  1635. c_msg = msg_update(&snac, msg);
  1636. }
  1637. else
  1638. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  1639. }
  1640. if (c_msg != NULL)
  1641. enqueue_message(&snac, c_msg);
  1642. history_del(&snac, "timeline.html_");
  1643. }
  1644. status = 303;
  1645. }
  1646. else
  1647. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  1648. /* action on an entry */
  1649. char *id = xs_dict_get(p_vars, "id");
  1650. char *actor = xs_dict_get(p_vars, "actor");
  1651. char *action = xs_dict_get(p_vars, "action");
  1652. if (action == NULL)
  1653. return 404;
  1654. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1655. status = 303;
  1656. if (strcmp(action, L("Like")) == 0) { /** **/
  1657. xs *msg = msg_admiration(&snac, id, "Like");
  1658. if (msg != NULL) {
  1659. enqueue_message(&snac, msg);
  1660. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  1661. }
  1662. }
  1663. else
  1664. if (strcmp(action, L("Boost")) == 0) { /** **/
  1665. xs *msg = msg_admiration(&snac, id, "Announce");
  1666. if (msg != NULL) {
  1667. enqueue_message(&snac, msg);
  1668. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  1669. }
  1670. }
  1671. else
  1672. if (strcmp(action, L("MUTE")) == 0) { /** **/
  1673. mute(&snac, actor);
  1674. }
  1675. else
  1676. if (strcmp(action, L("Unmute")) == 0) { /** **/
  1677. unmute(&snac, actor);
  1678. }
  1679. else
  1680. if (strcmp(action, L("Hide")) == 0) { /** **/
  1681. hide(&snac, id);
  1682. }
  1683. else
  1684. if (strcmp(action, L("Follow")) == 0) { /** **/
  1685. xs *msg = msg_follow(&snac, actor);
  1686. if (msg != NULL) {
  1687. /* reload the actor from the message, in may be different */
  1688. actor = xs_dict_get(msg, "object");
  1689. following_add(&snac, actor, msg);
  1690. enqueue_output_by_actor(&snac, msg, actor, 0);
  1691. }
  1692. }
  1693. else
  1694. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  1695. /* get the following object */
  1696. xs *object = NULL;
  1697. if (valid_status(following_get(&snac, actor, &object))) {
  1698. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1699. following_del(&snac, actor);
  1700. enqueue_output_by_actor(&snac, msg, actor, 0);
  1701. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1702. }
  1703. else
  1704. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1705. }
  1706. else
  1707. if (strcmp(action, L("Delete")) == 0) { /** **/
  1708. char *actor_form = xs_dict_get(p_vars, "actor-form");
  1709. if (actor_form != NULL) {
  1710. /* delete follower */
  1711. if (valid_status(follower_del(&snac, actor)))
  1712. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  1713. else
  1714. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  1715. }
  1716. else {
  1717. /* delete an entry */
  1718. if (xs_startswith(id, snac.actor)) {
  1719. /* it's a post by us: generate a delete */
  1720. xs *msg = msg_delete(&snac, id);
  1721. enqueue_message(&snac, msg);
  1722. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1723. }
  1724. timeline_del(&snac, id);
  1725. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1726. }
  1727. }
  1728. else
  1729. if (strcmp(action, L("Pin")) == 0) { /** **/
  1730. pin(&snac, id);
  1731. timeline_touch(&snac);
  1732. }
  1733. else
  1734. if (strcmp(action, L("Unpin")) == 0) { /** **/
  1735. unpin(&snac, id);
  1736. timeline_touch(&snac);
  1737. }
  1738. else
  1739. status = 404;
  1740. /* delete the cached timeline */
  1741. if (status == 303)
  1742. history_del(&snac, "timeline.html_");
  1743. }
  1744. else
  1745. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  1746. /* change of user data */
  1747. char *v;
  1748. char *p1, *p2;
  1749. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1750. snac.config = xs_dict_set(snac.config, "name", v);
  1751. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1752. snac.config = xs_dict_set(snac.config, "avatar", v);
  1753. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1754. snac.config = xs_dict_set(snac.config, "bio", v);
  1755. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1756. strcmp(v, "on") == 0) {
  1757. snac.config = xs_dict_set(snac.config, "cw", "open");
  1758. } else { /* if the checkbox is not set, the parameter is missing */
  1759. snac.config = xs_dict_set(snac.config, "cw", "");
  1760. }
  1761. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1762. snac.config = xs_dict_set(snac.config, "email", v);
  1763. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  1764. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  1765. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  1766. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  1767. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  1768. xs *days = xs_number_new(atof(v));
  1769. snac.config = xs_dict_set(snac.config, "purge_days", days);
  1770. }
  1771. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  1772. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_true);
  1773. else
  1774. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_false);
  1775. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  1776. snac.config = xs_dict_set(snac.config, "bot", xs_stock_true);
  1777. else
  1778. snac.config = xs_dict_set(snac.config, "bot", xs_stock_false);
  1779. /* avatar upload */
  1780. xs_list *avatar_file = xs_dict_get(p_vars, "avatar_file");
  1781. if (!xs_is_null(avatar_file) && xs_type(avatar_file) == XSTYPE_LIST) {
  1782. char *fn = xs_list_get(avatar_file, 0);
  1783. if (*fn != '\0') {
  1784. char *ext = strrchr(fn, '.');
  1785. xs *id = xs_fmt("avatar%s", ext);
  1786. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1787. int fo = xs_number_get(xs_list_get(avatar_file, 1));
  1788. int fs = xs_number_get(xs_list_get(avatar_file, 2));
  1789. /* store */
  1790. static_put(&snac, id, payload + fo, fs);
  1791. snac.config = xs_dict_set(snac.config, "avatar", url);
  1792. }
  1793. }
  1794. /* password change? */
  1795. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1796. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1797. *p1 && strcmp(p1, p2) == 0) {
  1798. xs *pw = hash_password(snac.uid, p1, NULL);
  1799. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1800. }
  1801. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1802. xs *bfn = xs_fmt("%s.bak", fn);
  1803. FILE *f;
  1804. rename(fn, bfn);
  1805. if ((f = fopen(fn, "w")) != NULL) {
  1806. xs *j = xs_json_dumps_pp(snac.config, 4);
  1807. fwrite(j, strlen(j), 1, f);
  1808. fclose(f);
  1809. }
  1810. else
  1811. rename(bfn, fn);
  1812. history_del(&snac, "timeline.html_");
  1813. xs *a_msg = msg_actor(&snac);
  1814. xs *u_msg = msg_update(&snac, a_msg);
  1815. enqueue_message(&snac, u_msg);
  1816. status = 303;
  1817. }
  1818. else
  1819. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  1820. notify_clear(&snac);
  1821. timeline_touch(&snac);
  1822. status = 303;
  1823. }
  1824. else
  1825. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  1826. char *irt = xs_dict_get(p_vars, "irt");
  1827. const char *opt = xs_dict_get(p_vars, "question");
  1828. const char *actor = xs_dict_get(p_vars, "actor");
  1829. xs *ls = NULL;
  1830. /* multiple choices? */
  1831. if (xs_type(opt) == XSTYPE_LIST)
  1832. ls = xs_dup(opt);
  1833. else {
  1834. ls = xs_list_new();
  1835. ls = xs_list_append(ls, opt);
  1836. }
  1837. xs_list *p = ls;
  1838. xs_str *v;
  1839. while (xs_list_iter(&p, &v)) {
  1840. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
  1841. /* set the option */
  1842. msg = xs_dict_append(msg, "name", v);
  1843. xs *c_msg = msg_create(&snac, msg);
  1844. enqueue_message(&snac, c_msg);
  1845. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1846. }
  1847. status = 303;
  1848. }
  1849. if (status == 303) {
  1850. char *redir = xs_dict_get(p_vars, "redir");
  1851. if (xs_is_null(redir))
  1852. redir = "snac-posts";
  1853. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1854. *b_size = strlen(*body);
  1855. }
  1856. user_free(&snac);
  1857. return status;
  1858. }