html.c 55 KB

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