html.c 41 KB

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