html.c 66 KB

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