html.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902
  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. 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. /* 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. xs_str *html_user_footer(xs_str *s)
  770. {
  771. xs *s1 = xs_fmt(
  772. "<div class=\"snac-footer\">\n"
  773. "<a href=\"%s\">%s</a> - "
  774. "powered by <a href=\"%s\""
  775. "<abbr title=\"Social Networks Are Crap\">snac</abbr></div></a>\n",
  776. srv_baseurl,
  777. L("about this site"),
  778. WHAT_IS_SNAC_URL
  779. );
  780. return xs_str_cat(s, s1);
  781. }
  782. d_char *html_timeline(snac *snac, char *list, int local, int skip, int show, int show_more)
  783. /* returns the HTML for the timeline */
  784. {
  785. d_char *s = xs_str_new(NULL);
  786. char *v;
  787. double t = ftime();
  788. s = html_user_header(snac, s, local);
  789. if (!local)
  790. s = html_top_controls(snac, s);
  791. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  792. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  793. while (xs_list_iter(&list, &v)) {
  794. xs *msg = NULL;
  795. if (!valid_status(timeline_get_by_md5(snac, v, &msg)))
  796. continue;
  797. s = html_entry(snac, s, msg, local, 0, v, 0);
  798. }
  799. s = xs_str_cat(s, "</div>\n");
  800. if (local) {
  801. xs *s1 = xs_fmt(
  802. "<div class=\"snac-history\">\n"
  803. "<p class=\"snac-history-title\">%s</p><ul>\n",
  804. L("History")
  805. );
  806. s = xs_str_cat(s, s1);
  807. xs *list = history_list(snac);
  808. char *p, *v;
  809. p = list;
  810. while (xs_list_iter(&p, &v)) {
  811. xs *fn = xs_replace(v, ".html", "");
  812. xs *s1 = xs_fmt(
  813. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  814. snac->actor, v, fn);
  815. s = xs_str_cat(s, s1);
  816. }
  817. s = xs_str_cat(s, "</ul></div>\n");
  818. }
  819. {
  820. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  821. s = xs_str_cat(s, s1);
  822. }
  823. if (show_more) {
  824. xs *s1 = xs_fmt(
  825. "<p>"
  826. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  827. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("Load more..."));
  828. s = xs_str_cat(s, s1);
  829. }
  830. s = html_user_footer(s);
  831. s = xs_str_cat(s, "</body>\n</html>\n");
  832. return s;
  833. }
  834. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  835. {
  836. xs *s = xs_str_new(NULL);
  837. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  838. char *p, *actor_id;
  839. s = xs_str_cat(s, h);
  840. p = list;
  841. while (xs_list_iter(&p, &actor_id)) {
  842. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  843. xs *actor = NULL;
  844. if (valid_status(actor_get(snac, actor_id, &actor))) {
  845. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  846. s = html_actor_icon(s, actor, xs_dict_get(actor, "published"), NULL, NULL, 0);
  847. /* content (user bio) */
  848. char *c = xs_dict_get(actor, "summary");
  849. if (!xs_is_null(c)) {
  850. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  851. xs *sc = sanitize(c);
  852. if (xs_startswith(sc, "<p>"))
  853. s = xs_str_cat(s, sc);
  854. else {
  855. xs *s1 = xs_fmt("<p>%s</p>", sc);
  856. s = xs_str_cat(s, s1);
  857. }
  858. s = xs_str_cat(s, "</div>\n");
  859. }
  860. /* buttons */
  861. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  862. xs *s1 = xs_fmt(
  863. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  864. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  865. "<input type=\"hidden\" name=\"actor-form\" value=\"yes\">\n",
  866. snac->actor, actor_id
  867. );
  868. s = xs_str_cat(s, s1);
  869. if (following_check(snac, actor_id))
  870. s = html_button(s, "unfollow", L("Unfollow"));
  871. else {
  872. s = html_button(s, "follow", L("Follow"));
  873. if (follower_check(snac, actor_id))
  874. s = html_button(s, "delete", L("Delete"));
  875. }
  876. if (is_muted(snac, actor_id))
  877. s = html_button(s, "unmute", L("Unmute"));
  878. else
  879. s = html_button(s, "mute", L("MUTE"));
  880. s = xs_str_cat(s, "</form>\n");
  881. /* the post textarea */
  882. xs *s2 = xs_fmt(
  883. "<p><details><summary>%s</summary>\n"
  884. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  885. "<form method=\"post\" action=\"%s/admin/note\" "
  886. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  887. "<textarea class=\"snac-textarea\" name=\"content\" "
  888. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  889. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  890. "<p><input type=\"file\" name=\"attach\">\n"
  891. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  892. "</form><p></div>\n"
  893. "</details><p>\n",
  894. L("Direct Message..."),
  895. md5, t,
  896. snac->actor, md5,
  897. actor_id,
  898. L("Post")
  899. );
  900. s = xs_str_cat(s, s2);
  901. s = xs_str_cat(s, "</div>\n");
  902. s = xs_str_cat(s, "</div>\n");
  903. }
  904. }
  905. return xs_str_cat(os, s);
  906. }
  907. d_char *html_people(snac *snac)
  908. {
  909. d_char *s = xs_str_new(NULL);
  910. xs *wing = following_list(snac);
  911. xs *wers = follower_list(snac);
  912. s = html_user_header(snac, s, 0);
  913. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  914. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  915. s = html_user_footer(s);
  916. s = xs_str_cat(s, "</body>\n</html>\n");
  917. return s;
  918. }
  919. xs_str *html_notifications(snac *snac)
  920. {
  921. xs_str *s = xs_str_new(NULL);
  922. xs *n_list = notify_list(snac, 0);
  923. xs *n_time = notify_check_time(snac, 0);
  924. xs_list *p = n_list;
  925. xs_str *v;
  926. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  927. s = html_user_header(snac, s, 0);
  928. xs *s1 = xs_fmt(
  929. "<form method=\"post\" action=\"%s/admin/clear-notifications\" id=\"clear\">\n"
  930. "<input type=\"submit\" class=\"snac-btn-like\" value=\"%s\">\n"
  931. "</form><p>", snac->actor, L("Clear all"));
  932. s = xs_str_cat(s, s1);
  933. while (xs_list_iter(&p, &v)) {
  934. xs *noti = notify_get(snac, v);
  935. if (noti == NULL)
  936. continue;
  937. xs *obj = NULL;
  938. const char *type = xs_dict_get(noti, "type");
  939. const char *id = xs_dict_get(noti, "objid");
  940. if (!valid_status(object_get(id, &obj)))
  941. continue;
  942. if (is_hidden(snac, id))
  943. continue;
  944. const char *actor_id = xs_dict_get(noti, "actor");
  945. xs *actor = NULL;
  946. if (!valid_status(actor_get(snac, actor_id, &actor)))
  947. continue;
  948. xs *a_name = actor_name(actor);
  949. if (strcmp(v, n_time) > 0) {
  950. /* unseen notification */
  951. if (stage == NHDR_NONE) {
  952. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("New"));
  953. s = xs_str_cat(s, s1);
  954. stage = NHDR_NEW;
  955. }
  956. }
  957. else {
  958. /* already seen notification */
  959. if (stage != NHDR_OLD) {
  960. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("Already seen"));
  961. s = xs_str_cat(s, s1);
  962. stage = NHDR_OLD;
  963. }
  964. }
  965. s = xs_str_cat(s, "<div>\n");
  966. xs *s1 = xs_fmt("<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  967. strcmp(type, "Create") == 0 ? "Mention" : type, actor_id, a_name);
  968. s = xs_str_cat(s, s1);
  969. if (strcmp(type, "Follow") == 0) {
  970. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  971. s = html_msg_icon(snac, s, obj);
  972. s = xs_str_cat(s, "</div>\n");
  973. }
  974. else {
  975. xs *md5 = xs_md5_hex(id, strlen(id));
  976. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  977. }
  978. s = xs_str_cat(s, "</div>\n");
  979. }
  980. if (stage == NHDR_NONE) {
  981. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("None"));
  982. s = xs_str_cat(s, s1);
  983. }
  984. s = html_user_footer(s);
  985. s = xs_str_cat(s, "</body>\n</html>\n");
  986. /* set the check time to now */
  987. xs *dummy = notify_check_time(snac, 1);
  988. dummy = xs_free(dummy);
  989. timeline_touch(snac);
  990. return s;
  991. }
  992. int html_get_handler(const xs_dict *req, const char *q_path,
  993. char **body, int *b_size, char **ctype)
  994. {
  995. char *accept = xs_dict_get(req, "accept");
  996. int status = 404;
  997. snac snac;
  998. xs *uid = NULL;
  999. char *p_path;
  1000. int cache = 1;
  1001. int save = 1;
  1002. char *v;
  1003. xs *l = xs_split_n(q_path, "/", 2);
  1004. v = xs_list_get(l, 1);
  1005. if (xs_is_null(v)) {
  1006. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1007. return 404;
  1008. }
  1009. uid = xs_dup(v);
  1010. /* rss extension? */
  1011. if (xs_endswith(uid, ".rss")) {
  1012. uid = xs_crop_i(uid, 0, -4);
  1013. p_path = ".rss";
  1014. }
  1015. else
  1016. p_path = xs_list_get(l, 2);
  1017. if (!uid || !user_open(&snac, uid)) {
  1018. /* invalid user */
  1019. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1020. return 404;
  1021. }
  1022. /* return the RSS if requested by Accept header */
  1023. if (accept != NULL) {
  1024. if (xs_str_in(accept, "text/xml") != -1 ||
  1025. xs_str_in(accept, "application/rss+xml") != -1)
  1026. p_path = ".rss";
  1027. }
  1028. /* check if server config variable 'disable_cache' is set */
  1029. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1030. cache = 0;
  1031. int skip = 0;
  1032. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1033. char *q_vars = xs_dict_get(req, "q_vars");
  1034. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1035. skip = atoi(v), cache = 0, save = 0;
  1036. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1037. show = atoi(v), cache = 0, save = 0;
  1038. if (p_path == NULL) {
  1039. /* public timeline */
  1040. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1041. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1042. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1043. *body = history_get(&snac, h);
  1044. *b_size = strlen(*body);
  1045. status = 200;
  1046. }
  1047. else {
  1048. xs *list = timeline_list(&snac, "public", skip, show);
  1049. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1050. *body = html_timeline(&snac, list, 1, skip, show, xs_list_len(next));
  1051. *b_size = strlen(*body);
  1052. status = 200;
  1053. if (save)
  1054. history_add(&snac, h, *body, *b_size);
  1055. }
  1056. }
  1057. else
  1058. if (strcmp(p_path, "admin") == 0) {
  1059. /* private timeline */
  1060. if (!login(&snac, req))
  1061. status = 401;
  1062. else {
  1063. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1064. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1065. *body = history_get(&snac, "timeline.html_");
  1066. *b_size = strlen(*body);
  1067. status = 200;
  1068. }
  1069. else {
  1070. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1071. xs *list = timeline_list(&snac, "private", skip, show);
  1072. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1073. *body = html_timeline(&snac, list, 0, skip, show, xs_list_len(next));
  1074. *b_size = strlen(*body);
  1075. status = 200;
  1076. if (save)
  1077. history_add(&snac, "timeline.html_", *body, *b_size);
  1078. }
  1079. }
  1080. }
  1081. else
  1082. if (strcmp(p_path, "people") == 0) {
  1083. /* the list of people */
  1084. if (!login(&snac, req))
  1085. status = 401;
  1086. else {
  1087. *body = html_people(&snac);
  1088. *b_size = strlen(*body);
  1089. status = 200;
  1090. }
  1091. }
  1092. else
  1093. if (strcmp(p_path, "notifications") == 0) {
  1094. /* the list of notifications */
  1095. if (!login(&snac, req))
  1096. status = 401;
  1097. else {
  1098. *body = html_notifications(&snac);
  1099. *b_size = strlen(*body);
  1100. status = 200;
  1101. }
  1102. }
  1103. else
  1104. if (xs_startswith(p_path, "p/")) {
  1105. /* a timeline with just one entry */
  1106. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1107. xs *msg = NULL;
  1108. if (valid_status(object_get(id, &msg))) {
  1109. xs *md5 = xs_md5_hex(id, strlen(id));
  1110. xs *list = xs_list_new();
  1111. list = xs_list_append(list, md5);
  1112. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  1113. *b_size = strlen(*body);
  1114. status = 200;
  1115. }
  1116. }
  1117. else
  1118. if (xs_startswith(p_path, "s/")) {
  1119. /* a static file */
  1120. xs *l = xs_split(p_path, "/");
  1121. char *id = xs_list_get(l, 1);
  1122. int sz;
  1123. if (valid_status(static_get(&snac, id, body, &sz))) {
  1124. *b_size = sz;
  1125. *ctype = xs_mime_by_ext(id);
  1126. status = 200;
  1127. }
  1128. }
  1129. else
  1130. if (xs_startswith(p_path, "h/")) {
  1131. /* an entry from the history */
  1132. xs *l = xs_split(p_path, "/");
  1133. char *id = xs_list_get(l, 1);
  1134. if ((*body = history_get(&snac, id)) != NULL) {
  1135. *b_size = strlen(*body);
  1136. status = 200;
  1137. }
  1138. }
  1139. else
  1140. if (strcmp(p_path, ".rss") == 0) {
  1141. /* public timeline in RSS format */
  1142. d_char *rss;
  1143. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1144. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"));
  1145. char *p, *v;
  1146. /* escape tags */
  1147. bio = xs_replace_i(bio, "<", "&lt;");
  1148. bio = xs_replace_i(bio, ">", "&gt;");
  1149. rss = xs_fmt(
  1150. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1151. "<rss version=\"0.91\">\n"
  1152. "<channel>\n"
  1153. "<title>%s (@%s@%s)</title>\n"
  1154. "<language>en</language>\n"
  1155. "<link>%s.rss</link>\n"
  1156. "<description>%s</description>\n",
  1157. xs_dict_get(snac.config, "name"),
  1158. snac.uid,
  1159. xs_dict_get(srv_config, "host"),
  1160. snac.actor,
  1161. bio
  1162. );
  1163. p = elems;
  1164. while (xs_list_iter(&p, &v)) {
  1165. xs *msg = NULL;
  1166. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1167. continue;
  1168. char *id = xs_dict_get(msg, "id");
  1169. if (!xs_startswith(id, snac.actor))
  1170. continue;
  1171. xs *content = sanitize(xs_dict_get(msg, "content"));
  1172. xs *title = xs_str_new(NULL);
  1173. int i;
  1174. /* escape tags */
  1175. content = xs_replace_i(content, "<", "&lt;");
  1176. content = xs_replace_i(content, ">", "&gt;");
  1177. for (i = 0; content[i] && content[i] != '<' && content[i] != '&' && i < 40; i++)
  1178. title = xs_append_m(title, &content[i], 1);
  1179. xs *s = xs_fmt(
  1180. "<item>\n"
  1181. "<title>%s...</title>\n"
  1182. "<link>%s</link>\n"
  1183. "<description>%s</description>\n"
  1184. "</item>\n",
  1185. title, id, content
  1186. );
  1187. rss = xs_str_cat(rss, s);
  1188. }
  1189. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  1190. *body = rss;
  1191. *b_size = strlen(rss);
  1192. *ctype = "application/rss+xml; charset=utf-8";
  1193. status = 200;
  1194. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1195. }
  1196. else
  1197. status = 404;
  1198. user_free(&snac);
  1199. if (valid_status(status) && *ctype == NULL) {
  1200. *ctype = "text/html; charset=utf-8";
  1201. }
  1202. return status;
  1203. }
  1204. int html_post_handler(const xs_dict *req, const char *q_path,
  1205. char *payload, int p_size,
  1206. char **body, int *b_size, char **ctype)
  1207. {
  1208. (void)p_size;
  1209. (void)ctype;
  1210. int status = 0;
  1211. snac snac;
  1212. char *uid, *p_path;
  1213. xs_dict *p_vars;
  1214. xs *l = xs_split_n(q_path, "/", 2);
  1215. uid = xs_list_get(l, 1);
  1216. if (!uid || !user_open(&snac, uid)) {
  1217. /* invalid user */
  1218. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1219. return 404;
  1220. }
  1221. p_path = xs_list_get(l, 2);
  1222. /* all posts must be authenticated */
  1223. if (!login(&snac, req)) {
  1224. user_free(&snac);
  1225. return 401;
  1226. }
  1227. p_vars = xs_dict_get(req, "p_vars");
  1228. #if 0
  1229. {
  1230. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  1231. printf("%s\n", j1);
  1232. }
  1233. #endif
  1234. if (p_path && strcmp(p_path, "admin/note") == 0) {
  1235. /* post note */
  1236. xs_str *content = xs_dict_get(p_vars, "content");
  1237. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1238. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1239. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1240. xs_str *to = xs_dict_get(p_vars, "to");
  1241. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1242. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1243. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1244. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1245. xs *attach_list = xs_list_new();
  1246. /* default alt text */
  1247. if (xs_is_null(alt_text))
  1248. alt_text = "";
  1249. /* is attach_url set? */
  1250. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  1251. xs *l = xs_list_new();
  1252. l = xs_list_append(l, attach_url);
  1253. l = xs_list_append(l, alt_text);
  1254. attach_list = xs_list_append(attach_list, l);
  1255. }
  1256. /* is attach_file set? */
  1257. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  1258. char *fn = xs_list_get(attach_file, 0);
  1259. if (*fn != '\0') {
  1260. char *ext = strrchr(fn, '.');
  1261. xs *hash = xs_md5_hex(fn, strlen(fn));
  1262. xs *id = xs_fmt("%s%s", hash, ext);
  1263. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1264. int fo = xs_number_get(xs_list_get(attach_file, 1));
  1265. int fs = xs_number_get(xs_list_get(attach_file, 2));
  1266. /* store */
  1267. static_put(&snac, id, payload + fo, fs);
  1268. xs *l = xs_list_new();
  1269. l = xs_list_append(l, url);
  1270. l = xs_list_append(l, alt_text);
  1271. attach_list = xs_list_append(attach_list, l);
  1272. }
  1273. }
  1274. if (content != NULL) {
  1275. xs *msg = NULL;
  1276. xs *c_msg = NULL;
  1277. xs *content_2 = xs_replace(content, "\r", "");
  1278. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  1279. if (sensitive != NULL) {
  1280. xs *t = xs_val_new(XSTYPE_TRUE);
  1281. msg = xs_dict_set(msg, "sensitive", t);
  1282. msg = xs_dict_set(msg, "summary", "...");
  1283. }
  1284. if (xs_is_null(edit_id)) {
  1285. /* new message */
  1286. c_msg = msg_create(&snac, msg);
  1287. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1288. }
  1289. else {
  1290. /* an edition of a previous message */
  1291. xs *p_msg = NULL;
  1292. if (valid_status(object_get(edit_id, &p_msg))) {
  1293. /* copy relevant fields from previous version */
  1294. char *fields[] = { "id", "context", "url", "published",
  1295. "to", "inReplyTo", NULL };
  1296. int n;
  1297. for (n = 0; fields[n]; n++) {
  1298. char *v = xs_dict_get(p_msg, fields[n]);
  1299. msg = xs_dict_set(msg, fields[n], v);
  1300. }
  1301. /* set the updated field */
  1302. xs *updated = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  1303. msg = xs_dict_set(msg, "updated", updated);
  1304. /* overwrite object, not updating the indexes */
  1305. object_add_ow(edit_id, msg);
  1306. /* update message */
  1307. c_msg = msg_update(&snac, msg);
  1308. }
  1309. else
  1310. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  1311. }
  1312. if (c_msg != NULL)
  1313. enqueue_message(&snac, c_msg);
  1314. history_del(&snac, "timeline.html_");
  1315. }
  1316. status = 303;
  1317. }
  1318. else
  1319. if (p_path && strcmp(p_path, "admin/action") == 0) {
  1320. /* action on an entry */
  1321. char *id = xs_dict_get(p_vars, "id");
  1322. char *actor = xs_dict_get(p_vars, "actor");
  1323. char *action = xs_dict_get(p_vars, "action");
  1324. if (action == NULL)
  1325. return 404;
  1326. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1327. status = 303;
  1328. if (strcmp(action, L("Like")) == 0) {
  1329. xs *msg = msg_admiration(&snac, id, "Like");
  1330. if (msg != NULL) {
  1331. enqueue_message(&snac, msg);
  1332. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  1333. }
  1334. }
  1335. else
  1336. if (strcmp(action, L("Boost")) == 0) {
  1337. xs *msg = msg_admiration(&snac, id, "Announce");
  1338. if (msg != NULL) {
  1339. enqueue_message(&snac, msg);
  1340. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  1341. }
  1342. }
  1343. else
  1344. if (strcmp(action, L("MUTE")) == 0) {
  1345. mute(&snac, actor);
  1346. }
  1347. else
  1348. if (strcmp(action, L("Unmute")) == 0) {
  1349. unmute(&snac, actor);
  1350. }
  1351. else
  1352. if (strcmp(action, L("Hide")) == 0) {
  1353. hide(&snac, id);
  1354. }
  1355. else
  1356. if (strcmp(action, L("Follow")) == 0) {
  1357. xs *msg = msg_follow(&snac, actor);
  1358. if (msg != NULL) {
  1359. /* reload the actor from the message, in may be different */
  1360. actor = xs_dict_get(msg, "object");
  1361. following_add(&snac, actor, msg);
  1362. enqueue_output_by_actor(&snac, msg, actor, 0);
  1363. }
  1364. }
  1365. else
  1366. if (strcmp(action, L("Unfollow")) == 0) {
  1367. /* get the following object */
  1368. xs *object = NULL;
  1369. if (valid_status(following_get(&snac, actor, &object))) {
  1370. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1371. following_del(&snac, actor);
  1372. enqueue_output_by_actor(&snac, msg, actor, 0);
  1373. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1374. }
  1375. else
  1376. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1377. }
  1378. else
  1379. if (strcmp(action, L("Delete")) == 0) {
  1380. char *actor_form = xs_dict_get(p_vars, "actor-form");
  1381. if (actor_form != NULL) {
  1382. /* delete follower */
  1383. if (valid_status(follower_del(&snac, actor)))
  1384. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  1385. else
  1386. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  1387. }
  1388. else {
  1389. /* delete an entry */
  1390. if (xs_startswith(id, snac.actor)) {
  1391. /* it's a post by us: generate a delete */
  1392. xs *msg = msg_delete(&snac, id);
  1393. enqueue_message(&snac, msg);
  1394. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1395. }
  1396. timeline_del(&snac, id);
  1397. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1398. }
  1399. }
  1400. else
  1401. status = 404;
  1402. /* delete the cached timeline */
  1403. if (status == 303)
  1404. history_del(&snac, "timeline.html_");
  1405. }
  1406. else
  1407. if (p_path && strcmp(p_path, "admin/user-setup") == 0) {
  1408. /* change of user data */
  1409. char *v;
  1410. char *p1, *p2;
  1411. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1412. snac.config = xs_dict_set(snac.config, "name", v);
  1413. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1414. snac.config = xs_dict_set(snac.config, "avatar", v);
  1415. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1416. snac.config = xs_dict_set(snac.config, "bio", v);
  1417. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1418. strcmp(v, "on") == 0) {
  1419. snac.config = xs_dict_set(snac.config, "cw", "open");
  1420. } else { /* if the checkbox is not set, the parameter is missing */
  1421. snac.config = xs_dict_set(snac.config, "cw", "");
  1422. }
  1423. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1424. snac.config = xs_dict_set(snac.config, "email", v);
  1425. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  1426. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  1427. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  1428. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  1429. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  1430. xs *days = xs_number_new(atof(v));
  1431. snac.config = xs_dict_set(snac.config, "purge_days", days);
  1432. }
  1433. /* avatar upload */
  1434. xs_list *avatar_file = xs_dict_get(p_vars, "avatar_file");
  1435. if (!xs_is_null(avatar_file) && xs_type(avatar_file) == XSTYPE_LIST) {
  1436. char *fn = xs_list_get(avatar_file, 0);
  1437. if (*fn != '\0') {
  1438. char *ext = strrchr(fn, '.');
  1439. xs *id = xs_fmt("avatar%s", ext);
  1440. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1441. int fo = xs_number_get(xs_list_get(avatar_file, 1));
  1442. int fs = xs_number_get(xs_list_get(avatar_file, 2));
  1443. /* store */
  1444. static_put(&snac, id, payload + fo, fs);
  1445. snac.config = xs_dict_set(snac.config, "avatar", url);
  1446. }
  1447. }
  1448. /* password change? */
  1449. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1450. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1451. *p1 && strcmp(p1, p2) == 0) {
  1452. xs *pw = hash_password(snac.uid, p1, NULL);
  1453. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1454. }
  1455. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1456. xs *bfn = xs_fmt("%s.bak", fn);
  1457. FILE *f;
  1458. rename(fn, bfn);
  1459. if ((f = fopen(fn, "w")) != NULL) {
  1460. xs *j = xs_json_dumps_pp(snac.config, 4);
  1461. fwrite(j, strlen(j), 1, f);
  1462. fclose(f);
  1463. }
  1464. else
  1465. rename(bfn, fn);
  1466. history_del(&snac, "timeline.html_");
  1467. xs *a_msg = msg_actor(&snac);
  1468. xs *u_msg = msg_update(&snac, a_msg);
  1469. enqueue_message(&snac, u_msg);
  1470. status = 303;
  1471. }
  1472. else
  1473. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) {
  1474. notify_clear(&snac);
  1475. timeline_touch(&snac);
  1476. status = 303;
  1477. }
  1478. if (status == 303) {
  1479. char *redir = xs_dict_get(p_vars, "redir");
  1480. if (xs_is_null(redir))
  1481. redir = "snac-posts";
  1482. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1483. *b_size = strlen(*body);
  1484. }
  1485. user_free(&snac);
  1486. return status;
  1487. }