html.c 64 KB

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