data.c 121 KB

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