html.c 40 KB

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