html.c 74 KB

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