data.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_hex.h"
  5. #include "xs_io.h"
  6. #include "xs_json.h"
  7. #include "xs_openssl.h"
  8. #include "xs_glob.h"
  9. #include "xs_set.h"
  10. #include "xs_time.h"
  11. #include "xs_regex.h"
  12. #include "xs_match.h"
  13. #include "xs_unicode.h"
  14. #include "xs_random.h"
  15. #include "xs_po.h"
  16. #include "xs_http.h"
  17. #include "snac.h"
  18. #include <time.h>
  19. #include <sys/stat.h>
  20. #include <sys/file.h>
  21. #include <sys/time.h>
  22. #include <fcntl.h>
  23. #include <pthread.h>
  24. double disk_layout = 2.7;
  25. /* storage serializer */
  26. pthread_mutex_t data_mutex = {0};
  27. int snac_upgrade(xs_str **error);
  28. int srv_open(const char *basedir, int auto_upgrade)
  29. /* opens a server */
  30. {
  31. int ret = 0;
  32. xs *cfg_file = NULL;
  33. FILE *f;
  34. xs_str *error = NULL;
  35. /* MUST be three letters */
  36. months[0] = LL("Jan");
  37. months[1] = LL("Feb");
  38. months[2] = LL("Mar");
  39. months[3] = LL("Apr");
  40. months[4] = LL("May");
  41. months[5] = LL("Jun");
  42. months[6] = LL("Jul");
  43. months[7] = LL("Aug");
  44. months[8] = LL("Sep");
  45. months[9] = LL("Oct");
  46. months[10] = LL("Nov");
  47. months[11] = LL("Dec");
  48. pthread_mutex_init(&data_mutex, NULL);
  49. srv_basedir = xs_str_new(basedir);
  50. if (xs_endswith(srv_basedir, "/"))
  51. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  52. cfg_file = xs_fmt("%s/server.json", basedir);
  53. if ((f = fopen(cfg_file, "r")) == NULL)
  54. error = xs_fmt("ERROR: cannot open '%s'", cfg_file);
  55. else {
  56. /* read full config file */
  57. srv_config = xs_json_load(f);
  58. fclose(f);
  59. /* parse */
  60. if (srv_config == NULL)
  61. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  62. else {
  63. const char *host;
  64. const char *prefix;
  65. const char *dbglvl;
  66. const char *proto;
  67. host = xs_dict_get(srv_config, "host");
  68. prefix = xs_dict_get(srv_config, "prefix");
  69. dbglvl = xs_dict_get(srv_config, "dbglevel");
  70. proto = xs_dict_get_def(srv_config, "protocol", "https");
  71. if (host == NULL || prefix == NULL)
  72. error = xs_str_new("ERROR: cannot get server data");
  73. else {
  74. srv_baseurl = xs_fmt("%s:/" "/%s%s", proto, host, prefix);
  75. dbglevel = (int) xs_number_get(dbglvl);
  76. if ((dbglvl = getenv("DEBUG")) != NULL) {
  77. dbglevel = atoi(dbglvl);
  78. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  79. }
  80. if (auto_upgrade)
  81. ret = snac_upgrade(&error);
  82. else if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
  83. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  84. else if (!check_strip_tool())
  85. error = xs_fmt("ERROR: strip_exif enabled but commands not found or working");
  86. else
  87. ret = 1;
  88. }
  89. }
  90. }
  91. if (error != NULL)
  92. srv_log(error);
  93. if (!ret)
  94. return ret;
  95. /* create the queue/ subdir, just in case */
  96. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  97. mkdirx(qdir);
  98. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  99. mkdirx(ibdir);
  100. xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
  101. mkdirx(tmpdir);
  102. xs *faildir = xs_fmt("%s/failure", srv_basedir);
  103. mkdirx(faildir);
  104. #ifdef __APPLE__
  105. /* Apple uses st_atimespec instead of st_atim etc */
  106. #define st_atim st_atimespec
  107. #define st_ctim st_ctimespec
  108. #define st_mtim st_mtimespec
  109. #endif
  110. sbox_enter(srv_basedir);
  111. /* read (and drop) emojis.json, possibly creating it */
  112. xs_free(emojis());
  113. /* if style.css does not exist, create it */
  114. xs *css_fn = xs_fmt("%s/style.css", srv_basedir);
  115. if (mtime(css_fn) == 0) {
  116. srv_log(xs_fmt("Writing style.css"));
  117. write_default_css();
  118. }
  119. /* create the proxy token seed */
  120. {
  121. char rnd[16];
  122. xs_rnd_buf(rnd, sizeof(rnd));
  123. srv_proxy_token_seed = xs_hex_enc(rnd, sizeof(rnd));
  124. }
  125. /* ensure user directories include important subdirectories */
  126. xs *users = user_list();
  127. const char *uid;
  128. xs_list_foreach(users, uid) {
  129. xs *impdir = xs_fmt("%s/user/%s/import", srv_basedir, uid);
  130. xs *expdir = xs_fmt("%s/user/%s/export", srv_basedir, uid);
  131. mkdirx(impdir);
  132. mkdirx(expdir);
  133. }
  134. /* languages */
  135. srv_langs = xs_dict_new();
  136. srv_langs = xs_dict_set(srv_langs, "en", xs_stock(XSTYPE_NULL));
  137. xs *l_dir = xs_fmt("%s/lang/", srv_basedir);
  138. mkdirx(l_dir);
  139. l_dir = xs_str_cat(l_dir, "*.po");
  140. xs *pos = xs_glob(l_dir, 0, 0);
  141. const char *po;
  142. xs_list_foreach(pos, po) {
  143. xs *d = xs_po_to_dict(po);
  144. if (xs_is_dict(d)) {
  145. xs *l = xs_split(po, "/");
  146. xs *id = xs_dup(xs_list_get(l, -1));
  147. id = xs_replace_i(id, ".po", "");
  148. srv_langs = xs_dict_set(srv_langs, id, d);
  149. }
  150. }
  151. return ret;
  152. }
  153. void srv_free(void)
  154. {
  155. xs_free(srv_basedir);
  156. xs_free(srv_config);
  157. xs_free(srv_baseurl);
  158. pthread_mutex_destroy(&data_mutex);
  159. }
  160. void user_free(snac *snac)
  161. /* frees a user snac */
  162. {
  163. xs_free(snac->uid);
  164. xs_free(snac->basedir);
  165. xs_free(snac->config);
  166. xs_free(snac->config_o);
  167. xs_free(snac->key);
  168. xs_free(snac->links);
  169. xs_free(snac->actor);
  170. xs_free(snac->actor_alt);
  171. xs_free(snac->md5);
  172. }
  173. int user_open(snac *user, const char *uid)
  174. /* opens a user */
  175. {
  176. int ret = 0;
  177. *user = (snac){0};
  178. if (validate_uid(uid)) {
  179. xs *cfg_file = NULL;
  180. FILE *f;
  181. xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
  182. if (mtime(t) == 0.0) {
  183. /* user folder does not exist; try with a different case */
  184. xs *lcuid = xs_tolower_i(xs_dup(uid));
  185. xs *ulist = user_list();
  186. xs_list *p = ulist;
  187. const xs_str *v;
  188. while (xs_list_iter(&p, &v)) {
  189. xs *v2 = xs_tolower_i(xs_dup(v));
  190. if (strcmp(lcuid, v2) == 0) {
  191. user->uid = xs_dup(v);
  192. break;
  193. }
  194. }
  195. }
  196. else
  197. user->uid = xs_str_new(uid);
  198. if (user->uid == NULL)
  199. return ret;
  200. user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
  201. cfg_file = xs_fmt("%s/user.json", user->basedir);
  202. if ((f = fopen(cfg_file, "r")) != NULL) {
  203. /* read full config file */
  204. user->config = xs_json_load(f);
  205. fclose(f);
  206. if (user->config != NULL) {
  207. xs *key_file = xs_fmt("%s/key.json", user->basedir);
  208. if ((f = fopen(key_file, "r")) != NULL) {
  209. user->key = xs_json_load(f);
  210. fclose(f);
  211. if (user->key != NULL) {
  212. user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
  213. user->actor_alt = xs_fmt("%s/users/%s", srv_baseurl ,user->uid);
  214. user->md5 = xs_md5_hex(user->actor, strlen(user->actor));
  215. /* everything is ok right now */
  216. ret = 1;
  217. /* does it have a configuration override? */
  218. xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
  219. if ((f = fopen(cfg_file_o, "r")) != NULL) {
  220. user->config_o = xs_json_load(f);
  221. fclose(f);
  222. if (user->config_o == NULL)
  223. srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
  224. }
  225. if (user->config_o == NULL)
  226. user->config_o = xs_dict_new();
  227. }
  228. else
  229. srv_log(xs_fmt("error parsing '%s'", key_file));
  230. }
  231. else
  232. srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
  233. }
  234. else
  235. srv_log(xs_fmt("error parsing '%s'", cfg_file));
  236. user->tz = xs_dict_get_def(user->config, "tz", "UTC");
  237. }
  238. else
  239. srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
  240. /* verified links */
  241. xs *links_file = xs_fmt("%s/links.json", user->basedir);
  242. if ((f = fopen(links_file, "r")) != NULL) {
  243. user->links = xs_json_load(f);
  244. fclose(f);
  245. }
  246. }
  247. else
  248. srv_debug(2, xs_fmt("invalid user '%s'", uid));
  249. if (!ret)
  250. user_free(user);
  251. return ret;
  252. }
  253. xs_list *user_list(void)
  254. /* returns the list of user ids */
  255. {
  256. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  257. return xs_glob(spec, 1, 0);
  258. }
  259. int user_open_by_md5(snac *snac, const char *md5)
  260. /* iterates all users searching by md5 */
  261. {
  262. xs *ulist = user_list();
  263. xs_list *p = ulist;
  264. const xs_str *v;
  265. while (xs_list_iter(&p, &v)) {
  266. user_open(snac, v);
  267. if (strcmp(snac->md5, md5) == 0)
  268. return 1;
  269. user_free(snac);
  270. }
  271. return 0;
  272. }
  273. int user_persist(snac *snac, int publish)
  274. /* store user */
  275. {
  276. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  277. xs *bfn = xs_fmt("%s.bak", fn);
  278. FILE *f;
  279. if (publish) {
  280. /* check if any of the relevant fields have really changed */
  281. if ((f = fopen(fn, "r")) != NULL) {
  282. xs *old = xs_json_load(f);
  283. fclose(f);
  284. if (old != NULL) {
  285. int nw = 0;
  286. const char *fields[] = { "header", "avatar", "name", "bio", "alias", "alias_raw",
  287. "metadata", "latitude", "longitude", NULL };
  288. for (int n = 0; fields[n]; n++) {
  289. const char *of = xs_dict_get(old, fields[n]);
  290. const char *nf = xs_dict_get(snac->config, fields[n]);
  291. if (of == NULL && nf == NULL)
  292. continue;
  293. if (xs_type(of) != XSTYPE_STRING || xs_type(nf) != XSTYPE_STRING || strcmp(of, nf)) {
  294. nw = 1;
  295. break;
  296. }
  297. }
  298. if (!nw)
  299. publish = 0;
  300. else {
  301. /* uncache the actor object */
  302. object_del(snac->actor);
  303. }
  304. }
  305. }
  306. }
  307. rename(fn, bfn);
  308. if ((f = fopen(fn, "w")) != NULL) {
  309. xs_json_dump(snac->config, 4, f);
  310. fclose(f);
  311. }
  312. else
  313. rename(bfn, fn);
  314. history_del(snac, "timeline.html_");
  315. timeline_touch(snac);
  316. if (publish) {
  317. xs *a_msg = msg_actor(snac);
  318. xs *u_msg = msg_update(snac, a_msg);
  319. enqueue_message(snac, u_msg);
  320. enqueue_verify_links(snac);
  321. }
  322. return 0;
  323. }
  324. double mtime_nl(const char *fn, int *n_link)
  325. /* returns the mtime and number of links of a file or directory, or 0.0 */
  326. {
  327. struct stat st;
  328. double r = 0.0;
  329. int n = 0;
  330. if (fn && stat(fn, &st) != -1) {
  331. r = (double) st.st_mtim.tv_sec;
  332. n = st.st_nlink;
  333. }
  334. if (n_link)
  335. *n_link = n;
  336. return r;
  337. }
  338. #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
  339. double f_ctime(const char *fn)
  340. /* returns the ctime of a file or directory, or 0.0 */
  341. {
  342. struct stat st;
  343. double r = 0.0;
  344. if (fn && stat(fn, &st) != -1) {
  345. /* return the lowest of ctime and mtime;
  346. there are operations that change the ctime, like link() */
  347. r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
  348. }
  349. return r;
  350. }
  351. int is_md5_hex(const char *md5)
  352. {
  353. return xs_is_hex(md5) && strlen(md5) == MD5_HEX_SIZE - 1;
  354. }
  355. /** database 2.1+ **/
  356. /** indexes **/
  357. int index_add_md5(const char *fn, const char *md5)
  358. /* adds an md5 to an index */
  359. {
  360. int status = HTTP_STATUS_CREATED;
  361. FILE *f;
  362. if (!is_md5_hex(md5)) {
  363. srv_log(xs_fmt("index_add_md5: bad md5 %s %s", fn, md5));
  364. return HTTP_STATUS_BAD_REQUEST;
  365. }
  366. pthread_mutex_lock(&data_mutex);
  367. if ((f = fopen(fn, "a")) != NULL) {
  368. flock(fileno(f), LOCK_EX);
  369. /* ensure the position is at the end after getting the lock */
  370. fseek(f, 0, SEEK_END);
  371. fprintf(f, "%s\n", md5);
  372. fclose(f);
  373. }
  374. else
  375. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  376. pthread_mutex_unlock(&data_mutex);
  377. return status;
  378. }
  379. int index_add(const char *fn, const char *id)
  380. /* adds an id to an index */
  381. {
  382. xs *md5 = xs_md5_hex(id, strlen(id));
  383. return index_add_md5(fn, md5);
  384. }
  385. int index_del_md5(const char *fn, const char *md5)
  386. /* deletes an md5 from an index */
  387. {
  388. int status = HTTP_STATUS_NOT_FOUND;
  389. FILE *f;
  390. pthread_mutex_lock(&data_mutex);
  391. if ((f = fopen(fn, "r+")) != NULL) {
  392. char line[256];
  393. while (fgets(line, sizeof(line), f) != NULL) {
  394. line[MD5_HEX_SIZE - 1] = '\0';
  395. if (strcmp(line, md5) == 0) {
  396. /* found! just rewind, overwrite it with garbage
  397. and an eventual call to index_gc() will clean it
  398. [yes: this breaks index_len()] */
  399. fseek(f, -MD5_HEX_SIZE, SEEK_CUR);
  400. fwrite("-", 1, 1, f);
  401. status = HTTP_STATUS_OK;
  402. break;
  403. }
  404. }
  405. fclose(f);
  406. }
  407. else
  408. status = HTTP_STATUS_GONE;
  409. pthread_mutex_unlock(&data_mutex);
  410. return status;
  411. }
  412. int index_del(const char *fn, const char *id)
  413. /* deletes an id from an index */
  414. {
  415. xs *md5 = xs_md5_hex(id, strlen(id));
  416. return index_del_md5(fn, md5);
  417. }
  418. int index_gc(const char *fn)
  419. /* garbage-collects an index, deleting objects that are not here */
  420. {
  421. FILE *i, *o;
  422. int gc = -1;
  423. pthread_mutex_lock(&data_mutex);
  424. if ((i = fopen(fn, "r")) != NULL) {
  425. xs *nfn = xs_fmt("%s.new", fn);
  426. char line[256];
  427. if ((o = fopen(nfn, "w")) != NULL) {
  428. gc = 0;
  429. while (fgets(line, sizeof(line), i) != NULL) {
  430. line[MD5_HEX_SIZE - 1] = '\0';
  431. if (line[0] != '-' && object_here_by_md5(line))
  432. fprintf(o, "%s\n", line);
  433. else
  434. gc++;
  435. }
  436. fclose(o);
  437. xs *ofn = xs_fmt("%s.bak", fn);
  438. unlink(ofn);
  439. link(fn, ofn);
  440. rename(nfn, fn);
  441. }
  442. fclose(i);
  443. }
  444. pthread_mutex_unlock(&data_mutex);
  445. return gc;
  446. }
  447. int index_in_md5(const char *fn, const char *md5)
  448. /* checks if the md5 is already in the index */
  449. {
  450. FILE *f;
  451. int ret = 0;
  452. if ((f = fopen(fn, "r")) != NULL) {
  453. flock(fileno(f), LOCK_SH);
  454. char line[256];
  455. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  456. line[MD5_HEX_SIZE - 1] = '\0';
  457. if (strcmp(line, md5) == 0)
  458. ret = 1;
  459. }
  460. fclose(f);
  461. }
  462. return ret;
  463. }
  464. int index_in(const char *fn, const char *id)
  465. /* checks if the object id is already in the index */
  466. {
  467. xs *md5 = xs_md5_hex(id, strlen(id));
  468. return index_in_md5(fn, md5);
  469. }
  470. int index_first(const char *fn, char md5[MD5_HEX_SIZE])
  471. /* reads the first entry of an index */
  472. {
  473. FILE *f;
  474. int ret = 0;
  475. if ((f = fopen(fn, "r")) != NULL) {
  476. if (fread(md5, MD5_HEX_SIZE, 1, f)) {
  477. md5[MD5_HEX_SIZE - 1] = '\0';
  478. ret = 1;
  479. }
  480. fclose(f);
  481. }
  482. return ret;
  483. }
  484. int index_len(const char *fn)
  485. /* returns the number of elements in an index */
  486. {
  487. struct stat st;
  488. int len = 0;
  489. if (stat(fn, &st) != -1)
  490. len = st.st_size / MD5_HEX_SIZE;
  491. return len;
  492. }
  493. xs_list *index_list(const char *fn, int max)
  494. /* returns an index as a list */
  495. {
  496. xs_list *list = xs_list_new();
  497. FILE *f;
  498. int n = 0;
  499. if ((f = fopen(fn, "r")) != NULL) {
  500. flock(fileno(f), LOCK_SH);
  501. char line[256];
  502. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  503. if (line[0] != '-') {
  504. line[MD5_HEX_SIZE - 1] = '\0';
  505. list = xs_list_append(list, line);
  506. n++;
  507. }
  508. }
  509. fclose(f);
  510. }
  511. return list;
  512. }
  513. int index_desc_next(FILE *f, char md5[MD5_HEX_SIZE])
  514. /* reads the next entry of a desc index */
  515. {
  516. for (;;) {
  517. /* move backwards 2 entries */
  518. if (fseek(f, MD5_HEX_SIZE * -2, SEEK_CUR) == -1)
  519. return 0;
  520. /* read and md5 */
  521. if (!fread(md5, MD5_HEX_SIZE, 1, f))
  522. return 0;
  523. if (md5[0] != '-')
  524. break;
  525. }
  526. md5[MD5_HEX_SIZE - 1] = '\0';
  527. return 1;
  528. }
  529. int index_desc_first(FILE *f, char md5[MD5_HEX_SIZE], int skip)
  530. /* reads the first entry of a desc index */
  531. {
  532. /* try to position at the end and then back to the first element */
  533. if (fseek(f, 0, SEEK_END) || fseek(f, (skip + 1) * -MD5_HEX_SIZE, SEEK_CUR))
  534. return 0;
  535. /* try to read an md5 */
  536. if (!fread(md5, MD5_HEX_SIZE, 1, f))
  537. return 0;
  538. /* null-terminate */
  539. md5[MD5_HEX_SIZE - 1] = '\0';
  540. /* deleted? retry next */
  541. if (md5[0] == '-')
  542. return index_desc_next(f, md5);
  543. return 1;
  544. }
  545. int index_asc_first(FILE *f,char md5[MD5_HEX_SIZE], const char *seek_md5)
  546. /* reads the first entry of an ascending index, starting from a given md5 */
  547. {
  548. fseek(f, SEEK_SET, 0);
  549. while (fread(md5, MD5_HEX_SIZE, 1, f)) {
  550. md5[MD5_HEX_SIZE - 1] = '\0';
  551. if (strcmp(md5,seek_md5) == 0) {
  552. return index_asc_next(f, md5);
  553. }
  554. }
  555. return 0;
  556. }
  557. int index_asc_next(FILE *f, char md5[MD5_HEX_SIZE])
  558. /* reads the next entry of an ascending index */
  559. {
  560. for (;;) {
  561. /* read an md5 */
  562. if (!fread(md5, MD5_HEX_SIZE, 1, f))
  563. return 0;
  564. /* deleted, skip */
  565. if (md5[0] != '-')
  566. break;
  567. }
  568. md5[MD5_HEX_SIZE - 1] = '\0';
  569. return 1;
  570. }
  571. xs_list *index_list_desc(const char *fn, int skip, int show)
  572. /* returns an index as a list, in reverse order */
  573. {
  574. xs_list *list = xs_list_new();
  575. FILE *f;
  576. if ((f = fopen(fn, "r")) != NULL) {
  577. char md5[MD5_HEX_SIZE];
  578. if (index_desc_first(f, md5, skip)) {
  579. int n = 1;
  580. do {
  581. list = xs_list_append(list, md5);
  582. } while (n++ < show && index_desc_next(f, md5));
  583. }
  584. fclose(f);
  585. }
  586. return list;
  587. }
  588. /** objects **/
  589. static xs_str *_object_fn_by_md5(const char *md5, const char *func)
  590. {
  591. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  592. xs_str *ret;
  593. int ok = 1;
  594. /* an object deleted from an index; fail but don't bark */
  595. if (md5[0] == '-')
  596. ok = 0;
  597. else
  598. if (!is_md5_hex(md5)) {
  599. srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
  600. ok = 0;
  601. }
  602. if (ok) {
  603. mkdirx(bfn);
  604. ret = xs_fmt("%s/%s.json", bfn, md5);
  605. }
  606. else
  607. ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
  608. return ret;
  609. }
  610. static xs_str *_object_fn(const char *id)
  611. {
  612. xs *md5 = xs_md5_hex(id, strlen(id));
  613. return _object_fn_by_md5(md5, "_object_fn");
  614. }
  615. int object_here_by_md5(const char *id)
  616. /* checks if an object is already downloaded */
  617. {
  618. xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
  619. return mtime(fn) > 0.0;
  620. }
  621. int object_here(const char *id)
  622. /* checks if an object is already downloaded */
  623. {
  624. xs *fn = _object_fn(id);
  625. return mtime(fn) > 0.0;
  626. }
  627. int object_get_by_md5(const char *md5, xs_dict **obj)
  628. /* returns a stored object, optionally of the requested type */
  629. {
  630. int status = HTTP_STATUS_NOT_FOUND;
  631. xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
  632. FILE *f;
  633. if ((f = fopen(fn, "r")) != NULL) {
  634. *obj = xs_json_load(f);
  635. fclose(f);
  636. if (*obj)
  637. status = HTTP_STATUS_OK;
  638. }
  639. else
  640. *obj = NULL;
  641. return status;
  642. }
  643. int object_get(const char *id, xs_dict **obj)
  644. /* returns a stored object, optionally of the requested type */
  645. {
  646. xs *md5 = xs_md5_hex(id, strlen(id));
  647. return object_get_by_md5(md5, obj);
  648. }
  649. int _object_add(const char *id, const xs_dict *obj, int ow)
  650. /* stores an object */
  651. {
  652. int status = HTTP_STATUS_CREATED; /* Created */
  653. xs *fn = _object_fn(id);
  654. FILE *f;
  655. if (mtime(fn) > 0.0) {
  656. if (!ow) {
  657. /* object already here */
  658. srv_debug(1, xs_fmt("object_add object already here %s", id));
  659. return HTTP_STATUS_NO_CONTENT;
  660. }
  661. else
  662. status = HTTP_STATUS_OK;
  663. }
  664. if ((f = fopen(fn, "w")) != NULL) {
  665. flock(fileno(f), LOCK_EX);
  666. xs_json_dump(obj, 4, f);
  667. fclose(f);
  668. /* does this object has a parent? */
  669. const char *in_reply_to = get_in_reply_to(obj);
  670. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  671. /* update the children index of the parent */
  672. xs *c_idx = _object_fn(in_reply_to);
  673. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  674. if (!index_in(c_idx, id)) {
  675. index_add(c_idx, id);
  676. srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
  677. }
  678. else
  679. srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
  680. /* create a one-element index with the parent */
  681. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  682. if (mtime(p_idx) == 0.0) {
  683. index_add(p_idx, in_reply_to);
  684. srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  685. }
  686. }
  687. }
  688. else {
  689. srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
  690. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  691. }
  692. srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
  693. return status;
  694. }
  695. int object_add(const char *id, const xs_dict *obj)
  696. /* stores an object */
  697. {
  698. return _object_add(id, obj, 0);
  699. }
  700. int object_add_ow(const char *id, const xs_dict *obj)
  701. /* stores an object (overwriting allowed) */
  702. {
  703. return _object_add(id, obj, 1);
  704. }
  705. int object_del_by_md5(const char *md5)
  706. /* deletes an object by its md5 */
  707. {
  708. int status = HTTP_STATUS_NOT_FOUND;
  709. xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
  710. if (unlink(fn) != -1) {
  711. status = HTTP_STATUS_OK;
  712. /* also delete associated indexes */
  713. xs *spec = xs_dup(fn);
  714. spec = xs_replace_i(spec, ".json", "*.idx");
  715. xs *files = xs_glob(spec, 0, 0);
  716. char *p;
  717. const char *v;
  718. p = files;
  719. while (xs_list_iter(&p, &v)) {
  720. srv_debug(1, xs_fmt("object_del index %s", v));
  721. unlink(v);
  722. }
  723. }
  724. srv_debug(1, xs_fmt("object_del %s %d", fn, status));
  725. return status;
  726. }
  727. int object_del(const char *id)
  728. /* deletes an object */
  729. {
  730. xs *md5 = xs_md5_hex(id, strlen(id));
  731. return object_del_by_md5(md5);
  732. }
  733. int object_del_if_unref(const char *id)
  734. /* deletes an object if its n_links < 2 */
  735. {
  736. xs *fn = _object_fn(id);
  737. int n_links;
  738. int ret = 0;
  739. if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
  740. ret = object_del(id);
  741. return ret;
  742. }
  743. double object_ctime_by_md5(const char *md5)
  744. {
  745. xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
  746. return f_ctime(fn);
  747. }
  748. double object_ctime(const char *id)
  749. {
  750. xs *md5 = xs_md5_hex(id, strlen(id));
  751. return object_ctime_by_md5(md5);
  752. }
  753. double object_mtime_by_md5(const char *md5)
  754. {
  755. xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
  756. return mtime(fn);
  757. }
  758. double object_mtime(const char *id)
  759. {
  760. xs *md5 = xs_md5_hex(id, strlen(id));
  761. return object_mtime_by_md5(md5);
  762. }
  763. void object_touch(const char *id)
  764. {
  765. xs *md5 = xs_md5_hex(id, strlen(id));
  766. xs *fn = _object_fn_by_md5(md5, "object_touch");
  767. if (mtime(fn))
  768. utimes(fn, NULL);
  769. }
  770. xs_str *_object_index_fn(const char *id, const char *idxsfx)
  771. /* returns the filename of an object's index */
  772. {
  773. xs_str *fn = _object_fn(id);
  774. return xs_replace_i(fn, ".json", idxsfx);
  775. }
  776. int object_likes_len(const char *id)
  777. /* returns the number of likes (without reading the index) */
  778. {
  779. xs *fn = _object_index_fn(id, "_l.idx");
  780. return index_len(fn);
  781. }
  782. int object_announces_len(const char *id)
  783. /* returns the number of announces (without reading the index) */
  784. {
  785. xs *fn = _object_index_fn(id, "_a.idx");
  786. return index_len(fn);
  787. }
  788. int object_emojireacts_len(const char *id)
  789. /* returns the number of emojireacts (without reading the index) */
  790. {
  791. xs *fn = _object_index_fn(id, "_e.idx");
  792. return index_len(fn);
  793. }
  794. xs_list *object_children(const char *id)
  795. /* returns the list of an object's children */
  796. {
  797. xs *fn = _object_index_fn(id, "_c.idx");
  798. return index_list(fn, XS_ALL);
  799. }
  800. xs_list *object_get_emoji_reacts(const char *id)
  801. /* returns the list of an object's emoji reactions */
  802. {
  803. xs *fn = _object_index_fn(id, "_e.idx");
  804. return index_list(fn, XS_ALL);
  805. }
  806. xs_list *object_likes(const char *id)
  807. {
  808. xs *fn = _object_index_fn(id, "_l.idx");
  809. return index_list(fn, XS_ALL);
  810. }
  811. xs_list *object_announces(const char *id)
  812. {
  813. xs *fn = _object_index_fn(id, "_a.idx");
  814. return index_list(fn, XS_ALL);
  815. }
  816. int object_parent(const char *md5, char parent[MD5_HEX_SIZE])
  817. /* returns the object parent, if any */
  818. {
  819. xs *fn = _object_fn_by_md5(md5, "object_parent");
  820. fn = xs_replace_i(fn, ".json", "_p.idx");
  821. return index_first(fn, parent);
  822. }
  823. int object_admire(const char *id, const char *actor, int like)
  824. /* actor likes or announces this object */
  825. {
  826. int status = HTTP_STATUS_OK;
  827. xs *fn = _object_fn(id);
  828. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  829. if (!index_in(fn, actor)) {
  830. status = index_add(fn, actor);
  831. srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  832. }
  833. return status;
  834. }
  835. int object_unadmire(const char *id, const char *actor, int like)
  836. /* actor retrives their likes, announces or emojis this object */
  837. {
  838. switch (like) {
  839. case 0:
  840. like = 'a';
  841. break;
  842. case 1:
  843. like = 'l';
  844. break;
  845. case 2:
  846. like = 'e';
  847. break;
  848. }
  849. int status;
  850. xs *fn = _object_fn(id);
  851. char sfx[7] = "_x.idx";
  852. sfx[1] = like;
  853. fn = xs_replace_i(fn, ".json", sfx);
  854. status = index_del(fn, actor);
  855. if (valid_status(status))
  856. index_gc(fn);
  857. srv_debug(0,
  858. xs_fmt("object_unadmire (%s) %s %s %d", like >= 'e' ?
  859. (like == 'l' ? "Like" : "EmojiReact") : "Announce" , actor, fn, status));
  860. return status;
  861. }
  862. int object_emoji_react(const char *mid, const char *eid)
  863. /* actor reacts w/ an emoji */
  864. {
  865. int status = HTTP_STATUS_OK;
  866. xs *fn = _object_fn(mid);
  867. fn = xs_replace_i(fn, ".json", "_e.idx");
  868. if (!index_in(fn, eid)) {
  869. status = index_add(fn, eid);
  870. srv_debug(1, xs_fmt("object_emoji_react (%s) added %s to %s", "EmojiReact", eid, fn));
  871. }
  872. return status;
  873. }
  874. int object_rm_emoji_react(const char *mid, const char *eid)
  875. /* actor retrives their emoji reaction */
  876. {
  877. int status;
  878. xs *fn = _object_fn(mid);
  879. fn = xs_replace_i(fn, ".json", "_e.idx");
  880. status = index_del(fn, eid);
  881. object_del(eid);
  882. if (valid_status(status))
  883. index_gc(fn);
  884. srv_debug(0,
  885. xs_fmt("object_unadmire (EmojiReact) %s %s %d", eid, fn, status));
  886. return status;
  887. }
  888. xs_str *object_user_cache_fn_by_md5(snac *user, const char *md5, const char *cachedir)
  889. {
  890. return xs_fmt("%s/%s/%s.json", user->basedir, cachedir, md5);
  891. }
  892. xs_str *object_user_cache_fn(snac *user, const char *id, const char *cachedir)
  893. {
  894. xs *md5 = xs_md5_hex(id, strlen(id));
  895. return object_user_cache_fn_by_md5(user, md5, cachedir);
  896. }
  897. xs_str *object_user_cache_index_fn(snac *user, const char *cachedir)
  898. {
  899. return xs_fmt("%s/%s.idx", user->basedir, cachedir);
  900. }
  901. int _object_user_cache(snac *user, const char *id, const char *cachedir, int del)
  902. /* adds or deletes from a user cache */
  903. {
  904. xs *ofn = _object_fn(id);
  905. xs *cfn = object_user_cache_fn(user, id, cachedir);
  906. xs *idx = object_user_cache_index_fn(user, cachedir);
  907. int ret;
  908. if (del) {
  909. ret = unlink(cfn);
  910. index_del(idx, id);
  911. }
  912. else {
  913. /* create the subfolder, if it does not exist */
  914. xs *dir = xs_fmt("%s/%s/", user->basedir, cachedir);
  915. mkdirx(dir);
  916. if ((ret = link(ofn, cfn)) != -1)
  917. index_add(idx, id);
  918. }
  919. return ret;
  920. }
  921. int object_user_cache_add(snac *user, const char *id, const char *cachedir)
  922. /* caches an object into a user cache */
  923. {
  924. return _object_user_cache(user, id, cachedir, 0);
  925. }
  926. int object_user_cache_del(snac *user, const char *id, const char *cachedir)
  927. /* deletes an object from a user cache */
  928. {
  929. return _object_user_cache(user, id, cachedir, 1);
  930. }
  931. int object_user_cache_in(snac *user, const char *id, const char *cachedir)
  932. /* checks if an object is stored in a cache */
  933. {
  934. xs *cfn = object_user_cache_fn(user, id, cachedir);
  935. return !!(mtime(cfn) != 0.0);
  936. }
  937. int object_user_cache_in_by_md5(snac *user, const char *md5, const char *cachedir)
  938. /* checks if an object is stored in a cache */
  939. {
  940. xs *cfn = object_user_cache_fn_by_md5(user, md5, cachedir);
  941. return !!(mtime(cfn) != 0.0);
  942. }
  943. xs_list *object_user_cache_list(snac *user, const char *cachedir, int max, int inv)
  944. /* returns the objects in a cache as a list */
  945. {
  946. xs *idx = xs_fmt("%s/%s.idx", user->basedir, cachedir);
  947. return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
  948. }
  949. /** specialized functions **/
  950. /** followers **/
  951. int follower_add(snac *snac, const char *actor)
  952. /* adds a follower */
  953. {
  954. int ret = object_user_cache_add(snac, actor, "followers");
  955. snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
  956. return ret == -1 ? HTTP_STATUS_INTERNAL_SERVER_ERROR : HTTP_STATUS_OK;
  957. }
  958. int follower_del(snac *snac, const char *actor)
  959. /* deletes a follower */
  960. {
  961. int ret = object_user_cache_del(snac, actor, "followers");
  962. snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
  963. return ret == -1 ? HTTP_STATUS_NOT_FOUND : HTTP_STATUS_OK;
  964. }
  965. int follower_check(snac *snac, const char *actor)
  966. /* checks if someone is a follower */
  967. {
  968. return object_user_cache_in(snac, actor, "followers");
  969. }
  970. int follower_list_len(snac *snac)
  971. /* returns the number of followers */
  972. {
  973. xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
  974. return xs_list_len(list);
  975. }
  976. xs_list *follower_list(snac *snac)
  977. /* returns the list of followers */
  978. {
  979. xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
  980. xs_list *fwers = xs_list_new();
  981. char *p;
  982. const char *v;
  983. /* resolve the list of md5 to be a list of actors */
  984. p = list;
  985. while (xs_list_iter(&p, &v)) {
  986. xs *a_obj = NULL;
  987. if (valid_status(object_get_by_md5(v, &a_obj))) {
  988. const char *actor = xs_dict_get(a_obj, "id");
  989. if (!xs_is_null(actor)) {
  990. /* check if the actor is still cached */
  991. xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
  992. if (mtime(fn) > 0.0)
  993. fwers = xs_list_append(fwers, actor);
  994. }
  995. }
  996. }
  997. return fwers;
  998. }
  999. /** pending followers **/
  1000. int pending_add(snac *user, const char *actor, const xs_dict *msg)
  1001. /* stores the follow message for later confirmation */
  1002. {
  1003. xs *dir = xs_fmt("%s/pending", user->basedir);
  1004. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1005. xs *fn = xs_fmt("%s/%s.json", dir, md5);
  1006. FILE *f;
  1007. mkdirx(dir);
  1008. if ((f = fopen(fn, "w")) == NULL)
  1009. return -1;
  1010. xs_json_dump(msg, 4, f);
  1011. fclose(f);
  1012. return 0;
  1013. }
  1014. int pending_check(snac *user, const char *actor)
  1015. /* checks if there is a pending follow confirmation for the actor */
  1016. {
  1017. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1018. xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
  1019. return mtime(fn) != 0;
  1020. }
  1021. xs_dict *pending_get(snac *user, const char *actor)
  1022. /* returns the pending follow confirmation for the actor */
  1023. {
  1024. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1025. xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
  1026. xs_dict *msg = NULL;
  1027. FILE *f;
  1028. if ((f = fopen(fn, "r")) != NULL) {
  1029. msg = xs_json_load(f);
  1030. fclose(f);
  1031. }
  1032. return msg;
  1033. }
  1034. void pending_del(snac *user, const char *actor)
  1035. /* deletes a pending follow confirmation for the actor */
  1036. {
  1037. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1038. xs *fn = xs_fmt("%s/pending/%s.json", user->basedir, md5);
  1039. unlink(fn);
  1040. }
  1041. xs_list *pending_list(snac *user)
  1042. /* returns a list of pending follow confirmations */
  1043. {
  1044. xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
  1045. xs *l = xs_glob(spec, 0, 0);
  1046. xs_list *r = xs_list_new();
  1047. const char *v;
  1048. xs_list_foreach(l, v) {
  1049. FILE *f;
  1050. xs *msg = NULL;
  1051. if ((f = fopen(v, "r")) == NULL)
  1052. continue;
  1053. msg = xs_json_load(f);
  1054. fclose(f);
  1055. if (msg == NULL)
  1056. continue;
  1057. const char *actor = xs_dict_get(msg, "actor");
  1058. if (xs_type(actor) == XSTYPE_STRING)
  1059. r = xs_list_append(r, actor);
  1060. }
  1061. return r;
  1062. }
  1063. int pending_count(snac *user)
  1064. /* returns the number of pending follow confirmations */
  1065. {
  1066. xs *spec = xs_fmt("%s/pending/""*.json", user->basedir);
  1067. xs *l = xs_glob(spec, 0, 0);
  1068. return xs_list_len(l);
  1069. }
  1070. int is_msg_mine(snac *user, const char *id)
  1071. /* returns true if a post id is by the given user */
  1072. {
  1073. int ret = 0;
  1074. if (xs_is_string(id)) {
  1075. xs *s1 = xs_fmt("%s/", user->actor);
  1076. ret = xs_startswith(id, s1);
  1077. }
  1078. return ret;
  1079. }
  1080. /** timeline **/
  1081. double timeline_mtime(snac *snac)
  1082. {
  1083. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  1084. return mtime(fn);
  1085. }
  1086. int timeline_touch(snac *snac)
  1087. /* changes the date of the timeline index */
  1088. {
  1089. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  1090. return utimes(fn, NULL);
  1091. }
  1092. xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
  1093. /* get the filename of an entry by md5 from any timeline */
  1094. {
  1095. xs_str *fn = NULL;
  1096. if (is_md5_hex(md5)) {
  1097. fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
  1098. if (mtime(fn) == 0.0) {
  1099. fn = xs_free(fn);
  1100. fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
  1101. if (mtime(fn) == 0.0)
  1102. fn = xs_free(fn);
  1103. }
  1104. }
  1105. return fn;
  1106. }
  1107. int timeline_here_by_md5(snac *snac, const char *md5)
  1108. /* checks if an object is in the user cache */
  1109. {
  1110. xs *fn = timeline_fn_by_md5(snac, md5);
  1111. return !(fn == NULL);
  1112. }
  1113. int timeline_here(snac *user, const char *id)
  1114. {
  1115. xs *md5 = xs_md5_hex(id, strlen(id));
  1116. return timeline_here_by_md5(user, md5);
  1117. }
  1118. int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
  1119. /* gets a message from the timeline */
  1120. {
  1121. int status = HTTP_STATUS_NOT_FOUND;
  1122. FILE *f = NULL;
  1123. xs *fn = timeline_fn_by_md5(snac, md5);
  1124. if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
  1125. *msg = xs_json_load(f);
  1126. fclose(f);
  1127. if (*msg != NULL)
  1128. status = HTTP_STATUS_OK;
  1129. }
  1130. return status;
  1131. }
  1132. int timeline_del(snac *snac, const char *id)
  1133. /* deletes a message from the timeline */
  1134. {
  1135. /* delete from the user's caches */
  1136. object_user_cache_del(snac, id, "public");
  1137. object_user_cache_del(snac, id, "private");
  1138. unpin(snac, id);
  1139. unbookmark(snac, id);
  1140. /* try to delete the object if it's not used elsewhere */
  1141. return object_del_if_unref(id);
  1142. }
  1143. void timeline_update_indexes(snac *snac, const char *id)
  1144. /* updates the indexes */
  1145. {
  1146. object_user_cache_add(snac, id, "private");
  1147. if (is_msg_mine(snac, id)) {
  1148. xs *msg = NULL;
  1149. if (valid_status(object_get(id, &msg))) {
  1150. const int scope = get_msg_visibility(msg);
  1151. /* if its ours and is public, also store in public */
  1152. if (scope == SCOPE_PUBLIC) {
  1153. if (object_user_cache_add(snac, id, "public") >= 0) {
  1154. /* also add it to the instance public timeline */
  1155. xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
  1156. index_add(ipt, id);
  1157. }
  1158. else
  1159. srv_debug(1, xs_fmt("Not added to public instance index %s, visibility %d", id, scope));
  1160. }
  1161. else
  1162. /* also add it to public, it will be discarded later */
  1163. object_user_cache_add(snac, id, "public");
  1164. /* if `keep_replied_posts` is `true` and a local post is a reply to a remote post, add the parent remote post to the public */
  1165. if (xs_is_true(xs_dict_get(srv_config, "keep_replied_posts"))) {
  1166. const char *in_reply_to = get_in_reply_to(msg);
  1167. if (xs_is_string(in_reply_to) && *in_reply_to &&
  1168. !is_msg_mine(snac, in_reply_to)) {
  1169. if (object_user_cache_add(snac, in_reply_to, "public") >= 0) {
  1170. snac_debug(snac, 1,
  1171. xs_fmt("keep_replied_posts: added parent %s to public", in_reply_to));
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. else {
  1178. /* if `keep_replied_me` is `true` and a remote post is a reply to one of local posts, add it to public */
  1179. if (xs_is_true(xs_dict_get(srv_config, "keep_replied_me"))) {
  1180. xs *msg = NULL;
  1181. if (valid_status(object_get(id, &msg))) {
  1182. const char *in_reply_to = get_in_reply_to(msg);
  1183. if (xs_is_string(in_reply_to) && *in_reply_to &&
  1184. is_msg_mine(snac, in_reply_to)) {
  1185. if (object_user_cache_add(snac, id, "public") >= 0) {
  1186. snac_debug(snac, 1,
  1187. xs_fmt("keep_replied_me: added reply %s to public", id));
  1188. }
  1189. }
  1190. }
  1191. }
  1192. }
  1193. }
  1194. int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
  1195. /* adds a message to the timeline */
  1196. {
  1197. int ret = object_add(id, o_msg);
  1198. timeline_update_indexes(snac, id);
  1199. tag_index(id, o_msg);
  1200. list_distribute(snac, NULL, o_msg);
  1201. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  1202. return ret;
  1203. }
  1204. int timeline_emoji_react(const char *act, const char *id, const xs_dict *msg_o)
  1205. /* adds an emoji reaction to a message */
  1206. {
  1207. xs *msg = xs_dup(msg_o);
  1208. msg = xs_dict_append(msg, "attributedTo", act);
  1209. msg = xs_dict_set(msg, "type", "EmojiReact");
  1210. const char *emote_id = xs_dict_get(msg, "id");
  1211. int ret = object_add(emote_id, msg);
  1212. if (ret == HTTP_STATUS_OK || ret == HTTP_STATUS_CREATED)
  1213. ret = object_emoji_react(id, emote_id);
  1214. return ret;
  1215. }
  1216. int timeline_admire(snac *snac, const char *id,
  1217. const char *admirer, int like, const xs_dict *msg)
  1218. /* updates a timeline entry with a new admiration or emoji reaction */
  1219. {
  1220. int ret;
  1221. const char *content = xs_dict_get_path(msg, "content");
  1222. const char *type = xs_dict_get_path(msg, "type");
  1223. /* if we are admiring this, add to both timelines, and store for later */
  1224. if (!like && strcmp(admirer, snac->actor) == 0) {
  1225. object_user_cache_add(snac, id, "public");
  1226. object_user_cache_add(snac, id, "private");
  1227. }
  1228. if (strcmp(admirer, snac->actor) == 0)
  1229. object_user_cache_add(snac, id, "admire");
  1230. /* use utf <3 as a like, as it is ugly */
  1231. if (type && xs_match(type, "Like|EmojiReact|Emoji") &&
  1232. content && strcmp(content, "❤") != 0) {
  1233. ret = timeline_emoji_react(snac->actor, id, msg);
  1234. snac_debug(snac, 1, xs_fmt("timeline_emoji_react %s", id));
  1235. }
  1236. else {
  1237. ret = object_admire(id, admirer, like);
  1238. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  1239. like ? "Like" : "Announce", id, admirer));
  1240. }
  1241. return ret;
  1242. }
  1243. xs_list *timeline_top_level(snac *snac, const xs_list *list)
  1244. /* returns the top level md5 entries from this index */
  1245. {
  1246. xs_set seen;
  1247. const xs_str *v;
  1248. xs_set_init(&seen);
  1249. int c = 0;
  1250. while (xs_list_next(list, &v, &c)) {
  1251. char line[MD5_HEX_SIZE] = "";
  1252. strncpy(line, v, sizeof(line));
  1253. for (;;) {
  1254. char line2[MD5_HEX_SIZE];
  1255. /* if it doesn't have a parent, use this */
  1256. if (!object_parent(line, line2))
  1257. break;
  1258. /* well, there is a parent... but is it here? */
  1259. if (!timeline_here_by_md5(snac, line2))
  1260. break;
  1261. /* it's here! try again with its own parent */
  1262. strncpy(line, line2, sizeof(line));
  1263. }
  1264. xs_set_add(&seen, line);
  1265. }
  1266. return xs_set_result(&seen);
  1267. }
  1268. xs_str *user_index_fn(snac *user, const char *idx_name)
  1269. /* returns the filename of a user index */
  1270. {
  1271. return xs_fmt("%s/%s.idx", user->basedir, idx_name);
  1272. }
  1273. xs_list *timeline_simple_list(snac *user, const char *idx_name, int skip, int show, int *more)
  1274. /* returns a timeline (with all entries) */
  1275. {
  1276. xs *idx = user_index_fn(user, idx_name);
  1277. /* if a more flag is sent, request one more */
  1278. xs_list *lst = index_list_desc(idx, skip, show + (more != NULL ? 1 : 0));
  1279. if (more != NULL) {
  1280. if (xs_list_len(lst) > show) {
  1281. *more = 1;
  1282. lst = xs_list_del(lst, -1);
  1283. }
  1284. else
  1285. *more = 0;
  1286. }
  1287. return lst;
  1288. }
  1289. xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show, int *more)
  1290. /* returns a timeline (only top level entries) */
  1291. {
  1292. int c_max;
  1293. /* maximum number of items in the timeline */
  1294. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  1295. /* never more timeline entries than the configured maximum */
  1296. if (show > c_max)
  1297. show = c_max;
  1298. xs *list = timeline_simple_list(snac, idx_name, skip, show, more);
  1299. return timeline_top_level(snac, list);
  1300. }
  1301. void timeline_add_mark(snac *user)
  1302. /* adds an "already seen" mark to the private timeline */
  1303. {
  1304. xs *fn = xs_fmt("%s/private.idx", user->basedir);
  1305. char last_entry[MD5_HEX_SIZE] = "";
  1306. FILE *f;
  1307. /* get the last entry in the index */
  1308. if ((f = fopen(fn, "r")) != NULL) {
  1309. index_desc_first(f, last_entry, 0);
  1310. fclose(f);
  1311. }
  1312. /* is the last entry *not* a mark? */
  1313. if (strcmp(last_entry, MD5_ALREADY_SEEN_MARK) != 0) {
  1314. /* add it */
  1315. index_add_md5(fn, MD5_ALREADY_SEEN_MARK);
  1316. }
  1317. }
  1318. xs_str *instance_index_fn(void)
  1319. {
  1320. return xs_fmt("%s/public.idx", srv_basedir);
  1321. }
  1322. xs_list *timeline_instance_list(int skip, int show)
  1323. /* returns the timeline for the full instance */
  1324. {
  1325. xs *idx = instance_index_fn();
  1326. xs *lst = index_list_desc(idx, skip, show);
  1327. /* make the list unique */
  1328. xs_set rep;
  1329. xs_set_init(&rep);
  1330. const char *md5;
  1331. xs_list_foreach(lst, md5)
  1332. xs_set_add(&rep, md5);
  1333. return xs_set_result(&rep);
  1334. }
  1335. /** following **/
  1336. /* this needs special treatment and cannot use the object db as is,
  1337. with a link to a cached author, because we need the Follow object
  1338. in case we need to unfollow (Undo + original Follow) */
  1339. xs_str *_following_fn(snac *snac, const char *actor)
  1340. {
  1341. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1342. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  1343. }
  1344. int following_add(snac *snac, const char *actor, const xs_dict *msg)
  1345. /* adds to the following list */
  1346. {
  1347. int ret = HTTP_STATUS_CREATED;
  1348. xs *fn = _following_fn(snac, actor);
  1349. FILE *f;
  1350. xs *p_object = NULL;
  1351. if (valid_status(following_get(snac, actor, &p_object))) {
  1352. /* object already exists; if it's of type Accept,
  1353. the actor is already being followed and confirmed,
  1354. so do nothing */
  1355. const char *type = xs_dict_get(p_object, "type");
  1356. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  1357. snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
  1358. return HTTP_STATUS_OK;
  1359. }
  1360. }
  1361. if ((f = fopen(fn, "w")) != NULL) {
  1362. xs_json_dump(msg, 4, f);
  1363. fclose(f);
  1364. /* get the filename of the actor object */
  1365. xs *actor_fn = _object_fn(actor);
  1366. /* increase its reference count */
  1367. fn = xs_replace_i(fn, ".json", "_a.json");
  1368. link(actor_fn, fn);
  1369. }
  1370. else
  1371. ret = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  1372. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  1373. return ret;
  1374. }
  1375. int following_del(snac *snac, const char *actor)
  1376. /* we're not following this actor any longer */
  1377. {
  1378. xs *fn = _following_fn(snac, actor);
  1379. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  1380. unlink(fn);
  1381. /* also delete the reference to the author */
  1382. fn = xs_replace_i(fn, ".json", "_a.json");
  1383. unlink(fn);
  1384. return HTTP_STATUS_OK;
  1385. }
  1386. int following_check(snac *snac, const char *actor)
  1387. /* checks if we are following this actor */
  1388. {
  1389. xs *fn = _following_fn(snac, actor);
  1390. return !!(mtime(fn) != 0.0);
  1391. }
  1392. int following_get(snac *snac, const char *actor, xs_dict **data)
  1393. /* returns the 'Follow' object */
  1394. {
  1395. xs *fn = _following_fn(snac, actor);
  1396. FILE *f;
  1397. int status = HTTP_STATUS_OK;
  1398. if ((f = fopen(fn, "r")) != NULL) {
  1399. *data = xs_json_load(f);
  1400. fclose(f);
  1401. }
  1402. else
  1403. status = HTTP_STATUS_NOT_FOUND;
  1404. return status;
  1405. }
  1406. int following_list_len(snac *snac)
  1407. /* returns number of people being followed */
  1408. {
  1409. xs *spec = xs_fmt("%s/following/" "*_a.json", snac->basedir);
  1410. xs *glist = xs_glob(spec, 0, 0);
  1411. return xs_list_len(glist);
  1412. }
  1413. xs_list *following_list(snac *snac)
  1414. /* returns the list of people being followed */
  1415. {
  1416. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  1417. xs *glist = xs_glob(spec, 0, 0);
  1418. xs_list *p;
  1419. const xs_str *v;
  1420. xs_list *list = xs_list_new();
  1421. /* iterate the list of files */
  1422. p = glist;
  1423. while (xs_list_iter(&p, &v)) {
  1424. FILE *f;
  1425. /* load the follower data */
  1426. if ((f = fopen(v, "r")) != NULL) {
  1427. xs *o = xs_json_load(f);
  1428. fclose(f);
  1429. if (o != NULL) {
  1430. const char *type = xs_dict_get(o, "type");
  1431. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  1432. const char *actor = xs_dict_get(o, "actor");
  1433. if (!xs_is_null(actor)) {
  1434. list = xs_list_append(list, actor);
  1435. /* check if there is a link to the actor object */
  1436. xs *v2 = xs_replace(v, ".json", "_a.json");
  1437. if (mtime(v2) == 0.0) {
  1438. /* no; add a link to it */
  1439. xs *actor_fn = _object_fn(actor);
  1440. link(actor_fn, v2);
  1441. }
  1442. }
  1443. }
  1444. }
  1445. }
  1446. }
  1447. return list;
  1448. }
  1449. xs_str *_muted_fn(snac *snac, const char *actor)
  1450. {
  1451. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1452. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  1453. }
  1454. void mute(snac *snac, const char *actor)
  1455. /* mutes a moron */
  1456. {
  1457. xs *fn = _muted_fn(snac, actor);
  1458. FILE *f;
  1459. if ((f = fopen(fn, "w")) != NULL) {
  1460. fprintf(f, "%s\n", actor);
  1461. fclose(f);
  1462. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  1463. }
  1464. }
  1465. void unmute(snac *snac, const char *actor)
  1466. /* actor is no longer a moron */
  1467. {
  1468. xs *fn = _muted_fn(snac, actor);
  1469. unlink(fn);
  1470. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  1471. }
  1472. int is_muted(snac *snac, const char *actor)
  1473. /* check if someone is muted */
  1474. {
  1475. xs *fn = _muted_fn(snac, actor);
  1476. return !!(mtime(fn) != 0.0);
  1477. }
  1478. xs_list *muted_list(snac *user)
  1479. /* returns the list (actor URLs) of the muted morons */
  1480. {
  1481. xs_list *l = xs_list_new();
  1482. xs *spec = xs_fmt("%s/muted/" "*", user->basedir);
  1483. xs *files = xs_glob(spec, 0, 0);
  1484. const char *fn;
  1485. xs_list_foreach(files, fn) {
  1486. FILE *f;
  1487. if ((f = fopen(fn, "r")) != NULL) {
  1488. xs *actor = xs_strip_i(xs_readline(f));
  1489. fclose(f);
  1490. l = xs_list_append(l, actor);
  1491. }
  1492. }
  1493. return l;
  1494. }
  1495. /** emojis react **/
  1496. xs_str *emoji_reacted(snac *user, const char *id)
  1497. /* returns the emoji an user reacted to a message */
  1498. {
  1499. xs *emojis = object_get_emoji_reacts(id);
  1500. int c = 0;
  1501. const char *v;
  1502. while (xs_list_next(emojis, &v, &c)) {
  1503. xs *msg = NULL;
  1504. if (object_get_by_md5(v, &msg)) {
  1505. const xs_val *act = xs_dict_get(msg, "actor");
  1506. if (act && strcmp(act, user->actor) == 0)
  1507. return xs_dup(xs_dict_get(msg, "content"));
  1508. }
  1509. }
  1510. return NULL;
  1511. }
  1512. /** bookmarking **/
  1513. int is_bookmarked(snac *user, const char *id)
  1514. /* returns true if this note is bookmarked */
  1515. {
  1516. return object_user_cache_in(user, id, "bookmark");
  1517. }
  1518. int bookmark(snac *user, const char *id)
  1519. /* bookmarks a post */
  1520. {
  1521. if (is_bookmarked(user, id))
  1522. return -3;
  1523. return object_user_cache_add(user, id, "bookmark");
  1524. }
  1525. int unbookmark(snac *user, const char *id)
  1526. /* unbookmarks a post */
  1527. {
  1528. return object_user_cache_del(user, id, "bookmark");
  1529. }
  1530. xs_list *bookmark_list(snac *user)
  1531. /* return the lists of bookmarked posts */
  1532. {
  1533. return object_user_cache_list(user, "bookmark", XS_ALL, 1);
  1534. }
  1535. xs_str *bookmark_index_fn(snac *user)
  1536. {
  1537. return object_user_cache_index_fn(user, "bookmark");
  1538. }
  1539. /** pinning **/
  1540. int is_pinned(snac *user, const char *id)
  1541. /* returns true if this note is pinned */
  1542. {
  1543. return object_user_cache_in(user, id, "pinned");
  1544. }
  1545. int is_pinned_by_md5(snac *user, const char *md5)
  1546. {
  1547. return object_user_cache_in_by_md5(user, md5, "pinned");
  1548. }
  1549. int pin(snac *user, const char *id)
  1550. /* pins a message */
  1551. {
  1552. int ret = -2;
  1553. if (is_msg_mine(user, id)) {
  1554. if (is_pinned(user, id))
  1555. ret = -3;
  1556. else
  1557. ret = object_user_cache_add(user, id, "pinned");
  1558. }
  1559. return ret;
  1560. }
  1561. int unpin(snac *user, const char *id)
  1562. /* unpin a message */
  1563. {
  1564. return object_user_cache_del(user, id, "pinned");
  1565. }
  1566. xs_list *pinned_list(snac *user)
  1567. /* return the lists of pinned posts */
  1568. {
  1569. return object_user_cache_list(user, "pinned", XS_ALL, 1);
  1570. }
  1571. /** drafts **/
  1572. int is_draft(snac *user, const char *id)
  1573. /* returns true if this note is a draft */
  1574. {
  1575. return object_user_cache_in(user, id, "draft");
  1576. }
  1577. void draft_del(snac *user, const char *id)
  1578. /* delete a message from the draft cache */
  1579. {
  1580. object_user_cache_del(user, id, "draft");
  1581. }
  1582. void draft_add(snac *user, const char *id, const xs_dict *msg)
  1583. /* store the message as a draft */
  1584. {
  1585. /* delete from the index, in case it was already there */
  1586. draft_del(user, id);
  1587. /* overwrite object */
  1588. object_add_ow(id, msg);
  1589. /* [re]add to the index */
  1590. object_user_cache_add(user, id, "draft");
  1591. }
  1592. xs_list *draft_list(snac *user)
  1593. /* return the lists of drafts */
  1594. {
  1595. return object_user_cache_list(user, "draft", XS_ALL, 1);
  1596. }
  1597. /** scheduled posts **/
  1598. int is_scheduled(snac *user, const char *id)
  1599. /* returns true if this note is scheduled for future sending */
  1600. {
  1601. return object_user_cache_in(user, id, "sched");
  1602. }
  1603. void schedule_del(snac *user, const char *id)
  1604. /* deletes an scheduled post */
  1605. {
  1606. object_user_cache_del(user, id, "sched");
  1607. }
  1608. void schedule_add(snac *user, const char *id, const xs_dict *msg)
  1609. /* schedules this post for later */
  1610. {
  1611. /* delete from the index, in case it was already there */
  1612. schedule_del(user, id);
  1613. /* overwrite object */
  1614. object_add_ow(id, msg);
  1615. /* [re]add to the index */
  1616. object_user_cache_add(user, id, "sched");
  1617. }
  1618. xs_list *scheduled_list(snac *user)
  1619. /* return the list of scheduled posts */
  1620. {
  1621. return object_user_cache_list(user, "sched", XS_ALL, 1);
  1622. }
  1623. void scheduled_process(snac *user)
  1624. /* processes the scheduled list, sending those ready to be sent */
  1625. {
  1626. xs *posts = scheduled_list(user);
  1627. const char *md5;
  1628. xs *right_now = xs_str_utctime(0, ISO_DATE_SPEC);
  1629. xs_list_foreach(posts, md5) {
  1630. xs *msg = NULL;
  1631. if (valid_status(object_get_by_md5(md5, &msg))) {
  1632. if (strcmp(xs_dict_get(msg, "published"), right_now) < 0) {
  1633. /* due date! */
  1634. const char *id = xs_dict_get(msg, "id");
  1635. timeline_add(user, id, msg);
  1636. xs *c_msg = msg_create(user, msg);
  1637. enqueue_message(user, c_msg);
  1638. schedule_del(user, id);
  1639. }
  1640. }
  1641. }
  1642. }
  1643. /** hiding **/
  1644. xs_str *_hidden_fn(snac *snac, const char *id)
  1645. {
  1646. xs *md5 = xs_md5_hex(id, strlen(id));
  1647. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  1648. }
  1649. void hide(snac *snac, const char *id)
  1650. /* hides an object and its children (if it's a post) */
  1651. {
  1652. xs *fn = _hidden_fn(snac, id);
  1653. FILE *f;
  1654. if ((f = fopen(fn, "w")) != NULL) {
  1655. fprintf(f, "%s\n", id);
  1656. fclose(f);
  1657. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  1658. /* hide all the children */
  1659. xs *chld = object_children(id);
  1660. char *p;
  1661. const char *v;
  1662. p = chld;
  1663. while (xs_list_iter(&p, &v)) {
  1664. xs *co = NULL;
  1665. /* resolve to get the id */
  1666. if (valid_status(object_get_by_md5(v, &co))) {
  1667. const char *id = xs_dict_get(co, "id");
  1668. if (id != NULL)
  1669. hide(snac, id);
  1670. }
  1671. }
  1672. }
  1673. }
  1674. int is_hidden(snac *snac, const char *id)
  1675. /* check is id is hidden */
  1676. {
  1677. xs *fn = _hidden_fn(snac, id);
  1678. return !!(mtime(fn) != 0.0);
  1679. }
  1680. int unhide(snac *user, const char *id)
  1681. /* unhides an object */
  1682. {
  1683. xs *fn = _hidden_fn(user, id);
  1684. return unlink(fn);
  1685. }
  1686. int actor_add(const char *actor, const xs_dict *msg)
  1687. /* adds an actor */
  1688. {
  1689. return object_add_ow(actor, msg);
  1690. }
  1691. int actor_get(const char *actor, xs_dict **data)
  1692. /* returns an already downloaded actor */
  1693. {
  1694. int status = HTTP_STATUS_OK;
  1695. xs_dict *d = NULL;
  1696. if (xs_startswith(actor, srv_baseurl)) {
  1697. /* it's a (possible) local user */
  1698. xs *l = xs_split(actor, "/");
  1699. const char *uid = xs_list_get(l, -1);
  1700. snac user;
  1701. if (!xs_is_null(uid) && user_open(&user, uid)) {
  1702. if (data)
  1703. *data = msg_actor(&user);
  1704. user_free(&user);
  1705. return HTTP_STATUS_OK;
  1706. }
  1707. else
  1708. return HTTP_STATUS_NOT_FOUND;
  1709. }
  1710. /* read the object */
  1711. if (!valid_status(status = object_get(actor, &d))) {
  1712. d = xs_free(d);
  1713. return status;
  1714. }
  1715. /* if the object is corrupted, discard it */
  1716. if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
  1717. srv_debug(1, xs_fmt("corrupted actor object %s", actor));
  1718. d = xs_free(d);
  1719. return HTTP_STATUS_NOT_FOUND;
  1720. }
  1721. if (data)
  1722. *data = d;
  1723. else
  1724. d = xs_free(d);
  1725. xs *fn = _object_fn(actor);
  1726. double max_time;
  1727. /* maximum time for the actor data to be considered stale */
  1728. max_time = 3600.0 * 36.0;
  1729. if (mtime(fn) + max_time < (double) time(NULL)) {
  1730. /* actor data exists but also stinks */
  1731. status = HTTP_STATUS_RESET_CONTENT; /* "110: Response Is Stale" */
  1732. }
  1733. return status;
  1734. }
  1735. int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
  1736. /* gets an actor and requests a refresh if it's stale */
  1737. {
  1738. int status = actor_get(actor, data);
  1739. if (status == HTTP_STATUS_RESET_CONTENT && user && !xs_startswith(actor, srv_baseurl))
  1740. enqueue_actor_refresh(user, actor, 0);
  1741. return status;
  1742. }
  1743. /** user limiting (announce blocks) **/
  1744. int limited(snac *user, const char *id, snac_op op)
  1745. /* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
  1746. {
  1747. int ret = 0;
  1748. xs *dir = xs_fmt("%s/limited", user->basedir);
  1749. xs *md5 = xs_md5_hex(id, strlen(id));
  1750. xs *fn = xs_fmt("%s/%s", dir, md5);
  1751. switch (op) {
  1752. case OP_CHECK: /** check **/
  1753. ret = !!(mtime(fn) > 0.0);
  1754. break;
  1755. case OP_ADD: /** limit **/
  1756. mkdirx(dir);
  1757. if (mtime(fn) > 0.0)
  1758. ret = -1;
  1759. else {
  1760. FILE *f;
  1761. if ((f = fopen(fn, "w")) != NULL) {
  1762. fprintf(f, "%s\n", id);
  1763. fclose(f);
  1764. }
  1765. else
  1766. ret = -2;
  1767. }
  1768. break;
  1769. case OP_DEL: /** unlimit **/
  1770. if (mtime(fn) > 0.0)
  1771. ret = unlink(fn);
  1772. else
  1773. ret = -1;
  1774. break;
  1775. default:
  1776. break;
  1777. }
  1778. return ret;
  1779. }
  1780. /** cool users **/
  1781. int cool(snac *user, const char *id, snac_op op)
  1782. /* cool users (those users whose posts are notified) */
  1783. {
  1784. int ret = 0;
  1785. xs *dir = xs_fmt("%s/cool", user->basedir);
  1786. xs *md5 = xs_md5_hex(id, strlen(id));
  1787. xs *fn = xs_fmt("%s/%s", dir, md5);
  1788. switch (op) {
  1789. case OP_CHECK: /** check **/
  1790. ret = !!(mtime(fn) > 0.0);
  1791. break;
  1792. case OP_ADD: /** limit **/
  1793. mkdirx(dir);
  1794. if (mtime(fn) > 0.0)
  1795. ret = -1;
  1796. else {
  1797. FILE *f;
  1798. if ((f = fopen(fn, "w")) != NULL) {
  1799. fprintf(f, "%s\n", id);
  1800. fclose(f);
  1801. }
  1802. else
  1803. ret = -2;
  1804. }
  1805. break;
  1806. case OP_DEL: /** unlimit **/
  1807. if (mtime(fn) > 0.0)
  1808. ret = unlink(fn);
  1809. else
  1810. ret = -1;
  1811. break;
  1812. default:
  1813. break;
  1814. }
  1815. return ret;
  1816. }
  1817. /** tag indexing **/
  1818. void tag_index(const char *id, const xs_dict *obj)
  1819. /* update the tag indexes for this object */
  1820. {
  1821. const xs_list *tags = xs_dict_get(obj, "tag");
  1822. xs *md5_id = xs_md5_hex(id, strlen(id));
  1823. if (get_msg_visibility(obj) != SCOPE_PUBLIC)
  1824. return;
  1825. if (xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
  1826. xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
  1827. mkdirx(g_tag_dir);
  1828. const xs_dict *v;
  1829. int ct = 0;
  1830. while (xs_list_next(tags, &v, &ct)) {
  1831. const char *type = xs_dict_get(v, "type");
  1832. const char *name = xs_dict_get(v, "name");
  1833. if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
  1834. while (*name == '#' || *name == '@')
  1835. name++;
  1836. if (*name == '\0')
  1837. continue;
  1838. xs* name_cased = xs_utf8_to_lower((xs_str *)name);
  1839. xs *md5_tag = xs_md5_hex(name_cased, strlen(name_cased));
  1840. xs *tag_dir = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
  1841. mkdirx(tag_dir);
  1842. xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
  1843. if (!index_in_md5(g_tag_idx, md5_id))
  1844. index_add_md5(g_tag_idx, md5_id);
  1845. FILE *f;
  1846. xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
  1847. if ((f = fopen(g_tag_name, "w")) != NULL) {
  1848. fprintf(f, "%s\n", name_cased);
  1849. fclose(f);
  1850. }
  1851. srv_debug(1, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
  1852. }
  1853. }
  1854. }
  1855. }
  1856. xs_str *tag_fn(const char *tag)
  1857. {
  1858. if (*tag == '#')
  1859. tag++;
  1860. xs *lw_tag = xs_utf8_to_lower(tag);
  1861. xs *md5 = xs_md5_hex(lw_tag, strlen(lw_tag));
  1862. return xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
  1863. }
  1864. xs_list *tag_search(const char *tag, int skip, int show)
  1865. /* returns the list of posts tagged with tag */
  1866. {
  1867. xs *idx = tag_fn(tag);
  1868. return index_list_desc(idx, skip, show);
  1869. }
  1870. /** lists **/
  1871. xs_val *list_maint(snac *user, const char *list, snac_op op)
  1872. /* list maintenance */
  1873. {
  1874. xs_val *l = NULL;
  1875. switch (op) {
  1876. case OP_LIST: /** list of lists **/
  1877. {
  1878. FILE *f;
  1879. xs *spec = xs_fmt("%s/list/" "*.id", user->basedir);
  1880. xs *ls = xs_glob(spec, 0, 0);
  1881. int c = 0;
  1882. const char *v;
  1883. l = xs_list_new();
  1884. while (xs_list_next(ls, &v, &c)) {
  1885. if ((f = fopen(v, "r")) != NULL) {
  1886. xs *title = xs_readline(f);
  1887. fclose(f);
  1888. title = xs_strip_i(title);
  1889. xs *v2 = xs_replace(v, ".id", "");
  1890. xs *l2 = xs_split(v2, "/");
  1891. /* return [ list_id, list_title ] */
  1892. xs *tmp_list = xs_list_append(xs_list_new(), xs_list_get(l2, -1), title);
  1893. l = xs_list_append(l, tmp_list);
  1894. }
  1895. }
  1896. }
  1897. break;
  1898. case OP_ADD: /** create new list (list is the name) **/
  1899. {
  1900. xs *lol = list_maint(user, NULL, OP_LIST);
  1901. int c = 0;
  1902. const xs_list *v;
  1903. int add = 1;
  1904. /* check if this list name already exists */
  1905. while (xs_list_next(lol, &v, &c)) {
  1906. if (strcmp(xs_list_get(v, 1), list) == 0) {
  1907. add = 0;
  1908. l = xs_dup(xs_list_get(v, 0));
  1909. break;
  1910. }
  1911. }
  1912. if (add) {
  1913. FILE *f;
  1914. xs *dir = xs_fmt("%s/list/", user->basedir);
  1915. l = xs_fmt("%010x", time(NULL));
  1916. mkdirx(dir);
  1917. xs *fn = xs_fmt("%s%s.id", dir, l);
  1918. if ((f = fopen(fn, "w")) != NULL) {
  1919. fprintf(f, "%s\n", list);
  1920. fclose(f);
  1921. }
  1922. }
  1923. }
  1924. break;
  1925. case OP_DEL: /** delete list (list is the id) **/
  1926. {
  1927. if (xs_is_hex(list)) {
  1928. xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
  1929. unlink(fn);
  1930. fn = xs_replace_i(fn, ".id", ".lst");
  1931. unlink(fn);
  1932. fn = xs_replace_i(fn, ".lst", ".idx");
  1933. unlink(fn);
  1934. fn = xs_str_cat(fn, ".bak");
  1935. unlink(fn);
  1936. }
  1937. }
  1938. break;
  1939. case OP_ID: /** get list name **/
  1940. if (xs_is_hex(list)) {
  1941. FILE *f;
  1942. xs *fn = xs_fmt("%s/list/%s.id", user->basedir, list);
  1943. if ((f = fopen(fn, "r")) != NULL) {
  1944. l = xs_strip_i(xs_readline(f));
  1945. fclose(f);
  1946. }
  1947. }
  1948. break;
  1949. case OP_FIND: /** find list id by name **/
  1950. if (xs_is_string(list)) {
  1951. xs *lol = list_maint(user, NULL, OP_LIST);
  1952. const xs_list *li;
  1953. xs_list_foreach(lol, li) {
  1954. if (strcmp(list, xs_list_get(li, 1)) == 0) {
  1955. l = xs_dup(xs_list_get(li, 0));
  1956. break;
  1957. }
  1958. }
  1959. }
  1960. default:
  1961. break;
  1962. }
  1963. return l;
  1964. }
  1965. xs_str *list_timeline_fn(snac *user, const char *list)
  1966. {
  1967. return xs_fmt("%s/list/%s.idx", user->basedir, list);
  1968. }
  1969. xs_list *list_timeline(snac *user, const char *list, int skip, int show)
  1970. /* returns the timeline of a list */
  1971. {
  1972. xs_list *l = NULL;
  1973. if (!xs_is_hex(list))
  1974. return NULL;
  1975. xs *fn = list_timeline_fn(user, list);
  1976. if (mtime(fn) > 0.0)
  1977. l = index_list_desc(fn, skip, show);
  1978. else
  1979. l = xs_list_new();
  1980. return l;
  1981. }
  1982. xs_val *list_members(snac *user, const char *list, const char *actor_md5, snac_op op)
  1983. /* list member management */
  1984. {
  1985. xs_val *l = NULL;
  1986. if (!xs_is_hex(list))
  1987. return NULL;
  1988. if (actor_md5 != NULL && !xs_is_hex(actor_md5))
  1989. return NULL;
  1990. xs *fn = xs_fmt("%s/list/%s.lst", user->basedir, list);
  1991. switch (op) {
  1992. case OP_LIST: /** list members **/
  1993. l = index_list(fn, XS_ALL);
  1994. break;
  1995. case OP_ADD: /** append actor to list **/
  1996. if (xs_is_string(actor_md5) && xs_is_hex(actor_md5)) {
  1997. if (!index_in_md5(fn, actor_md5))
  1998. index_add_md5(fn, actor_md5);
  1999. }
  2000. break;
  2001. case OP_DEL: /** delete actor from list **/
  2002. if (xs_is_string(actor_md5) && xs_is_hex(actor_md5))
  2003. index_del_md5(fn, actor_md5);
  2004. break;
  2005. default:
  2006. srv_log(xs_fmt("ERROR: list_content: bad op %d", op));
  2007. break;
  2008. }
  2009. return l;
  2010. }
  2011. void list_distribute(snac *user, const char *who, const xs_dict *post)
  2012. /* distributes the post to all appropriate lists */
  2013. {
  2014. const char *id = xs_dict_get(post, "id");
  2015. /* if who is not set, use the attributedTo in the message */
  2016. if (xs_is_null(who))
  2017. who = get_atto(post);
  2018. if (xs_type(who) == XSTYPE_STRING && xs_type(id) == XSTYPE_STRING) {
  2019. xs *a_md5 = xs_md5_hex(who, strlen(who));
  2020. xs *i_md5 = xs_md5_hex(id, strlen(id));
  2021. xs *spec = xs_fmt("%s/list/" "*.lst", user->basedir);
  2022. xs *ls = xs_glob(spec, 0, 0);
  2023. int c = 0;
  2024. const char *v;
  2025. while (xs_list_next(ls, &v, &c)) {
  2026. /* is the actor in this list? */
  2027. if (index_in_md5(v, a_md5)) {
  2028. /* it is; add post md5 to its timeline */
  2029. xs *idx = xs_replace(v, ".lst", ".idx");
  2030. index_add_md5(idx, i_md5);
  2031. snac_debug(user, 1, xs_fmt("listed post %s in %s", id, idx));
  2032. }
  2033. }
  2034. }
  2035. }
  2036. /** static data **/
  2037. static int _load_raw_file(const char *fn, xs_val **data, int *size,
  2038. const char *inm, xs_str **etag)
  2039. /* loads a cached file */
  2040. {
  2041. int status = HTTP_STATUS_NOT_FOUND;
  2042. if (fn) {
  2043. double tm = mtime(fn);
  2044. if (tm > 0.0) {
  2045. /* file exists; build the etag */
  2046. xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
  2047. /* if if-none-match is set, check if it's the same */
  2048. if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
  2049. /* client has the newest version */
  2050. status = HTTP_STATUS_NOT_MODIFIED;
  2051. }
  2052. else {
  2053. /* newer or never downloaded; read the full file */
  2054. struct stat sb;
  2055. FILE *f;
  2056. if (lstat(fn, &sb) == 0 && (sb.st_mode&S_IFMT) == S_IFREG) {
  2057. if ((f = fopen(fn, "rb")) != NULL) {
  2058. *size = XS_ALL;
  2059. *data = xs_read(f, size);
  2060. fclose(f);
  2061. status = HTTP_STATUS_OK;
  2062. }
  2063. }
  2064. }
  2065. /* if caller wants the etag, return it */
  2066. if (etag != NULL)
  2067. *etag = xs_dup(e);
  2068. srv_debug(2, xs_fmt("_load_raw_file(): %s %d", fn, status));
  2069. }
  2070. }
  2071. return status;
  2072. }
  2073. xs_str *_static_fn(snac *snac, const char *id)
  2074. /* gets the filename for a static file */
  2075. {
  2076. if (strstr(id, ".."))
  2077. return NULL;
  2078. else
  2079. return xs_fmt("%s/static/%s", snac->basedir, id);
  2080. }
  2081. int static_get(snac *snac, const char *id, xs_val **data, int *size,
  2082. const char *inm, xs_str **etag)
  2083. /* returns static content */
  2084. {
  2085. xs *fn = _static_fn(snac, id);
  2086. return _load_raw_file(fn, data, size, inm, etag);
  2087. }
  2088. void static_put(snac *snac, const char *id, const char *data, int size)
  2089. /* writes status content */
  2090. {
  2091. xs *fn = _static_fn(snac, id);
  2092. FILE *f;
  2093. if (fn && (f = fopen(fn, "wb")) != NULL) {
  2094. fwrite(data, size, 1, f);
  2095. fclose(f);
  2096. strip_media(fn);
  2097. }
  2098. }
  2099. void static_put_meta(snac *snac, const char *id, const char *str)
  2100. /* puts metadata (i.e. a media description string) to id */
  2101. {
  2102. xs *fn = _static_fn(snac, id);
  2103. if (fn) {
  2104. fn = xs_str_cat(fn, ".txt");
  2105. FILE *f;
  2106. if ((f = fopen(fn, "w")) != NULL) {
  2107. fprintf(f, "%s\n", str);
  2108. fclose(f);
  2109. }
  2110. }
  2111. }
  2112. xs_str *static_get_meta(snac *snac, const char *id)
  2113. /* gets metadata from a media */
  2114. {
  2115. xs *fn = _static_fn(snac, id);
  2116. xs_str *r = NULL;
  2117. if (fn) {
  2118. fn = xs_str_cat(fn, ".txt");
  2119. FILE *f;
  2120. if ((f = fopen(fn, "r")) != NULL) {
  2121. r = xs_strip_i(xs_readline(f));
  2122. fclose(f);
  2123. }
  2124. }
  2125. else
  2126. r = xs_str_new("");
  2127. return r;
  2128. }
  2129. /** history **/
  2130. xs_str *_history_fn(snac *snac, const char *id)
  2131. /* gets the filename for the history */
  2132. {
  2133. if (strchr(id, '/'))
  2134. return NULL;
  2135. else
  2136. return xs_fmt("%s/history/%s", snac->basedir, id);
  2137. }
  2138. double history_mtime(snac *snac, const char *id)
  2139. {
  2140. double t = 0.0;
  2141. xs *fn = _history_fn(snac, id);
  2142. if (fn != NULL)
  2143. t = mtime(fn);
  2144. return t;
  2145. }
  2146. void history_add(snac *snac, const char *id, const char *content, int size,
  2147. xs_str **etag)
  2148. /* adds something to the history */
  2149. {
  2150. xs *fn = _history_fn(snac, id);
  2151. FILE *f;
  2152. if (fn && (f = fopen(fn, "w")) != NULL) {
  2153. fwrite(content, size, 1, f);
  2154. fclose(f);
  2155. if (etag) {
  2156. double tm = mtime(fn);
  2157. *etag = xs_fmt("W/\"snac-%.0lf\"", tm);
  2158. }
  2159. }
  2160. }
  2161. int history_get(snac *snac, const char *id, xs_str **content, int *size,
  2162. const char *inm, xs_str **etag)
  2163. {
  2164. xs *fn = _history_fn(snac, id);
  2165. return _load_raw_file(fn, content, size, inm, etag);
  2166. }
  2167. int history_del(snac *snac, const char *id)
  2168. {
  2169. xs *fn = _history_fn(snac, id);
  2170. if (fn)
  2171. return unlink(fn);
  2172. else
  2173. return -1;
  2174. }
  2175. xs_list *history_list(snac *snac)
  2176. {
  2177. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  2178. return xs_glob(spec, 1, 1);
  2179. }
  2180. void lastlog_write(snac *snac, const char *source)
  2181. /* writes the last time the user logged in */
  2182. {
  2183. xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
  2184. FILE *f;
  2185. if ((f = fopen(fn, "w")) != NULL) {
  2186. fprintf(f, "%lf %s\n", ftime(), source);
  2187. fclose(f);
  2188. }
  2189. }
  2190. /** inbox collection **/
  2191. void inbox_add(const char *inbox)
  2192. /* collects a shared inbox */
  2193. {
  2194. /* don't collect ourselves */
  2195. if (xs_startswith(inbox, srv_baseurl))
  2196. return;
  2197. xs *md5 = xs_md5_hex(inbox, strlen(inbox));
  2198. xs *fn = xs_fmt("%s/inbox/%s", srv_basedir, md5);
  2199. FILE *f;
  2200. if ((f = fopen(fn, "w")) != NULL) {
  2201. fprintf(f, "%s\n", inbox);
  2202. fclose(f);
  2203. }
  2204. }
  2205. void inbox_add_by_actor(const xs_dict *actor)
  2206. /* collects an actor's shared inbox, if it has one */
  2207. {
  2208. const char *v;
  2209. if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
  2210. !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
  2211. /* only collect this inbox if its instance is not blocked */
  2212. if (!is_instance_blocked(v))
  2213. inbox_add(v);
  2214. }
  2215. }
  2216. xs_list *inbox_list(void)
  2217. /* returns the collected inboxes as a list */
  2218. {
  2219. xs_list *ibl = xs_list_new();
  2220. xs *spec = xs_fmt("%s/inbox/" "*", srv_basedir);
  2221. xs *files = xs_glob(spec, 0, 0);
  2222. const xs_val *v;
  2223. xs_list_foreach(files, v) {
  2224. FILE *f;
  2225. if ((f = fopen(v, "r")) != NULL) {
  2226. xs *line = xs_readline(f);
  2227. if (line && *line) {
  2228. line = xs_strip_i(line);
  2229. if (!is_instance_blocked(line))
  2230. ibl = xs_list_append(ibl, line);
  2231. }
  2232. fclose(f);
  2233. }
  2234. }
  2235. return ibl;
  2236. }
  2237. /** instance-wide operations **/
  2238. xs_str *_instance_block_fn(const char *instance)
  2239. {
  2240. xs *s = xs_replace(instance, "http:/" "/", "");
  2241. xs *s1 = xs_replace(s, "https:/" "/", "");
  2242. xs *l = xs_split(s1, "/");
  2243. const char *p = xs_list_get(l, 0);
  2244. xs *md5 = xs_md5_hex(p, strlen(p));
  2245. return xs_fmt("%s/block/%s", srv_basedir, md5);
  2246. }
  2247. int is_instance_blocked(const char *instance)
  2248. {
  2249. xs *fn = _instance_block_fn(instance);
  2250. return !!(mtime(fn) != 0.0);
  2251. }
  2252. int instance_block(const char *instance)
  2253. /* blocks a full instance */
  2254. {
  2255. int ret;
  2256. /* create the subdir */
  2257. xs *dir = xs_fmt("%s/block/", srv_basedir);
  2258. mkdirx(dir);
  2259. if (!is_instance_blocked(instance)) {
  2260. xs *fn = _instance_block_fn(instance);
  2261. FILE *f;
  2262. if ((f = fopen(fn, "w")) != NULL) {
  2263. fprintf(f, "%s\n", instance);
  2264. fclose(f);
  2265. ret = 0;
  2266. }
  2267. else
  2268. ret = -1;
  2269. }
  2270. else
  2271. ret = -2;
  2272. return ret;
  2273. }
  2274. int instance_unblock(const char *instance)
  2275. /* unblocks a full instance */
  2276. {
  2277. int ret;
  2278. if (is_instance_blocked(instance)) {
  2279. xs *fn = _instance_block_fn(instance);
  2280. ret = unlink(fn);
  2281. }
  2282. else
  2283. ret = -2;
  2284. return ret;
  2285. }
  2286. /** operations by content **/
  2287. int content_match(const char *file, const xs_dict *msg)
  2288. /* checks if a message's content matches any of the regexes in file */
  2289. /* file format: one regex per line */
  2290. {
  2291. xs *fn = xs_fmt("%s/%s", srv_basedir, file);
  2292. FILE *f;
  2293. int r = 0;
  2294. const char *v = xs_dict_get(msg, "content");
  2295. if (xs_type(v) == XSTYPE_STRING && *v) {
  2296. if ((f = fopen(fn, "r")) != NULL) {
  2297. srv_debug(1, xs_fmt("content_match: loading regexes from %s", fn));
  2298. /* massage content (strip HTML tags, etc.) */
  2299. xs *c1 = xs_regex_replace(v, "<[^>]+>", " ");
  2300. c1 = xs_regex_replace_i(c1, " {2,}", " ");
  2301. xs *c = xs_utf8_to_lower(c1);
  2302. while (!r && !feof(f)) {
  2303. xs *rx = xs_strip_i(xs_readline(f));
  2304. if (*rx && xs_regex_match(c, rx)) {
  2305. srv_debug(1, xs_fmt("content_match: match for '%s'", rx));
  2306. r = 1;
  2307. }
  2308. }
  2309. fclose(f);
  2310. }
  2311. }
  2312. return r;
  2313. }
  2314. xs_list *content_search(snac *user, const char *regex,
  2315. int priv, int skip, int show, int max_secs, int *timeout)
  2316. /* returns a list of posts which content matches the regex */
  2317. {
  2318. if (regex == NULL || *regex == '\0')
  2319. return xs_list_new();
  2320. xs *i_regex = xs_utf8_to_lower(regex);
  2321. xs_set seen;
  2322. xs_set_init(&seen);
  2323. if (max_secs == 0)
  2324. max_secs = 3;
  2325. time_t t = time(NULL) + max_secs;
  2326. *timeout = 0;
  2327. /* iterate all timelines simultaneously */
  2328. xs_list *tls[3] = {0};
  2329. const char *md5s[3] = {0};
  2330. int c[3] = {0};
  2331. tls[0] = timeline_simple_list(user, "public", 0, XS_ALL, NULL); /* public */
  2332. tls[1] = timeline_instance_list(0, XS_ALL); /* instance */
  2333. tls[2] = priv ? timeline_simple_list(user, "private", 0, XS_ALL, NULL) : xs_list_new(); /* private or none */
  2334. /* first positioning */
  2335. for (int n = 0; n < 3; n++)
  2336. xs_list_next(tls[n], &md5s[n], &c[n]);
  2337. show += skip;
  2338. while (show > 0) {
  2339. /* timeout? */
  2340. if (time(NULL) > t) {
  2341. *timeout = 1;
  2342. break;
  2343. }
  2344. /* find the newest post */
  2345. int newest = -1;
  2346. double mtime = 0.0;
  2347. for (int n = 0; n < 3; n++) {
  2348. if (md5s[n] != NULL) {
  2349. xs *fn = _object_fn_by_md5(md5s[n], "content_search");
  2350. double mt;
  2351. while ((mt = mtime(fn)) == 0 && md5s[n] != NULL) {
  2352. /* object is not here: move to the next one */
  2353. if (xs_list_next(tls[n], &md5s[n], &c[n])) {
  2354. xs_free(fn);
  2355. fn = _object_fn_by_md5(md5s[n], "content_search_2");
  2356. }
  2357. else
  2358. md5s[n] = NULL;
  2359. }
  2360. if (mt > mtime) {
  2361. newest = n;
  2362. mtime = mt;
  2363. }
  2364. }
  2365. }
  2366. if (newest == -1)
  2367. break;
  2368. const char *md5 = md5s[newest];
  2369. /* advance the chosen timeline */
  2370. if (!xs_list_next(tls[newest], &md5s[newest], &c[newest]))
  2371. md5s[newest] = NULL;
  2372. xs *post = NULL;
  2373. if (!valid_status(object_get_by_md5(md5, &post)))
  2374. continue;
  2375. if (!xs_match(xs_dict_get_def(post, "type", "-"), POSTLIKE_OBJECT_TYPE))
  2376. continue;
  2377. const char *id = xs_dict_get(post, "id");
  2378. if (id == NULL || is_hidden(user, id))
  2379. continue;
  2380. /* recalculate the md5 id to be sure it's not repeated
  2381. (it may have been searched by the "url" field instead of "id") */
  2382. xs *new_md5 = xs_md5_hex(id, strlen(id));
  2383. md5 = new_md5;
  2384. /* test for the post URL */
  2385. if (strcmp(id, regex) == 0) {
  2386. if (xs_set_add(&seen, md5) == 1)
  2387. show--;
  2388. continue;
  2389. }
  2390. /* test for the alternate post id */
  2391. const char *url = xs_dict_get(post, "url");
  2392. if (xs_type(url) == XSTYPE_STRING && strcmp(url, regex) == 0) {
  2393. if (xs_set_add(&seen, md5) == 1)
  2394. show--;
  2395. continue;
  2396. }
  2397. xs *c = xs_str_new(NULL);
  2398. const char *content = xs_dict_get(post, "content");
  2399. const char *name = xs_dict_get(post, "name");
  2400. const char *atto = get_atto(post);
  2401. if (!xs_is_null(content))
  2402. c = xs_str_cat(c, content);
  2403. if (!xs_is_null(name))
  2404. c = xs_str_cat(c, " ", name);
  2405. if (!xs_is_null(atto))
  2406. c = xs_str_cat(c, " ", atto);
  2407. /* add alt-texts from attachments */
  2408. const xs_list *atts = xs_dict_get(post, "attachment");
  2409. int tc = 0;
  2410. const xs_dict *att;
  2411. while (xs_list_next(atts, &att, &tc)) {
  2412. const char *name = xs_dict_get(att, "name");
  2413. if (name != NULL)
  2414. c = xs_str_cat(c, " ", name);
  2415. }
  2416. /* strip HTML */
  2417. c = xs_regex_replace_i(c, "<[^>]+>", " ");
  2418. c = xs_regex_replace_i(c, " {2,}", " ");
  2419. /* convert to lowercase */
  2420. xs *lc = xs_utf8_to_lower(c);
  2421. /* apply regex */
  2422. if (xs_regex_match(lc, i_regex)) {
  2423. if (xs_set_add(&seen, md5) == 1)
  2424. show--;
  2425. }
  2426. }
  2427. xs_list *r = xs_set_result(&seen);
  2428. if (skip) {
  2429. /* BAD */
  2430. while (skip--) {
  2431. r = xs_list_del(r, 0);
  2432. }
  2433. }
  2434. xs_free(tls[0]);
  2435. xs_free(tls[1]);
  2436. xs_free(tls[2]);
  2437. return r;
  2438. }
  2439. int actor_failure(const char *actor, snac_op op)
  2440. /* actor failure maintenance */
  2441. {
  2442. int ret = 0;
  2443. xs *md5 = xs_md5_hex(actor, strlen(actor));
  2444. xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
  2445. switch (op) {
  2446. case OP_CHECK: /** check **/
  2447. if (mtime(fn))
  2448. ret = -1;
  2449. break;
  2450. case OP_ADD: /** register a failure **/
  2451. if (mtime(fn) == 0.0) {
  2452. FILE *f;
  2453. /* only create once, as the date will be used */
  2454. if ((f = fopen(fn, "w")) != NULL) {
  2455. fprintf(f, "%s\n", actor);
  2456. fclose(f);
  2457. }
  2458. }
  2459. break;
  2460. case OP_DEL: /** clear a failure **/
  2461. /* called whenever a message comes from this instance */
  2462. unlink(fn);
  2463. break;
  2464. default:
  2465. break;
  2466. }
  2467. return ret;
  2468. }
  2469. int instance_failure(const char *url, snac_op op)
  2470. /* do some checks and accounting on instance failures */
  2471. {
  2472. int ret = 0;
  2473. xs *l = xs_split(url, "/");
  2474. const char *hostname = xs_list_get(l, 2);
  2475. double mt;
  2476. if (!xs_is_string(hostname))
  2477. return 0;
  2478. xs *md5 = xs_md5_hex(hostname, strlen(hostname));
  2479. xs *fn = xs_fmt("%s/failure/%s", srv_basedir, md5);
  2480. switch (op) {
  2481. case OP_CHECK: /** check **/
  2482. if ((mt = mtime(fn)) != 0.0) {
  2483. /* grace time */
  2484. double seconds_failing = xs_number_get(xs_dict_get_def(srv_config, "max_failing_days", "15"))
  2485. * (24 * 60 * 60);
  2486. if ((double)time(NULL) - mt > seconds_failing)
  2487. ret = -1;
  2488. }
  2489. break;
  2490. case OP_ADD: /** register a failure **/
  2491. if (mtime(fn) == 0.0) {
  2492. FILE *f;
  2493. /* only create once, as the date will be used */
  2494. if ((f = fopen(fn, "w")) != NULL) {
  2495. fprintf(f, "%s\n", hostname);
  2496. fclose(f);
  2497. }
  2498. }
  2499. break;
  2500. case OP_DEL: /** clear a failure **/
  2501. /* called whenever a message comes from this instance */
  2502. unlink(fn);
  2503. break;
  2504. default:
  2505. break;
  2506. }
  2507. return ret;
  2508. }
  2509. int grave(const char *objid, snac_op op)
  2510. /* the graveyeard of deleted objects */
  2511. {
  2512. int ret = 0;
  2513. xs *dir = xs_fmt("%s/grave", srv_basedir);
  2514. xs *md5 = xs_md5_hex(objid, strlen(objid));
  2515. xs *fn = xs_fmt("%s/%s", dir, md5);
  2516. FILE *f;
  2517. switch (op) {
  2518. case OP_CHECK: /** check **/
  2519. ret = mtime(fn) > 0.0 ? 1 : 0;
  2520. break;
  2521. case OP_ADD: /** add **/
  2522. mkdirx(dir);
  2523. if ((f = fopen(fn, "w")) != NULL) {
  2524. fprintf(f, "%s\n", objid);
  2525. fclose(f);
  2526. }
  2527. break;
  2528. case OP_DEL: /** del **/
  2529. unlink(fn);
  2530. break;
  2531. default:
  2532. break;
  2533. }
  2534. return ret;
  2535. }
  2536. /** notifications **/
  2537. xs_str *notify_check_time(snac *snac, int reset)
  2538. /* gets or resets the latest notification check time */
  2539. {
  2540. xs_str *t = NULL;
  2541. xs *fn = xs_fmt("%s/notifydate.txt", snac->basedir);
  2542. FILE *f;
  2543. if (reset) {
  2544. if ((f = fopen(fn, "w")) != NULL) {
  2545. t = tid(0);
  2546. fprintf(f, "%s\n", t);
  2547. fclose(f);
  2548. }
  2549. }
  2550. else {
  2551. if ((f = fopen(fn, "r")) != NULL) {
  2552. t = xs_readline(f);
  2553. fclose(f);
  2554. }
  2555. else
  2556. /* never set before */
  2557. t = xs_fmt("%16.6f", 0.0);
  2558. }
  2559. return t;
  2560. }
  2561. xs_dict *markers_get(snac *snac, const xs_list *markers)
  2562. {
  2563. xs *data = NULL;
  2564. xs_dict *returns = xs_dict_new();
  2565. xs *fn = xs_fmt("%s/markers.json", snac->basedir);
  2566. const xs_str *v = NULL;
  2567. FILE *f;
  2568. if ((f = fopen(fn, "r")) != NULL) {
  2569. data = xs_json_load(f);
  2570. fclose(f);
  2571. }
  2572. if (xs_is_null(data))
  2573. data = xs_dict_new();
  2574. xs_list_foreach(markers, v) {
  2575. const xs_dict *mark = xs_dict_get(data, v);
  2576. if (!xs_is_null(mark)) {
  2577. returns = xs_dict_append(returns, v, mark);
  2578. }
  2579. }
  2580. return returns;
  2581. }
  2582. xs_dict *markers_set(snac *snac, const char *home_marker, const char *notify_marker)
  2583. /* gets or sets notification marker */
  2584. {
  2585. xs *data = NULL;
  2586. xs_dict *written = xs_dict_new();
  2587. xs *fn = xs_fmt("%s/markers.json", snac->basedir);
  2588. FILE *f;
  2589. if ((f = fopen(fn, "r")) != NULL) {
  2590. data = xs_json_load(f);
  2591. fclose(f);
  2592. }
  2593. if (xs_is_null(data))
  2594. data = xs_dict_new();
  2595. if (!xs_is_null(home_marker)) {
  2596. xs *home = xs_dict_new();
  2597. xs *s_tid = tid(0);
  2598. home = xs_dict_append(home, "last_read_id", home_marker);
  2599. home = xs_dict_append(home, "version", xs_stock(0));
  2600. home = xs_dict_append(home, "updated_at", s_tid);
  2601. data = xs_dict_set(data, "home", home);
  2602. written = xs_dict_append(written, "home", home);
  2603. }
  2604. if (!xs_is_null(notify_marker)) {
  2605. xs *notify = xs_dict_new();
  2606. xs *s_tid = tid(0);
  2607. notify = xs_dict_append(notify, "last_read_id", notify_marker);
  2608. notify = xs_dict_append(notify, "version", xs_stock(0));
  2609. notify = xs_dict_append(notify, "updated_at", s_tid);
  2610. data = xs_dict_set(data, "notifications", notify);
  2611. written = xs_dict_append(written, "notifications", notify);
  2612. }
  2613. if ((f = fopen(fn, "w")) != NULL) {
  2614. xs_json_dump(data, 4, f);
  2615. fclose(f);
  2616. }
  2617. return written;
  2618. }
  2619. void notify_add(snac *snac, const char *type, const char *utype,
  2620. const char *actor, const char *objid, const xs_dict *msg)
  2621. /* adds a new notification */
  2622. {
  2623. xs *ntid = tid(0);
  2624. xs *fn = xs_fmt("%s/notify/", snac->basedir);
  2625. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2626. FILE *f;
  2627. /* create the directory */
  2628. mkdirx(fn);
  2629. fn = xs_str_cat(fn, ntid);
  2630. fn = xs_str_cat(fn, ".json");
  2631. xs *noti = xs_dict_new();
  2632. noti = xs_dict_append(noti, "id", ntid);
  2633. noti = xs_dict_append(noti, "type", type);
  2634. noti = xs_dict_append(noti, "utype", utype);
  2635. noti = xs_dict_append(noti, "actor", actor);
  2636. noti = xs_dict_append(noti, "date", date);
  2637. noti = xs_dict_append(noti, "msg", msg);
  2638. if (!xs_is_null(objid))
  2639. noti = xs_dict_append(noti, "objid", objid);
  2640. if ((f = fopen(fn, "w")) != NULL) {
  2641. xs_json_dump(noti, 4, f);
  2642. fclose(f);
  2643. }
  2644. /* add it to the index if it already exists */
  2645. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  2646. if (mtime(idx) != 0.0) {
  2647. pthread_mutex_lock(&data_mutex);
  2648. if ((f = fopen(idx, "a")) != NULL) {
  2649. fprintf(f, "%-32s\n", ntid);
  2650. fclose(f);
  2651. }
  2652. pthread_mutex_unlock(&data_mutex);
  2653. }
  2654. if (!xs_is_true(xs_dict_get(srv_config, "disable_notify_webhook")))
  2655. enqueue_notify_webhook(snac, noti, 0);
  2656. }
  2657. xs_dict *notify_get(snac *snac, const char *id)
  2658. /* gets a notification */
  2659. {
  2660. /* base file */
  2661. xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
  2662. /* strip spaces and add extension */
  2663. fn = xs_strip_i(fn);
  2664. fn = xs_str_cat(fn, ".json");
  2665. xs_dict *out = NULL;
  2666. struct stat st;
  2667. if (stat(fn, &st) != -1) {
  2668. if (st.st_size < 100000) {
  2669. FILE *f;
  2670. if ((f = fopen(fn, "r")) != NULL) {
  2671. out = xs_json_load(f);
  2672. fclose(f);
  2673. }
  2674. }
  2675. }
  2676. return out;
  2677. }
  2678. xs_list *notify_list(snac *snac, int skip, int show)
  2679. /* returns a list of notification ids */
  2680. {
  2681. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  2682. if (mtime(idx) == 0.0) {
  2683. /* create the index from scratch */
  2684. FILE *f;
  2685. pthread_mutex_lock(&data_mutex);
  2686. if ((f = fopen(idx, "w")) != NULL) {
  2687. xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
  2688. xs *lst = xs_glob(spec, 1, 0);
  2689. xs_list *p = lst;
  2690. const char *v;
  2691. while (xs_list_iter(&p, &v)) {
  2692. char *p = strrchr(v, '.');
  2693. if (p) {
  2694. *p = '\0';
  2695. fprintf(f, "%-32s\n", v);
  2696. }
  2697. }
  2698. fclose(f);
  2699. }
  2700. pthread_mutex_unlock(&data_mutex);
  2701. }
  2702. return index_list_desc(idx, skip, show);
  2703. }
  2704. xs_list *notify_filter_list(snac *snac, xs_list *notifs, int skip, int show)
  2705. /* apply user-defined notification filter to IDs */
  2706. {
  2707. const xs_dict *n_filter = xs_dict_get(snac->config, "notify_filter");
  2708. if (!n_filter) {
  2709. return xs_dup(notifs);
  2710. }
  2711. const xs_val *n_def = xs_stock( XSTYPE_TRUE );
  2712. int n_likes_on = xs_is_true(xs_dict_get_def(n_filter, "likes", n_def));
  2713. int n_reacts_on = xs_is_true(xs_dict_get_def(n_filter, "reacts", n_def));
  2714. int n_ments_on = xs_is_true(xs_dict_get_def(n_filter, "mentions", n_def));
  2715. int n_ann_on = xs_is_true(xs_dict_get_def(n_filter, "announces", n_def));
  2716. int n_fol_on = xs_is_true(xs_dict_get_def(n_filter, "follows", n_def));
  2717. int n_unfol_on = xs_is_true(xs_dict_get_def(n_filter, "unfollows", n_def));
  2718. int n_folreq_on = xs_is_true(xs_dict_get_def(n_filter, "folreqs", n_def));
  2719. int n_blocks_on = xs_is_true(xs_dict_get_def(n_filter, "blocks", n_def));
  2720. int n_polls_on = xs_is_true(xs_dict_get_def(n_filter, "polls", n_def));
  2721. int n_webmen_on = xs_is_true(xs_dict_get_def(n_filter, "webmentions", n_def));
  2722. const xs_str *v;
  2723. xs_list *flt = xs_list_new();
  2724. xs_list_foreach(notifs, v) {
  2725. xs *noti = notify_get(snac, v);
  2726. if (noti == NULL)
  2727. continue;
  2728. const char *type = xs_dict_get(noti, "type");
  2729. const char *utype = xs_dict_get(noti, "utype");
  2730. const char *actor_id = xs_dict_get(noti, "actor");
  2731. if (strcmp(type, "EmojiReact") == 0 && xs_is_true(xs_dict_get(srv_config, "disable_emojireact")))
  2732. continue;
  2733. if (strcmp(type, "Create") == 0 && !n_ments_on)
  2734. continue;
  2735. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0 && !n_polls_on)
  2736. continue;
  2737. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") == 0 && !n_unfol_on)
  2738. continue;
  2739. if (strcmp(type, "EmojiReact") == 0 || strcmp(type, "Like") == 0) {
  2740. if (strcmp(type, "Like") == 0 && !n_likes_on)
  2741. continue;
  2742. if (strcmp(type, "EmojiReact") == 0 && !n_reacts_on)
  2743. continue;
  2744. }
  2745. if (strcmp(type, "Follow") == 0) {
  2746. if (pending_check(snac, actor_id)) {
  2747. if (!n_folreq_on)
  2748. continue;
  2749. }
  2750. else
  2751. if (!n_fol_on)
  2752. continue;
  2753. }
  2754. if (strcmp(type, "Block") == 0 && !n_blocks_on)
  2755. continue;
  2756. if (strcmp(type, "Announce") == 0 && !n_ann_on)
  2757. continue;
  2758. if (strcmp(type, "Webmention") == 0 && !n_webmen_on)
  2759. continue;
  2760. if (skip) {
  2761. skip--;
  2762. continue;
  2763. }
  2764. flt = xs_list_append(flt, v);
  2765. show--;
  2766. if (show == 0)
  2767. break;
  2768. }
  2769. return flt;
  2770. }
  2771. int notify_new_num(snac *snac)
  2772. /* counts the number of new notifications */
  2773. {
  2774. xs *t = notify_check_time(snac, 0);
  2775. xs *lst_unfilt = notify_list(snac, 0, XS_ALL);
  2776. xs *lst = notify_filter_list(snac, lst_unfilt, 0, XS_ALL);
  2777. int cnt = 0;
  2778. xs_list *p = lst;
  2779. const xs_str *v;
  2780. while (xs_list_iter(&p, &v)) {
  2781. xs *id = xs_strip_i(xs_dup(v));
  2782. /* old? count no more */
  2783. if (strcmp(id, t) < 0)
  2784. break;
  2785. cnt++;
  2786. }
  2787. return cnt;
  2788. }
  2789. void notify_clear(snac *snac)
  2790. /* clears all notifications */
  2791. {
  2792. xs *spec = xs_fmt("%s/notify/" "*", snac->basedir);
  2793. xs *lst = xs_glob(spec, 0, 0);
  2794. xs_list *p = lst;
  2795. const xs_str *v;
  2796. while (xs_list_iter(&p, &v))
  2797. unlink(v);
  2798. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  2799. if (mtime(idx) != 0.0) {
  2800. pthread_mutex_lock(&data_mutex);
  2801. truncate(idx, 0);
  2802. pthread_mutex_unlock(&data_mutex);
  2803. }
  2804. }
  2805. /** the queue **/
  2806. static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
  2807. /* writes safely to the queue */
  2808. {
  2809. xs *tfn = xs_fmt("%s.tmp", fn);
  2810. FILE *f;
  2811. if ((f = fopen(tfn, "w")) != NULL) {
  2812. xs_json_dump(msg, 4, f);
  2813. fclose(f);
  2814. rename(tfn, fn);
  2815. }
  2816. return msg;
  2817. }
  2818. static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
  2819. /* creates a queue message */
  2820. {
  2821. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  2822. xs *ntid = tid(retries * 60 * qrt);
  2823. xs *rn = xs_number_new(retries);
  2824. xs_dict *qmsg = xs_dict_new();
  2825. qmsg = xs_dict_append(qmsg, "type", type);
  2826. qmsg = xs_dict_append(qmsg, "message", msg);
  2827. qmsg = xs_dict_append(qmsg, "retries", rn);
  2828. qmsg = xs_dict_append(qmsg, "ntid", ntid);
  2829. return qmsg;
  2830. }
  2831. void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
  2832. /* enqueues an input message */
  2833. {
  2834. xs *qmsg = _new_qmsg("input", msg, retries);
  2835. const char *ntid = xs_dict_get(qmsg, "ntid");
  2836. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  2837. qmsg = xs_dict_append(qmsg, "req", req);
  2838. qmsg = _enqueue_put(fn, qmsg);
  2839. snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
  2840. }
  2841. void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
  2842. /* enqueues an input message from the shared input */
  2843. {
  2844. xs *qmsg = _new_qmsg("input", msg, retries);
  2845. const char *ntid = xs_dict_get(qmsg, "ntid");
  2846. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2847. qmsg = xs_dict_append(qmsg, "req", req);
  2848. qmsg = _enqueue_put(fn, qmsg);
  2849. srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
  2850. }
  2851. void enqueue_output_raw(const char *keyid, const char *seckey,
  2852. const xs_dict *msg, const xs_str *inbox,
  2853. int retries, int p_status)
  2854. /* enqueues an output message to an inbox */
  2855. {
  2856. xs *qmsg = _new_qmsg("output", msg, retries);
  2857. const char *ntid = xs_dict_get(qmsg, "ntid");
  2858. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2859. xs *ns = xs_number_new(p_status);
  2860. qmsg = xs_dict_append(qmsg, "p_status", ns);
  2861. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  2862. qmsg = xs_dict_append(qmsg, "keyid", keyid);
  2863. qmsg = xs_dict_append(qmsg, "seckey", seckey);
  2864. /* if it's to be sent right now, bypass the disk queue and post the job */
  2865. if (retries == 0 && p_state != NULL)
  2866. job_post(qmsg, 0);
  2867. else {
  2868. qmsg = _enqueue_put(fn, qmsg);
  2869. srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  2870. }
  2871. }
  2872. void enqueue_output(snac *snac, const xs_dict *msg,
  2873. const xs_str *inbox, int retries, int p_status)
  2874. /* enqueues an output message to an inbox */
  2875. {
  2876. if (is_msg_mine(snac, inbox)) {
  2877. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  2878. return;
  2879. }
  2880. const char *seckey = xs_dict_get(snac->key, "secret");
  2881. enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
  2882. }
  2883. void enqueue_output_by_actor(snac *snac, const xs_dict *msg,
  2884. const xs_str *actor, int retries)
  2885. /* enqueues an output message for an actor */
  2886. {
  2887. xs *inbox = get_actor_inbox(actor, 1);
  2888. if (!xs_is_null(inbox))
  2889. enqueue_output(snac, msg, inbox, retries, 0);
  2890. else
  2891. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  2892. }
  2893. void enqueue_email(const xs_dict *msg, int retries)
  2894. /* enqueues an email message to be sent */
  2895. {
  2896. xs *qmsg = _new_qmsg("email", msg, retries);
  2897. const char *ntid = xs_dict_get(qmsg, "ntid");
  2898. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2899. qmsg = _enqueue_put(fn, qmsg);
  2900. srv_debug(1, xs_fmt("enqueue_email %d", retries));
  2901. }
  2902. void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
  2903. /* enqueues a message to be sent via Telegram */
  2904. {
  2905. xs *qmsg = _new_qmsg("telegram", msg, 0);
  2906. const char *ntid = xs_dict_get(qmsg, "ntid");
  2907. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2908. qmsg = xs_dict_append(qmsg, "bot", bot);
  2909. qmsg = xs_dict_append(qmsg, "chat_id", chat_id);
  2910. qmsg = _enqueue_put(fn, qmsg);
  2911. srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
  2912. }
  2913. void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
  2914. /* enqueues a message to be sent via ntfy */
  2915. {
  2916. xs *qmsg = _new_qmsg("ntfy", msg, 0);
  2917. const char *ntid = xs_dict_get(qmsg, "ntid");
  2918. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2919. qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
  2920. qmsg = xs_dict_append(qmsg, "ntfy_token", ntfy_token);
  2921. qmsg = _enqueue_put(fn, qmsg);
  2922. srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
  2923. }
  2924. void enqueue_message(snac *snac, const xs_dict *msg)
  2925. /* enqueues an output message */
  2926. {
  2927. xs *qmsg = _new_qmsg("message", msg, 0);
  2928. const char *ntid = xs_dict_get(qmsg, "ntid");
  2929. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  2930. qmsg = _enqueue_put(fn, qmsg);
  2931. snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
  2932. }
  2933. void enqueue_close_question(snac *user, const char *id, int end_secs)
  2934. /* enqueues the closing of a question */
  2935. {
  2936. xs *qmsg = _new_qmsg("close_question", id, 0);
  2937. xs *ntid = tid(end_secs);
  2938. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  2939. qmsg = xs_dict_set(qmsg, "ntid", ntid);
  2940. qmsg = _enqueue_put(fn, qmsg);
  2941. snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
  2942. }
  2943. void enqueue_object_request(snac *user, const char *id, int forward_secs)
  2944. /* enqueues the request of an object in the future */
  2945. {
  2946. if (forward_secs < 0) {
  2947. /* set it to a somewhat random delay */
  2948. forward_secs = xs_hash_func(user->uid, strlen(user->uid)) % -forward_secs;
  2949. }
  2950. xs *qmsg = _new_qmsg("object_request", id, 0);
  2951. xs *ntid = tid(forward_secs);
  2952. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  2953. qmsg = xs_dict_set(qmsg, "ntid", ntid);
  2954. qmsg = _enqueue_put(fn, qmsg);
  2955. snac_debug(user, 0, xs_fmt("enqueue_object_request %s %d", id, forward_secs));
  2956. }
  2957. void enqueue_verify_links(snac *user)
  2958. /* enqueues a link verification */
  2959. {
  2960. xs *qmsg = _new_qmsg("verify_links", "", 0);
  2961. const char *ntid = xs_dict_get(qmsg, "ntid");
  2962. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  2963. qmsg = _enqueue_put(fn, qmsg);
  2964. snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
  2965. }
  2966. void enqueue_actor_refresh(snac *user, const char *actor, int forward_secs)
  2967. /* enqueues an actor refresh */
  2968. {
  2969. if (forward_secs < 0) {
  2970. /* set it to a somewhat random delay */
  2971. forward_secs = xs_hash_func(user->uid, strlen(user->uid)) % -forward_secs;
  2972. }
  2973. xs *qmsg = _new_qmsg("actor_refresh", "", 0);
  2974. xs *ntid = tid(forward_secs);
  2975. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  2976. qmsg = xs_dict_set(qmsg, "ntid", ntid);
  2977. qmsg = xs_dict_append(qmsg, "actor", actor);
  2978. qmsg = _enqueue_put(fn, qmsg);
  2979. snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s %d", actor, forward_secs));
  2980. }
  2981. void enqueue_webmention(const xs_dict *msg)
  2982. /* enqueues a webmention for the post */
  2983. {
  2984. xs *qmsg = _new_qmsg("webmention", msg, 0);
  2985. const char *ntid = xs_dict_get(qmsg, "ntid");
  2986. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  2987. qmsg = _enqueue_put(fn, qmsg);
  2988. srv_debug(1, xs_fmt("enqueue_webmention"));
  2989. }
  2990. void enqueue_notify_webhook(snac *user, const xs_dict *noti, int retries)
  2991. /* enqueues a notification webhook */
  2992. {
  2993. const char *webhook = xs_dict_get(user->config, "notify_webhook");
  2994. if (xs_is_string(webhook) && xs_match(webhook, "https://*|http://*")) { /** **/
  2995. xs *msg = xs_dup(noti);
  2996. /* add more data */
  2997. msg = xs_dict_set(msg, "target", user->actor);
  2998. msg = xs_dict_set(msg, "uid", user->uid);
  2999. msg = xs_dict_set(msg, "basedir", srv_basedir);
  3000. msg = xs_dict_set(msg, "baseurl", srv_baseurl);
  3001. xs *actor_obj = NULL;
  3002. if (valid_status(object_get(xs_dict_get(noti, "actor"), &actor_obj)) && actor_obj)
  3003. msg = xs_dict_set(msg, "account", actor_obj);
  3004. /* if this post is a reply, also add the inReplyTo object */
  3005. const char *in_reply_to = xs_dict_get_path(msg, "msg.object.inReplyTo");
  3006. if (xs_is_string(in_reply_to)) {
  3007. xs *irt_obj = NULL;
  3008. if (valid_status(object_get(in_reply_to, &irt_obj)))
  3009. msg = xs_dict_set(msg, "reply", irt_obj);
  3010. }
  3011. xs *qmsg = _new_qmsg("notify_webhook", msg, retries);
  3012. const char *ntid = xs_dict_get(qmsg, "ntid");
  3013. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  3014. qmsg = _enqueue_put(fn, qmsg);
  3015. snac_debug(user, 1, xs_fmt("notify_webhook"));
  3016. }
  3017. }
  3018. void enqueue_collect_replies(snac *user, const char *post)
  3019. /* enqueues a collect replies request */
  3020. {
  3021. xs *qmsg = _new_qmsg("collect_replies", post, 0);
  3022. const char *ntid = xs_dict_get(qmsg, "ntid");
  3023. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  3024. qmsg = _enqueue_put(fn, qmsg);
  3025. snac_debug(user, 1, xs_fmt("enqueue_collect_replies %s", post));
  3026. }
  3027. void enqueue_collect_outbox(snac *user, const char *actor_id)
  3028. /* enqueues a collect outbox request */
  3029. {
  3030. xs *qmsg = _new_qmsg("collect_outbox", actor_id, 0);
  3031. const char *ntid = xs_dict_get(qmsg, "ntid");
  3032. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  3033. qmsg = _enqueue_put(fn, qmsg);
  3034. snac_debug(user, 1, xs_fmt("enqueue_collect_outbox %s", actor_id));
  3035. }
  3036. void enqueue_fsck(void)
  3037. /* enqueues an fsck */
  3038. {
  3039. xs *qmsg = _new_qmsg("fsck", "", 0);
  3040. const char *ntid = xs_dict_get(qmsg, "ntid");
  3041. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  3042. qmsg = _enqueue_put(fn, qmsg);
  3043. }
  3044. int was_question_voted(snac *user, const char *id)
  3045. /* returns true if the user voted in this poll */
  3046. {
  3047. xs *children = object_children(id);
  3048. int voted = 0;
  3049. xs_list *p;
  3050. const xs_str *md5;
  3051. p = children;
  3052. while (xs_list_iter(&p, &md5)) {
  3053. xs *obj = NULL;
  3054. if (valid_status(object_get_by_md5(md5, &obj))) {
  3055. const char *atto = get_atto(obj);
  3056. if (atto && strcmp(atto, user->actor) == 0 &&
  3057. !xs_is_null(xs_dict_get(obj, "name"))) {
  3058. voted = 1;
  3059. break;
  3060. }
  3061. }
  3062. }
  3063. return voted;
  3064. }
  3065. xs_list *user_queue(snac *snac)
  3066. /* returns a list with filenames that can be dequeued */
  3067. {
  3068. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  3069. xs_list *list = xs_list_new();
  3070. time_t t = time(NULL);
  3071. xs_list *p;
  3072. const xs_val *v;
  3073. xs *fns = xs_glob(spec, 0, 0);
  3074. p = fns;
  3075. while (xs_list_iter(&p, &v)) {
  3076. /* get the retry time from the basename */
  3077. char *bn = strrchr(v, '/');
  3078. time_t t2 = atol(bn + 1);
  3079. if (t2 > t)
  3080. snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
  3081. else {
  3082. list = xs_list_append(list, v);
  3083. snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
  3084. }
  3085. }
  3086. return list;
  3087. }
  3088. xs_list *queue(void)
  3089. /* returns a list with filenames that can be dequeued */
  3090. {
  3091. xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
  3092. xs_list *list = xs_list_new();
  3093. time_t t = time(NULL);
  3094. xs_list *p;
  3095. const xs_val *v;
  3096. xs *fns = xs_glob(spec, 0, 0);
  3097. p = fns;
  3098. while (xs_list_iter(&p, &v)) {
  3099. /* get the retry time from the basename */
  3100. char *bn = strrchr(v, '/');
  3101. time_t t2 = atol(bn + 1);
  3102. if (t2 > t)
  3103. srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  3104. else {
  3105. list = xs_list_append(list, v);
  3106. srv_debug(2, xs_fmt("queue ready for %s", v));
  3107. }
  3108. }
  3109. return list;
  3110. }
  3111. xs_dict *queue_get(const char *fn)
  3112. /* gets a file from a queue */
  3113. {
  3114. FILE *f;
  3115. xs_dict *obj = NULL;
  3116. if ((f = fopen(fn, "r")) != NULL) {
  3117. obj = xs_json_load(f);
  3118. fclose(f);
  3119. }
  3120. return obj;
  3121. }
  3122. xs_dict *dequeue(const char *fn)
  3123. /* dequeues a message */
  3124. {
  3125. xs_dict *obj = queue_get(fn);
  3126. unlink(fn);
  3127. return obj;
  3128. }
  3129. /** the purge **/
  3130. static int _purge_file(const char *fn, time_t mt)
  3131. /* purge fn if it's older than days */
  3132. {
  3133. int ret = 0;
  3134. if (mtime(fn) < mt) {
  3135. /* older than the minimum time: delete it */
  3136. unlink(fn);
  3137. srv_debug(2, xs_fmt("purged %s", fn));
  3138. ret = 1;
  3139. }
  3140. return ret;
  3141. }
  3142. static void _purge_dir(const char *dir, int days)
  3143. /* purges all files in a directory older than days */
  3144. {
  3145. int cnt = 0;
  3146. if (days) {
  3147. time_t mt = time(NULL) - days * 24 * 3600;
  3148. xs *spec = xs_fmt("%s/" "*", dir);
  3149. xs *list = xs_glob(spec, 0, 0);
  3150. xs_list *p;
  3151. const xs_str *v;
  3152. p = list;
  3153. while (xs_list_iter(&p, &v))
  3154. cnt += _purge_file(v, mt);
  3155. srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
  3156. }
  3157. }
  3158. static void _purge_user_subdir(snac *snac, const char *subdir, int days)
  3159. /* purges all files in a user subdir older than days */
  3160. {
  3161. xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
  3162. _purge_dir(u_subdir, days);
  3163. }
  3164. void purge_server(void)
  3165. /* purge global server data */
  3166. {
  3167. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  3168. xs *dirs = xs_glob(spec, 0, 0);
  3169. xs_list *p;
  3170. const xs_str *v;
  3171. int cnt = 0;
  3172. int icnt = 0;
  3173. time_t mt = time(NULL) - 7 * 24 * 3600;
  3174. p = dirs;
  3175. while (xs_list_iter(&p, &v)) {
  3176. xs_list *p2;
  3177. const xs_str *v2;
  3178. {
  3179. xs *spec2 = xs_fmt("%s/" "*.json", v);
  3180. xs *files = xs_glob(spec2, 0, 0);
  3181. p2 = files;
  3182. while (xs_list_iter(&p2, &v2)) {
  3183. int n_link;
  3184. /* old and with no hard links? */
  3185. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  3186. xs *s1 = xs_replace(v2, ".json", "");
  3187. xs *l = xs_split(s1, "/");
  3188. const char *md5 = xs_list_get(l, -1);
  3189. object_del_by_md5(md5);
  3190. cnt++;
  3191. }
  3192. }
  3193. }
  3194. {
  3195. /* look for stray indexes */
  3196. xs *speci = xs_fmt("%s/" "*_?.idx", v);
  3197. xs *idxfs = xs_glob(speci, 0, 0);
  3198. p2 = idxfs;
  3199. while (xs_list_iter(&p2, &v2)) {
  3200. /* old enough to consider? */
  3201. if (mtime(v2) < mt) {
  3202. /* check if the indexed object is here */
  3203. xs *o = xs_dup(v2);
  3204. char *ext = strchr(o, '_');
  3205. if (ext) {
  3206. *ext = '\0';
  3207. o = xs_str_cat(o, ".json");
  3208. if (mtime(o) == 0.0) {
  3209. /* delete */
  3210. unlink(v2);
  3211. srv_debug(1, xs_fmt("purged %s", v2));
  3212. icnt++;
  3213. }
  3214. }
  3215. }
  3216. }
  3217. /* delete index backups */
  3218. xs *specb = xs_fmt("%s/" "*.bak", v);
  3219. xs *bakfs = xs_glob(specb, 0, 0);
  3220. p2 = bakfs;
  3221. while (xs_list_iter(&p2, &v2)) {
  3222. unlink(v2);
  3223. srv_debug(1, xs_fmt("purged %s", v2));
  3224. }
  3225. }
  3226. }
  3227. /* purge collected inboxes */
  3228. xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
  3229. _purge_dir(ib_dir, 7);
  3230. /* purge the instance timeline */
  3231. xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
  3232. int itl_gc = index_gc(itl_fn);
  3233. /* purge tag indexes */
  3234. xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
  3235. xs *tag_dirs = xs_glob(tag_spec, 0, 0);
  3236. p = tag_dirs;
  3237. int tag_gc = 0;
  3238. while (xs_list_iter(&p, &v)) {
  3239. xs *spec2 = xs_fmt("%s/" "*.idx", v);
  3240. xs *files = xs_glob(spec2, 0, 0);
  3241. xs_list *p2;
  3242. const xs_str *v2;
  3243. p2 = files;
  3244. while (xs_list_iter(&p2, &v2)) {
  3245. tag_gc += index_gc(v2);
  3246. xs *bak = xs_fmt("%s.bak", v2);
  3247. unlink(bak);
  3248. if (index_len(v2) == 0) {
  3249. /* there are no longer any entry with this tag;
  3250. purge it completely */
  3251. unlink(v2);
  3252. xs *dottag = xs_replace(v2, ".idx", ".tag");
  3253. unlink(dottag);
  3254. }
  3255. }
  3256. }
  3257. srv_debug(1, xs_fmt("purge: global "
  3258. "(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
  3259. }
  3260. void delete_purged_posts(snac *user, int days)
  3261. /* enqueues Delete activities for local purged messages */
  3262. {
  3263. if (days == 0)
  3264. return;
  3265. time_t mt = time(NULL) - days * 24 * 3600;
  3266. xs *spec = xs_fmt("%s/public/" "*.json", user->basedir);
  3267. xs *list = xs_glob(spec, 0, 0);
  3268. const char *v;
  3269. xs_list_foreach(list, v) {
  3270. if (mtime(v) < mt) {
  3271. /* to be purged; is it a Note by us? */
  3272. FILE *f;
  3273. if ((f = fopen(v, "r")) != NULL) {
  3274. xs *msg = xs_json_load(f);
  3275. fclose(f);
  3276. if (xs_is_dict(msg)) {
  3277. const char *id = xs_dict_get(msg, "id");
  3278. if (xs_is_string(id) && is_msg_mine(user, id)) {
  3279. xs *d_msg = msg_delete(user, id);
  3280. enqueue_message(user, d_msg);
  3281. snac_log(user, xs_fmt("enqueued Delete for purged message %s", id));
  3282. }
  3283. }
  3284. }
  3285. }
  3286. }
  3287. }
  3288. void purge_static(snac *user)
  3289. /* purges the static directory of a user */
  3290. {
  3291. xs *spec = xs_fmt("%s/static/""*", user->basedir);
  3292. xs *fns = xs_glob(spec, 1, 0);
  3293. xs *files = xs_dict_new();
  3294. const char *k, *v;
  3295. int cnt = 0;
  3296. /* fill a dict with the static resource urls */
  3297. xs_list_foreach(fns, v) {
  3298. if (strcmp(v, "style.css") == 0) {
  3299. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
  3300. continue;
  3301. }
  3302. xs *s = xs_fmt("%s/s/%s", user->actor, v);
  3303. xs *fn = xs_fmt("%s/static/%s", user->basedir, v);
  3304. struct stat st;
  3305. /* exclude non-files */
  3306. if (stat(fn, &st) == -1 || (st.st_mode & S_IFMT) != S_IFREG) {
  3307. snac_debug(user, 2, xs_fmt("purge_static: excluding non-file '%s'", v));
  3308. continue;
  3309. }
  3310. files = xs_dict_set(files, s, v);
  3311. cnt++;
  3312. }
  3313. /* don't purge avatars nor headers */
  3314. if (xs_is_string(v = xs_dict_get(user->config, "avatar")) && xs_dict_get(files, v)) {
  3315. files = xs_dict_del(files, v);
  3316. cnt--;
  3317. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
  3318. }
  3319. if (xs_is_string(v = xs_dict_get(user->config, "header")) && xs_dict_get(files, v)) {
  3320. files = xs_dict_del(files, v);
  3321. cnt--;
  3322. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
  3323. }
  3324. if (xs_is_string(v = xs_dict_get(srv_config, "favicon_url")) && xs_dict_get(files, v)) {
  3325. files = xs_dict_del(files, v);
  3326. cnt--;
  3327. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
  3328. }
  3329. /* exclude emojis served from the static directory */
  3330. xs *emo = emojis();
  3331. xs_dict_foreach(emo, k, v) {
  3332. if (xs_dict_get(files, v)) {
  3333. files = xs_dict_del(files, v);
  3334. cnt--;
  3335. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", v));
  3336. }
  3337. }
  3338. if (cnt <= 0)
  3339. return;
  3340. xs *tl = timeline_simple_list(user, "public", 0, XS_ALL, NULL);
  3341. const char *md5;
  3342. xs_list_foreach(tl, md5) {
  3343. if (cnt <= 0)
  3344. break;
  3345. xs *msg = NULL;
  3346. if (!valid_status(object_get_by_md5(md5, &msg)))
  3347. continue;
  3348. if (!is_msg_mine(user, xs_dict_get_def(msg, "id", "")))
  3349. continue;
  3350. const xs_list *atts = xs_dict_get(msg, "attachment");
  3351. if (!xs_is_list(atts))
  3352. continue;
  3353. const xs_dict *d;
  3354. xs_list_foreach(atts, d) {
  3355. const char *url;
  3356. if (xs_is_dict(d) && xs_is_string(url = xs_dict_get(d, "url"))) {
  3357. if (xs_dict_get(files, url)) {
  3358. files = xs_dict_del(files, url);
  3359. cnt--;
  3360. snac_debug(user, 2, xs_fmt("purge_static: excluding '%s'", url));
  3361. xs *alt_text = xs_fmt("%s.txt", url);
  3362. if (xs_dict_get(files, alt_text)) {
  3363. files = xs_dict_del(files, alt_text);
  3364. cnt--;
  3365. snac_debug(user, 2, xs_fmt("purge_static: excluding alt_text '%s'", alt_text));
  3366. }
  3367. }
  3368. }
  3369. }
  3370. }
  3371. /* what is left in the files dict is not referenced anywhere */
  3372. xs_dict_foreach(files, k, v) {
  3373. xs *s = xs_fmt("%s/static/%s", user->basedir, v);
  3374. snac_debug(user, 1, xs_fmt("purge_static: %s", s));
  3375. unlink(s);
  3376. }
  3377. }
  3378. void purge_user(snac *snac)
  3379. /* do the purge for this user */
  3380. {
  3381. int priv_days, pub_days, user_days = 0;
  3382. const char *v;
  3383. int n;
  3384. priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  3385. pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  3386. if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
  3387. (v = xs_dict_get(snac->config, "purge_days")) != NULL)
  3388. user_days = xs_number_get(v);
  3389. if (user_days) {
  3390. /* override admin settings only if they are lesser */
  3391. if (priv_days == 0 || user_days < priv_days)
  3392. priv_days = user_days;
  3393. if (pub_days == 0 || user_days < pub_days)
  3394. pub_days = user_days;
  3395. }
  3396. if (xs_is_true(xs_dict_get(srv_config, "propagate_local_purge")))
  3397. delete_purged_posts(snac, pub_days);
  3398. _purge_user_subdir(snac, "hidden", priv_days);
  3399. _purge_user_subdir(snac, "private", priv_days);
  3400. _purge_user_subdir(snac, "pending", priv_days);
  3401. _purge_user_subdir(snac, "public", pub_days);
  3402. _purge_user_subdir(snac, "admire", pub_days);
  3403. const char *idxs[] = { "followers.idx", "private.idx", "public.idx", "pinned.idx",
  3404. "bookmark.idx", "draft.idx", "sched.idx", "admire.idx", NULL };
  3405. for (n = 0; idxs[n]; n++) {
  3406. xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
  3407. int gc = index_gc(idx);
  3408. srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
  3409. }
  3410. /* purge lists */
  3411. {
  3412. xs *spec = xs_fmt("%s/list/" "*.idx", snac->basedir);
  3413. xs *lol = xs_glob(spec, 0, 0);
  3414. int c = 0;
  3415. const char *v;
  3416. while (xs_list_next(lol, &v, &c)) {
  3417. int gc = index_gc(v);
  3418. srv_debug(1, xs_fmt("purge: %s %d", v, gc));
  3419. }
  3420. }
  3421. /* purge static directories, if so desired */
  3422. if (xs_is_true(xs_dict_get(srv_config, "purge_static")))
  3423. purge_static(snac);
  3424. /* unrelated to purging, but it's a janitorial process, so what the hell */
  3425. verify_links(snac);
  3426. }
  3427. void purge_all(void)
  3428. /* purge all users */
  3429. {
  3430. snac snac;
  3431. xs *list = user_list();
  3432. char *p;
  3433. const char *uid;
  3434. p = list;
  3435. while (xs_list_iter(&p, &uid)) {
  3436. if (user_open(&snac, uid)) {
  3437. purge_user(&snac);
  3438. user_free(&snac);
  3439. }
  3440. }
  3441. purge_server();
  3442. #ifndef NO_MASTODON_API
  3443. mastoapi_purge();
  3444. #endif
  3445. }
  3446. /** archive **/
  3447. void srv_archive(const char *direction, const char *url, xs_dict *req,
  3448. const char *payload, int p_size,
  3449. int status, xs_dict *headers,
  3450. const char *body, int b_size)
  3451. /* archives a connection */
  3452. {
  3453. /* obsessive archiving */
  3454. xs *date = tid(0);
  3455. xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
  3456. FILE *f;
  3457. if (mkdirx(dir) != -1) {
  3458. xs *meta_fn = xs_fmt("%s/_META", dir);
  3459. if ((f = fopen(meta_fn, "w")) != NULL) {
  3460. xs *j1 = xs_json_dumps(req, 4);
  3461. xs *j2 = xs_json_dumps(headers, 4);
  3462. fprintf(f, "dir: %s\n", direction);
  3463. if (url)
  3464. fprintf(f, "url: %s\n", url);
  3465. fprintf(f, "req: %s\n", j1);
  3466. fprintf(f, "p_size: %d\n", p_size);
  3467. fprintf(f, "status: %d\n", status);
  3468. fprintf(f, "response: %s\n", j2);
  3469. fprintf(f, "b_size: %d\n", b_size);
  3470. fclose(f);
  3471. }
  3472. if (p_size && payload) {
  3473. xs *payload_fn = NULL;
  3474. xs *payload_fn_raw = NULL;
  3475. const char *v = xs_dict_get(req, "content-type");
  3476. if (v && xs_str_in(v, "json") != -1) {
  3477. payload_fn = xs_fmt("%s/payload.json", dir);
  3478. if ((f = fopen(payload_fn, "w")) != NULL) {
  3479. xs *v1 = xs_json_loads(payload);
  3480. xs *j1 = NULL;
  3481. if (v1 != NULL)
  3482. j1 = xs_json_dumps(v1, 4);
  3483. if (j1 != NULL)
  3484. fwrite(j1, strlen(j1), 1, f);
  3485. else
  3486. fwrite(payload, p_size, 1, f);
  3487. fclose(f);
  3488. }
  3489. }
  3490. payload_fn_raw = xs_fmt("%s/payload", dir);
  3491. if ((f = fopen(payload_fn_raw, "w")) != NULL) {
  3492. fwrite(payload, p_size, 1, f);
  3493. fclose(f);
  3494. }
  3495. }
  3496. if (b_size && body) {
  3497. xs *body_fn = NULL;
  3498. const char *v = xs_dict_get(headers, "content-type");
  3499. if (v && xs_str_in(v, "json") != -1) {
  3500. body_fn = xs_fmt("%s/body.json", dir);
  3501. if ((f = fopen(body_fn, "w")) != NULL) {
  3502. xs *v1 = xs_json_loads(body);
  3503. xs *j1 = NULL;
  3504. if (v1 != NULL)
  3505. j1 = xs_json_dumps(v1, 4);
  3506. if (j1 != NULL)
  3507. fwrite(j1, strlen(j1), 1, f);
  3508. else
  3509. fwrite(body, b_size, 1, f);
  3510. fclose(f);
  3511. }
  3512. }
  3513. else {
  3514. body_fn = xs_fmt("%s/body", dir);
  3515. if ((f = fopen(body_fn, "w")) != NULL) {
  3516. fwrite(body, b_size, 1, f);
  3517. fclose(f);
  3518. }
  3519. }
  3520. }
  3521. }
  3522. }
  3523. void srv_archive_error(const char *prefix, const xs_str *err,
  3524. const xs_dict *req, const xs_val *data)
  3525. /* archives an error */
  3526. {
  3527. xs *ntid = tid(0);
  3528. xs *fn = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
  3529. FILE *f;
  3530. if ((f = fopen(fn, "w")) != NULL) {
  3531. fprintf(f, "Error: %s\n", err);
  3532. if (req) {
  3533. fprintf(f, "Request headers:\n");
  3534. xs_json_dump(req, 4, f);
  3535. fprintf(f, "\n");
  3536. }
  3537. if (data) {
  3538. fprintf(f, "Data:\n");
  3539. if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
  3540. xs_json_dump(data, 4, f);
  3541. }
  3542. else
  3543. fprintf(f, "%s", data);
  3544. fprintf(f, "\n");
  3545. }
  3546. fclose(f);
  3547. }
  3548. }
  3549. void srv_archive_qitem(const char *prefix, xs_dict *q_item)
  3550. /* archives a q_item in the error folder */
  3551. {
  3552. xs *ntid = tid(0);
  3553. xs *fn = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
  3554. FILE *f;
  3555. if ((f = fopen(fn, "w")) != NULL) {
  3556. xs_json_dump(q_item, 4, f);
  3557. fclose(f);
  3558. }
  3559. }
  3560. t_announcement *announcement(const double after)
  3561. /* returns announcement text or NULL if none exists or it is olde than "after" */
  3562. {
  3563. static const long int MAX_SIZE = 2048;
  3564. static t_announcement a = {
  3565. .text = NULL,
  3566. .timestamp = 0.0,
  3567. };
  3568. static xs_str *fn = NULL;
  3569. if (fn == NULL)
  3570. fn = xs_fmt("%s/announcement.txt", srv_basedir);
  3571. const double ts = mtime(fn);
  3572. /* file does not exist or other than what was requested */
  3573. if (ts == 0.0 || ts <= after)
  3574. return NULL;
  3575. /* nothing changed, just return the current announcement */
  3576. if (a.text != NULL && ts <= a.timestamp)
  3577. return &a;
  3578. /* read and store new announcement */
  3579. FILE *f;
  3580. if ((f = fopen(fn, "r")) != NULL) {
  3581. fseek (f, 0, SEEK_END);
  3582. const long int length = ftell(f);
  3583. if (length > MAX_SIZE) {
  3584. /* this is probably unintentional */
  3585. srv_log(xs_fmt("announcement.txt too big: %ld bytes, max is %ld, ignoring.", length, MAX_SIZE));
  3586. }
  3587. else
  3588. if (length > 0) {
  3589. fseek (f, 0, SEEK_SET);
  3590. char *buffer = malloc(length + 1);
  3591. if (buffer) {
  3592. fread(buffer, 1, length, f);
  3593. buffer[length] = '\0';
  3594. free(a.text);
  3595. a.text = buffer;
  3596. a.timestamp = ts;
  3597. }
  3598. else {
  3599. srv_log("Error allocating memory for announcement");
  3600. }
  3601. }
  3602. else {
  3603. /* an empty file means no announcement */
  3604. free(a.text);
  3605. a.text = NULL;
  3606. a.timestamp = 0.0;
  3607. }
  3608. fclose (f);
  3609. }
  3610. if (a.text != NULL)
  3611. return &a;
  3612. return NULL;
  3613. }
  3614. xs_str *make_url(const char *href, const char *proxy, int by_token)
  3615. /* makes an URL, possibly including proxying */
  3616. {
  3617. xs_str *url = NULL;
  3618. if (proxy && !xs_startswith(href, srv_baseurl)) {
  3619. xs *p = NULL;
  3620. if (by_token) {
  3621. xs *tks = xs_fmt("%s:%s", srv_proxy_token_seed, proxy);
  3622. xs *tk = xs_md5_hex(tks, strlen(tks));
  3623. p = xs_fmt("%s/y/%s/", proxy, tk);
  3624. }
  3625. else
  3626. p = xs_fmt("%s/x/", proxy);
  3627. url = xs_replace(href, "https:/" "/", p);
  3628. }
  3629. else
  3630. url = xs_dup(href);
  3631. return url;
  3632. }
  3633. /** bad login throttle **/
  3634. xs_str *_badlogin_fn(const char *addr)
  3635. {
  3636. xs *md5 = xs_md5_hex(addr, strlen(addr));
  3637. xs *dir = xs_fmt("%s/badlogin", srv_basedir);
  3638. mkdirx(dir);
  3639. return xs_fmt("%s/%s", dir, md5);
  3640. }
  3641. int _badlogin_read(const char *fn, int *failures)
  3642. /* reads a badlogin file */
  3643. {
  3644. int ok = 0;
  3645. FILE *f;
  3646. pthread_mutex_lock(&data_mutex);
  3647. if ((f = fopen(fn, "r")) != NULL) {
  3648. xs *l = xs_readline(f);
  3649. fclose(f);
  3650. if (sscanf(l, "%d", failures) == 1)
  3651. ok = 1;
  3652. }
  3653. pthread_mutex_unlock(&data_mutex);
  3654. return ok;
  3655. }
  3656. int badlogin_check(const char *user, const char *addr)
  3657. /* checks if this address is authorized to try a login */
  3658. {
  3659. int valid = 1;
  3660. if (xs_type(addr) == XSTYPE_STRING) {
  3661. xs *fn = _badlogin_fn(addr);
  3662. double mt = mtime(fn);
  3663. if (mt > 0) {
  3664. int badlogin_expire = xs_number_get(xs_dict_get_def(srv_config,
  3665. "badlogin_expire", "300"));
  3666. mt += badlogin_expire;
  3667. /* if file is expired, delete and give pass */
  3668. if (mt < time(NULL)) {
  3669. srv_debug(1, xs_fmt("Login from %s for %s allowed again", addr, user));
  3670. unlink(fn);
  3671. }
  3672. else {
  3673. int failures;
  3674. if (_badlogin_read(fn, &failures)) {
  3675. int badlogin_max = xs_number_get(xs_dict_get_def(srv_config,
  3676. "badlogin_retries", "5"));
  3677. if (failures >= badlogin_max) {
  3678. valid = 0;
  3679. xs *d = xs_str_iso_date((time_t) mt);
  3680. srv_debug(1,
  3681. xs_fmt("Login from %s for %s forbidden until %s", addr, user, d));
  3682. }
  3683. }
  3684. }
  3685. }
  3686. }
  3687. return valid;
  3688. }
  3689. void badlogin_inc(const char *user, const char *addr)
  3690. /* increments a bad login from this address */
  3691. {
  3692. if (xs_type(addr) == XSTYPE_STRING) {
  3693. int failures = 0;
  3694. xs *fn = _badlogin_fn(addr);
  3695. FILE *f;
  3696. _badlogin_read(fn, &failures);
  3697. pthread_mutex_lock(&data_mutex);
  3698. if ((f = fopen(fn, "w")) != NULL) {
  3699. failures++;
  3700. fprintf(f, "%d %s %s\n", failures, addr, user);
  3701. fclose(f);
  3702. srv_log(xs_fmt("Registered %d login failure(s) from %s for %s", failures, addr, user));
  3703. }
  3704. pthread_mutex_unlock(&data_mutex);
  3705. }
  3706. }
  3707. /** language strings **/
  3708. const char *lang_str(const char *str, const snac *user)
  3709. /* returns a translated string */
  3710. {
  3711. const char *n_str = str;
  3712. if (user && xs_is_dict(user->lang) && xs_is_string(str)) {
  3713. n_str = xs_dict_get(user->lang, str);
  3714. if (xs_is_null(n_str) || *n_str == '\0')
  3715. n_str = str;
  3716. }
  3717. return n_str;
  3718. }
  3719. /** integrity checks **/
  3720. void data_fsck(void)
  3721. {
  3722. xs *list = user_list();
  3723. const char *uid;
  3724. xs_list_foreach(list, uid) {
  3725. snac user;
  3726. if (!user_open(&user, uid))
  3727. continue;
  3728. {
  3729. /* iterate all private posts and check that non-public posts
  3730. from this user are also linked into the public directory,
  3731. to avoid the don't-fucking-delete-my-own-private-posts purge bug */
  3732. xs *priv_spec = xs_fmt("%s/private/""*.json", user.basedir);
  3733. xs *posts = xs_glob(priv_spec, 0, 0);
  3734. const char *priv_fn;
  3735. xs_list_foreach(posts, priv_fn) {
  3736. xs *pub_fn = xs_replace(priv_fn, "/private/", "/public/");
  3737. /* already there? look no more */
  3738. if (mtime(pub_fn))
  3739. continue;
  3740. /* read the post */
  3741. FILE *f;
  3742. if ((f = fopen(priv_fn, "r")) == NULL)
  3743. continue;
  3744. xs *post = xs_json_load(f);
  3745. fclose(f);
  3746. if (!xs_is_dict(post))
  3747. continue;
  3748. const char *attr_to = get_atto(post);
  3749. if (!xs_is_string(attr_to) || strcmp(attr_to, user.actor) != 0) {
  3750. /* not from this user */
  3751. continue;
  3752. }
  3753. /* link */
  3754. snac_debug(&user, 1, xs_fmt("fsck: fixed missing link %s", xs_dict_get(post, "id")));
  3755. link(priv_fn, pub_fn);
  3756. }
  3757. }
  3758. user_free(&user);
  3759. }
  3760. }