html.c 50 KB

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