html.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_json.h"
  7. #include "xs_regex.h"
  8. #include "xs_set.h"
  9. #include "xs_openssl.h"
  10. #include "xs_time.h"
  11. #include "xs_mime.h"
  12. #include "snac.h"
  13. int login(snac *snac, char *headers)
  14. /* tries a login */
  15. {
  16. int logged_in = 0;
  17. char *auth = xs_dict_get(headers, "authorization");
  18. if (auth && xs_startswith(auth, "Basic ")) {
  19. int sz;
  20. xs *s1 = xs_crop(xs_dup(auth), 6, 0);
  21. xs *s2 = xs_base64_dec(s1, &sz);
  22. xs *l1 = xs_split_n(s2, ":", 1);
  23. if (xs_list_len(l1) == 2) {
  24. logged_in = check_password(
  25. xs_list_get(l1, 0), xs_list_get(l1, 1),
  26. xs_dict_get(snac->config, "passwd"));
  27. }
  28. }
  29. return logged_in;
  30. }
  31. d_char *html_actor_icon(snac *snac, d_char *os, char *actor, char *date, char *url, int priv)
  32. {
  33. xs *s = xs_str_new(NULL);
  34. xs *name = NULL;
  35. xs *avatar = NULL;
  36. char *p, *v;
  37. /* get the 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"))) {
  40. v = "user";
  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. /* get the avatar */
  61. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  62. (v = xs_dict_get(v, "url")) != NULL) {
  63. avatar = xs_dup(v);
  64. }
  65. if (avatar == NULL)
  66. avatar = xs_fmt("data:image/png;base64, %s", susie);
  67. {
  68. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\" "
  69. "loading=\"lazy\"/>\n", avatar);
  70. s = xs_str_cat(s, s1);
  71. }
  72. {
  73. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  74. xs_dict_get(actor, "id"), name);
  75. s = xs_str_cat(s, s1);
  76. }
  77. if (!xs_is_null(url)) {
  78. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", url);
  79. s = xs_str_cat(s, s1);
  80. }
  81. if (priv)
  82. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  83. if (xs_is_null(date)) {
  84. s = xs_str_cat(s, "<br>\n&nbsp;\n");
  85. }
  86. else {
  87. xs *sd = xs_crop(xs_dup(date), 0, 10);
  88. xs *s1 = xs_fmt(
  89. "<br>\n<time class=\"dt-published snac-pubdate\" title=\"%s\">%s</time>\n",
  90. date, sd);
  91. s = xs_str_cat(s, s1);
  92. }
  93. return xs_str_cat(os, s);
  94. }
  95. d_char *html_msg_icon(snac *snac, d_char *os, char *msg)
  96. {
  97. char *actor_id;
  98. xs *actor = NULL;
  99. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  100. actor_id = xs_dict_get(msg, "actor");
  101. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  102. char *date = NULL;
  103. char *url = NULL;
  104. int priv = 0;
  105. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0)
  106. url = xs_dict_get(msg, "id");
  107. priv = !is_msg_public(snac, msg);
  108. date = xs_dict_get(msg, "published");
  109. os = html_actor_icon(snac, os, actor, date, url, priv);
  110. }
  111. return os;
  112. }
  113. d_char *html_user_header(snac *snac, d_char *s, int local)
  114. /* creates the HTML header */
  115. {
  116. char *p, *v;
  117. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  118. s = xs_str_cat(s, "<meta name=\"viewport\" "
  119. "content=\"width=device-width, initial-scale=1\"/>\n");
  120. s = xs_str_cat(s, "<meta name=\"generator\" "
  121. "content=\"" USER_AGENT "\"/>\n");
  122. /* add server CSS */
  123. p = xs_dict_get(srv_config, "cssurls");
  124. while (xs_list_iter(&p, &v)) {
  125. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  126. s = xs_str_cat(s, s1);
  127. }
  128. /* add the user CSS */
  129. {
  130. xs *css = NULL;
  131. int size;
  132. if (valid_status(static_get(snac, "style.css", &css, &size))) {
  133. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  134. s = xs_str_cat(s, s1);
  135. }
  136. }
  137. {
  138. xs *s1 = xs_fmt("<title>%s (@%s@%s)</title>\n",
  139. xs_dict_get(snac->config, "name"),
  140. snac->uid,
  141. xs_dict_get(srv_config, "host"));
  142. s = xs_str_cat(s, s1);
  143. }
  144. s = xs_str_cat(s, "</head>\n<body>\n");
  145. /* top nav */
  146. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  147. {
  148. xs *s1;
  149. if (local)
  150. s1 = xs_fmt(
  151. "<a href=\"%s.rss\">%s</a> - "
  152. "<a href=\"%s/admin\" rel=\"nofollow\">%s</a></nav>\n",
  153. snac->actor, L("RSS"),
  154. snac->actor, L("private"));
  155. else
  156. s1 = xs_fmt(
  157. "<a href=\"%s\">%s</a> - "
  158. "<a href=\"%s/admin\">%s</a> - "
  159. "<a href=\"%s/people\">%s</a></nav>\n",
  160. snac->actor, L("public"),
  161. snac->actor, L("private"),
  162. snac->actor, L("people"));
  163. s = xs_str_cat(s, s1);
  164. }
  165. /* user info */
  166. {
  167. xs *bio = NULL;
  168. char *_tmpl =
  169. "<div class=\"h-card snac-top-user\">\n"
  170. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  171. "<p class=\"snac-top-user-id\">@%s@%s</p>\n"
  172. "<div class=\"p-note snac-top-user-bio\">%s</div>\n"
  173. "</div>\n";
  174. bio = not_really_markdown(xs_dict_get(snac->config, "bio"));
  175. xs *s1 = xs_fmt(_tmpl,
  176. xs_dict_get(snac->config, "name"),
  177. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"),
  178. bio
  179. );
  180. s = xs_str_cat(s, s1);
  181. }
  182. return s;
  183. }
  184. d_char *html_top_controls(snac *snac, d_char *s)
  185. /* generates the top controls */
  186. {
  187. char *_tmpl =
  188. "<div class=\"snac-top-controls\">\n"
  189. "<div class=\"snac-note\">\n"
  190. "<form method=\"post\" action=\"%s/admin/note\" enctype=\"multipart/form-data\">\n"
  191. "<textarea class=\"snac-textarea\" name=\"content\" "
  192. "rows=\"8\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  193. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  194. "<p><input type=\"checkbox\" name=\"sensitive\"> %s\n"
  195. "<p><input type=\"file\" name=\"attach\">\n"
  196. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  197. "</form><p>\n"
  198. "</div>\n"
  199. "<div class=\"snac-top-controls-more\">\n"
  200. "<details><summary>%s</summary>\n"
  201. "<form method=\"post\" action=\"%s/admin/action\">\n"
  202. "<input type=\"text\" name=\"actor\" required=\"required\">\n"
  203. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  204. "</form><p>\n"
  205. "<form method=\"post\" action=\"%s/admin/action\">\n"
  206. "<input type=\"text\" name=\"id\" required=\"required\">\n"
  207. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  208. "</form><p>\n"
  209. "<details><summary>%s</summary>\n"
  210. "<div class=\"snac-user-setup\">\n"
  211. "<form method=\"post\" action=\"%s/admin/user-setup\">\n"
  212. "<p>%s:<br>\n"
  213. "<input type=\"text\" name=\"name\" value=\"%s\"></p>\n"
  214. "<p>%s:<br>\n"
  215. "<input type=\"text\" name=\"avatar\" value=\"%s\"></p>\n"
  216. "<p>%s:<br>\n"
  217. "<textarea name=\"bio\" cols=\"40\" rows=\"4\">%s</textarea></p>\n"
  218. "<p><input type=\"checkbox\" name=\"cw\" id=\"cw\" %s>\n"
  219. "<label for=\"cw\">%s</label></p>\n"
  220. "<p>%s:<br>\n"
  221. "<input type=\"text\" name=\"email\" value=\"%s\"></p>\n"
  222. "<p>%s:<br>\n"
  223. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  224. "<p>%s:<br>\n"
  225. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  226. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  227. "</form>\n"
  228. "</div>\n"
  229. "</details>\n"
  230. "</details>\n"
  231. "</div>\n"
  232. "</div>\n";
  233. char *email = xs_dict_get(snac->config, "email");
  234. if (xs_is_null(email))
  235. email = "";
  236. char *cw = xs_dict_get(snac->config, "cw");
  237. if (xs_is_null(cw))
  238. cw = "";
  239. xs *s1 = xs_fmt(_tmpl,
  240. snac->actor,
  241. L("Sensitive content"),
  242. L("Post"),
  243. L("More options..."),
  244. snac->actor,
  245. L("Follow"), L("(by URL or user@host)"),
  246. snac->actor,
  247. L("Boost"), L("(by URL)"),
  248. L("User setup..."),
  249. snac->actor,
  250. L("User name"),
  251. xs_dict_get(snac->config, "name"),
  252. L("Avatar URL"),
  253. xs_dict_get(snac->config, "avatar"),
  254. L("Bio"),
  255. xs_dict_get(snac->config, "bio"),
  256. strcmp(cw, "open") == 0 ? "checked" : "",
  257. L("Always show sensitive content"),
  258. L("Email address for notifications"),
  259. email,
  260. L("Password (only to change it)"),
  261. L("Repeat Password"),
  262. L("Update user info")
  263. );
  264. s = xs_str_cat(s, s1);
  265. return s;
  266. }
  267. d_char *html_button(d_char *s, char *clss, char *label)
  268. {
  269. xs *s1 = xs_fmt(
  270. "<input type=\"submit\" name=\"action\" "
  271. "class=\"snac-btn-%s\" value=\"%s\">\n",
  272. clss, label);
  273. return xs_str_cat(s, s1);
  274. }
  275. d_char *build_mentions(snac *snac, char *msg)
  276. /* returns a string with the mentions in msg */
  277. {
  278. d_char *s = xs_str_new(NULL);
  279. char *list = xs_dict_get(msg, "tag");
  280. char *v;
  281. while (xs_list_iter(&list, &v)) {
  282. char *type = xs_dict_get(v, "type");
  283. char *href = xs_dict_get(v, "href");
  284. char *name = xs_dict_get(v, "name");
  285. if (type && strcmp(type, "Mention") == 0 &&
  286. href && strcmp(href, snac->actor) != 0 && name) {
  287. xs *s1 = NULL;
  288. if (name[0] != '@') {
  289. s1 = xs_fmt("@%s", name);
  290. name = s1;
  291. }
  292. xs *l = xs_split(name, "@");
  293. /* is it a name without a host? */
  294. if (xs_list_len(l) < 3) {
  295. /* split the href and pick the host name LIKE AN ANIMAL */
  296. /* would be better to query the webfinger but *won't do that* here */
  297. xs *l2 = xs_split(href, "/");
  298. if (xs_list_len(l2) >= 3) {
  299. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  300. s = xs_str_cat(s, s1);
  301. }
  302. }
  303. else {
  304. s = xs_str_cat(s, name);
  305. s = xs_str_cat(s, " ");
  306. }
  307. }
  308. }
  309. return s;
  310. }
  311. d_char *html_entry_controls(snac *snac, d_char *os, char *msg, const char *md5)
  312. {
  313. char *id = xs_dict_get(msg, "id");
  314. char *actor = xs_dict_get(msg, "attributedTo");
  315. xs *likes = object_likes(id);
  316. xs *boosts = object_announces(id);
  317. xs *s = xs_str_new(NULL);
  318. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  319. {
  320. xs *s1 = xs_fmt(
  321. "<form method=\"post\" action=\"%s/admin/action\">\n"
  322. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  323. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  324. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  325. "\n",
  326. snac->actor, id, actor, md5
  327. );
  328. s = xs_str_cat(s, s1);
  329. }
  330. if (xs_list_in(likes, snac->md5) == -1) {
  331. /* not already liked; add button */
  332. s = html_button(s, "like", L("Like"));
  333. }
  334. if (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) {
  335. /* not already boosted or us; add button */
  336. s = html_button(s, "boost", L("Boost"));
  337. }
  338. if (strcmp(actor, snac->actor) != 0) {
  339. /* controls for other actors than this one */
  340. if (following_check(snac, actor)) {
  341. s = html_button(s, "unfollow", L("Unfollow"));
  342. }
  343. else {
  344. s = html_button(s, "follow", L("Follow"));
  345. }
  346. s = html_button(s, "mute", L("MUTE"));
  347. }
  348. s = html_button(s, "delete", L("Delete"));
  349. s = html_button(s, "hide", L("Hide"));
  350. s = xs_str_cat(s, "</form>\n");
  351. {
  352. /* the post textarea */
  353. xs *ct = build_mentions(snac, msg);
  354. xs *s1 = xs_fmt(
  355. "<p><details><summary>%s</summary>\n"
  356. "<p><div class=\"snac-note\" id=\"%s_reply\">\n"
  357. "<form method=\"post\" action=\"%s/admin/note\" "
  358. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  359. "<textarea class=\"snac-textarea\" name=\"content\" "
  360. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  361. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  362. "<p><input type=\"checkbox\" name=\"sensitive\"> %s\n"
  363. "<p><input type=\"file\" name=\"attach\">\n"
  364. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  365. "</form><p></div>\n"
  366. "</details><p>"
  367. "\n",
  368. L("Reply..."),
  369. md5,
  370. snac->actor, md5,
  371. ct,
  372. id,
  373. L("Sensitive content"),
  374. L("Post")
  375. );
  376. s = xs_str_cat(s, s1);
  377. }
  378. s = xs_str_cat(s, "</div>\n");
  379. return xs_str_cat(os, s);
  380. }
  381. d_char *html_entry(snac *snac, d_char *os, char *msg, int local, int level, const char *md5)
  382. {
  383. char *id = xs_dict_get(msg, "id");
  384. char *type = xs_dict_get(msg, "type");
  385. char *actor;
  386. int sensitive = 0;
  387. char *v;
  388. xs *likes = NULL;
  389. xs *boosts = NULL;
  390. /* do not show non-public messages in the public timeline */
  391. if (local && !is_msg_public(snac, msg))
  392. return os;
  393. xs *s = xs_str_new(NULL);
  394. /* top wrap */
  395. if (is_hidden(snac, id))
  396. s = xs_str_cat(s, "<div style=\"display: none\">\n");
  397. else
  398. s = xs_str_cat(s, "<div>\n");
  399. {
  400. xs *s1 = xs_fmt("<a name=\"%s_entry\"></a>\n", md5);
  401. s = xs_str_cat(s, s1);
  402. }
  403. if (strcmp(type, "Follow") == 0) {
  404. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  405. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  406. s = xs_str_cat(s, s1);
  407. s = html_msg_icon(snac, s, msg);
  408. s = xs_str_cat(s, "</div>\n");
  409. return xs_str_cat(os, s);
  410. }
  411. else
  412. if (strcmp(type, "Note") != 0) {
  413. /* skip oddities */
  414. return os;
  415. }
  416. /* bring the main actor */
  417. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  418. return os;
  419. /* ignore muted morons immediately */
  420. if (is_muted(snac, actor))
  421. return os;
  422. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  423. return os;
  424. /* if this is our post, add the score */
  425. if (xs_startswith(id, snac->actor)) {
  426. likes = object_likes(id);
  427. boosts = object_announces(id);
  428. /* alternate emojis: %d &#128077; %d &#128257; */
  429. xs *s1 = xs_fmt(
  430. "<div class=\"snac-score\">%d &#9733; %d &#8634;</div>\n",
  431. xs_list_len(likes), xs_list_len(boosts));
  432. s = xs_str_cat(s, s1);
  433. }
  434. if (level == 0)
  435. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  436. else
  437. s = xs_str_cat(s, "<div class=\"snac-child\">\n");
  438. if (boosts == NULL)
  439. boosts = object_announces(id);
  440. if (xs_list_len(boosts)) {
  441. /* if somebody boosted this, show as origin */
  442. char *p = xs_list_get(boosts, -1);
  443. xs *actor_r = NULL;
  444. if (xs_list_in(boosts, snac->md5) != -1) {
  445. /* we boosted this */
  446. xs *s1 = xs_fmt(
  447. "<div class=\"snac-origin\">"
  448. "<a href=\"%s\">%s</a> %s</a></div>",
  449. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  450. );
  451. s = xs_str_cat(s, s1);
  452. }
  453. else
  454. if (valid_status(object_get_by_md5(p, &actor_r, NULL))) {
  455. char *name;
  456. if ((name = xs_dict_get(actor_r, "name")) == NULL)
  457. name = xs_dict_get(actor_r, "preferredUsername");
  458. if (!xs_is_null(name)) {
  459. xs *s1 = xs_fmt(
  460. "<div class=\"snac-origin\">"
  461. "<a href=\"%s\">%s</a> %s</div>\n",
  462. xs_dict_get(actor_r, "id"),
  463. name,
  464. L("boosted")
  465. );
  466. s = xs_str_cat(s, s1);
  467. }
  468. }
  469. }
  470. else
  471. if (strcmp(type, "Note") == 0) {
  472. /* is the parent not here? */
  473. char *parent = xs_dict_get(msg, "inReplyTo");
  474. if (!xs_is_null(parent) && *parent && !object_here(parent)) {
  475. xs *s1 = xs_fmt(
  476. "<div class=\"snac-origin\">%s "
  477. "<a href=\"%s\">»</a></div>\n",
  478. L("in reply to"), parent
  479. );
  480. s = xs_str_cat(s, s1);
  481. }
  482. }
  483. s = html_msg_icon(snac, s, msg);
  484. /* add the content */
  485. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n");
  486. /* is it sensitive? */
  487. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  488. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  489. v = "...";
  490. /* only show it when not in the public timeline and the config setting is "open" */
  491. char *cw = xs_dict_get(snac->config, "cw");
  492. if (xs_is_null(cw) || local)
  493. cw = "";
  494. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, v, L("SENSITIVE CONTENT"));
  495. s = xs_str_cat(s, s1);
  496. sensitive = 1;
  497. }
  498. #if 0
  499. {
  500. xs *md5 = xs_md5_hex(id, strlen(id));
  501. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  502. s = xs_str_cat(s, s1);
  503. }
  504. #endif
  505. {
  506. xs *c = sanitize(xs_dict_get(msg, "content"));
  507. char *p, *v;
  508. /* do some tweaks to the content */
  509. c = xs_replace_i(c, "\r", "");
  510. while (xs_endswith(c, "<br><br>"))
  511. c = xs_crop(c, 0, -4);
  512. c = xs_replace_i(c, "<br><br>", "<p>");
  513. if (!xs_startswith(c, "<p>")) {
  514. xs *s1 = c;
  515. c = xs_fmt("<p>%s</p>", s1);
  516. }
  517. /* replace the :shortnames: */
  518. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  519. /* iterate the tags */
  520. while (xs_list_iter(&p, &v)) {
  521. char *t = xs_dict_get(v, "type");
  522. if (t && strcmp(t, "Emoji") == 0) {
  523. char *n = xs_dict_get(v, "name");
  524. char *i = xs_dict_get(v, "icon");
  525. if (n && i) {
  526. char *u = xs_dict_get(i, "url");
  527. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\" "
  528. "loading=\"lazy\"/>", u);
  529. c = xs_replace_i(c, n, img);
  530. }
  531. }
  532. }
  533. }
  534. s = xs_str_cat(s, c);
  535. }
  536. s = xs_str_cat(s, "\n");
  537. /* add the attachments */
  538. char *attach;
  539. if ((attach = xs_dict_get(msg, "attachment")) != NULL) {
  540. char *v;
  541. while (xs_list_iter(&attach, &v)) {
  542. char *t = xs_dict_get(v, "mediaType");
  543. if (xs_is_null(t))
  544. continue;
  545. if (xs_startswith(t, "image/")) {
  546. char *url = xs_dict_get(v, "url");
  547. char *name = xs_dict_get(v, "name");
  548. if (url != NULL) {
  549. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\" loading=\"lazy\"/></p>\n",
  550. url, xs_is_null(name) ? "" : name);
  551. s = xs_str_cat(s, s1);
  552. }
  553. }
  554. else
  555. if (xs_startswith(t, "video/")) {
  556. char *url = xs_dict_get(v, "url");
  557. if (url != NULL) {
  558. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  559. s = xs_str_cat(s, s1);
  560. }
  561. }
  562. }
  563. }
  564. if (sensitive)
  565. s = xs_str_cat(s, "</details><p>\n");
  566. s = xs_str_cat(s, "</div>\n");
  567. /** controls **/
  568. if (!local)
  569. s = html_entry_controls(snac, s, msg, md5);
  570. /** children **/
  571. xs *children = object_children(id);
  572. int left = xs_list_len(children);
  573. if (left) {
  574. char *p, *cmd5;
  575. if (level < 4)
  576. s = xs_str_cat(s, "<div class=\"snac-children\">\n");
  577. else
  578. s = xs_str_cat(s, "<div>\n");
  579. if (left > 3)
  580. s = xs_str_cat(s, "<details><summary>...</summary>\n");
  581. p = children;
  582. while (xs_list_iter(&p, &cmd5)) {
  583. xs *chd = NULL;
  584. object_get_by_md5(cmd5, &chd, NULL);
  585. if (left == 3)
  586. s = xs_str_cat(s, "</details>\n");
  587. if (chd != NULL)
  588. s = html_entry(snac, s, chd, local, level + 1, cmd5);
  589. else
  590. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", cmd5));
  591. left--;
  592. }
  593. s = xs_str_cat(s, "</div>\n");
  594. }
  595. s = xs_str_cat(s, "</div>\n</div>\n");
  596. return xs_str_cat(os, s);
  597. }
  598. d_char *html_user_footer(snac *snac, d_char *s)
  599. {
  600. xs *s1 = xs_fmt(
  601. "<div class=\"snac-footer\">\n"
  602. "<a href=\"%s\">%s</a> - "
  603. "powered by <abbr title=\"Social Networks Are Crap\">snac</abbr></div>\n",
  604. srv_baseurl,
  605. L("about this site")
  606. );
  607. return xs_str_cat(s, s1);
  608. }
  609. d_char *html_timeline(snac *snac, char *list, int local, int skip, int show, int show_more)
  610. /* returns the HTML for the timeline */
  611. {
  612. d_char *s = xs_str_new(NULL);
  613. char *v;
  614. double t = ftime();
  615. s = html_user_header(snac, s, local);
  616. if (!local)
  617. s = html_top_controls(snac, s);
  618. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  619. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  620. while (xs_list_iter(&list, &v)) {
  621. xs *msg = NULL;
  622. if (!valid_status(object_get_by_md5(v, &msg, NULL)))
  623. continue;
  624. s = html_entry(snac, s, msg, local, 0, v);
  625. }
  626. s = xs_str_cat(s, "</div>\n");
  627. if (local) {
  628. xs *s1 = xs_fmt(
  629. "<div class=\"snac-history\">\n"
  630. "<p class=\"snac-history-title\">%s</p><ul>\n",
  631. L("History")
  632. );
  633. s = xs_str_cat(s, s1);
  634. xs *list = history_list(snac);
  635. char *p, *v;
  636. p = list;
  637. while (xs_list_iter(&p, &v)) {
  638. xs *fn = xs_replace(v, ".html", "");
  639. xs *s1 = xs_fmt(
  640. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  641. snac->actor, v, fn);
  642. s = xs_str_cat(s, s1);
  643. }
  644. s = xs_str_cat(s, "</ul></div>\n");
  645. }
  646. s = html_user_footer(snac, s);
  647. {
  648. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  649. s = xs_str_cat(s, s1);
  650. }
  651. if (show_more) {
  652. xs *s1 = xs_fmt(
  653. "<p>"
  654. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  655. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("More..."));
  656. s = xs_str_cat(s, s1);
  657. }
  658. s = xs_str_cat(s, "</body>\n</html>\n");
  659. return s;
  660. }
  661. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  662. {
  663. xs *s = xs_str_new(NULL);
  664. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  665. char *p, *actor_id;
  666. s = xs_str_cat(s, h);
  667. p = list;
  668. while (xs_list_iter(&p, &actor_id)) {
  669. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  670. xs *actor = NULL;
  671. if (valid_status(actor_get(snac, actor_id, &actor))) {
  672. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  673. s = html_actor_icon(snac, s, actor, xs_dict_get(actor, "published"), NULL, 0);
  674. /* content (user bio) */
  675. char *c = xs_dict_get(actor, "summary");
  676. if (!xs_is_null(c)) {
  677. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  678. xs *sc = sanitize(c);
  679. if (xs_startswith(sc, "<p>"))
  680. s = xs_str_cat(s, sc);
  681. else {
  682. xs *s1 = xs_fmt("<p>%s</p>", sc);
  683. s = xs_str_cat(s, s1);
  684. }
  685. s = xs_str_cat(s, "</div>\n");
  686. }
  687. /* buttons */
  688. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  689. xs *s1 = xs_fmt(
  690. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  691. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n",
  692. snac->actor, actor_id,
  693. md5, t
  694. );
  695. s = xs_str_cat(s, s1);
  696. if (following_check(snac, actor_id))
  697. s = html_button(s, "unfollow", L("Unfollow"));
  698. else
  699. s = html_button(s, "follow", L("Follow"));
  700. if (is_muted(snac, actor_id))
  701. s = html_button(s, "unmute", L("Unmute"));
  702. else
  703. s = html_button(s, "mute", L("MUTE"));
  704. s = xs_str_cat(s, "</form>\n");
  705. /* the post textarea */
  706. xs *s2 = xs_fmt(
  707. "<p><details><summary>%s</summary>\n"
  708. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  709. "<form method=\"post\" action=\"%s/admin/note\" "
  710. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  711. "<textarea class=\"snac-textarea\" name=\"content\" "
  712. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  713. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  714. "<p><input type=\"file\" name=\"attach\">\n"
  715. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  716. "</form><p></div>\n"
  717. "</details><p>\n",
  718. L("Direct Message..."),
  719. md5, t,
  720. snac->actor, md5,
  721. actor_id,
  722. L("Post")
  723. );
  724. s = xs_str_cat(s, s2);
  725. s = xs_str_cat(s, "</div>\n");
  726. s = xs_str_cat(s, "</div>\n");
  727. }
  728. }
  729. return xs_str_cat(os, s);
  730. }
  731. d_char *html_people(snac *snac)
  732. {
  733. d_char *s = xs_str_new(NULL);
  734. xs *wing = following_list(snac);
  735. xs *wers = follower_list(snac);
  736. s = html_user_header(snac, s, 0);
  737. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  738. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  739. s = html_user_footer(snac, s);
  740. s = xs_str_cat(s, "</body>\n</html>\n");
  741. return s;
  742. }
  743. int html_get_handler(d_char *req, char *q_path, char **body, int *b_size, char **ctype)
  744. {
  745. char *accept = xs_dict_get(req, "accept");
  746. int status = 404;
  747. snac snac;
  748. xs *uid = NULL;
  749. char *p_path;
  750. int cache = 1;
  751. int save = 1;
  752. char *v;
  753. xs *l = xs_split_n(q_path, "/", 2);
  754. v = xs_list_get(l, 1);
  755. if (xs_is_null(v)) {
  756. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  757. return 404;
  758. }
  759. uid = xs_dup(v);
  760. /* rss extension? */
  761. if (xs_endswith(uid, ".rss")) {
  762. uid = xs_crop(uid, 0, -4);
  763. p_path = ".rss";
  764. }
  765. else
  766. p_path = xs_list_get(l, 2);
  767. if (!uid || !user_open(&snac, uid)) {
  768. /* invalid user */
  769. srv_log(xs_fmt("html_get_handler bad user %s", uid));
  770. return 404;
  771. }
  772. /* return the RSS if requested by Accept header */
  773. if (accept != NULL) {
  774. if (xs_str_in(accept, "text/xml") != -1 ||
  775. xs_str_in(accept, "application/rss+xml") != -1)
  776. p_path = ".rss";
  777. }
  778. /* check if server config variable 'disable_cache' is set */
  779. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  780. cache = 0;
  781. int skip = 0;
  782. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  783. char *q_vars = xs_dict_get(req, "q_vars");
  784. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  785. skip = atoi(v), cache = 0, save = 0;
  786. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  787. show = atoi(v), cache = 0, save = 0;
  788. if (p_path == NULL) {
  789. /* public timeline */
  790. xs *h = xs_str_localtime(0, "%Y-%m.html");
  791. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  792. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  793. *body = history_get(&snac, h);
  794. *b_size = strlen(*body);
  795. status = 200;
  796. }
  797. else {
  798. xs *list = timeline_list(&snac, "public", skip, show);
  799. xs *next = timeline_list(&snac, "public", skip + show, 1);
  800. *body = html_timeline(&snac, list, 1, skip, show, xs_list_len(next));
  801. *b_size = strlen(*body);
  802. status = 200;
  803. if (save)
  804. history_add(&snac, h, *body, *b_size);
  805. }
  806. }
  807. else
  808. if (strcmp(p_path, "admin") == 0) {
  809. /* private timeline */
  810. if (!login(&snac, req))
  811. status = 401;
  812. else {
  813. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  814. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  815. *body = history_get(&snac, "timeline.html_");
  816. *b_size = strlen(*body);
  817. status = 200;
  818. }
  819. else {
  820. snac_debug(&snac, 1, xs_fmt("building timeline"));
  821. xs *list = timeline_list(&snac, "private", skip, show);
  822. xs *next = timeline_list(&snac, "private", skip + show, 1);
  823. *body = html_timeline(&snac, list, 0, skip, show, xs_list_len(next));
  824. *b_size = strlen(*body);
  825. status = 200;
  826. if (save)
  827. history_add(&snac, "timeline.html_", *body, *b_size);
  828. }
  829. }
  830. }
  831. else
  832. if (strcmp(p_path, "people") == 0) {
  833. /* the list of people */
  834. if (!login(&snac, req))
  835. status = 401;
  836. else {
  837. *body = html_people(&snac);
  838. *b_size = strlen(*body);
  839. status = 200;
  840. }
  841. }
  842. else
  843. if (xs_startswith(p_path, "p/")) {
  844. /* a timeline with just one entry */
  845. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  846. xs *msg = NULL;
  847. if (valid_status(object_get(id, &msg, NULL))) {
  848. xs *md5 = xs_md5_hex(id, strlen(id));
  849. xs *list = xs_list_new();
  850. list = xs_list_append(list, md5);
  851. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  852. *b_size = strlen(*body);
  853. status = 200;
  854. }
  855. }
  856. else
  857. if (xs_startswith(p_path, "s/")) {
  858. /* a static file */
  859. xs *l = xs_split(p_path, "/");
  860. char *id = xs_list_get(l, 1);
  861. int sz;
  862. if (valid_status(static_get(&snac, id, body, &sz))) {
  863. *b_size = sz;
  864. *ctype = xs_mime_by_ext(id);
  865. status = 200;
  866. }
  867. }
  868. else
  869. if (xs_startswith(p_path, "h/")) {
  870. /* an entry from the history */
  871. xs *l = xs_split(p_path, "/");
  872. char *id = xs_list_get(l, 1);
  873. if ((*body = history_get(&snac, id)) != NULL) {
  874. *b_size = strlen(*body);
  875. status = 200;
  876. }
  877. }
  878. else
  879. if (strcmp(p_path, ".rss") == 0) {
  880. /* public timeline in RSS format */
  881. d_char *rss;
  882. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  883. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"));
  884. char *p, *v;
  885. /* escape tags */
  886. bio = xs_replace_i(bio, "<", "&lt;");
  887. bio = xs_replace_i(bio, ">", "&gt;");
  888. rss = xs_fmt(
  889. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  890. "<rss version=\"0.91\">\n"
  891. "<channel>\n"
  892. "<title>%s (@%s@%s)</title>\n"
  893. "<language>en</language>\n"
  894. "<link>%s.rss</link>\n"
  895. "<description>%s</description>\n",
  896. xs_dict_get(snac.config, "name"),
  897. snac.uid,
  898. xs_dict_get(srv_config, "host"),
  899. snac.actor,
  900. bio
  901. );
  902. p = elems;
  903. while (xs_list_iter(&p, &v)) {
  904. xs *msg = NULL;
  905. if (!valid_status(object_get_by_md5(v, &msg, NULL)))
  906. continue;
  907. char *id = xs_dict_get(msg, "id");
  908. if (!xs_startswith(id, snac.actor))
  909. continue;
  910. xs *content = sanitize(xs_dict_get(msg, "content"));
  911. xs *title = xs_str_new(NULL);
  912. int i;
  913. /* escape tags */
  914. content = xs_replace_i(content, "<", "&lt;");
  915. content = xs_replace_i(content, ">", "&gt;");
  916. for (i = 0; content[i] && content[i] != '<' && content[i] != '&' && i < 40; i++)
  917. title = xs_append_m(title, &content[i], 1);
  918. xs *s = xs_fmt(
  919. "<item>\n"
  920. "<title>%s...</title>\n"
  921. "<link>%s</link>\n"
  922. "<description>%s</description>\n"
  923. "</item>\n",
  924. title, id, content
  925. );
  926. rss = xs_str_cat(rss, s);
  927. }
  928. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  929. *body = rss;
  930. *b_size = strlen(rss);
  931. *ctype = "application/rss+xml; charset=utf-8";
  932. status = 200;
  933. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  934. }
  935. else
  936. status = 404;
  937. user_free(&snac);
  938. if (valid_status(status) && *ctype == NULL) {
  939. *ctype = "text/html; charset=utf-8";
  940. }
  941. return status;
  942. }
  943. int html_post_handler(d_char *req, char *q_path, d_char *payload, int p_size,
  944. char **body, int *b_size, char **ctype)
  945. {
  946. int status = 0;
  947. snac snac;
  948. char *uid, *p_path;
  949. char *p_vars;
  950. xs *l = xs_split_n(q_path, "/", 2);
  951. uid = xs_list_get(l, 1);
  952. if (!uid || !user_open(&snac, uid)) {
  953. /* invalid user */
  954. srv_log(xs_fmt("html_post_handler bad user %s", uid));
  955. return 404;
  956. }
  957. p_path = xs_list_get(l, 2);
  958. /* all posts must be authenticated */
  959. if (!login(&snac, req)) {
  960. user_free(&snac);
  961. return 401;
  962. }
  963. p_vars = xs_dict_get(req, "p_vars");
  964. #if 0
  965. {
  966. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  967. printf("%s\n", j1);
  968. }
  969. #endif
  970. if (p_path && strcmp(p_path, "admin/note") == 0) {
  971. /* post note */
  972. char *content = xs_dict_get(p_vars, "content");
  973. char *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  974. char *attach_url = xs_dict_get(p_vars, "attach_url");
  975. char *attach_file = xs_dict_get(p_vars, "attach");
  976. char *to = xs_dict_get(p_vars, "to");
  977. char *sensitive = xs_dict_get(p_vars, "sensitive");
  978. xs *attach_list = xs_list_new();
  979. /* is attach_url set? */
  980. if (!xs_is_null(attach_url) && *attach_url != '\0')
  981. attach_list = xs_list_append(attach_list, attach_url);
  982. /* is attach_file set? */
  983. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  984. char *fn = xs_list_get(attach_file, 0);
  985. if (*fn != '\0') {
  986. char *ext = strrchr(fn, '.');
  987. xs *ntid = tid(0);
  988. xs *id = xs_fmt("%s%s", ntid, ext);
  989. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  990. int fo = xs_number_get(xs_list_get(attach_file, 1));
  991. int fs = xs_number_get(xs_list_get(attach_file, 2));
  992. /* store */
  993. static_put(&snac, id, payload + fo, fs);
  994. attach_list = xs_list_append(attach_list, url);
  995. }
  996. }
  997. if (content != NULL) {
  998. xs *msg = NULL;
  999. xs *c_msg = NULL;
  1000. xs *content_2 = xs_replace(content, "\r", "");
  1001. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list);
  1002. if (sensitive != NULL) {
  1003. xs *t = xs_val_new(XSTYPE_TRUE);
  1004. msg = xs_dict_set(msg, "sensitive", t);
  1005. msg = xs_dict_set(msg, "summary", "...");
  1006. }
  1007. c_msg = msg_create(&snac, msg);
  1008. post(&snac, c_msg);
  1009. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  1010. }
  1011. status = 303;
  1012. }
  1013. else
  1014. if (p_path && strcmp(p_path, "admin/action") == 0) {
  1015. /* action on an entry */
  1016. char *id = xs_dict_get(p_vars, "id");
  1017. char *actor = xs_dict_get(p_vars, "actor");
  1018. char *action = xs_dict_get(p_vars, "action");
  1019. if (action == NULL)
  1020. return 404;
  1021. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1022. status = 303;
  1023. if (strcmp(action, L("Like")) == 0) {
  1024. xs *msg = msg_admiration(&snac, id, "Like");
  1025. if (msg != NULL) {
  1026. post(&snac, msg);
  1027. timeline_admire(&snac, msg, id, snac.actor, 1);
  1028. }
  1029. }
  1030. else
  1031. if (strcmp(action, L("Boost")) == 0) {
  1032. xs *msg = msg_admiration(&snac, id, "Announce");
  1033. if (msg != NULL) {
  1034. post(&snac, msg);
  1035. timeline_admire(&snac, msg, id, snac.actor, 0);
  1036. }
  1037. }
  1038. else
  1039. if (strcmp(action, L("MUTE")) == 0) {
  1040. mute(&snac, actor);
  1041. }
  1042. else
  1043. if (strcmp(action, L("Unmute")) == 0) {
  1044. unmute(&snac, actor);
  1045. }
  1046. else
  1047. if (strcmp(action, L("Hide")) == 0) {
  1048. hide(&snac, id);
  1049. }
  1050. else
  1051. if (strcmp(action, L("Follow")) == 0) {
  1052. xs *msg = msg_follow(&snac, actor);
  1053. if (msg != NULL) {
  1054. /* reload the actor from the message, in may be different */
  1055. actor = xs_dict_get(msg, "object");
  1056. following_add(&snac, actor, msg);
  1057. enqueue_output_by_actor(&snac, msg, actor, 0);
  1058. }
  1059. }
  1060. else
  1061. if (strcmp(action, L("Unfollow")) == 0) {
  1062. /* get the following object */
  1063. xs *object = NULL;
  1064. if (valid_status(following_get(&snac, actor, &object))) {
  1065. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1066. following_del(&snac, actor);
  1067. enqueue_output_by_actor(&snac, msg, actor, 0);
  1068. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1069. }
  1070. else
  1071. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1072. }
  1073. else
  1074. if (strcmp(action, L("Delete")) == 0) {
  1075. /* delete an entry */
  1076. if (xs_startswith(id, snac.actor)) {
  1077. /* it's a post by us: generate a delete */
  1078. xs *msg = msg_delete(&snac, id);
  1079. post(&snac, msg);
  1080. /* FIXME: also post this Tombstone to people
  1081. that Announce'd it */
  1082. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1083. }
  1084. timeline_del(&snac, id);
  1085. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1086. }
  1087. else
  1088. status = 404;
  1089. /* delete the cached timeline */
  1090. if (status == 303)
  1091. history_del(&snac, "timeline.html_");
  1092. }
  1093. else
  1094. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  1095. /* change of user data */
  1096. char *v;
  1097. char *p1, *p2;
  1098. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1099. snac.config = xs_dict_set(snac.config, "name", v);
  1100. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1101. snac.config = xs_dict_set(snac.config, "avatar", v);
  1102. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1103. snac.config = xs_dict_set(snac.config, "bio", v);
  1104. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1105. strcmp(v, "on") == 0) {
  1106. snac.config = xs_dict_set(snac.config, "cw", "open");
  1107. } else { /* if the checkbox is not set, the parameter is missing */
  1108. snac.config = xs_dict_set(snac.config, "cw", "");
  1109. }
  1110. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1111. snac.config = xs_dict_set(snac.config, "email", v);
  1112. /* password change? */
  1113. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1114. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1115. *p1 && strcmp(p1, p2) == 0) {
  1116. xs *pw = hash_password(snac.uid, p1, NULL);
  1117. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1118. }
  1119. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1120. xs *bfn = xs_fmt("%s.bak", fn);
  1121. FILE *f;
  1122. rename(fn, bfn);
  1123. if ((f = fopen(fn, "w")) != NULL) {
  1124. xs *j = xs_json_dumps_pp(snac.config, 4);
  1125. fwrite(j, strlen(j), 1, f);
  1126. fclose(f);
  1127. }
  1128. else
  1129. rename(bfn, fn);
  1130. history_del(&snac, "timeline.html_");
  1131. xs *a_msg = msg_actor(&snac);
  1132. xs *u_msg = msg_update(&snac, a_msg);
  1133. post(&snac, u_msg);
  1134. status = 303;
  1135. }
  1136. if (status == 303) {
  1137. char *redir = xs_dict_get(p_vars, "redir");
  1138. if (xs_is_null(redir))
  1139. redir = "snac-posts";
  1140. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1141. *b_size = strlen(*body);
  1142. }
  1143. user_free(&snac);
  1144. return status;
  1145. }