html.c 41 KB

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