html.c 67 KB

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