html.c 46 KB

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