html.c 51 KB

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