html.c 45 KB

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