html.c 97 KB

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