html.c 69 KB

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