html.c 93 KB

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