html.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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. if (was_question_voted(snac, id)) {
  637. /* add a check to show this poll was voted */
  638. xs *f2 = xs_fmt("<span title=\"%s\"> &#10003; </span>", L("Voted"));
  639. s = xs_str_cat(s, f2);
  640. }
  641. }
  642. /* if this is our post, add the score */
  643. if (xs_startswith(id, snac->actor)) {
  644. int n_likes = object_likes_len(id);
  645. int n_boosts = object_announces_len(id);
  646. /* alternate emojis: %d &#128077; %d &#128257; */
  647. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  648. s = xs_str_cat(s, s1);
  649. }
  650. s = xs_str_cat(s, "</div>\n");
  651. if (level == 0)
  652. s = xs_str_cat(s, "<div class=\"snac-post\">\n"); /** **/
  653. else
  654. s = xs_str_cat(s, "<div class=\"snac-child\">\n"); /** **/
  655. if (boosts == NULL)
  656. boosts = object_announces(id);
  657. if (xs_list_len(boosts)) {
  658. /* if somebody boosted this, show as origin */
  659. char *p = xs_list_get(boosts, -1);
  660. xs *actor_r = NULL;
  661. if (xs_list_in(boosts, snac->md5) != -1) {
  662. /* we boosted this */
  663. xs *s1 = xs_fmt(
  664. "<div class=\"snac-origin\">"
  665. "<a href=\"%s\">%s</a> %s</a></div>",
  666. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  667. );
  668. s = xs_str_cat(s, s1);
  669. }
  670. else
  671. if (valid_status(object_get_by_md5(p, &actor_r))) {
  672. xs *name = actor_name(actor_r);
  673. if (!xs_is_null(name)) {
  674. xs *s1 = xs_fmt(
  675. "<div class=\"snac-origin\">"
  676. "<a href=\"%s\">%s</a> %s</div>\n",
  677. xs_dict_get(actor_r, "id"),
  678. name,
  679. L("boosted")
  680. );
  681. s = xs_str_cat(s, s1);
  682. }
  683. }
  684. }
  685. else
  686. if (strcmp(type, "Note") == 0) {
  687. if (level == 0) {
  688. /* is the parent not here? */
  689. char *parent = xs_dict_get(msg, "inReplyTo");
  690. if (!xs_is_null(parent) && *parent && !timeline_here(snac, parent)) {
  691. xs *s1 = xs_fmt(
  692. "<div class=\"snac-origin\">%s "
  693. "<a href=\"%s\">»</a></div>\n",
  694. L("in reply to"), parent
  695. );
  696. s = xs_str_cat(s, s1);
  697. }
  698. }
  699. }
  700. s = html_msg_icon(snac, s, msg);
  701. /* add the content */
  702. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n"); /** **/
  703. /* is it sensitive? */
  704. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  705. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  706. v = "...";
  707. /* only show it when not in the public timeline and the config setting is "open" */
  708. char *cw = xs_dict_get(snac->config, "cw");
  709. if (xs_is_null(cw) || local)
  710. cw = "";
  711. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, v, L("SENSITIVE CONTENT"));
  712. s = xs_str_cat(s, s1);
  713. sensitive = 1;
  714. }
  715. #if 0
  716. {
  717. xs *md5 = xs_md5_hex(id, strlen(id));
  718. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  719. s = xs_str_cat(s, s1);
  720. }
  721. #endif
  722. {
  723. const char *content = xs_dict_get(msg, "content");
  724. xs *c = sanitize(xs_is_null(content) ? "" : content);
  725. char *p, *v;
  726. /* do some tweaks to the content */
  727. c = xs_replace_i(c, "\r", "");
  728. while (xs_endswith(c, "<br><br>"))
  729. c = xs_crop_i(c, 0, -4);
  730. c = xs_replace_i(c, "<br><br>", "<p>");
  731. if (!xs_startswith(c, "<p>")) {
  732. xs *s1 = c;
  733. c = xs_fmt("<p>%s</p>", s1);
  734. }
  735. /* replace the :shortnames: */
  736. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  737. /* iterate the tags */
  738. while (xs_list_iter(&p, &v)) {
  739. char *t = xs_dict_get(v, "type");
  740. if (t && strcmp(t, "Emoji") == 0) {
  741. char *n = xs_dict_get(v, "name");
  742. char *i = xs_dict_get(v, "icon");
  743. if (n && i) {
  744. char *u = xs_dict_get(i, "url");
  745. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\" "
  746. "loading=\"lazy\"/>", u);
  747. c = xs_replace_i(c, n, img);
  748. }
  749. }
  750. }
  751. }
  752. if (strcmp(type, "Question") == 0) { /** question content **/
  753. xs_list *oo = xs_dict_get(msg, "oneOf");
  754. xs_list *ao = xs_dict_get(msg, "anyOf");
  755. xs_list *p;
  756. xs_dict *v;
  757. int closed = 0;
  758. if (xs_dict_get(msg, "closed"))
  759. closed = 2;
  760. else
  761. if (xs_startswith(id, snac->actor))
  762. closed = 1; /* we questioned; closed for us */
  763. else {
  764. /* not yet closed? check if we already voted for this */
  765. xs *children = object_children(id);
  766. p = children;
  767. while (!closed && xs_list_iter(&p, &v)) {
  768. xs *msg = NULL;
  769. if (valid_status(object_get_by_md5(v, &msg))) {
  770. const char *atto = xs_dict_get(msg, "attributedTo");
  771. if (atto && strcmp(atto, snac->actor) == 0)
  772. closed = 1; /* we already voted; closed for us */
  773. }
  774. }
  775. }
  776. /* get the appropriate list of options */
  777. p = oo != NULL ? oo : ao;
  778. if (closed) {
  779. /* closed poll */
  780. c = xs_str_cat(c, "<table>\n");
  781. while (xs_list_iter(&p, &v)) {
  782. const char *name = xs_dict_get(v, "name");
  783. const xs_dict *replies = xs_dict_get(v, "replies");
  784. if (name && replies) {
  785. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  786. xs *l = xs_fmt("<tr><td>%s:</td><td>%d</td></tr>\n", name, nr);
  787. c = xs_str_cat(c, l);
  788. }
  789. }
  790. c = xs_str_cat(c, "</table>\n");
  791. /* if it's *really* closed, say it */
  792. if (closed == 2) {
  793. xs *s1 = xs_fmt("<p>%s</p>\n", L("Closed"));
  794. c = xs_str_cat(c, s1);
  795. }
  796. }
  797. else {
  798. /* poll still active */
  799. xs *s1 = xs_fmt("<form method=\"post\" action=\"%s/admin/vote\">\n"
  800. "<input type=\"hidden\" name=\"actor\" value= \"%s\">\n"
  801. "<input type=\"hidden\" name=\"irt\" value=\"%s\">\n",
  802. snac->actor, actor, id);
  803. while (xs_list_iter(&p, &v)) {
  804. const char *name = xs_dict_get(v, "name");
  805. if (name) {
  806. xs *opt = xs_fmt("<input type=\"%s\""
  807. " id=\"%s\" value=\"%s\" name=\"question\"> %s<br>\n",
  808. !xs_is_null(oo) ? "radio" : "checkbox",
  809. name, name, name);
  810. s1 = xs_str_cat(s1, opt);
  811. }
  812. }
  813. xs *s2 = xs_fmt("<p><input type=\"submit\" "
  814. "class=\"button\" value=\"%s\">\n</form>\n\n", L("Vote"));
  815. s1 = xs_str_cat(s1, s2);
  816. c = xs_str_cat(c, s1);
  817. }
  818. }
  819. s = xs_str_cat(s, c);
  820. }
  821. s = xs_str_cat(s, "\n");
  822. /* add the attachments */
  823. char *attach;
  824. if ((attach = xs_dict_get(msg, "attachment")) != NULL) { /** **/
  825. char *v;
  826. while (xs_list_iter(&attach, &v)) {
  827. char *t = xs_dict_get(v, "mediaType");
  828. if (xs_is_null(t))
  829. continue;
  830. if (xs_startswith(t, "image/")) {
  831. char *url = xs_dict_get(v, "url");
  832. char *name = xs_dict_get(v, "name");
  833. if (url != NULL) {
  834. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\" loading=\"lazy\"/></p>\n",
  835. url, xs_is_null(name) ? "" : name);
  836. s = xs_str_cat(s, s1);
  837. }
  838. }
  839. else
  840. if (xs_startswith(t, "video/")) {
  841. char *url = xs_dict_get(v, "url");
  842. if (url != NULL) {
  843. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  844. s = xs_str_cat(s, s1);
  845. }
  846. }
  847. }
  848. }
  849. if (sensitive)
  850. s = xs_str_cat(s, "</details><p>\n");
  851. s = xs_str_cat(s, "</div>\n");
  852. /** controls **/
  853. if (!local)
  854. s = html_entry_controls(snac, s, msg, md5);
  855. /** children **/
  856. if (!hide_children) {
  857. xs *children = object_children(id);
  858. int left = xs_list_len(children);
  859. if (left) {
  860. char *p, *cmd5;
  861. int older_open = 0;
  862. xs *ss = xs_str_new(NULL);
  863. int n_children = 0;
  864. ss = xs_str_cat(ss, "<details open><summary>...</summary><p>\n");
  865. if (level < 4)
  866. ss = xs_str_cat(ss, "<div class=\"snac-children\">\n");
  867. else
  868. ss = xs_str_cat(ss, "<div>\n");
  869. if (left > 3) {
  870. xs *s1 = xs_fmt("<details><summary>%s</summary>\n", L("Older..."));
  871. ss = xs_str_cat(ss, s1);
  872. older_open = 1;
  873. }
  874. p = children;
  875. while (xs_list_iter(&p, &cmd5)) {
  876. xs *chd = NULL;
  877. timeline_get_by_md5(snac, cmd5, &chd);
  878. if (older_open && left <= 3) {
  879. ss = xs_str_cat(ss, "</details>\n");
  880. older_open = 0;
  881. }
  882. if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
  883. ss = html_entry(snac, ss, chd, local, level + 1, cmd5, hide_children);
  884. n_children++;
  885. }
  886. else
  887. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", cmd5));
  888. left--;
  889. }
  890. if (older_open)
  891. ss = xs_str_cat(ss, "</details>\n");
  892. ss = xs_str_cat(ss, "</div>\n");
  893. ss = xs_str_cat(ss, "</details>\n");
  894. if (n_children)
  895. s = xs_str_cat(s, ss);
  896. }
  897. }
  898. s = xs_str_cat(s, "</div>\n</div>\n");
  899. return xs_str_cat(os, s);
  900. }
  901. xs_str *html_user_footer(xs_str *s)
  902. {
  903. xs *s1 = xs_fmt(
  904. "<div class=\"snac-footer\">\n"
  905. "<a href=\"%s\">%s</a> - "
  906. "powered by <a href=\"%s\""
  907. "<abbr title=\"Social Networks Are Crap\">snac</abbr></div></a>\n",
  908. srv_baseurl,
  909. L("about this site"),
  910. WHAT_IS_SNAC_URL
  911. );
  912. return xs_str_cat(s, s1);
  913. }
  914. xs_str *html_timeline(snac *snac, const xs_list *list, int local, int skip, int show, int show_more)
  915. /* returns the HTML for the timeline */
  916. {
  917. xs_str *s = xs_str_new(NULL);
  918. xs_list *p = (xs_list *)list;
  919. char *v;
  920. double t = ftime();
  921. s = html_user_header(snac, s, local);
  922. if (!local)
  923. s = html_top_controls(snac, s);
  924. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  925. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  926. while (xs_list_iter(&p, &v)) {
  927. xs *msg = NULL;
  928. if (!valid_status(timeline_get_by_md5(snac, v, &msg)))
  929. continue;
  930. s = html_entry(snac, s, msg, local, 0, v, 0);
  931. }
  932. s = xs_str_cat(s, "</div>\n");
  933. if (local) {
  934. xs *s1 = xs_fmt(
  935. "<div class=\"snac-history\">\n"
  936. "<p class=\"snac-history-title\">%s</p><ul>\n",
  937. L("History")
  938. );
  939. s = xs_str_cat(s, s1);
  940. xs *list = history_list(snac);
  941. char *p, *v;
  942. p = list;
  943. while (xs_list_iter(&p, &v)) {
  944. xs *fn = xs_replace(v, ".html", "");
  945. xs *s1 = xs_fmt(
  946. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  947. snac->actor, v, fn);
  948. s = xs_str_cat(s, s1);
  949. }
  950. s = xs_str_cat(s, "</ul></div>\n");
  951. }
  952. {
  953. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  954. s = xs_str_cat(s, s1);
  955. }
  956. if (show_more) {
  957. xs *s1 = xs_fmt(
  958. "<p>"
  959. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  960. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("Load more..."));
  961. s = xs_str_cat(s, s1);
  962. }
  963. s = html_user_footer(s);
  964. s = xs_str_cat(s, "</body>\n</html>\n");
  965. return s;
  966. }
  967. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  968. {
  969. xs *s = xs_str_new(NULL);
  970. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  971. char *p, *actor_id;
  972. s = xs_str_cat(s, h);
  973. p = list;
  974. while (xs_list_iter(&p, &actor_id)) {
  975. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  976. xs *actor = NULL;
  977. if (valid_status(actor_get(snac, actor_id, &actor))) {
  978. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  979. s = html_actor_icon(s, actor, xs_dict_get(actor, "published"), NULL, NULL, 0);
  980. /* content (user bio) */
  981. char *c = xs_dict_get(actor, "summary");
  982. if (!xs_is_null(c)) {
  983. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  984. xs *sc = sanitize(c);
  985. if (xs_startswith(sc, "<p>"))
  986. s = xs_str_cat(s, sc);
  987. else {
  988. xs *s1 = xs_fmt("<p>%s</p>", sc);
  989. s = xs_str_cat(s, s1);
  990. }
  991. s = xs_str_cat(s, "</div>\n");
  992. }
  993. /* buttons */
  994. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  995. xs *s1 = xs_fmt(
  996. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  997. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  998. "<input type=\"hidden\" name=\"actor-form\" value=\"yes\">\n",
  999. snac->actor, actor_id
  1000. );
  1001. s = xs_str_cat(s, s1);
  1002. if (following_check(snac, actor_id))
  1003. s = html_button(s, "unfollow", L("Unfollow"));
  1004. else {
  1005. s = html_button(s, "follow", L("Follow"));
  1006. if (follower_check(snac, actor_id))
  1007. s = html_button(s, "delete", L("Delete"));
  1008. }
  1009. if (is_muted(snac, actor_id))
  1010. s = html_button(s, "unmute", L("Unmute"));
  1011. else
  1012. s = html_button(s, "mute", L("MUTE"));
  1013. s = xs_str_cat(s, "</form>\n");
  1014. /* the post textarea */
  1015. xs *s2 = xs_fmt(
  1016. "<p><details><summary>%s</summary>\n"
  1017. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  1018. "<form method=\"post\" action=\"%s/admin/note\" "
  1019. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  1020. "<textarea class=\"snac-textarea\" name=\"content\" "
  1021. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  1022. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  1023. "<p><input type=\"file\" name=\"attach\">\n"
  1024. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  1025. "</form><p></div>\n"
  1026. "</details><p>\n",
  1027. L("Direct Message..."),
  1028. md5, t,
  1029. snac->actor, md5,
  1030. actor_id,
  1031. L("Post")
  1032. );
  1033. s = xs_str_cat(s, s2);
  1034. s = xs_str_cat(s, "</div>\n");
  1035. s = xs_str_cat(s, "</div>\n");
  1036. }
  1037. }
  1038. return xs_str_cat(os, s);
  1039. }
  1040. d_char *html_people(snac *snac)
  1041. {
  1042. d_char *s = xs_str_new(NULL);
  1043. xs *wing = following_list(snac);
  1044. xs *wers = follower_list(snac);
  1045. s = html_user_header(snac, s, 0);
  1046. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  1047. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  1048. s = html_user_footer(s);
  1049. s = xs_str_cat(s, "</body>\n</html>\n");
  1050. return s;
  1051. }
  1052. xs_str *html_notifications(snac *snac)
  1053. {
  1054. xs_str *s = xs_str_new(NULL);
  1055. xs *n_list = notify_list(snac, 0);
  1056. xs *n_time = notify_check_time(snac, 0);
  1057. xs_list *p = n_list;
  1058. xs_str *v;
  1059. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  1060. s = html_user_header(snac, s, 0);
  1061. xs *s1 = xs_fmt(
  1062. "<form method=\"post\" action=\"%s/admin/clear-notifications\" id=\"clear\">\n"
  1063. "<input type=\"submit\" class=\"snac-btn-like\" value=\"%s\">\n"
  1064. "</form><p>", snac->actor, L("Clear all"));
  1065. s = xs_str_cat(s, s1);
  1066. while (xs_list_iter(&p, &v)) {
  1067. xs *noti = notify_get(snac, v);
  1068. if (noti == NULL)
  1069. continue;
  1070. xs *obj = NULL;
  1071. const char *type = xs_dict_get(noti, "type");
  1072. const char *utype = xs_dict_get(noti, "utype");
  1073. const char *id = xs_dict_get(noti, "objid");
  1074. if (!valid_status(object_get(id, &obj)))
  1075. continue;
  1076. if (is_hidden(snac, id))
  1077. continue;
  1078. const char *actor_id = xs_dict_get(noti, "actor");
  1079. xs *actor = NULL;
  1080. if (!valid_status(actor_get(snac, actor_id, &actor)))
  1081. continue;
  1082. xs *a_name = actor_name(actor);
  1083. if (strcmp(v, n_time) > 0) {
  1084. /* unseen notification */
  1085. if (stage == NHDR_NONE) {
  1086. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("New"));
  1087. s = xs_str_cat(s, s1);
  1088. stage = NHDR_NEW;
  1089. }
  1090. }
  1091. else {
  1092. /* already seen notification */
  1093. if (stage != NHDR_OLD) {
  1094. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("Already seen"));
  1095. s = xs_str_cat(s, s1);
  1096. stage = NHDR_OLD;
  1097. }
  1098. }
  1099. s = xs_str_cat(s, "<div>\n");
  1100. const char *label = type;
  1101. if (strcmp(type, "Create") == 0)
  1102. label = L("Mention");
  1103. else
  1104. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1105. label = L("Finished poll");
  1106. xs *s1 = xs_fmt("<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  1107. label, actor_id, a_name);
  1108. s = xs_str_cat(s, s1);
  1109. if (strcmp(type, "Follow") == 0) {
  1110. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  1111. s = html_msg_icon(snac, s, obj);
  1112. s = xs_str_cat(s, "</div>\n");
  1113. }
  1114. else {
  1115. xs *md5 = xs_md5_hex(id, strlen(id));
  1116. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  1117. }
  1118. s = xs_str_cat(s, "</div>\n");
  1119. }
  1120. if (stage == NHDR_NONE) {
  1121. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("None"));
  1122. s = xs_str_cat(s, s1);
  1123. }
  1124. s = html_user_footer(s);
  1125. s = xs_str_cat(s, "</body>\n</html>\n");
  1126. /* set the check time to now */
  1127. xs *dummy = notify_check_time(snac, 1);
  1128. dummy = xs_free(dummy);
  1129. timeline_touch(snac);
  1130. return s;
  1131. }
  1132. int html_get_handler(const xs_dict *req, const char *q_path,
  1133. char **body, int *b_size, char **ctype)
  1134. {
  1135. char *accept = xs_dict_get(req, "accept");
  1136. int status = 404;
  1137. snac snac;
  1138. xs *uid = NULL;
  1139. char *p_path;
  1140. int cache = 1;
  1141. int save = 1;
  1142. char *v;
  1143. xs *l = xs_split_n(q_path, "/", 2);
  1144. v = xs_list_get(l, 1);
  1145. if (xs_is_null(v)) {
  1146. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1147. return 404;
  1148. }
  1149. uid = xs_dup(v);
  1150. /* rss extension? */
  1151. if (xs_endswith(uid, ".rss")) {
  1152. uid = xs_crop_i(uid, 0, -4);
  1153. p_path = ".rss";
  1154. }
  1155. else
  1156. p_path = xs_list_get(l, 2);
  1157. if (!uid || !user_open(&snac, uid)) {
  1158. /* invalid user */
  1159. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1160. return 404;
  1161. }
  1162. /* return the RSS if requested by Accept header */
  1163. if (accept != NULL) {
  1164. if (xs_str_in(accept, "text/xml") != -1 ||
  1165. xs_str_in(accept, "application/rss+xml") != -1)
  1166. p_path = ".rss";
  1167. }
  1168. /* check if server config variable 'disable_cache' is set */
  1169. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1170. cache = 0;
  1171. int skip = 0;
  1172. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1173. char *q_vars = xs_dict_get(req, "q_vars");
  1174. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1175. skip = atoi(v), cache = 0, save = 0;
  1176. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1177. show = atoi(v), cache = 0, save = 0;
  1178. if (p_path == NULL) { /** public timeline **/
  1179. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1180. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1181. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1182. *body = history_get(&snac, h);
  1183. *b_size = strlen(*body);
  1184. status = 200;
  1185. }
  1186. else {
  1187. xs *list = timeline_list(&snac, "public", skip, show);
  1188. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1189. *body = html_timeline(&snac, list, 1, skip, show, xs_list_len(next));
  1190. *b_size = strlen(*body);
  1191. status = 200;
  1192. if (save)
  1193. history_add(&snac, h, *body, *b_size);
  1194. }
  1195. }
  1196. else
  1197. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  1198. if (!login(&snac, req))
  1199. status = 401;
  1200. else {
  1201. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1202. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1203. *body = history_get(&snac, "timeline.html_");
  1204. *b_size = strlen(*body);
  1205. status = 200;
  1206. }
  1207. else {
  1208. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1209. xs *list = timeline_list(&snac, "private", skip, show);
  1210. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1211. *body = html_timeline(&snac, list, 0, skip, show, xs_list_len(next));
  1212. *b_size = strlen(*body);
  1213. status = 200;
  1214. if (save)
  1215. history_add(&snac, "timeline.html_", *body, *b_size);
  1216. }
  1217. }
  1218. }
  1219. else
  1220. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  1221. if (!login(&snac, req))
  1222. status = 401;
  1223. else {
  1224. *body = html_people(&snac);
  1225. *b_size = strlen(*body);
  1226. status = 200;
  1227. }
  1228. }
  1229. else
  1230. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  1231. if (!login(&snac, req))
  1232. status = 401;
  1233. else {
  1234. *body = html_notifications(&snac);
  1235. *b_size = strlen(*body);
  1236. status = 200;
  1237. }
  1238. }
  1239. else
  1240. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  1241. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1242. xs *msg = NULL;
  1243. if (valid_status(object_get(id, &msg))) {
  1244. xs *md5 = xs_md5_hex(id, strlen(id));
  1245. xs *list = xs_list_new();
  1246. list = xs_list_append(list, md5);
  1247. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  1248. *b_size = strlen(*body);
  1249. status = 200;
  1250. }
  1251. }
  1252. else
  1253. if (xs_startswith(p_path, "s/")) { /** a static file **/
  1254. xs *l = xs_split(p_path, "/");
  1255. char *id = xs_list_get(l, 1);
  1256. int sz;
  1257. if (valid_status(static_get(&snac, id, body, &sz))) {
  1258. *b_size = sz;
  1259. *ctype = xs_mime_by_ext(id);
  1260. status = 200;
  1261. }
  1262. }
  1263. else
  1264. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  1265. xs *l = xs_split(p_path, "/");
  1266. char *id = xs_list_get(l, 1);
  1267. if ((*body = history_get(&snac, id)) != NULL) {
  1268. *b_size = strlen(*body);
  1269. status = 200;
  1270. }
  1271. }
  1272. else
  1273. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  1274. d_char *rss;
  1275. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1276. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  1277. char *p, *v;
  1278. /* escape tags */
  1279. bio = xs_replace_i(bio, "<", "&lt;");
  1280. bio = xs_replace_i(bio, ">", "&gt;");
  1281. rss = xs_fmt(
  1282. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1283. "<rss version=\"0.91\">\n"
  1284. "<channel>\n"
  1285. "<title>%s (@%s@%s)</title>\n"
  1286. "<language>en</language>\n"
  1287. "<link>%s.rss</link>\n"
  1288. "<description>%s</description>\n",
  1289. xs_dict_get(snac.config, "name"),
  1290. snac.uid,
  1291. xs_dict_get(srv_config, "host"),
  1292. snac.actor,
  1293. bio
  1294. );
  1295. p = elems;
  1296. while (xs_list_iter(&p, &v)) {
  1297. xs *msg = NULL;
  1298. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1299. continue;
  1300. char *id = xs_dict_get(msg, "id");
  1301. if (!xs_startswith(id, snac.actor))
  1302. continue;
  1303. xs *content = sanitize(xs_dict_get(msg, "content"));
  1304. xs *title = xs_str_new(NULL);
  1305. int i;
  1306. /* escape tags */
  1307. content = xs_replace_i(content, "<", "&lt;");
  1308. content = xs_replace_i(content, ">", "&gt;");
  1309. for (i = 0; content[i] && content[i] != '<' && content[i] != '&' && i < 40; i++)
  1310. title = xs_append_m(title, &content[i], 1);
  1311. xs *s = xs_fmt(
  1312. "<item>\n"
  1313. "<title>%s...</title>\n"
  1314. "<link>%s</link>\n"
  1315. "<description>%s</description>\n"
  1316. "</item>\n",
  1317. title, id, content
  1318. );
  1319. rss = xs_str_cat(rss, s);
  1320. }
  1321. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  1322. *body = rss;
  1323. *b_size = strlen(rss);
  1324. *ctype = "application/rss+xml; charset=utf-8";
  1325. status = 200;
  1326. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1327. }
  1328. else
  1329. status = 404;
  1330. user_free(&snac);
  1331. if (valid_status(status) && *ctype == NULL) {
  1332. *ctype = "text/html; charset=utf-8";
  1333. }
  1334. return status;
  1335. }
  1336. int html_post_handler(const xs_dict *req, const char *q_path,
  1337. char *payload, int p_size,
  1338. char **body, int *b_size, char **ctype)
  1339. {
  1340. (void)p_size;
  1341. (void)ctype;
  1342. int status = 0;
  1343. snac snac;
  1344. char *uid, *p_path;
  1345. xs_dict *p_vars;
  1346. xs *l = xs_split_n(q_path, "/", 2);
  1347. uid = xs_list_get(l, 1);
  1348. if (!uid || !user_open(&snac, uid)) {
  1349. /* invalid user */
  1350. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1351. return 404;
  1352. }
  1353. p_path = xs_list_get(l, 2);
  1354. /* all posts must be authenticated */
  1355. if (!login(&snac, req)) {
  1356. user_free(&snac);
  1357. return 401;
  1358. }
  1359. p_vars = xs_dict_get(req, "p_vars");
  1360. #if 0
  1361. {
  1362. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  1363. printf("%s\n", j1);
  1364. }
  1365. #endif
  1366. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  1367. /* post note */
  1368. xs_str *content = xs_dict_get(p_vars, "content");
  1369. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1370. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1371. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1372. xs_str *to = xs_dict_get(p_vars, "to");
  1373. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1374. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1375. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1376. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1377. xs *attach_list = xs_list_new();
  1378. /* default alt text */
  1379. if (xs_is_null(alt_text))
  1380. alt_text = "";
  1381. /* is attach_url set? */
  1382. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  1383. xs *l = xs_list_new();
  1384. l = xs_list_append(l, attach_url);
  1385. l = xs_list_append(l, alt_text);
  1386. attach_list = xs_list_append(attach_list, l);
  1387. }
  1388. /* is attach_file set? */
  1389. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  1390. char *fn = xs_list_get(attach_file, 0);
  1391. if (*fn != '\0') {
  1392. char *ext = strrchr(fn, '.');
  1393. xs *hash = xs_md5_hex(fn, strlen(fn));
  1394. xs *id = xs_fmt("%s%s", hash, ext);
  1395. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1396. int fo = xs_number_get(xs_list_get(attach_file, 1));
  1397. int fs = xs_number_get(xs_list_get(attach_file, 2));
  1398. /* store */
  1399. static_put(&snac, id, payload + fo, fs);
  1400. xs *l = xs_list_new();
  1401. l = xs_list_append(l, url);
  1402. l = xs_list_append(l, alt_text);
  1403. attach_list = xs_list_append(attach_list, l);
  1404. }
  1405. }
  1406. if (content != NULL) {
  1407. xs *msg = NULL;
  1408. xs *c_msg = NULL;
  1409. xs *content_2 = xs_replace(content, "\r", "");
  1410. xs *poll_opts = NULL;
  1411. /* is there a valid set of poll options? */
  1412. const char *v = xs_dict_get(p_vars, "poll_options");
  1413. if (!xs_is_null(v) && *v) {
  1414. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  1415. poll_opts = xs_split(v2, "\n");
  1416. }
  1417. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  1418. /* get the rest of poll configuration */
  1419. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  1420. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  1421. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  1422. int multiple = !xs_is_null(p_multiple);
  1423. msg = msg_question(&snac, content_2, attach_list,
  1424. poll_opts, multiple, end_secs);
  1425. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  1426. }
  1427. else
  1428. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  1429. if (sensitive != NULL) {
  1430. xs *t = xs_val_new(XSTYPE_TRUE);
  1431. msg = xs_dict_set(msg, "sensitive", t);
  1432. msg = xs_dict_set(msg, "summary", "...");
  1433. }
  1434. if (xs_is_null(edit_id)) {
  1435. /* new message */
  1436. c_msg = msg_create(&snac, msg);
  1437. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1438. }
  1439. else {
  1440. /* an edition of a previous message */
  1441. xs *p_msg = NULL;
  1442. if (valid_status(object_get(edit_id, &p_msg))) {
  1443. /* copy relevant fields from previous version */
  1444. char *fields[] = { "id", "context", "url", "published",
  1445. "to", "inReplyTo", NULL };
  1446. int n;
  1447. for (n = 0; fields[n]; n++) {
  1448. char *v = xs_dict_get(p_msg, fields[n]);
  1449. msg = xs_dict_set(msg, fields[n], v);
  1450. }
  1451. /* set the updated field */
  1452. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  1453. msg = xs_dict_set(msg, "updated", updated);
  1454. /* overwrite object, not updating the indexes */
  1455. object_add_ow(edit_id, msg);
  1456. /* update message */
  1457. c_msg = msg_update(&snac, msg);
  1458. }
  1459. else
  1460. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  1461. }
  1462. if (c_msg != NULL)
  1463. enqueue_message(&snac, c_msg);
  1464. history_del(&snac, "timeline.html_");
  1465. }
  1466. status = 303;
  1467. }
  1468. else
  1469. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  1470. /* action on an entry */
  1471. char *id = xs_dict_get(p_vars, "id");
  1472. char *actor = xs_dict_get(p_vars, "actor");
  1473. char *action = xs_dict_get(p_vars, "action");
  1474. if (action == NULL)
  1475. return 404;
  1476. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1477. status = 303;
  1478. if (strcmp(action, L("Like")) == 0) { /** **/
  1479. xs *msg = msg_admiration(&snac, id, "Like");
  1480. if (msg != NULL) {
  1481. enqueue_message(&snac, msg);
  1482. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  1483. }
  1484. }
  1485. else
  1486. if (strcmp(action, L("Boost")) == 0) { /** **/
  1487. xs *msg = msg_admiration(&snac, id, "Announce");
  1488. if (msg != NULL) {
  1489. enqueue_message(&snac, msg);
  1490. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  1491. }
  1492. }
  1493. else
  1494. if (strcmp(action, L("MUTE")) == 0) { /** **/
  1495. mute(&snac, actor);
  1496. }
  1497. else
  1498. if (strcmp(action, L("Unmute")) == 0) { /** **/
  1499. unmute(&snac, actor);
  1500. }
  1501. else
  1502. if (strcmp(action, L("Hide")) == 0) { /** **/
  1503. hide(&snac, id);
  1504. }
  1505. else
  1506. if (strcmp(action, L("Follow")) == 0) { /** **/
  1507. xs *msg = msg_follow(&snac, actor);
  1508. if (msg != NULL) {
  1509. /* reload the actor from the message, in may be different */
  1510. actor = xs_dict_get(msg, "object");
  1511. following_add(&snac, actor, msg);
  1512. enqueue_output_by_actor(&snac, msg, actor, 0);
  1513. }
  1514. }
  1515. else
  1516. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  1517. /* get the following object */
  1518. xs *object = NULL;
  1519. if (valid_status(following_get(&snac, actor, &object))) {
  1520. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1521. following_del(&snac, actor);
  1522. enqueue_output_by_actor(&snac, msg, actor, 0);
  1523. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1524. }
  1525. else
  1526. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1527. }
  1528. else
  1529. if (strcmp(action, L("Delete")) == 0) { /** **/
  1530. char *actor_form = xs_dict_get(p_vars, "actor-form");
  1531. if (actor_form != NULL) {
  1532. /* delete follower */
  1533. if (valid_status(follower_del(&snac, actor)))
  1534. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  1535. else
  1536. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  1537. }
  1538. else {
  1539. /* delete an entry */
  1540. if (xs_startswith(id, snac.actor)) {
  1541. /* it's a post by us: generate a delete */
  1542. xs *msg = msg_delete(&snac, id);
  1543. enqueue_message(&snac, msg);
  1544. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1545. }
  1546. timeline_del(&snac, id);
  1547. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1548. }
  1549. }
  1550. else
  1551. status = 404;
  1552. /* delete the cached timeline */
  1553. if (status == 303)
  1554. history_del(&snac, "timeline.html_");
  1555. }
  1556. else
  1557. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  1558. /* change of user data */
  1559. char *v;
  1560. char *p1, *p2;
  1561. xs *byes = xs_val_new(XSTYPE_TRUE);
  1562. xs *bno = xs_val_new(XSTYPE_FALSE);
  1563. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1564. snac.config = xs_dict_set(snac.config, "name", v);
  1565. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1566. snac.config = xs_dict_set(snac.config, "avatar", v);
  1567. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1568. snac.config = xs_dict_set(snac.config, "bio", v);
  1569. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1570. strcmp(v, "on") == 0) {
  1571. snac.config = xs_dict_set(snac.config, "cw", "open");
  1572. } else { /* if the checkbox is not set, the parameter is missing */
  1573. snac.config = xs_dict_set(snac.config, "cw", "");
  1574. }
  1575. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1576. snac.config = xs_dict_set(snac.config, "email", v);
  1577. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  1578. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  1579. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  1580. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  1581. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  1582. xs *days = xs_number_new(atof(v));
  1583. snac.config = xs_dict_set(snac.config, "purge_days", days);
  1584. }
  1585. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  1586. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", byes);
  1587. else
  1588. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", bno);
  1589. /* avatar upload */
  1590. xs_list *avatar_file = xs_dict_get(p_vars, "avatar_file");
  1591. if (!xs_is_null(avatar_file) && xs_type(avatar_file) == XSTYPE_LIST) {
  1592. char *fn = xs_list_get(avatar_file, 0);
  1593. if (*fn != '\0') {
  1594. char *ext = strrchr(fn, '.');
  1595. xs *id = xs_fmt("avatar%s", ext);
  1596. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1597. int fo = xs_number_get(xs_list_get(avatar_file, 1));
  1598. int fs = xs_number_get(xs_list_get(avatar_file, 2));
  1599. /* store */
  1600. static_put(&snac, id, payload + fo, fs);
  1601. snac.config = xs_dict_set(snac.config, "avatar", url);
  1602. }
  1603. }
  1604. /* password change? */
  1605. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1606. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1607. *p1 && strcmp(p1, p2) == 0) {
  1608. xs *pw = hash_password(snac.uid, p1, NULL);
  1609. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1610. }
  1611. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1612. xs *bfn = xs_fmt("%s.bak", fn);
  1613. FILE *f;
  1614. rename(fn, bfn);
  1615. if ((f = fopen(fn, "w")) != NULL) {
  1616. xs *j = xs_json_dumps_pp(snac.config, 4);
  1617. fwrite(j, strlen(j), 1, f);
  1618. fclose(f);
  1619. }
  1620. else
  1621. rename(bfn, fn);
  1622. history_del(&snac, "timeline.html_");
  1623. xs *a_msg = msg_actor(&snac);
  1624. xs *u_msg = msg_update(&snac, a_msg);
  1625. enqueue_message(&snac, u_msg);
  1626. status = 303;
  1627. }
  1628. else
  1629. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  1630. notify_clear(&snac);
  1631. timeline_touch(&snac);
  1632. status = 303;
  1633. }
  1634. else
  1635. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  1636. char *irt = xs_dict_get(p_vars, "irt");
  1637. const char *opt = xs_dict_get(p_vars, "question");
  1638. const char *actor = xs_dict_get(p_vars, "actor");
  1639. xs *ls = NULL;
  1640. /* multiple choices? */
  1641. if (xs_type(opt) == XSTYPE_LIST)
  1642. ls = xs_dup(opt);
  1643. else {
  1644. ls = xs_list_new();
  1645. ls = xs_list_append(ls, opt);
  1646. }
  1647. xs_list *p = ls;
  1648. xs_str *v;
  1649. while (xs_list_iter(&p, &v)) {
  1650. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
  1651. /* set the option */
  1652. msg = xs_dict_append(msg, "name", v);
  1653. xs *c_msg = msg_create(&snac, msg);
  1654. enqueue_message(&snac, c_msg);
  1655. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1656. }
  1657. status = 303;
  1658. }
  1659. if (status == 303) {
  1660. char *redir = xs_dict_get(p_vars, "redir");
  1661. if (xs_is_null(redir))
  1662. redir = "snac-posts";
  1663. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1664. *b_size = strlen(*body);
  1665. }
  1666. user_free(&snac);
  1667. return status;
  1668. }