html.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / 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 "xs_match.h"
  12. #include "xs_html.h"
  13. #include "snac.h"
  14. int login(snac *snac, const xs_dict *headers)
  15. /* tries a login */
  16. {
  17. int logged_in = 0;
  18. const char *auth = xs_dict_get(headers, "authorization");
  19. if (auth && xs_startswith(auth, "Basic ")) {
  20. int sz;
  21. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  22. xs *s2 = xs_base64_dec(s1, &sz);
  23. xs *l1 = xs_split_n(s2, ":", 1);
  24. if (xs_list_len(l1) == 2) {
  25. logged_in = check_password(
  26. xs_list_get(l1, 0), xs_list_get(l1, 1),
  27. xs_dict_get(snac->config, "passwd"));
  28. }
  29. }
  30. if (logged_in)
  31. lastlog_write(snac, "web");
  32. return logged_in;
  33. }
  34. xs_str *actor_name(xs_dict *actor)
  35. /* gets the actor name */
  36. {
  37. xs_list *p;
  38. char *v;
  39. xs_str *name;
  40. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  41. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  42. v = "anonymous";
  43. }
  44. }
  45. name = encode_html(v);
  46. /* replace the :shortnames: */
  47. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  48. xs *tag = NULL;
  49. if (xs_type(p) == XSTYPE_DICT) {
  50. /* not a list */
  51. tag = xs_list_new();
  52. tag = xs_list_append(tag, p);
  53. } else {
  54. /* is a list */
  55. tag = xs_dup(p);
  56. }
  57. xs_list *tags = tag;
  58. /* iterate the tags */
  59. while (xs_list_iter(&tags, &v)) {
  60. char *t = xs_dict_get(v, "type");
  61. if (t && strcmp(t, "Emoji") == 0) {
  62. char *n = xs_dict_get(v, "name");
  63. char *i = xs_dict_get(v, "icon");
  64. if (n && i) {
  65. char *u = xs_dict_get(i, "url");
  66. xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" style=\"height: 1em; vertical-align: middle;\"/>", u);
  67. name = xs_replace_i(name, n, img);
  68. }
  69. }
  70. }
  71. }
  72. return name;
  73. }
  74. xs_html *html_actor_icon(xs_dict *actor, const char *date,
  75. const char *udate, const char *url, int priv)
  76. {
  77. xs_html *actor_icon = xs_html_tag("p", NULL);
  78. xs *avatar = NULL;
  79. char *v;
  80. xs *name = actor_name(actor);
  81. /* get the avatar */
  82. if ((v = xs_dict_get(actor, "icon")) != NULL &&
  83. (v = xs_dict_get(v, "url")) != NULL) {
  84. avatar = xs_dup(v);
  85. }
  86. if (avatar == NULL)
  87. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  88. xs_html_add(actor_icon,
  89. xs_html_sctag("img",
  90. xs_html_attr("loading", "lazy"),
  91. xs_html_attr("class", "snac-avatar"),
  92. xs_html_attr("src", avatar),
  93. xs_html_attr("alt", "")),
  94. xs_html_tag("a",
  95. xs_html_attr("href", xs_dict_get(actor, "id")),
  96. xs_html_attr("class", "p-author h-card snac-author"),
  97. xs_html_raw(name))); /* name is already html-escaped */
  98. if (!xs_is_null(url)) {
  99. xs_html_add(actor_icon,
  100. xs_html_text(" "),
  101. xs_html_tag("a",
  102. xs_html_attr("href", (char *)url),
  103. xs_html_text("»")));
  104. }
  105. if (priv) {
  106. xs_html_add(actor_icon,
  107. xs_html_text(" "),
  108. xs_html_tag("span",
  109. xs_html_attr("title", "private"),
  110. xs_html_raw("&#128274;")));
  111. }
  112. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
  113. xs_html_add(actor_icon,
  114. xs_html_text(" "),
  115. xs_html_tag("span",
  116. xs_html_attr("title", "bot"),
  117. xs_html_raw("&#129302;")));
  118. }
  119. if (xs_is_null(date)) {
  120. xs_html_add(actor_icon,
  121. xs_html_raw("&nbsp;"));
  122. }
  123. else {
  124. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  125. xs *date_title = xs_dup(date);
  126. if (!xs_is_null(udate)) {
  127. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  128. date_label = xs_str_cat(date_label, " / ", sd);
  129. date_title = xs_str_cat(date_title, " / ", udate);
  130. }
  131. xs_html_add(actor_icon,
  132. xs_html_text(" "),
  133. xs_html_tag("time",
  134. xs_html_attr("class", "dt-published snac-pubdate"),
  135. xs_html_attr("title", date_title),
  136. xs_html_text(date_label)));
  137. }
  138. {
  139. char *username, *id;
  140. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  141. /* This should never be reached */
  142. username = "anonymous";
  143. }
  144. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  145. /* This should never be reached */
  146. id = "https://social.example.org/anonymous";
  147. }
  148. /* "LIKE AN ANIMAL" */
  149. xs *domain = xs_split(id, "/");
  150. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  151. xs_html_add(actor_icon,
  152. xs_html_sctag("br", NULL),
  153. xs_html_tag("a",
  154. xs_html_attr("href", xs_dict_get(actor, "id")),
  155. xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
  156. xs_html_text(user)));
  157. }
  158. return actor_icon;
  159. }
  160. xs_str *html_msg_icon(xs_str *os, const xs_dict *msg)
  161. {
  162. char *actor_id;
  163. xs *actor = NULL;
  164. if ((actor_id = xs_dict_get(msg, "attributedTo")) == NULL)
  165. actor_id = xs_dict_get(msg, "actor");
  166. if (actor_id && valid_status(actor_get(actor_id, &actor))) {
  167. char *date = NULL;
  168. char *udate = NULL;
  169. char *url = NULL;
  170. int priv = 0;
  171. const char *type = xs_dict_get(msg, "type");
  172. if (xs_match(type, "Note|Question|Page|Article"))
  173. url = xs_dict_get(msg, "id");
  174. priv = !is_msg_public(msg);
  175. date = xs_dict_get(msg, "published");
  176. udate = xs_dict_get(msg, "updated");
  177. xs_html *actor_icon = html_actor_icon(actor, date, udate, url, priv);
  178. xs *s1 = xs_html_render(actor_icon);
  179. os = xs_str_cat(os, s1);
  180. }
  181. return os;
  182. }
  183. xs_html *html_note(snac *user, char *summary,
  184. char *div_id, char *form_id,
  185. char *ta_plh, char *ta_content,
  186. char *edit_id, char *actor_id,
  187. xs_val *cw_yn, char *cw_text,
  188. xs_val *mnt_only, char *redir,
  189. char *in_reply_to, int poll)
  190. {
  191. xs *action = xs_fmt("%s/admin/note", user->actor);
  192. xs_html *form;
  193. xs_html *note = xs_html_tag("div",
  194. xs_html_tag("details",
  195. xs_html_tag("summary",
  196. xs_html_text(summary)),
  197. xs_html_tag("p", NULL),
  198. xs_html_tag("div",
  199. xs_html_attr("class", "snac-note"),
  200. xs_html_attr("id", div_id),
  201. form = xs_html_tag("form",
  202. xs_html_attr("autocomplete", "off"),
  203. xs_html_attr("method", "post"),
  204. xs_html_attr("action", action),
  205. xs_html_attr("enctype", "multipart/form-data"),
  206. xs_html_attr("id", form_id),
  207. xs_html_tag("textarea",
  208. xs_html_attr("class", "snac-textarea"),
  209. xs_html_attr("name", "content"),
  210. xs_html_attr("rows", "4"),
  211. xs_html_attr("wrap", "virtual"),
  212. xs_html_attr("required", "required"),
  213. xs_html_attr("placeholder", ta_plh),
  214. xs_html_text(ta_content)),
  215. xs_html_tag("p", NULL),
  216. xs_html_text(L("Sensitive content: ")),
  217. xs_html_sctag("input",
  218. xs_html_attr("type", "checkbox"),
  219. xs_html_attr("name", "sensitive"),
  220. xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
  221. xs_html_sctag("input",
  222. xs_html_attr("type", "text"),
  223. xs_html_attr("name", "summary"),
  224. xs_html_attr("placeholder", L("Sensitive content description")),
  225. xs_html_attr("value", cw_text))))));
  226. if (actor_id)
  227. xs_html_add(form,
  228. xs_html_sctag("input",
  229. xs_html_attr("type", "hidden"),
  230. xs_html_attr("name", "to"),
  231. xs_html_attr("value", actor_id)));
  232. else {
  233. /* no actor_id; ask for mentioned_only */
  234. xs_html_add(form,
  235. xs_html_tag("p", NULL),
  236. xs_html_text(L("Only for mentioned people")),
  237. xs_html_sctag("input",
  238. xs_html_attr("type", "checkbox"),
  239. xs_html_attr("name", "mentioned_only"),
  240. xs_html_attr(xs_type(mnt_only) == XSTYPE_TRUE ? "checked" : "", NULL)));
  241. }
  242. if (redir)
  243. xs_html_add(form,
  244. xs_html_sctag("input",
  245. xs_html_attr("type", "hidden"),
  246. xs_html_attr("name", "redir"),
  247. xs_html_attr("value", redir)));
  248. if (in_reply_to)
  249. xs_html_add(form,
  250. xs_html_sctag("input",
  251. xs_html_attr("type", "hidden"),
  252. xs_html_attr("name", "in_reply_to"),
  253. xs_html_attr("value", in_reply_to)));
  254. if (edit_id)
  255. xs_html_add(form,
  256. xs_html_sctag("input",
  257. xs_html_attr("type", "hidden"),
  258. xs_html_attr("name", "edit_id"),
  259. xs_html_attr("value", edit_id)));
  260. xs_html_add(form,
  261. xs_html_tag("details",
  262. xs_html_tag("summary",
  263. xs_html_text(L("Attachment..."))),
  264. xs_html_tag("p", NULL),
  265. xs_html_sctag("input",
  266. xs_html_attr("type", "file"),
  267. xs_html_attr("name", "attach")),
  268. xs_html_sctag("input",
  269. xs_html_attr("type", "text"),
  270. xs_html_attr("name", "alt_text"),
  271. xs_html_attr("placeholder", L("Attachment description")))));
  272. /* add poll controls */
  273. if (poll) {
  274. xs_html_add(form,
  275. xs_html_tag("details",
  276. xs_html_tag("summary",
  277. xs_html_text(L("Poll..."))),
  278. xs_html_tag("p", NULL),
  279. xs_html_tag("textarea",
  280. xs_html_attr("class", "snac-textarea"),
  281. xs_html_attr("name", "poll_options"),
  282. xs_html_attr("rows", "4"),
  283. xs_html_attr("wrap", "virtual"),
  284. xs_html_attr("placeholder", "Option 1...\nOption 2...\nOption 3...\n...")),
  285. xs_html_tag("p", NULL),
  286. xs_html_tag("select",
  287. xs_html_attr("name", "poll_multiple"),
  288. xs_html_tag("option",
  289. xs_html_attr("value", "off"),
  290. xs_html_text(L("One choice"))),
  291. xs_html_tag("option",
  292. xs_html_attr("value", "on"),
  293. xs_html_text(L("Multiple choices")))),
  294. xs_html_tag("select",
  295. xs_html_attr("name", "poll_end_secs"),
  296. xs_html_tag("option",
  297. xs_html_attr("value", "300"),
  298. xs_html_text(L("End in 5 minutes"))),
  299. xs_html_tag("option",
  300. xs_html_attr("value", "3600"),
  301. xs_html_attr("selected", NULL),
  302. xs_html_text(L("End in 1 hour"))),
  303. xs_html_tag("option",
  304. xs_html_attr("value", "86400"),
  305. xs_html_text(L("End in 1 day"))))));
  306. }
  307. xs_html_add(form,
  308. xs_html_tag("p", NULL),
  309. xs_html_sctag("input",
  310. xs_html_attr("type", "submit"),
  311. xs_html_attr("class", "button"),
  312. xs_html_attr("value", L("Post"))));
  313. return note;
  314. }
  315. xs_str *html_base_header(xs_str *s)
  316. {
  317. xs_list *p;
  318. xs_str *v;
  319. s = xs_str_cat(s, "<!DOCTYPE html>\n<html>\n<head>\n");
  320. s = xs_str_cat(s, "<meta name=\"viewport\" "
  321. "content=\"width=device-width, initial-scale=1\"/>\n");
  322. s = xs_str_cat(s, "<meta name=\"generator\" "
  323. "content=\"" USER_AGENT "\"/>\n");
  324. /* add server CSS */
  325. p = xs_dict_get(srv_config, "cssurls");
  326. while (xs_list_iter(&p, &v)) {
  327. xs *s1 = xs_fmt("<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>\n", v);
  328. s = xs_str_cat(s, s1);
  329. }
  330. return s;
  331. }
  332. xs_str *html_instance_header(xs_str *s, char *tag)
  333. {
  334. s = html_base_header(s);
  335. {
  336. FILE *f;
  337. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  338. if ((f = fopen(g_css_fn, "r")) != NULL) {
  339. xs *css = xs_readall(f);
  340. fclose(f);
  341. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  342. s = xs_str_cat(s, s1);
  343. }
  344. }
  345. const char *host = xs_dict_get(srv_config, "host");
  346. const char *title = xs_dict_get(srv_config, "title");
  347. const char *sdesc = xs_dict_get(srv_config, "short_description");
  348. const char *email = xs_dict_get(srv_config, "admin_email");
  349. const char *acct = xs_dict_get(srv_config, "admin_account");
  350. {
  351. xs *s1 = xs_fmt("<title>%s</title>\n", title && *title ? title : host);
  352. s = xs_str_cat(s, s1);
  353. }
  354. s = xs_str_cat(s, "</head>\n<body>\n");
  355. s = xs_str_cat(s, "<div class=\"snac-instance-blurb\">\n");
  356. {
  357. xs *s1 = xs_replace(snac_blurb, "%host%", host);
  358. s = xs_str_cat(s, s1);
  359. }
  360. s = xs_str_cat(s, "<dl>\n");
  361. if (sdesc && *sdesc) {
  362. xs *s1 = xs_fmt("<di><dt>%s</dt><dd>%s</dd></di>\n", L("Site description"), sdesc);
  363. s = xs_str_cat(s, s1);
  364. }
  365. if (email && *email) {
  366. xs *s1 = xs_fmt("<di><dt>%s</dt><dd>"
  367. "<a href=\"mailto:%s\">%s</a></dd></di>\n",
  368. L("Admin email"), email, email);
  369. s = xs_str_cat(s, s1);
  370. }
  371. if (acct && *acct) {
  372. xs *s1 = xs_fmt("<di><dt>%s</dt><dd>"
  373. "<a href=\"%s/%s\">@%s@%s</a></dd></di>\n",
  374. L("Admin account"), srv_baseurl, acct, acct, host);
  375. s = xs_str_cat(s, s1);
  376. }
  377. s = xs_str_cat(s, "</dl>\n");
  378. s = xs_str_cat(s, "</div>\n");
  379. {
  380. xs *l = tag ? xs_fmt(L("Search results for #%s"), tag) :
  381. xs_dup(L("Recent posts by users in this instance"));
  382. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", l);
  383. s = xs_str_cat(s, s1);
  384. }
  385. return s;
  386. }
  387. xs_str *html_user_header(snac *snac, xs_str *s, int local)
  388. /* creates the HTML header */
  389. {
  390. s = html_base_header(s);
  391. /* add the user CSS */
  392. {
  393. xs *css = NULL;
  394. int size;
  395. /* try to open the user css */
  396. if (!valid_status(static_get(snac, "style.css", &css, &size, NULL, NULL))) {
  397. /* it's not there; try to open the server-wide css */
  398. FILE *f;
  399. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  400. if ((f = fopen(g_css_fn, "r")) != NULL) {
  401. css = xs_readall(f);
  402. fclose(f);
  403. }
  404. }
  405. if (css != NULL) {
  406. xs *s1 = xs_fmt("<style>%s</style>\n", css);
  407. s = xs_str_cat(s, s1);
  408. }
  409. }
  410. {
  411. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  412. xs *es2 = encode_html(snac->uid);
  413. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  414. xs *s1 = xs_fmt("<title>%s (@%s@%s)</title>\n", es1, es2, es3);
  415. s = xs_str_cat(s, s1);
  416. }
  417. xs *avatar = xs_dup(xs_dict_get(snac->config, "avatar"));
  418. if (avatar == NULL || *avatar == '\0') {
  419. xs_free(avatar);
  420. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  421. }
  422. {
  423. xs *s_bio = xs_dup(xs_dict_get(snac->config, "bio"));
  424. int n;
  425. /* shorten the bio */
  426. for (n = 0; s_bio[n] && s_bio[n] != '&' && s_bio[n] != '.' &&
  427. s_bio[n] != '\r' && s_bio[n] != '\n' && n < 128; n++);
  428. s_bio[n] = '\0';
  429. xs *s_avatar = xs_dup(avatar);
  430. /* don't inline an empty avatar: create a real link */
  431. if (xs_startswith(s_avatar, "data:")) {
  432. xs_free(s_avatar);
  433. s_avatar = xs_fmt("%s/susie.png", srv_baseurl);
  434. }
  435. /* og properties */
  436. xs *es1 = encode_html(xs_dict_get(srv_config, "host"));
  437. xs *es2 = encode_html(xs_dict_get(snac->config, "name"));
  438. xs *es3 = encode_html(snac->uid);
  439. xs *es4 = encode_html(xs_dict_get(srv_config, "host"));
  440. xs *es5 = encode_html(s_bio);
  441. xs *es6 = encode_html(s_avatar);
  442. xs *s1 = xs_fmt(
  443. "<meta property=\"og:site_name\" content=\"%s\"/>\n"
  444. "<meta property=\"og:title\" content=\"%s (@%s@%s)\"/>\n"
  445. "<meta property=\"og:description\" content=\"%s\"/>\n"
  446. "<meta property=\"og:image\" content=\"%s\"/>\n"
  447. "<meta property=\"og:image:width\" content=\"300\"/>\n"
  448. "<meta property=\"og:image:height\" content=\"300\"/>\n",
  449. es1, es2, es3, es4, es5, es6);
  450. s = xs_str_cat(s, s1);
  451. }
  452. {
  453. xs *s1 = xs_fmt("<link rel=\"alternate\" type=\"application/rss+xml\" "
  454. "title=\"RSS\" href=\"%s.rss\" />\n", snac->actor); /* snac->actor is likely need to be URLEncoded. */
  455. s = xs_str_cat(s, s1);
  456. }
  457. s = xs_str_cat(s, "</head>\n<body>\n");
  458. /* top nav */
  459. s = xs_str_cat(s, "<nav class=\"snac-top-nav\">");
  460. {
  461. xs *s1;
  462. s1 = xs_fmt("<img src=\"%s\" class=\"snac-avatar\" alt=\"\"/>&nbsp;", avatar);
  463. s = xs_str_cat(s, s1);
  464. }
  465. {
  466. xs *s1;
  467. if (local)
  468. s1 = xs_fmt(
  469. "<a href=\"%s.rss\">%s</a> - "
  470. "<a href=\"%s/admin\" rel=\"nofollow\">%s</a></nav>\n",
  471. snac->actor, L("RSS"),
  472. snac->actor, L("private"));
  473. else {
  474. xs *n_list = notify_list(snac, 1);
  475. int n_len = xs_list_len(n_list);
  476. xs *n_str = NULL;
  477. /* show the number of new notifications, if there are any */
  478. if (n_len)
  479. n_str = xs_fmt("<sup style=\"background-color: red; "
  480. "color: white;\"> %d </sup> ", n_len);
  481. else
  482. n_str = xs_str_new("");
  483. s1 = xs_fmt(
  484. "<a href=\"%s\">%s</a> - "
  485. "<a href=\"%s/admin\">%s</a> - "
  486. "<a href=\"%s/notifications\">%s</a>%s - "
  487. "<a href=\"%s/people\">%s</a></nav>\n",
  488. snac->actor, L("public"),
  489. snac->actor, L("private"),
  490. snac->actor, L("notifications"), n_str,
  491. snac->actor, L("people"));
  492. }
  493. s = xs_str_cat(s, s1);
  494. }
  495. /* user info */
  496. {
  497. s = xs_str_cat(s, "<div class=\"h-card snac-top-user\">\n");
  498. if (local) {
  499. const char *header = xs_dict_get(snac->config, "header");
  500. if (header && *header) {
  501. xs *h = encode_html(header);
  502. xs *s1 = xs_fmt("<div class=\"snac-top-user-banner\" style=\"clear: both\">"
  503. "<br><img src=\"%s\"/></div>\n", h);
  504. s = xs_str_cat(s, s1);
  505. }
  506. }
  507. const char *_tmpl =
  508. "<p class=\"p-name snac-top-user-name\">%s</p>\n"
  509. "<p class=\"snac-top-user-id\">@%s@%s</p>\n";
  510. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  511. xs *es2 = encode_html(xs_dict_get(snac->config, "uid"));
  512. xs *es3 = encode_html(xs_dict_get(srv_config, "host"));
  513. xs *s1 = xs_fmt(_tmpl, es1, es2, es3);
  514. s = xs_str_cat(s, s1);
  515. if (local) {
  516. xs *es1 = encode_html(xs_dict_get(snac->config, "bio"));
  517. xs *bio1 = not_really_markdown(es1, NULL);
  518. xs *tags = xs_list_new();
  519. xs *bio2 = process_tags(snac, bio1, &tags);
  520. xs *s1 = xs_fmt("<div class=\"p-note snac-top-user-bio\">%s</div>\n", bio2);
  521. s = xs_str_cat(s, s1);
  522. xs_dict *metadata = xs_dict_get(snac->config, "metadata");
  523. if (xs_type(metadata) == XSTYPE_DICT) {
  524. xs_str *k;
  525. xs_str *v;
  526. s = xs_str_cat(s, "<br><div class=\"snac-metadata\">\n");
  527. while (xs_dict_iter(&metadata, &k, &v)) {
  528. xs *k2 = encode_html(k);
  529. xs *v2 = encode_html(v);
  530. xs *v3 = NULL;
  531. if (xs_startswith(v, "https:/" "/"))
  532. v3 = xs_fmt("<a href=\"%s\">%s</a>", v2, v2);
  533. else
  534. v3 = xs_dup(v2);
  535. xs *s1 = xs_fmt("<span class=\"snac-property-name\">%s</span>:<br>"
  536. "<span class=\"snac-property-value\">%s</span><br>\n", k2, v3);
  537. s = xs_str_cat(s, s1);
  538. }
  539. s = xs_str_cat(s, "</div>\n");
  540. }
  541. }
  542. s = xs_str_cat(s, "</div>\n");
  543. }
  544. return s;
  545. }
  546. xs_str *html_top_controls(snac *snac, xs_str *s)
  547. /* generates the top controls */
  548. {
  549. char *_tmpl =
  550. "<div class=\"snac-top-controls\">\n"
  551. "<div class=\"snac-note\">\n"
  552. "<details><summary>%s</summary>\n"
  553. "<form autocomplete=\"off\" method=\"post\" "
  554. "action=\"%s/admin/note\" enctype=\"multipart/form-data\">\n"
  555. "<textarea class=\"snac-textarea\" name=\"content\" "
  556. "rows=\"8\" wrap=\"virtual\" required=\"required\" placeholder=\"What's on your mind?\"></textarea>\n"
  557. "<input type=\"hidden\" name=\"in_reply_to\" value=\"\">\n"
  558. "<p>%s: <input type=\"checkbox\" name=\"sensitive\"> "
  559. "<input type=\"text\" name=\"summary\" placeholder=\"%s\">\n"
  560. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  561. "<details><summary>%s</summary>\n" /** attach **/
  562. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  563. "<p>%s: <input type=\"text\" name=\"alt_text\" placeholder=\"[Optional]\">\n"
  564. "</details>\n"
  565. "<p>"
  566. "<details><summary>%s</summary>\n" /** poll **/
  567. "<p>%s:<br>\n"
  568. "<textarea class=\"snac-textarea\" name=\"poll_options\" "
  569. "rows=\"6\" wrap=\"virtual\" placeholder=\"Option 1...\nOption 2...\nOption 3...\n...\"></textarea>\n"
  570. "<p><select name=\"poll_multiple\">\n"
  571. "<option value=\"off\">%s</option>\n"
  572. "<option value=\"on\">%s</option>\n"
  573. "</select>\n"
  574. "<select name=\"poll_end_secs\" id=\"poll_end_secs\">\n"
  575. "<option value=\"300\">%s</option>\n"
  576. "<option value=\"3600\" selected>%s</option>\n"
  577. "<option value=\"86400\">%s</option>\n"
  578. "</select>\n"
  579. "</details>\n"
  580. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  581. "</form><p>\n"
  582. "</details>\n"
  583. "</div>\n"
  584. "<div class=\"snac-top-controls-more\">\n"
  585. "<details><summary>%s</summary>\n"
  586. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n" /** follow **/
  587. "<input type=\"text\" name=\"actor\" required=\"required\" placeholder=\"bob@example.com\">\n"
  588. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  589. "</form><p>\n"
  590. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n" /** boost **/
  591. "<input type=\"text\" name=\"id\" required=\"required\" placeholder=\"https://fedi.example.com/bob/...\">\n"
  592. "<input type=\"submit\" name=\"action\" value=\"%s\"> %s\n"
  593. "</form><p>\n"
  594. "</details>\n"
  595. "<details><summary>%s</summary>\n"
  596. "<div class=\"snac-user-setup\">\n" /** user setup **/
  597. "<form autocomplete=\"off\" method=\"post\" "
  598. "action=\"%s/admin/user-setup\" enctype=\"multipart/form-data\">\n"
  599. "<p>%s:<br>\n"
  600. "<input type=\"text\" name=\"name\" value=\"%s\" placeholder=\"Your name.\"></p>\n"
  601. "<p>%s: <input type=\"file\" name=\"avatar_file\"></p>\n"
  602. "<p>%s: <input type=\"file\" name=\"header_file\"></p>\n"
  603. "<p>%s:<br>\n"
  604. "<textarea name=\"bio\" cols=\"40\" rows=\"4\" placeholder=\"Write about yourself here...\">%s</textarea></p>\n"
  605. "<p><input type=\"checkbox\" name=\"cw\" id=\"cw\" %s>\n"
  606. "<label for=\"cw\">%s</label></p>\n"
  607. "<p>%s:<br>\n"
  608. "<input type=\"text\" name=\"email\" value=\"%s\" placeholder=\"bob@example.com\"></p>\n"
  609. "<p>%s:<br>\n"
  610. "<input type=\"text\" name=\"telegram_bot\" placeholder=\"Bot API key\" value=\"%s\"> "
  611. "<input type=\"text\" name=\"telegram_chat_id\" placeholder=\"Chat id\" value=\"%s\"></p>\n"
  612. "<p>%s:<br>\n"
  613. "<input type=\"number\" name=\"purge_days\" value=\"%s\"></p>\n"
  614. "<p><input type=\"checkbox\" name=\"drop_dm_from_unknown\" id=\"drop_dm_from_unknown\" %s>\n"
  615. "<label for=\"drop_dm_from_unknown\">%s</label></p>\n"
  616. "<p><input type=\"checkbox\" name=\"bot\" id=\"bot\" %s>\n"
  617. "<label for=\"bot\">%s</label></p>\n"
  618. "<p><input type=\"checkbox\" name=\"private\" id=\"private\" %s>\n"
  619. "<label for=\"private\">%s</label></p>\n"
  620. "<p>%s:<br>\n"
  621. "<textarea name=\"metadata\" cols=\"40\" rows=\"4\" "
  622. "placeholder=\"Blog=https:/" "/example.com/my-blog\nGPG Key=1FA54\n...\">"
  623. "%s</textarea></p>\n"
  624. "<p>%s:<br>\n"
  625. "<input type=\"password\" name=\"passwd1\" value=\"\"></p>\n"
  626. "<p>%s:<br>\n"
  627. "<input type=\"password\" name=\"passwd2\" value=\"\"></p>\n"
  628. "<input type=\"submit\" class=\"button\" value=\"%s\">\n"
  629. "</form>\n"
  630. "</div>\n"
  631. "</details>\n"
  632. "</div>\n"
  633. "</div>\n";
  634. const char *email = "[disabled by admin]";
  635. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  636. email = xs_dict_get(snac->config_o, "email");
  637. if (xs_is_null(email)) {
  638. email = xs_dict_get(snac->config, "email");
  639. if (xs_is_null(email))
  640. email = "";
  641. }
  642. }
  643. char *cw = xs_dict_get(snac->config, "cw");
  644. if (xs_is_null(cw))
  645. cw = "";
  646. char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
  647. if (xs_is_null(telegram_bot))
  648. telegram_bot = "";
  649. char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  650. if (xs_is_null(telegram_chat_id))
  651. telegram_chat_id = "";
  652. const char *purge_days = xs_dict_get(snac->config, "purge_days");
  653. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  654. purge_days = xs_number_str(purge_days);
  655. else
  656. purge_days = "0";
  657. const char *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
  658. const char *bot = xs_dict_get(snac->config, "bot");
  659. const char *a_private = xs_dict_get(snac->config, "private");
  660. xs *es1 = encode_html(xs_dict_get(snac->config, "name"));
  661. xs *es2 = encode_html(xs_dict_get(snac->config, "bio"));
  662. xs *es3 = encode_html(email);
  663. xs *es4 = encode_html(telegram_bot);
  664. xs *es5 = encode_html(telegram_chat_id);
  665. xs *es6 = encode_html(purge_days);
  666. xs *metadata = xs_str_new(NULL);
  667. xs_dict *md = xs_dict_get(snac->config, "metadata");
  668. xs_str *k;
  669. xs_str *v;
  670. while (xs_dict_iter(&md, &k, &v)) {
  671. xs *kp = xs_fmt("%s=%s", k, v);
  672. if (*metadata)
  673. metadata = xs_str_cat(metadata, "\n");
  674. metadata = xs_str_cat(metadata, kp);
  675. }
  676. xs *s1 = xs_fmt(_tmpl,
  677. L("New Post..."),
  678. snac->actor,
  679. L("Sensitive content"),
  680. L("Sensitive content description"),
  681. L("Only for mentioned people"),
  682. L("Attachment..."),
  683. L("File"),
  684. L("File description"),
  685. L("Poll..."),
  686. L("Poll options (one per line, up to 8)"),
  687. L("One choice"),
  688. L("Multiple choices"),
  689. L("End in 5 minutes"),
  690. L("End in 1 hour"),
  691. L("End in 1 day"),
  692. L("Post"),
  693. L("Operations..."),
  694. snac->actor,
  695. L("Follow"), L("(by URL or user@host)"),
  696. snac->actor,
  697. L("Boost"), L("(by URL)"),
  698. L("User Settings..."),
  699. snac->actor,
  700. L("Display name"),
  701. es1,
  702. L("Avatar"),
  703. L("Header image (banner)"),
  704. L("Bio"),
  705. es2,
  706. strcmp(cw, "open") == 0 ? "checked" : "",
  707. L("Always show sensitive content"),
  708. L("Email address for notifications"),
  709. es3,
  710. L("Telegram notifications (bot key and chat id)"),
  711. es4,
  712. es5,
  713. L("Maximum days to keep posts (0: server settings)"),
  714. es6,
  715. xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "",
  716. L("Drop direct messages from people you don't follow"),
  717. xs_type(bot) == XSTYPE_TRUE ? "checked" : "",
  718. L("This account is a bot"),
  719. xs_type(a_private) == XSTYPE_TRUE ? "checked" : "",
  720. L("This account is private (posts are not shown through the web)"),
  721. L("Profile metadata (key=value pairs in each line)"),
  722. metadata,
  723. L("New password"),
  724. L("Repeat new password"),
  725. L("Update user info")
  726. );
  727. s = xs_str_cat(s, s1);
  728. return s;
  729. }
  730. static xs_str *html_button(xs_str *s, const char *clss, const char *label, const char *hint)
  731. {
  732. xs *s1 = xs_fmt(
  733. "<input type=\"submit\" name=\"action\" "
  734. "class=\"snac-btn-%s\" value=\"%s\" title=\"%s\">\n",
  735. clss, label, hint);
  736. return xs_str_cat(s, s1);
  737. }
  738. static xs_html *html_button_2(char *clss, char *label, char *hint)
  739. {
  740. xs *c = xs_fmt("snac-btn-%s", clss);
  741. return xs_html_sctag("input",
  742. xs_html_attr("type", "submit"),
  743. xs_html_attr("name", "action"),
  744. xs_html_attr("class", c),
  745. xs_html_attr("value", label),
  746. xs_html_attr("title", hint));
  747. }
  748. xs_str *build_mentions(snac *snac, const xs_dict *msg)
  749. /* returns a string with the mentions in msg */
  750. {
  751. xs_str *s = xs_str_new(NULL);
  752. char *list = xs_dict_get(msg, "tag");
  753. char *v;
  754. while (xs_list_iter(&list, &v)) {
  755. char *type = xs_dict_get(v, "type");
  756. char *href = xs_dict_get(v, "href");
  757. char *name = xs_dict_get(v, "name");
  758. if (type && strcmp(type, "Mention") == 0 &&
  759. href && strcmp(href, snac->actor) != 0 && name) {
  760. xs *s1 = NULL;
  761. if (name[0] != '@') {
  762. s1 = xs_fmt("@%s", name);
  763. name = s1;
  764. }
  765. xs *l = xs_split(name, "@");
  766. /* is it a name without a host? */
  767. if (xs_list_len(l) < 3) {
  768. /* split the href and pick the host name LIKE AN ANIMAL */
  769. /* would be better to query the webfinger but *won't do that* here */
  770. xs *l2 = xs_split(href, "/");
  771. if (xs_list_len(l2) >= 3) {
  772. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  773. s = xs_str_cat(s, s1);
  774. }
  775. }
  776. else {
  777. s = xs_str_cat(s, name);
  778. s = xs_str_cat(s, " ");
  779. }
  780. }
  781. }
  782. if (*s) {
  783. xs *s1 = s;
  784. s = xs_fmt("\n\n\nCC: %s", s1);
  785. }
  786. return s;
  787. }
  788. xs_str *html_entry_controls(snac *snac, xs_str *os, const xs_dict *msg, const char *md5)
  789. {
  790. const char *id = xs_dict_get(msg, "id");
  791. const char *actor = xs_dict_get(msg, "attributedTo");
  792. const char *group = xs_dict_get(msg, "audience");
  793. xs *likes = object_likes(id);
  794. xs *boosts = object_announces(id);
  795. xs *s = xs_str_new(NULL);
  796. s = xs_str_cat(s, "<div class=\"snac-controls\">\n");
  797. {
  798. xs *s1 = xs_fmt(
  799. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/action\">\n"
  800. "<input type=\"hidden\" name=\"id\" value=\"%s\">\n"
  801. "<input type=\"hidden\" name=\"actor\" value=\"%s\">\n"
  802. "<input type=\"hidden\" name=\"group\" value=\"%s\">\n"
  803. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  804. "\n",
  805. snac->actor, id, actor, group ? group : "", md5
  806. );
  807. s = xs_str_cat(s, s1);
  808. }
  809. if (!xs_startswith(id, snac->actor)) {
  810. if (xs_list_in(likes, snac->md5) == -1) {
  811. /* not already liked; add button */
  812. s = html_button(s, "like", L("Like"), L("Say you like this post"));
  813. }
  814. }
  815. else {
  816. if (is_pinned(snac, id))
  817. s = html_button(s, "unpin", L("Unpin"), L("Unpin this post from your timeline"));
  818. else
  819. s = html_button(s, "pin", L("Pin"), L("Pin this post to the top of your timeline"));
  820. }
  821. if (is_msg_public(msg)) {
  822. if (strcmp(actor, snac->actor) == 0 || xs_list_in(boosts, snac->md5) == -1) {
  823. /* not already boosted or us; add button */
  824. s = html_button(s, "boost", L("Boost"), L("Announce this post to your followers"));
  825. }
  826. }
  827. if (strcmp(actor, snac->actor) != 0) {
  828. /* controls for other actors than this one */
  829. if (following_check(snac, actor)) {
  830. s = html_button(s, "unfollow", L("Unfollow"), L("Stop following this user's activity"));
  831. }
  832. else {
  833. s = html_button(s, "follow", L("Follow"), L("Start following this user's activity"));
  834. }
  835. if (!xs_is_null(group)) {
  836. if (following_check(snac, group)) {
  837. s = html_button(s, "unfollow", L("Unfollow Group"),
  838. L("Stop following this group or channel"));
  839. }
  840. else {
  841. s = html_button(s, "follow", L("Follow Group"),
  842. L("Start following this group or channel"));
  843. }
  844. }
  845. s = html_button(s, "mute", L("MUTE"),
  846. L("Block any activity from this user forever"));
  847. }
  848. s = html_button(s, "delete", L("Delete"), L("Delete this post"));
  849. s = html_button(s, "hide", L("Hide"), L("Hide this post and its children"));
  850. s = xs_str_cat(s, "</form>\n");
  851. const char *prev_src1 = xs_dict_get(msg, "sourceContent");
  852. if (!xs_is_null(prev_src1) && strcmp(actor, snac->actor) == 0) { /** edit **/
  853. xs *prev_src = encode_html(prev_src1);
  854. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  855. const char *summary = xs_dict_get(msg, "summary");
  856. /* post can be edited */
  857. xs *s1 = xs_fmt(
  858. "<p><details><summary>%s</summary>\n"
  859. "<p><div class=\"snac-note\" id=\"%s_edit\">\n"
  860. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/note\" "
  861. "enctype=\"multipart/form-data\" id=\"%s_edit_form\">\n"
  862. "<textarea class=\"snac-textarea\" name=\"content\" "
  863. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  864. "<input type=\"hidden\" name=\"edit_id\" value=\"%s\">\n"
  865. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  866. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  867. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  868. "<details><summary>%s</summary>\n"
  869. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  870. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  871. "</details>\n"
  872. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  873. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  874. "</form><p></div>\n"
  875. "</details><p>"
  876. "\n",
  877. L("Edit..."),
  878. md5,
  879. snac->actor, md5,
  880. prev_src,
  881. id,
  882. L("Sensitive content"),
  883. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  884. L("Sensitive content description"),
  885. xs_is_null(summary) ? "" : summary,
  886. L("Only for mentioned people"),
  887. L("Attach..."),
  888. L("File"),
  889. L("File description"),
  890. md5,
  891. L("Post")
  892. );
  893. s = xs_str_cat(s, s1);
  894. }
  895. { /** reply **/
  896. /* the post textarea */
  897. xs *ct = build_mentions(snac, msg);
  898. const xs_val *sensitive = xs_dict_get(msg, "sensitive");
  899. const char *summary = xs_dict_get(msg, "summary");
  900. xs *s1 = xs_fmt(
  901. "<p><details><summary>%s</summary>\n"
  902. "<p><div class=\"snac-note\" id=\"%s_reply\">\n"
  903. "<form autocomplete=\"off\" method=\"post\" action=\"%s/admin/note\" "
  904. "enctype=\"multipart/form-data\" id=\"%s_reply_form\">\n"
  905. "<textarea class=\"snac-textarea\" name=\"content\" "
  906. "rows=\"4\" wrap=\"virtual\" required=\"required\">%s</textarea>\n"
  907. "<input type=\"hidden\" name=\"in_reply_to\" value=\"%s\">\n"
  908. "<p>%s: <input type=\"checkbox\" name=\"sensitive\" %s> "
  909. "<input type=\"text\" name=\"summary\" placeholder=\"%s\" value=\"%s\">\n"
  910. "<p>%s: <input type=\"checkbox\" name=\"mentioned_only\">\n"
  911. "<details><summary>%s</summary>\n"
  912. "<p>%s: <input type=\"file\" name=\"attach\">\n"
  913. "<p>%s: <input type=\"text\" name=\"alt_text\">\n"
  914. "</details>\n"
  915. "<input type=\"hidden\" name=\"redir\" value=\"%s_entry\">\n"
  916. "<p><input type=\"submit\" class=\"button\" value=\"%s\">\n"
  917. "</form><p></div>\n"
  918. "</details><p>"
  919. "\n",
  920. L("Reply..."),
  921. md5,
  922. snac->actor, md5,
  923. ct,
  924. id,
  925. L("Sensitive content"),
  926. xs_type(sensitive) == XSTYPE_TRUE ? "checked" : "",
  927. L("Sensitive content description"),
  928. xs_is_null(summary) ? "" : summary,
  929. L("Only for mentioned people"),
  930. L("Attach..."),
  931. L("File"),
  932. L("File description"),
  933. md5,
  934. L("Post")
  935. );
  936. s = xs_str_cat(s, s1);
  937. }
  938. s = xs_str_cat(s, "</div>\n");
  939. return xs_str_cat(os, s);
  940. }
  941. xs_str *html_entry(snac *user, xs_str *os, const xs_dict *msg, int local,
  942. int level, const char *md5, int hide_children)
  943. {
  944. char *id = xs_dict_get(msg, "id");
  945. char *type = xs_dict_get(msg, "type");
  946. char *actor;
  947. int sensitive = 0;
  948. char *v;
  949. xs *boosts = NULL;
  950. /* do not show non-public messages in the public timeline */
  951. if ((local || !user) && !is_msg_public(msg))
  952. return os;
  953. /* hidden? do nothing more for this conversation */
  954. if (user && is_hidden(user, id))
  955. return os;
  956. /* avoid too deep nesting, as it may be a loop */
  957. if (level >= 256)
  958. return os;
  959. xs *s = xs_str_new("<div>\n");
  960. {
  961. xs *s1 = xs_fmt("<a name=\"%s_entry\"></a>\n", md5);
  962. s = xs_str_cat(s, s1);
  963. }
  964. if (strcmp(type, "Follow") == 0) {
  965. s = xs_str_cat(s, "<div class=\"snac-post\">\n<div class=\"snac-post-header\">\n");
  966. xs *s1 = xs_fmt("<div class=\"snac-origin\">%s</div>\n", L("follows you"));
  967. s = xs_str_cat(s, s1);
  968. s = html_msg_icon(s, msg);
  969. s = xs_str_cat(s, "</div>\n</div>\n");
  970. return xs_str_cat(os, s);
  971. }
  972. else
  973. if (!xs_match(type, "Note|Question|Page|Article")) {
  974. /* skip oddities */
  975. return os;
  976. }
  977. /* ignore notes with "name", as they are votes to Questions */
  978. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  979. return os;
  980. /* bring the main actor */
  981. if ((actor = xs_dict_get(msg, "attributedTo")) == NULL)
  982. return os;
  983. /* ignore muted morons immediately */
  984. if (user && is_muted(user, actor))
  985. return os;
  986. if ((user == NULL || strcmp(actor, user->actor) != 0)
  987. && !valid_status(actor_get(actor, NULL)))
  988. return os;
  989. if (level == 0)
  990. s = xs_str_cat(s, "<div class=\"snac-post\">\n"); /** **/
  991. else
  992. s = xs_str_cat(s, "<div class=\"snac-child\">\n"); /** **/
  993. s = xs_str_cat(s, "<div class=\"snac-post-header\">\n<div class=\"snac-score\">"); /** **/
  994. if (user && is_pinned(user, id)) {
  995. /* add a pin emoji */
  996. xs *f = xs_fmt("<span title=\"%s\"> &#128204; </span>", L("Pinned"));
  997. s = xs_str_cat(s, f);
  998. }
  999. if (strcmp(type, "Question") == 0) {
  1000. /* add the ballot box emoji */
  1001. xs *f = xs_fmt("<span title=\"%s\"> &#128499; </span>", L("Poll"));
  1002. s = xs_str_cat(s, f);
  1003. if (user && was_question_voted(user, id)) {
  1004. /* add a check to show this poll was voted */
  1005. xs *f2 = xs_fmt("<span title=\"%s\"> &#10003; </span>", L("Voted"));
  1006. s = xs_str_cat(s, f2);
  1007. }
  1008. }
  1009. /* if it's a user from this same instance, add the score */
  1010. if (xs_startswith(id, srv_baseurl)) {
  1011. int n_likes = object_likes_len(id);
  1012. int n_boosts = object_announces_len(id);
  1013. /* alternate emojis: %d &#128077; %d &#128257; */
  1014. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  1015. s = xs_str_cat(s, s1);
  1016. }
  1017. s = xs_str_cat(s, "</div>\n");
  1018. if (boosts == NULL)
  1019. boosts = object_announces(id);
  1020. if (xs_list_len(boosts)) {
  1021. /* if somebody boosted this, show as origin */
  1022. char *p = xs_list_get(boosts, -1);
  1023. xs *actor_r = NULL;
  1024. if (user && xs_list_in(boosts, user->md5) != -1) {
  1025. /* we boosted this */
  1026. xs *es1 = encode_html(xs_dict_get(user->config, "name"));
  1027. xs *s1 = xs_fmt(
  1028. "<div class=\"snac-origin\">"
  1029. "<a href=\"%s\">%s</a> %s</div>",
  1030. user->actor, es1, L("boosted")
  1031. );
  1032. s = xs_str_cat(s, s1);
  1033. }
  1034. else
  1035. if (valid_status(object_get_by_md5(p, &actor_r))) {
  1036. xs *name = actor_name(actor_r);
  1037. if (!xs_is_null(name)) {
  1038. xs *s1 = xs_fmt(
  1039. "<div class=\"snac-origin\">"
  1040. "<a href=\"%s\">%s</a> %s</div>\n",
  1041. xs_dict_get(actor_r, "id"),
  1042. name,
  1043. L("boosted")
  1044. );
  1045. s = xs_str_cat(s, s1);
  1046. }
  1047. }
  1048. }
  1049. else
  1050. if (strcmp(type, "Note") == 0) {
  1051. if (level == 0) {
  1052. /* is the parent not here? */
  1053. char *parent = xs_dict_get(msg, "inReplyTo");
  1054. if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
  1055. xs *s1 = xs_fmt(
  1056. "<div class=\"snac-origin\">%s "
  1057. "<a href=\"%s\">»</a></div>\n",
  1058. L("in reply to"), parent
  1059. );
  1060. s = xs_str_cat(s, s1);
  1061. }
  1062. }
  1063. }
  1064. s = html_msg_icon(s, msg);
  1065. /* add the content */
  1066. s = xs_str_cat(s, "</div>\n<div class=\"e-content snac-content\">\n"); /** **/
  1067. if (!xs_is_null(v = xs_dict_get(msg, "name"))) {
  1068. xs *es1 = encode_html(v);
  1069. xs *s1 = xs_fmt("<h3 class=\"snac-entry-title\">%s</h3>\n", es1);
  1070. s = xs_str_cat(s, s1);
  1071. }
  1072. /* is it sensitive? */
  1073. if (user && xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  1074. if (xs_is_null(v = xs_dict_get(msg, "summary")) || *v == '\0')
  1075. v = "...";
  1076. /* only show it when not in the public timeline and the config setting is "open" */
  1077. char *cw = xs_dict_get(user->config, "cw");
  1078. if (xs_is_null(cw) || local)
  1079. cw = "";
  1080. xs *es1 = encode_html(v);
  1081. xs *s1 = xs_fmt("<details %s><summary>%s [%s]</summary>\n", cw, es1, L("SENSITIVE CONTENT"));
  1082. s = xs_str_cat(s, s1);
  1083. sensitive = 1;
  1084. }
  1085. #if 0
  1086. {
  1087. xs *md5 = xs_md5_hex(id, strlen(id));
  1088. xs *s1 = xs_fmt("<p><code>%s</code></p>\n", md5);
  1089. s = xs_str_cat(s, s1);
  1090. }
  1091. #endif
  1092. {
  1093. const char *content = xs_dict_get(msg, "content");
  1094. xs *c = sanitize(xs_is_null(content) ? "" : content);
  1095. char *p, *v;
  1096. /* do some tweaks to the content */
  1097. c = xs_replace_i(c, "\r", "");
  1098. while (xs_endswith(c, "<br><br>"))
  1099. c = xs_crop_i(c, 0, -4);
  1100. c = xs_replace_i(c, "<br><br>", "<p>");
  1101. if (!xs_startswith(c, "<p>")) {
  1102. xs *s1 = c;
  1103. c = xs_fmt("<p>%s</p>", s1);
  1104. }
  1105. /* replace the :shortnames: */
  1106. if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
  1107. xs *tag = NULL;
  1108. if (xs_type(p) == XSTYPE_DICT) {
  1109. /* not a list */
  1110. tag = xs_list_new();
  1111. tag = xs_list_append(tag, p);
  1112. }
  1113. else
  1114. if (xs_type(p) == XSTYPE_LIST)
  1115. tag = xs_dup(p);
  1116. else
  1117. tag = xs_list_new();
  1118. xs_list *tags = tag;
  1119. /* iterate the tags */
  1120. while (xs_list_iter(&tags, &v)) {
  1121. char *t = xs_dict_get(v, "type");
  1122. if (t && strcmp(t, "Emoji") == 0) {
  1123. char *n = xs_dict_get(v, "name");
  1124. char *i = xs_dict_get(v, "icon");
  1125. if (n && i) {
  1126. char *u = xs_dict_get(i, "url");
  1127. xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" "
  1128. "style=\"height: 2em; vertical-align: middle;\" "
  1129. "title=\"%s\"/>", u, n);
  1130. c = xs_replace_i(c, n, img);
  1131. }
  1132. }
  1133. }
  1134. }
  1135. if (strcmp(type, "Question") == 0) { /** question content **/
  1136. xs_list *oo = xs_dict_get(msg, "oneOf");
  1137. xs_list *ao = xs_dict_get(msg, "anyOf");
  1138. xs_list *p;
  1139. xs_dict *v;
  1140. int closed = 0;
  1141. if (xs_dict_get(msg, "closed"))
  1142. closed = 2;
  1143. else
  1144. if (user && xs_startswith(id, user->actor))
  1145. closed = 1; /* we questioned; closed for us */
  1146. else
  1147. if (user && was_question_voted(user, id))
  1148. closed = 1; /* we already voted; closed for us */
  1149. /* get the appropriate list of options */
  1150. p = oo != NULL ? oo : ao;
  1151. if (closed || user == NULL) {
  1152. /* closed poll */
  1153. c = xs_str_cat(c, "<table class=\"snac-poll-result\">\n");
  1154. while (xs_list_iter(&p, &v)) {
  1155. const char *name = xs_dict_get(v, "name");
  1156. const xs_dict *replies = xs_dict_get(v, "replies");
  1157. if (name && replies) {
  1158. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  1159. xs *es1 = encode_html(name);
  1160. xs *l = xs_fmt("<tr><td>%s:</td><td>%d</td></tr>\n", es1, nr);
  1161. c = xs_str_cat(c, l);
  1162. }
  1163. }
  1164. c = xs_str_cat(c, "</table>\n");
  1165. }
  1166. else {
  1167. /* poll still active */
  1168. xs *s1 = xs_fmt("<div class=\"snac-poll-form\">\n"
  1169. "<form autocomplete=\"off\" "
  1170. "method=\"post\" action=\"%s/admin/vote\">\n"
  1171. "<input type=\"hidden\" name=\"actor\" value= \"%s\">\n"
  1172. "<input type=\"hidden\" name=\"irt\" value=\"%s\">\n",
  1173. user->actor, actor, id);
  1174. while (xs_list_iter(&p, &v)) {
  1175. const char *name = xs_dict_get(v, "name");
  1176. const xs_dict *replies = xs_dict_get(v, "replies");
  1177. if (name) {
  1178. int nr = xs_number_get(xs_dict_get(replies, "totalItems"));
  1179. xs *es1 = encode_html(name);
  1180. xs *opt = xs_fmt("<input type=\"%s\""
  1181. " id=\"%s\" value=\"%s\""
  1182. " name=\"question\"> <span title=\"%d\">%s</span><br>\n",
  1183. !xs_is_null(oo) ? "radio" : "checkbox",
  1184. es1, es1, nr, es1);
  1185. s1 = xs_str_cat(s1, opt);
  1186. }
  1187. }
  1188. xs *s2 = xs_fmt("<p><input type=\"submit\" "
  1189. "class=\"button\" value=\"%s\">\n</form>\n</div>\n\n", L("Vote"));
  1190. s1 = xs_str_cat(s1, s2);
  1191. c = xs_str_cat(c, s1);
  1192. }
  1193. /* if it's *really* closed, say it */
  1194. if (closed == 2) {
  1195. xs *s1 = xs_fmt("<p>%s</p>\n", L("Closed"));
  1196. c = xs_str_cat(c, s1);
  1197. }
  1198. else {
  1199. /* show when the poll closes */
  1200. const char *end_time = xs_dict_get(msg, "endTime");
  1201. if (!xs_is_null(end_time)) {
  1202. time_t t0 = time(NULL);
  1203. time_t t1 = xs_parse_iso_date(end_time, 0);
  1204. if (t1 > 0 && t1 > t0) {
  1205. time_t diff_time = t1 - t0;
  1206. xs *tf = xs_str_time_diff(diff_time);
  1207. char *p = tf;
  1208. /* skip leading zeros */
  1209. for (; *p == '0' || *p == ':'; p++);
  1210. xs *es1 = encode_html(p);
  1211. xs *s1 = xs_fmt("<p>%s %s</p>", L("Closes in"), es1);
  1212. c = xs_str_cat(c, s1);
  1213. }
  1214. }
  1215. }
  1216. }
  1217. s = xs_str_cat(s, c);
  1218. }
  1219. s = xs_str_cat(s, "\n");
  1220. /* add the attachments */
  1221. v = xs_dict_get(msg, "attachment");
  1222. if (!xs_is_null(v)) { /** attachments **/
  1223. xs *attach = NULL;
  1224. /* ensure it's a list */
  1225. if (xs_type(v) == XSTYPE_DICT) {
  1226. attach = xs_list_new();
  1227. attach = xs_list_append(attach, v);
  1228. }
  1229. else
  1230. if (xs_type(v) == XSTYPE_LIST)
  1231. attach = xs_dup(v);
  1232. else
  1233. attach = xs_list_new();
  1234. /* does the message have an image? */
  1235. if (xs_type(v = xs_dict_get(msg, "image")) == XSTYPE_DICT) {
  1236. /* add it to the attachment list */
  1237. attach = xs_list_append(attach, v);
  1238. }
  1239. /* make custom css for attachments easier */
  1240. s = xs_str_cat(s, "<div class=\"snac-content-attachments\">\n");
  1241. xs_list *p = attach;
  1242. while (xs_list_iter(&p, &v)) {
  1243. const char *t = xs_dict_get(v, "mediaType");
  1244. if (xs_is_null(t))
  1245. t = xs_dict_get(v, "type");
  1246. if (xs_is_null(t))
  1247. continue;
  1248. const char *url = xs_dict_get(v, "url");
  1249. if (xs_is_null(url))
  1250. url = xs_dict_get(v, "href");
  1251. if (xs_is_null(url))
  1252. continue;
  1253. /* infer MIME type from non-specific attachments */
  1254. if (xs_list_len(attach) < 2 && xs_match(t, "Link|Document")) {
  1255. const char *mt = xs_mime_by_ext(url);
  1256. if (xs_match(mt, "image/*|audio/*|video/*")) /* */
  1257. t = mt;
  1258. }
  1259. const char *name = xs_dict_get(v, "name");
  1260. if (xs_is_null(name))
  1261. name = xs_dict_get(msg, "name");
  1262. if (xs_is_null(name))
  1263. name = L("No description");
  1264. xs *es1 = encode_html(name);
  1265. xs *s1 = NULL;
  1266. if (xs_startswith(t, "image/") || strcmp(t, "Image") == 0) {
  1267. s1 = xs_fmt(
  1268. "<a href=\"%s\" target=\"_blank\">"
  1269. "<img loading=\"lazy\" src=\"%s\" alt=\"%s\" title=\"%s\"/></a>\n",
  1270. url, url, es1, es1);
  1271. }
  1272. else
  1273. if (xs_startswith(t, "video/")) {
  1274. s1 = xs_fmt("<video style=\"width: 100%\" class=\"snac-embedded-video\" "
  1275. "controls src=\"%s\">Video: "
  1276. "<a href=\"%s\">%s</a></video>\n", url, url, es1);
  1277. }
  1278. else
  1279. if (xs_startswith(t, "audio/")) {
  1280. s1 = xs_fmt("<audio style=\"width: 100%\" class=\"snac-embedded-audio\" "
  1281. "controls src=\"%s\">Audio: "
  1282. "<a href=\"%s\">%s</a></audio>\n", url, url, es1);
  1283. }
  1284. else
  1285. if (strcmp(t, "Link") == 0) {
  1286. xs *es2 = encode_html(url);
  1287. s1 = xs_fmt("<p><a href=\"%s\">%s</a></p>\n", url, es2);
  1288. }
  1289. else {
  1290. s1 = xs_fmt("<p><a href=\"%s\">Attachment: %s</a></p>\n", url, es1);
  1291. }
  1292. if (!xs_is_null(s1))
  1293. s = xs_str_cat(s, s1);
  1294. }
  1295. s = xs_str_cat(s, "</div>\n");
  1296. }
  1297. /* has this message an audience (i.e., comes from a channel or community)? */
  1298. const char *audience = xs_dict_get(msg, "audience");
  1299. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  1300. xs *es1 = encode_html(audience);
  1301. xs *s1 = xs_fmt("<p>(<a href=\"%s\" title=\"%s\">%s</a>)</p>\n",
  1302. audience, L("Source channel or community"), es1);
  1303. s = xs_str_cat(s, s1);
  1304. }
  1305. if (sensitive)
  1306. s = xs_str_cat(s, "</details><p>\n");
  1307. s = xs_str_cat(s, "</div>\n");
  1308. /** controls **/
  1309. if (!local && user)
  1310. s = html_entry_controls(user, s, msg, md5);
  1311. /** children **/
  1312. if (!hide_children) {
  1313. xs *children = object_children(id);
  1314. int left = xs_list_len(children);
  1315. if (left) {
  1316. char *p, *cmd5;
  1317. int older_open = 0;
  1318. xs *ss = xs_str_new(NULL);
  1319. int n_children = 0;
  1320. ss = xs_str_cat(ss, "<details open><summary>...</summary><p>\n");
  1321. if (level < 4)
  1322. ss = xs_str_cat(ss, "<div class=\"snac-children\">\n");
  1323. else
  1324. ss = xs_str_cat(ss, "<div>\n");
  1325. if (left > 3) {
  1326. xs *s1 = xs_fmt("<details><summary>%s</summary>\n", L("Older..."));
  1327. ss = xs_str_cat(ss, s1);
  1328. older_open = 1;
  1329. }
  1330. p = children;
  1331. while (xs_list_iter(&p, &cmd5)) {
  1332. xs *chd = NULL;
  1333. if (user)
  1334. timeline_get_by_md5(user, cmd5, &chd);
  1335. else
  1336. object_get_by_md5(cmd5, &chd);
  1337. if (older_open && left <= 3) {
  1338. ss = xs_str_cat(ss, "</details>\n");
  1339. older_open = 0;
  1340. }
  1341. if (chd != NULL && xs_is_null(xs_dict_get(chd, "name"))) {
  1342. ss = html_entry(user, ss, chd, local, level + 1, cmd5, hide_children);
  1343. n_children++;
  1344. }
  1345. else
  1346. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  1347. left--;
  1348. }
  1349. if (older_open)
  1350. ss = xs_str_cat(ss, "</details>\n");
  1351. ss = xs_str_cat(ss, "</div>\n");
  1352. ss = xs_str_cat(ss, "</details>\n");
  1353. if (n_children)
  1354. s = xs_str_cat(s, ss);
  1355. }
  1356. }
  1357. s = xs_str_cat(s, "</div>\n</div>\n");
  1358. return xs_str_cat(os, s);
  1359. }
  1360. xs_str *html_footer(xs_str *s)
  1361. {
  1362. xs_html *footer = xs_html_tag("div",
  1363. xs_html_attr("class", "snac-footer"),
  1364. xs_html_tag("a",
  1365. xs_html_attr("href", srv_baseurl),
  1366. xs_html_text(L("about this site"))),
  1367. xs_html_text(" - "),
  1368. xs_html_text(L("powered by ")),
  1369. xs_html_tag("a",
  1370. xs_html_attr("href", WHAT_IS_SNAC_URL),
  1371. xs_html_tag("abbr",
  1372. xs_html_attr("title", "Social Network Are Crap"),
  1373. xs_html_text("snac"))));
  1374. xs *s1 = xs_html_render(footer);
  1375. return xs_str_cat(s, s1, "\n");
  1376. }
  1377. xs_str *html_timeline(snac *user, const xs_list *list, int local,
  1378. int skip, int show, int show_more, char *tag)
  1379. /* returns the HTML for the timeline */
  1380. {
  1381. xs_str *s = xs_str_new(NULL);
  1382. xs_list *p = (xs_list *)list;
  1383. char *v;
  1384. double t = ftime();
  1385. if (user)
  1386. s = html_user_header(user, s, local);
  1387. else
  1388. s = html_instance_header(s, tag);
  1389. if (user && !local)
  1390. s = html_top_controls(user, s);
  1391. s = xs_str_cat(s, "<a name=\"snac-posts\"></a>\n");
  1392. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1393. while (xs_list_iter(&p, &v)) {
  1394. xs *msg = NULL;
  1395. int status;
  1396. if (user && !is_pinned_by_md5(user, v))
  1397. status = timeline_get_by_md5(user, v, &msg);
  1398. else
  1399. status = object_get_by_md5(v, &msg);
  1400. if (!valid_status(status))
  1401. continue;
  1402. /* if it's an instance page, discard private users */
  1403. if (user == NULL && xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
  1404. const char *atto = xs_dict_get(msg, "attributedTo");
  1405. xs *l = xs_split(atto, "/");
  1406. const char *uid = xs_list_get(l, -1);
  1407. snac user;
  1408. int skip = 1;
  1409. if (uid && user_open(&user, uid)) {
  1410. if (xs_type(xs_dict_get(user.config, "private")) != XSTYPE_TRUE)
  1411. skip = 0;
  1412. user_free(&user);
  1413. }
  1414. if (skip)
  1415. continue;
  1416. }
  1417. s = html_entry(user, s, msg, local, 0, v, user ? 0 : 1);
  1418. }
  1419. s = xs_str_cat(s, "</div>\n");
  1420. if (list && user && local) {
  1421. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE) {
  1422. s = xs_str_cat(s, "<!-- history disabled -->\n");
  1423. }
  1424. else {
  1425. xs *s1 = xs_fmt(
  1426. "<div class=\"snac-history\">\n"
  1427. "<p class=\"snac-history-title\">%s</p><ul>\n",
  1428. L("History")
  1429. );
  1430. s = xs_str_cat(s, s1);
  1431. xs *list = history_list(user);
  1432. char *p, *v;
  1433. p = list;
  1434. while (xs_list_iter(&p, &v)) {
  1435. xs *fn = xs_replace(v, ".html", "");
  1436. xs *s1 = xs_fmt(
  1437. "<li><a href=\"%s/h/%s\">%s</a></li>\n",
  1438. user->actor, v, fn);
  1439. s = xs_str_cat(s, s1);
  1440. }
  1441. s = xs_str_cat(s, "</ul></div>\n");
  1442. }
  1443. }
  1444. {
  1445. xs *s1 = xs_fmt("<!-- %lf seconds -->\n", ftime() - t);
  1446. s = xs_str_cat(s, s1);
  1447. }
  1448. if (show_more) {
  1449. xs *t = NULL;
  1450. xs *m = NULL;
  1451. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  1452. if (tag) {
  1453. t = xs_fmt("%s?t=%s", srv_baseurl, tag);
  1454. m = xs_fmt("%s&%s", t, ss);
  1455. }
  1456. else {
  1457. t = xs_fmt("%s%s", user ? user->actor : srv_baseurl, local ? "" : "/admin");
  1458. m = xs_fmt("%s?%s", t, ss);
  1459. }
  1460. xs *s1 = xs_fmt(
  1461. "<p>"
  1462. "<a href=\"%s\" name=\"snac-more\">%s</a> - "
  1463. "<a href=\"%s\" name=\"snac-more\">%s</a>"
  1464. "</p>\n",
  1465. t, L("Back to top"),
  1466. m, L("More...")
  1467. );
  1468. s = xs_str_cat(s, s1);
  1469. }
  1470. s = html_footer(s);
  1471. s = xs_str_cat(s, "</body>\n</html>\n");
  1472. return s;
  1473. }
  1474. xs_str *html_people_list(snac *snac, xs_str *os, xs_list *list, const char *header, const char *t)
  1475. {
  1476. xs *s = xs_str_new(NULL);
  1477. xs *es1 = encode_html(header);
  1478. xs *h = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", es1);
  1479. xs_list *p;
  1480. char *actor_id;
  1481. s = xs_str_cat(s, h);
  1482. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1483. p = list;
  1484. while (xs_list_iter(&p, &actor_id)) {
  1485. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  1486. xs *actor = NULL;
  1487. if (valid_status(actor_get(actor_id, &actor))) {
  1488. xs_html *snac_post = xs_html_tag("div",
  1489. xs_html_attr("class", "snac-post"),
  1490. xs_html_tag("div",
  1491. xs_html_attr("class", "snac-post-header"),
  1492. html_actor_icon(actor, xs_dict_get(actor, "published"), NULL, NULL, 0)));
  1493. /* content (user bio) */
  1494. char *c = xs_dict_get(actor, "summary");
  1495. if (!xs_is_null(c)) {
  1496. xs *sc = sanitize(c);
  1497. xs_html *snac_content = xs_html_tag("div",
  1498. xs_html_attr("class", "snac-content"));
  1499. if (xs_startswith(sc, "<p>"))
  1500. xs_html_add(snac_content,
  1501. xs_html_raw(sc)); /* already sanitized */
  1502. else
  1503. xs_html_add(snac_content,
  1504. xs_html_tag("p",
  1505. xs_html_raw(sc))); /* already sanitized */
  1506. xs_html_add(snac_post, snac_content);
  1507. }
  1508. /* buttons */
  1509. xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
  1510. xs_html *snac_controls = xs_html_tag("div",
  1511. xs_html_attr("class", "snac-controls"));
  1512. xs_html *form = xs_html_tag("form",
  1513. xs_html_attr("autocomplete", "off"),
  1514. xs_html_attr("method", "post"),
  1515. xs_html_attr("action", btn_form_action),
  1516. xs_html_sctag("input",
  1517. xs_html_attr("type", "hidden"),
  1518. xs_html_attr("name", "actor"),
  1519. xs_html_attr("value", actor_id)),
  1520. xs_html_sctag("input",
  1521. xs_html_attr("type", "hidden"),
  1522. xs_html_attr("name", "actor-form"),
  1523. xs_html_attr("value", "yes")));
  1524. xs_html_add(snac_controls, form);
  1525. if (following_check(snac, actor_id)) {
  1526. xs_html_add(form,
  1527. html_button_2("unfollow", L("Unfollow"),
  1528. L("Stop following this user's activity")));
  1529. if (is_limited(snac, actor_id))
  1530. xs_html_add(form,
  1531. html_button_2("unlimit", L("Unlimit"),
  1532. L("Allow announces (boosts) from this user")));
  1533. else
  1534. xs_html_add(form,
  1535. html_button_2("limit", L("Limit"),
  1536. L("Block announces (boosts) from this user")));
  1537. }
  1538. else {
  1539. xs_html_add(form,
  1540. html_button_2("follow", L("Follow"),
  1541. L("Start following this user's activity")));
  1542. if (follower_check(snac, actor_id))
  1543. xs_html_add(form,
  1544. html_button_2("delete", L("Delete"), L("Delete this user")));
  1545. }
  1546. if (is_muted(snac, actor_id))
  1547. xs_html_add(form,
  1548. html_button_2("unmute", L("Unmute"),
  1549. L("Stop blocking activities from this user")));
  1550. else
  1551. xs_html_add(form,
  1552. html_button_2("mute", L("MUTE"),
  1553. L("Block any activity from this user")));
  1554. /* the post textarea */
  1555. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  1556. xs *dm_action = xs_fmt("%s/admin/note", snac->actor);
  1557. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  1558. xs_html *dm_textarea = xs_html_tag("div",
  1559. xs_html_tag("details",
  1560. xs_html_tag("summary",
  1561. xs_html_text(L("Direct Message..."))),
  1562. xs_html_tag("p", NULL),
  1563. xs_html_tag("div",
  1564. xs_html_attr("class", "snac-note"),
  1565. xs_html_attr("id", dm_div_id),
  1566. xs_html_tag("form",
  1567. xs_html_attr("autocomplete", "off"),
  1568. xs_html_attr("method", "post"),
  1569. xs_html_attr("action", dm_action),
  1570. xs_html_attr("enctype", "multipart/form-data"),
  1571. xs_html_attr("id", dm_form_id),
  1572. xs_html_tag("textarea",
  1573. xs_html_attr("class", "snac-textarea"),
  1574. xs_html_attr("name", "content"),
  1575. xs_html_attr("rows", "4"),
  1576. xs_html_attr("wrap", "virtual"),
  1577. xs_html_attr("required", "required"),
  1578. xs_html_attr("placeholder", ""),
  1579. xs_html_text("")),
  1580. xs_html_sctag("input",
  1581. xs_html_attr("type", "hidden"),
  1582. xs_html_attr("name", "to"),
  1583. xs_html_attr("value", actor_id)),
  1584. xs_html_tag("p", NULL),
  1585. xs_html_tag("details",
  1586. xs_html_tag("summary",
  1587. xs_html_text(L("Attachment..."))),
  1588. xs_html_tag("p", NULL),
  1589. xs_html_sctag("input",
  1590. xs_html_attr("type", "file"),
  1591. xs_html_attr("name", "attach")),
  1592. xs_html_sctag("input",
  1593. xs_html_attr("type", "text"),
  1594. xs_html_attr("name", "alt_text"),
  1595. xs_html_attr("placeholder", L("Attachment description")))),
  1596. xs_html_tag("p", NULL),
  1597. xs_html_sctag("input",
  1598. xs_html_attr("type", "submit"),
  1599. xs_html_attr("class", "button"),
  1600. xs_html_attr("value", L("Post")))),
  1601. xs_html_tag("p", NULL))),
  1602. xs_html_tag("p", NULL));
  1603. xs_html_add(snac_controls, dm_textarea);
  1604. xs_html_add(snac_post, snac_controls);
  1605. {
  1606. xs *s1 = xs_html_render_s(snac_post, xs_dup("\n"));
  1607. s = xs_str_cat(s, s1);
  1608. }
  1609. }
  1610. }
  1611. s = xs_str_cat(s, "</div>\n");
  1612. return xs_str_cat(os, s);
  1613. }
  1614. xs_str *html_people(snac *snac)
  1615. {
  1616. xs_str *s = xs_str_new(NULL);
  1617. xs *wing = following_list(snac);
  1618. xs *wers = follower_list(snac);
  1619. s = html_user_header(snac, s, 0);
  1620. s = html_people_list(snac, s, wing, L("People you follow"), "i");
  1621. s = html_people_list(snac, s, wers, L("People that follow you"), "e");
  1622. s = html_footer(s);
  1623. s = xs_str_cat(s, "</body>\n</html>\n");
  1624. return s;
  1625. }
  1626. xs_str *html_notifications(snac *snac)
  1627. {
  1628. xs_str *s = xs_str_new(NULL);
  1629. xs *n_list = notify_list(snac, 0);
  1630. xs *n_time = notify_check_time(snac, 0);
  1631. xs_list *p = n_list;
  1632. xs_str *v;
  1633. enum { NHDR_NONE, NHDR_NEW, NHDR_OLD } stage = NHDR_NONE;
  1634. s = html_user_header(snac, s, 0);
  1635. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", snac->actor);
  1636. xs_html *clear_all_form = xs_html_tag("form",
  1637. xs_html_attr("autocomplete", "off"),
  1638. xs_html_attr("method", "post"),
  1639. xs_html_attr("action", clear_all_action),
  1640. xs_html_attr("id", "clear"),
  1641. xs_html_sctag("input",
  1642. xs_html_attr("type", "submit"),
  1643. xs_html_attr("class", "snac-btn-like"),
  1644. xs_html_attr("value", L("Clear all"))));
  1645. {
  1646. xs *s1 = xs_html_render(clear_all_form);
  1647. s = xs_str_cat(s, s1);
  1648. }
  1649. while (xs_list_iter(&p, &v)) {
  1650. xs *noti = notify_get(snac, v);
  1651. if (noti == NULL)
  1652. continue;
  1653. xs *obj = NULL;
  1654. const char *type = xs_dict_get(noti, "type");
  1655. const char *utype = xs_dict_get(noti, "utype");
  1656. const char *id = xs_dict_get(noti, "objid");
  1657. if (xs_is_null(id) || !valid_status(object_get(id, &obj)))
  1658. continue;
  1659. if (is_hidden(snac, id))
  1660. continue;
  1661. const char *actor_id = xs_dict_get(noti, "actor");
  1662. xs *actor = NULL;
  1663. if (!valid_status(actor_get(actor_id, &actor)))
  1664. continue;
  1665. xs *a_name = actor_name(actor);
  1666. if (strcmp(v, n_time) > 0) {
  1667. /* unseen notification */
  1668. if (stage == NHDR_NONE) {
  1669. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("New"));
  1670. s = xs_str_cat(s, s1);
  1671. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1672. stage = NHDR_NEW;
  1673. }
  1674. }
  1675. else {
  1676. /* already seen notification */
  1677. if (stage != NHDR_OLD) {
  1678. if (stage == NHDR_NEW)
  1679. s = xs_str_cat(s, "</div>\n");
  1680. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("Already seen"));
  1681. s = xs_str_cat(s, s1);
  1682. s = xs_str_cat(s, "<div class=\"snac-posts\">\n");
  1683. stage = NHDR_OLD;
  1684. }
  1685. }
  1686. const char *label = type;
  1687. if (strcmp(type, "Create") == 0)
  1688. label = L("Mention");
  1689. else
  1690. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1691. label = L("Finished poll");
  1692. else
  1693. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  1694. label = L("Unfollow");
  1695. xs *es1 = encode_html(label);
  1696. xs *s1 = xs_fmt("<div class=\"snac-post-with-desc\">\n"
  1697. "<p><b>%s by <a href=\"%s\">%s</a></b>:</p>\n",
  1698. es1, actor_id, a_name);
  1699. s = xs_str_cat(s, s1);
  1700. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0) {
  1701. xs_html *div = xs_html_tag("div",
  1702. xs_html_attr("class", "snac-post"),
  1703. html_actor_icon(actor, NULL, NULL, NULL, 0));
  1704. xs *s1 = xs_html_render(div);
  1705. s = xs_str_cat(s, s1);
  1706. }
  1707. else {
  1708. xs *md5 = xs_md5_hex(id, strlen(id));
  1709. s = html_entry(snac, s, obj, 0, 0, md5, 1);
  1710. }
  1711. s = xs_str_cat(s, "</div>\n");
  1712. }
  1713. if (stage == NHDR_NONE) {
  1714. xs *s1 = xs_fmt("<h2 class=\"snac-header\">%s</h2>\n", L("None"));
  1715. s = xs_str_cat(s, s1);
  1716. }
  1717. else
  1718. s = xs_str_cat(s, "</div>\n");
  1719. s = html_footer(s);
  1720. s = xs_str_cat(s, "</body>\n</html>\n");
  1721. /* set the check time to now */
  1722. xs *dummy = notify_check_time(snac, 1);
  1723. dummy = xs_free(dummy);
  1724. timeline_touch(snac);
  1725. return s;
  1726. }
  1727. int html_get_handler(const xs_dict *req, const char *q_path,
  1728. char **body, int *b_size, char **ctype, xs_str **etag)
  1729. {
  1730. char *accept = xs_dict_get(req, "accept");
  1731. int status = 404;
  1732. snac snac;
  1733. xs *uid = NULL;
  1734. char *p_path;
  1735. int cache = 1;
  1736. int save = 1;
  1737. char *v;
  1738. xs *l = xs_split_n(q_path, "/", 2);
  1739. v = xs_list_get(l, 1);
  1740. if (xs_is_null(v)) {
  1741. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  1742. return 404;
  1743. }
  1744. uid = xs_dup(v);
  1745. /* rss extension? */
  1746. if (xs_endswith(uid, ".rss")) {
  1747. uid = xs_crop_i(uid, 0, -4);
  1748. p_path = ".rss";
  1749. }
  1750. else
  1751. p_path = xs_list_get(l, 2);
  1752. if (!uid || !user_open(&snac, uid)) {
  1753. /* invalid user */
  1754. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  1755. return 404;
  1756. }
  1757. /* return the RSS if requested by Accept header */
  1758. if (accept != NULL) {
  1759. if (xs_str_in(accept, "text/xml") != -1 ||
  1760. xs_str_in(accept, "application/rss+xml") != -1)
  1761. p_path = ".rss";
  1762. }
  1763. /* check if server config variable 'disable_cache' is set */
  1764. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  1765. cache = 0;
  1766. int skip = 0;
  1767. int show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1768. char *q_vars = xs_dict_get(req, "q_vars");
  1769. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  1770. skip = atoi(v), cache = 0, save = 0;
  1771. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  1772. show = atoi(v), cache = 0, save = 0;
  1773. if (p_path == NULL) { /** public timeline **/
  1774. xs *h = xs_str_localtime(0, "%Y-%m.html");
  1775. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  1776. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL);
  1777. *b_size = strlen(*body);
  1778. status = 200;
  1779. }
  1780. else
  1781. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  1782. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  1783. status = history_get(&snac, h, body, b_size,
  1784. xs_dict_get(req, "if-none-match"), etag);
  1785. }
  1786. else {
  1787. xs *list = timeline_list(&snac, "public", skip, show);
  1788. xs *next = timeline_list(&snac, "public", skip + show, 1);
  1789. xs *pins = pinned_list(&snac);
  1790. pins = xs_list_cat(pins, list);
  1791. *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL);
  1792. *b_size = strlen(*body);
  1793. status = 200;
  1794. if (save)
  1795. history_add(&snac, h, *body, *b_size, etag);
  1796. }
  1797. }
  1798. else
  1799. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  1800. if (!login(&snac, req)) {
  1801. *body = xs_dup(uid);
  1802. status = 401;
  1803. }
  1804. else {
  1805. if (cache && history_mtime(&snac, "timeline.html_") > timeline_mtime(&snac)) {
  1806. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  1807. status = history_get(&snac, "timeline.html_", body, b_size,
  1808. xs_dict_get(req, "if-none-match"), etag);
  1809. }
  1810. else {
  1811. snac_debug(&snac, 1, xs_fmt("building timeline"));
  1812. xs *list = timeline_list(&snac, "private", skip, show);
  1813. xs *next = timeline_list(&snac, "private", skip + show, 1);
  1814. xs *pins = pinned_list(&snac);
  1815. pins = xs_list_cat(pins, list);
  1816. *body = html_timeline(&snac, pins, 0, skip, show, xs_list_len(next), NULL);
  1817. *b_size = strlen(*body);
  1818. status = 200;
  1819. if (save)
  1820. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  1821. }
  1822. }
  1823. }
  1824. else
  1825. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  1826. if (!login(&snac, req)) {
  1827. *body = xs_dup(uid);
  1828. status = 401;
  1829. }
  1830. else {
  1831. *body = html_people(&snac);
  1832. *b_size = strlen(*body);
  1833. status = 200;
  1834. }
  1835. }
  1836. else
  1837. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  1838. if (!login(&snac, req)) {
  1839. *body = xs_dup(uid);
  1840. status = 401;
  1841. }
  1842. else {
  1843. *body = html_notifications(&snac);
  1844. *b_size = strlen(*body);
  1845. status = 200;
  1846. }
  1847. }
  1848. else
  1849. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  1850. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  1851. return 403;
  1852. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1853. xs *msg = NULL;
  1854. if (valid_status(object_get(id, &msg))) {
  1855. xs *md5 = xs_md5_hex(id, strlen(id));
  1856. xs *list = xs_list_new();
  1857. list = xs_list_append(list, md5);
  1858. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL);
  1859. *b_size = strlen(*body);
  1860. status = 200;
  1861. }
  1862. }
  1863. else
  1864. if (xs_startswith(p_path, "s/")) { /** a static file **/
  1865. xs *l = xs_split(p_path, "/");
  1866. char *id = xs_list_get(l, 1);
  1867. int sz;
  1868. if (id && *id) {
  1869. status = static_get(&snac, id, body, &sz,
  1870. xs_dict_get(req, "if-none-match"), etag);
  1871. if (valid_status(status)) {
  1872. *b_size = sz;
  1873. *ctype = (char *)xs_mime_by_ext(id);
  1874. }
  1875. }
  1876. }
  1877. else
  1878. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  1879. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  1880. return 403;
  1881. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  1882. return 403;
  1883. xs *l = xs_split(p_path, "/");
  1884. char *id = xs_list_get(l, 1);
  1885. if (id && *id) {
  1886. if (xs_endswith(id, "timeline.html_")) {
  1887. /* Don't let them in */
  1888. *b_size = 0;
  1889. status = 404;
  1890. }
  1891. else
  1892. status = history_get(&snac, id, body, b_size,
  1893. xs_dict_get(req, "if-none-match"), etag);
  1894. }
  1895. }
  1896. else
  1897. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  1898. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  1899. return 403;
  1900. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1901. xs *bio = not_really_markdown(xs_dict_get(snac.config, "bio"), NULL);
  1902. xs *rss_title = xs_fmt("%s (@%s@%s)",
  1903. xs_dict_get(snac.config, "name"),
  1904. snac.uid,
  1905. xs_dict_get(srv_config, "host"));
  1906. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  1907. xs_html *rss = xs_html_tag("rss",
  1908. xs_html_attr("version", "0.91"));
  1909. xs_html *channel = xs_html_tag("channel",
  1910. xs_html_tag("title",
  1911. xs_html_text(rss_title)),
  1912. xs_html_tag("language",
  1913. xs_html_text("en")),
  1914. xs_html_tag("link",
  1915. xs_html_text(rss_link)),
  1916. xs_html_tag("description",
  1917. xs_html_text(bio)));
  1918. xs_html_add(rss, channel);
  1919. xs_list *p = elems;
  1920. char *v;
  1921. while (xs_list_iter(&p, &v)) {
  1922. xs *msg = NULL;
  1923. if (!valid_status(timeline_get_by_md5(&snac, v, &msg)))
  1924. continue;
  1925. char *id = xs_dict_get(msg, "id");
  1926. char *content = xs_dict_get(msg, "content");
  1927. if (!xs_startswith(id, snac.actor))
  1928. continue;
  1929. /* create a title with the first line of the content */
  1930. xs *es_title = xs_replace(content, "<br>", "\n");
  1931. xs *title = xs_str_new(NULL);
  1932. int i;
  1933. for (i = 0; es_title[i] && es_title[i] != '\n' && es_title[i] != '&' && i < 50; i++)
  1934. title = xs_append_m(title, &es_title[i], 1);
  1935. xs_html_add(channel,
  1936. xs_html_tag("item",
  1937. xs_html_tag("title",
  1938. xs_html_text(title)),
  1939. xs_html_tag("link",
  1940. xs_html_text(id)),
  1941. xs_html_tag("description",
  1942. xs_html_text(content))));
  1943. }
  1944. *body = xs_html_render_s(rss, xs_dup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"));
  1945. *b_size = strlen(*body);
  1946. *ctype = "application/rss+xml; charset=utf-8";
  1947. status = 200;
  1948. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  1949. }
  1950. else
  1951. status = 404;
  1952. user_free(&snac);
  1953. if (valid_status(status) && *ctype == NULL) {
  1954. *ctype = "text/html; charset=utf-8";
  1955. }
  1956. return status;
  1957. }
  1958. int html_post_handler(const xs_dict *req, const char *q_path,
  1959. char *payload, int p_size,
  1960. char **body, int *b_size, char **ctype)
  1961. {
  1962. (void)p_size;
  1963. (void)ctype;
  1964. int status = 0;
  1965. snac snac;
  1966. char *uid, *p_path;
  1967. xs_dict *p_vars;
  1968. xs *l = xs_split_n(q_path, "/", 2);
  1969. uid = xs_list_get(l, 1);
  1970. if (!uid || !user_open(&snac, uid)) {
  1971. /* invalid user */
  1972. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  1973. return 404;
  1974. }
  1975. p_path = xs_list_get(l, 2);
  1976. /* all posts must be authenticated */
  1977. if (!login(&snac, req)) {
  1978. user_free(&snac);
  1979. *body = xs_dup(uid);
  1980. return 401;
  1981. }
  1982. p_vars = xs_dict_get(req, "p_vars");
  1983. #if 0
  1984. xs_json_dump(p_vars, 4, stdout);
  1985. #endif
  1986. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  1987. /* post note */
  1988. xs_str *content = xs_dict_get(p_vars, "content");
  1989. xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  1990. xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  1991. xs_list *attach_file = xs_dict_get(p_vars, "attach");
  1992. xs_str *to = xs_dict_get(p_vars, "to");
  1993. xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  1994. xs_str *summary = xs_dict_get(p_vars, "summary");
  1995. xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  1996. xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  1997. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  1998. xs *attach_list = xs_list_new();
  1999. /* default alt text */
  2000. if (xs_is_null(alt_text))
  2001. alt_text = "";
  2002. /* is attach_url set? */
  2003. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  2004. xs *l = xs_list_new();
  2005. l = xs_list_append(l, attach_url);
  2006. l = xs_list_append(l, alt_text);
  2007. attach_list = xs_list_append(attach_list, l);
  2008. }
  2009. /* is attach_file set? */
  2010. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  2011. char *fn = xs_list_get(attach_file, 0);
  2012. if (*fn != '\0') {
  2013. char *ext = strrchr(fn, '.');
  2014. xs *hash = xs_md5_hex(fn, strlen(fn));
  2015. xs *id = xs_fmt("%s%s", hash, ext);
  2016. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2017. int fo = xs_number_get(xs_list_get(attach_file, 1));
  2018. int fs = xs_number_get(xs_list_get(attach_file, 2));
  2019. /* store */
  2020. static_put(&snac, id, payload + fo, fs);
  2021. xs *l = xs_list_new();
  2022. l = xs_list_append(l, url);
  2023. l = xs_list_append(l, alt_text);
  2024. attach_list = xs_list_append(attach_list, l);
  2025. }
  2026. }
  2027. if (content != NULL) {
  2028. xs *msg = NULL;
  2029. xs *c_msg = NULL;
  2030. xs *content_2 = xs_replace(content, "\r", "");
  2031. xs *poll_opts = NULL;
  2032. /* is there a valid set of poll options? */
  2033. const char *v = xs_dict_get(p_vars, "poll_options");
  2034. if (!xs_is_null(v) && *v) {
  2035. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  2036. poll_opts = xs_split(v2, "\n");
  2037. }
  2038. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  2039. /* get the rest of poll configuration */
  2040. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  2041. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  2042. int multiple = 0;
  2043. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  2044. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  2045. multiple = 1;
  2046. msg = msg_question(&snac, content_2, attach_list,
  2047. poll_opts, multiple, end_secs);
  2048. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  2049. }
  2050. else
  2051. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv);
  2052. if (sensitive != NULL) {
  2053. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  2054. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  2055. }
  2056. if (xs_is_null(edit_id)) {
  2057. /* new message */
  2058. c_msg = msg_create(&snac, msg);
  2059. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2060. }
  2061. else {
  2062. /* an edition of a previous message */
  2063. xs *p_msg = NULL;
  2064. if (valid_status(object_get(edit_id, &p_msg))) {
  2065. /* copy relevant fields from previous version */
  2066. char *fields[] = { "id", "context", "url", "published",
  2067. "to", "inReplyTo", NULL };
  2068. int n;
  2069. for (n = 0; fields[n]; n++) {
  2070. char *v = xs_dict_get(p_msg, fields[n]);
  2071. msg = xs_dict_set(msg, fields[n], v);
  2072. }
  2073. /* set the updated field */
  2074. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2075. msg = xs_dict_set(msg, "updated", updated);
  2076. /* overwrite object, not updating the indexes */
  2077. object_add_ow(edit_id, msg);
  2078. /* update message */
  2079. c_msg = msg_update(&snac, msg);
  2080. }
  2081. else
  2082. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  2083. }
  2084. if (c_msg != NULL)
  2085. enqueue_message(&snac, c_msg);
  2086. history_del(&snac, "timeline.html_");
  2087. }
  2088. status = 303;
  2089. }
  2090. else
  2091. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  2092. /* action on an entry */
  2093. char *id = xs_dict_get(p_vars, "id");
  2094. char *actor = xs_dict_get(p_vars, "actor");
  2095. char *action = xs_dict_get(p_vars, "action");
  2096. char *group = xs_dict_get(p_vars, "group");
  2097. if (action == NULL)
  2098. return 404;
  2099. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  2100. status = 303;
  2101. if (strcmp(action, L("Like")) == 0) { /** **/
  2102. xs *msg = msg_admiration(&snac, id, "Like");
  2103. if (msg != NULL) {
  2104. enqueue_message(&snac, msg);
  2105. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  2106. }
  2107. }
  2108. else
  2109. if (strcmp(action, L("Boost")) == 0) { /** **/
  2110. xs *msg = msg_admiration(&snac, id, "Announce");
  2111. if (msg != NULL) {
  2112. enqueue_message(&snac, msg);
  2113. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  2114. }
  2115. }
  2116. else
  2117. if (strcmp(action, L("MUTE")) == 0) { /** **/
  2118. mute(&snac, actor);
  2119. }
  2120. else
  2121. if (strcmp(action, L("Unmute")) == 0) { /** **/
  2122. unmute(&snac, actor);
  2123. }
  2124. else
  2125. if (strcmp(action, L("Hide")) == 0) { /** **/
  2126. hide(&snac, id);
  2127. }
  2128. else
  2129. if (strcmp(action, L("Limit")) == 0) { /** **/
  2130. limit(&snac, actor);
  2131. }
  2132. else
  2133. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  2134. unlimit(&snac, actor);
  2135. }
  2136. else
  2137. if (strcmp(action, L("Follow")) == 0) { /** **/
  2138. xs *msg = msg_follow(&snac, actor);
  2139. if (msg != NULL) {
  2140. /* reload the actor from the message, in may be different */
  2141. actor = xs_dict_get(msg, "object");
  2142. following_add(&snac, actor, msg);
  2143. enqueue_output_by_actor(&snac, msg, actor, 0);
  2144. }
  2145. }
  2146. else
  2147. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  2148. /* get the following object */
  2149. xs *object = NULL;
  2150. if (valid_status(following_get(&snac, actor, &object))) {
  2151. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2152. following_del(&snac, actor);
  2153. enqueue_output_by_actor(&snac, msg, actor, 0);
  2154. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  2155. }
  2156. else
  2157. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  2158. }
  2159. else
  2160. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  2161. xs *msg = msg_follow(&snac, group);
  2162. if (msg != NULL) {
  2163. /* reload the group from the message, in may be different */
  2164. group = xs_dict_get(msg, "object");
  2165. following_add(&snac, group, msg);
  2166. enqueue_output_by_actor(&snac, msg, group, 0);
  2167. }
  2168. }
  2169. else
  2170. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  2171. /* get the following object */
  2172. xs *object = NULL;
  2173. if (valid_status(following_get(&snac, group, &object))) {
  2174. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2175. following_del(&snac, group);
  2176. enqueue_output_by_actor(&snac, msg, group, 0);
  2177. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  2178. }
  2179. else
  2180. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  2181. }
  2182. else
  2183. if (strcmp(action, L("Delete")) == 0) { /** **/
  2184. char *actor_form = xs_dict_get(p_vars, "actor-form");
  2185. if (actor_form != NULL) {
  2186. /* delete follower */
  2187. if (valid_status(follower_del(&snac, actor)))
  2188. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  2189. else
  2190. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  2191. }
  2192. else {
  2193. /* delete an entry */
  2194. if (xs_startswith(id, snac.actor)) {
  2195. /* it's a post by us: generate a delete */
  2196. xs *msg = msg_delete(&snac, id);
  2197. enqueue_message(&snac, msg);
  2198. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  2199. }
  2200. timeline_del(&snac, id);
  2201. snac_log(&snac, xs_fmt("deleted entry %s", id));
  2202. }
  2203. }
  2204. else
  2205. if (strcmp(action, L("Pin")) == 0) { /** **/
  2206. pin(&snac, id);
  2207. timeline_touch(&snac);
  2208. }
  2209. else
  2210. if (strcmp(action, L("Unpin")) == 0) { /** **/
  2211. unpin(&snac, id);
  2212. timeline_touch(&snac);
  2213. }
  2214. else
  2215. status = 404;
  2216. /* delete the cached timeline */
  2217. if (status == 303)
  2218. history_del(&snac, "timeline.html_");
  2219. }
  2220. else
  2221. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  2222. /* change of user data */
  2223. char *v;
  2224. char *p1, *p2;
  2225. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  2226. snac.config = xs_dict_set(snac.config, "name", v);
  2227. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  2228. snac.config = xs_dict_set(snac.config, "avatar", v);
  2229. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  2230. snac.config = xs_dict_set(snac.config, "bio", v);
  2231. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  2232. strcmp(v, "on") == 0) {
  2233. snac.config = xs_dict_set(snac.config, "cw", "open");
  2234. } else { /* if the checkbox is not set, the parameter is missing */
  2235. snac.config = xs_dict_set(snac.config, "cw", "");
  2236. }
  2237. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  2238. snac.config = xs_dict_set(snac.config, "email", v);
  2239. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  2240. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  2241. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  2242. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  2243. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  2244. xs *days = xs_number_new(atof(v));
  2245. snac.config = xs_dict_set(snac.config, "purge_days", days);
  2246. }
  2247. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  2248. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_true);
  2249. else
  2250. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock_false);
  2251. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  2252. snac.config = xs_dict_set(snac.config, "bot", xs_stock_true);
  2253. else
  2254. snac.config = xs_dict_set(snac.config, "bot", xs_stock_false);
  2255. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  2256. snac.config = xs_dict_set(snac.config, "private", xs_stock_true);
  2257. else
  2258. snac.config = xs_dict_set(snac.config, "private", xs_stock_false);
  2259. if ((v = xs_dict_get(p_vars, "metadata")) != NULL) {
  2260. /* split the metadata and store it as a dict */
  2261. xs_dict *md = xs_dict_new();
  2262. xs *l = xs_split(v, "\n");
  2263. xs_list *p = l;
  2264. xs_str *kp;
  2265. while (xs_list_iter(&p, &kp)) {
  2266. xs *kpl = xs_split_n(kp, "=", 1);
  2267. if (xs_list_len(kpl) == 2) {
  2268. xs *k2 = xs_strip_i(xs_dup(xs_list_get(kpl, 0)));
  2269. xs *v2 = xs_strip_i(xs_dup(xs_list_get(kpl, 1)));
  2270. md = xs_dict_set(md, k2, v2);
  2271. }
  2272. }
  2273. snac.config = xs_dict_set(snac.config, "metadata", md);
  2274. }
  2275. /* uploads */
  2276. const char *uploads[] = { "avatar", "header", NULL };
  2277. int n;
  2278. for (n = 0; uploads[n]; n++) {
  2279. xs *var_name = xs_fmt("%s_file", uploads[n]);
  2280. xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  2281. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  2282. const char *fn = xs_list_get(uploaded_file, 0);
  2283. if (fn && *fn) {
  2284. const char *mimetype = xs_mime_by_ext(fn);
  2285. if (xs_startswith(mimetype, "image/")) {
  2286. const char *ext = strrchr(fn, '.');
  2287. xs *hash = xs_md5_hex(fn, strlen(fn));
  2288. xs *id = xs_fmt("%s%s", hash, ext);
  2289. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2290. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  2291. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  2292. /* store */
  2293. static_put(&snac, id, payload + fo, fs);
  2294. snac.config = xs_dict_set(snac.config, uploads[n], url);
  2295. }
  2296. }
  2297. }
  2298. }
  2299. /* password change? */
  2300. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  2301. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  2302. *p1 && strcmp(p1, p2) == 0) {
  2303. xs *pw = hash_password(snac.uid, p1, NULL);
  2304. snac.config = xs_dict_set(snac.config, "passwd", pw);
  2305. }
  2306. xs *fn = xs_fmt("%s/user.json", snac.basedir);
  2307. xs *bfn = xs_fmt("%s.bak", fn);
  2308. FILE *f;
  2309. rename(fn, bfn);
  2310. if ((f = fopen(fn, "w")) != NULL) {
  2311. xs_json_dump(snac.config, 4, f);
  2312. fclose(f);
  2313. }
  2314. else
  2315. rename(bfn, fn);
  2316. history_del(&snac, "timeline.html_");
  2317. xs *a_msg = msg_actor(&snac);
  2318. xs *u_msg = msg_update(&snac, a_msg);
  2319. enqueue_message(&snac, u_msg);
  2320. status = 303;
  2321. }
  2322. else
  2323. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  2324. notify_clear(&snac);
  2325. timeline_touch(&snac);
  2326. status = 303;
  2327. }
  2328. else
  2329. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  2330. char *irt = xs_dict_get(p_vars, "irt");
  2331. const char *opt = xs_dict_get(p_vars, "question");
  2332. const char *actor = xs_dict_get(p_vars, "actor");
  2333. xs *ls = NULL;
  2334. /* multiple choices? */
  2335. if (xs_type(opt) == XSTYPE_LIST)
  2336. ls = xs_dup(opt);
  2337. else {
  2338. ls = xs_list_new();
  2339. ls = xs_list_append(ls, opt);
  2340. }
  2341. xs_list *p = ls;
  2342. xs_str *v;
  2343. while (xs_list_iter(&p, &v)) {
  2344. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1);
  2345. /* set the option */
  2346. msg = xs_dict_append(msg, "name", v);
  2347. xs *c_msg = msg_create(&snac, msg);
  2348. enqueue_message(&snac, c_msg);
  2349. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2350. }
  2351. status = 303;
  2352. }
  2353. if (status == 303) {
  2354. char *redir = xs_dict_get(p_vars, "redir");
  2355. if (xs_is_null(redir))
  2356. redir = "top";
  2357. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  2358. *b_size = strlen(*body);
  2359. }
  2360. user_free(&snac);
  2361. return status;
  2362. }