html.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  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_json.h"
  6. #include "xs_regex.h"
  7. #include "xs_set.h"
  8. #include "xs_openssl.h"
  9. #include "xs_time.h"
  10. #include "xs_mime.h"
  11. #include "snac.h"
  12. int login(snac *snac, const xs_dict *headers)
  13. /* tries a login */
  14. {
  15. int logged_in = 0;
  16. const char *auth = xs_dict_get(headers, "authorization");
  17. if (auth && xs_startswith(auth, "Basic ")) {
  18. int sz;
  19. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  20. xs *s2 = xs_base64_dec(s1, &sz);
  21. xs *l1 = xs_split_n(s2, ":", 1);
  22. if (xs_list_len(l1) == 2) {
  23. logged_in = check_password(
  24. xs_list_get(l1, 0), xs_list_get(l1, 1),
  25. xs_dict_get(snac->config, "passwd"));
  26. }
  27. }
  28. if (logged_in)
  29. lastlog_write(snac, "web");
  30. return logged_in;
  31. }
  32. xs_str *actor_name(xs_dict *actor)
  33. /* gets the actor name */
  34. {
  35. xs_list *p;
  36. char *v;
  37. xs_str *name;
  38. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  39. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  40. v = "anonymous";
  41. }
  42. }
  43. name = encode_html(v);
  44. /* replace the :shortnames: */
  45. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  46. /* iterate the tags */
  47. while (xs_list_iter(&p, &v)) {
  48. char *t = xs_dict_get(v, "type");
  49. if (t && strcmp(t, "Emoji") == 0) {
  50. char *n = xs_dict_get(v, "name");
  51. char *i = xs_dict_get(v, "icon");
  52. if (n && i) {
  53. char *u = xs_dict_get(i, "url");
  54. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em; vertical-align: middle;\" loading=\"lazy\"/>", u);
  55. name = xs_replace_i(name, n, img);
  56. }
  57. }
  58. }
  59. }
  60. return name;
  61. }
  62. xs_str *html_actor_icon(xs_str *os, char *actor,
  63. const char *date, const char *udate, const char *url, int priv)
  64. {
  65. xs *s = xs_str_new(NULL);
  66. xs *avatar = NULL;
  67. char *v;
  68. xs *name = actor_name(actor);
  69. /* get the avatar */
  70. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  71. (v = xs_dict_get(v, "url")) != NULL) {
  72. avatar = xs_dup(v);
  73. }
  74. if (avatar == NULL)
  75. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  76. {
  77. xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" src=\"%s\" alt=\"\" "
  78. "loading=\"lazy\"/>\n", avatar);
  79. s = xs_str_cat(s, s1);
  80. }
  81. {
  82. xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
  83. xs_dict_get(actor, "id"), name);
  84. s = xs_str_cat(s, s1);
  85. }
  86. if (!xs_is_null(url)) {
  87. xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", url);
  88. s = xs_str_cat(s, s1);
  89. }
  90. if (priv)
  91. s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
  92. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  93. s = xs_str_cat(s, " <span title=\"bot\">&#129302;</span>");
  94. if (xs_is_null(date)) {
  95. s = xs_str_cat(s, "\n&nbsp;\n");
  96. }
  97. else {
  98. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  99. xs *date_title = xs_dup(date);
  100. if (!xs_is_null(udate)) {
  101. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  102. date_label = xs_str_cat(date_label, " / ");
  103. date_label = xs_str_cat(date_label, sd);
  104. date_title = xs_str_cat(date_title, " / ");
  105. date_title = xs_str_cat(date_title, udate);
  106. }
  107. xs *edt = encode_html(date_title);
  108. xs *edl = encode_html(date_label);
  109. xs *s1 = xs_fmt(
  110. "\n<time class=\"dt-published snac-pubdate\" title=\"%s\">%s</time>\n",
  111. edt, edl);
  112. s = xs_str_cat(s, s1);
  113. }
  114. {
  115. char *username, *id;
  116. xs *s1;
  117. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  118. /* This should never be reached */
  119. username = "anonymous";
  120. }
  121. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  122. /* This should never be reached */
  123. id = "https://social.example.org/anonymous";
  124. }
  125. /* "LIKE AN ANIMAL" */
  126. xs *domain = xs_split(id, "/");
  127. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  128. xs *u1 = encode_html(user);
  129. s1 = xs_fmt(
  130. "<br><a href=\"%s\" class=\"p-author-tag h-card snac-author-tag\">%s</a>",
  131. xs_dict_get(actor, "id"), u1);
  132. s = xs_str_cat(s, s1);
  133. }
  134. return xs_str_cat(os, s);
  135. }
  136. xs_str *html_msg_icon(snac *snac, xs_str *os, const xs_dict *msg)
  137. {
  138. char *actor_id;
  139. xs *actor = NULL;
  140. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  141. actor_id = xs_dict_get(msg, "actor");
  142. if (actor_id && valid_status(actor_get(snac, actor_id, &actor))) {
  143. char *date = NULL;
  144. char *udate = NULL;
  145. char *url = NULL;
  146. int priv = 0;
  147. const char *type = xs_dict_get(msg, "type");
  148. if (strcmp(type, "Note") == 0 || strcmp(type, "Question") == 0 || strcmp(type, "Page") == 0)
  149. url = xs_dict_get(msg, "id");
  150. priv = !is_msg_public(snac, msg);
  151. date = xs_dict_get(msg, "published");
  152. udate = xs_dict_get(msg, "updated");
  153. os = html_actor_icon(os, actor, date, udate, url, priv);
  154. }
  155. return os;
  156. }
  157. d_char *html_user_header(snac *snac, d_char *s, int local)
  158. /* creates the HTML header */
  159. {
  160. char *p, *v;
  161. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  162. s = xs_str_cat(s, "<meta name=\"viewport\" "
  163. "content=\"width=device-width, initial-scale=1\"/>\n");
  164. s = xs_str_cat(s, "<meta name=\"generator\" "
  165. "content=\"" USER_AGENT "\"/>\n");
  166. /* add server CSS */
  167. p = xs_dict_get(srv_config, "cssurls");
  168. while (xs_list_iter(&p, &v)) {
  169. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  170. s = xs_str_cat(s, s1);
  171. }
  172. /* add the user CSS */
  173. {
  174. xs *css = NULL;
  175. int size;
  176. /* try to open the user css */
  177. if (!valid_status(static_get(snac, "style.css", &css, &size, NULL, NULL))) {
  178. /* it's not there; try to open the server-wide css */
  179. FILE *f;
  180. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  181. if ((f = fopen(g_css_fn, "r")) != NULL) {
  182. css = xs_readall(f);
  183. fclose(f);
  184. }
  185. }
  186. if (css != NULL) {
  187. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  188. s = xs_str_cat(s, s1);
  189. }
  190. }
  191. {
  192. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  193. xs *es2 = encode_html(snac->uid);
  194. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  195. xs *s1 = xs_fmt("<title>%s (@%s@%s)</title>\n", es1, es2, es3);
  196. s = xs_str_cat(s, s1);
  197. }
  198. xs *avatar = xs_dup(xs_dict_get(snac->config, "avatar"));
  199. if (avatar == NULL || *avatar == '\0') {
  200. xs_free(avatar);
  201. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  202. }
  203. {
  204. xs *s_bio = xs_dup(xs_dict_get(snac->config, "bio"));
  205. int n;
  206. /* shorten the bio */
  207. for (n = 0; s_bio[n] && s_bio[n] != '&' && s_bio[n] != '.' &&
  208. s_bio[n] != '\r' && s_bio[n] != '\n' && n < 128; n++);
  209. s_bio[n] = '\0';
  210. xs *s_avatar = xs_dup(avatar);
  211. /* don't inline an empty avatar: create a real link */
  212. if (xs_startswith(s_avatar, "data:")) {
  213. xs_free(s_avatar);
  214. s_avatar = xs_fmt("%s/susie.png", srv_baseurl);
  215. }
  216. /* og properties */
  217. xs *es1 = encode_html(xs_dict_get(srv_config, "host"));
  218. xs *es2 = encode_html(xs_dict_get(snac->config, "name"));
  219. xs *es3 = encode_html(snac->uid);
  220. xs *es4 = encode_html(xs_dict_get(srv_config, "host"));
  221. xs *es5 = encode_html(s_bio);
  222. xs *es6 = encode_html(s_avatar);
  223. xs *s1 = xs_fmt(
  224. "<meta property=\"og:site_name\" content=\"%s\"/>\n"
  225. "<meta property=\"og:title\" content=\"%s (@%s@%s)\"/>\n"
  226. "<meta property=\"og:description\" content=\"%s\"/>\n"
  227. "<meta property=\"og:image\" content=\"%s\"/>\n"
  228. "<meta property=\"og:image:width\" content=\"300\"/>\n"
  229. "<meta property=\"og:image:height\" content=\"300\"/>\n",
  230. es1, es2, es3, es4, es5, es6);
  231. s = xs_str_cat(s, s1);
  232. }
  233. {
  234. xs *s1 = xs_fmt("<link rel=\"alternate\" type=\"application/rss+xml\" "
  235. "title=\"RSS\" href=\"%s.rss\" />\n", snac->actor); /* snac->actor is likely need to be URLEncoded. */
  236. s = xs_str_cat(s, s1);
  237. }
  238. s = xs_str_cat(s, "</head>\n<body>\n");
  239. /* top nav */
  240. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  241. {
  242. xs *s1;
  243. s1 = xs_fmt("<img src=\"%s\" class=\"snac-avatar\" alt=\"\"/>&nbsp;", avatar);
  244. s = xs_str_cat(s, s1);
  245. }
  246. {
  247. xs *s1;
  248. if (local)
  249. s1 = xs_fmt(
  250. "<a href=\"%s.rss\">%s</a> - "
  251. "<a href=\"%s/admin\" rel=\"nofollow\">%s</a></nav>\n",
  252. snac->uid, L("RSS"),
  253. snac->uid, L("private"));
  254. else {
  255. xs *n_list = notify_list(snac, 1);
  256. int n_len = xs_list_len(n_list);
  257. xs *n_str = NULL;
  258. /* show the number of new notifications, if there are any */
  259. if (n_len)
  260. n_str = xs_fmt("<sup style=\"background-color: red; "
  261. "color: white;\"> %d </sup> ", n_len);
  262. else
  263. n_str = xs_str_new("");
  264. s1 = xs_fmt(
  265. "<a href=\"../%s\">%s</a> - "
  266. "<a href=\"admin\">%s</a> - "
  267. "<a href=\"notifications\">%s</a>%s - "
  268. "<a href=\"people\">%s</a></nav>\n"
  269. "<a href=\"settings\">%s</a>"
  270. "</nav>",
  271. snac->uid, L("public"),
  272. L("private"),
  273. L("notifications"), n_str,
  274. L("people"),
  275. L("settings"));
  276. }
  277. s = xs_str_cat(s, s1);
  278. }
  279. /* user info */
  280. {
  281. char *_tmpl =
  282. "<div class=\"h-card snac-top-user\">\n"
  283. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  284. "<p class=\"snac-top-user-id\">@%s@%s</p>\n";
  285. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  286. xs *es2 = encode_html(xs_dict_get(snac->config, "uid"));
  287. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  288. xs *s1 = xs_fmt(_tmpl, es1, es2, es3);
  289. s = xs_str_cat(s, s1);
  290. if (local) {
  291. xs *es1 = encode_html(xs_dict_get(snac->config, "bio"));
  292. xs *bio1 = not_really_markdown(es1, NULL);
  293. xs *tags = xs_list_new();
  294. xs *bio2 = process_tags(snac, bio1, &tags);
  295. xs *s1 = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio2);
  296. s = xs_str_cat(s, s1);
  297. }
  298. s = xs_str_cat(s, "</div>\n");
  299. }
  300. return s;
  301. }
  302. xs_str *html_top_controls(snac *snac, xs_str *s, int settings)
  303. /* generates the top controls */
  304. {
  305. char *_tmpl =
  306. "<div class=\"snac-top-controls\">\n"
  307. "<div class=\"snac-note\" %s>\n"
  308. "<details><summary>%s</summary>\n"
  309. "<form autocomplete=\"off\" method=\"post\" "
  310. "action=\"admin/note\" enctype=\"multipart/form-data\">\n"
  311. "<textarea class=\"snac-textarea\" name=\"content\" "
  312. "rows=\"8\" wrap=\"virtual\" required=\"required\" placeholder=\"What's on your mind?\"></textarea>\n"
  313. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  314. "<p>%s: <input type=\"checkbox\" name=\"sensitive\"> "
  315. "<input type=\"text\" name=\"summary\" placeholder=\"%s\">\n"
  316. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  317. "<details><summary>%s</summary>\n" /** attach **/
  318. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  319. "<p>%s: <input type=\"text\" name=\"alt_text\" placeholder=\"[Optional]\">\n"
  320. "</details>\n"
  321. "<p>"
  322. "<details><summary>%s</summary>\n" /** poll **/
  323. "<p>%s:<br>\n"
  324. "<textarea class=\"snac-textarea\" name=\"poll_options\" "
  325. "rows=\"6\" wrap=\"virtual\" placeholder=\"Option 1...\nOption 2...\nOption 3...\n....\"></textarea>\n"
  326. "<p><select name=\"poll_multiple\">\n"
  327. "<option value=\"off\">%s</option>\n"
  328. "<option value=\"on\">%s</option>\n"
  329. "</select>\n"
  330. "<select name=\"poll_end_secs\" id=\"poll_end_secs\">\n"
  331. "<option value=\"300\">%s</option>\n"
  332. "<option value=\"3600\">%s</option>\n"
  333. "<option value=\"86400\">%s</option>\n"
  334. "</select>\n"
  335. "</details>\n"
  336. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  337. "</form><p>\n"
  338. "</div>\n"
  339. "</details>\n"
  340. "<div class=\"snac-top-controls-more\" %s>\n"
  341. "<p><details><summary>%s</summary>\n"
  342. "<form autocomplete=\"off\" method=\"post\" action=\"admin/action\">\n" /** follow **/
  343. "<input type=\"text\" name=\"actor\" required=\"required\" placeholder=\"bob@example.com\">\n"
  344. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  345. "</form><p>\n"
  346. "<form autocomplete=\"off\" method=\"post\" action=\"admin/action\">\n" /** boost **/
  347. "<input type=\"text\" name=\"id\" required=\"required\" placeholder=\"https://fedi.example.com/bob/....\">\n"
  348. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  349. "</form><p>\n"
  350. "</details>\n"
  351. "</div>\n"
  352. "<div class=\"snac-user-settings\" %s>\n"
  353. "<h2 class=\"snac-header\">%s</h2>\n"
  354. "<div class=\"snac-user-setup\">\n" /** user setup **/
  355. "<form autocomplete=\"off\" method=\"post\" "
  356. "action=\"admin/user-setup\" enctype=\"multipart/form-data\">\n"
  357. "<p>%s:<br>\n"
  358. "<input type=\"text\" name=\"name\" value=\"%s\" placeholder=\"Your name.\"></p>\n"
  359. "<p>%s: <input type=\"file\" name=\"avatar_file\"></p>\n"
  360. "<p>%s:<br>\n"
  361. "<textarea name=\"bio\" cols=\"40\" rows=\"4\" placeholder=\"Write about yourself here....\">%s</textarea></p>\n"
  362. "<p><input type=\"checkbox\" name=\"cw\" id=\"cw\" %s>\n"
  363. "<label for=\"cw\">%s</label></p>\n"
  364. "<p>%s:<br>\n"
  365. "<input type=\"text\" name=\"email\" value=\"%s\" placeholder=\"bob@example.com\"></p>\n"
  366. "<p>%s:<br>\n"
  367. "<input type=\"text\" name=\"telegram_bot\" placeholder=\"Bot API key\" value=\"%s\"> "
  368. "<input type=\"text\" name=\"telegram_chat_id\" placeholder=\"Chat id\" value=\"%s\"></p>\n"
  369. "<p>%s:<br>\n"
  370. "<input type=\"number\" name=\"purge_days\" value=\"%s\"></p>\n"
  371. "<p><input type=\"checkbox\" name=\"drop_dm_from_unknown\" id=\"drop_dm_from_unknown\" %s>\n"
  372. "<label for=\"drop_dm_from_unknown\">%s</label></p>\n"
  373. "<p><input type=\"checkbox\" name=\"bot\" id=\"bot\" %s>\n"
  374. "<label for=\"bot\">%s</label></p>\n"
  375. "<p>%s:<br>\n"
  376. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  377. "<p>%s:<br>\n"
  378. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  379. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  380. "</form>\n"
  381. "</div>\n"
  382. "</div>\n"
  383. "</div>\n";
  384. const char *email = "[disabled by admin]";
  385. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  386. email = xs_dict_get(snac->config_o, "email");
  387. if (xs_is_null(email)) {
  388. email = xs_dict_get(snac->config, "email");
  389. if (xs_is_null(email))
  390. email = "";
  391. }
  392. }
  393. char *cw = xs_dict_get(snac->config, "cw");
  394. if (xs_is_null(cw))
  395. cw = "";
  396. char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
  397. if (xs_is_null(telegram_bot))
  398. telegram_bot = "";
  399. char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  400. if (xs_is_null(telegram_chat_id))
  401. telegram_chat_id = "";
  402. const char *purge_days = xs_dict_get(snac->config, "purge_days");
  403. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  404. purge_days = xs_number_str(purge_days);
  405. else
  406. purge_days = "0";
  407. const char *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
  408. const char *bot = xs_dict_get(snac->config, "bot");
  409. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  410. xs *es2 = encode_html(xs_dict_get(snac->config, "bio"));
  411. xs *es3 = encode_html(email);
  412. xs *es4 = encode_html(telegram_bot);
  413. xs *es5 = encode_html(telegram_chat_id);
  414. xs *es6 = encode_html(purge_days);
  415. xs *s1 = xs_fmt(_tmpl,
  416. settings ? "style=\"display: none\"" : "",
  417. L("New Post..."),
  418. L("Sensitive content"),
  419. L("Sensitive content description"),
  420. L("Only for mentioned people"),
  421. L("Attachment..."),
  422. L("File"),
  423. L("File description"),
  424. L("Poll..."),
  425. L("Poll options (one per line, up to 8)"),
  426. L("One choice"),
  427. L("Multiple choices"),
  428. L("End in 5 minutes"),
  429. L("End in 1 hour"),
  430. L("End in 1 day"),
  431. L("Post"),
  432. settings ? "style=\"display: none\"" : "",
  433. L("Operations..."),
  434. L("Follow"), L("(by URL or user@host)"),
  435. L("Boost"), L("(by URL)"),
  436. !settings ? "style=\"display: none\"" : "",
  437. L("User Settings"),
  438. L("Display name"),
  439. es1,
  440. L("Avatar"),
  441. L("Bio"),
  442. es2,
  443. strcmp(cw, "open") == 0 ? "checked" : "",
  444. L("Always show sensitive content"),
  445. L("Email address for notifications"),
  446. es3,
  447. L("Telegram notifications (bot key and chat id)"),
  448. es4,
  449. es5,
  450. L("Maximum days to keep posts (0: server settings)"),
  451. es6,
  452. xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "",
  453. L("Drop direct messages from people you don't follow"),
  454. xs_type(bot) == XSTYPE_TRUE ? "checked" : "",
  455. L("This account is a bot"),
  456. L("New password"),
  457. L("Repeat new password"),
  458. L("Update user info")
  459. );
  460. s = xs_str_cat(s, s1);
  461. return s;
  462. }
  463. d_char *html_button(d_char *s, char *clss, char *label)
  464. {
  465. xs *s1 = xs_fmt(
  466. "<input type=\"submit\" name=\"action\" "
  467. "class=\"snac-btn-%s\" value=\"%s\">\n",
  468. clss, label);
  469. return xs_str_cat(s, s1);
  470. }
  471. xs_str *build_mentions(snac *snac, const xs_dict *msg)
  472. /* returns a string with the mentions in msg */
  473. {
  474. xs_str *s = xs_str_new(NULL);
  475. char *list = xs_dict_get(msg, "tag");
  476. char *v;
  477. while (xs_list_iter(&list, &v)) {
  478. char *type = xs_dict_get(v, "type");
  479. char *href = xs_dict_get(v, "href");
  480. char *name = xs_dict_get(v, "name");
  481. if (type && strcmp(type, "Mention") == 0 &&
  482. href && strcmp(href, snac->actor) != 0 && name) {
  483. xs *s1 = NULL;
  484. if (name[0] != '@') {
  485. s1 = xs_fmt("@%s", name);
  486. name = s1;
  487. }
  488. xs *l = xs_split(name, "@");
  489. /* is it a name without a host? */
  490. if (xs_list_len(l) < 3) {
  491. /* split the href and pick the host name LIKE AN ANIMAL */
  492. /* would be better to query the webfinger but *won't do that* here */
  493. xs *l2 = xs_split(href, "/");
  494. if (xs_list_len(l2) >= 3) {
  495. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  496. s = xs_str_cat(s, s1);
  497. }
  498. }
  499. else {
  500. s = xs_str_cat(s, name);
  501. s = xs_str_cat(s, " ");
  502. }
  503. }
  504. }
  505. if (*s) {
  506. xs *s1 = s;
  507. s = xs_fmt("\n\n\nCC: %s", s1);
  508. }
  509. return s;
  510. }
  511. xs_str *html_entry_controls(snac *snac, xs_str *os, const xs_dict *msg, const char *md5)
  512. {
  513. char *id = xs_dict_get(msg, "id");
  514. char *actor = xs_dict_get(msg, "attributedTo");
  515. xs *likes = object_likes(id);
  516. xs *boosts = object_announces(id);
  517. xs *s = xs_str_new(NULL);
  518. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  519. {
  520. xs *s1 = xs_fmt(
  521. "<form autocomplete=\"off\" method=\"post\" action=\"admin/action\">\n"
  522. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  523. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  524. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  525. "\n",
  526. id, actor, md5
  527. );
  528. s = xs_str_cat(s, s1);
  529. }
  530. if (!xs_startswith(id, snac->actor)) {
  531. if (xs_list_in(likes, snac->md5) == -1) {
  532. /* not already liked; add button */
  533. s = html_button(s, "like", L("Like"));
  534. }
  535. }
  536. else {
  537. if (is_pinned(snac, id))
  538. s = html_button(s, "unpin", L("Unpin"));
  539. else
  540. s = html_button(s, "pin", L("Pin"));
  541. }
  542. if (is_msg_public(snac, msg)) {
  543. if (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) {
  544. /* not already boosted or us; add button */
  545. s = html_button(s, "boost", L("Boost"));
  546. }
  547. }
  548. if (strcmp(actor, snac->actor) != 0) {
  549. /* controls for other actors than this one */
  550. if (following_check(snac, actor)) {
  551. s = html_button(s, "unfollow", L("Unfollow"));
  552. }
  553. else {
  554. s = html_button(s, "follow", L("Follow"));
  555. }
  556. s = html_button(s, "mute", L("MUTE"));
  557. }
  558. s = html_button(s, "delete", L("Delete"));
  559. s = html_button(s, "hide", L("Hide"));
  560. s = xs_str_cat(s, "</form>\n");
  561. const char *prev_src1 = xs_dict_get(msg, "sourceContent");
  562. if (!xs_is_null(prev_src1) && strcmp(actor, snac->actor) == 0) { /** edit **/
  563. xs *prev_src = encode_html(prev_src1);
  564. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  565. const char *summary = xs_dict_get(msg, "summary");
  566. /* post can be edited */
  567. xs *s1 = xs_fmt(
  568. "<p><details><summary>%s</summary>\n"
  569. "<p><div class=\"snac-note\" id=\"%s_edit\">\n"
  570. "<form autocomplete=\"off\" method=\"post\" action=\"admin/note\" "
  571. "enctype=\"multipart/form-data\" id=\"%s_edit_form\">\n"
  572. "<textarea class=\"snac-textarea\" name=\"content\" "
  573. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  574. "<input type=\"hidden\" name=\"edit_id\" value=\"%s\">\n"
  575. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  576. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  577. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  578. "<details><summary>%s</summary>\n"
  579. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  580. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  581. "</details>\n"
  582. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  583. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  584. "</form><p></div>\n"
  585. "</details><p>"
  586. "\n",
  587. L("Edit..."),
  588. md5,
  589. md5,
  590. prev_src,
  591. id,
  592. L("Sensitive content"),
  593. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  594. L("Sensitive content description"),
  595. xs_is_null(summary) ? "" : summary,
  596. L("Only for mentioned people"),
  597. L("Attach..."),
  598. L("File"),
  599. L("File description"),
  600. md5,
  601. L("Post")
  602. );
  603. s = xs_str_cat(s, s1);
  604. }
  605. { /** reply **/
  606. /* the post textarea */
  607. xs *ct = build_mentions(snac, msg);
  608. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  609. const char *summary = xs_dict_get(msg, "summary");
  610. xs *s1 = xs_fmt(
  611. "<p><details><summary>%s</summary>\n"
  612. "<p><div class=\"snac-note\" id=\"%s_reply\">\n"
  613. "<form autocomplete=\"off\" method=\"post\" action=\"admin/note\" "
  614. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  615. "<textarea class=\"snac-textarea\" name=\"content\" "
  616. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  617. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  618. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  619. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  620. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  621. "<details><summary>%s</summary>\n"
  622. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  623. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  624. "</details>\n"
  625. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  626. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  627. "</form><p></div>\n"
  628. "</details><p>"
  629. "\n",
  630. L("Reply..."),
  631. md5,
  632. md5,
  633. ct,
  634. id,
  635. L("Sensitive content"),
  636. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  637. L("Sensitive content description"),
  638. xs_is_null(summary) ? "" : summary,
  639. L("Only for mentioned people"),
  640. L("Attach..."),
  641. L("File"),
  642. L("File description"),
  643. md5,
  644. L("Post")
  645. );
  646. s = xs_str_cat(s, s1);
  647. }
  648. s = xs_str_cat(s, "</div>\n");
  649. return xs_str_cat(os, s);
  650. }
  651. xs_str *html_entry(snac *snac, xs_str *os, const xs_dict *msg, int local,
  652. int level, const char *md5, int hide_children)
  653. {
  654. char *id = xs_dict_get(msg, "id");
  655. char *type = xs_dict_get(msg, "type");
  656. char *actor;
  657. int sensitive = 0;
  658. char *v;
  659. xs *boosts = NULL;
  660. /* do not show non-public messages in the public timeline */
  661. if (local && !is_msg_public(snac, msg))
  662. return os;
  663. /* hidden? do nothing more for this conversation */
  664. if (is_hidden(snac, id))
  665. return os;
  666. /* avoid too deep nesting, as it may be a loop */
  667. if (level >= 256)
  668. return os;
  669. xs *s = xs_str_new("<div>\n");
  670. {
  671. xs *s1 = xs_fmt("<a name=\"%s_entry\"></a>\n", md5);
  672. s = xs_str_cat(s, s1);
  673. }
  674. if (strcmp(type, "Follow") == 0) {
  675. s = xs_str_cat(s, "<div class=\"snac-post\">\n<div class=\"snac-post-header\">\n");
  676. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  677. s = xs_str_cat(s, s1);
  678. s = html_msg_icon(snac, s, msg);
  679. s = xs_str_cat(s, "</div>\n</div>\n");
  680. return xs_str_cat(os, s);
  681. }
  682. else
  683. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0 && strcmp(type, "Page") != 0) {
  684. /* skip oddities */
  685. return os;
  686. }
  687. /* ignore notes with "name", as they are votes to Questions */
  688. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  689. return os;
  690. /* bring the main actor */
  691. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  692. return os;
  693. /* ignore muted morons immediately */
  694. if (is_muted(snac, actor))
  695. return os;
  696. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  697. return os;
  698. if (level == 0)
  699. s = xs_str_cat(s, "<div class=\"snac-post\">\n"); /** **/
  700. else
  701. s = xs_str_cat(s, "<div class=\"snac-child\">\n"); /** **/
  702. s = xs_str_cat(s, "<div class=\"snac-post-header\">\n<div class=\"snac-score\">"); /** **/
  703. if (is_pinned(snac, id)) {
  704. /* add a pin emoji */
  705. xs *f = xs_fmt("<span title=\"%s\"> &#128204; </span>", L("Pinned"));
  706. s = xs_str_cat(s, f);
  707. }
  708. if (strcmp(type, "Question") == 0) {
  709. /* add the ballot box emoji */
  710. xs *f = xs_fmt("<span title=\"%s\"> &#128499; </span>", L("Poll"));
  711. s = xs_str_cat(s, f);
  712. if (was_question_voted(snac, id)) {
  713. /* add a check to show this poll was voted */
  714. xs *f2 = xs_fmt("<span title=\"%s\"> &#10003; </span>", L("Voted"));
  715. s = xs_str_cat(s, f2);
  716. }
  717. }
  718. /* if this is our post, add the score */
  719. if (xs_startswith(id, snac->actor)) {
  720. int n_likes = object_likes_len(id);
  721. int n_boosts = object_announces_len(id);
  722. /* alternate emojis: %d &#128077; %d &#128257; */
  723. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  724. s = xs_str_cat(s, s1);
  725. }
  726. s = xs_str_cat(s, "</div>\n");
  727. if (boosts == NULL)
  728. boosts = object_announces(id);
  729. if (xs_list_len(boosts)) {
  730. /* if somebody boosted this, show as origin */
  731. char *p = xs_list_get(boosts, -1);
  732. xs *actor_r = NULL;
  733. if (xs_list_in(boosts, snac->md5) != -1) {
  734. /* we boosted this */
  735. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  736. xs *s1 = xs_fmt(
  737. "<div class=\"snac-origin\">"
  738. "<a href=\"%s\">%s</a> %s</a></div>",
  739. snac->actor, es1, L("boosted")
  740. );
  741. s = xs_str_cat(s, s1);
  742. }
  743. else
  744. if (valid_status(object_get_by_md5(p, &actor_r))) {
  745. xs *name = actor_name(actor_r);
  746. if (!xs_is_null(name)) {
  747. xs *s1 = xs_fmt(
  748. "<div class=\"snac-origin\">"
  749. "<a href=\"%s\">%s</a> %s</div>\n",
  750. xs_dict_get(actor_r, "id"),
  751. name,
  752. L("boosted")
  753. );
  754. s = xs_str_cat(s, s1);
  755. }
  756. }
  757. }
  758. else
  759. if (strcmp(type, "Note") == 0) {
  760. if (level == 0) {
  761. /* is the parent not here? */
  762. char *parent = xs_dict_get(msg, "inReplyTo");
  763. if (!xs_is_null(parent) && *parent && !timeline_here(snac, parent)) {
  764. xs *s1 = xs_fmt(
  765. "<div class=\"snac-origin\">%s "
  766. "<a href=\"%s\">»</a></div>\n",
  767. L("in reply to"), parent
  768. );
  769. s = xs_str_cat(s, s1);
  770. }
  771. }
  772. }
  773. s = html_msg_icon(snac, s, msg);
  774. /* add the content */
  775. s = xs_str_cat(s, "</div>\n<div class=\"e-content snac-content\">\n"); /** **/
  776. if (!xs_is_null(v = xs_dict_get(msg, "name"))) {
  777. xs *es1 = encode_html(v);
  778. xs *s1 = xs_fmt("<h3 class=\"snac-entry-title\">%s</h3>\n", es1);
  779. s = xs_str_cat(s, s1);
  780. }
  781. /* is it sensitive? */
  782. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  783. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  784. v = "...";
  785. /* only show it when not in the public timeline and the config setting is "open" */
  786. char *cw = xs_dict_get(snac->config, "cw");
  787. if (xs_is_null(cw) || local)
  788. cw = "";
  789. xs *es1 = encode_html(v);
  790. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, es1, L("SENSITIVE CONTENT"));
  791. s = xs_str_cat(s, s1);
  792. sensitive = 1;
  793. }
  794. #if 0
  795. {
  796. xs *md5 = xs_md5_hex(id, strlen(id));
  797. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  798. s = xs_str_cat(s, s1);
  799. }
  800. #endif
  801. {
  802. const char *content = xs_dict_get(msg, "content");
  803. xs *c = sanitize(xs_is_null(content) ? "" : content);
  804. char *p, *v;
  805. /* do some tweaks to the content */
  806. c = xs_replace_i(c, "\r", "");
  807. while (xs_endswith(c, "<br><br>"))
  808. c = xs_crop_i(c, 0, -4);
  809. c = xs_replace_i(c, "<br><br>", "<p>");
  810. if (!xs_startswith(c, "<p>")) {
  811. xs *s1 = c;
  812. c = xs_fmt("<p>%s</p>", s1);
  813. }
  814. /* replace the :shortnames: */
  815. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  816. /* iterate the tags */
  817. while (xs_list_iter(&p, &v)) {
  818. char *t = xs_dict_get(v, "type");
  819. if (t && strcmp(t, "Emoji") == 0) {
  820. char *n = xs_dict_get(v, "name");
  821. char *i = xs_dict_get(v, "icon");
  822. if (n && i) {
  823. char *u = xs_dict_get(i, "url");
  824. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 2em; vertical-align: middle;\" "
  825. "loading=\"lazy\" title=\"%s\"/>", u, n);
  826. c = xs_replace_i(c, n, img);
  827. }
  828. }
  829. }
  830. }
  831. if (strcmp(type, "Question") == 0) { /** question content **/
  832. xs_list *oo = xs_dict_get(msg, "oneOf");
  833. xs_list *ao = xs_dict_get(msg, "anyOf");
  834. xs_list *p;
  835. xs_dict *v;
  836. int closed = 0;
  837. if (xs_dict_get(msg, "closed"))
  838. closed = 2;
  839. else
  840. if (xs_startswith(id, snac->actor))
  841. closed = 1; /* we questioned; closed for us */
  842. else
  843. if (was_question_voted(snac, id))
  844. closed = 1; /* we already voted; closed for us */
  845. /* get the appropriate list of options */
  846. p = oo != NULL ? oo : ao;
  847. if (closed) {
  848. /* closed poll */
  849. c = xs_str_cat(c, "<table class=\"snac-poll-result\">\n");
  850. while (xs_list_iter(&p, &v)) {
  851. const char *name = xs_dict_get(v, "name");
  852. const xs_dict *replies = xs_dict_get(v, "replies");
  853. if (name && replies) {
  854. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  855. xs *es1 = encode_html(name);
  856. xs *l = xs_fmt("<tr><td>%s:</td><td>%d</td></tr>\n", es1, nr);
  857. c = xs_str_cat(c, l);
  858. }
  859. }
  860. c = xs_str_cat(c, "</table>\n");
  861. }
  862. else {
  863. /* poll still active */
  864. xs *s1 = xs_fmt("<div class=\"snac-poll-form\">\n"
  865. "<form autocomplete=\"off\" "
  866. "method=\"post\" action=\"admin/vote\">\n"
  867. "<input type=\"hidden\" name=\"actor\" value= \"%s\">\n"
  868. "<input type=\"hidden\" name=\"irt\" value=\"%s\">\n",
  869. actor, id);
  870. while (xs_list_iter(&p, &v)) {
  871. const char *name = xs_dict_get(v, "name");
  872. if (name) {
  873. xs *es1 = encode_html(name);
  874. xs *opt = xs_fmt("<input type=\"%s\""
  875. " id=\"%s\" value=\"%s\" name=\"question\"> %s<br>\n",
  876. !xs_is_null(oo) ? "radio" : "checkbox",
  877. es1, es1, es1);
  878. s1 = xs_str_cat(s1, opt);
  879. }
  880. }
  881. xs *s2 = xs_fmt("<p><input type=\"submit\" "
  882. "class=\"button\" value=\"%s\">\n</form>\n</div>\n\n", L("Vote"));
  883. s1 = xs_str_cat(s1, s2);
  884. c = xs_str_cat(c, s1);
  885. }
  886. /* if it's *really* closed, say it */
  887. if (closed == 2) {
  888. xs *s1 = xs_fmt("<p>%s</p>\n", L("Closed"));
  889. c = xs_str_cat(c, s1);
  890. }
  891. else {
  892. /* show when the poll closes */
  893. const char *end_time = xs_dict_get(msg, "endTime");
  894. if (!xs_is_null(end_time)) {
  895. time_t t0 = time(NULL);
  896. time_t t1 = xs_parse_iso_date(end_time, 0);
  897. if (t1 > 0 && t1 > t0) {
  898. time_t diff_time = t1 - t0;
  899. xs *tf = xs_str_time_diff(diff_time);
  900. char *p = tf;
  901. /* skip leading zeros */
  902. for (; *p == '0' || *p == ':'; p++);
  903. xs *es1 = encode_html(p);
  904. xs *s1 = xs_fmt("<p>%s %s</p>", L("Closes in"), es1);
  905. c = xs_str_cat(c, s1);
  906. }
  907. }
  908. }
  909. }
  910. s = xs_str_cat(s, c);
  911. }
  912. s = xs_str_cat(s, "\n");
  913. /* add the attachments */
  914. xs_list *attach = xs_dict_get(msg, "attachment");
  915. xs_dict *image = xs_dict_get(msg, "image");
  916. if (!xs_is_null(attach) || !xs_is_null(image)) { /** **/
  917. char *v;
  918. /* make custom css for attachments easier */
  919. s = xs_str_cat(s, "<p class=\"snac-content-attachments\">\n");
  920. while (xs_list_iter(&attach, &v)) {
  921. const char *t = xs_dict_get(v, "mediaType");
  922. if (xs_is_null(t))
  923. t = xs_dict_get(v, "type");
  924. if (xs_is_null(t))
  925. continue;
  926. if (xs_startswith(t, "image/")) {
  927. char *url = xs_dict_get(v, "url");
  928. char *name = xs_dict_get(v, "name");
  929. if (url != NULL) {
  930. if (xs_is_null(name))
  931. name = "";
  932. xs *es1 = encode_html(name);
  933. xs *s1 = xs_fmt(
  934. "<a href=\"%s\" target=\"_blank\">"
  935. "<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
  936. url, url, es1, es1);
  937. s = xs_str_cat(s, s1);
  938. }
  939. }
  940. else
  941. if (xs_startswith(t, "video/")) {
  942. char *url = xs_dict_get(v, "url");
  943. char *name = xs_dict_get(v, "name");
  944. if (url != NULL) {
  945. if (xs_is_null(name))
  946. name = "No description";
  947. xs *es1 = encode_html(name);
  948. xs *s1 = xs_fmt("<video style=\"width: 100%\" class=\"snac-embedded-video\" "
  949. "controls src=\"%s\">Video: "
  950. "<a href=\"%s\">%s</a></video>\n", url, url, es1);
  951. s = xs_str_cat(s, s1);
  952. }
  953. }
  954. else
  955. if (xs_startswith(t, "audio/")) {
  956. char *url = xs_dict_get(v, "url");
  957. char *name = xs_dict_get(v, "name");
  958. if (url != NULL) {
  959. if (xs_is_null(name))
  960. name = "No description";
  961. xs *es1 = encode_html(name);
  962. xs *s1 = xs_fmt("<audio style=\"width: 100%\" class=\"snac-embedded-audio\" "
  963. "controls src=\"%s\">Audio: "
  964. "<a href=\"%s\">%s</a></audio>\n", url, url, es1);
  965. s = xs_str_cat(s, s1);
  966. }
  967. }
  968. else
  969. if (strcmp(t, "Link") == 0) {
  970. const char *url = xs_dict_get(v, "href");
  971. if (!xs_is_null(url)) {
  972. xs *es1 = encode_html(url);
  973. xs *s1 = xs_fmt("<p><a href=\"%s\">%s</a></p>\n", url, es1);
  974. s = xs_str_cat(s, s1);
  975. }
  976. }
  977. else {
  978. char *url = xs_dict_get(v, "url");
  979. char *name = xs_dict_get(v, "name");
  980. if (url != NULL) {
  981. if (xs_is_null(name))
  982. name = "No description";
  983. xs *es1 = encode_html(name);
  984. xs *s1 = xs_fmt("<a href=\"%s\">Attachment: %s</a>", url, es1);
  985. s = xs_str_cat(s, s1);
  986. }
  987. }
  988. }
  989. /* if the message has an image, add it */
  990. if (!xs_is_null(image)) {
  991. if (!xs_is_null(image = xs_dict_get(image, "url"))) {
  992. xs *es1;
  993. if (!xs_is_null(v = xs_dict_get(msg, "name")))
  994. es1 = encode_html(v);
  995. else
  996. es1 = xs_str_new(NULL);
  997. xs *s1 = xs_fmt(
  998. "<a href=\"%s\" target=\"_blank\">"
  999. "<img src=\"%s\" alt=\"%s\" title=\"%s\" loading=\"lazy\"/></a>\n",
  1000. image, image, es1, es1);
  1001. s = xs_str_cat(s, s1);
  1002. }
  1003. }
  1004. s = xs_str_cat(s, "</p>\n");
  1005. }
  1006. /* has this message an audience (i.e., comes from a channel or community)? */
  1007. const char *audience = xs_dict_get(msg, "audience");
  1008. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  1009. xs *es1 = encode_html(audience);
  1010. xs *s1 = xs_fmt("<p>(<a href=\"%s\" title=\"%s\">%s</a>)</p>\n",
  1011. audience, L("Source channel or community"), es1);
  1012. s = xs_str_cat(s, s1);
  1013. }
  1014. if (sensitive)
  1015. s = xs_str_cat(s, "</details><p>\n");
  1016. s = xs_str_cat(s, "</div>\n");
  1017. /** controls **/
  1018. if (!local)
  1019. s = html_entry_controls(snac, s, msg, md5);
  1020. /** children **/
  1021. if (!hide_children) {
  1022. xs *children = object_children(id);
  1023. int left = xs_list_len(children);
  1024. if (left) {
  1025. char *p, *cmd5;
  1026. int older_open = 0;
  1027. xs *ss = xs_str_new(NULL);
  1028. int n_children = 0;
  1029. ss = xs_str_cat(ss, "<details open><summary>...</summary><p>\n");
  1030. if (level < 4)
  1031. ss = xs_str_cat(ss, "<div class=\"snac-children\">\n");
  1032. else
  1033. ss = xs_str_cat(ss, "<div>\n");
  1034. if (left > 3) {
  1035. xs *s1 = xs_fmt("<details><summary>%s</summary>\n", L("Older..."));
  1036. ss = xs_str_cat(ss, s1);
  1037. older_open = 1;
  1038. }
  1039. p = children;
  1040. while (xs_list_iter(&p, &cmd5)) {
  1041. xs *chd = NULL;
  1042. timeline_get_by_md5(snac, cmd5, &chd);
  1043. if (older_open && left <= 3) {
  1044. ss = xs_str_cat(ss, "</details>\n");
  1045. older_open = 0;
  1046. }
  1047. if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
  1048. ss = html_entry(snac, ss, chd, local, level + 1, cmd5, hide_children);
  1049. n_children++;
  1050. }
  1051. else
  1052. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", cmd5));
  1053. left--;
  1054. }
  1055. if (older_open)
  1056. ss = xs_str_cat(ss, "</details>\n");
  1057. ss = xs_str_cat(ss, "</div>\n");
  1058. ss = xs_str_cat(ss, "</details>\n");
  1059. if (n_children)
  1060. s = xs_str_cat(s, ss);
  1061. }
  1062. }
  1063. s = xs_str_cat(s, "</div>\n</div>\n");
  1064. return xs_str_cat(os, s);
  1065. }
  1066. xs_str *html_user_footer(xs_str *s)
  1067. {
  1068. xs *s1 = xs_fmt(
  1069. "<div class=\"snac-footer\">\n"
  1070. "<a href=\"%s\">%s</a> - "
  1071. "powered by <a href=\"%s\">"
  1072. "<abbr title=\"Social Networks Are Crap\">snac</abbr></a></div>\n",
  1073. srv_baseurl,
  1074. L("about this site"),
  1075. WHAT_IS_SNAC_URL
  1076. );
  1077. return xs_str_cat(s, s1);
  1078. }
  1079. xs_str *html_timeline(snac *snac, const xs_list *list, int local, int skip, int show, int show_more)
  1080. /* returns the HTML for the timeline */
  1081. {
  1082. xs_str *s = xs_str_new(NULL);
  1083. xs_list *p = (xs_list *)list;
  1084. char *v;
  1085. double t = ftime();
  1086. s = html_user_header(snac, s, local);
  1087. if (!local)
  1088. s = html_top_controls(snac, s, list == NULL ? 1 : 0);
  1089. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  1090. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1091. while (xs_list_iter(&p, &v)) {
  1092. xs *msg = NULL;
  1093. if (!valid_status(timeline_get_by_md5(snac, v, &msg)))
  1094. continue;
  1095. s = html_entry(snac, s, msg, local, 0, v, 0);
  1096. }
  1097. s = xs_str_cat(s, "</div>\n");
  1098. if (local) {
  1099. xs *s1 = xs_fmt(
  1100. "<div class=\"snac-history\">\n"
  1101. "<p class=\"snac-history-title\">%s</p><ul>\n",
  1102. L("History")
  1103. );
  1104. s = xs_str_cat(s, s1);
  1105. xs *list = history_list(snac);
  1106. char *p, *v;
  1107. p = list;
  1108. while (xs_list_iter(&p, &v)) {
  1109. xs *fn = xs_replace(v, ".html", "");
  1110. xs *s1 = xs_fmt(
  1111. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  1112. snac->actor, v, fn);
  1113. s = xs_str_cat(s, s1);
  1114. }
  1115. s = xs_str_cat(s, "</ul></div>\n");
  1116. }
  1117. {
  1118. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  1119. s = xs_str_cat(s, s1);
  1120. }
  1121. if (show_more) {
  1122. xs *s1 = xs_fmt(
  1123. "<p>"
  1124. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  1125. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("Load more..."));
  1126. s = xs_str_cat(s, s1);
  1127. }
  1128. s = html_user_footer(s);
  1129. s = xs_str_cat(s, "</body>\n</html>\n");
  1130. return s;
  1131. }
  1132. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  1133. {
  1134. xs *s = xs_str_new(NULL);
  1135. xs *es1 = encode_html(header);
  1136. xs *h = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", es1);
  1137. char *p, *actor_id;
  1138. s = xs_str_cat(s, h);
  1139. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1140. p = list;
  1141. while (xs_list_iter(&p, &actor_id)) {
  1142. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  1143. xs *actor = NULL;
  1144. if (valid_status(actor_get(snac, actor_id, &actor))) {
  1145. s = xs_str_cat(s, "<div class=\"snac-post\">\n<div class=\"snac-post-header\">\n");
  1146. s = html_actor_icon(s, actor, xs_dict_get(actor, "published"), NULL, NULL, 0);
  1147. s = xs_str_cat(s, "</div>\n");
  1148. /* content (user bio) */
  1149. char *c = xs_dict_get(actor, "summary");
  1150. if (!xs_is_null(c)) {
  1151. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  1152. xs *sc = sanitize(c);
  1153. if (xs_startswith(sc, "<p>"))
  1154. s = xs_str_cat(s, sc);
  1155. else {
  1156. xs *s1 = xs_fmt("<p>%s</p>", sc);
  1157. s = xs_str_cat(s, s1);
  1158. }
  1159. s = xs_str_cat(s, "</div>\n");
  1160. }
  1161. /* buttons */
  1162. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  1163. xs *s1 = xs_fmt(
  1164. "<p><form autocomplete=\"off\" method=\"post\" action=\"admin/action\">\n"
  1165. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  1166. "<input type=\"hidden\" name=\"actor-form\" value=\"yes\">\n",
  1167. actor_id
  1168. );
  1169. s = xs_str_cat(s, s1);
  1170. if (following_check(snac, actor_id))
  1171. s = html_button(s, "unfollow", L("Unfollow"));
  1172. else {
  1173. s = html_button(s, "follow", L("Follow"));
  1174. if (follower_check(snac, actor_id))
  1175. s = html_button(s, "delete", L("Delete"));
  1176. }
  1177. if (is_muted(snac, actor_id))
  1178. s = html_button(s, "unmute", L("Unmute"));
  1179. else
  1180. s = html_button(s, "mute", L("MUTE"));
  1181. s = xs_str_cat(s, "</form>\n");
  1182. /* the post textarea */
  1183. xs *s2 = xs_fmt(
  1184. "<p><details><summary>%s</summary>\n"
  1185. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  1186. "<form autocomplete=\"off\" method=\"post\" action=\"admin/note\" "
  1187. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  1188. "<textarea class=\"snac-textarea\" name=\"content\" "
  1189. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  1190. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  1191. "<p><input type=\"file\" name=\"attach\">\n"
  1192. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  1193. "</form><p></div>\n"
  1194. "</details><p>\n",
  1195. L("Direct Message..."),
  1196. md5, t,
  1197. md5,
  1198. actor_id,
  1199. L("Post")
  1200. );
  1201. s = xs_str_cat(s, s2);
  1202. s = xs_str_cat(s, "</div>\n");
  1203. s = xs_str_cat(s, "</div>\n");
  1204. }
  1205. }
  1206. s = xs_str_cat(s, "</div>\n");
  1207. return xs_str_cat(os, s);
  1208. }
  1209. d_char *html_people(snac *snac)
  1210. {
  1211. d_char *s = xs_str_new(NULL);
  1212. xs *wing = following_list(snac);
  1213. xs *wers = follower_list(snac);
  1214. s = html_user_header(snac, s, 0);
  1215. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  1216. s = html_people_list(snac, s, wers, L("People that follow you"), "e");
  1217. s = html_user_footer(s);
  1218. s = xs_str_cat(s, "</body>\n</html>\n");
  1219. return s;
  1220. }
  1221. xs_str *html_notifications(snac *snac)
  1222. {
  1223. xs_str *s = xs_str_new(NULL);
  1224. xs *n_list = notify_list(snac, 0);
  1225. xs *n_time = notify_check_time(snac, 0);
  1226. xs_list *p = n_list;
  1227. xs_str *v;
  1228. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  1229. s = html_user_header(snac, s, 0);
  1230. xs *s1 = xs_fmt(
  1231. "<form autocomplete=\"off\" "
  1232. "method=\"post\" action=\"admin/clear-notifications\" id=\"clear\">\n"
  1233. "<input type=\"submit\" class=\"snac-btn-like\" value=\"%s\">\n"
  1234. "</form><p>\n", L("Clear all"));
  1235. s = xs_str_cat(s, s1);
  1236. while (xs_list_iter(&p, &v)) {
  1237. xs *noti = notify_get(snac, v);
  1238. if (noti == NULL)
  1239. continue;
  1240. xs *obj = NULL;
  1241. const char *type = xs_dict_get(noti, "type");
  1242. const char *utype = xs_dict_get(noti, "utype");
  1243. const char *id = xs_dict_get(noti, "objid");
  1244. if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
  1245. continue;
  1246. if (is_hidden(snac, id))
  1247. continue;
  1248. const char *actor_id = xs_dict_get(noti, "actor");
  1249. xs *actor = NULL;
  1250. if (!valid_status(actor_get(snac, actor_id, &actor)))
  1251. continue;
  1252. xs *a_name = actor_name(actor);
  1253. if (strcmp(v, n_time) > 0) {
  1254. /* unseen notification */
  1255. if (stage == NHDR_NONE) {
  1256. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("New"));
  1257. s = xs_str_cat(s, s1);
  1258. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1259. stage = NHDR_NEW;
  1260. }
  1261. }
  1262. else {
  1263. /* already seen notification */
  1264. if (stage != NHDR_OLD) {
  1265. if (stage == NHDR_NEW)
  1266. s = xs_str_cat(s, "</div>\n");
  1267. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("Already seen"));
  1268. s = xs_str_cat(s, s1);
  1269. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1270. stage = NHDR_OLD;
  1271. }
  1272. }
  1273. const char *label = type;
  1274. if (strcmp(type, "Create") == 0)
  1275. label = L("Mention");
  1276. else
  1277. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1278. label = L("Finished poll");
  1279. else
  1280. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  1281. label = L("Unfollow");
  1282. xs *es1 = encode_html(label);
  1283. xs *s1 = xs_fmt("<div class=\"snac-post-with-desc\">\n"
  1284. "<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  1285. es1, actor_id, a_name);
  1286. s = xs_str_cat(s, s1);
  1287. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  1288. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  1289. s = html_actor_icon(s, actor, NULL, NULL, NULL, 0);
  1290. s = xs_str_cat(s, "</div>\n");
  1291. }
  1292. else {
  1293. xs *md5 = xs_md5_hex(id, strlen(id));
  1294. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  1295. }
  1296. s = xs_str_cat(s, "</div>\n");
  1297. }
  1298. if (stage == NHDR_NONE) {
  1299. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("None"));
  1300. s = xs_str_cat(s, s1);
  1301. }
  1302. else
  1303. s = xs_str_cat(s, "</div>\n");
  1304. s = html_user_footer(s);
  1305. s = xs_str_cat(s, "</body>\n</html>\n");
  1306. /* set the check time to now */
  1307. xs *dummy = notify_check_time(snac, 1);
  1308. dummy = xs_free(dummy);
  1309. timeline_touch(snac);
  1310. return s;
  1311. }
  1312. int html_get_handler(const xs_dict *req, const char *q_path,
  1313. char **body, int *b_size, char **ctype, xs_str **etag)
  1314. {
  1315. char *accept = xs_dict_get(req, "accept");
  1316. int status = 404;
  1317. snac snac;
  1318. xs *uid = NULL;
  1319. char *p_path;
  1320. int cache = 1;
  1321. int save = 1;
  1322. char *v;
  1323. xs *l = xs_split_n(q_path, "/", 2);
  1324. v = xs_list_get(l, 1);
  1325. if (xs_is_null(v)) {
  1326. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1327. return 404;
  1328. }
  1329. uid = xs_dup(v);
  1330. /* rss extension? */
  1331. if (xs_endswith(uid, ".rss")) {
  1332. uid = xs_crop_i(uid, 0, -4);
  1333. p_path = ".rss";
  1334. }
  1335. else
  1336. p_path = xs_list_get(l, 2);
  1337. if (!uid || !user_open(&snac, uid)) {
  1338. /* invalid user */
  1339. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1340. return 404;
  1341. }
  1342. /* return the RSS if requested by Accept header */
  1343. if (accept != NULL) {
  1344. if (xs_str_in(accept, "text/xml") != -1 ||
  1345. xs_str_in(accept, "application/rss+xml") != -1)
  1346. p_path = ".rss";
  1347. }
  1348. /* check if server config variable 'disable_cache' is set */
  1349. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1350. cache = 0;
  1351. int skip = 0;
  1352. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1353. char *q_vars = xs_dict_get(req, "q_vars");
  1354. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1355. skip = atoi(v), cache = 0, save = 0;
  1356. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1357. show = atoi(v), cache = 0, save = 0;
  1358. if (p_path == NULL) { /** public timeline **/
  1359. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1360. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1361. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1362. *body = history_get(&snac, h);
  1363. *b_size = strlen(*body);
  1364. status = 200;
  1365. }
  1366. else {
  1367. xs *list = timeline_list(&snac, "public", skip, show);
  1368. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1369. xs *pins = pinned_list(&snac);
  1370. pins = xs_list_cat(pins, list);
  1371. *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next));
  1372. *b_size = strlen(*body);
  1373. status = 200;
  1374. if (save)
  1375. history_add(&snac, h, *body, *b_size);
  1376. }
  1377. }
  1378. else
  1379. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  1380. if (!login(&snac, req)) {
  1381. *body = xs_dup(uid);
  1382. status = 401;
  1383. }
  1384. else {
  1385. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1386. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1387. *body = history_get(&snac, "timeline.html_");
  1388. *b_size = strlen(*body);
  1389. status = 200;
  1390. }
  1391. else {
  1392. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1393. xs *list = timeline_list(&snac, "private", skip, show);
  1394. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1395. xs *pins = pinned_list(&snac);
  1396. pins = xs_list_cat(pins, list);
  1397. *body = html_timeline(&snac, pins, 0, skip, show, xs_list_len(next));
  1398. *b_size = strlen(*body);
  1399. status = 200;
  1400. if (save)
  1401. history_add(&snac, "timeline.html_", *body, *b_size);
  1402. }
  1403. }
  1404. }
  1405. else
  1406. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  1407. if (!login(&snac, req)) {
  1408. *body = xs_dup(uid);
  1409. status = 401;
  1410. }
  1411. else {
  1412. *body = html_people(&snac);
  1413. *b_size = strlen(*body);
  1414. status = 200;
  1415. }
  1416. }
  1417. else
  1418. if (strcmp(p_path, "settings") == 0) { /** user settings **/
  1419. if (!login(&snac, req)) {
  1420. *body = xs_dup(uid);
  1421. status = 401;
  1422. }
  1423. else {
  1424. *body = html_timeline(&snac, NULL, 0, 0, 0, 0);
  1425. *b_size = strlen(*body);
  1426. status = 200;
  1427. }
  1428. }
  1429. else
  1430. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  1431. if (!login(&snac, req)) {
  1432. *body = xs_dup(uid);
  1433. status = 401;
  1434. }
  1435. else {
  1436. *body = html_notifications(&snac);
  1437. *b_size = strlen(*body);
  1438. status = 200;
  1439. }
  1440. }
  1441. else
  1442. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  1443. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1444. xs *msg = NULL;
  1445. if (valid_status(object_get(id, &msg))) {
  1446. xs *md5 = xs_md5_hex(id, strlen(id));
  1447. xs *list = xs_list_new();
  1448. list = xs_list_append(list, md5);
  1449. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  1450. *b_size = strlen(*body);
  1451. status = 200;
  1452. }
  1453. }
  1454. else
  1455. if (xs_startswith(p_path, "s/")) { /** a static file **/
  1456. xs *l = xs_split(p_path, "/");
  1457. char *id = xs_list_get(l, 1);
  1458. int sz;
  1459. if (id && *id) {
  1460. status = static_get(&snac, id, body, &sz,
  1461. xs_dict_get(req, "if-none-match"), etag);
  1462. if (valid_status(status)) {
  1463. *b_size = sz;
  1464. *ctype = xs_mime_by_ext(id);
  1465. }
  1466. }
  1467. }
  1468. else
  1469. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  1470. xs *l = xs_split(p_path, "/");
  1471. char *id = xs_list_get(l, 1);
  1472. if (id && *id) {
  1473. if (xs_endswith(id, "timeline.html_")) {
  1474. /* Don't let them in */
  1475. *b_size = 0;
  1476. status = 404;
  1477. }
  1478. else
  1479. if ((*body = history_get(&snac, id)) != NULL) {
  1480. *b_size = strlen(*body);
  1481. status = 200;
  1482. }
  1483. }
  1484. }
  1485. else
  1486. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  1487. d_char *rss;
  1488. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1489. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  1490. char *p, *v;
  1491. xs *es1 = encode_html(xs_dict_get(snac.config, "name"));
  1492. xs *es2 = encode_html(snac.uid);
  1493. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  1494. xs *es4 = encode_html(bio);
  1495. rss = xs_fmt(
  1496. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1497. "<rss version=\"0.91\">\n"
  1498. "<channel>\n"
  1499. "<title>%s (@%s@%s)</title>\n"
  1500. "<language>en</language>\n"
  1501. "<link>%s.rss</link>\n"
  1502. "<description>%s</description>\n",
  1503. es1,
  1504. es2,
  1505. es3,
  1506. snac.actor,
  1507. es4
  1508. );
  1509. p = elems;
  1510. while (xs_list_iter(&p, &v)) {
  1511. xs *msg = NULL;
  1512. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1513. continue;
  1514. char *id = xs_dict_get(msg, "id");
  1515. if (!xs_startswith(id, snac.actor))
  1516. continue;
  1517. xs *content = sanitize(xs_dict_get(msg, "content"));
  1518. // We SHOULD only use sanitized one for description.
  1519. // So, only encode for feed title, while the description just keep it sanitized as is.
  1520. xs *es_title_enc = encode_html(xs_dict_get(msg, "content"));
  1521. xs *es_title = xs_replace(es_title_enc, "<br>", "\n");
  1522. xs *title = xs_str_new(NULL);
  1523. int i;
  1524. for (i = 0; es_title[i] && es_title[i] != '\n' && i < 50; i++)
  1525. title = xs_append_m(title, &es_title[i], 1);
  1526. xs *s = xs_fmt(
  1527. "<item>\n"
  1528. "<title>%s...</title>\n"
  1529. "<link>%s</link>\n"
  1530. "<description>%s</description>\n"
  1531. "</item>\n",
  1532. title, id, content
  1533. );
  1534. rss = xs_str_cat(rss, s);
  1535. }
  1536. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  1537. *body = rss;
  1538. *b_size = strlen(rss);
  1539. *ctype = "application/rss+xml; charset=utf-8";
  1540. status = 200;
  1541. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1542. }
  1543. else
  1544. status = 404;
  1545. user_free(&snac);
  1546. if (valid_status(status) && *ctype == NULL) {
  1547. *ctype = "text/html; charset=utf-8";
  1548. }
  1549. return status;
  1550. }
  1551. int html_post_handler(const xs_dict *req, const char *q_path,
  1552. char *payload, int p_size,
  1553. char **body, int *b_size, char **ctype)
  1554. {
  1555. (void)p_size;
  1556. (void)ctype;
  1557. int status = 0;
  1558. snac snac;
  1559. char *uid, *p_path;
  1560. xs_dict *p_vars;
  1561. xs *l = xs_split_n(q_path, "/", 2);
  1562. uid = xs_list_get(l, 1);
  1563. if (!uid || !user_open(&snac, uid)) {
  1564. /* invalid user */
  1565. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1566. return 404;
  1567. }
  1568. p_path = xs_list_get(l, 2);
  1569. /* all posts must be authenticated */
  1570. if (!login(&snac, req)) {
  1571. user_free(&snac);
  1572. *body = xs_dup(uid);
  1573. return 401;
  1574. }
  1575. p_vars = xs_dict_get(req, "p_vars");
  1576. #if 0
  1577. {
  1578. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  1579. printf("%s\n", j1);
  1580. }
  1581. #endif
  1582. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  1583. /* post note */
  1584. xs_str *content = xs_dict_get(p_vars, "content");
  1585. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1586. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1587. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1588. xs_str *to = xs_dict_get(p_vars, "to");
  1589. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1590. xs_str *summary = xs_dict_get(p_vars, "summary");
  1591. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1592. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1593. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1594. xs *attach_list = xs_list_new();
  1595. /* default alt text */
  1596. if (xs_is_null(alt_text))
  1597. alt_text = "";
  1598. /* is attach_url set? */
  1599. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  1600. xs *l = xs_list_new();
  1601. l = xs_list_append(l, attach_url);
  1602. l = xs_list_append(l, alt_text);
  1603. attach_list = xs_list_append(attach_list, l);
  1604. }
  1605. /* is attach_file set? */
  1606. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  1607. char *fn = xs_list_get(attach_file, 0);
  1608. if (*fn != '\0') {
  1609. char *ext = strrchr(fn, '.');
  1610. xs *hash = xs_md5_hex(fn, strlen(fn));
  1611. xs *id = xs_fmt("%s%s", hash, ext);
  1612. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1613. int fo = xs_number_get(xs_list_get(attach_file, 1));
  1614. int fs = xs_number_get(xs_list_get(attach_file, 2));
  1615. /* store */
  1616. static_put(&snac, id, payload + fo, fs);
  1617. xs *l = xs_list_new();
  1618. l = xs_list_append(l, url);
  1619. l = xs_list_append(l, alt_text);
  1620. attach_list = xs_list_append(attach_list, l);
  1621. }
  1622. }
  1623. if (content != NULL) {
  1624. xs *msg = NULL;
  1625. xs *c_msg = NULL;
  1626. xs *content_2 = xs_replace(content, "\r", "");
  1627. xs *poll_opts = NULL;
  1628. /* is there a valid set of poll options? */
  1629. const char *v = xs_dict_get(p_vars, "poll_options");
  1630. if (!xs_is_null(v) && *v) {
  1631. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  1632. poll_opts = xs_split(v2, "\n");
  1633. }
  1634. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  1635. /* get the rest of poll configuration */
  1636. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  1637. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  1638. int multiple = 0;
  1639. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  1640. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  1641. multiple = 1;
  1642. msg = msg_question(&snac, content_2, attach_list,
  1643. poll_opts, multiple, end_secs);
  1644. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  1645. }
  1646. else
  1647. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  1648. if (sensitive != NULL) {
  1649. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1650. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  1651. }
  1652. if (xs_is_null(edit_id)) {
  1653. /* new message */
  1654. c_msg = msg_create(&snac, msg);
  1655. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1656. }
  1657. else {
  1658. /* an edition of a previous message */
  1659. xs *p_msg = NULL;
  1660. if (valid_status(object_get(edit_id, &p_msg))) {
  1661. /* copy relevant fields from previous version */
  1662. char *fields[] = { "id", "context", "url", "published",
  1663. "to", "inReplyTo", NULL };
  1664. int n;
  1665. for (n = 0; fields[n]; n++) {
  1666. char *v = xs_dict_get(p_msg, fields[n]);
  1667. msg = xs_dict_set(msg, fields[n], v);
  1668. }
  1669. /* set the updated field */
  1670. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  1671. msg = xs_dict_set(msg, "updated", updated);
  1672. /* overwrite object, not updating the indexes */
  1673. object_add_ow(edit_id, msg);
  1674. /* update message */
  1675. c_msg = msg_update(&snac, msg);
  1676. }
  1677. else
  1678. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  1679. }
  1680. if (c_msg != NULL)
  1681. enqueue_message(&snac, c_msg);
  1682. history_del(&snac, "timeline.html_");
  1683. }
  1684. status = 303;
  1685. }
  1686. else
  1687. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  1688. /* action on an entry */
  1689. char *id = xs_dict_get(p_vars, "id");
  1690. char *actor = xs_dict_get(p_vars, "actor");
  1691. char *action = xs_dict_get(p_vars, "action");
  1692. if (action == NULL)
  1693. return 404;
  1694. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1695. status = 303;
  1696. if (strcmp(action, L("Like")) == 0) { /** **/
  1697. xs *msg = msg_admiration(&snac, id, "Like");
  1698. if (msg != NULL) {
  1699. enqueue_message(&snac, msg);
  1700. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  1701. }
  1702. }
  1703. else
  1704. if (strcmp(action, L("Boost")) == 0) { /** **/
  1705. xs *msg = msg_admiration(&snac, id, "Announce");
  1706. if (msg != NULL) {
  1707. enqueue_message(&snac, msg);
  1708. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  1709. }
  1710. }
  1711. else
  1712. if (strcmp(action, L("MUTE")) == 0) { /** **/
  1713. mute(&snac, actor);
  1714. }
  1715. else
  1716. if (strcmp(action, L("Unmute")) == 0) { /** **/
  1717. unmute(&snac, actor);
  1718. }
  1719. else
  1720. if (strcmp(action, L("Hide")) == 0) { /** **/
  1721. hide(&snac, id);
  1722. }
  1723. else
  1724. if (strcmp(action, L("Follow")) == 0) { /** **/
  1725. xs *msg = msg_follow(&snac, actor);
  1726. if (msg != NULL) {
  1727. /* reload the actor from the message, in may be different */
  1728. actor = xs_dict_get(msg, "object");
  1729. following_add(&snac, actor, msg);
  1730. enqueue_output_by_actor(&snac, msg, actor, 0);
  1731. }
  1732. }
  1733. else
  1734. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  1735. /* get the following object */
  1736. xs *object = NULL;
  1737. if (valid_status(following_get(&snac, actor, &object))) {
  1738. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1739. following_del(&snac, actor);
  1740. enqueue_output_by_actor(&snac, msg, actor, 0);
  1741. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1742. }
  1743. else
  1744. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1745. }
  1746. else
  1747. if (strcmp(action, L("Delete")) == 0) { /** **/
  1748. char *actor_form = xs_dict_get(p_vars, "actor-form");
  1749. if (actor_form != NULL) {
  1750. /* delete follower */
  1751. if (valid_status(follower_del(&snac, actor)))
  1752. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  1753. else
  1754. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  1755. }
  1756. else {
  1757. /* delete an entry */
  1758. if (xs_startswith(id, snac.actor)) {
  1759. /* it's a post by us: generate a delete */
  1760. xs *msg = msg_delete(&snac, id);
  1761. enqueue_message(&snac, msg);
  1762. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1763. }
  1764. timeline_del(&snac, id);
  1765. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1766. }
  1767. }
  1768. else
  1769. if (strcmp(action, L("Pin")) == 0) { /** **/
  1770. pin(&snac, id);
  1771. timeline_touch(&snac);
  1772. }
  1773. else
  1774. if (strcmp(action, L("Unpin")) == 0) { /** **/
  1775. unpin(&snac, id);
  1776. timeline_touch(&snac);
  1777. }
  1778. else
  1779. status = 404;
  1780. /* delete the cached timeline */
  1781. if (status == 303)
  1782. history_del(&snac, "timeline.html_");
  1783. }
  1784. else
  1785. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  1786. /* change of user data */
  1787. char *v;
  1788. char *p1, *p2;
  1789. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1790. snac.config = xs_dict_set(snac.config, "name", v);
  1791. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1792. snac.config = xs_dict_set(snac.config, "avatar", v);
  1793. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1794. snac.config = xs_dict_set(snac.config, "bio", v);
  1795. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1796. strcmp(v, "on") == 0) {
  1797. snac.config = xs_dict_set(snac.config, "cw", "open");
  1798. } else { /* if the checkbox is not set, the parameter is missing */
  1799. snac.config = xs_dict_set(snac.config, "cw", "");
  1800. }
  1801. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1802. snac.config = xs_dict_set(snac.config, "email", v);
  1803. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  1804. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  1805. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  1806. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  1807. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  1808. xs *days = xs_number_new(atof(v));
  1809. snac.config = xs_dict_set(snac.config, "purge_days", days);
  1810. }
  1811. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  1812. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_true);
  1813. else
  1814. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_false);
  1815. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  1816. snac.config = xs_dict_set(snac.config, "bot", xs_stock_true);
  1817. else
  1818. snac.config = xs_dict_set(snac.config, "bot", xs_stock_false);
  1819. /* avatar upload */
  1820. xs_list *avatar_file = xs_dict_get(p_vars, "avatar_file");
  1821. if (!xs_is_null(avatar_file) && xs_type(avatar_file) == XSTYPE_LIST) {
  1822. char *fn = xs_list_get(avatar_file, 0);
  1823. if (*fn != '\0') {
  1824. char *ext = strrchr(fn, '.');
  1825. xs *id = xs_fmt("avatar%s", ext);
  1826. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1827. int fo = xs_number_get(xs_list_get(avatar_file, 1));
  1828. int fs = xs_number_get(xs_list_get(avatar_file, 2));
  1829. /* store */
  1830. static_put(&snac, id, payload + fo, fs);
  1831. snac.config = xs_dict_set(snac.config, "avatar", url);
  1832. }
  1833. }
  1834. /* password change? */
  1835. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1836. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1837. *p1 && strcmp(p1, p2) == 0) {
  1838. xs *pw = hash_password(snac.uid, p1, NULL);
  1839. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1840. }
  1841. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1842. xs *bfn = xs_fmt("%s.bak", fn);
  1843. FILE *f;
  1844. rename(fn, bfn);
  1845. if ((f = fopen(fn, "w")) != NULL) {
  1846. xs *j = xs_json_dumps_pp(snac.config, 4);
  1847. fwrite(j, strlen(j), 1, f);
  1848. fclose(f);
  1849. }
  1850. else
  1851. rename(bfn, fn);
  1852. history_del(&snac, "timeline.html_");
  1853. xs *a_msg = msg_actor(&snac);
  1854. xs *u_msg = msg_update(&snac, a_msg);
  1855. enqueue_message(&snac, u_msg);
  1856. status = 303;
  1857. }
  1858. else
  1859. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  1860. notify_clear(&snac);
  1861. timeline_touch(&snac);
  1862. status = 303;
  1863. }
  1864. else
  1865. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  1866. char *irt = xs_dict_get(p_vars, "irt");
  1867. const char *opt = xs_dict_get(p_vars, "question");
  1868. const char *actor = xs_dict_get(p_vars, "actor");
  1869. xs *ls = NULL;
  1870. /* multiple choices? */
  1871. if (xs_type(opt) == XSTYPE_LIST)
  1872. ls = xs_dup(opt);
  1873. else {
  1874. ls = xs_list_new();
  1875. ls = xs_list_append(ls, opt);
  1876. }
  1877. xs_list *p = ls;
  1878. xs_str *v;
  1879. while (xs_list_iter(&p, &v)) {
  1880. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
  1881. /* set the option */
  1882. msg = xs_dict_append(msg, "name", v);
  1883. xs *c_msg = msg_create(&snac, msg);
  1884. enqueue_message(&snac, c_msg);
  1885. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1886. }
  1887. status = 303;
  1888. }
  1889. if (status == 303) {
  1890. char *redir = xs_dict_get(p_vars, "redir");
  1891. if (xs_is_null(redir))
  1892. redir = "";
  1893. *body = xs_fmt("../admin#%s", redir);
  1894. *b_size = strlen(*body);
  1895. }
  1896. user_free(&snac);
  1897. return status;
  1898. }