html.c 40 KB

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