html.c 60 KB

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