html.c 55 KB

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