html.c 74 KB

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