html.c 42 KB

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