html.c 63 KB

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