html.c 41 KB

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