html.c 78 KB

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