html.c 85 KB

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