html.c 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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. /* bring the main actor */
  602. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  603. return os;
  604. /* ignore muted morons immediately */
  605. if (is_muted(snac, actor))
  606. return os;
  607. if (strcmp(actor, snac->actor) != 0 && !valid_status(actor_get(snac, actor, NULL)))
  608. return os;
  609. s = xs_str_cat(s, "<div class=\"snac-score\">"); /** **/
  610. if (strcmp(type, "Question") == 0) {
  611. /* add the ballot box emoji */
  612. xs *f = xs_fmt("<span title=\"%s\"> &#128499; </span>", L("Poll"));
  613. s = xs_str_cat(s, f);
  614. }
  615. /* if this is our post, add the score */
  616. if (xs_startswith(id, snac->actor)) {
  617. int n_likes = object_likes_len(id);
  618. int n_boosts = object_announces_len(id);
  619. /* alternate emojis: %d &#128077; %d &#128257; */
  620. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  621. s = xs_str_cat(s, s1);
  622. }
  623. s = xs_str_cat(s, "</div>\n");
  624. if (level == 0)
  625. s = xs_str_cat(s, "<div class=\"snac-post\">\n"); /** **/
  626. else
  627. s = xs_str_cat(s, "<div class=\"snac-child\">\n"); /** **/
  628. if (boosts == NULL)
  629. boosts = object_announces(id);
  630. if (xs_list_len(boosts)) {
  631. /* if somebody boosted this, show as origin */
  632. char *p = xs_list_get(boosts, -1);
  633. xs *actor_r = NULL;
  634. if (xs_list_in(boosts, snac->md5) != -1) {
  635. /* we boosted this */
  636. xs *s1 = xs_fmt(
  637. "<div class=\"snac-origin\">"
  638. "<a href=\"%s\">%s</a> %s</a></div>",
  639. snac->actor, xs_dict_get(snac->config, "name"), L("boosted")
  640. );
  641. s = xs_str_cat(s, s1);
  642. }
  643. else
  644. if (valid_status(object_get_by_md5(p, &actor_r))) {
  645. xs *name = actor_name(actor_r);
  646. if (!xs_is_null(name)) {
  647. xs *s1 = xs_fmt(
  648. "<div class=\"snac-origin\">"
  649. "<a href=\"%s\">%s</a> %s</div>\n",
  650. xs_dict_get(actor_r, "id"),
  651. name,
  652. L("boosted")
  653. );
  654. s = xs_str_cat(s, s1);
  655. }
  656. }
  657. }
  658. else
  659. if (strcmp(type, "Note") == 0) {
  660. if (level == 0) {
  661. /* is the parent not here? */
  662. char *parent = xs_dict_get(msg, "inReplyTo");
  663. if (!xs_is_null(parent) && *parent && !timeline_here(snac, parent)) {
  664. xs *s1 = xs_fmt(
  665. "<div class=\"snac-origin\">%s "
  666. "<a href=\"%s\">»</a></div>\n",
  667. L("in reply to"), parent
  668. );
  669. s = xs_str_cat(s, s1);
  670. }
  671. }
  672. }
  673. s = html_msg_icon(snac, s, msg);
  674. /* add the content */
  675. s = xs_str_cat(s, "<div class=\"e-content snac-content\">\n"); /** **/
  676. /* is it sensitive? */
  677. if (!xs_is_null(v = xs_dict_get(msg, "sensitive")) && xs_type(v) == XSTYPE_TRUE) {
  678. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  679. v = "...";
  680. /* only show it when not in the public timeline and the config setting is "open" */
  681. char *cw = xs_dict_get(snac->config, "cw");
  682. if (xs_is_null(cw) || local)
  683. cw = "";
  684. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, v, L("SENSITIVE CONTENT"));
  685. s = xs_str_cat(s, s1);
  686. sensitive = 1;
  687. }
  688. #if 0
  689. {
  690. xs *md5 = xs_md5_hex(id, strlen(id));
  691. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  692. s = xs_str_cat(s, s1);
  693. }
  694. #endif
  695. {
  696. const char *content = xs_dict_get(msg, "content");
  697. xs *c = sanitize(xs_is_null(content) ? "" : content);
  698. char *p, *v;
  699. /* do some tweaks to the content */
  700. c = xs_replace_i(c, "\r", "");
  701. while (xs_endswith(c, "<br><br>"))
  702. c = xs_crop_i(c, 0, -4);
  703. c = xs_replace_i(c, "<br><br>", "<p>");
  704. if (!xs_startswith(c, "<p>")) {
  705. xs *s1 = c;
  706. c = xs_fmt("<p>%s</p>", s1);
  707. }
  708. /* replace the :shortnames: */
  709. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  710. /* iterate the tags */
  711. while (xs_list_iter(&p, &v)) {
  712. char *t = xs_dict_get(v, "type");
  713. if (t && strcmp(t, "Emoji") == 0) {
  714. char *n = xs_dict_get(v, "name");
  715. char *i = xs_dict_get(v, "icon");
  716. if (n && i) {
  717. char *u = xs_dict_get(i, "url");
  718. xs *img = xs_fmt("<img src=\"%s\" style=\"height: 1em\" "
  719. "loading=\"lazy\"/>", u);
  720. c = xs_replace_i(c, n, img);
  721. }
  722. }
  723. }
  724. }
  725. if (strcmp(type, "Question") == 0) { /** question content **/
  726. xs_list *oo = xs_dict_get(msg, "oneOf");
  727. xs_list *ao = xs_dict_get(msg, "anyOf");
  728. xs_list *p;
  729. xs_dict *v;
  730. int closed = 0;
  731. if (xs_dict_get(msg, "closed"))
  732. closed = 2;
  733. else
  734. if (xs_startswith(id, snac->actor))
  735. closed = 1; /* we questioned; closed for us */
  736. else {
  737. /* not yet closed? check if we already voted for this */
  738. xs *children = object_children(id);
  739. p = children;
  740. while (!closed && xs_list_iter(&p, &v)) {
  741. xs *msg = NULL;
  742. if (valid_status(object_get_by_md5(v, &msg))) {
  743. const char *atto = xs_dict_get(msg, "attributedTo");
  744. if (atto && strcmp(atto, snac->actor) == 0)
  745. closed = 1; /* we already voted; closed for us */
  746. }
  747. }
  748. }
  749. /* get the appropriate list of options */
  750. p = oo != NULL ? oo : ao;
  751. if (closed) {
  752. /* closed poll */
  753. c = xs_str_cat(c, "<table>\n");
  754. while (xs_list_iter(&p, &v)) {
  755. const char *name = xs_dict_get(v, "name");
  756. const xs_dict *replies = xs_dict_get(v, "replies");
  757. if (name && replies) {
  758. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  759. xs *l = xs_fmt("<tr><td>%s:</td><td>%d</td></tr>\n", name, nr);
  760. c = xs_str_cat(c, l);
  761. }
  762. }
  763. c = xs_str_cat(c, "</table>\n");
  764. /* if it's *really* closed, say it */
  765. if (closed == 2) {
  766. xs *s1 = xs_fmt("<p>%s</p>\n", L("Closed"));
  767. c = xs_str_cat(c, s1);
  768. }
  769. }
  770. else {
  771. /* poll still active */
  772. xs *s1 = xs_fmt("<form method=\"post\" action=\"%s/admin/vote\">\n"
  773. "<input type=\"hidden\" name=\"actor\" value= \"%s\">\n"
  774. "<input type=\"hidden\" name=\"irt\" value=\"%s\">\n",
  775. snac->actor, actor, id);
  776. while (xs_list_iter(&p, &v)) {
  777. const char *name = xs_dict_get(v, "name");
  778. if (name) {
  779. xs *opt = xs_fmt("<input type=\"%s\""
  780. " id=\"%s\" value=\"%s\" name=\"question\"> %s<br>\n",
  781. !xs_is_null(oo) ? "radio" : "checkbox",
  782. name, name, name);
  783. s1 = xs_str_cat(s1, opt);
  784. }
  785. }
  786. xs *s2 = xs_fmt("<p><input type=\"submit\" "
  787. "class=\"button\" value=\"%s\">\n</form>\n\n", L("Vote"));
  788. s1 = xs_str_cat(s1, s2);
  789. c = xs_str_cat(c, s1);
  790. }
  791. }
  792. s = xs_str_cat(s, c);
  793. }
  794. s = xs_str_cat(s, "\n");
  795. /* add the attachments */
  796. char *attach;
  797. if ((attach = xs_dict_get(msg, "attachment")) != NULL) { /** **/
  798. char *v;
  799. while (xs_list_iter(&attach, &v)) {
  800. char *t = xs_dict_get(v, "mediaType");
  801. if (xs_is_null(t))
  802. continue;
  803. if (xs_startswith(t, "image/")) {
  804. char *url = xs_dict_get(v, "url");
  805. char *name = xs_dict_get(v, "name");
  806. if (url != NULL) {
  807. xs *s1 = xs_fmt("<p><img src=\"%s\" alt=\"%s\" loading=\"lazy\"/></p>\n",
  808. url, xs_is_null(name) ? "" : name);
  809. s = xs_str_cat(s, s1);
  810. }
  811. }
  812. else
  813. if (xs_startswith(t, "video/")) {
  814. char *url = xs_dict_get(v, "url");
  815. if (url != NULL) {
  816. xs *s1 = xs_fmt("<p><object data=\"%s\"></object></p>\n", url);
  817. s = xs_str_cat(s, s1);
  818. }
  819. }
  820. }
  821. }
  822. if (sensitive)
  823. s = xs_str_cat(s, "</details><p>\n");
  824. s = xs_str_cat(s, "</div>\n");
  825. /** controls **/
  826. if (!local)
  827. s = html_entry_controls(snac, s, msg, md5);
  828. /** children **/
  829. if (!hide_children && strcmp(type, "Question") != 0) {
  830. xs *children = object_children(id);
  831. int left = xs_list_len(children);
  832. if (left) {
  833. char *p, *cmd5;
  834. int older_open = 0;
  835. xs *ss = xs_str_new(NULL);
  836. int n_children = 0;
  837. ss = xs_str_cat(ss, "<details open><summary>...</summary><p>\n");
  838. if (level < 4)
  839. ss = xs_str_cat(ss, "<div class=\"snac-children\">\n");
  840. else
  841. ss = xs_str_cat(ss, "<div>\n");
  842. if (left > 3) {
  843. xs *s1 = xs_fmt("<details><summary>%s</summary>\n", L("Older..."));
  844. ss = xs_str_cat(ss, s1);
  845. older_open = 1;
  846. }
  847. p = children;
  848. while (xs_list_iter(&p, &cmd5)) {
  849. xs *chd = NULL;
  850. timeline_get_by_md5(snac, cmd5, &chd);
  851. if (older_open && left <= 3) {
  852. ss = xs_str_cat(ss, "</details>\n");
  853. older_open = 0;
  854. }
  855. if (chd != NULL) {
  856. ss = html_entry(snac, ss, chd, local, level + 1, cmd5, hide_children);
  857. n_children++;
  858. }
  859. else
  860. snac_debug(snac, 2, xs_fmt("cannot read from timeline child %s", cmd5));
  861. left--;
  862. }
  863. if (older_open)
  864. ss = xs_str_cat(ss, "</details>\n");
  865. ss = xs_str_cat(ss, "</div>\n");
  866. ss = xs_str_cat(ss, "</details>\n");
  867. if (n_children)
  868. s = xs_str_cat(s, ss);
  869. }
  870. }
  871. s = xs_str_cat(s, "</div>\n</div>\n");
  872. return xs_str_cat(os, s);
  873. }
  874. xs_str *html_user_footer(xs_str *s)
  875. {
  876. xs *s1 = xs_fmt(
  877. "<div class=\"snac-footer\">\n"
  878. "<a href=\"%s\">%s</a> - "
  879. "powered by <a href=\"%s\""
  880. "<abbr title=\"Social Networks Are Crap\">snac</abbr></div></a>\n",
  881. srv_baseurl,
  882. L("about this site"),
  883. WHAT_IS_SNAC_URL
  884. );
  885. return xs_str_cat(s, s1);
  886. }
  887. xs_str *html_timeline(snac *snac, const xs_list *list, int local, int skip, int show, int show_more)
  888. /* returns the HTML for the timeline */
  889. {
  890. xs_str *s = xs_str_new(NULL);
  891. xs_list *p = (xs_list *)list;
  892. char *v;
  893. double t = ftime();
  894. s = html_user_header(snac, s, local);
  895. if (!local)
  896. s = html_top_controls(snac, s);
  897. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  898. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  899. while (xs_list_iter(&p, &v)) {
  900. xs *msg = NULL;
  901. if (!valid_status(timeline_get_by_md5(snac, v, &msg)))
  902. continue;
  903. s = html_entry(snac, s, msg, local, 0, v, 0);
  904. }
  905. s = xs_str_cat(s, "</div>\n");
  906. if (local) {
  907. xs *s1 = xs_fmt(
  908. "<div class=\"snac-history\">\n"
  909. "<p class=\"snac-history-title\">%s</p><ul>\n",
  910. L("History")
  911. );
  912. s = xs_str_cat(s, s1);
  913. xs *list = history_list(snac);
  914. char *p, *v;
  915. p = list;
  916. while (xs_list_iter(&p, &v)) {
  917. xs *fn = xs_replace(v, ".html", "");
  918. xs *s1 = xs_fmt(
  919. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  920. snac->actor, v, fn);
  921. s = xs_str_cat(s, s1);
  922. }
  923. s = xs_str_cat(s, "</ul></div>\n");
  924. }
  925. {
  926. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  927. s = xs_str_cat(s, s1);
  928. }
  929. if (show_more) {
  930. xs *s1 = xs_fmt(
  931. "<p>"
  932. "<a href=\"%s%s?skip=%d&show=%d\" name=\"snac-more\">%s</a>"
  933. "</p>\n", snac->actor, local ? "" : "/admin", skip + show, show, L("Load more..."));
  934. s = xs_str_cat(s, s1);
  935. }
  936. s = html_user_footer(s);
  937. s = xs_str_cat(s, "</body>\n</html>\n");
  938. return s;
  939. }
  940. d_char *html_people_list(snac *snac, d_char *os, d_char *list, const char *header, const char *t)
  941. {
  942. xs *s = xs_str_new(NULL);
  943. xs *h = xs_fmt("<h2>%s</h2>\n", header);
  944. char *p, *actor_id;
  945. s = xs_str_cat(s, h);
  946. p = list;
  947. while (xs_list_iter(&p, &actor_id)) {
  948. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  949. xs *actor = NULL;
  950. if (valid_status(actor_get(snac, actor_id, &actor))) {
  951. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  952. s = html_actor_icon(s, actor, xs_dict_get(actor, "published"), NULL, NULL, 0);
  953. /* content (user bio) */
  954. char *c = xs_dict_get(actor, "summary");
  955. if (!xs_is_null(c)) {
  956. s = xs_str_cat(s, "<div class=\"snac-content\">\n");
  957. xs *sc = sanitize(c);
  958. if (xs_startswith(sc, "<p>"))
  959. s = xs_str_cat(s, sc);
  960. else {
  961. xs *s1 = xs_fmt("<p>%s</p>", sc);
  962. s = xs_str_cat(s, s1);
  963. }
  964. s = xs_str_cat(s, "</div>\n");
  965. }
  966. /* buttons */
  967. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  968. xs *s1 = xs_fmt(
  969. "<p><form method=\"post\" action=\"%s/admin/action\">\n"
  970. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  971. "<input type=\"hidden\" name=\"actor-form\" value=\"yes\">\n",
  972. snac->actor, actor_id
  973. );
  974. s = xs_str_cat(s, s1);
  975. if (following_check(snac, actor_id))
  976. s = html_button(s, "unfollow", L("Unfollow"));
  977. else {
  978. s = html_button(s, "follow", L("Follow"));
  979. if (follower_check(snac, actor_id))
  980. s = html_button(s, "delete", L("Delete"));
  981. }
  982. if (is_muted(snac, actor_id))
  983. s = html_button(s, "unmute", L("Unmute"));
  984. else
  985. s = html_button(s, "mute", L("MUTE"));
  986. s = xs_str_cat(s, "</form>\n");
  987. /* the post textarea */
  988. xs *s2 = xs_fmt(
  989. "<p><details><summary>%s</summary>\n"
  990. "<p><div class=\"snac-note\" id=\"%s_%s_dm\">\n"
  991. "<form method=\"post\" action=\"%s/admin/note\" "
  992. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  993. "<textarea class=\"snac-textarea\" name=\"content\" "
  994. "rows=\"4\" wrap=\"virtual\" required=\"required\"></textarea>\n"
  995. "<input type=\"hidden\" name=\"to\" value=\"%s\">\n"
  996. "<p><input type=\"file\" name=\"attach\">\n"
  997. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  998. "</form><p></div>\n"
  999. "</details><p>\n",
  1000. L("Direct Message..."),
  1001. md5, t,
  1002. snac->actor, md5,
  1003. actor_id,
  1004. L("Post")
  1005. );
  1006. s = xs_str_cat(s, s2);
  1007. s = xs_str_cat(s, "</div>\n");
  1008. s = xs_str_cat(s, "</div>\n");
  1009. }
  1010. }
  1011. return xs_str_cat(os, s);
  1012. }
  1013. d_char *html_people(snac *snac)
  1014. {
  1015. d_char *s = xs_str_new(NULL);
  1016. xs *wing = following_list(snac);
  1017. xs *wers = follower_list(snac);
  1018. s = html_user_header(snac, s, 0);
  1019. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  1020. s = html_people_list(snac, s, wers, L("People that follows you"), "e");
  1021. s = html_user_footer(s);
  1022. s = xs_str_cat(s, "</body>\n</html>\n");
  1023. return s;
  1024. }
  1025. xs_str *html_notifications(snac *snac)
  1026. {
  1027. xs_str *s = xs_str_new(NULL);
  1028. xs *n_list = notify_list(snac, 0);
  1029. xs *n_time = notify_check_time(snac, 0);
  1030. xs_list *p = n_list;
  1031. xs_str *v;
  1032. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  1033. s = html_user_header(snac, s, 0);
  1034. xs *s1 = xs_fmt(
  1035. "<form method=\"post\" action=\"%s/admin/clear-notifications\" id=\"clear\">\n"
  1036. "<input type=\"submit\" class=\"snac-btn-like\" value=\"%s\">\n"
  1037. "</form><p>", snac->actor, L("Clear all"));
  1038. s = xs_str_cat(s, s1);
  1039. while (xs_list_iter(&p, &v)) {
  1040. xs *noti = notify_get(snac, v);
  1041. if (noti == NULL)
  1042. continue;
  1043. xs *obj = NULL;
  1044. const char *type = xs_dict_get(noti, "type");
  1045. const char *utype = xs_dict_get(noti, "utype");
  1046. const char *id = xs_dict_get(noti, "objid");
  1047. if (!valid_status(object_get(id, &obj)))
  1048. continue;
  1049. if (is_hidden(snac, id))
  1050. continue;
  1051. const char *actor_id = xs_dict_get(noti, "actor");
  1052. xs *actor = NULL;
  1053. if (!valid_status(actor_get(snac, actor_id, &actor)))
  1054. continue;
  1055. xs *a_name = actor_name(actor);
  1056. if (strcmp(v, n_time) > 0) {
  1057. /* unseen notification */
  1058. if (stage == NHDR_NONE) {
  1059. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("New"));
  1060. s = xs_str_cat(s, s1);
  1061. stage = NHDR_NEW;
  1062. }
  1063. }
  1064. else {
  1065. /* already seen notification */
  1066. if (stage != NHDR_OLD) {
  1067. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("Already seen"));
  1068. s = xs_str_cat(s, s1);
  1069. stage = NHDR_OLD;
  1070. }
  1071. }
  1072. s = xs_str_cat(s, "<div>\n");
  1073. const char *label = type;
  1074. if (strcmp(type, "Create") == 0)
  1075. label = L("Mention");
  1076. else
  1077. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1078. label = L("Finished poll");
  1079. xs *s1 = xs_fmt("<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  1080. label, actor_id, a_name);
  1081. s = xs_str_cat(s, s1);
  1082. if (strcmp(type, "Follow") == 0) {
  1083. s = xs_str_cat(s, "<div class=\"snac-post\">\n");
  1084. s = html_msg_icon(snac, s, obj);
  1085. s = xs_str_cat(s, "</div>\n");
  1086. }
  1087. else {
  1088. xs *md5 = xs_md5_hex(id, strlen(id));
  1089. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  1090. }
  1091. s = xs_str_cat(s, "</div>\n");
  1092. }
  1093. if (stage == NHDR_NONE) {
  1094. xs *s1 = xs_fmt("<h2>%s</h2>\n", L("None"));
  1095. s = xs_str_cat(s, s1);
  1096. }
  1097. s = html_user_footer(s);
  1098. s = xs_str_cat(s, "</body>\n</html>\n");
  1099. /* set the check time to now */
  1100. xs *dummy = notify_check_time(snac, 1);
  1101. dummy = xs_free(dummy);
  1102. timeline_touch(snac);
  1103. return s;
  1104. }
  1105. int html_get_handler(const xs_dict *req, const char *q_path,
  1106. char **body, int *b_size, char **ctype)
  1107. {
  1108. char *accept = xs_dict_get(req, "accept");
  1109. int status = 404;
  1110. snac snac;
  1111. xs *uid = NULL;
  1112. char *p_path;
  1113. int cache = 1;
  1114. int save = 1;
  1115. char *v;
  1116. xs *l = xs_split_n(q_path, "/", 2);
  1117. v = xs_list_get(l, 1);
  1118. if (xs_is_null(v)) {
  1119. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1120. return 404;
  1121. }
  1122. uid = xs_dup(v);
  1123. /* rss extension? */
  1124. if (xs_endswith(uid, ".rss")) {
  1125. uid = xs_crop_i(uid, 0, -4);
  1126. p_path = ".rss";
  1127. }
  1128. else
  1129. p_path = xs_list_get(l, 2);
  1130. if (!uid || !user_open(&snac, uid)) {
  1131. /* invalid user */
  1132. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1133. return 404;
  1134. }
  1135. /* return the RSS if requested by Accept header */
  1136. if (accept != NULL) {
  1137. if (xs_str_in(accept, "text/xml") != -1 ||
  1138. xs_str_in(accept, "application/rss+xml") != -1)
  1139. p_path = ".rss";
  1140. }
  1141. /* check if server config variable 'disable_cache' is set */
  1142. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1143. cache = 0;
  1144. int skip = 0;
  1145. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1146. char *q_vars = xs_dict_get(req, "q_vars");
  1147. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1148. skip = atoi(v), cache = 0, save = 0;
  1149. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1150. show = atoi(v), cache = 0, save = 0;
  1151. if (p_path == NULL) { /** public timeline **/
  1152. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1153. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1154. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1155. *body = history_get(&snac, h);
  1156. *b_size = strlen(*body);
  1157. status = 200;
  1158. }
  1159. else {
  1160. xs *list = timeline_list(&snac, "public", skip, show);
  1161. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1162. *body = html_timeline(&snac, list, 1, skip, show, xs_list_len(next));
  1163. *b_size = strlen(*body);
  1164. status = 200;
  1165. if (save)
  1166. history_add(&snac, h, *body, *b_size);
  1167. }
  1168. }
  1169. else
  1170. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  1171. if (!login(&snac, req))
  1172. status = 401;
  1173. else {
  1174. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1175. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1176. *body = history_get(&snac, "timeline.html_");
  1177. *b_size = strlen(*body);
  1178. status = 200;
  1179. }
  1180. else {
  1181. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1182. xs *list = timeline_list(&snac, "private", skip, show);
  1183. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1184. *body = html_timeline(&snac, list, 0, skip, show, xs_list_len(next));
  1185. *b_size = strlen(*body);
  1186. status = 200;
  1187. if (save)
  1188. history_add(&snac, "timeline.html_", *body, *b_size);
  1189. }
  1190. }
  1191. }
  1192. else
  1193. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  1194. if (!login(&snac, req))
  1195. status = 401;
  1196. else {
  1197. *body = html_people(&snac);
  1198. *b_size = strlen(*body);
  1199. status = 200;
  1200. }
  1201. }
  1202. else
  1203. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  1204. if (!login(&snac, req))
  1205. status = 401;
  1206. else {
  1207. *body = html_notifications(&snac);
  1208. *b_size = strlen(*body);
  1209. status = 200;
  1210. }
  1211. }
  1212. else
  1213. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  1214. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1215. xs *msg = NULL;
  1216. if (valid_status(object_get(id, &msg))) {
  1217. xs *md5 = xs_md5_hex(id, strlen(id));
  1218. xs *list = xs_list_new();
  1219. list = xs_list_append(list, md5);
  1220. *body = html_timeline(&snac, list, 1, 0, 0, 0);
  1221. *b_size = strlen(*body);
  1222. status = 200;
  1223. }
  1224. }
  1225. else
  1226. if (xs_startswith(p_path, "s/")) { /** a static file **/
  1227. xs *l = xs_split(p_path, "/");
  1228. char *id = xs_list_get(l, 1);
  1229. int sz;
  1230. if (valid_status(static_get(&snac, id, body, &sz))) {
  1231. *b_size = sz;
  1232. *ctype = xs_mime_by_ext(id);
  1233. status = 200;
  1234. }
  1235. }
  1236. else
  1237. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  1238. xs *l = xs_split(p_path, "/");
  1239. char *id = xs_list_get(l, 1);
  1240. if ((*body = history_get(&snac, id)) != NULL) {
  1241. *b_size = strlen(*body);
  1242. status = 200;
  1243. }
  1244. }
  1245. else
  1246. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  1247. d_char *rss;
  1248. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1249. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  1250. char *p, *v;
  1251. /* escape tags */
  1252. bio = xs_replace_i(bio, "<", "&lt;");
  1253. bio = xs_replace_i(bio, ">", "&gt;");
  1254. rss = xs_fmt(
  1255. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  1256. "<rss version=\"0.91\">\n"
  1257. "<channel>\n"
  1258. "<title>%s (@%s@%s)</title>\n"
  1259. "<language>en</language>\n"
  1260. "<link>%s.rss</link>\n"
  1261. "<description>%s</description>\n",
  1262. xs_dict_get(snac.config, "name"),
  1263. snac.uid,
  1264. xs_dict_get(srv_config, "host"),
  1265. snac.actor,
  1266. bio
  1267. );
  1268. p = elems;
  1269. while (xs_list_iter(&p, &v)) {
  1270. xs *msg = NULL;
  1271. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1272. continue;
  1273. char *id = xs_dict_get(msg, "id");
  1274. if (!xs_startswith(id, snac.actor))
  1275. continue;
  1276. xs *content = sanitize(xs_dict_get(msg, "content"));
  1277. xs *title = xs_str_new(NULL);
  1278. int i;
  1279. /* escape tags */
  1280. content = xs_replace_i(content, "<", "&lt;");
  1281. content = xs_replace_i(content, ">", "&gt;");
  1282. for (i = 0; content[i] && content[i] != '<' && content[i] != '&' && i < 40; i++)
  1283. title = xs_append_m(title, &content[i], 1);
  1284. xs *s = xs_fmt(
  1285. "<item>\n"
  1286. "<title>%s...</title>\n"
  1287. "<link>%s</link>\n"
  1288. "<description>%s</description>\n"
  1289. "</item>\n",
  1290. title, id, content
  1291. );
  1292. rss = xs_str_cat(rss, s);
  1293. }
  1294. rss = xs_str_cat(rss, "</channel>\n</rss>\n");
  1295. *body = rss;
  1296. *b_size = strlen(rss);
  1297. *ctype = "application/rss+xml; charset=utf-8";
  1298. status = 200;
  1299. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1300. }
  1301. else
  1302. status = 404;
  1303. user_free(&snac);
  1304. if (valid_status(status) && *ctype == NULL) {
  1305. *ctype = "text/html; charset=utf-8";
  1306. }
  1307. return status;
  1308. }
  1309. int html_post_handler(const xs_dict *req, const char *q_path,
  1310. char *payload, int p_size,
  1311. char **body, int *b_size, char **ctype)
  1312. {
  1313. (void)p_size;
  1314. (void)ctype;
  1315. int status = 0;
  1316. snac snac;
  1317. char *uid, *p_path;
  1318. xs_dict *p_vars;
  1319. xs *l = xs_split_n(q_path, "/", 2);
  1320. uid = xs_list_get(l, 1);
  1321. if (!uid || !user_open(&snac, uid)) {
  1322. /* invalid user */
  1323. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1324. return 404;
  1325. }
  1326. p_path = xs_list_get(l, 2);
  1327. /* all posts must be authenticated */
  1328. if (!login(&snac, req)) {
  1329. user_free(&snac);
  1330. return 401;
  1331. }
  1332. p_vars = xs_dict_get(req, "p_vars");
  1333. #if 0
  1334. {
  1335. xs *j1 = xs_json_dumps_pp(p_vars, 4);
  1336. printf("%s\n", j1);
  1337. }
  1338. #endif
  1339. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  1340. /* post note */
  1341. xs_str *content = xs_dict_get(p_vars, "content");
  1342. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1343. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1344. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1345. xs_str *to = xs_dict_get(p_vars, "to");
  1346. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1347. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1348. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1349. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1350. xs *attach_list = xs_list_new();
  1351. /* default alt text */
  1352. if (xs_is_null(alt_text))
  1353. alt_text = "";
  1354. /* is attach_url set? */
  1355. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  1356. xs *l = xs_list_new();
  1357. l = xs_list_append(l, attach_url);
  1358. l = xs_list_append(l, alt_text);
  1359. attach_list = xs_list_append(attach_list, l);
  1360. }
  1361. /* is attach_file set? */
  1362. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  1363. char *fn = xs_list_get(attach_file, 0);
  1364. if (*fn != '\0') {
  1365. char *ext = strrchr(fn, '.');
  1366. xs *hash = xs_md5_hex(fn, strlen(fn));
  1367. xs *id = xs_fmt("%s%s", hash, ext);
  1368. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1369. int fo = xs_number_get(xs_list_get(attach_file, 1));
  1370. int fs = xs_number_get(xs_list_get(attach_file, 2));
  1371. /* store */
  1372. static_put(&snac, id, payload + fo, fs);
  1373. xs *l = xs_list_new();
  1374. l = xs_list_append(l, url);
  1375. l = xs_list_append(l, alt_text);
  1376. attach_list = xs_list_append(attach_list, l);
  1377. }
  1378. }
  1379. if (content != NULL) {
  1380. xs *msg = NULL;
  1381. xs *c_msg = NULL;
  1382. xs *content_2 = xs_replace(content, "\r", "");
  1383. xs *poll_opts = NULL;
  1384. /* is there a valid set of poll options? */
  1385. const char *v = xs_dict_get(p_vars, "poll_options");
  1386. if (!xs_is_null(v) && *v) {
  1387. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  1388. poll_opts = xs_split(v2, "\n");
  1389. }
  1390. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  1391. /* get the rest of poll configuration */
  1392. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  1393. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  1394. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  1395. int multiple = !xs_is_null(p_multiple);
  1396. msg = msg_question(&snac, content_2, attach_list,
  1397. poll_opts, multiple, end_secs);
  1398. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  1399. }
  1400. else
  1401. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  1402. if (sensitive != NULL) {
  1403. xs *t = xs_val_new(XSTYPE_TRUE);
  1404. msg = xs_dict_set(msg, "sensitive", t);
  1405. msg = xs_dict_set(msg, "summary", "...");
  1406. }
  1407. if (xs_is_null(edit_id)) {
  1408. /* new message */
  1409. c_msg = msg_create(&snac, msg);
  1410. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1411. }
  1412. else {
  1413. /* an edition of a previous message */
  1414. xs *p_msg = NULL;
  1415. if (valid_status(object_get(edit_id, &p_msg))) {
  1416. /* copy relevant fields from previous version */
  1417. char *fields[] = { "id", "context", "url", "published",
  1418. "to", "inReplyTo", NULL };
  1419. int n;
  1420. for (n = 0; fields[n]; n++) {
  1421. char *v = xs_dict_get(p_msg, fields[n]);
  1422. msg = xs_dict_set(msg, fields[n], v);
  1423. }
  1424. /* set the updated field */
  1425. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  1426. msg = xs_dict_set(msg, "updated", updated);
  1427. /* overwrite object, not updating the indexes */
  1428. object_add_ow(edit_id, msg);
  1429. /* update message */
  1430. c_msg = msg_update(&snac, msg);
  1431. }
  1432. else
  1433. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  1434. }
  1435. if (c_msg != NULL)
  1436. enqueue_message(&snac, c_msg);
  1437. history_del(&snac, "timeline.html_");
  1438. }
  1439. status = 303;
  1440. }
  1441. else
  1442. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  1443. /* action on an entry */
  1444. char *id = xs_dict_get(p_vars, "id");
  1445. char *actor = xs_dict_get(p_vars, "actor");
  1446. char *action = xs_dict_get(p_vars, "action");
  1447. if (action == NULL)
  1448. return 404;
  1449. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  1450. status = 303;
  1451. if (strcmp(action, L("Like")) == 0) { /** **/
  1452. xs *msg = msg_admiration(&snac, id, "Like");
  1453. if (msg != NULL) {
  1454. enqueue_message(&snac, msg);
  1455. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  1456. }
  1457. }
  1458. else
  1459. if (strcmp(action, L("Boost")) == 0) { /** **/
  1460. xs *msg = msg_admiration(&snac, id, "Announce");
  1461. if (msg != NULL) {
  1462. enqueue_message(&snac, msg);
  1463. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  1464. }
  1465. }
  1466. else
  1467. if (strcmp(action, L("MUTE")) == 0) { /** **/
  1468. mute(&snac, actor);
  1469. }
  1470. else
  1471. if (strcmp(action, L("Unmute")) == 0) { /** **/
  1472. unmute(&snac, actor);
  1473. }
  1474. else
  1475. if (strcmp(action, L("Hide")) == 0) { /** **/
  1476. hide(&snac, id);
  1477. }
  1478. else
  1479. if (strcmp(action, L("Follow")) == 0) { /** **/
  1480. xs *msg = msg_follow(&snac, actor);
  1481. if (msg != NULL) {
  1482. /* reload the actor from the message, in may be different */
  1483. actor = xs_dict_get(msg, "object");
  1484. following_add(&snac, actor, msg);
  1485. enqueue_output_by_actor(&snac, msg, actor, 0);
  1486. }
  1487. }
  1488. else
  1489. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  1490. /* get the following object */
  1491. xs *object = NULL;
  1492. if (valid_status(following_get(&snac, actor, &object))) {
  1493. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1494. following_del(&snac, actor);
  1495. enqueue_output_by_actor(&snac, msg, actor, 0);
  1496. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  1497. }
  1498. else
  1499. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  1500. }
  1501. else
  1502. if (strcmp(action, L("Delete")) == 0) { /** **/
  1503. char *actor_form = xs_dict_get(p_vars, "actor-form");
  1504. if (actor_form != NULL) {
  1505. /* delete follower */
  1506. if (valid_status(follower_del(&snac, actor)))
  1507. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  1508. else
  1509. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  1510. }
  1511. else {
  1512. /* delete an entry */
  1513. if (xs_startswith(id, snac.actor)) {
  1514. /* it's a post by us: generate a delete */
  1515. xs *msg = msg_delete(&snac, id);
  1516. enqueue_message(&snac, msg);
  1517. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  1518. }
  1519. timeline_del(&snac, id);
  1520. snac_log(&snac, xs_fmt("deleted entry %s", id));
  1521. }
  1522. }
  1523. else
  1524. status = 404;
  1525. /* delete the cached timeline */
  1526. if (status == 303)
  1527. history_del(&snac, "timeline.html_");
  1528. }
  1529. else
  1530. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  1531. /* change of user data */
  1532. char *v;
  1533. char *p1, *p2;
  1534. xs *byes = xs_val_new(XSTYPE_TRUE);
  1535. xs *bno = xs_val_new(XSTYPE_FALSE);
  1536. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  1537. snac.config = xs_dict_set(snac.config, "name", v);
  1538. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  1539. snac.config = xs_dict_set(snac.config, "avatar", v);
  1540. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  1541. snac.config = xs_dict_set(snac.config, "bio", v);
  1542. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  1543. strcmp(v, "on") == 0) {
  1544. snac.config = xs_dict_set(snac.config, "cw", "open");
  1545. } else { /* if the checkbox is not set, the parameter is missing */
  1546. snac.config = xs_dict_set(snac.config, "cw", "");
  1547. }
  1548. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  1549. snac.config = xs_dict_set(snac.config, "email", v);
  1550. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  1551. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  1552. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  1553. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  1554. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  1555. xs *days = xs_number_new(atof(v));
  1556. snac.config = xs_dict_set(snac.config, "purge_days", days);
  1557. }
  1558. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  1559. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", byes);
  1560. else
  1561. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", bno);
  1562. /* avatar upload */
  1563. xs_list *avatar_file = xs_dict_get(p_vars, "avatar_file");
  1564. if (!xs_is_null(avatar_file) && xs_type(avatar_file) == XSTYPE_LIST) {
  1565. char *fn = xs_list_get(avatar_file, 0);
  1566. if (*fn != '\0') {
  1567. char *ext = strrchr(fn, '.');
  1568. xs *id = xs_fmt("avatar%s", ext);
  1569. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1570. int fo = xs_number_get(xs_list_get(avatar_file, 1));
  1571. int fs = xs_number_get(xs_list_get(avatar_file, 2));
  1572. /* store */
  1573. static_put(&snac, id, payload + fo, fs);
  1574. snac.config = xs_dict_set(snac.config, "avatar", url);
  1575. }
  1576. }
  1577. /* password change? */
  1578. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  1579. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  1580. *p1 && strcmp(p1, p2) == 0) {
  1581. xs *pw = hash_password(snac.uid, p1, NULL);
  1582. snac.config = xs_dict_set(snac.config, "passwd", pw);
  1583. }
  1584. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  1585. xs *bfn = xs_fmt("%s.bak", fn);
  1586. FILE *f;
  1587. rename(fn, bfn);
  1588. if ((f = fopen(fn, "w")) != NULL) {
  1589. xs *j = xs_json_dumps_pp(snac.config, 4);
  1590. fwrite(j, strlen(j), 1, f);
  1591. fclose(f);
  1592. }
  1593. else
  1594. rename(bfn, fn);
  1595. history_del(&snac, "timeline.html_");
  1596. xs *a_msg = msg_actor(&snac);
  1597. xs *u_msg = msg_update(&snac, a_msg);
  1598. enqueue_message(&snac, u_msg);
  1599. status = 303;
  1600. }
  1601. else
  1602. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  1603. notify_clear(&snac);
  1604. timeline_touch(&snac);
  1605. status = 303;
  1606. }
  1607. else
  1608. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  1609. char *irt = xs_dict_get(p_vars, "irt");
  1610. const char *opt = xs_dict_get(p_vars, "question");
  1611. const char *actor = xs_dict_get(p_vars, "actor");
  1612. xs *ls = NULL;
  1613. /* multiple choices? */
  1614. if (xs_type(opt) == XSTYPE_LIST)
  1615. ls = xs_dup(opt);
  1616. else {
  1617. ls = xs_list_new();
  1618. ls = xs_list_append(ls, opt);
  1619. }
  1620. xs_list *p = ls;
  1621. xs_str *v;
  1622. while (xs_list_iter(&p, &v)) {
  1623. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
  1624. /* set the option */
  1625. msg = xs_dict_append(msg, "name", v);
  1626. xs *c_msg = msg_create(&snac, msg);
  1627. enqueue_message(&snac, c_msg);
  1628. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1629. }
  1630. status = 303;
  1631. }
  1632. if (status == 303) {
  1633. char *redir = xs_dict_get(p_vars, "redir");
  1634. if (xs_is_null(redir))
  1635. redir = "snac-posts";
  1636. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  1637. *b_size = strlen(*body);
  1638. }
  1639. user_free(&snac);
  1640. return status;
  1641. }