html.c 74 KB

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