html.c 39 KB

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