html.c 103 KB

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