html.c 63 KB

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