html.c 51 KB

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