html.c 147 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_regex.h"
  7. #include "xs_set.h"
  8. #include "xs_openssl.h"
  9. #include "xs_time.h"
  10. #include "xs_mime.h"
  11. #include "xs_match.h"
  12. #include "xs_html.h"
  13. #include "xs_curl.h"
  14. #include "snac.h"
  15. int login(snac *snac, const xs_dict *headers)
  16. /* tries a login */
  17. {
  18. int logged_in = 0;
  19. const char *auth = xs_dict_get(headers, "authorization");
  20. if (auth && xs_startswith(auth, "Basic ")) {
  21. int sz;
  22. xs *s1 = xs_crop_i(xs_dup(auth), 6, 0);
  23. xs *s2 = xs_base64_dec(s1, &sz);
  24. xs *l1 = xs_split_n(s2, ":", 1);
  25. if (xs_list_len(l1) == 2) {
  26. const char *user = xs_list_get(l1, 0);
  27. const char *pwd = xs_list_get(l1, 1);
  28. const char *addr = xs_or(xs_dict_get(headers, "remote-addr"),
  29. xs_dict_get(headers, "x-forwarded-for"));
  30. if (badlogin_check(user, addr)) {
  31. logged_in = check_password(user, pwd,
  32. xs_dict_get(snac->config, "passwd"));
  33. if (!logged_in)
  34. badlogin_inc(user, addr);
  35. }
  36. }
  37. }
  38. if (logged_in)
  39. lastlog_write(snac, "web");
  40. return logged_in;
  41. }
  42. xs_str *replace_shortnames(xs_str *s, const xs_list *tag, int ems, const char *proxy)
  43. /* replaces all the :shortnames: with the emojis in tag */
  44. {
  45. if (!xs_is_null(tag)) {
  46. xs *tag_list = NULL;
  47. if (xs_type(tag) == XSTYPE_DICT) {
  48. /* not a list */
  49. tag_list = xs_list_new();
  50. tag_list = xs_list_append(tag_list, tag);
  51. } else {
  52. /* is a list */
  53. tag_list = xs_dup(tag);
  54. }
  55. xs *style = xs_fmt("height: %dem; width: %dem; vertical-align: middle;", ems, ems);
  56. const char *v;
  57. int c = 0;
  58. while (xs_list_next(tag_list, &v, &c)) {
  59. const char *t = xs_dict_get(v, "type");
  60. if (t && strcmp(t, "Emoji") == 0) {
  61. const char *n = xs_dict_get(v, "name");
  62. const char *i = xs_dict_get(v, "icon");
  63. if (n && i) {
  64. const char *u = xs_dict_get(i, "url");
  65. xs *url = make_url(u, proxy, 0);
  66. xs_html *img = xs_html_sctag("img",
  67. xs_html_attr("loading", "lazy"),
  68. xs_html_attr("src", url),
  69. xs_html_attr("style", style));
  70. xs *s1 = xs_html_render(img);
  71. s = xs_replace_i(s, n, s1);
  72. }
  73. }
  74. }
  75. }
  76. return s;
  77. }
  78. xs_str *actor_name(xs_dict *actor, const char *proxy)
  79. /* gets the actor name */
  80. {
  81. const char *v;
  82. if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
  83. if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
  84. v = "anonymous";
  85. }
  86. }
  87. return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"), 1, proxy);
  88. }
  89. xs_html *html_actor_icon(snac *user, xs_dict *actor, const char *date,
  90. const char *udate, const char *url, int priv, int in_people, const char *proxy)
  91. {
  92. xs_html *actor_icon = xs_html_tag("p", NULL);
  93. xs *avatar = NULL;
  94. const char *v;
  95. int fwing = 0;
  96. int fwer = 0;
  97. xs *name = actor_name(actor, proxy);
  98. /* get the avatar */
  99. if ((v = xs_dict_get(actor, "icon")) != NULL) {
  100. /* if it's a list (Peertube), get the first one */
  101. if (xs_type(v) == XSTYPE_LIST)
  102. v = xs_list_get(v, 0);
  103. if ((v = xs_dict_get(v, "url")) != NULL)
  104. avatar = make_url(v, proxy, 0);
  105. }
  106. if (avatar == NULL)
  107. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  108. const char *actor_id = xs_dict_get(actor, "id");
  109. xs *href = NULL;
  110. if (user) {
  111. fwer = follower_check(user, actor_id);
  112. fwing = following_check(user, actor_id);
  113. }
  114. if (user && !in_people) {
  115. /* if this actor is a follower or being followed, create an
  116. anchored link to the people page instead of the actor url */
  117. if (fwer || fwing) {
  118. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  119. href = xs_fmt("%s/people#%s", user->actor, md5);
  120. }
  121. }
  122. if (href == NULL)
  123. href = xs_dup(actor_id);
  124. xs_html_add(actor_icon,
  125. xs_html_sctag("img",
  126. xs_html_attr("loading", "lazy"),
  127. xs_html_attr("class", "snac-avatar"),
  128. xs_html_attr("src", avatar),
  129. xs_html_attr("alt", "")),
  130. xs_html_tag("a",
  131. xs_html_attr("href", href),
  132. xs_html_attr("class", "p-author h-card snac-author"),
  133. xs_html_raw(name))); /* name is already html-escaped */
  134. if (!xs_is_null(url)) {
  135. xs *md5 = xs_md5_hex(url, strlen(url));
  136. xs_html_add(actor_icon,
  137. xs_html_text(" "),
  138. xs_html_tag("a",
  139. xs_html_attr("href", (char *)url),
  140. xs_html_attr("title", md5),
  141. xs_html_text("»")));
  142. }
  143. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
  144. xs_html_add(actor_icon,
  145. xs_html_text(" "),
  146. xs_html_tag("span",
  147. xs_html_attr("title", "bot"),
  148. xs_html_raw("🤖")));
  149. }
  150. if (fwing && fwer) {
  151. xs_html_add(actor_icon,
  152. xs_html_text(" "),
  153. xs_html_tag("span",
  154. xs_html_attr("title", "mutual relation"),
  155. xs_html_raw("🤝")));
  156. }
  157. if (priv) {
  158. xs_html_add(actor_icon,
  159. xs_html_text(" "),
  160. xs_html_tag("span",
  161. xs_html_attr("title", "private"),
  162. xs_html_raw("🔒")));
  163. }
  164. if (xs_is_null(date)) {
  165. xs_html_add(actor_icon,
  166. xs_html_raw(" "));
  167. }
  168. else {
  169. xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
  170. xs *date_title = xs_dup(date);
  171. if (!xs_is_null(udate)) {
  172. xs *sd = xs_crop_i(xs_dup(udate), 0, 10);
  173. date_label = xs_str_cat(date_label, " / ", sd);
  174. date_title = xs_str_cat(date_title, " / ", udate);
  175. }
  176. xs_html_add(actor_icon,
  177. xs_html_text(" "),
  178. xs_html_tag("time",
  179. xs_html_attr("class", "dt-published snac-pubdate"),
  180. xs_html_attr("title", date_title),
  181. xs_html_text(date_label)));
  182. }
  183. {
  184. const char *username, *id;
  185. if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
  186. /* This should never be reached */
  187. username = "anonymous";
  188. }
  189. if (xs_is_null(id = xs_dict_get(actor, "id")) || *id == '\0') {
  190. /* This should never be reached */
  191. id = "https://social.example.org/anonymous";
  192. }
  193. /* "LIKE AN ANIMAL" */
  194. xs *domain = xs_split(id, "/");
  195. xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
  196. xs_html_add(actor_icon,
  197. xs_html_sctag("br", NULL),
  198. xs_html_tag("a",
  199. xs_html_attr("href", xs_dict_get(actor, "id")),
  200. xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
  201. xs_html_text(user)));
  202. }
  203. return actor_icon;
  204. }
  205. xs_html *html_msg_icon(snac *user, const char *actor_id, const xs_dict *msg, const char *proxy)
  206. {
  207. xs *actor = NULL;
  208. xs_html *actor_icon = NULL;
  209. if (actor_id && valid_status(actor_get_refresh(user, actor_id, &actor))) {
  210. const char *date = NULL;
  211. const char *udate = NULL;
  212. const char *url = NULL;
  213. int priv = 0;
  214. const char *type = xs_dict_get(msg, "type");
  215. if (xs_match(type, POSTLIKE_OBJECT_TYPE))
  216. url = xs_dict_get(msg, "id");
  217. priv = !is_msg_public(msg);
  218. date = xs_dict_get(msg, "published");
  219. udate = xs_dict_get(msg, "updated");
  220. actor_icon = html_actor_icon(user, actor, date, udate, url, priv, 0, proxy);
  221. }
  222. return actor_icon;
  223. }
  224. xs_html *html_note(snac *user, const char *summary,
  225. const char *div_id, const char *form_id,
  226. const char *ta_plh, const char *ta_content,
  227. const char *edit_id, const char *actor_id,
  228. const xs_val *cw_yn, const char *cw_text,
  229. const xs_val *mnt_only, const char *redir,
  230. const char *in_reply_to, int poll,
  231. const char *att_file, const char *att_alt_text,
  232. int is_draft)
  233. /* Yes, this is a FUCKTON of arguments and I'm a bit embarrased */
  234. {
  235. xs *action = xs_fmt("%s/admin/note", user->actor);
  236. xs_html *form;
  237. xs_html *note = xs_html_tag("div",
  238. xs_html_tag("details",
  239. xs_html_tag("summary",
  240. xs_html_text(summary)),
  241. xs_html_tag("p", NULL),
  242. xs_html_tag("div",
  243. xs_html_attr("class", "snac-note"),
  244. xs_html_attr("id", div_id),
  245. form = xs_html_tag("form",
  246. xs_html_attr("autocomplete", "off"),
  247. xs_html_attr("method", "post"),
  248. xs_html_attr("action", action),
  249. xs_html_attr("enctype", "multipart/form-data"),
  250. xs_html_attr("id", form_id),
  251. xs_html_tag("textarea",
  252. xs_html_attr("class", "snac-textarea"),
  253. xs_html_attr("name", "content"),
  254. xs_html_attr("rows", "4"),
  255. xs_html_attr("wrap", "virtual"),
  256. xs_html_attr("required", "required"),
  257. xs_html_attr("placeholder", ta_plh),
  258. xs_html_text(ta_content)),
  259. xs_html_tag("p", NULL),
  260. xs_html_text(L("Sensitive content: ")),
  261. xs_html_sctag("input",
  262. xs_html_attr("type", "checkbox"),
  263. xs_html_attr("name", "sensitive"),
  264. xs_html_attr(xs_type(cw_yn) == XSTYPE_TRUE ? "checked" : "", NULL)),
  265. xs_html_sctag("input",
  266. xs_html_attr("type", "text"),
  267. xs_html_attr("name", "summary"),
  268. xs_html_attr("placeholder", L("Sensitive content description")),
  269. xs_html_attr("value", xs_is_null(cw_text) ? "" : cw_text))))));
  270. if (actor_id)
  271. xs_html_add(form,
  272. xs_html_sctag("input",
  273. xs_html_attr("type", "hidden"),
  274. xs_html_attr("name", "to"),
  275. xs_html_attr("value", actor_id)));
  276. else {
  277. /* no actor_id; ask for mentioned_only */
  278. xs_html_add(form,
  279. xs_html_tag("p", NULL),
  280. xs_html_text(L("Only for mentioned people: ")),
  281. xs_html_sctag("input",
  282. xs_html_attr("type", "checkbox"),
  283. xs_html_attr("name", "mentioned_only"),
  284. xs_html_attr(xs_type(mnt_only) == XSTYPE_TRUE ? "checked" : "", NULL)));
  285. }
  286. if (redir)
  287. xs_html_add(form,
  288. xs_html_sctag("input",
  289. xs_html_attr("type", "hidden"),
  290. xs_html_attr("name", "redir"),
  291. xs_html_attr("value", redir)));
  292. if (in_reply_to)
  293. xs_html_add(form,
  294. xs_html_sctag("input",
  295. xs_html_attr("type", "hidden"),
  296. xs_html_attr("name", "in_reply_to"),
  297. xs_html_attr("value", in_reply_to)));
  298. else
  299. xs_html_add(form,
  300. xs_html_tag("p", NULL),
  301. xs_html_text(L("Reply to (URL): ")),
  302. xs_html_sctag("input",
  303. xs_html_attr("type", "url"),
  304. xs_html_attr("name", "in_reply_to"),
  305. xs_html_attr("placeholder", "Optional URL to reply to")));
  306. xs_html_add(form,
  307. xs_html_tag("p", NULL),
  308. xs_html_tag("span",
  309. xs_html_attr("title", L("Don't send, but store as a draft")),
  310. xs_html_text(L("Draft:")),
  311. xs_html_sctag("input",
  312. xs_html_attr("type", "checkbox"),
  313. xs_html_attr("name", "is_draft"),
  314. xs_html_attr(is_draft ? "checked" : "", NULL))));
  315. if (edit_id)
  316. xs_html_add(form,
  317. xs_html_sctag("input",
  318. xs_html_attr("type", "hidden"),
  319. xs_html_attr("name", "edit_id"),
  320. xs_html_attr("value", edit_id)));
  321. /* attachment controls */
  322. xs_html *att;
  323. xs_html_add(form,
  324. xs_html_tag("p", NULL),
  325. att = xs_html_tag("details",
  326. xs_html_tag("summary",
  327. xs_html_text(L("Attachment..."))),
  328. xs_html_tag("p", NULL)));
  329. if (att_file && *att_file)
  330. xs_html_add(att,
  331. xs_html_text(L("File:")),
  332. xs_html_sctag("input",
  333. xs_html_attr("type", "text"),
  334. xs_html_attr("name", "attach_url"),
  335. xs_html_attr("title", L("Clear this field to delete the attachment")),
  336. xs_html_attr("value", att_file)));
  337. else
  338. xs_html_add(att,
  339. xs_html_sctag("input",
  340. xs_html_attr("type", "file"),
  341. xs_html_attr("name", "attach")));
  342. xs_html_add(att,
  343. xs_html_text(" "),
  344. xs_html_sctag("input",
  345. xs_html_attr("type", "text"),
  346. xs_html_attr("name", "alt_text"),
  347. xs_html_attr("value", att_alt_text),
  348. xs_html_attr("placeholder", L("Attachment description"))));
  349. /* add poll controls */
  350. if (poll) {
  351. xs_html_add(form,
  352. xs_html_tag("p", NULL),
  353. xs_html_tag("details",
  354. xs_html_tag("summary",
  355. xs_html_text(L("Poll..."))),
  356. xs_html_tag("p",
  357. xs_html_text(L("Poll options (one per line, up to 8):")),
  358. xs_html_sctag("br", NULL),
  359. xs_html_tag("textarea",
  360. xs_html_attr("class", "snac-textarea"),
  361. xs_html_attr("name", "poll_options"),
  362. xs_html_attr("rows", "4"),
  363. xs_html_attr("wrap", "virtual"),
  364. xs_html_attr("placeholder", "Option 1...\nOption 2...\nOption 3...\n..."))),
  365. xs_html_tag("select",
  366. xs_html_attr("name", "poll_multiple"),
  367. xs_html_tag("option",
  368. xs_html_attr("value", "off"),
  369. xs_html_text(L("One choice"))),
  370. xs_html_tag("option",
  371. xs_html_attr("value", "on"),
  372. xs_html_text(L("Multiple choices")))),
  373. xs_html_text(" "),
  374. xs_html_tag("select",
  375. xs_html_attr("name", "poll_end_secs"),
  376. xs_html_tag("option",
  377. xs_html_attr("value", "300"),
  378. xs_html_text(L("End in 5 minutes"))),
  379. xs_html_tag("option",
  380. xs_html_attr("value", "3600"),
  381. xs_html_attr("selected", NULL),
  382. xs_html_text(L("End in 1 hour"))),
  383. xs_html_tag("option",
  384. xs_html_attr("value", "86400"),
  385. xs_html_text(L("End in 1 day"))))));
  386. }
  387. xs_html_add(form,
  388. xs_html_tag("p", NULL),
  389. xs_html_sctag("input",
  390. xs_html_attr("type", "submit"),
  391. xs_html_attr("class", "button"),
  392. xs_html_attr("value", L("Post"))),
  393. xs_html_tag("p", NULL));
  394. return note;
  395. }
  396. static xs_html *html_base_head(void)
  397. {
  398. xs_html *head = xs_html_tag("head",
  399. xs_html_sctag("meta",
  400. xs_html_attr("name", "viewport"),
  401. xs_html_attr("content", "width=device-width, initial-scale=1")),
  402. xs_html_sctag("meta",
  403. xs_html_attr("name", "generator"),
  404. xs_html_attr("content", USER_AGENT)));
  405. /* add server CSS and favicon */
  406. xs *f;
  407. f = xs_fmt("%s/favicon.ico", srv_baseurl);
  408. const xs_list *p = xs_dict_get(srv_config, "cssurls");
  409. const char *v;
  410. int c = 0;
  411. while (xs_list_next(p, &v, &c)) {
  412. xs_html_add(head,
  413. xs_html_sctag("link",
  414. xs_html_attr("rel", "stylesheet"),
  415. xs_html_attr("type", "text/css"),
  416. xs_html_attr("href", v)));
  417. }
  418. xs_html_add(head,
  419. xs_html_sctag("link",
  420. xs_html_attr("rel", "icon"),
  421. xs_html_attr("type", "image/x-icon"),
  422. xs_html_attr("href", f)));
  423. return head;
  424. }
  425. xs_html *html_instance_head(void)
  426. {
  427. xs_html *head = html_base_head();
  428. {
  429. FILE *f;
  430. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  431. if ((f = fopen(g_css_fn, "r")) != NULL) {
  432. xs *css = xs_readall(f);
  433. fclose(f);
  434. xs_html_add(head,
  435. xs_html_tag("style",
  436. xs_html_raw(css)));
  437. }
  438. }
  439. const char *host = xs_dict_get(srv_config, "host");
  440. const char *title = xs_dict_get(srv_config, "title");
  441. xs_html_add(head,
  442. xs_html_tag("title",
  443. xs_html_text(title && *title ? title : host)));
  444. return head;
  445. }
  446. static xs_html *html_instance_body(void)
  447. {
  448. const char *host = xs_dict_get(srv_config, "host");
  449. const char *sdesc = xs_dict_get(srv_config, "short_description");
  450. const char *email = xs_dict_get(srv_config, "admin_email");
  451. const char *acct = xs_dict_get(srv_config, "admin_account");
  452. xs *blurb = xs_replace(snac_blurb, "%host%", host);
  453. xs_html *dl;
  454. xs_html *body = xs_html_tag("body",
  455. xs_html_tag("div",
  456. xs_html_attr("class", "snac-instance-blurb"),
  457. xs_html_raw(blurb), /* pure html */
  458. dl = xs_html_tag("dl", NULL)));
  459. if (sdesc && *sdesc) {
  460. xs_html_add(dl,
  461. xs_html_tag("di",
  462. xs_html_tag("dt",
  463. xs_html_text(L("Site description"))),
  464. xs_html_tag("dd",
  465. xs_html_text(sdesc))));
  466. }
  467. if (email && *email) {
  468. xs *mailto = xs_fmt("mailto:%s", email);
  469. xs_html_add(dl,
  470. xs_html_tag("di",
  471. xs_html_tag("dt",
  472. xs_html_text(L("Admin email"))),
  473. xs_html_tag("dd",
  474. xs_html_tag("a",
  475. xs_html_attr("href", mailto),
  476. xs_html_text(email)))));
  477. }
  478. if (acct && *acct) {
  479. xs *url = xs_fmt("%s/%s", srv_baseurl, acct);
  480. xs *handle = xs_fmt("@%s@%s", acct, host);
  481. xs_html_add(dl,
  482. xs_html_tag("di",
  483. xs_html_tag("dt",
  484. xs_html_text(L("Admin account"))),
  485. xs_html_tag("dd",
  486. xs_html_tag("a",
  487. xs_html_attr("href", url),
  488. xs_html_text(handle)))));
  489. }
  490. return body;
  491. }
  492. xs_html *html_user_head(snac *user, const char *desc, const char *url)
  493. {
  494. xs_html *head = html_base_head();
  495. /* add the user CSS */
  496. {
  497. xs *css = NULL;
  498. int size;
  499. /* try to open the user css */
  500. if (!valid_status(static_get(user, "style.css", &css, &size, NULL, NULL))) {
  501. /* it's not there; try to open the server-wide css */
  502. FILE *f;
  503. xs *g_css_fn = xs_fmt("%s/style.css", srv_basedir);
  504. if ((f = fopen(g_css_fn, "r")) != NULL) {
  505. css = xs_readall(f);
  506. fclose(f);
  507. }
  508. }
  509. if (css != NULL) {
  510. xs_html_add(head,
  511. xs_html_tag("style",
  512. xs_html_raw(css)));
  513. }
  514. }
  515. /* title */
  516. xs *title = xs_fmt("%s (@%s@%s)", xs_dict_get(user->config, "name"),
  517. user->uid, xs_dict_get(srv_config, "host"));
  518. xs_html_add(head,
  519. xs_html_tag("title",
  520. xs_html_text(title)));
  521. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  522. if (avatar == NULL || *avatar == '\0') {
  523. xs_free(avatar);
  524. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  525. }
  526. /* create a description field */
  527. xs *s_desc = NULL;
  528. int n;
  529. if (desc == NULL)
  530. s_desc = xs_dup(xs_dict_get(user->config, "bio"));
  531. else
  532. s_desc = xs_dup(desc);
  533. /* show metrics in og:description? */
  534. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  535. xs *fwers = follower_list(user);
  536. xs *fwing = following_list(user);
  537. xs *s1 = xs_fmt(L("%d following, %d followers · "),
  538. xs_list_len(fwing), xs_list_len(fwers));
  539. s_desc = xs_str_prepend_i(s_desc, s1);
  540. }
  541. /* shorten desc to a reasonable size */
  542. for (n = 0; s_desc[n]; n++) {
  543. if (n > 512 && (s_desc[n] == ' ' || s_desc[n] == '\n'))
  544. break;
  545. }
  546. s_desc[n] = '\0';
  547. /* og properties */
  548. xs_html_add(head,
  549. xs_html_sctag("meta",
  550. xs_html_attr("property", "og:site_name"),
  551. xs_html_attr("content", xs_dict_get(srv_config, "host"))),
  552. xs_html_sctag("meta",
  553. xs_html_attr("property", "og:title"),
  554. xs_html_attr("content", title)),
  555. xs_html_sctag("meta",
  556. xs_html_attr("property", "og:description"),
  557. xs_html_attr("content", s_desc)),
  558. xs_html_sctag("meta",
  559. xs_html_attr("property", "og:image"),
  560. xs_html_attr("content", avatar)),
  561. xs_html_sctag("meta",
  562. xs_html_attr("property", "og:width"),
  563. xs_html_attr("content", "300")),
  564. xs_html_sctag("meta",
  565. xs_html_attr("property", "og:height"),
  566. xs_html_attr("content", "300")));
  567. /* RSS link */
  568. xs *rss_url = xs_fmt("%s.rss", user->actor);
  569. xs_html_add(head,
  570. xs_html_sctag("link",
  571. xs_html_attr("rel", "alternate"),
  572. xs_html_attr("type", "application/rss+xml"),
  573. xs_html_attr("title", "RSS"),
  574. xs_html_attr("href", rss_url)));
  575. /* ActivityPub alternate link (actor id) */
  576. xs_html_add(head,
  577. xs_html_sctag("link",
  578. xs_html_attr("rel", "alternate"),
  579. xs_html_attr("type", "application/activity+json"),
  580. xs_html_attr("href", url ? url : user->actor)));
  581. return head;
  582. }
  583. static xs_html *html_user_body(snac *user, int read_only)
  584. {
  585. const char *proxy = NULL;
  586. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  587. proxy = user->actor;
  588. xs_html *body = xs_html_tag("body", NULL);
  589. /* top nav */
  590. xs_html *top_nav = xs_html_tag("nav",
  591. xs_html_attr("class", "snac-top-nav"));
  592. xs *avatar = xs_dup(xs_dict_get(user->config, "avatar"));
  593. if (avatar == NULL || *avatar == '\0') {
  594. xs_free(avatar);
  595. avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
  596. }
  597. xs_html_add(top_nav,
  598. xs_html_sctag("img",
  599. xs_html_attr("src", avatar),
  600. xs_html_attr("class", "snac-avatar"),
  601. xs_html_attr("alt", "")));
  602. if (read_only) {
  603. xs *rss_url = xs_fmt("%s.rss", user->actor);
  604. xs *admin_url = xs_fmt("%s/admin", user->actor);
  605. xs_html_add(top_nav,
  606. xs_html_tag("a",
  607. xs_html_attr("href", rss_url),
  608. xs_html_text(L("RSS"))),
  609. xs_html_text(" - "),
  610. xs_html_tag("a",
  611. xs_html_attr("href", admin_url),
  612. xs_html_attr("rel", "nofollow"),
  613. xs_html_text(L("private"))));
  614. }
  615. else {
  616. int n_len = notify_new_num(user);
  617. xs_html *notify_count = NULL;
  618. /* show the number of new notifications, if there are any */
  619. if (n_len) {
  620. xs *n_len_str = xs_fmt(" %d ", n_len);
  621. notify_count = xs_html_tag("sup",
  622. xs_html_attr("style", "background-color: red; color: white;"),
  623. xs_html_text(n_len_str));
  624. }
  625. else
  626. notify_count = xs_html_text("");
  627. xs *admin_url = xs_fmt("%s/admin", user->actor);
  628. xs *notify_url = xs_fmt("%s/notifications", user->actor);
  629. xs *people_url = xs_fmt("%s/people", user->actor);
  630. xs *instance_url = xs_fmt("%s/instance", user->actor);
  631. xs_html_add(top_nav,
  632. xs_html_tag("a",
  633. xs_html_attr("href", user->actor),
  634. xs_html_text(L("public"))),
  635. xs_html_text(" - "),
  636. xs_html_tag("a",
  637. xs_html_attr("href", admin_url),
  638. xs_html_text(L("private"))),
  639. xs_html_text(" - "),
  640. xs_html_tag("a",
  641. xs_html_attr("href", notify_url),
  642. xs_html_text(L("notifications"))),
  643. notify_count,
  644. xs_html_text(" - "),
  645. xs_html_tag("a",
  646. xs_html_attr("href", people_url),
  647. xs_html_text(L("people"))),
  648. xs_html_text(" - "),
  649. xs_html_tag("a",
  650. xs_html_attr("href", instance_url),
  651. xs_html_text(L("instance"))),
  652. xs_html_text(" "),
  653. xs_html_tag("form",
  654. xs_html_attr("style", "display: inline!important"),
  655. xs_html_attr("class", "snac-search-box"),
  656. xs_html_attr("action", admin_url),
  657. xs_html_sctag("input",
  658. xs_html_attr("type", "text"),
  659. xs_html_attr("name", "q"),
  660. xs_html_attr("title", L("Search posts by URL or content (regular expression), @user@host accounts, or #tag")),
  661. xs_html_attr("placeholder", L("Content search")))));
  662. }
  663. xs_html_add(body,
  664. top_nav);
  665. /* user info */
  666. xs_html *top_user = xs_html_tag("div",
  667. xs_html_attr("class", "h-card snac-top-user"));
  668. if (read_only) {
  669. const char *header = xs_dict_get(user->config, "header");
  670. if (header && *header) {
  671. xs_html_add(top_user,
  672. xs_html_tag("div",
  673. xs_html_attr("class", "snac-top-user-banner"),
  674. xs_html_attr("style", "clear: both"),
  675. xs_html_sctag("br", NULL),
  676. xs_html_sctag("img",
  677. xs_html_attr("src", header))));
  678. }
  679. }
  680. xs *handle = xs_fmt("@%s@%s",
  681. xs_dict_get(user->config, "uid"),
  682. xs_dict_get(srv_config, "host"));
  683. xs_html_add(top_user,
  684. xs_html_tag("p",
  685. xs_html_attr("class", "p-name snac-top-user-name"),
  686. xs_html_text(xs_dict_get(user->config, "name"))),
  687. xs_html_tag("p",
  688. xs_html_attr("class", "snac-top-user-id"),
  689. xs_html_text(handle)));
  690. /** instance announcement **/
  691. if (!read_only) {
  692. double la = 0.0;
  693. xs *user_la = xs_dup(xs_dict_get(user->config, "last_announcement"));
  694. if (user_la != NULL)
  695. la = xs_number_get(user_la);
  696. const t_announcement *an = announcement(la);
  697. if (an != NULL && (an->text != NULL)) {
  698. xs *s = xs_fmt("?da=%.0f", an->timestamp);
  699. xs_html_add(top_user, xs_html_tag("div",
  700. xs_html_attr("class", "snac-announcement"),
  701. xs_html_text(an->text),
  702. xs_html_text(" "),
  703. xs_html_tag("a",
  704. xs_html_attr("href", s),
  705. xs_html_text("Dismiss"))));
  706. }
  707. }
  708. if (read_only) {
  709. xs *tags = xs_list_new();
  710. xs *bio1 = not_really_markdown(xs_dict_get(user->config, "bio"), NULL, &tags);
  711. xs *bio2 = process_tags(user, bio1, &tags);
  712. xs *bio3 = sanitize(bio2);
  713. bio3 = replace_shortnames(bio3, tags, 2, proxy);
  714. xs_html *top_user_bio = xs_html_tag("div",
  715. xs_html_attr("class", "p-note snac-top-user-bio"),
  716. xs_html_raw(bio3)); /* already sanitized */
  717. xs_html_add(top_user,
  718. top_user_bio);
  719. xs *metadata = NULL;
  720. const xs_dict *md = xs_dict_get(user->config, "metadata");
  721. if (xs_type(md) == XSTYPE_DICT)
  722. metadata = xs_dup(md);
  723. else
  724. if (xs_type(md) == XSTYPE_STRING) {
  725. /* convert to dict for easier iteration */
  726. metadata = xs_dict_new();
  727. xs *l = xs_split(md, "\n");
  728. const char *ll;
  729. xs_list_foreach(l, ll) {
  730. xs *kv = xs_split_n(ll, "=", 1);
  731. const char *k = xs_list_get(kv, 0);
  732. const char *v = xs_list_get(kv, 1);
  733. if (k && v) {
  734. xs *kk = xs_strip_i(xs_dup(k));
  735. xs *vv = xs_strip_i(xs_dup(v));
  736. metadata = xs_dict_set(metadata, kk, vv);
  737. }
  738. }
  739. }
  740. if (xs_type(metadata) == XSTYPE_DICT) {
  741. const xs_str *k;
  742. const xs_str *v;
  743. xs_dict *val_links = user->links;
  744. if (xs_is_null(val_links))
  745. val_links = xs_stock(XSTYPE_DICT);
  746. xs_html *snac_metadata = xs_html_tag("div",
  747. xs_html_attr("class", "snac-metadata"));
  748. int c = 0;
  749. while (xs_dict_next(metadata, &k, &v, &c)) {
  750. xs_html *value;
  751. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
  752. /* is this link validated? */
  753. xs *verified_link = NULL;
  754. const xs_number *val_time = xs_dict_get(val_links, v);
  755. if (xs_type(val_time) == XSTYPE_NUMBER) {
  756. time_t t = xs_number_get(val_time);
  757. if (t > 0) {
  758. xs *s1 = xs_str_utctime(t, ISO_DATE_SPEC);
  759. verified_link = xs_fmt("%s (%s)", L("verified link"), s1);
  760. }
  761. }
  762. if (!xs_is_null(verified_link)) {
  763. value = xs_html_tag("span",
  764. xs_html_attr("title", verified_link),
  765. xs_html_raw("✔ "),
  766. xs_html_tag("a",
  767. xs_html_attr("rel", "me"),
  768. xs_html_attr("href", v),
  769. xs_html_text(v)));
  770. }
  771. else {
  772. value = xs_html_tag("a",
  773. xs_html_attr("rel", "me"),
  774. xs_html_attr("href", v),
  775. xs_html_text(v));
  776. }
  777. }
  778. else
  779. if (xs_startswith(v, "gemini:/") || xs_startswith(v, "xmpp:")) {
  780. value = xs_html_tag("a",
  781. xs_html_attr("rel", "me"),
  782. xs_html_attr("href", v),
  783. xs_html_text(v));
  784. }
  785. else
  786. value = xs_html_text(v);
  787. xs_html_add(snac_metadata,
  788. xs_html_tag("span",
  789. xs_html_attr("class", "snac-property-name"),
  790. xs_html_text(k)),
  791. xs_html_text(":"),
  792. xs_html_raw(" "),
  793. xs_html_tag("span",
  794. xs_html_attr("class", "snac-property-value"),
  795. value),
  796. xs_html_sctag("br", NULL));
  797. }
  798. xs_html_add(top_user,
  799. snac_metadata);
  800. }
  801. if (xs_is_true(xs_dict_get(user->config, "show_contact_metrics"))) {
  802. xs *fwers = follower_list(user);
  803. xs *fwing = following_list(user);
  804. xs *s1 = xs_fmt(L("%d following %d followers"),
  805. xs_list_len(fwing), xs_list_len(fwers));
  806. xs_html_add(top_user,
  807. xs_html_tag("p",
  808. xs_html_text(s1)));
  809. }
  810. }
  811. xs_html_add(body,
  812. top_user);
  813. return body;
  814. }
  815. xs_html *html_top_controls(snac *snac)
  816. /* generates the top controls */
  817. {
  818. xs *ops_action = xs_fmt("%s/admin/action", snac->actor);
  819. xs_html *top_controls = xs_html_tag("div",
  820. xs_html_attr("class", "snac-top-controls"),
  821. /** new post **/
  822. html_note(snac, L("New Post..."),
  823. "new_post_div", "new_post_form",
  824. L("What's on your mind?"), "",
  825. NULL, NULL,
  826. xs_stock(XSTYPE_FALSE), "",
  827. xs_stock(XSTYPE_FALSE), NULL,
  828. NULL, 1, "", "", 0),
  829. /** operations **/
  830. xs_html_tag("details",
  831. xs_html_tag("summary",
  832. xs_html_text(L("Operations..."))),
  833. xs_html_tag("p", NULL),
  834. xs_html_tag("form",
  835. xs_html_attr("autocomplete", "off"),
  836. xs_html_attr("method", "post"),
  837. xs_html_attr("action", ops_action),
  838. xs_html_sctag("input",
  839. xs_html_attr("type", "text"),
  840. xs_html_attr("name", "actor"),
  841. xs_html_attr("required", "required"),
  842. xs_html_attr("placeholder", "bob@example.com")),
  843. xs_html_text(" "),
  844. xs_html_sctag("input",
  845. xs_html_attr("type", "submit"),
  846. xs_html_attr("name", "action"),
  847. xs_html_attr("value", L("Follow"))),
  848. xs_html_text(" "),
  849. xs_html_text(L("(by URL or user@host)"))),
  850. xs_html_tag("p", NULL),
  851. xs_html_tag("form",
  852. xs_html_attr("autocomplete", "off"),
  853. xs_html_attr("method", "post"),
  854. xs_html_attr("action", ops_action),
  855. xs_html_sctag("input",
  856. xs_html_attr("type", "url"),
  857. xs_html_attr("name", "id"),
  858. xs_html_attr("required", "required"),
  859. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  860. xs_html_text(" "),
  861. xs_html_sctag("input",
  862. xs_html_attr("type", "submit"),
  863. xs_html_attr("name", "action"),
  864. xs_html_attr("value", L("Boost"))),
  865. xs_html_text(" "),
  866. xs_html_text(L("(by URL)"))),
  867. xs_html_tag("p", NULL),
  868. xs_html_tag("form",
  869. xs_html_attr("autocomplete", "off"),
  870. xs_html_attr("method", "post"),
  871. xs_html_attr("action", ops_action),
  872. xs_html_sctag("input",
  873. xs_html_attr("type", "text"),
  874. xs_html_attr("name", "id"),
  875. xs_html_attr("required", "required"),
  876. xs_html_attr("placeholder", "https:/" "/fedi.example.com/bob/...")),
  877. xs_html_text(" "),
  878. xs_html_sctag("input",
  879. xs_html_attr("type", "submit"),
  880. xs_html_attr("name", "action"),
  881. xs_html_attr("value", L("Like"))),
  882. xs_html_text(" "),
  883. xs_html_text(L("(by URL)"))),
  884. xs_html_tag("p", NULL)));
  885. /** user settings **/
  886. const char *email = "[disabled by admin]";
  887. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  888. email = xs_dict_get(snac->config_o, "email");
  889. if (xs_is_null(email)) {
  890. email = xs_dict_get(snac->config, "email");
  891. if (xs_is_null(email))
  892. email = "";
  893. }
  894. }
  895. const char *cw = xs_dict_get(snac->config, "cw");
  896. if (xs_is_null(cw))
  897. cw = "";
  898. const char *telegram_bot = xs_dict_get(snac->config, "telegram_bot");
  899. if (xs_is_null(telegram_bot))
  900. telegram_bot = "";
  901. const char *telegram_chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  902. if (xs_is_null(telegram_chat_id))
  903. telegram_chat_id = "";
  904. const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
  905. if (xs_is_null(ntfy_server))
  906. ntfy_server = "";
  907. const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
  908. if (xs_is_null(ntfy_token))
  909. ntfy_token = "";
  910. const char *purge_days = xs_dict_get(snac->config, "purge_days");
  911. if (!xs_is_null(purge_days) && xs_type(purge_days) == XSTYPE_NUMBER)
  912. purge_days = (char *)xs_number_str(purge_days);
  913. else
  914. purge_days = "0";
  915. const xs_val *d_dm_f_u = xs_dict_get(snac->config, "drop_dm_from_unknown");
  916. const xs_val *bot = xs_dict_get(snac->config, "bot");
  917. const xs_val *a_private = xs_dict_get(snac->config, "private");
  918. const xs_val *auto_boost = xs_dict_get(snac->config, "auto_boost");
  919. const xs_val *coll_thrds = xs_dict_get(snac->config, "collapse_threads");
  920. const xs_val *pending = xs_dict_get(snac->config, "approve_followers");
  921. const xs_val *show_foll = xs_dict_get(snac->config, "show_contact_metrics");
  922. xs *metadata = NULL;
  923. const xs_dict *md = xs_dict_get(snac->config, "metadata");
  924. if (xs_type(md) == XSTYPE_DICT) {
  925. const xs_str *k;
  926. const xs_str *v;
  927. metadata = xs_str_new(NULL);
  928. xs_dict_foreach(md, k, v) {
  929. xs *kp = xs_fmt("%s=%s", k, v);
  930. if (*metadata)
  931. metadata = xs_str_cat(metadata, "\n");
  932. metadata = xs_str_cat(metadata, kp);
  933. }
  934. }
  935. else
  936. if (xs_type(md) == XSTYPE_STRING)
  937. metadata = xs_dup(md);
  938. else
  939. metadata = xs_str_new(NULL);
  940. xs *user_setup_action = xs_fmt("%s/admin/user-setup", snac->actor);
  941. xs_html_add(top_controls,
  942. xs_html_tag("details",
  943. xs_html_tag("summary",
  944. xs_html_text(L("User Settings..."))),
  945. xs_html_tag("div",
  946. xs_html_attr("class", "snac-user-setup"),
  947. xs_html_tag("form",
  948. xs_html_attr("autocomplete", "off"),
  949. xs_html_attr("method", "post"),
  950. xs_html_attr("action", user_setup_action),
  951. xs_html_attr("enctype", "multipart/form-data"),
  952. xs_html_tag("p",
  953. xs_html_text(L("Display name:")),
  954. xs_html_sctag("br", NULL),
  955. xs_html_sctag("input",
  956. xs_html_attr("type", "text"),
  957. xs_html_attr("name", "name"),
  958. xs_html_attr("value", xs_dict_get(snac->config, "name")),
  959. xs_html_attr("placeholder", L("Your name")))),
  960. xs_html_tag("p",
  961. xs_html_text(L("Avatar: ")),
  962. xs_html_sctag("input",
  963. xs_html_attr("type", "file"),
  964. xs_html_attr("name", "avatar_file"))),
  965. xs_html_tag("p",
  966. xs_html_sctag("input",
  967. xs_html_attr("type", "checkbox"),
  968. xs_html_attr("name", "avatar_delete")),
  969. xs_html_text(L("Delete current avatar"))),
  970. xs_html_tag("p",
  971. xs_html_text(L("Header image (banner): ")),
  972. xs_html_sctag("input",
  973. xs_html_attr("type", "file"),
  974. xs_html_attr("name", "header_file"))),
  975. xs_html_tag("p",
  976. xs_html_sctag("input",
  977. xs_html_attr("type", "checkbox"),
  978. xs_html_attr("name", "header_delete")),
  979. xs_html_text(L("Delete current header image"))),
  980. xs_html_tag("p",
  981. xs_html_text(L("Bio:")),
  982. xs_html_sctag("br", NULL),
  983. xs_html_tag("textarea",
  984. xs_html_attr("name", "bio"),
  985. xs_html_attr("cols", "40"),
  986. xs_html_attr("rows", "4"),
  987. xs_html_attr("placeholder", L("Write about yourself here...")),
  988. xs_html_text(xs_dict_get(snac->config, "bio")))),
  989. xs_html_sctag("input",
  990. xs_html_attr("type", "checkbox"),
  991. xs_html_attr("name", "cw"),
  992. xs_html_attr("id", "cw"),
  993. xs_html_attr(strcmp(cw, "open") == 0 ? "checked" : "", NULL)),
  994. xs_html_tag("label",
  995. xs_html_attr("for", "cw"),
  996. xs_html_text(L("Always show sensitive content"))),
  997. xs_html_tag("p",
  998. xs_html_text(L("Email address for notifications:")),
  999. xs_html_sctag("br", NULL),
  1000. xs_html_sctag("input",
  1001. xs_html_attr("type", "text"),
  1002. xs_html_attr("name", "email"),
  1003. xs_html_attr("value", email),
  1004. xs_html_attr("placeholder", "bob@example.com"))),
  1005. xs_html_tag("p",
  1006. xs_html_text(L("Telegram notifications (bot key and chat id):")),
  1007. xs_html_sctag("br", NULL),
  1008. xs_html_sctag("input",
  1009. xs_html_attr("type", "text"),
  1010. xs_html_attr("name", "telegram_bot"),
  1011. xs_html_attr("value", telegram_bot),
  1012. xs_html_attr("placeholder", "Bot API key")),
  1013. xs_html_text(" "),
  1014. xs_html_sctag("input",
  1015. xs_html_attr("type", "text"),
  1016. xs_html_attr("name", "telegram_chat_id"),
  1017. xs_html_attr("value", telegram_chat_id),
  1018. xs_html_attr("placeholder", "Chat id"))),
  1019. xs_html_tag("p",
  1020. xs_html_text(L("ntfy notifications (ntfy server and token):")),
  1021. xs_html_sctag("br", NULL),
  1022. xs_html_sctag("input",
  1023. xs_html_attr("type", "text"),
  1024. xs_html_attr("name", "ntfy_server"),
  1025. xs_html_attr("value", ntfy_server),
  1026. xs_html_attr("placeholder", "ntfy server - full URL (example: https://ntfy.sh/YourTopic)")),
  1027. xs_html_text(" "),
  1028. xs_html_sctag("input",
  1029. xs_html_attr("type", "text"),
  1030. xs_html_attr("name", "ntfy_token"),
  1031. xs_html_attr("value", ntfy_token),
  1032. xs_html_attr("placeholder", "ntfy token - if needed"))),
  1033. xs_html_tag("p",
  1034. xs_html_text(L("Maximum days to keep posts (0: server settings):")),
  1035. xs_html_sctag("br", NULL),
  1036. xs_html_sctag("input",
  1037. xs_html_attr("type", "number"),
  1038. xs_html_attr("name", "purge_days"),
  1039. xs_html_attr("value", purge_days))),
  1040. xs_html_tag("p",
  1041. xs_html_sctag("input",
  1042. xs_html_attr("type", "checkbox"),
  1043. xs_html_attr("name", "drop_dm_from_unknown"),
  1044. xs_html_attr("id", "drop_dm_from_unknown"),
  1045. xs_html_attr(xs_type(d_dm_f_u) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1046. xs_html_tag("label",
  1047. xs_html_attr("for", "drop_dm_from_unknown"),
  1048. xs_html_text(L("Drop direct messages from people you don't follow")))),
  1049. xs_html_tag("p",
  1050. xs_html_sctag("input",
  1051. xs_html_attr("type", "checkbox"),
  1052. xs_html_attr("name", "bot"),
  1053. xs_html_attr("id", "bot"),
  1054. xs_html_attr(xs_type(bot) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1055. xs_html_tag("label",
  1056. xs_html_attr("for", "bot"),
  1057. xs_html_text(L("This account is a bot")))),
  1058. xs_html_tag("p",
  1059. xs_html_sctag("input",
  1060. xs_html_attr("type", "checkbox"),
  1061. xs_html_attr("name", "auto_boost"),
  1062. xs_html_attr("id", "auto_boost"),
  1063. xs_html_attr(xs_is_true(auto_boost) ? "checked" : "", NULL)),
  1064. xs_html_tag("label",
  1065. xs_html_attr("for", "auto_boost"),
  1066. xs_html_text(L("Auto-boost all mentions to this account")))),
  1067. xs_html_tag("p",
  1068. xs_html_sctag("input",
  1069. xs_html_attr("type", "checkbox"),
  1070. xs_html_attr("name", "private"),
  1071. xs_html_attr("id", "private"),
  1072. xs_html_attr(xs_type(a_private) == XSTYPE_TRUE ? "checked" : "", NULL)),
  1073. xs_html_tag("label",
  1074. xs_html_attr("for", "private"),
  1075. xs_html_text(L("This account is private "
  1076. "(posts are not shown through the web)")))),
  1077. xs_html_tag("p",
  1078. xs_html_sctag("input",
  1079. xs_html_attr("type", "checkbox"),
  1080. xs_html_attr("name", "collapse_threads"),
  1081. xs_html_attr("id", "collapse_threads"),
  1082. xs_html_attr(xs_is_true(coll_thrds) ? "checked" : "", NULL)),
  1083. xs_html_tag("label",
  1084. xs_html_attr("for", "collapse_threads"),
  1085. xs_html_text(L("Collapse top threads by default")))),
  1086. xs_html_tag("p",
  1087. xs_html_sctag("input",
  1088. xs_html_attr("type", "checkbox"),
  1089. xs_html_attr("name", "approve_followers"),
  1090. xs_html_attr("id", "approve_followers"),
  1091. xs_html_attr(xs_is_true(pending) ? "checked" : "", NULL)),
  1092. xs_html_tag("label",
  1093. xs_html_attr("for", "approve_followers"),
  1094. xs_html_text(L("Follow requests must be approved")))),
  1095. xs_html_tag("p",
  1096. xs_html_sctag("input",
  1097. xs_html_attr("type", "checkbox"),
  1098. xs_html_attr("name", "show_contact_metrics"),
  1099. xs_html_attr("id", "show_contact_metrics"),
  1100. xs_html_attr(xs_is_true(show_foll) ? "checked" : "", NULL)),
  1101. xs_html_tag("label",
  1102. xs_html_attr("for", "show_contact_metrics"),
  1103. xs_html_text(L("Publish follower and following metrics")))),
  1104. xs_html_tag("p",
  1105. xs_html_text(L("Profile metadata (key=value pairs in each line):")),
  1106. xs_html_sctag("br", NULL),
  1107. xs_html_tag("textarea",
  1108. xs_html_attr("name", "metadata"),
  1109. xs_html_attr("cols", "40"),
  1110. xs_html_attr("rows", "4"),
  1111. xs_html_attr("placeholder", "Blog=https:/"
  1112. "/example.com/my-blog\nGPG Key=1FA54\n..."),
  1113. xs_html_text(metadata))),
  1114. xs_html_tag("p",
  1115. xs_html_text(L("New password:")),
  1116. xs_html_sctag("br", NULL),
  1117. xs_html_sctag("input",
  1118. xs_html_attr("type", "password"),
  1119. xs_html_attr("name", "passwd1"),
  1120. xs_html_attr("value", ""))),
  1121. xs_html_tag("p",
  1122. xs_html_text(L("Repeat new password:")),
  1123. xs_html_sctag("br", NULL),
  1124. xs_html_sctag("input",
  1125. xs_html_attr("type", "password"),
  1126. xs_html_attr("name", "passwd2"),
  1127. xs_html_attr("value", ""))),
  1128. xs_html_sctag("input",
  1129. xs_html_attr("type", "submit"),
  1130. xs_html_attr("class", "button"),
  1131. xs_html_attr("value", L("Update user info")))))));
  1132. return top_controls;
  1133. }
  1134. static xs_html *html_button(char *clss, char *label, char *hint)
  1135. {
  1136. xs *c = xs_fmt("snac-btn-%s", clss);
  1137. /* use an NULL tag to separate non-css-classed buttons from one another */
  1138. return xs_html_container(
  1139. xs_html_sctag("input",
  1140. xs_html_attr("type", "submit"),
  1141. xs_html_attr("name", "action"),
  1142. xs_html_attr("class", c),
  1143. xs_html_attr("value", label),
  1144. xs_html_attr("title", hint)),
  1145. xs_html_text("\n"));
  1146. }
  1147. xs_str *build_mentions(snac *snac, const xs_dict *msg)
  1148. /* returns a string with the mentions in msg */
  1149. {
  1150. xs_str *s = xs_str_new(NULL);
  1151. const char *list = xs_dict_get(msg, "tag");
  1152. const char *v;
  1153. int c = 0;
  1154. while (xs_list_next(list, &v, &c)) {
  1155. const char *type = xs_dict_get(v, "type");
  1156. const char *href = xs_dict_get(v, "href");
  1157. const char *name = xs_dict_get(v, "name");
  1158. if (type && strcmp(type, "Mention") == 0 &&
  1159. href && strcmp(href, snac->actor) != 0 && name) {
  1160. xs *s1 = NULL;
  1161. if (name[0] != '@') {
  1162. s1 = xs_fmt("@%s", name);
  1163. name = s1;
  1164. }
  1165. xs *l = xs_split(name, "@");
  1166. /* is it a name without a host? */
  1167. if (xs_list_len(l) < 3) {
  1168. /* split the href and pick the host name LIKE AN ANIMAL */
  1169. /* would be better to query the webfinger but *won't do that* here */
  1170. xs *l2 = xs_split(href, "/");
  1171. if (xs_list_len(l2) >= 3) {
  1172. xs *s1 = xs_fmt("%s@%s ", name, xs_list_get(l2, 2));
  1173. if (xs_str_in(s, s1) == -1)
  1174. s = xs_str_cat(s, s1);
  1175. }
  1176. }
  1177. else {
  1178. if (xs_str_in(s, name) == -1) {
  1179. s = xs_str_cat(s, name);
  1180. s = xs_str_cat(s, " ");
  1181. }
  1182. }
  1183. }
  1184. }
  1185. if (*s) {
  1186. xs *s1 = s;
  1187. s = xs_fmt("\n\n\nCC: %s", s1);
  1188. }
  1189. return s;
  1190. }
  1191. xs_html *html_entry_controls(snac *snac, const char *actor,
  1192. const xs_dict *msg, const char *md5)
  1193. {
  1194. const char *id = xs_dict_get(msg, "id");
  1195. const char *group = xs_dict_get(msg, "audience");
  1196. xs *likes = object_likes(id);
  1197. xs *boosts = object_announces(id);
  1198. xs *action = xs_fmt("%s/admin/action", snac->actor);
  1199. xs *redir = xs_fmt("%s_entry", md5);
  1200. xs_html *form;
  1201. xs_html *controls = xs_html_tag("div",
  1202. xs_html_attr("class", "snac-controls"),
  1203. form = xs_html_tag("form",
  1204. xs_html_attr("autocomplete", "off"),
  1205. xs_html_attr("method", "post"),
  1206. xs_html_attr("action", action),
  1207. xs_html_sctag("input",
  1208. xs_html_attr("type", "hidden"),
  1209. xs_html_attr("name", "id"),
  1210. xs_html_attr("value", id)),
  1211. xs_html_sctag("input",
  1212. xs_html_attr("type", "hidden"),
  1213. xs_html_attr("name", "actor"),
  1214. xs_html_attr("value", actor)),
  1215. xs_html_sctag("input",
  1216. xs_html_attr("type", "hidden"),
  1217. xs_html_attr("name", "group"),
  1218. xs_html_attr("value", xs_is_null(group) ? "" : group)),
  1219. xs_html_sctag("input",
  1220. xs_html_attr("type", "hidden"),
  1221. xs_html_attr("name", "redir"),
  1222. xs_html_attr("value", redir))));
  1223. if (!xs_startswith(id, snac->actor)) {
  1224. if (xs_list_in(likes, snac->md5) == -1) {
  1225. /* not already liked; add button */
  1226. xs_html_add(form,
  1227. html_button("like", L("Like"), L("Say you like this post")));
  1228. }
  1229. else {
  1230. /* not like it anymore */
  1231. xs_html_add(form,
  1232. html_button("unlike", L("Unlike"), L("Nah don't like it that much")));
  1233. }
  1234. }
  1235. else {
  1236. if (is_pinned(snac, id))
  1237. xs_html_add(form,
  1238. html_button("unpin", L("Unpin"), L("Unpin this post from your timeline")));
  1239. else
  1240. xs_html_add(form,
  1241. html_button("pin", L("Pin"), L("Pin this post to the top of your timeline")));
  1242. }
  1243. if (is_msg_public(msg)) {
  1244. if (xs_list_in(boosts, snac->md5) == -1) {
  1245. /* not already boosted; add button */
  1246. xs_html_add(form,
  1247. html_button("boost", L("Boost"), L("Announce this post to your followers")));
  1248. }
  1249. else {
  1250. /* already boosted; add button to regret */
  1251. xs_html_add(form,
  1252. html_button("unboost", L("Unboost"), L("I regret I boosted this")));
  1253. }
  1254. }
  1255. if (is_bookmarked(snac, id))
  1256. xs_html_add(form,
  1257. html_button("unbookmark", L("Unbookmark"), L("Delete this post from your bookmarks")));
  1258. else
  1259. xs_html_add(form,
  1260. html_button("bookmark", L("Bookmark"), L("Add this post to your bookmarks")));
  1261. if (strcmp(actor, snac->actor) != 0) {
  1262. /* controls for other actors than this one */
  1263. if (following_check(snac, actor)) {
  1264. xs_html_add(form,
  1265. html_button("unfollow", L("Unfollow"), L("Stop following this user's activity")));
  1266. }
  1267. else {
  1268. xs_html_add(form,
  1269. html_button("follow", L("Follow"), L("Start following this user's activity")));
  1270. }
  1271. if (!xs_is_null(group)) {
  1272. if (following_check(snac, group)) {
  1273. xs_html_add(form,
  1274. html_button("unfollow", L("Unfollow Group"),
  1275. L("Stop following this group or channel")));
  1276. }
  1277. else {
  1278. xs_html_add(form,
  1279. html_button("follow", L("Follow Group"),
  1280. L("Start following this group or channel")));
  1281. }
  1282. }
  1283. xs_html_add(form,
  1284. html_button("mute", L("MUTE"),
  1285. L("Block any activity from this user forever")));
  1286. }
  1287. if (!xs_is_true(xs_dict_get(srv_config, "hide_delete_post_button")))
  1288. xs_html_add(form,
  1289. html_button("delete", L("Delete"), L("Delete this post")));
  1290. xs_html_add(form,
  1291. html_button("hide", L("Hide"), L("Hide this post and its children")));
  1292. const char *prev_src = xs_dict_get(msg, "sourceContent");
  1293. if (!xs_is_null(prev_src) && strcmp(actor, snac->actor) == 0) { /** edit **/
  1294. /* post can be edited */
  1295. xs *div_id = xs_fmt("%s_edit", md5);
  1296. xs *form_id = xs_fmt("%s_edit_form", md5);
  1297. xs *redir = xs_fmt("%s_entry", md5);
  1298. const char *att_file = "";
  1299. const char *att_alt_text = "";
  1300. const xs_list *att_list = xs_dict_get(msg, "attachment");
  1301. /* does it have an attachment? */
  1302. if (xs_type(att_list) == XSTYPE_LIST && xs_list_len(att_list)) {
  1303. const xs_dict *d = xs_list_get(att_list, 0);
  1304. if (xs_type(d) == XSTYPE_DICT) {
  1305. att_file = xs_dict_get_def(d, "url", "");
  1306. att_alt_text = xs_dict_get_def(d, "name", "");
  1307. }
  1308. }
  1309. xs_html_add(controls, xs_html_tag("div",
  1310. xs_html_tag("p", NULL),
  1311. html_note(snac, L("Edit..."),
  1312. div_id, form_id,
  1313. "", prev_src,
  1314. id, NULL,
  1315. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1316. xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
  1317. NULL, 0, att_file, att_alt_text, is_draft(snac, id))),
  1318. xs_html_tag("p", NULL));
  1319. }
  1320. { /** reply **/
  1321. /* the post textarea */
  1322. xs *ct = build_mentions(snac, msg);
  1323. xs *div_id = xs_fmt("%s_reply", md5);
  1324. xs *form_id = xs_fmt("%s_reply_form", md5);
  1325. xs *redir = xs_fmt("%s_entry", md5);
  1326. xs_html_add(controls, xs_html_tag("div",
  1327. xs_html_tag("p", NULL),
  1328. html_note(snac, L("Reply..."),
  1329. div_id, form_id,
  1330. "", ct,
  1331. NULL, NULL,
  1332. xs_dict_get(msg, "sensitive"), xs_dict_get(msg, "summary"),
  1333. xs_stock(is_msg_public(msg) ? XSTYPE_FALSE : XSTYPE_TRUE), redir,
  1334. id, 0, "", "", 0)),
  1335. xs_html_tag("p", NULL));
  1336. }
  1337. return controls;
  1338. }
  1339. xs_html *html_entry(snac *user, xs_dict *msg, int read_only,
  1340. int level, const char *md5, int hide_children)
  1341. {
  1342. const char *id = xs_dict_get(msg, "id");
  1343. const char *type = xs_dict_get(msg, "type");
  1344. const char *actor;
  1345. const char *v;
  1346. int has_title = 0;
  1347. int collapse_threads = 0;
  1348. const char *proxy = NULL;
  1349. if (user && !read_only && xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  1350. proxy = user->actor;
  1351. /* do not show non-public messages in the public timeline */
  1352. if ((read_only || !user) && !is_msg_public(msg))
  1353. return NULL;
  1354. if (id && is_instance_blocked(id))
  1355. return NULL;
  1356. if (user && level == 0 && xs_is_true(xs_dict_get(user->config, "collapse_threads")))
  1357. collapse_threads = 1;
  1358. /* hidden? do nothing more for this conversation */
  1359. if (user && is_hidden(user, id)) {
  1360. xs *s1 = xs_fmt("%s_entry", md5);
  1361. /* return just an dummy anchor, to keep position after hitting 'Hide' */
  1362. return xs_html_tag("div",
  1363. xs_html_tag("a",
  1364. xs_html_attr("name", s1)));
  1365. }
  1366. /* avoid too deep nesting, as it may be a loop */
  1367. if (level >= MAX_CONVERSATION_LEVELS)
  1368. return xs_html_tag("mark",
  1369. xs_html_text(L("Truncated (too deep)")));
  1370. if (strcmp(type, "Follow") == 0) {
  1371. return xs_html_tag("div",
  1372. xs_html_attr("class", "snac-post"),
  1373. xs_html_tag("div",
  1374. xs_html_attr("class", "snac-post-header"),
  1375. xs_html_tag("div",
  1376. xs_html_attr("class", "snac-origin"),
  1377. xs_html_text(L("follows you"))),
  1378. html_msg_icon(read_only ? NULL : user, xs_dict_get(msg, "actor"), msg, proxy)));
  1379. }
  1380. else
  1381. if (!xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  1382. /* skip oddities */
  1383. snac_debug(user, 1, xs_fmt("html_entry: ignoring object type '%s' %s", type, id));
  1384. return NULL;
  1385. }
  1386. /* ignore notes with "name", as they are votes to Questions */
  1387. if (strcmp(type, "Note") == 0 && !xs_is_null(xs_dict_get(msg, "name")))
  1388. return NULL;
  1389. /* get the attributedTo */
  1390. if ((actor = get_atto(msg)) == NULL)
  1391. return NULL;
  1392. /* ignore muted morons immediately */
  1393. if (user && is_muted(user, actor)) {
  1394. xs *s1 = xs_fmt("%s_entry", md5);
  1395. /* return just an dummy anchor, to keep position after hitting 'MUTE' */
  1396. return xs_html_tag("div",
  1397. xs_html_tag("a",
  1398. xs_html_attr("name", s1)));
  1399. }
  1400. if ((user == NULL || strcmp(actor, user->actor) != 0)
  1401. && !valid_status(actor_get(actor, NULL)))
  1402. return NULL;
  1403. /** html_entry top tag **/
  1404. xs_html *entry_top = xs_html_tag("div", NULL);
  1405. {
  1406. xs *s1 = xs_fmt("%s_entry", md5);
  1407. xs_html_add(entry_top,
  1408. xs_html_tag("a",
  1409. xs_html_attr("name", s1)));
  1410. }
  1411. xs_html *entry = xs_html_tag("div",
  1412. xs_html_attr("class", level == 0 ? "snac-post" : "snac-child"));
  1413. xs_html_add(entry_top,
  1414. entry);
  1415. /** post header **/
  1416. xs_html *score;
  1417. xs_html *post_header = xs_html_tag("div",
  1418. xs_html_attr("class", "snac-post-header"),
  1419. score = xs_html_tag("div",
  1420. xs_html_attr("class", "snac-score")));
  1421. xs_html_add(entry,
  1422. post_header);
  1423. if (user && is_pinned(user, id)) {
  1424. /* add a pin emoji */
  1425. xs_html_add(score,
  1426. xs_html_tag("span",
  1427. xs_html_attr("title", L("Pinned")),
  1428. xs_html_raw(" &#128204; ")));
  1429. }
  1430. if (user && is_bookmarked(user, id)) {
  1431. /* add a bookmark emoji */
  1432. xs_html_add(score,
  1433. xs_html_tag("span",
  1434. xs_html_attr("title", L("Bookmarked")),
  1435. xs_html_raw(" &#128278; ")));
  1436. }
  1437. if (strcmp(type, "Question") == 0) {
  1438. /* add the ballot box emoji */
  1439. xs_html_add(score,
  1440. xs_html_tag("span",
  1441. xs_html_attr("title", L("Poll")),
  1442. xs_html_raw(" &#128499; ")));
  1443. if (user && was_question_voted(user, id)) {
  1444. /* add a check to show this poll was voted */
  1445. xs_html_add(score,
  1446. xs_html_tag("span",
  1447. xs_html_attr("title", L("Voted")),
  1448. xs_html_raw(" &#10003; ")));
  1449. }
  1450. }
  1451. if (strcmp(type, "Event") == 0) {
  1452. /* add the calendar emoji */
  1453. xs_html_add(score,
  1454. xs_html_tag("span",
  1455. xs_html_attr("title", L("Event")),
  1456. xs_html_raw(" &#128197; ")));
  1457. }
  1458. /* if it's a user from this same instance, add the score */
  1459. if (xs_startswith(id, srv_baseurl)) {
  1460. int n_likes = object_likes_len(id);
  1461. int n_boosts = object_announces_len(id);
  1462. /* alternate emojis: %d &#128077; %d &#128257; */
  1463. xs *s1 = xs_fmt("%d &#9733; %d &#8634;\n", n_likes, n_boosts);
  1464. xs_html_add(score,
  1465. xs_html_raw(s1));
  1466. }
  1467. xs *boosts = object_announces(id);
  1468. if (xs_list_len(boosts)) {
  1469. /* if somebody boosted this, show as origin */
  1470. const char *p = xs_list_get(boosts, -1);
  1471. xs *actor_r = NULL;
  1472. if (user && xs_list_in(boosts, user->md5) != -1) {
  1473. /* we boosted this */
  1474. xs_html_add(post_header,
  1475. xs_html_tag("div",
  1476. xs_html_attr("class", "snac-origin"),
  1477. xs_html_tag("a",
  1478. xs_html_attr("href", user->actor),
  1479. xs_html_text(xs_dict_get(user->config, "name"))),
  1480. xs_html_text(" "),
  1481. xs_html_text(L("boosted"))));
  1482. }
  1483. else
  1484. if (valid_status(object_get_by_md5(p, &actor_r))) {
  1485. xs *name = actor_name(actor_r, proxy);
  1486. if (!xs_is_null(name)) {
  1487. xs *href = NULL;
  1488. const char *id = xs_dict_get(actor_r, "id");
  1489. int fwers = 0;
  1490. int fwing = 0;
  1491. if (user != NULL) {
  1492. fwers = follower_check(user, id);
  1493. fwing = following_check(user, id);
  1494. }
  1495. if (!read_only && (fwers || fwing))
  1496. href = xs_fmt("%s/people#%s", user->actor, p);
  1497. else
  1498. href = xs_dup(id);
  1499. xs_html_add(post_header,
  1500. xs_html_tag("div",
  1501. xs_html_attr("class", "snac-origin"),
  1502. xs_html_tag("a",
  1503. xs_html_attr("href", href),
  1504. xs_html_raw(name)), /* already sanitized */
  1505. xs_html_text(" "),
  1506. xs_html_text(L("boosted"))));
  1507. }
  1508. }
  1509. }
  1510. else
  1511. if (strcmp(type, "Note") == 0) {
  1512. if (level == 0) {
  1513. /* is the parent not here? */
  1514. const char *parent = get_in_reply_to(msg);
  1515. if (user && !xs_is_null(parent) && *parent && !timeline_here(user, parent)) {
  1516. xs_html_add(post_header,
  1517. xs_html_tag("div",
  1518. xs_html_attr("class", "snac-origin"),
  1519. xs_html_text(L("in reply to")),
  1520. xs_html_text(" "),
  1521. xs_html_tag("a",
  1522. xs_html_attr("href", parent),
  1523. xs_html_text("»"))));
  1524. }
  1525. }
  1526. }
  1527. xs_html_add(post_header,
  1528. html_msg_icon(read_only ? NULL : user, actor, msg, proxy));
  1529. /** post content **/
  1530. xs_html *snac_content_wrap = xs_html_tag("div",
  1531. xs_html_attr("class", "e-content snac-content"));
  1532. xs_html_add(entry,
  1533. snac_content_wrap);
  1534. if (!has_title && !xs_is_null(v = xs_dict_get(msg, "name"))) {
  1535. xs_html_add(snac_content_wrap,
  1536. xs_html_tag("h3",
  1537. xs_html_attr("class", "snac-entry-title"),
  1538. xs_html_text(v)));
  1539. has_title = 1;
  1540. }
  1541. xs_html *snac_content = NULL;
  1542. v = xs_dict_get(msg, "summary");
  1543. /* is it sensitive? */
  1544. if (xs_type(xs_dict_get(msg, "sensitive")) == XSTYPE_TRUE) {
  1545. if (xs_is_null(v) || *v == '\0')
  1546. v = "...";
  1547. const char *cw = "";
  1548. if (user) {
  1549. /* only show it when not in the public timeline and the config setting is "open" */
  1550. cw = xs_dict_get(user->config, "cw");
  1551. if (xs_is_null(cw) || read_only)
  1552. cw = "";
  1553. }
  1554. snac_content = xs_html_tag("details",
  1555. xs_html_attr(cw, NULL),
  1556. xs_html_tag("summary",
  1557. xs_html_text(v),
  1558. xs_html_text(L(" [SENSITIVE CONTENT]"))));
  1559. }
  1560. else {
  1561. /* print the summary as a header (sites like e.g. Friendica can contain one) */
  1562. if (!has_title && !xs_is_null(v) && *v) {
  1563. xs_html_add(snac_content_wrap,
  1564. xs_html_tag("h3",
  1565. xs_html_attr("class", "snac-entry-title"),
  1566. xs_html_text(v)));
  1567. has_title = 1;
  1568. }
  1569. snac_content = xs_html_tag("div", NULL);
  1570. }
  1571. xs_html_add(snac_content_wrap,
  1572. snac_content);
  1573. {
  1574. /** build the content string **/
  1575. const char *content = xs_dict_get(msg, "content");
  1576. if (xs_type(content) != XSTYPE_STRING) {
  1577. if (!xs_is_null(content))
  1578. srv_archive_error("unexpected_content_xstype",
  1579. "content field type", xs_stock(XSTYPE_DICT), msg);
  1580. content = "";
  1581. }
  1582. /* skip ugly line breaks at the beginning */
  1583. while (xs_startswith(content, "<br>"))
  1584. content += 4;
  1585. xs *c = sanitize(content);
  1586. /* do some tweaks to the content */
  1587. c = xs_replace_i(c, "\r", "");
  1588. while (xs_endswith(c, "<br><br>"))
  1589. c = xs_crop_i(c, 0, -4);
  1590. c = xs_replace_i(c, "<br><br>", "<p>");
  1591. c = xs_str_cat(c, "<p>");
  1592. /* replace the :shortnames: */
  1593. c = replace_shortnames(c, xs_dict_get(msg, "tag"), 2, proxy);
  1594. /* Peertube videos content is in markdown */
  1595. const char *mtype = xs_dict_get(msg, "mediaType");
  1596. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "text/markdown") == 0) {
  1597. /* a full conversion could be better */
  1598. c = xs_replace_i(c, "\r", "");
  1599. c = xs_replace_i(c, "\n", "<br>");
  1600. }
  1601. /* c contains sanitized HTML */
  1602. xs_html_add(snac_content,
  1603. xs_html_raw(c));
  1604. }
  1605. if (strcmp(type, "Question") == 0) { /** question content **/
  1606. const xs_list *oo = xs_dict_get(msg, "oneOf");
  1607. const xs_list *ao = xs_dict_get(msg, "anyOf");
  1608. const xs_list *p;
  1609. const xs_dict *v;
  1610. int closed = 0;
  1611. const char *f_closed = NULL;
  1612. xs_html *poll = xs_html_tag("div", NULL);
  1613. if (read_only)
  1614. closed = 1; /* non-identified page; show as closed */
  1615. else
  1616. if (user && xs_startswith(id, user->actor))
  1617. closed = 1; /* we questioned; closed for us */
  1618. else
  1619. if (user && was_question_voted(user, id))
  1620. closed = 1; /* we already voted; closed for us */
  1621. if ((f_closed = xs_dict_get(msg, "closed")) != NULL) {
  1622. /* it has a closed date... but is it in the past? */
  1623. time_t t0 = time(NULL);
  1624. time_t t1 = xs_parse_iso_date(f_closed, 0);
  1625. if (t1 < t0)
  1626. closed = 2;
  1627. }
  1628. /* get the appropriate list of options */
  1629. p = oo != NULL ? oo : ao;
  1630. if (closed || user == NULL) {
  1631. /* closed poll */
  1632. xs_html *poll_result = xs_html_tag("table",
  1633. xs_html_attr("class", "snac-poll-result"));
  1634. int c = 0;
  1635. while (xs_list_next(p, &v, &c)) {
  1636. const char *name = xs_dict_get(v, "name");
  1637. const xs_dict *replies = xs_dict_get(v, "replies");
  1638. if (name && replies) {
  1639. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  1640. xs_html_add(poll_result,
  1641. xs_html_tag("tr",
  1642. xs_html_tag("td",
  1643. xs_html_text(name),
  1644. xs_html_text(":")),
  1645. xs_html_tag("td",
  1646. xs_html_text(ti))));
  1647. }
  1648. }
  1649. xs_html_add(poll,
  1650. poll_result);
  1651. }
  1652. else {
  1653. /* poll still active */
  1654. xs *vote_action = xs_fmt("%s/admin/vote", user->actor);
  1655. xs_html *form;
  1656. xs_html *poll_form = xs_html_tag("div",
  1657. xs_html_attr("class", "snac-poll-form"),
  1658. form = xs_html_tag("form",
  1659. xs_html_attr("autocomplete", "off"),
  1660. xs_html_attr("method", "post"),
  1661. xs_html_attr("action", vote_action),
  1662. xs_html_sctag("input",
  1663. xs_html_attr("type", "hidden"),
  1664. xs_html_attr("name", "actor"),
  1665. xs_html_attr("value", actor)),
  1666. xs_html_sctag("input",
  1667. xs_html_attr("type", "hidden"),
  1668. xs_html_attr("name", "irt"),
  1669. xs_html_attr("value", id))));
  1670. int c = 0;
  1671. while (xs_list_next(p, &v, &c)) {
  1672. const char *name = xs_dict_get(v, "name");
  1673. const xs_dict *replies = xs_dict_get(v, "replies");
  1674. if (name) {
  1675. char *ti = (char *)xs_number_str(xs_dict_get(replies, "totalItems"));
  1676. xs_html *btn = xs_html_sctag("input",
  1677. xs_html_attr("id", name),
  1678. xs_html_attr("value", name),
  1679. xs_html_attr("name", "question"));
  1680. if (!xs_is_null(oo)) {
  1681. xs_html_add(btn,
  1682. xs_html_attr("type", "radio"),
  1683. xs_html_attr("required", "required"));
  1684. }
  1685. else
  1686. xs_html_add(btn,
  1687. xs_html_attr("type", "checkbox"));
  1688. xs_html_add(form,
  1689. btn,
  1690. xs_html_text(" "),
  1691. xs_html_tag("span",
  1692. xs_html_attr("title", ti),
  1693. xs_html_text(name)),
  1694. xs_html_sctag("br", NULL));
  1695. }
  1696. }
  1697. xs_html_add(form,
  1698. xs_html_tag("p", NULL),
  1699. xs_html_sctag("input",
  1700. xs_html_attr("type", "submit"),
  1701. xs_html_attr("class", "button"),
  1702. xs_html_attr("value", L("Vote"))));
  1703. xs_html_add(poll,
  1704. poll_form);
  1705. }
  1706. /* if it's *really* closed, say it */
  1707. if (closed == 2) {
  1708. xs_html_add(poll,
  1709. xs_html_tag("p",
  1710. xs_html_text(L("Closed"))));
  1711. }
  1712. else {
  1713. /* show when the poll closes */
  1714. const char *end_time = xs_dict_get(msg, "endTime");
  1715. /* Pleroma does not have an endTime field;
  1716. it has a closed time in the future */
  1717. if (xs_is_null(end_time))
  1718. end_time = xs_dict_get(msg, "closed");
  1719. if (!xs_is_null(end_time)) {
  1720. time_t t0 = time(NULL);
  1721. time_t t1 = xs_parse_iso_date(end_time, 0);
  1722. if (t1 > 0 && t1 > t0) {
  1723. time_t diff_time = t1 - t0;
  1724. xs *tf = xs_str_time_diff(diff_time);
  1725. char *p = tf;
  1726. /* skip leading zeros */
  1727. for (; *p == '0' || *p == ':'; p++);
  1728. xs_html_add(poll,
  1729. xs_html_tag("p",
  1730. xs_html_text(L("Closes in")),
  1731. xs_html_text(" "),
  1732. xs_html_text(p)));
  1733. }
  1734. }
  1735. }
  1736. xs_html_add(snac_content,
  1737. poll);
  1738. }
  1739. /** attachments **/
  1740. xs *attach = get_attachments(msg);
  1741. {
  1742. /* make custom css for attachments easier */
  1743. xs_html *content_attachments = xs_html_tag("div",
  1744. xs_html_attr("class", "snac-content-attachments"));
  1745. xs_html_add(snac_content,
  1746. content_attachments);
  1747. const char *content = xs_dict_get(msg, "content");
  1748. int c = 0;
  1749. const xs_dict *a;
  1750. while (xs_list_next(attach, &a, &c)) {
  1751. const char *type = xs_dict_get(a, "type");
  1752. const char *o_href = xs_dict_get(a, "href");
  1753. const char *name = xs_dict_get(a, "name");
  1754. /* if this image is already in the post content, skip */
  1755. if (content && xs_str_in(content, o_href) != -1)
  1756. continue;
  1757. /* do this attachment include an icon? */
  1758. const xs_dict *icon = xs_dict_get(a, "icon");
  1759. if (xs_type(icon) == XSTYPE_DICT) {
  1760. const char *icon_mtype = xs_dict_get(icon, "mediaType");
  1761. const char *icon_url = xs_dict_get(icon, "url");
  1762. if (icon_mtype && icon_url && xs_startswith(icon_mtype, "image/")) {
  1763. xs_html_add(content_attachments,
  1764. xs_html_tag("a",
  1765. xs_html_attr("href", icon_url),
  1766. xs_html_attr("target", "_blank"),
  1767. xs_html_sctag("img",
  1768. xs_html_attr("loading", "lazy"),
  1769. xs_html_attr("src", icon_url))));
  1770. }
  1771. }
  1772. xs *href = make_url(o_href, proxy, 0);
  1773. if (xs_startswith(type, "image/") || strcmp(type, "Image") == 0) {
  1774. xs_html_add(content_attachments,
  1775. xs_html_tag("a",
  1776. xs_html_attr("href", href),
  1777. xs_html_attr("target", "_blank"),
  1778. xs_html_sctag("img",
  1779. xs_html_attr("loading", "lazy"),
  1780. xs_html_attr("src", href),
  1781. xs_html_attr("alt", name),
  1782. xs_html_attr("title", name))));
  1783. }
  1784. else
  1785. if (xs_startswith(type, "video/")) {
  1786. xs_html_add(content_attachments,
  1787. xs_html_tag("video",
  1788. xs_html_attr("preload", "none"),
  1789. xs_html_attr("style", "width: 100%"),
  1790. xs_html_attr("class", "snac-embedded-video"),
  1791. xs_html_attr("controls", NULL),
  1792. xs_html_attr("src", href),
  1793. xs_html_text(L("Video")),
  1794. xs_html_text(": "),
  1795. xs_html_tag("a",
  1796. xs_html_attr("href", href),
  1797. xs_html_text(name))));
  1798. }
  1799. else
  1800. if (xs_startswith(type, "audio/")) {
  1801. xs_html_add(content_attachments,
  1802. xs_html_tag("audio",
  1803. xs_html_attr("preload", "none"),
  1804. xs_html_attr("style", "width: 100%"),
  1805. xs_html_attr("class", "snac-embedded-audio"),
  1806. xs_html_attr("controls", NULL),
  1807. xs_html_attr("src", href),
  1808. xs_html_text(L("Audio")),
  1809. xs_html_text(": "),
  1810. xs_html_tag("a",
  1811. xs_html_attr("href", href),
  1812. xs_html_text(name))));
  1813. }
  1814. else
  1815. if (strcmp(type, "Link") == 0) {
  1816. xs_html_add(content_attachments,
  1817. xs_html_tag("p",
  1818. xs_html_tag("a",
  1819. xs_html_attr("href", o_href),
  1820. xs_html_text(href))));
  1821. /* do not generate an Alt... */
  1822. name = NULL;
  1823. }
  1824. else {
  1825. xs_html_add(content_attachments,
  1826. xs_html_tag("p",
  1827. xs_html_tag("a",
  1828. xs_html_attr("href", o_href),
  1829. xs_html_text(L("Attachment")),
  1830. xs_html_text(": "),
  1831. xs_html_text(o_href))));
  1832. /* do not generate an Alt... */
  1833. name = NULL;
  1834. }
  1835. if (name != NULL && *name) {
  1836. xs_html_add(content_attachments,
  1837. xs_html_tag("p",
  1838. xs_html_attr("class", "snac-alt-text"),
  1839. xs_html_tag("details",
  1840. xs_html_tag("summary",
  1841. xs_html_text(L("Alt..."))),
  1842. xs_html_text(name))));
  1843. }
  1844. }
  1845. }
  1846. /* has this message an audience (i.e., comes from a channel or community)? */
  1847. const char *audience = xs_dict_get(msg, "audience");
  1848. if (strcmp(type, "Page") == 0 && !xs_is_null(audience)) {
  1849. xs_html *au_tag = xs_html_tag("p",
  1850. xs_html_text("("),
  1851. xs_html_tag("a",
  1852. xs_html_attr("href", audience),
  1853. xs_html_attr("title", L("Source channel or community")),
  1854. xs_html_text(audience)),
  1855. xs_html_text(")"));
  1856. xs_html_add(snac_content_wrap,
  1857. au_tag);
  1858. }
  1859. /** controls **/
  1860. if (!read_only && user) {
  1861. xs_html_add(entry,
  1862. html_entry_controls(user, actor, msg, md5));
  1863. }
  1864. /** children **/
  1865. if (!hide_children) {
  1866. xs *children = object_children(id);
  1867. int left = xs_list_len(children);
  1868. if (left) {
  1869. xs_html *ch_details = xs_html_tag("details",
  1870. xs_html_attr(collapse_threads ? "" : "open", NULL),
  1871. xs_html_tag("summary",
  1872. xs_html_text("...")));
  1873. xs_html_add(entry,
  1874. ch_details);
  1875. xs_html *ch_container = xs_html_tag("div",
  1876. xs_html_attr("class", level < 4 ? "snac-children" : "snac-children-too-deep"));
  1877. xs_html_add(ch_details,
  1878. ch_container);
  1879. xs_html *ch_older = NULL;
  1880. if (left > 3) {
  1881. xs_html_add(ch_container,
  1882. ch_older = xs_html_tag("details",
  1883. xs_html_tag("summary",
  1884. xs_html_text(L("Older...")))));
  1885. }
  1886. xs_list *p = children;
  1887. const char *cmd5;
  1888. int cnt = 0;
  1889. int o_cnt = 0;
  1890. while (xs_list_iter(&p, &cmd5)) {
  1891. xs *chd = NULL;
  1892. if (user)
  1893. timeline_get_by_md5(user, cmd5, &chd);
  1894. else
  1895. object_get_by_md5(cmd5, &chd);
  1896. if (chd != NULL) {
  1897. if (xs_is_null(xs_dict_get(chd, "name"))) {
  1898. xs_html *che = html_entry(user, chd, read_only,
  1899. level + 1, cmd5, hide_children);
  1900. if (che != NULL) {
  1901. if (left > 3) {
  1902. xs_html_add(ch_older,
  1903. che);
  1904. o_cnt++;
  1905. }
  1906. else
  1907. xs_html_add(ch_container,
  1908. che);
  1909. cnt++;
  1910. }
  1911. }
  1912. left--;
  1913. }
  1914. else
  1915. srv_debug(2, xs_fmt("cannot read child %s", cmd5));
  1916. }
  1917. /* if no children were finally added, hide the details */
  1918. if (cnt == 0)
  1919. xs_html_add(ch_details,
  1920. xs_html_attr("style", "display: none"));
  1921. if (o_cnt == 0 && ch_older)
  1922. xs_html_add(ch_older,
  1923. xs_html_attr("style", "display: none"));
  1924. }
  1925. }
  1926. return entry_top;
  1927. }
  1928. xs_html *html_footer(void)
  1929. {
  1930. return xs_html_tag("div",
  1931. xs_html_attr("class", "snac-footer"),
  1932. xs_html_tag("a",
  1933. xs_html_attr("href", srv_baseurl),
  1934. xs_html_text(L("about this site"))),
  1935. xs_html_text(" - "),
  1936. xs_html_text(L("powered by ")),
  1937. xs_html_tag("a",
  1938. xs_html_attr("href", WHAT_IS_SNAC_URL),
  1939. xs_html_tag("abbr",
  1940. xs_html_attr("title", "Social Networks Are Crap"),
  1941. xs_html_text("snac"))));
  1942. }
  1943. xs_str *html_timeline(snac *user, const xs_list *list, int read_only,
  1944. int skip, int show, int show_more,
  1945. const char *title, const char *page,
  1946. int utl, const char *error)
  1947. /* returns the HTML for the timeline */
  1948. {
  1949. xs_list *p = (xs_list *)list;
  1950. const char *v;
  1951. double t = ftime();
  1952. int hide_children = xs_is_true(xs_dict_get(srv_config, "strict_public_timelines")) && read_only;
  1953. xs *desc = NULL;
  1954. xs *alternate = NULL;
  1955. if (xs_list_len(list) == 1) {
  1956. /* only one element? pick the description from the source */
  1957. const char *id = xs_list_get(list, 0);
  1958. xs *d = NULL;
  1959. object_get_by_md5(id, &d);
  1960. const char *sc = xs_dict_get(d, "sourceContent");
  1961. if (d && sc != NULL)
  1962. desc = xs_dup(sc);
  1963. alternate = xs_dup(xs_dict_get(d, "id"));
  1964. }
  1965. xs_html *head;
  1966. xs_html *body;
  1967. if (user) {
  1968. head = html_user_head(user, desc, alternate);
  1969. body = html_user_body(user, read_only);
  1970. }
  1971. else {
  1972. head = html_instance_head();
  1973. body = html_instance_body();
  1974. }
  1975. xs_html *html = xs_html_tag("html",
  1976. head,
  1977. body);
  1978. if (user && !read_only)
  1979. xs_html_add(body,
  1980. html_top_controls(user));
  1981. if (error != NULL) {
  1982. xs_html_add(body,
  1983. xs_html_tag("dialog",
  1984. xs_html_attr("open", NULL),
  1985. xs_html_tag("p",
  1986. xs_html_text(error)),
  1987. xs_html_tag("form",
  1988. xs_html_attr("method", "dialog"),
  1989. xs_html_sctag("input",
  1990. xs_html_attr("type", "submit"),
  1991. xs_html_attr("value", L("Dismiss"))))));
  1992. }
  1993. /* show links to the available lists */
  1994. if (user && !read_only) {
  1995. xs_html *lol = xs_html_tag("ul",
  1996. xs_html_attr("class", "snac-list-of-lists"));
  1997. xs_html_add(body, lol);
  1998. xs *lists = list_maint(user, NULL, 0); /* get list of lists */
  1999. int ct = 0;
  2000. const char *v;
  2001. while (xs_list_next(lists, &v, &ct)) {
  2002. const char *lname = xs_list_get(v, 1);
  2003. xs *url = xs_fmt("%s/list/%s", user->actor, xs_list_get(v, 0));
  2004. xs *ttl = xs_fmt(L("Timeline for list '%s'"), lname);
  2005. xs_html_add(lol,
  2006. xs_html_tag("li",
  2007. xs_html_tag("a",
  2008. xs_html_attr("href", url),
  2009. xs_html_attr("class", "snac-list-link"),
  2010. xs_html_attr("title", ttl),
  2011. xs_html_text(lname))));
  2012. }
  2013. {
  2014. /* show the list of pinned posts */
  2015. xs *url = xs_fmt("%s/pinned", user->actor);
  2016. xs_html_add(lol,
  2017. xs_html_tag("li",
  2018. xs_html_tag("a",
  2019. xs_html_attr("href", url),
  2020. xs_html_attr("class", "snac-list-link"),
  2021. xs_html_attr("title", L("Pinned posts")),
  2022. xs_html_text("pinned"))));
  2023. }
  2024. {
  2025. /* show the list of bookmarked posts */
  2026. xs *url = xs_fmt("%s/bookmarks", user->actor);
  2027. xs_html_add(lol,
  2028. xs_html_tag("li",
  2029. xs_html_tag("a",
  2030. xs_html_attr("href", url),
  2031. xs_html_attr("class", "snac-list-link"),
  2032. xs_html_attr("title", L("Bookmarked posts")),
  2033. xs_html_text("bookmarks"))));
  2034. }
  2035. {
  2036. /* show the list of drafts */
  2037. xs *url = xs_fmt("%s/drafts", user->actor);
  2038. xs_html_add(lol,
  2039. xs_html_tag("li",
  2040. xs_html_tag("a",
  2041. xs_html_attr("href", url),
  2042. xs_html_attr("class", "snac-list-link"),
  2043. xs_html_attr("title", L("Post drafts")),
  2044. xs_html_text("drafts"))));
  2045. }
  2046. }
  2047. xs_html_add(body,
  2048. xs_html_tag("a",
  2049. xs_html_attr("name", "snac-posts")));
  2050. xs_html *posts = xs_html_tag("div",
  2051. xs_html_attr("class", "snac-posts"));
  2052. if (title) {
  2053. xs_html_add(posts,
  2054. xs_html_tag("h2",
  2055. xs_html_attr("class", "snac-header"),
  2056. xs_html_text(title)));
  2057. }
  2058. xs_html_add(body,
  2059. posts);
  2060. while (xs_list_iter(&p, &v)) {
  2061. xs *msg = NULL;
  2062. int status;
  2063. if (utl && user && !is_pinned_by_md5(user, v))
  2064. status = timeline_get_by_md5(user, v, &msg);
  2065. else
  2066. status = object_get_by_md5(v, &msg);
  2067. if (!valid_status(status))
  2068. continue;
  2069. /* if it's an instance page, discard messages from private users */
  2070. if (user == NULL && is_msg_from_private_user(msg))
  2071. continue;
  2072. /* is this message a non-public reply? */
  2073. if (user != NULL && !is_msg_public(msg)) {
  2074. const char *irt = get_in_reply_to(msg);
  2075. /* is it a reply to something not in the storage? */
  2076. if (!xs_is_null(irt) && !object_here(irt)) {
  2077. /* is it for me? */
  2078. const xs_list *to = xs_dict_get_def(msg, "to", xs_stock(XSTYPE_LIST));
  2079. const xs_list *cc = xs_dict_get_def(msg, "cc", xs_stock(XSTYPE_LIST));
  2080. if (xs_list_in(to, user->actor) == -1 && xs_list_in(cc, user->actor) == -1) {
  2081. snac_debug(user, 1, xs_fmt("skipping non-public reply to an unknown post %s", v));
  2082. continue;
  2083. }
  2084. }
  2085. }
  2086. xs_html *entry = html_entry(user, msg, read_only, 0, v, (user && !hide_children) ? 0 : 1);
  2087. if (entry != NULL)
  2088. xs_html_add(posts,
  2089. entry);
  2090. }
  2091. if (list && user && read_only) {
  2092. /** history **/
  2093. if (xs_type(xs_dict_get(srv_config, "disable_history")) != XSTYPE_TRUE) {
  2094. xs_html *ul = xs_html_tag("ul", NULL);
  2095. xs_html *history = xs_html_tag("div",
  2096. xs_html_attr("class", "snac-history"),
  2097. xs_html_tag("p",
  2098. xs_html_attr("class", "snac-history-title"),
  2099. xs_html_text(L("History"))),
  2100. ul);
  2101. xs *list = history_list(user);
  2102. xs_list *p = list;
  2103. const char *v;
  2104. while (xs_list_iter(&p, &v)) {
  2105. xs *fn = xs_replace(v, ".html", "");
  2106. xs *url = xs_fmt("%s/h/%s", user->actor, v);
  2107. xs_html_add(ul,
  2108. xs_html_tag("li",
  2109. xs_html_tag("a",
  2110. xs_html_attr("href", url),
  2111. xs_html_text(fn))));
  2112. }
  2113. xs_html_add(body,
  2114. history);
  2115. }
  2116. }
  2117. {
  2118. xs *s1 = xs_fmt("\n<!-- %lf seconds -->\n", ftime() - t);
  2119. xs_html_add(body,
  2120. xs_html_raw(s1));
  2121. }
  2122. if (show_more) {
  2123. xs *m = NULL;
  2124. xs *ss = xs_fmt("skip=%d&show=%d", skip + show, show);
  2125. xs *url = xs_dup(user == NULL ? srv_baseurl : user->actor);
  2126. if (page != NULL)
  2127. url = xs_str_cat(url, page);
  2128. if (xs_str_in(url, "?") != -1)
  2129. m = xs_fmt("%s&%s", url, ss);
  2130. else
  2131. m = xs_fmt("%s?%s", url, ss);
  2132. xs_html *more_links = xs_html_tag("p",
  2133. xs_html_tag("a",
  2134. xs_html_attr("href", url),
  2135. xs_html_attr("name", "snac-more"),
  2136. xs_html_text(L("Back to top"))),
  2137. xs_html_text(" - "),
  2138. xs_html_tag("a",
  2139. xs_html_attr("href", m),
  2140. xs_html_attr("name", "snac-more"),
  2141. xs_html_text(L("More..."))));
  2142. xs_html_add(body,
  2143. more_links);
  2144. }
  2145. xs_html_add(body,
  2146. html_footer());
  2147. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2148. }
  2149. xs_html *html_people_list(snac *snac, xs_list *list, char *header, char *t, const char *proxy)
  2150. {
  2151. xs_html *snac_posts;
  2152. xs_html *people = xs_html_tag("div",
  2153. xs_html_tag("h2",
  2154. xs_html_attr("class", "snac-header"),
  2155. xs_html_text(header)),
  2156. snac_posts = xs_html_tag("details",
  2157. xs_html_attr("open", NULL),
  2158. xs_html_tag("summary",
  2159. xs_html_text("..."))));
  2160. const char *actor_id;
  2161. xs_list_foreach(list, actor_id) {
  2162. xs *md5 = xs_md5_hex(actor_id, strlen(actor_id));
  2163. xs *actor = NULL;
  2164. if (valid_status(actor_get(actor_id, &actor))) {
  2165. xs_html *snac_post = xs_html_tag("div",
  2166. xs_html_attr("class", "snac-post"),
  2167. xs_html_tag("a",
  2168. xs_html_attr("name", md5)),
  2169. xs_html_tag("div",
  2170. xs_html_attr("class", "snac-post-header"),
  2171. html_actor_icon(snac, actor, xs_dict_get(actor, "published"),
  2172. NULL, NULL, 0, 1, proxy)));
  2173. /* content (user bio) */
  2174. const char *c = xs_dict_get(actor, "summary");
  2175. if (!xs_is_null(c)) {
  2176. xs *sc = sanitize(c);
  2177. xs_html *snac_content = xs_html_tag("div",
  2178. xs_html_attr("class", "snac-content"));
  2179. if (xs_startswith(sc, "<p>"))
  2180. xs_html_add(snac_content,
  2181. xs_html_raw(sc)); /* already sanitized */
  2182. else
  2183. xs_html_add(snac_content,
  2184. xs_html_tag("p",
  2185. xs_html_raw(sc))); /* already sanitized */
  2186. xs_html_add(snac_post, snac_content);
  2187. }
  2188. /* buttons */
  2189. xs *btn_form_action = xs_fmt("%s/admin/action", snac->actor);
  2190. xs_html *snac_controls = xs_html_tag("div",
  2191. xs_html_attr("class", "snac-controls"));
  2192. xs_html *form = xs_html_tag("form",
  2193. xs_html_attr("autocomplete", "off"),
  2194. xs_html_attr("method", "post"),
  2195. xs_html_attr("action", btn_form_action),
  2196. xs_html_sctag("input",
  2197. xs_html_attr("type", "hidden"),
  2198. xs_html_attr("name", "actor"),
  2199. xs_html_attr("value", actor_id)),
  2200. xs_html_sctag("input",
  2201. xs_html_attr("type", "hidden"),
  2202. xs_html_attr("name", "actor-form"),
  2203. xs_html_attr("value", "yes")));
  2204. xs_html_add(snac_controls, form);
  2205. if (following_check(snac, actor_id)) {
  2206. xs_html_add(form,
  2207. html_button("unfollow", L("Unfollow"),
  2208. L("Stop following this user's activity")));
  2209. if (is_limited(snac, actor_id))
  2210. xs_html_add(form,
  2211. html_button("unlimit", L("Unlimit"),
  2212. L("Allow announces (boosts) from this user")));
  2213. else
  2214. xs_html_add(form,
  2215. html_button("limit", L("Limit"),
  2216. L("Block announces (boosts) from this user")));
  2217. }
  2218. else {
  2219. xs_html_add(form,
  2220. html_button("follow", L("Follow"),
  2221. L("Start following this user's activity")));
  2222. if (follower_check(snac, actor_id))
  2223. xs_html_add(form,
  2224. html_button("delete", L("Delete"), L("Delete this user")));
  2225. }
  2226. if (pending_check(snac, actor_id)) {
  2227. xs_html_add(form,
  2228. html_button("approve", L("Approve"),
  2229. L("Approve this follow request")));
  2230. xs_html_add(form,
  2231. html_button("discard", L("Discard"), L("Discard this follow request")));
  2232. }
  2233. if (is_muted(snac, actor_id))
  2234. xs_html_add(form,
  2235. html_button("unmute", L("Unmute"),
  2236. L("Stop blocking activities from this user")));
  2237. else
  2238. xs_html_add(form,
  2239. html_button("mute", L("MUTE"),
  2240. L("Block any activity from this user")));
  2241. /* the post textarea */
  2242. xs *dm_div_id = xs_fmt("%s_%s_dm", md5, t);
  2243. xs *dm_form_id = xs_fmt("%s_reply_form", md5);
  2244. xs_html_add(snac_controls,
  2245. xs_html_tag("p", NULL),
  2246. html_note(snac, L("Direct Message..."),
  2247. dm_div_id, dm_form_id,
  2248. "", "",
  2249. NULL, actor_id,
  2250. xs_stock(XSTYPE_FALSE), "",
  2251. xs_stock(XSTYPE_FALSE), NULL,
  2252. NULL, 0, "", "", 0),
  2253. xs_html_tag("p", NULL));
  2254. xs_html_add(snac_post, snac_controls);
  2255. xs_html_add(snac_posts, snac_post);
  2256. }
  2257. }
  2258. return people;
  2259. }
  2260. xs_str *html_people(snac *user)
  2261. {
  2262. const char *proxy = NULL;
  2263. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2264. proxy = user->actor;
  2265. xs *wing = following_list(user);
  2266. xs *wers = follower_list(user);
  2267. xs_html *lists = xs_html_tag("div",
  2268. xs_html_attr("class", "snac-posts"));
  2269. if (xs_is_true(xs_dict_get(user->config, "approve_followers"))) {
  2270. xs *pending = pending_list(user);
  2271. xs_html_add(lists,
  2272. html_people_list(user, pending, L("Pending follow confirmations"), "p", proxy));
  2273. }
  2274. xs_html_add(lists,
  2275. html_people_list(user, wing, L("People you follow"), "i", proxy),
  2276. html_people_list(user, wers, L("People that follow you"), "e", proxy));
  2277. xs_html *html = xs_html_tag("html",
  2278. html_user_head(user, NULL, NULL),
  2279. xs_html_add(html_user_body(user, 0),
  2280. lists,
  2281. html_footer()));
  2282. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2283. }
  2284. xs_str *html_notifications(snac *user, int skip, int show)
  2285. {
  2286. const char *proxy = NULL;
  2287. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2288. proxy = user->actor;
  2289. xs *n_list = notify_list(user, skip, show);
  2290. xs *n_time = notify_check_time(user, 0);
  2291. xs_html *body = html_user_body(user, 0);
  2292. xs_html *html = xs_html_tag("html",
  2293. html_user_head(user, NULL, NULL),
  2294. body);
  2295. xs *clear_all_action = xs_fmt("%s/admin/clear-notifications", user->actor);
  2296. xs_html_add(body,
  2297. xs_html_tag("form",
  2298. xs_html_attr("autocomplete", "off"),
  2299. xs_html_attr("method", "post"),
  2300. xs_html_attr("action", clear_all_action),
  2301. xs_html_attr("id", "clear"),
  2302. xs_html_sctag("input",
  2303. xs_html_attr("type", "submit"),
  2304. xs_html_attr("class", "snac-btn-like"),
  2305. xs_html_attr("value", L("Clear all")))));
  2306. xs_html *noti_new = NULL;
  2307. xs_html *noti_seen = NULL;
  2308. xs_html *posts = xs_html_tag("div",
  2309. xs_html_attr("class", "snac-posts"));
  2310. xs_html_add(body, posts);
  2311. xs_list *p = n_list;
  2312. const xs_str *v;
  2313. while (xs_list_iter(&p, &v)) {
  2314. xs *noti = notify_get(user, v);
  2315. if (noti == NULL)
  2316. continue;
  2317. xs *obj = NULL;
  2318. const char *type = xs_dict_get(noti, "type");
  2319. const char *utype = xs_dict_get(noti, "utype");
  2320. const char *id = xs_dict_get(noti, "objid");
  2321. const char *date = xs_dict_get(noti, "date");
  2322. xs *wrk = NULL;
  2323. if (xs_is_null(id))
  2324. continue;
  2325. if (is_hidden(user, id))
  2326. continue;
  2327. object_get(id, &obj);
  2328. const char *actor_id = xs_dict_get(noti, "actor");
  2329. xs *actor = NULL;
  2330. if (!valid_status(actor_get(actor_id, &actor)))
  2331. continue;
  2332. xs *a_name = actor_name(actor, proxy);
  2333. const char *label = type;
  2334. if (strcmp(type, "Create") == 0)
  2335. label = L("Mention");
  2336. else
  2337. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  2338. label = L("Finished poll");
  2339. else
  2340. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0)
  2341. label = L("Unfollow");
  2342. else
  2343. if (strcmp(type, "EmojiReact") == 0) {
  2344. const char *content = xs_dict_get_path(noti, "msg.content");
  2345. if (xs_type(content) == XSTYPE_STRING) {
  2346. wrk = xs_fmt("%s (%s)", type, content);
  2347. label = wrk;
  2348. }
  2349. }
  2350. else
  2351. if (strcmp(type, "Follow") == 0 && pending_check(user, actor_id))
  2352. label = L("Follow Request");
  2353. xs *s_date = xs_crop_i(xs_dup(date), 0, 10);
  2354. xs_html *entry = xs_html_tag("div",
  2355. xs_html_attr("class", "snac-post-with-desc"),
  2356. xs_html_tag("p",
  2357. xs_html_tag("b",
  2358. xs_html_text(label),
  2359. xs_html_text(" by "),
  2360. xs_html_tag("a",
  2361. xs_html_attr("href", actor_id),
  2362. xs_html_raw(a_name))), /* a_name is already sanitized */
  2363. xs_html_text(" "),
  2364. xs_html_tag("time",
  2365. xs_html_attr("class", "dt-published snac-pubdate"),
  2366. xs_html_attr("title", date),
  2367. xs_html_text(s_date))));
  2368. if (strcmp(type, "Follow") == 0 || strcmp(utype, "Follow") == 0 || strcmp(type, "Block") == 0) {
  2369. xs_html_add(entry,
  2370. xs_html_tag("div",
  2371. xs_html_attr("class", "snac-post"),
  2372. html_actor_icon(user, actor, NULL, NULL, NULL, 0, 0, proxy)));
  2373. }
  2374. else
  2375. if (strcmp(type, "Move") == 0) {
  2376. const xs_dict *o_msg = xs_dict_get(noti, "msg");
  2377. const char *target;
  2378. if (xs_type(o_msg) == XSTYPE_DICT && (target = xs_dict_get(o_msg, "target"))) {
  2379. xs *old_actor = NULL;
  2380. if (valid_status(actor_get(target, &old_actor))) {
  2381. xs_html_add(entry,
  2382. xs_html_tag("div",
  2383. xs_html_attr("class", "snac-post"),
  2384. html_actor_icon(user, old_actor, NULL, NULL, NULL, 0, 0, proxy)));
  2385. }
  2386. }
  2387. }
  2388. else
  2389. if (obj != NULL) {
  2390. xs *md5 = xs_md5_hex(id, strlen(id));
  2391. xs_html *h = html_entry(user, obj, 0, 0, md5, 1);
  2392. if (h != NULL) {
  2393. xs_html_add(entry,
  2394. h);
  2395. }
  2396. }
  2397. if (strcmp(v, n_time) > 0) {
  2398. /* unseen notification */
  2399. if (noti_new == NULL) {
  2400. noti_new = xs_html_tag("div",
  2401. xs_html_tag("h2",
  2402. xs_html_attr("class", "snac-header"),
  2403. xs_html_text(L("New"))));
  2404. xs_html_add(posts,
  2405. noti_new);
  2406. }
  2407. xs_html_add(noti_new,
  2408. entry);
  2409. }
  2410. else {
  2411. /* already seen notification */
  2412. if (noti_seen == NULL) {
  2413. noti_seen = xs_html_tag("div",
  2414. xs_html_tag("h2",
  2415. xs_html_attr("class", "snac-header"),
  2416. xs_html_text(L("Already seen"))));
  2417. xs_html_add(posts,
  2418. noti_seen);
  2419. }
  2420. xs_html_add(noti_seen,
  2421. entry);
  2422. }
  2423. }
  2424. if (noti_new == NULL && noti_seen == NULL)
  2425. xs_html_add(body,
  2426. xs_html_tag("h2",
  2427. xs_html_attr("class", "snac-header"),
  2428. xs_html_text(L("None"))));
  2429. /* add the navigation footer */
  2430. xs *next_p = notify_list(user, skip + show, 1);
  2431. if (xs_list_len(next_p)) {
  2432. xs *url = xs_fmt("%s/notifications?skip=%d&show=%d",
  2433. user->actor, skip + show, show);
  2434. xs_html_add(body,
  2435. xs_html_tag("p",
  2436. xs_html_tag("a",
  2437. xs_html_attr("href", url),
  2438. xs_html_text(L("More...")))));
  2439. }
  2440. xs_html_add(body,
  2441. html_footer());
  2442. /* set the check time to now */
  2443. xs *dummy = notify_check_time(user, 1);
  2444. dummy = xs_free(dummy);
  2445. timeline_touch(user);
  2446. return xs_html_render_s(html, "<!DOCTYPE html>\n");
  2447. }
  2448. int html_get_handler(const xs_dict *req, const char *q_path,
  2449. char **body, int *b_size, char **ctype,
  2450. xs_str **etag, xs_str **last_modified)
  2451. {
  2452. const char *accept = xs_dict_get(req, "accept");
  2453. int status = HTTP_STATUS_NOT_FOUND;
  2454. snac snac;
  2455. xs *uid = NULL;
  2456. const char *p_path;
  2457. int cache = 1;
  2458. int save = 1;
  2459. int proxy = 0;
  2460. const char *v;
  2461. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  2462. xs *l = xs_split_n(q_path, "/", 2);
  2463. v = xs_list_get(l, 1);
  2464. if (xs_is_null(v)) {
  2465. srv_log(xs_fmt("html_get_handler bad query '%s'", q_path));
  2466. return HTTP_STATUS_NOT_FOUND;
  2467. }
  2468. if (strcmp(v, "share-bridge") == 0) {
  2469. /* temporary redirect for a post */
  2470. const char *login = xs_dict_get(q_vars, "login");
  2471. const char *content = xs_dict_get(q_vars, "content");
  2472. if (xs_type(login) == XSTYPE_STRING && xs_type(content) == XSTYPE_STRING) {
  2473. xs *b64 = xs_base64_enc(content, strlen(content));
  2474. srv_log(xs_fmt("share-bridge for user '%s'", login));
  2475. *body = xs_fmt("%s/%s/share?content=%s", srv_baseurl, login, b64);
  2476. return HTTP_STATUS_SEE_OTHER;
  2477. }
  2478. else
  2479. return HTTP_STATUS_NOT_FOUND;
  2480. }
  2481. uid = xs_dup(v);
  2482. /* rss extension? */
  2483. if (xs_endswith(uid, ".rss")) {
  2484. uid = xs_crop_i(uid, 0, -4);
  2485. p_path = ".rss";
  2486. }
  2487. else
  2488. p_path = xs_list_get(l, 2);
  2489. if (!uid || !user_open(&snac, uid)) {
  2490. /* invalid user */
  2491. srv_debug(1, xs_fmt("html_get_handler bad user %s", uid));
  2492. return HTTP_STATUS_NOT_FOUND;
  2493. }
  2494. if (xs_is_true(xs_dict_get(srv_config, "proxy_media")))
  2495. proxy = 1;
  2496. /* return the RSS if requested by Accept header */
  2497. if (accept != NULL) {
  2498. if (xs_str_in(accept, "text/xml") != -1 ||
  2499. xs_str_in(accept, "application/rss+xml") != -1)
  2500. p_path = ".rss";
  2501. }
  2502. /* check if server config variable 'disable_cache' is set */
  2503. if ((v = xs_dict_get(srv_config, "disable_cache")) && xs_type(v) == XSTYPE_TRUE)
  2504. cache = 0;
  2505. int skip = 0;
  2506. int def_show = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  2507. int show = def_show;
  2508. if ((v = xs_dict_get(q_vars, "skip")) != NULL)
  2509. skip = atoi(v), cache = 0, save = 0;
  2510. if ((v = xs_dict_get(q_vars, "show")) != NULL)
  2511. show = atoi(v), cache = 0, save = 0;
  2512. if ((v = xs_dict_get(q_vars, "da")) != NULL) {
  2513. /* user dismissed an announcement */
  2514. if (login(&snac, req)) {
  2515. double ts = atof(v);
  2516. xs *timestamp = xs_number_new(ts);
  2517. srv_log(xs_fmt("user dismissed announcements until %d", ts));
  2518. snac.config = xs_dict_set(snac.config, "last_announcement", timestamp);
  2519. user_persist(&snac, 0);
  2520. }
  2521. }
  2522. /* get a possible error message */
  2523. const char *error = xs_dict_get(q_vars, "error");
  2524. if (error != NULL)
  2525. cache = 0;
  2526. /* a show of 0 has no sense */
  2527. if (show == 0)
  2528. show = def_show;
  2529. if (p_path == NULL) { /** public timeline **/
  2530. xs *h = xs_str_localtime(0, "%Y-%m.html");
  2531. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE) {
  2532. /** empty public timeline for private users **/
  2533. *body = html_timeline(&snac, NULL, 1, 0, 0, 0, NULL, "", 1, error);
  2534. *b_size = strlen(*body);
  2535. status = HTTP_STATUS_OK;
  2536. }
  2537. else
  2538. if (cache && history_mtime(&snac, h) > timeline_mtime(&snac)) {
  2539. snac_debug(&snac, 1, xs_fmt("serving cached local timeline"));
  2540. status = history_get(&snac, h, body, b_size,
  2541. xs_dict_get(req, "if-none-match"), etag);
  2542. }
  2543. else {
  2544. xs *list = NULL;
  2545. xs *next = NULL;
  2546. if (xs_is_true(xs_dict_get(srv_config, "strict_public_timelines"))) {
  2547. list = timeline_simple_list(&snac, "public", skip, show);
  2548. next = timeline_simple_list(&snac, "public", skip + show, 1);
  2549. }
  2550. else {
  2551. list = timeline_list(&snac, "public", skip, show);
  2552. next = timeline_list(&snac, "public", skip + show, 1);
  2553. }
  2554. xs *pins = pinned_list(&snac);
  2555. pins = xs_list_cat(pins, list);
  2556. *body = html_timeline(&snac, pins, 1, skip, show, xs_list_len(next), NULL, "", 1, error);
  2557. *b_size = strlen(*body);
  2558. status = HTTP_STATUS_OK;
  2559. if (save)
  2560. history_add(&snac, h, *body, *b_size, etag);
  2561. }
  2562. }
  2563. else
  2564. if (strcmp(p_path, "admin") == 0) { /** private timeline **/
  2565. if (!login(&snac, req)) {
  2566. *body = xs_dup(uid);
  2567. status = HTTP_STATUS_UNAUTHORIZED;
  2568. }
  2569. else {
  2570. const char *q = xs_dict_get(q_vars, "q");
  2571. xs *url_acct = NULL;
  2572. /* searching for an URL? */
  2573. if (q && xs_match(q, "https://*|http://*")) {
  2574. /* may by an actor; try a webfinger */
  2575. xs *actor_obj = NULL;
  2576. if (valid_status(webfinger_request(q, &actor_obj, &url_acct))) {
  2577. /* it's an actor; do the dirty trick of changing q to the account name */
  2578. q = url_acct;
  2579. }
  2580. else {
  2581. /* bring it to the user's timeline */
  2582. xs *object = NULL;
  2583. int status;
  2584. status = activitypub_request(&snac, q, &object);
  2585. snac_debug(&snac, 1, xs_fmt("Request searched URL %s %d", q, status));
  2586. if (valid_status(status)) {
  2587. /* got it; also request the actor */
  2588. const char *attr_to = get_atto(object);
  2589. if (!xs_is_null(attr_to)) {
  2590. status = actor_request(&snac, attr_to, &actor_obj);
  2591. if (valid_status(status)) {
  2592. /* reset the query string to be the real id */
  2593. url_acct = xs_dup(xs_dict_get(object, "id"));
  2594. q = url_acct;
  2595. /* add the post to the timeline */
  2596. xs *md5 = xs_md5_hex(q, strlen(q));
  2597. if (!timeline_here(&snac, md5))
  2598. timeline_add(&snac, q, object);
  2599. }
  2600. }
  2601. }
  2602. }
  2603. /* fall through */
  2604. }
  2605. if (q && *q) {
  2606. if (xs_regex_match(q, "^@?[a-zA-Z0-9._]+@[a-zA-Z0-9-]+\\.")) {
  2607. /** search account **/
  2608. xs *actor = NULL;
  2609. xs *acct = NULL;
  2610. xs *l = xs_list_new();
  2611. xs_html *page = NULL;
  2612. if (valid_status(webfinger_request(q, &actor, &acct))) {
  2613. xs *actor_obj = NULL;
  2614. if (valid_status(actor_request(&snac, actor, &actor_obj))) {
  2615. actor_add(actor, actor_obj);
  2616. /* create a people list with only one element */
  2617. l = xs_list_append(xs_list_new(), actor);
  2618. xs *title = xs_fmt(L("Search results for account %s"), q);
  2619. page = html_people_list(&snac, l, title, "wf", NULL);
  2620. }
  2621. }
  2622. if (page == NULL) {
  2623. xs *title = xs_fmt(L("Account %s not found"), q);
  2624. page = xs_html_tag("div",
  2625. xs_html_tag("h2",
  2626. xs_html_attr("class", "snac-header"),
  2627. xs_html_text(title)));
  2628. }
  2629. xs_html *html = xs_html_tag("html",
  2630. html_user_head(&snac, NULL, NULL),
  2631. xs_html_add(html_user_body(&snac, 0),
  2632. page,
  2633. html_footer()));
  2634. *body = xs_html_render_s(html, "<!DOCTYPE html>\n");
  2635. *b_size = strlen(*body);
  2636. status = HTTP_STATUS_OK;
  2637. }
  2638. else
  2639. if (*q == '#') {
  2640. /** search by tag **/
  2641. xs *tl = tag_search(q, skip, show + 1);
  2642. int more = 0;
  2643. if (xs_list_len(tl) >= show + 1) {
  2644. /* drop the last one */
  2645. tl = xs_list_del(tl, -1);
  2646. more = 1;
  2647. }
  2648. xs *page = xs_fmt("/admin?q=%%23%s", q + 1);
  2649. xs *title = xs_fmt(xs_list_len(tl) ?
  2650. L("Search results for tag %s") : L("Nothing found for tag %s"), q);
  2651. *body = html_timeline(&snac, tl, 0, skip, show, more, title, page, 0, error);
  2652. *b_size = strlen(*body);
  2653. status = HTTP_STATUS_OK;
  2654. }
  2655. else {
  2656. /** search by content **/
  2657. int to = 0;
  2658. int msecs = atoi(xs_dict_get_def(q_vars, "msecs", "0"));
  2659. xs *tl = content_search(&snac, q, 1, skip, show, msecs, &to);
  2660. xs *title = NULL;
  2661. xs *page = xs_fmt("/admin?q=%s&msecs=%d", q, msecs + 10);
  2662. int tl_len = xs_list_len(tl);
  2663. if (to)
  2664. title = xs_fmt(L("Search results for '%s' (may be more)"), q);
  2665. else
  2666. if (tl_len)
  2667. title = xs_fmt(L("Search results for '%s'"), q);
  2668. else
  2669. if (skip)
  2670. title = xs_fmt(L("No more matches for '%s'"), q);
  2671. else
  2672. title = xs_fmt(L("Nothing found for '%s'"), q);
  2673. *body = html_timeline(&snac, tl, 0, skip, tl_len, to || tl_len == show,
  2674. title, page, 0, error);
  2675. *b_size = strlen(*body);
  2676. status = HTTP_STATUS_OK;
  2677. }
  2678. }
  2679. else {
  2680. double t = history_mtime(&snac, "timeline.html_");
  2681. /* if enabled by admin, return a cached page if its timestamp is:
  2682. a) newer than the timeline timestamp
  2683. b) newer than the start time of the server
  2684. */
  2685. if (cache && t > timeline_mtime(&snac) && t > p_state->srv_start_time) {
  2686. snac_debug(&snac, 1, xs_fmt("serving cached timeline"));
  2687. status = history_get(&snac, "timeline.html_", body, b_size,
  2688. xs_dict_get(req, "if-none-match"), etag);
  2689. }
  2690. else {
  2691. snac_debug(&snac, 1, xs_fmt("building timeline"));
  2692. xs *list = timeline_list(&snac, "private", skip, show);
  2693. xs *next = timeline_list(&snac, "private", skip + show, 1);
  2694. *body = html_timeline(&snac, list, 0, skip, show,
  2695. xs_list_len(next), NULL, "/admin", 1, error);
  2696. *b_size = strlen(*body);
  2697. status = HTTP_STATUS_OK;
  2698. if (save)
  2699. history_add(&snac, "timeline.html_", *body, *b_size, etag);
  2700. }
  2701. }
  2702. }
  2703. }
  2704. else
  2705. if (xs_startswith(p_path, "admin/p/")) { /** unique post by md5 **/
  2706. if (!login(&snac, req)) {
  2707. *body = xs_dup(uid);
  2708. status = HTTP_STATUS_UNAUTHORIZED;
  2709. }
  2710. else {
  2711. xs *l = xs_split(p_path, "/");
  2712. const char *md5 = xs_list_get(l, -1);
  2713. if (md5 && *md5 && timeline_here(&snac, md5)) {
  2714. xs *list = xs_list_append(xs_list_new(), md5);
  2715. *body = html_timeline(&snac, list, 0, 0, 0, 0, NULL, "/admin", 1, error);
  2716. *b_size = strlen(*body);
  2717. status = HTTP_STATUS_OK;
  2718. }
  2719. }
  2720. }
  2721. else
  2722. if (strcmp(p_path, "people") == 0) { /** the list of people **/
  2723. if (!login(&snac, req)) {
  2724. *body = xs_dup(uid);
  2725. status = HTTP_STATUS_UNAUTHORIZED;
  2726. }
  2727. else {
  2728. *body = html_people(&snac);
  2729. *b_size = strlen(*body);
  2730. status = HTTP_STATUS_OK;
  2731. }
  2732. }
  2733. else
  2734. if (strcmp(p_path, "notifications") == 0) { /** the list of notifications **/
  2735. if (!login(&snac, req)) {
  2736. *body = xs_dup(uid);
  2737. status = HTTP_STATUS_UNAUTHORIZED;
  2738. }
  2739. else {
  2740. *body = html_notifications(&snac, skip, show);
  2741. *b_size = strlen(*body);
  2742. status = HTTP_STATUS_OK;
  2743. }
  2744. }
  2745. else
  2746. if (strcmp(p_path, "instance") == 0) { /** instance timeline **/
  2747. if (!login(&snac, req)) {
  2748. *body = xs_dup(uid);
  2749. status = HTTP_STATUS_UNAUTHORIZED;
  2750. }
  2751. else {
  2752. xs *list = timeline_instance_list(skip, show);
  2753. xs *next = timeline_instance_list(skip + show, 1);
  2754. *body = html_timeline(&snac, list, 0, skip, show,
  2755. xs_list_len(next), L("Showing instance timeline"), "/instance", 0, error);
  2756. *b_size = strlen(*body);
  2757. status = HTTP_STATUS_OK;
  2758. }
  2759. }
  2760. else
  2761. if (strcmp(p_path, "pinned") == 0) { /** list of pinned posts **/
  2762. if (!login(&snac, req)) {
  2763. *body = xs_dup(uid);
  2764. status = HTTP_STATUS_UNAUTHORIZED;
  2765. }
  2766. else {
  2767. xs *list = pinned_list(&snac);
  2768. *body = html_timeline(&snac, list, 0, skip, show,
  2769. 0, L("Pinned posts"), "", 0, error);
  2770. *b_size = strlen(*body);
  2771. status = HTTP_STATUS_OK;
  2772. }
  2773. }
  2774. else
  2775. if (strcmp(p_path, "bookmarks") == 0) { /** list of bookmarked posts **/
  2776. if (!login(&snac, req)) {
  2777. *body = xs_dup(uid);
  2778. status = HTTP_STATUS_UNAUTHORIZED;
  2779. }
  2780. else {
  2781. xs *list = bookmark_list(&snac);
  2782. *body = html_timeline(&snac, list, 0, skip, show,
  2783. 0, L("Bookmarked posts"), "", 0, error);
  2784. *b_size = strlen(*body);
  2785. status = HTTP_STATUS_OK;
  2786. }
  2787. }
  2788. else
  2789. if (strcmp(p_path, "drafts") == 0) { /** list of drafts **/
  2790. if (!login(&snac, req)) {
  2791. *body = xs_dup(uid);
  2792. status = HTTP_STATUS_UNAUTHORIZED;
  2793. }
  2794. else {
  2795. xs *list = draft_list(&snac);
  2796. *body = html_timeline(&snac, list, 0, skip, show,
  2797. 0, L("Post drafts"), "", 0, error);
  2798. *b_size = strlen(*body);
  2799. status = HTTP_STATUS_OK;
  2800. }
  2801. }
  2802. else
  2803. if (xs_startswith(p_path, "list/")) { /** list timelines **/
  2804. if (!login(&snac, req)) {
  2805. *body = xs_dup(uid);
  2806. status = HTTP_STATUS_UNAUTHORIZED;
  2807. }
  2808. else {
  2809. xs *l = xs_split(p_path, "/");
  2810. const char *lid = xs_list_get(l, -1);
  2811. xs *list = list_timeline(&snac, lid, skip, show);
  2812. xs *next = list_timeline(&snac, lid, skip + show, 1);
  2813. if (list != NULL) {
  2814. xs *ttl = timeline_top_level(&snac, list);
  2815. xs *base = xs_fmt("/list/%s", lid);
  2816. xs *name = list_maint(&snac, lid, 3);
  2817. xs *title = xs_fmt(L("Showing timeline for list '%s'"), name);
  2818. *body = html_timeline(&snac, ttl, 0, skip, show,
  2819. xs_list_len(next), title, base, 1, error);
  2820. *b_size = strlen(*body);
  2821. status = HTTP_STATUS_OK;
  2822. }
  2823. }
  2824. }
  2825. else
  2826. if (xs_startswith(p_path, "p/")) { /** a timeline with just one entry **/
  2827. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2828. return HTTP_STATUS_FORBIDDEN;
  2829. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2830. xs *msg = NULL;
  2831. if (valid_status(object_get(id, &msg))) {
  2832. xs *md5 = xs_md5_hex(id, strlen(id));
  2833. xs *list = xs_list_new();
  2834. list = xs_list_append(list, md5);
  2835. *body = html_timeline(&snac, list, 1, 0, 0, 0, NULL, "", 1, error);
  2836. *b_size = strlen(*body);
  2837. status = HTTP_STATUS_OK;
  2838. }
  2839. }
  2840. else
  2841. if (xs_startswith(p_path, "s/")) { /** a static file **/
  2842. xs *l = xs_split(p_path, "/");
  2843. const char *id = xs_list_get(l, 1);
  2844. int sz;
  2845. if (id && *id) {
  2846. status = static_get(&snac, id, body, &sz,
  2847. xs_dict_get(req, "if-none-match"), etag);
  2848. if (valid_status(status)) {
  2849. *b_size = sz;
  2850. *ctype = (char *)xs_mime_by_ext(id);
  2851. }
  2852. }
  2853. }
  2854. else
  2855. if (xs_startswith(p_path, "h/")) { /** an entry from the history **/
  2856. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2857. return HTTP_STATUS_FORBIDDEN;
  2858. if (xs_type(xs_dict_get(srv_config, "disable_history")) == XSTYPE_TRUE)
  2859. return HTTP_STATUS_FORBIDDEN;
  2860. xs *l = xs_split(p_path, "/");
  2861. const char *id = xs_list_get(l, 1);
  2862. if (id && *id) {
  2863. if (xs_endswith(id, "timeline.html_")) {
  2864. /* Don't let them in */
  2865. *b_size = 0;
  2866. status = HTTP_STATUS_NOT_FOUND;
  2867. }
  2868. else
  2869. status = history_get(&snac, id, body, b_size,
  2870. xs_dict_get(req, "if-none-match"), etag);
  2871. }
  2872. }
  2873. else
  2874. if (strcmp(p_path, ".rss") == 0) { /** public timeline in RSS format **/
  2875. if (xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE)
  2876. return HTTP_STATUS_FORBIDDEN;
  2877. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  2878. xs *elems = timeline_simple_list(&snac, "public", 0, cnt);
  2879. xs *bio = xs_dup(xs_dict_get(snac.config, "bio"));
  2880. xs *rss_title = xs_fmt("%s (@%s@%s)",
  2881. xs_dict_get(snac.config, "name"),
  2882. snac.uid,
  2883. xs_dict_get(srv_config, "host"));
  2884. xs *rss_link = xs_fmt("%s.rss", snac.actor);
  2885. *body = timeline_to_rss(&snac, elems, rss_title, rss_link, bio);
  2886. *b_size = strlen(*body);
  2887. *ctype = "application/rss+xml; charset=utf-8";
  2888. status = HTTP_STATUS_OK;
  2889. snac_debug(&snac, 1, xs_fmt("serving RSS"));
  2890. }
  2891. else
  2892. if (proxy && (xs_startswith(p_path, "x/") || xs_startswith(p_path, "y/"))) { /** remote media by proxy **/
  2893. xs *proxy_prefix = NULL;
  2894. if (xs_startswith(p_path, "x/")) {
  2895. /* proxy usage authorized by http basic auth */
  2896. if (login(&snac, req))
  2897. proxy_prefix = xs_str_new("x/");
  2898. else {
  2899. *body = xs_dup(uid);
  2900. status = HTTP_STATUS_UNAUTHORIZED;
  2901. }
  2902. }
  2903. else {
  2904. /* proxy usage authorized by proxy_token */
  2905. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, snac.actor);
  2906. xs *tk = xs_md5_hex(tks, strlen(tks));
  2907. xs *p = xs_fmt("y/%s/", tk);
  2908. if (xs_startswith(p_path, p))
  2909. proxy_prefix = xs_dup(p);
  2910. }
  2911. if (proxy_prefix) {
  2912. /* pick the raw path (including optional ? arguments) */
  2913. const char *raw_path = xs_dict_get(req, "raw_path");
  2914. /* skip to where the proxy/ string starts */
  2915. raw_path += xs_str_in(raw_path, proxy_prefix);
  2916. xs *url = xs_replace_n(raw_path, proxy_prefix, "https:/" "/", 1);
  2917. xs *hdrs = xs_dict_new();
  2918. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  2919. const char *ims = xs_dict_get(req, "if-modified-since");
  2920. const char *inm = xs_dict_get(req, "if-none-match");
  2921. if (ims) hdrs = xs_dict_append(hdrs, "if-modified-since", ims);
  2922. if (inm) hdrs = xs_dict_append(hdrs, "if-none-match", inm);
  2923. xs *rsp = xs_http_request("GET", url, hdrs,
  2924. NULL, 0, &status, body, b_size, 0);
  2925. if (valid_status(status)) {
  2926. const char *ct = xs_or(xs_dict_get(rsp, "content-type"), "");
  2927. const char *lm = xs_dict_get(rsp, "last-modified");
  2928. const char *et = xs_dict_get(rsp, "etag");
  2929. if (lm) *last_modified = xs_dup(lm);
  2930. if (et) *etag = xs_dup(et);
  2931. /* find the content-type in the static mime types,
  2932. and return that value instead of ct, which will
  2933. be destroyed when out of scope */
  2934. for (int n = 0; xs_mime_types[n]; n += 2) {
  2935. if (strcmp(ct, xs_mime_types[n + 1]) == 0) {
  2936. *ctype = (char *)xs_mime_types[n + 1];
  2937. break;
  2938. }
  2939. }
  2940. }
  2941. snac_debug(&snac, 1, xs_fmt("Proxy for %s %d", url, status));
  2942. }
  2943. }
  2944. else
  2945. if (strcmp(p_path, "share") == 0) { /** direct post **/
  2946. if (!login(&snac, req)) {
  2947. *body = xs_dup(uid);
  2948. status = HTTP_STATUS_UNAUTHORIZED;
  2949. }
  2950. else {
  2951. const char *b64 = xs_dict_get(q_vars, "content");
  2952. int sz;
  2953. xs *content = xs_base64_dec(b64, &sz);
  2954. xs *msg = msg_note(&snac, content, NULL, NULL, NULL, 0, NULL);
  2955. xs *c_msg = msg_create(&snac, msg);
  2956. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2957. enqueue_message(&snac, c_msg);
  2958. snac_debug(&snac, 1, xs_fmt("web action 'share' received"));
  2959. *body = xs_fmt("%s/admin", snac.actor);
  2960. *b_size = strlen(*body);
  2961. status = HTTP_STATUS_SEE_OTHER;
  2962. }
  2963. }
  2964. else
  2965. status = HTTP_STATUS_NOT_FOUND;
  2966. user_free(&snac);
  2967. if (valid_status(status) && *ctype == NULL) {
  2968. *ctype = "text/html; charset=utf-8";
  2969. }
  2970. return status;
  2971. }
  2972. int html_post_handler(const xs_dict *req, const char *q_path,
  2973. char *payload, int p_size,
  2974. char **body, int *b_size, char **ctype)
  2975. {
  2976. (void)p_size;
  2977. (void)ctype;
  2978. int status = 0;
  2979. snac snac;
  2980. const char *uid;
  2981. const char *p_path;
  2982. const xs_dict *p_vars;
  2983. xs *l = xs_split_n(q_path, "/", 2);
  2984. uid = xs_list_get(l, 1);
  2985. if (!uid || !user_open(&snac, uid)) {
  2986. /* invalid user */
  2987. srv_debug(1, xs_fmt("html_post_handler bad user %s", uid));
  2988. return HTTP_STATUS_NOT_FOUND;
  2989. }
  2990. p_path = xs_list_get(l, 2);
  2991. /* all posts must be authenticated */
  2992. if (!login(&snac, req)) {
  2993. user_free(&snac);
  2994. *body = xs_dup(uid);
  2995. return HTTP_STATUS_UNAUTHORIZED;
  2996. }
  2997. p_vars = xs_dict_get(req, "p_vars");
  2998. if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
  2999. snac_debug(&snac, 1, xs_fmt("web action '%s' received", p_path));
  3000. /* post note */
  3001. const xs_str *content = xs_dict_get(p_vars, "content");
  3002. const xs_str *in_reply_to = xs_dict_get(p_vars, "in_reply_to");
  3003. const xs_str *attach_url = xs_dict_get(p_vars, "attach_url");
  3004. const xs_list *attach_file = xs_dict_get(p_vars, "attach");
  3005. const xs_str *to = xs_dict_get(p_vars, "to");
  3006. const xs_str *sensitive = xs_dict_get(p_vars, "sensitive");
  3007. const xs_str *summary = xs_dict_get(p_vars, "summary");
  3008. const xs_str *edit_id = xs_dict_get(p_vars, "edit_id");
  3009. const xs_str *alt_text = xs_dict_get(p_vars, "alt_text");
  3010. int priv = !xs_is_null(xs_dict_get(p_vars, "mentioned_only"));
  3011. int store_as_draft = !xs_is_null(xs_dict_get(p_vars, "is_draft"));
  3012. xs *attach_list = xs_list_new();
  3013. /* default alt text */
  3014. if (xs_is_null(alt_text))
  3015. alt_text = "";
  3016. /* is attach_url set? */
  3017. if (!xs_is_null(attach_url) && *attach_url != '\0') {
  3018. xs *l = xs_list_new();
  3019. l = xs_list_append(l, attach_url);
  3020. l = xs_list_append(l, alt_text);
  3021. attach_list = xs_list_append(attach_list, l);
  3022. }
  3023. /* is attach_file set? */
  3024. if (!xs_is_null(attach_file) && xs_type(attach_file) == XSTYPE_LIST) {
  3025. const char *fn = xs_list_get(attach_file, 0);
  3026. if (*fn != '\0') {
  3027. char *ext = strrchr(fn, '.');
  3028. xs *hash = xs_md5_hex(fn, strlen(fn));
  3029. xs *id = xs_fmt("%s%s", hash, ext);
  3030. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  3031. int fo = xs_number_get(xs_list_get(attach_file, 1));
  3032. int fs = xs_number_get(xs_list_get(attach_file, 2));
  3033. /* store */
  3034. static_put(&snac, id, payload + fo, fs);
  3035. xs *l = xs_list_new();
  3036. l = xs_list_append(l, url);
  3037. l = xs_list_append(l, alt_text);
  3038. attach_list = xs_list_append(attach_list, l);
  3039. }
  3040. }
  3041. if (content != NULL) {
  3042. xs *msg = NULL;
  3043. xs *c_msg = NULL;
  3044. xs *content_2 = xs_replace(content, "\r", "");
  3045. xs *poll_opts = NULL;
  3046. /* is there a valid set of poll options? */
  3047. const char *v = xs_dict_get(p_vars, "poll_options");
  3048. if (!xs_is_null(v) && *v) {
  3049. xs *v2 = xs_strip_i(xs_replace(v, "\r", ""));
  3050. poll_opts = xs_split(v2, "\n");
  3051. }
  3052. if (!xs_is_null(poll_opts) && xs_list_len(poll_opts)) {
  3053. /* get the rest of poll configuration */
  3054. const char *p_multiple = xs_dict_get(p_vars, "poll_multiple");
  3055. const char *p_end_secs = xs_dict_get(p_vars, "poll_end_secs");
  3056. int multiple = 0;
  3057. int end_secs = atoi(!xs_is_null(p_end_secs) ? p_end_secs : "60");
  3058. if (!xs_is_null(p_multiple) && strcmp(p_multiple, "on") == 0)
  3059. multiple = 1;
  3060. msg = msg_question(&snac, content_2, attach_list,
  3061. poll_opts, multiple, end_secs);
  3062. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  3063. }
  3064. else
  3065. msg = msg_note(&snac, content_2, to, in_reply_to, attach_list, priv, NULL);
  3066. if (sensitive != NULL) {
  3067. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  3068. msg = xs_dict_set(msg, "summary", xs_is_null(summary) ? "..." : summary);
  3069. }
  3070. if (xs_is_null(edit_id)) {
  3071. /* new message */
  3072. const char *id = xs_dict_get(msg, "id");
  3073. if (store_as_draft) {
  3074. draft_add(&snac, id, msg);
  3075. }
  3076. else {
  3077. c_msg = msg_create(&snac, msg);
  3078. timeline_add(&snac, id, msg);
  3079. }
  3080. }
  3081. else {
  3082. /* an edition of a previous message */
  3083. xs *p_msg = NULL;
  3084. if (valid_status(object_get(edit_id, &p_msg))) {
  3085. /* copy relevant fields from previous version */
  3086. char *fields[] = { "id", "context", "url", "published",
  3087. "to", "inReplyTo", NULL };
  3088. int n;
  3089. for (n = 0; fields[n]; n++) {
  3090. const char *v = xs_dict_get(p_msg, fields[n]);
  3091. msg = xs_dict_set(msg, fields[n], v);
  3092. }
  3093. if (store_as_draft) {
  3094. draft_add(&snac, edit_id, msg);
  3095. }
  3096. else
  3097. if (is_draft(&snac, edit_id)) {
  3098. /* message was previously a draft; it's a create activity */
  3099. /* set the published field to now */
  3100. xs *published = xs_str_utctime(0, ISO_DATE_SPEC);
  3101. msg = xs_dict_set(msg, "published", published);
  3102. /* overwrite object */
  3103. object_add_ow(edit_id, msg);
  3104. c_msg = msg_create(&snac, msg);
  3105. timeline_add(&snac, edit_id, msg);
  3106. draft_del(&snac, edit_id);
  3107. }
  3108. else {
  3109. /* set the updated field */
  3110. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  3111. msg = xs_dict_set(msg, "updated", updated);
  3112. /* overwrite object, not updating the indexes */
  3113. object_add_ow(edit_id, msg);
  3114. /* update message */
  3115. c_msg = msg_update(&snac, msg);
  3116. }
  3117. }
  3118. else
  3119. snac_log(&snac, xs_fmt("cannot get object '%s' for editing", edit_id));
  3120. }
  3121. if (c_msg != NULL)
  3122. enqueue_message(&snac, c_msg);
  3123. history_del(&snac, "timeline.html_");
  3124. }
  3125. status = HTTP_STATUS_SEE_OTHER;
  3126. }
  3127. else
  3128. if (p_path && strcmp(p_path, "admin/action") == 0) { /** **/
  3129. /* action on an entry */
  3130. const char *id = xs_dict_get(p_vars, "id");
  3131. const char *actor = xs_dict_get(p_vars, "actor");
  3132. const char *action = xs_dict_get(p_vars, "action");
  3133. const char *group = xs_dict_get(p_vars, "group");
  3134. if (action == NULL)
  3135. return HTTP_STATUS_NOT_FOUND;
  3136. snac_debug(&snac, 1, xs_fmt("web action '%s' received", action));
  3137. status = HTTP_STATUS_SEE_OTHER;
  3138. if (strcmp(action, L("Like")) == 0) { /** **/
  3139. xs *msg = msg_admiration(&snac, id, "Like");
  3140. if (msg != NULL) {
  3141. enqueue_message(&snac, msg);
  3142. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 1);
  3143. }
  3144. }
  3145. else
  3146. if (strcmp(action, L("Boost")) == 0) { /** **/
  3147. xs *msg = msg_admiration(&snac, id, "Announce");
  3148. if (msg != NULL) {
  3149. enqueue_message(&snac, msg);
  3150. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0);
  3151. }
  3152. }
  3153. else
  3154. if (strcmp(action, L("Unlike")) == 0) { /** **/
  3155. xs *msg = msg_repulsion(&snac, id, "Like");
  3156. if (msg != NULL) {
  3157. enqueue_message(&snac, msg);
  3158. }
  3159. }
  3160. else
  3161. if (strcmp(action, L("Unboost")) == 0) { /** **/
  3162. xs *msg = msg_repulsion(&snac, id, "Announce");
  3163. if (msg != NULL) {
  3164. enqueue_message(&snac, msg);
  3165. }
  3166. }
  3167. else
  3168. if (strcmp(action, L("MUTE")) == 0) { /** **/
  3169. mute(&snac, actor);
  3170. }
  3171. else
  3172. if (strcmp(action, L("Unmute")) == 0) { /** **/
  3173. unmute(&snac, actor);
  3174. }
  3175. else
  3176. if (strcmp(action, L("Hide")) == 0) { /** **/
  3177. if (is_draft(&snac, id))
  3178. draft_del(&snac, id);
  3179. else
  3180. hide(&snac, id);
  3181. }
  3182. else
  3183. if (strcmp(action, L("Limit")) == 0) { /** **/
  3184. limit(&snac, actor);
  3185. }
  3186. else
  3187. if (strcmp(action, L("Unlimit")) == 0) { /** **/
  3188. unlimit(&snac, actor);
  3189. }
  3190. else
  3191. if (strcmp(action, L("Follow")) == 0) { /** **/
  3192. xs *msg = msg_follow(&snac, actor);
  3193. if (msg != NULL) {
  3194. /* reload the actor from the message, in may be different */
  3195. actor = xs_dict_get(msg, "object");
  3196. following_add(&snac, actor, msg);
  3197. enqueue_output_by_actor(&snac, msg, actor, 0);
  3198. }
  3199. }
  3200. else
  3201. if (strcmp(action, L("Unfollow")) == 0) { /** **/
  3202. /* get the following object */
  3203. xs *object = NULL;
  3204. if (valid_status(following_get(&snac, actor, &object))) {
  3205. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  3206. following_del(&snac, actor);
  3207. enqueue_output_by_actor(&snac, msg, actor, 0);
  3208. snac_log(&snac, xs_fmt("unfollowed actor %s", actor));
  3209. }
  3210. else
  3211. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  3212. }
  3213. else
  3214. if (strcmp(action, L("Follow Group")) == 0) { /** **/
  3215. xs *msg = msg_follow(&snac, group);
  3216. if (msg != NULL) {
  3217. /* reload the group from the message, in may be different */
  3218. group = xs_dict_get(msg, "object");
  3219. following_add(&snac, group, msg);
  3220. enqueue_output_by_actor(&snac, msg, group, 0);
  3221. }
  3222. }
  3223. else
  3224. if (strcmp(action, L("Unfollow Group")) == 0) { /** **/
  3225. /* get the following object */
  3226. xs *object = NULL;
  3227. if (valid_status(following_get(&snac, group, &object))) {
  3228. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  3229. following_del(&snac, group);
  3230. enqueue_output_by_actor(&snac, msg, group, 0);
  3231. snac_log(&snac, xs_fmt("unfollowed group %s", group));
  3232. }
  3233. else
  3234. snac_log(&snac, xs_fmt("actor is not being followed %s", actor));
  3235. }
  3236. else
  3237. if (strcmp(action, L("Delete")) == 0) { /** **/
  3238. const char *actor_form = xs_dict_get(p_vars, "actor-form");
  3239. if (actor_form != NULL) {
  3240. /* delete follower */
  3241. if (valid_status(follower_del(&snac, actor)))
  3242. snac_log(&snac, xs_fmt("deleted follower %s", actor));
  3243. else
  3244. snac_log(&snac, xs_fmt("error deleting follower %s", actor));
  3245. }
  3246. else {
  3247. /* delete an entry */
  3248. if (xs_startswith(id, snac.actor) && !is_draft(&snac, id)) {
  3249. /* it's a post by us: generate a delete */
  3250. xs *msg = msg_delete(&snac, id);
  3251. enqueue_message(&snac, msg);
  3252. snac_log(&snac, xs_fmt("posted tombstone for %s", id));
  3253. }
  3254. timeline_del(&snac, id);
  3255. draft_del(&snac, id);
  3256. snac_log(&snac, xs_fmt("deleted entry %s", id));
  3257. }
  3258. }
  3259. else
  3260. if (strcmp(action, L("Pin")) == 0) { /** **/
  3261. pin(&snac, id);
  3262. timeline_touch(&snac);
  3263. }
  3264. else
  3265. if (strcmp(action, L("Unpin")) == 0) { /** **/
  3266. unpin(&snac, id);
  3267. timeline_touch(&snac);
  3268. }
  3269. else
  3270. if (strcmp(action, L("Bookmark")) == 0) { /** **/
  3271. bookmark(&snac, id);
  3272. timeline_touch(&snac);
  3273. }
  3274. else
  3275. if (strcmp(action, L("Unbookmark")) == 0) { /** **/
  3276. unbookmark(&snac, id);
  3277. timeline_touch(&snac);
  3278. }
  3279. else
  3280. if (strcmp(action, L("Approve")) == 0) { /** **/
  3281. xs *fwreq = pending_get(&snac, actor);
  3282. if (fwreq != NULL) {
  3283. xs *reply = msg_accept(&snac, fwreq, actor);
  3284. enqueue_message(&snac, reply);
  3285. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  3286. /* add a date if it doesn't include one (Mastodon) */
  3287. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  3288. fwreq = xs_dict_set(fwreq, "published", date);
  3289. }
  3290. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  3291. follower_add(&snac, actor);
  3292. pending_del(&snac, actor);
  3293. snac_log(&snac, xs_fmt("new follower %s", actor));
  3294. }
  3295. }
  3296. else
  3297. if (strcmp(action, L("Discard")) == 0) { /** **/
  3298. pending_del(&snac, actor);
  3299. }
  3300. else
  3301. status = HTTP_STATUS_NOT_FOUND;
  3302. /* delete the cached timeline */
  3303. if (status == HTTP_STATUS_SEE_OTHER)
  3304. history_del(&snac, "timeline.html_");
  3305. }
  3306. else
  3307. if (p_path && strcmp(p_path, "admin/user-setup") == 0) { /** **/
  3308. /* change of user data */
  3309. const char *v;
  3310. const char *p1, *p2;
  3311. if ((v = xs_dict_get(p_vars, "name")) != NULL)
  3312. snac.config = xs_dict_set(snac.config, "name", v);
  3313. if ((v = xs_dict_get(p_vars, "avatar")) != NULL)
  3314. snac.config = xs_dict_set(snac.config, "avatar", v);
  3315. if ((v = xs_dict_get(p_vars, "bio")) != NULL)
  3316. snac.config = xs_dict_set(snac.config, "bio", v);
  3317. if ((v = xs_dict_get(p_vars, "cw")) != NULL &&
  3318. strcmp(v, "on") == 0) {
  3319. snac.config = xs_dict_set(snac.config, "cw", "open");
  3320. } else { /* if the checkbox is not set, the parameter is missing */
  3321. snac.config = xs_dict_set(snac.config, "cw", "");
  3322. }
  3323. if ((v = xs_dict_get(p_vars, "email")) != NULL)
  3324. snac.config = xs_dict_set(snac.config, "email", v);
  3325. if ((v = xs_dict_get(p_vars, "telegram_bot")) != NULL)
  3326. snac.config = xs_dict_set(snac.config, "telegram_bot", v);
  3327. if ((v = xs_dict_get(p_vars, "telegram_chat_id")) != NULL)
  3328. snac.config = xs_dict_set(snac.config, "telegram_chat_id", v);
  3329. if ((v = xs_dict_get(p_vars, "ntfy_server")) != NULL)
  3330. snac.config = xs_dict_set(snac.config, "ntfy_server", v);
  3331. if ((v = xs_dict_get(p_vars, "ntfy_token")) != NULL)
  3332. snac.config = xs_dict_set(snac.config, "ntfy_token", v);
  3333. if ((v = xs_dict_get(p_vars, "purge_days")) != NULL) {
  3334. xs *days = xs_number_new(atof(v));
  3335. snac.config = xs_dict_set(snac.config, "purge_days", days);
  3336. }
  3337. if ((v = xs_dict_get(p_vars, "drop_dm_from_unknown")) != NULL && strcmp(v, "on") == 0)
  3338. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_TRUE));
  3339. else
  3340. snac.config = xs_dict_set(snac.config, "drop_dm_from_unknown", xs_stock(XSTYPE_FALSE));
  3341. if ((v = xs_dict_get(p_vars, "bot")) != NULL && strcmp(v, "on") == 0)
  3342. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_TRUE));
  3343. else
  3344. snac.config = xs_dict_set(snac.config, "bot", xs_stock(XSTYPE_FALSE));
  3345. if ((v = xs_dict_get(p_vars, "private")) != NULL && strcmp(v, "on") == 0)
  3346. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_TRUE));
  3347. else
  3348. snac.config = xs_dict_set(snac.config, "private", xs_stock(XSTYPE_FALSE));
  3349. if ((v = xs_dict_get(p_vars, "auto_boost")) != NULL && strcmp(v, "on") == 0)
  3350. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_TRUE));
  3351. else
  3352. snac.config = xs_dict_set(snac.config, "auto_boost", xs_stock(XSTYPE_FALSE));
  3353. if ((v = xs_dict_get(p_vars, "collapse_threads")) != NULL && strcmp(v, "on") == 0)
  3354. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_TRUE));
  3355. else
  3356. snac.config = xs_dict_set(snac.config, "collapse_threads", xs_stock(XSTYPE_FALSE));
  3357. if ((v = xs_dict_get(p_vars, "approve_followers")) != NULL && strcmp(v, "on") == 0)
  3358. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_TRUE));
  3359. else
  3360. snac.config = xs_dict_set(snac.config, "approve_followers", xs_stock(XSTYPE_FALSE));
  3361. if ((v = xs_dict_get(p_vars, "show_contact_metrics")) != NULL && strcmp(v, "on") == 0)
  3362. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_TRUE));
  3363. else
  3364. snac.config = xs_dict_set(snac.config, "show_contact_metrics", xs_stock(XSTYPE_FALSE));
  3365. if ((v = xs_dict_get(p_vars, "metadata")) != NULL)
  3366. snac.config = xs_dict_set(snac.config, "metadata", v);
  3367. /* uploads */
  3368. const char *uploads[] = { "avatar", "header", NULL };
  3369. int n;
  3370. for (n = 0; uploads[n]; n++) {
  3371. xs *var_name = xs_fmt("%s_file", uploads[n]);
  3372. const xs_list *uploaded_file = xs_dict_get(p_vars, var_name);
  3373. if (xs_type(uploaded_file) == XSTYPE_LIST) {
  3374. const char *fn = xs_list_get(uploaded_file, 0);
  3375. if (fn && *fn) {
  3376. const char *mimetype = xs_mime_by_ext(fn);
  3377. if (xs_startswith(mimetype, "image/")) {
  3378. const char *ext = strrchr(fn, '.');
  3379. xs *hash = xs_md5_hex(fn, strlen(fn));
  3380. xs *id = xs_fmt("%s%s", hash, ext);
  3381. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  3382. int fo = xs_number_get(xs_list_get(uploaded_file, 1));
  3383. int fs = xs_number_get(xs_list_get(uploaded_file, 2));
  3384. /* store */
  3385. static_put(&snac, id, payload + fo, fs);
  3386. snac.config = xs_dict_set(snac.config, uploads[n], url);
  3387. }
  3388. }
  3389. }
  3390. }
  3391. /* delete images by removing url from user.json */
  3392. for (n = 0; uploads[n]; n++) {
  3393. xs *var_name = xs_fmt("%s_delete", uploads[n]);
  3394. const char *delete_var = xs_dict_get(p_vars, var_name);
  3395. if (delete_var != NULL && strcmp(delete_var, "on") == 0) {
  3396. snac.config = xs_dict_set(snac.config, uploads[n], "");
  3397. }
  3398. }
  3399. /* password change? */
  3400. if ((p1 = xs_dict_get(p_vars, "passwd1")) != NULL &&
  3401. (p2 = xs_dict_get(p_vars, "passwd2")) != NULL &&
  3402. *p1 && strcmp(p1, p2) == 0) {
  3403. xs *pw = hash_password(snac.uid, p1, NULL);
  3404. snac.config = xs_dict_set(snac.config, "passwd", pw);
  3405. }
  3406. user_persist(&snac, 1);
  3407. status = HTTP_STATUS_SEE_OTHER;
  3408. }
  3409. else
  3410. if (p_path && strcmp(p_path, "admin/clear-notifications") == 0) { /** **/
  3411. notify_clear(&snac);
  3412. timeline_touch(&snac);
  3413. status = HTTP_STATUS_SEE_OTHER;
  3414. }
  3415. else
  3416. if (p_path && strcmp(p_path, "admin/vote") == 0) { /** **/
  3417. const char *irt = xs_dict_get(p_vars, "irt");
  3418. const char *opt = xs_dict_get(p_vars, "question");
  3419. const char *actor = xs_dict_get(p_vars, "actor");
  3420. xs *ls = NULL;
  3421. /* multiple choices? */
  3422. if (xs_type(opt) == XSTYPE_LIST)
  3423. ls = xs_dup(opt);
  3424. else
  3425. if (xs_type(opt) == XSTYPE_STRING) {
  3426. ls = xs_list_new();
  3427. ls = xs_list_append(ls, opt);
  3428. }
  3429. const xs_str *v;
  3430. int c = 0;
  3431. while (xs_list_next(ls, &v, &c)) {
  3432. xs *msg = msg_note(&snac, "", actor, irt, NULL, 1, NULL);
  3433. /* set the option */
  3434. msg = xs_dict_append(msg, "name", v);
  3435. xs *c_msg = msg_create(&snac, msg);
  3436. enqueue_message(&snac, c_msg);
  3437. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  3438. }
  3439. if (ls != NULL) {
  3440. /* get the poll object */
  3441. xs *poll = NULL;
  3442. if (valid_status(object_get(irt, &poll))) {
  3443. const char *date = xs_dict_get(poll, "endTime");
  3444. if (xs_is_null(date))
  3445. date = xs_dict_get(poll, "closed");
  3446. if (!xs_is_null(date)) {
  3447. time_t t = xs_parse_iso_date(date, 0) - time(NULL);
  3448. /* request the poll when it's closed;
  3449. Pleroma does not send and update when the poll closes */
  3450. enqueue_object_request(&snac, irt, t + 2);
  3451. }
  3452. }
  3453. }
  3454. status = HTTP_STATUS_SEE_OTHER;
  3455. }
  3456. if (status == HTTP_STATUS_SEE_OTHER) {
  3457. const char *redir = xs_dict_get(p_vars, "redir");
  3458. if (xs_is_null(redir))
  3459. redir = "top";
  3460. *body = xs_fmt("%s/admin#%s", snac.actor, redir);
  3461. *b_size = strlen(*body);
  3462. }
  3463. user_free(&snac);
  3464. return status;
  3465. }
  3466. xs_str *timeline_to_rss(snac *user, const xs_list *timeline,
  3467. const char *title, const char *link, const char *desc)
  3468. /* converts a timeline to rss */
  3469. {
  3470. xs_html *rss = xs_html_tag("rss",
  3471. xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
  3472. xs_html_attr("version", "2.0"),
  3473. xs_html_attr("xmlns:atom", "http:/" "/www.w3.org/2005/Atom"));
  3474. xs_html *channel = xs_html_tag("channel",
  3475. xs_html_tag("title",
  3476. xs_html_text(title)),
  3477. xs_html_tag("language",
  3478. xs_html_text("en")),
  3479. xs_html_tag("link",
  3480. xs_html_text(link)),
  3481. xs_html_sctag("atom:link",
  3482. xs_html_attr("href", link),
  3483. xs_html_attr("rel", "self"),
  3484. xs_html_attr("type", "application/rss+xml")),
  3485. xs_html_tag("generator",
  3486. xs_html_text(USER_AGENT)),
  3487. xs_html_tag("description",
  3488. xs_html_text(desc)));
  3489. xs_html_add(rss, channel);
  3490. int cnt = 0;
  3491. const char *v;
  3492. xs_list_foreach(timeline, v) {
  3493. xs *msg = NULL;
  3494. if (user) {
  3495. if (!valid_status(timeline_get_by_md5(user, v, &msg)))
  3496. continue;
  3497. }
  3498. else {
  3499. if (!valid_status(object_get_by_md5(v, &msg)))
  3500. continue;
  3501. }
  3502. const char *id = xs_dict_get(msg, "id");
  3503. const char *content = xs_dict_get(msg, "content");
  3504. const char *published = xs_dict_get(msg, "published");
  3505. if (user && !xs_startswith(id, user->actor))
  3506. continue;
  3507. if (!id || !content || !published)
  3508. continue;
  3509. /* create a title with the first line of the content */
  3510. xs *title = xs_replace(content, "<br>", "\n");
  3511. title = xs_regex_replace_i(title, "<[^>]+>", " ");
  3512. title = xs_regex_replace_i(title, "&[^;]+;", " ");
  3513. int i;
  3514. for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
  3515. if (title[i] != '\0') {
  3516. title[i] = '\0';
  3517. title = xs_str_cat(title, "...");
  3518. }
  3519. title = xs_strip_i(title);
  3520. /* convert the date */
  3521. time_t t = xs_parse_iso_date(published, 0);
  3522. xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
  3523. /* if it's the first one, add it to the header */
  3524. if (cnt == 0)
  3525. xs_html_add(channel,
  3526. xs_html_tag("lastBuildDate",
  3527. xs_html_text(rss_date)));
  3528. xs_html_add(channel,
  3529. xs_html_tag("item",
  3530. xs_html_tag("title",
  3531. xs_html_text(title)),
  3532. xs_html_tag("link",
  3533. xs_html_text(id)),
  3534. xs_html_tag("guid",
  3535. xs_html_text(id)),
  3536. xs_html_tag("pubDate",
  3537. xs_html_text(rss_date)),
  3538. xs_html_tag("description",
  3539. xs_html_text(content))));
  3540. cnt++;
  3541. }
  3542. return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  3543. }