html.c 51 KB

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