activitypub.c 122 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_json.h"
  5. #include "xs_curl.h"
  6. #include "xs_mime.h"
  7. #include "xs_openssl.h"
  8. #include "xs_regex.h"
  9. #include "xs_time.h"
  10. #include "xs_set.h"
  11. #include "xs_match.h"
  12. #include "xs_unicode.h"
  13. #include "xs_webmention.h"
  14. #include "xs_http.h"
  15. #include "snac.h"
  16. #include <sys/wait.h>
  17. const char * const public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  18. /* susie.png */
  19. const char * const susie =
  20. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  21. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  22. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  23. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  24. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  25. const char * const susie_cool =
  26. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  27. "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
  28. "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
  29. "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
  30. "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
  31. const char * const susie_muertos =
  32. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  33. "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
  34. "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
  35. "xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
  36. "QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
  37. const char *default_avatar_base64(void)
  38. /* returns the default avatar in base64 */
  39. {
  40. time_t t = time(NULL);
  41. struct tm tm;
  42. const char *p = susie;
  43. gmtime_r(&t, &tm);
  44. if (tm.tm_mon == 10 && tm.tm_mday == 2)
  45. p = susie_muertos;
  46. else
  47. if (tm.tm_wday == 0 || tm.tm_wday == 6)
  48. p = susie_cool;
  49. return p;
  50. }
  51. int activitypub_request(snac *user, const char *url, xs_dict **data)
  52. /* request an object */
  53. {
  54. int status = 0;
  55. xs *response = NULL;
  56. xs *payload = NULL;
  57. int p_size;
  58. const char *ctype;
  59. *data = NULL;
  60. if (user != NULL) {
  61. /* get from the net */
  62. response = http_signed_request(user, "GET", url,
  63. NULL, NULL, 0, &status, &payload, &p_size, 0);
  64. }
  65. if (status == 0 || (status >= 500 && status <= 599)) {
  66. /* I found an instance running Misskey that returned
  67. 500 on signed messages but returned the object
  68. perfectly without signing (?), so why not try */
  69. xs_free(response);
  70. xs *hdrs = xs_dict_new();
  71. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  72. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  73. response = xs_http_request("GET", url, hdrs,
  74. NULL, 0, &status, &payload, &p_size, 0);
  75. }
  76. if (valid_status(status)) {
  77. /* ensure it's ActivityPub data */
  78. ctype = xs_dict_get(response, "content-type");
  79. if (xs_is_null(ctype))
  80. status = HTTP_STATUS_BAD_REQUEST;
  81. else
  82. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  83. xs_str_in(ctype, "application/ld+json") != -1) {
  84. /* if there is no payload, fail */
  85. if (xs_is_null(payload))
  86. status = HTTP_STATUS_BAD_REQUEST;
  87. else
  88. *data = xs_json_loads(payload);
  89. }
  90. else
  91. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  92. }
  93. return status;
  94. }
  95. static xs_dict *actor_get_collections(snac *user, xs_dict *actor, int throttle)
  96. /* fetches follower/following/statuses counts from an actor's collections and adds them to the actor object */
  97. {
  98. /* only fetch if counts are not already present in the actor object */
  99. const xs_number *existing_followers = xs_dict_get(actor, "followers_count");
  100. const xs_number *existing_following = xs_dict_get(actor, "following_count");
  101. const xs_number *existing_statuses = xs_dict_get(actor, "statuses_count");
  102. /* skip if we already have all counts */
  103. if (xs_type(existing_followers) == XSTYPE_NUMBER &&
  104. xs_type(existing_following) == XSTYPE_NUMBER &&
  105. xs_type(existing_statuses) == XSTYPE_NUMBER) {
  106. return actor;
  107. }
  108. /* CRITICAL: duplicate URLs BEFORE any xs_dict_set calls, as xs_dict_set can reallocate the dict */
  109. xs *followers_url = xs_dup(xs_dict_get(actor, "followers"));
  110. xs *following_url = xs_dup(xs_dict_get(actor, "following"));
  111. xs *outbox_url = xs_dup(xs_dict_get(actor, "outbox"));
  112. /* only fetch followers count if not already present */
  113. if (xs_type(existing_followers) != XSTYPE_NUMBER && !xs_is_null(followers_url)) {
  114. xs *followers_coll = NULL;
  115. if (valid_status(activitypub_request(user, followers_url, &followers_coll))) {
  116. const xs_number *total = xs_dict_get(followers_coll, "totalItems");
  117. if (xs_type(total) == XSTYPE_NUMBER) {
  118. xs *total_copy = xs_dup(total);
  119. actor = xs_dict_set(actor, "followers_count", total_copy);
  120. }
  121. }
  122. /* throttle to prevent resource exhaustion on low-power devices */
  123. if (throttle)
  124. usleep(100000); /* 100ms delay between requests */
  125. }
  126. /* only fetch following count if not already present */
  127. if (xs_type(existing_following) != XSTYPE_NUMBER && !xs_is_null(following_url)) {
  128. xs *following_coll = NULL;
  129. if (valid_status(activitypub_request(user, following_url, &following_coll))) {
  130. const xs_number *total = xs_dict_get(following_coll, "totalItems");
  131. if (xs_type(total) == XSTYPE_NUMBER) {
  132. xs *total_copy = xs_dup(total);
  133. actor = xs_dict_set(actor, "following_count", total_copy);
  134. }
  135. }
  136. /* throttle to prevent resource exhaustion on low-power devices */
  137. if (throttle)
  138. usleep(100000); /* 100ms delay between requests */
  139. }
  140. /* only fetch statuses count if not already present */
  141. if (xs_type(existing_statuses) != XSTYPE_NUMBER && !xs_is_null(outbox_url)) {
  142. xs *outbox_coll = NULL;
  143. if (valid_status(activitypub_request(user, outbox_url, &outbox_coll))) {
  144. const xs_number *total = xs_dict_get(outbox_coll, "totalItems");
  145. if (xs_type(total) == XSTYPE_NUMBER) {
  146. xs *total_copy = xs_dup(total);
  147. actor = xs_dict_set(actor, "statuses_count", total_copy);
  148. }
  149. }
  150. /* throttle to prevent resource exhaustion on low-power devices */
  151. if (throttle)
  152. usleep(100000); /* 100ms delay between requests */
  153. }
  154. return actor;
  155. }
  156. int actor_request(snac *user, const char *actor, xs_dict **data)
  157. /* request an actor */
  158. {
  159. int status;
  160. xs *payload = NULL;
  161. if (data)
  162. *data = NULL;
  163. /* get from disk first */
  164. status = actor_get_refresh(user, actor, data);
  165. if (!valid_status(status)) {
  166. /* actor data non-existent: get from the net */
  167. status = activitypub_request(user, actor, &payload);
  168. if (valid_status(status)) {
  169. /* fetch collection counts when initially fetching an actor (no throttle) */
  170. payload = actor_get_collections(user, payload, 0);
  171. /* renew data */
  172. status = actor_add(actor, payload);
  173. if (data != NULL) {
  174. *data = payload;
  175. payload = NULL;
  176. }
  177. }
  178. else
  179. srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
  180. }
  181. /* collect the (presumed) shared inbox in this actor */
  182. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  183. if (valid_status(status) && data && *data)
  184. inbox_add_by_actor(*data);
  185. }
  186. return status;
  187. }
  188. const char *get_atto(const xs_dict *msg)
  189. /* gets the attributedTo field (an actor) */
  190. {
  191. const xs_val *actor = xs_dict_get(msg, "attributedTo");
  192. /* if the actor is a list of objects (like on Peertube videos), pick the Person */
  193. if (xs_type(actor) == XSTYPE_LIST) {
  194. const xs_list *p = actor;
  195. int c = 0;
  196. const xs_dict *v;
  197. actor = NULL;
  198. while (actor == NULL && xs_list_next(p, &v, &c)) {
  199. if (xs_type(v) == XSTYPE_DICT) {
  200. const char *type = xs_dict_get(v, "type");
  201. if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
  202. actor = xs_dict_get(v, "id");
  203. if (xs_type(actor) != XSTYPE_STRING)
  204. actor = NULL;
  205. }
  206. }
  207. }
  208. }
  209. else
  210. if (xs_type(actor) == XSTYPE_DICT) {
  211. /* bandwagon.fm returns this */
  212. actor = xs_dict_get(actor, "id");
  213. }
  214. return actor;
  215. }
  216. const char *get_in_reply_to(const xs_dict *msg)
  217. /* gets the inReplyTo id */
  218. {
  219. const xs_val *in_reply_to = xs_dict_get(msg, "inReplyTo");
  220. if (xs_type(in_reply_to) == XSTYPE_DICT)
  221. in_reply_to = xs_dict_get(in_reply_to, "id");
  222. return in_reply_to;
  223. }
  224. xs_list *get_attachments(const xs_dict *msg)
  225. /* unify the garbage fire that are the attachments */
  226. {
  227. xs_list *l = xs_list_new();
  228. const xs_list *p;
  229. /* try first the attachments list */
  230. if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
  231. xs *attach = NULL;
  232. const xs_val *v;
  233. /* ensure it's a list */
  234. if (xs_type(p) == XSTYPE_DICT) {
  235. attach = xs_list_new();
  236. attach = xs_list_append(attach, p);
  237. }
  238. else
  239. attach = xs_dup(p);
  240. if (xs_type(attach) == XSTYPE_LIST && xs_list_len(attach) == 0) {
  241. /* does the message have an image? */
  242. const xs_dict *d = xs_dict_get(msg, "image");
  243. if (xs_type(d) == XSTYPE_DICT) {
  244. /* add it to the attachment list */
  245. attach = xs_list_append(attach, d);
  246. }
  247. }
  248. /* now iterate the list */
  249. int c = 0;
  250. while (xs_list_next(attach, &v, &c)) {
  251. const char *type = xs_dict_get(v, "mediaType");
  252. if (xs_is_null(type))
  253. type = xs_dict_get(v, "type");
  254. if (xs_is_null(type))
  255. continue;
  256. const char *href = xs_dict_get(v, "url");
  257. if (xs_is_null(href))
  258. href = xs_dict_get(v, "href");
  259. if (xs_is_null(href))
  260. continue;
  261. /* infer MIME type from non-specific attachments */
  262. if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
  263. char *mt = (char *)xs_mime_by_ext(href);
  264. if (xs_match(mt, "image/*|audio/*|video/*")) /* */
  265. type = mt;
  266. }
  267. const char *name = xs_dict_get(v, "name");
  268. if (xs_is_null(name))
  269. name = xs_dict_get(msg, "name");
  270. if (xs_is_null(name))
  271. name = "";
  272. xs *d = xs_dict_new();
  273. d = xs_dict_append(d, "type", type);
  274. d = xs_dict_append(d, "href", href);
  275. d = xs_dict_append(d, "name", name);
  276. const xs_dict *icon = xs_dict_get(v, "icon");
  277. if (xs_type(icon) == XSTYPE_DICT)
  278. d = xs_dict_append(d, "icon", icon);
  279. l = xs_list_append(l, d);
  280. }
  281. }
  282. /** urls (attachments from Peertube) **/
  283. p = xs_dict_get(msg, "url");
  284. if (xs_type(p) == XSTYPE_LIST) {
  285. const char *href = NULL;
  286. const char *type = NULL;
  287. int c = 0;
  288. const xs_val *v;
  289. while (href == NULL && xs_list_next(p, &v, &c)) {
  290. if (xs_type(v) == XSTYPE_DICT) {
  291. const char *mtype = xs_dict_get(v, "type");
  292. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
  293. mtype = xs_dict_get(v, "mediaType");
  294. const xs_list *tag = xs_dict_get(v, "tag");
  295. if (xs_type(mtype) == XSTYPE_STRING &&
  296. strcmp(mtype, "application/x-mpegURL") == 0 &&
  297. xs_type(tag) == XSTYPE_LIST) {
  298. /* now iterate the tag list, looking for a video URL */
  299. const xs_dict *d;
  300. int c = 0;
  301. while (href == NULL && xs_list_next(tag, &d, &c)) {
  302. if (xs_type(d) == XSTYPE_DICT) {
  303. if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
  304. xs_startswith(mtype, "video/")) {
  305. const char *h = xs_dict_get(d, "href");
  306. /* this is probably it */
  307. if (xs_type(h) == XSTYPE_STRING) {
  308. href = h;
  309. type = mtype;
  310. }
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. }
  318. if (href && type) {
  319. xs *d = xs_dict_new();
  320. d = xs_dict_append(d, "href", href);
  321. d = xs_dict_append(d, "type", type);
  322. d = xs_dict_append(d, "name", "---");
  323. l = xs_list_append(l, d);
  324. }
  325. }
  326. return l;
  327. }
  328. int hashtag_in_msg(const xs_list *hashtags, const xs_dict *msg)
  329. /* returns 1 if the message contains any of the list of hashtags */
  330. {
  331. if (xs_is_list(hashtags) && xs_is_dict(msg)) {
  332. const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
  333. if (xs_is_list(tags_in_msg)) {
  334. const xs_dict *te;
  335. /* iterate the tags in the message */
  336. xs_list_foreach(tags_in_msg, te) {
  337. if (xs_is_dict(te)) {
  338. const char *type = xs_dict_get(te, "type");
  339. const char *name = xs_dict_get(te, "name");
  340. if (xs_is_string(type) && xs_is_string(name)) {
  341. if (strcmp(type, "Hashtag") == 0) {
  342. xs *lc_name = xs_utf8_to_lower(name);
  343. if (xs_list_in(hashtags, lc_name) != -1)
  344. return 1;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. return 0;
  352. }
  353. int followed_hashtag_check(snac *user, const xs_dict *msg)
  354. /* returns 1 if this message contains a hashtag followed by me */
  355. {
  356. return hashtag_in_msg(xs_dict_get(user->config, "followed_hashtags"), msg);
  357. }
  358. int blocked_hashtag_check(snac *user, const xs_dict *msg)
  359. /* returns 1 if this message contains a hashtag blocked by me */
  360. {
  361. return hashtag_in_msg(xs_dict_get(user->config, "blocked_hashtags"), msg);
  362. }
  363. int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
  364. /* ensures that an entry and its ancestors are in the timeline */
  365. {
  366. int status = 0;
  367. if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
  368. xs *msg = NULL;
  369. /* from a blocked instance? discard and break */
  370. if (is_instance_blocked(*id)) {
  371. snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
  372. return status;
  373. }
  374. /* is the object already there? */
  375. if (!valid_status((status = object_get(*id, &msg)))) {
  376. /* no; download it */
  377. status = activitypub_request(snac, *id, &msg);
  378. }
  379. if (valid_status(status)) {
  380. const xs_dict *object = msg;
  381. const char *type = xs_dict_get(object, "type");
  382. /* get the id again from the object, as it may be different */
  383. const char *nid = xs_dict_get(object, "id");
  384. if (xs_type(nid) != XSTYPE_STRING)
  385. return 0;
  386. if (wrk && strcmp(nid, *id) != 0) {
  387. snac_debug(snac, 1,
  388. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  389. *wrk = xs_dup(nid);
  390. *id = *wrk;
  391. }
  392. if (xs_is_null(type))
  393. type = "(null)";
  394. srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
  395. if (strcmp(type, "Create") == 0) {
  396. /* some software like lemmy nest Announce + Create + Note */
  397. if (!xs_is_null(object = xs_dict_get(object, "object"))) {
  398. type = xs_dict_get(object, "type");
  399. nid = xs_dict_get(object, "id");
  400. }
  401. else
  402. type = "(null)";
  403. }
  404. if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  405. if (content_match("filter_reject.txt", object))
  406. snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
  407. else
  408. if (blocked_hashtag_check(snac, object))
  409. snac_log(snac, xs_fmt("timeline_request rejected by hashtag %s", nid));
  410. else {
  411. const char *actor = get_atto(object);
  412. if (!xs_is_null(actor)) {
  413. /* request (and drop) the actor for this entry */
  414. if (!valid_status(actor_request(snac, actor, NULL))) {
  415. /* failed? retry later */
  416. enqueue_actor_refresh(snac, actor, 60);
  417. }
  418. /* does it have an ancestor? */
  419. const char *in_reply_to = get_in_reply_to(object);
  420. /* store */
  421. timeline_add(snac, nid, object);
  422. /* redistribute to lists for this user */
  423. list_distribute(snac, actor, object);
  424. /* recurse! */
  425. timeline_request(snac, &in_reply_to, NULL, level + 1);
  426. }
  427. }
  428. }
  429. }
  430. }
  431. return status;
  432. }
  433. int send_to_inbox_raw(const char *keyid, const char *seckey,
  434. const xs_str *inbox, const xs_dict *msg,
  435. xs_val **payload, int *p_size, int timeout)
  436. /* sends a message to an Inbox */
  437. {
  438. int status;
  439. xs_dict *response;
  440. xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
  441. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  442. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  443. xs_free(response);
  444. return status;
  445. }
  446. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  447. xs_val **payload, int *p_size, int timeout)
  448. /* sends a message to an Inbox */
  449. {
  450. const char *seckey = xs_dict_get(snac->key, "secret");
  451. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  452. }
  453. xs_str *get_actor_inbox(const char *actor, int shared)
  454. /* gets an actor's inbox */
  455. {
  456. xs *data = NULL;
  457. const char *v = NULL;
  458. if (valid_status(actor_request(NULL, actor, &data))) {
  459. /* try first endpoints/sharedInbox */
  460. if (shared && (v = xs_dict_get(data, "endpoints")))
  461. v = xs_dict_get(v, "sharedInbox");
  462. /* try then the regular inbox */
  463. if (xs_is_null(v))
  464. v = xs_dict_get(data, "inbox");
  465. }
  466. return xs_is_null(v) ? NULL : xs_dup(v);
  467. }
  468. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  469. xs_val **payload, int *p_size, int timeout)
  470. /* sends a message to an actor */
  471. {
  472. int status = HTTP_STATUS_BAD_REQUEST;
  473. xs *inbox = get_actor_inbox(actor, 1);
  474. if (!xs_is_null(inbox))
  475. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  476. return status;
  477. }
  478. void post_message(snac *snac, const char *actor, const xs_dict *msg)
  479. /* posts a message immediately (bypassing the output queues) */
  480. {
  481. xs *payload = NULL;
  482. int p_size;
  483. int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
  484. srv_log(xs_fmt("post_message to actor %s %d", actor, status));
  485. if (!valid_status(status))
  486. /* cannot send right now, enqueue */
  487. enqueue_message(snac, msg);
  488. }
  489. xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
  490. /* returns the list of recipients for a message */
  491. {
  492. const xs_val *to = xs_dict_get(msg, "to");
  493. const xs_val *cc = xs_dict_get(msg, "cc");
  494. xs_set rcpts;
  495. int n;
  496. xs_set_init(&rcpts);
  497. const xs_list *lists[] = { to, cc, NULL };
  498. for (n = 0; lists[n]; n++) {
  499. xs_list *l = (xs_list *)lists[n];
  500. const char *v;
  501. xs *tl = NULL;
  502. /* if it's a string, create a list with only one element */
  503. if (xs_type(l) == XSTYPE_STRING) {
  504. tl = xs_list_new();
  505. tl = xs_list_append(tl, l);
  506. l = tl;
  507. }
  508. while (xs_list_iter(&l, &v)) {
  509. if (expand_public) {
  510. if (strcmp(v, public_address) == 0 ||
  511. /* check if it's a followers collection URL */
  512. (xs_type(v) == XSTYPE_STRING &&
  513. strcmp(v, xs_fmt("%s/followers", snac->actor)) == 0) ||
  514. (xs_type(v) == XSTYPE_LIST &&
  515. xs_list_in(v, xs_fmt("%s/followers", snac->actor)) != -1)) {
  516. /* iterate the followers and add them */
  517. xs *fwers = follower_list(snac);
  518. const char *actor;
  519. char *p = fwers;
  520. while (xs_list_iter(&p, &actor))
  521. xs_set_add(&rcpts, actor);
  522. }
  523. else
  524. xs_set_add(&rcpts, v);
  525. }
  526. else
  527. xs_set_add(&rcpts, v);
  528. }
  529. }
  530. return xs_set_result(&rcpts);
  531. }
  532. int is_msg_mine(snac *user, const char *id)
  533. /* returns true if a post id is by the given user */
  534. {
  535. int ret = 0;
  536. if (xs_is_string(id)) {
  537. xs *s1 = xs_fmt("%s/", user->actor);
  538. ret = xs_startswith(id, s1);
  539. }
  540. return ret;
  541. }
  542. int is_msg_public(const xs_dict *msg)
  543. /* checks if a message is public */
  544. {
  545. const char *to = xs_dict_get(msg, "to");
  546. const char *cc = xs_dict_get(msg, "cc");
  547. int n;
  548. const char *lists[] = { to, cc, NULL };
  549. for (n = 0; lists[n]; n++) {
  550. const xs_val *l = lists[n];
  551. if (xs_type(l) == XSTYPE_STRING) {
  552. if (strcmp(l, public_address) == 0)
  553. return 1;
  554. }
  555. else
  556. if (xs_type(l) == XSTYPE_LIST) {
  557. if (xs_list_in(l, public_address) != -1)
  558. return 1;
  559. }
  560. }
  561. return 0;
  562. }
  563. int is_msg_from_private_user(const xs_dict *msg)
  564. /* checks if a message is from a local, private user */
  565. {
  566. int ret = 0;
  567. /* is this message from a local user? */
  568. if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
  569. const char *atto = get_atto(msg);
  570. xs *l = xs_split(atto, "/");
  571. const char *uid = xs_list_get(l, -1);
  572. snac user;
  573. if (uid && user_open(&user, uid)) {
  574. if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
  575. ret = 1;
  576. user_free(&user);
  577. }
  578. }
  579. return ret;
  580. }
  581. void followed_hashtag_distribute(const xs_dict *msg)
  582. /* distribute this post to all users following the included hashtags */
  583. {
  584. const char *id = xs_dict_get(msg, "id");
  585. const xs_list *tags_in_msg = xs_dict_get(msg, "tag");
  586. if (!xs_is_string(id) || !xs_is_list(tags_in_msg) || xs_list_len(tags_in_msg) == 0)
  587. return;
  588. srv_debug(1, xs_fmt("followed_hashtag_distribute check for %s", id));
  589. xs *users = user_list();
  590. const char *uid;
  591. xs_list_foreach(users, uid) {
  592. snac user;
  593. if (user_open(&user, uid)) {
  594. if (followed_hashtag_check(&user, msg)) {
  595. timeline_add(&user, id, msg);
  596. snac_log(&user, xs_fmt("followed hashtag in %s", id));
  597. }
  598. user_free(&user);
  599. }
  600. }
  601. }
  602. int is_msg_for_me(snac *snac, const xs_dict *c_msg)
  603. /* checks if this message is for me */
  604. {
  605. const char *type = xs_dict_get(c_msg, "type");
  606. const char *actor = xs_dict_get(c_msg, "actor");
  607. if (strcmp(actor, snac->actor) == 0) {
  608. /* message by myself? (most probably via the shared-inbox) reject */
  609. snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
  610. return 0;
  611. }
  612. if (xs_match(type, "Like|Announce|EmojiReact")) {
  613. const char *object = xs_dict_get(c_msg, "object");
  614. xs *obj = NULL;
  615. if (xs_is_dict(object)) {
  616. obj = xs_dup(object);
  617. object = xs_dict_get(object, "id");
  618. }
  619. /* bad object id? reject */
  620. if (!xs_is_string(object))
  621. return 0;
  622. /* try to get the object */
  623. if (!xs_is_dict(obj))
  624. object_get(object, &obj);
  625. /* if it's about one of our posts, accept it */
  626. if (xs_startswith(object, snac->actor))
  627. return 2;
  628. /* blocked by hashtag? */
  629. if (blocked_hashtag_check(snac, obj))
  630. return 0;
  631. /* if it's by someone we follow, accept it */
  632. if (following_check(snac, actor))
  633. return 1;
  634. /* do we follow any hashtag? */
  635. if (followed_hashtag_check(snac, obj))
  636. return 7;
  637. return 0;
  638. }
  639. /* if it's an Undo, it must be from someone related to us */
  640. if (xs_match(type, "Undo")) {
  641. return follower_check(snac, actor) || following_check(snac, actor);
  642. }
  643. /* if it's an Accept + Follow, it must be for a Follow we created */
  644. if (xs_match(type, "Accept")) {
  645. return following_check(snac, actor);
  646. }
  647. /* if it's a Follow, it must be explicitly for us */
  648. if (xs_match(type, "Follow")) {
  649. const char *object = xs_dict_get(c_msg, "object");
  650. return !xs_is_null(object) && strcmp(snac->actor, object) == 0;
  651. }
  652. /* only accept Ping directed to us */
  653. if (xs_match(type, "Ping")) {
  654. const char *dest = xs_dict_get(c_msg, "to");
  655. return !xs_is_null(dest) && strcmp(snac->actor, dest) == 0;
  656. }
  657. /* if it's not a Create or Update, allow as is */
  658. if (!xs_match(type, "Create|Update")) {
  659. return 1;
  660. }
  661. const xs_dict *msg = xs_dict_get(c_msg, "object");
  662. /* any blocked hashtag? reject */
  663. if (blocked_hashtag_check(snac, msg)) {
  664. snac_debug(snac, 1, xs_fmt("blocked by hashtag %s", xs_dict_get(msg, "id")));
  665. return 0;
  666. }
  667. int pub_msg = is_msg_public(c_msg);
  668. /* if this message is public and we follow the actor of this post, allow */
  669. if (pub_msg && following_check(snac, actor))
  670. return 1;
  671. xs *rcpts = recipient_list(snac, msg, 0);
  672. xs_list *p = rcpts;
  673. const xs_str *v;
  674. xs *actor_followers = NULL;
  675. if (!pub_msg) {
  676. /* not a public message; get the actor and its followers list */
  677. xs *actor_obj = NULL;
  678. if (valid_status(object_get(actor, &actor_obj))) {
  679. const xs_val *fw = xs_dict_get(actor_obj, "followers");
  680. if (fw)
  681. actor_followers = xs_dup(fw);
  682. }
  683. }
  684. while(xs_list_iter(&p, &v)) {
  685. /* explicitly for me? accept */
  686. if (strcmp(v, snac->actor) == 0)
  687. return 2;
  688. if (pub_msg) {
  689. /* a public message for someone we follow? (probably cc'ed) accept */
  690. if (strcmp(v, public_address) != 0 && following_check(snac, v))
  691. return 5;
  692. }
  693. else
  694. if (actor_followers && strcmp(v, actor_followers) == 0) {
  695. /* if this message is for this actor's followers, are we one of them? */
  696. if (following_check(snac, actor))
  697. return 6;
  698. }
  699. }
  700. /* accept if it's by someone we follow */
  701. const char *atto = get_atto(msg);
  702. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  703. return 3;
  704. /* is this message a reply to another? */
  705. const char *irt = get_in_reply_to(msg);
  706. if (!xs_is_null(irt)) {
  707. xs *r_msg = NULL;
  708. /* try to get the replied message */
  709. if (valid_status(object_get(irt, &r_msg))) {
  710. atto = get_atto(r_msg);
  711. /* accept if the replied message is from someone we follow */
  712. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  713. return 4;
  714. }
  715. }
  716. /* does this message contain a tag we are following? */
  717. if (pub_msg && followed_hashtag_check(snac, msg))
  718. return 7;
  719. return 0;
  720. }
  721. xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
  722. /* parses mentions and tags from content */
  723. {
  724. xs_str *nc = xs_str_new(NULL);
  725. xs_list *tl = *tag;
  726. xs *split;
  727. xs_list *p;
  728. const xs_val *v;
  729. int n = 0;
  730. /* create a default server for incomplete mentions */
  731. xs *def_srv = NULL;
  732. if (xs_list_len(tl)) {
  733. /* if there are any mentions, get the server from
  734. the first one, which is the inReplyTo author */
  735. p = tl;
  736. while (xs_list_iter(&p, &v)) {
  737. const char *type = xs_dict_get(v, "type");
  738. const char *name = xs_dict_get(v, "name");
  739. if (type && name && strcmp(type, "Mention") == 0) {
  740. xs *l = xs_split(name, "@");
  741. def_srv = xs_dup(xs_list_get(l, -1));
  742. break;
  743. }
  744. }
  745. }
  746. if (xs_is_null(def_srv))
  747. /* use this same server */
  748. def_srv = xs_dup(xs_dict_get(srv_config, "host"));
  749. split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#(_|[^[:punct:][:space:]])+)");
  750. p = split;
  751. while (xs_list_iter(&p, &v)) {
  752. if ((n & 0x1)) {
  753. if (*v == '@') {
  754. xs *link = NULL;
  755. xs *wuid = NULL;
  756. if (strchr(v + 1, '@') == NULL) {
  757. /* only one @? it's a dumb Mastodon-like mention
  758. without server; add the default one */
  759. wuid = xs_fmt("%s@%s", v, def_srv);
  760. snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
  761. }
  762. else
  763. wuid = xs_dup(v);
  764. /* query the webfinger about this fellow */
  765. xs *actor = NULL;
  766. xs *uid = NULL;
  767. int status;
  768. status = webfinger_request(wuid, &actor, &uid);
  769. if (valid_status(status) && actor && uid) {
  770. xs *d = xs_dict_new();
  771. xs *n = xs_fmt("@%s", uid);
  772. d = xs_dict_append(d, "type", "Mention");
  773. d = xs_dict_append(d, "href", actor);
  774. d = xs_dict_append(d, "name", n);
  775. tl = xs_list_append(tl, d);
  776. link = xs_fmt("<span class=\"h-card\"><a href=\"%s\" class=\"u-url mention\">%s</a></span>", actor, n);
  777. }
  778. if (!xs_is_null(link))
  779. nc = xs_str_cat(nc, link);
  780. else
  781. nc = xs_str_cat(nc, v);
  782. }
  783. else
  784. if (*v == '#') {
  785. /* hashtag */
  786. xs *d = xs_dict_new();
  787. xs *n = xs_utf8_to_lower(v);
  788. xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
  789. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  790. d = xs_dict_append(d, "type", "Hashtag");
  791. d = xs_dict_append(d, "href", h);
  792. d = xs_dict_append(d, "name", n);
  793. tl = xs_list_append(tl, d);
  794. /* add the code */
  795. nc = xs_str_cat(nc, l);
  796. }
  797. else
  798. if (*v == '&') {
  799. /* HTML Unicode entity, probably part of an emoji */
  800. /* write as is */
  801. nc = xs_str_cat(nc, v);
  802. }
  803. }
  804. else
  805. nc = xs_str_cat(nc, v);
  806. n++;
  807. }
  808. *tag = tl;
  809. return nc;
  810. }
  811. void collect_replies(snac *user, const char *id)
  812. /* collects all replies for a post */
  813. {
  814. xs *obj = NULL;
  815. if (!valid_status(object_get(id, &obj))) {
  816. snac_debug(user, 1, xs_fmt("collect_replies: object '%s' is not here", id));
  817. return;
  818. }
  819. const xs_dict *replies_first = xs_dict_get_path(obj, "replies.first");
  820. if (!xs_is_dict(replies_first)) {
  821. snac_debug(user, 1, xs_fmt("collect_replies: object '%s' does not have replies.first", id));
  822. return;
  823. }
  824. const xs_list *level0_replies = xs_dict_get(replies_first, "items");
  825. const xs_list *level1_replies = NULL;
  826. const char *next = xs_dict_get(replies_first, "next");
  827. xs *reply_obj = NULL;
  828. if (xs_is_string(next) && !valid_status(activitypub_request(user, next, &reply_obj))) {
  829. snac_debug(user, 1, xs_fmt("collect_replies: error getting next replies object '%s'", next));
  830. return;
  831. }
  832. if (xs_is_dict(reply_obj))
  833. level1_replies = xs_dict_get(reply_obj, "items");
  834. xs *items = xs_list_new();
  835. if (xs_is_list(level0_replies))
  836. items = xs_list_cat(items, level0_replies);
  837. if (xs_is_list(level1_replies))
  838. items = xs_list_cat(items, level1_replies);
  839. const xs_val *v;
  840. xs_list_foreach(items, v) {
  841. xs *reply = NULL;
  842. if (xs_is_string(v)) {
  843. /* request the object */
  844. if (!valid_status(object_get(v, &reply))) {
  845. if (!valid_status(activitypub_request(user, v, &reply))) {
  846. snac_debug(user, 1, xs_fmt("collect_replies: error requesting object '%s'", v));
  847. continue;
  848. }
  849. }
  850. }
  851. else
  852. if (xs_is_dict(v)) {
  853. /* actually the post object */
  854. reply = xs_dup(v);
  855. }
  856. if (!xs_is_dict(reply))
  857. continue;
  858. const char *id = xs_dict_get(reply, "id");
  859. const char *type = xs_dict_get(reply, "type");
  860. const char *attr_to = get_atto(reply);
  861. if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
  862. continue;
  863. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  864. continue;
  865. if (timeline_here(user, id)) {
  866. snac_debug(user, 1, xs_fmt("collect_replies: item already in timeline %s", id));
  867. continue;
  868. }
  869. enqueue_actor_refresh(user, attr_to, 0);
  870. timeline_add(user, id, reply);
  871. snac_log(user, xs_fmt("new '%s' (collect_replies) %s %s", type, attr_to, id));
  872. }
  873. }
  874. void collect_outbox(snac *user, const char *actor_id)
  875. /* gets an actor's outbox and inserts a bunch of posts in a user's timeline */
  876. {
  877. int status;
  878. xs *actor = NULL;
  879. if (!valid_status(status = actor_request(user, actor_id, &actor))) {
  880. snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor object '%s' %d", actor_id, status));
  881. return;
  882. }
  883. xs *outbox = NULL;
  884. const char *outbox_url = xs_dict_get(actor, "outbox");
  885. if (!xs_is_string(outbox_url))
  886. return;
  887. if (!valid_status(status = activitypub_request(user, outbox_url, &outbox))) {
  888. snac_debug(user, 1, xs_fmt("collect_outbox: cannot get actor outbox '%s' %d", outbox_url, status));
  889. return;
  890. }
  891. const xs_list *ordered_items = xs_dict_get(outbox, "orderedItems");
  892. if (!xs_is_list(ordered_items)) {
  893. /* the list is not here; does it have a 'first'? */
  894. const char *first = xs_dict_get(outbox, "first");
  895. if (xs_is_string(first)) {
  896. /* download this instead */
  897. xs *first2 = xs_dup(first);
  898. xs_free(outbox);
  899. if (!valid_status(status = activitypub_request(user, first2, &outbox))) {
  900. snac_debug(user, 1, xs_fmt("collect_outbox: cannot get first page of outbox '%s' %d", first2, status));
  901. return;
  902. }
  903. /* last chance */
  904. ordered_items = xs_dict_get(outbox, "orderedItems");
  905. }
  906. }
  907. if (!xs_is_list(ordered_items)) {
  908. snac_debug(user, 1, xs_fmt("collect_outbox: cannot get list of posts for actor '%s' outbox", actor_id));
  909. return;
  910. }
  911. /* well, ok, then */
  912. int max = 4;
  913. const xs_val *v;
  914. xs_list_foreach(ordered_items, v) {
  915. if (max == 0)
  916. break;
  917. xs *post = NULL;
  918. if (xs_is_string(v)) {
  919. /* it's probably the post url */
  920. if (!valid_status(activitypub_request(user, v, &post)))
  921. continue;
  922. }
  923. else
  924. if (xs_is_dict(v))
  925. post = xs_dup(v);
  926. if (post == NULL)
  927. continue;
  928. const char *type = xs_dict_get(post, "type");
  929. if (!xs_is_string(type) || strcmp(type, "Create")) {
  930. /* not a post */
  931. continue;
  932. }
  933. if (is_msg_for_me(user, post) == 0) {
  934. /* not for us */
  935. continue;
  936. }
  937. const xs_dict *object = xs_dict_get(post, "object");
  938. if (!xs_is_dict(object))
  939. continue;
  940. type = xs_dict_get(object, "type");
  941. const char *id = xs_dict_get(object, "id");
  942. const char *attr_to = get_atto(object);
  943. if (!xs_is_string(type) || !xs_is_string(id) || !xs_is_string(attr_to))
  944. continue;
  945. if (!timeline_here(user, id)) {
  946. timeline_add(user, id, object);
  947. snac_log(user, xs_fmt("new '%s' (collect_outbox) %s %s", type, attr_to, id));
  948. }
  949. else
  950. snac_debug(user, 1, xs_fmt("collect_outbox: post '%s' already here", id));
  951. max--;
  952. }
  953. }
  954. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  955. /* notifies the user of relevant events */
  956. {
  957. /* skip our own notifications */
  958. if (strcmp(snac->actor, actor) == 0)
  959. return;
  960. const char *id = xs_dict_get(msg, "id");
  961. if (strcmp(type, "Create") == 0) {
  962. /* only notify of notes specifically for us */
  963. xs *rcpts = recipient_list(snac, msg, 0);
  964. if (xs_list_in(rcpts, snac->actor) == -1)
  965. return;
  966. /* discard votes */
  967. const xs_dict *note = xs_dict_get(msg, "object");
  968. if (note && !xs_is_null(xs_dict_get(note, "name")))
  969. return;
  970. }
  971. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  972. return;
  973. /* get the object id */
  974. const char *objid = xs_dict_get(msg, "object");
  975. if (xs_type(objid) == XSTYPE_DICT)
  976. objid = xs_dict_get(objid, "id");
  977. if (xs_match(type, "Like|Announce|EmojiReact")) {
  978. /* if it's not an admiration about something by us, done */
  979. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  980. return;
  981. /* if it's an announce by our own relay, done */
  982. xs *relay_id = xs_fmt("%s/relay", srv_baseurl);
  983. if (xs_startswith(id, relay_id))
  984. return;
  985. }
  986. /* updated poll? */
  987. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  988. const xs_dict *poll;
  989. const char *poll_id;
  990. if ((poll = xs_dict_get(msg, "object")) == NULL)
  991. return;
  992. /* if it's not closed, discard */
  993. if (xs_is_null(xs_dict_get(poll, "closed")))
  994. return;
  995. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  996. return;
  997. /* if it's not ours and we didn't vote, discard */
  998. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  999. return;
  1000. }
  1001. /* user will love to know about this! */
  1002. /* prepare message body */
  1003. xs *body = xs_fmt("User : @%s@%s\n",
  1004. xs_dict_get(snac->config, "uid"),
  1005. xs_dict_get(srv_config, "host")
  1006. );
  1007. if (strcmp(utype, "(null)") != 0) {
  1008. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  1009. body = xs_str_cat(body, s1);
  1010. }
  1011. else {
  1012. xs *s1 = xs_fmt("Type : %s\n", type);
  1013. body = xs_str_cat(body, s1);
  1014. }
  1015. {
  1016. xs *s1 = xs_fmt("Actor : %s\n", actor);
  1017. body = xs_str_cat(body, s1);
  1018. }
  1019. if (objid != NULL) {
  1020. xs *s1 = xs_fmt("Object: %s\n", objid);
  1021. body = xs_str_cat(body, s1);
  1022. }
  1023. /* email */
  1024. const char *email = "[disabled by admin]";
  1025. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  1026. email = xs_dict_get(snac->config_o, "email");
  1027. if (xs_is_null(email)) {
  1028. email = xs_dict_get(snac->config, "email");
  1029. if (xs_is_null(email))
  1030. email = "[empty]";
  1031. }
  1032. }
  1033. if (*email != '\0' && *email != '[') {
  1034. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  1035. xs *subject = xs_fmt("snac notify for @%s@%s",
  1036. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  1037. xs *from = xs_fmt("<snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  1038. xs *header = xs_fmt(
  1039. "From: snac-daemon %s\n"
  1040. "To: %s\n"
  1041. "Subject: %s\n"
  1042. "\n",
  1043. from, email, subject);
  1044. xs *mailinfo = xs_dict_new();
  1045. xs *bd = xs_fmt("%s%s", header, body);
  1046. mailinfo = xs_dict_append(mailinfo, "from", from);
  1047. mailinfo = xs_dict_append(mailinfo, "to", email);
  1048. mailinfo = xs_dict_append(mailinfo, "body", bd);
  1049. enqueue_email(mailinfo, 0);
  1050. }
  1051. /* telegram */
  1052. const char *bot = xs_dict_get(snac->config, "telegram_bot");
  1053. const char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  1054. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  1055. enqueue_telegram(body, bot, chat_id);
  1056. /* ntfy */
  1057. const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
  1058. const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
  1059. if (!xs_is_null(ntfy_server) && *ntfy_server)
  1060. enqueue_ntfy(body, ntfy_server, ntfy_token);
  1061. /* auto boost */
  1062. if (xs_match(type, "Create") && xs_is_true(xs_dict_get(snac->config, "auto_boost"))) {
  1063. xs *msg = msg_admiration(snac, objid, "Announce");
  1064. enqueue_message(snac, msg);
  1065. snac_debug(snac, 1, xs_fmt("auto boosted %s", objid));
  1066. }
  1067. /* finally, store it in the notification folder */
  1068. if (strcmp(type, "Follow") == 0)
  1069. objid = id;
  1070. else
  1071. if (strcmp(utype, "Follow") == 0)
  1072. objid = actor;
  1073. notify_add(snac, type, utype, actor, objid != NULL ? objid : id, msg);
  1074. }
  1075. /** messages **/
  1076. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  1077. const char *actor, const char *date, const char *object)
  1078. /* creates a base ActivityPub message */
  1079. {
  1080. xs *did = NULL;
  1081. xs *published = NULL;
  1082. xs *ntid = tid(0);
  1083. const char *obj_id;
  1084. if (xs_type(object) == XSTYPE_DICT)
  1085. obj_id = xs_dict_get(object, "id");
  1086. else
  1087. obj_id = object;
  1088. /* generated values */
  1089. if (date && strcmp(date, "@now") == 0) {
  1090. published = xs_str_utctime(0, ISO_DATE_SPEC);
  1091. date = published;
  1092. }
  1093. if (id != NULL) {
  1094. if (strcmp(id, "@dummy") == 0) {
  1095. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  1096. id = did;
  1097. }
  1098. else
  1099. if (strcmp(id, "@object") == 0) {
  1100. if (obj_id != NULL) {
  1101. did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
  1102. id = did;
  1103. }
  1104. else
  1105. id = NULL;
  1106. }
  1107. else
  1108. if (strcmp(id, "@wrapper") == 0) {
  1109. /* like @object, but always generate the same id */
  1110. if (object != NULL) {
  1111. date = xs_dict_get(object, "published");
  1112. did = xs_fmt("%s/%s", obj_id, type);
  1113. id = did;
  1114. }
  1115. else
  1116. id = NULL;
  1117. }
  1118. }
  1119. xs_dict *msg = xs_dict_new();
  1120. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  1121. msg = xs_dict_append(msg, "type", type);
  1122. if (id != NULL)
  1123. msg = xs_dict_append(msg, "id", id);
  1124. if (actor != NULL)
  1125. msg = xs_dict_append(msg, "actor", actor);
  1126. if (date != NULL)
  1127. msg = xs_dict_append(msg, "published", date);
  1128. if (object != NULL)
  1129. msg = xs_dict_append(msg, "object", object);
  1130. return msg;
  1131. }
  1132. xs_dict *msg_collection(snac *snac, const char *id, int items)
  1133. /* creates an empty OrderedCollection message */
  1134. {
  1135. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  1136. xs *n = xs_number_new(items);
  1137. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  1138. msg = xs_dict_append(msg, "totalItems", n);
  1139. return msg;
  1140. }
  1141. xs_dict *msg_replies(snac *user, const char *id, int fill)
  1142. /* creates a CollectionPage with replies of id */
  1143. {
  1144. xs *r_id = xs_replace(id, "/p/", "/r/");
  1145. xs *r_idp = xs_fmt("%s?page=true", r_id);
  1146. xs *r_idh = xs_fmt("%s", r_id);
  1147. xs_dict *msg = msg_base(user, "CollectionPage", r_idp, NULL, NULL, NULL);
  1148. msg = xs_dict_set(msg, "partOf", r_idh);
  1149. xs *items = xs_list_new();
  1150. if (fill) {
  1151. xs *children = object_children(id);
  1152. const char *md5;
  1153. xs_list_foreach(children, md5) {
  1154. xs *obj = NULL;
  1155. if (valid_status(object_get_by_md5(md5, &obj)) && is_msg_public(obj)) {
  1156. const char *c_id = xs_dict_get(obj, "id");
  1157. if (xs_is_string(c_id))
  1158. items = xs_list_append(items, c_id);
  1159. }
  1160. }
  1161. }
  1162. else {
  1163. msg = xs_dict_del(msg, "@context");
  1164. msg = xs_dict_del(msg, "id");
  1165. msg = xs_dict_set(msg, "next", r_idp);
  1166. }
  1167. msg = xs_dict_set(msg, "items", items);
  1168. return msg;
  1169. }
  1170. xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
  1171. /* creates an Accept message (as a response to a Follow) */
  1172. {
  1173. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  1174. msg = xs_dict_append(msg, "to", to);
  1175. return msg;
  1176. }
  1177. xs_dict *msg_update(snac *snac, const xs_dict *object)
  1178. /* creates an Update message */
  1179. {
  1180. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  1181. const char *type = xs_dict_get(object, "type");
  1182. if (strcmp(type, "Note") == 0) {
  1183. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  1184. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  1185. }
  1186. else
  1187. if (strcmp(type, "Person") == 0) {
  1188. msg = xs_dict_append(msg, "to", public_address);
  1189. /* also spam the people being followed, so that
  1190. they have the newest information about who we are */
  1191. xs *cc = following_list(snac);
  1192. msg = xs_dict_append(msg, "cc", cc);
  1193. }
  1194. else
  1195. msg = xs_dict_append(msg, "to", public_address);
  1196. return msg;
  1197. }
  1198. xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
  1199. /* creates a Like or Announce message */
  1200. {
  1201. xs *a_msg = NULL;
  1202. xs_dict *msg = NULL;
  1203. xs *wrk = NULL;
  1204. /* call the object */
  1205. timeline_request(snac, &object, &wrk, 0);
  1206. if (valid_status(object_get(object, &a_msg))) {
  1207. xs *rcpts = xs_list_new();
  1208. xs *o_md5 = xs_md5_hex(object, strlen(object));
  1209. xs *id = xs_fmt("%s/%s/%s", snac->actor, *type == 'L' ? "l" : "a", o_md5);
  1210. msg = msg_base(snac, type, id, snac->actor, "@now", object);
  1211. if (is_msg_public(a_msg))
  1212. rcpts = xs_list_append(rcpts, public_address);
  1213. rcpts = xs_list_append(rcpts, get_atto(a_msg));
  1214. msg = xs_dict_append(msg, "to", rcpts);
  1215. }
  1216. else
  1217. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  1218. return msg;
  1219. }
  1220. xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
  1221. /* creates an Undo + admiration message */
  1222. {
  1223. xs *a_msg = NULL;
  1224. xs_dict *msg = NULL;
  1225. if (valid_status(object_get(id, &a_msg))) {
  1226. /* create a clone of the original admiration message */
  1227. xs *object = msg_admiration(user, id, type);
  1228. /* delete the published date */
  1229. object = xs_dict_del(object, "published");
  1230. /* create an undo message for this object */
  1231. msg = msg_undo(user, object);
  1232. /* copy the 'to' field */
  1233. msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
  1234. }
  1235. /* now we despise this */
  1236. object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
  1237. return msg;
  1238. }
  1239. xs_dict *msg_actor_place(snac *user, const char *label)
  1240. /* creates a Place object, if the user has a location defined */
  1241. {
  1242. xs_dict *place = NULL;
  1243. const char *latitude = xs_dict_get_def(user->config, "latitude", "");
  1244. const char *longitude = xs_dict_get_def(user->config, "longitude", "");
  1245. if (*latitude && *longitude) {
  1246. xs *d_la = xs_number_new(atof(latitude));
  1247. xs *d_lo = xs_number_new(atof(longitude));
  1248. place = msg_base(user, "Place", NULL, user->actor, NULL, NULL);
  1249. place = xs_dict_set(place, "name", label);
  1250. place = xs_dict_set(place, "latitude", d_la);
  1251. place = xs_dict_set(place, "longitude", d_lo);
  1252. }
  1253. return place;
  1254. }
  1255. xs_dict *msg_actor(snac *snac)
  1256. /* create a Person message for this actor */
  1257. {
  1258. xs *ctxt = xs_list_new();
  1259. xs *icon = xs_dict_new();
  1260. xs *keys = xs_dict_new();
  1261. xs *tags = xs_list_new();
  1262. xs *avtr = NULL;
  1263. xs *kid = NULL;
  1264. xs *f_bio = NULL;
  1265. xs_dict *msg = NULL;
  1266. const char *p;
  1267. int n;
  1268. /* everybody loves some caching */
  1269. if (time(NULL) - object_mtime(snac->actor) < 3 * 3600 &&
  1270. valid_status(object_get(snac->actor, &msg))) {
  1271. snac_debug(snac, 2, xs_fmt("Returning cached actor %s", snac->actor));
  1272. return msg;
  1273. }
  1274. msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  1275. /* change the @context (is this really necessary?) */
  1276. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  1277. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  1278. msg = xs_dict_set(msg, "@context", ctxt);
  1279. msg = xs_dict_set(msg, "url", snac->actor);
  1280. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  1281. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  1282. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  1283. // this exists so we get the emoji tags from our name too.
  1284. // and then we just throw away the result, because it's kinda useless to have markdown in the display name.
  1285. xs *name_dummy = not_really_markdown(xs_dict_get(snac->config, "name"), NULL, &tags);
  1286. xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
  1287. f_bio = process_tags(snac, f_bio_2, &tags);
  1288. msg = xs_dict_set(msg, "summary", f_bio);
  1289. msg = xs_dict_set(msg, "tag", tags);
  1290. char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
  1291. for (n = 0; folders[n]; n++) {
  1292. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  1293. msg = xs_dict_set(msg, folders[n], f);
  1294. }
  1295. p = xs_dict_get(snac->config, "avatar");
  1296. if (*p == '\0')
  1297. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  1298. else
  1299. avtr = xs_dup(p);
  1300. icon = xs_dict_append(icon, "type", "Image");
  1301. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  1302. icon = xs_dict_append(icon, "url", avtr);
  1303. msg = xs_dict_set(msg, "icon", icon);
  1304. kid = xs_fmt("%s#main-key", snac->actor);
  1305. keys = xs_dict_append(keys, "id", kid);
  1306. keys = xs_dict_append(keys, "owner", snac->actor);
  1307. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  1308. msg = xs_dict_set(msg, "publicKey", keys);
  1309. /* if the "bot" config field is set to true, change type to "Service" */
  1310. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  1311. msg = xs_dict_set(msg, "type", "Service");
  1312. /* if it's named "relay", then identify as an "Application" */
  1313. if (strcmp(snac->uid, "relay") == 0)
  1314. msg = xs_dict_set(msg, "type", "Application");
  1315. /* add the header image, if there is one defined */
  1316. const char *header = xs_dict_get(snac->config, "header");
  1317. if (!xs_is_null(header)) {
  1318. xs *d = xs_dict_new();
  1319. d = xs_dict_append(d, "type", "Image");
  1320. d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
  1321. d = xs_dict_append(d, "url", header);
  1322. msg = xs_dict_set(msg, "image", d);
  1323. }
  1324. /* add the metadata as attachments of PropertyValue */
  1325. xs *metadata = NULL;
  1326. const xs_dict *md = xs_dict_get(snac->config, "metadata");
  1327. if (xs_type(md) == XSTYPE_DICT)
  1328. metadata = xs_dup(md);
  1329. else
  1330. if (xs_type(md) == XSTYPE_STRING) {
  1331. metadata = xs_dict_new();
  1332. xs *l = xs_split(md, "\n");
  1333. const char *ll;
  1334. xs_list_foreach(l, ll) {
  1335. xs *kv = xs_split_n(ll, "=", 1);
  1336. const char *k = xs_list_get(kv, 0);
  1337. const char *v = xs_list_get(kv, 1);
  1338. if (k && v) {
  1339. xs *kk = xs_strip_i(xs_dup(k));
  1340. xs *vv = xs_strip_i(xs_dup(v));
  1341. metadata = xs_dict_set(metadata, kk, vv);
  1342. }
  1343. }
  1344. }
  1345. if (xs_type(metadata) == XSTYPE_DICT) {
  1346. xs *attach = xs_list_new();
  1347. const xs_str *k;
  1348. const xs_str *v;
  1349. int c = 0;
  1350. while (xs_dict_next(metadata, &k, &v, &c)) {
  1351. xs *d = xs_dict_new();
  1352. xs *k2 = encode_html(k);
  1353. xs *v2 = NULL;
  1354. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
  1355. xs *t = encode_html(v);
  1356. v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
  1357. }
  1358. else
  1359. v2 = encode_html(v);
  1360. d = xs_dict_append(d, "type", "PropertyValue");
  1361. d = xs_dict_append(d, "name", k2);
  1362. d = xs_dict_append(d, "value", v2);
  1363. attach = xs_list_append(attach, d);
  1364. }
  1365. msg = xs_dict_set(msg, "attachment", attach);
  1366. }
  1367. /* use shared inboxes? */
  1368. if (xs_is_true(xs_dict_get(srv_config, "shared_inboxes")) || strcmp(snac->uid, "relay") == 0) {
  1369. xs *d = xs_dict_new();
  1370. xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
  1371. d = xs_dict_append(d, "sharedInbox", si);
  1372. msg = xs_dict_set(msg, "endpoints", d);
  1373. }
  1374. /* does this user have an aka? */
  1375. const char *aka = xs_dict_get(snac->config, "alias");
  1376. if (xs_type(aka) == XSTYPE_STRING && *aka) {
  1377. xs *loaka = xs_list_append(xs_list_new(), aka);
  1378. msg = xs_dict_set(msg, "alsoKnownAs", loaka);
  1379. }
  1380. const xs_val *manually = xs_dict_get(snac->config, "approve_followers");
  1381. msg = xs_dict_set(msg, "manuallyApprovesFollowers",
  1382. xs_stock(xs_is_true(manually) ? XSTYPE_TRUE : XSTYPE_FALSE));
  1383. /* if there are location coords, create a Place object */
  1384. xs *location = msg_actor_place(snac, "Home");
  1385. if (xs_type(location) == XSTYPE_DICT)
  1386. msg = xs_dict_set(msg, "location", location);
  1387. /* cache it */
  1388. snac_debug(snac, 1, xs_fmt("Caching actor %s", snac->actor));
  1389. object_add_ow(snac->actor, msg);
  1390. return msg;
  1391. }
  1392. xs_dict *msg_create(snac *snac, const xs_dict *object)
  1393. /* creates a 'Create' message */
  1394. {
  1395. xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
  1396. const xs_val *v;
  1397. if ((v = get_atto(object)))
  1398. msg = xs_dict_append(msg, "attributedTo", v);
  1399. if ((v = xs_dict_get(object, "cc")))
  1400. msg = xs_dict_append(msg, "cc", v);
  1401. if ((v = xs_dict_get(object, "to")))
  1402. msg = xs_dict_append(msg, "to", v);
  1403. else
  1404. msg = xs_dict_append(msg, "to", public_address);
  1405. return msg;
  1406. }
  1407. xs_dict *msg_undo(snac *snac, const xs_val *object)
  1408. /* creates an 'Undo' message */
  1409. {
  1410. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  1411. const char *to;
  1412. if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
  1413. msg = xs_dict_append(msg, "to", to);
  1414. return msg;
  1415. }
  1416. xs_dict *msg_delete(snac *snac, const char *id)
  1417. /* creates a 'Delete' + 'Tombstone' for a local entry */
  1418. {
  1419. xs *tomb = xs_dict_new();
  1420. xs_dict *msg = NULL;
  1421. /* sculpt the tombstone */
  1422. tomb = xs_dict_append(tomb, "type", "Tombstone");
  1423. tomb = xs_dict_append(tomb, "id", id);
  1424. /* now create the Delete */
  1425. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  1426. xs *to = xs_list_new();
  1427. xs *involved = object_likes(id);
  1428. xs *boosts = object_announces(id);
  1429. xs *children = object_children(id);
  1430. const char *md5;
  1431. involved = xs_list_cat(involved, boosts);
  1432. involved = xs_list_cat(involved, children);
  1433. /* add everybody */
  1434. to = xs_list_append(to, public_address);
  1435. /* add actors that liked, boosted or replied to this */
  1436. xs_list_foreach(involved, md5) {
  1437. xs *actor = NULL;
  1438. if (valid_status(object_get_by_md5(md5, &actor))) {
  1439. const char *id = xs_dict_get(actor, "id");
  1440. const char *atto = get_atto(actor);
  1441. if (xs_is_string(atto))
  1442. to = xs_list_append(to, atto);
  1443. else
  1444. if (xs_is_string(id))
  1445. to = xs_list_append(to, id);
  1446. }
  1447. }
  1448. msg = xs_dict_append(msg, "to", to);
  1449. return msg;
  1450. }
  1451. xs_dict *msg_follow(snac *snac, const char *q)
  1452. /* creates a 'Follow' message */
  1453. {
  1454. xs *actor_o = NULL;
  1455. xs *actor = NULL;
  1456. xs_dict *msg = NULL;
  1457. int status;
  1458. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  1459. if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
  1460. actor = xs_dup(url_or_uid);
  1461. else
  1462. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  1463. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  1464. return NULL;
  1465. }
  1466. /* request the actor */
  1467. status = actor_request(snac, actor, &actor_o);
  1468. if (valid_status(status)) {
  1469. /* check if the actor is an alias */
  1470. const char *r_actor = xs_dict_get(actor_o, "id");
  1471. if (r_actor && strcmp(actor, r_actor) != 0) {
  1472. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  1473. }
  1474. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  1475. }
  1476. else
  1477. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  1478. return msg;
  1479. }
  1480. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  1481. const xs_str *in_reply_to, const xs_list *attach,
  1482. int scope, const char *lang_str, const char *msg_date)
  1483. /* creates a 'Note' message */
  1484. /* scope: 0, public; 1, private (mentioned only); 2, "quiet public"; 3, followers only */
  1485. {
  1486. xs *ntid = tid(0);
  1487. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  1488. xs *ctxt = NULL;
  1489. xs *fc2 = NULL;
  1490. xs *fc1 = NULL;
  1491. xs *to = NULL;
  1492. xs *cc = xs_list_new();
  1493. xs *irt = NULL;
  1494. xs *tag = xs_list_new();
  1495. xs *atls = xs_list_new();
  1496. /* discard non-parseable dates */
  1497. if (!xs_is_string(msg_date) || xs_parse_iso_date(msg_date, 0) == 0)
  1498. msg_date = NULL;
  1499. xs_dict *msg = msg_base(snac, "Note", id, NULL, xs_or(msg_date, "@now"), NULL);
  1500. xs_list *p;
  1501. const xs_val *v;
  1502. const int priv = (scope == SCOPE_MENTIONED || scope == SCOPE_FOLLOWERS);
  1503. if (rcpts == NULL)
  1504. to = xs_list_new();
  1505. else {
  1506. if (xs_type(rcpts) == XSTYPE_STRING) {
  1507. to = xs_list_new();
  1508. to = xs_list_append(to, rcpts);
  1509. }
  1510. else
  1511. to = xs_dup(rcpts);
  1512. }
  1513. /* format the content */
  1514. fc2 = not_really_markdown(content, &atls, &tag);
  1515. if (in_reply_to != NULL && *in_reply_to) {
  1516. xs *p_msg = NULL;
  1517. xs *wrk = NULL;
  1518. /* demand this thing */
  1519. timeline_request(snac, &in_reply_to, &wrk, 0);
  1520. if (valid_status(object_get(in_reply_to, &p_msg))) {
  1521. /* add this author as recipient */
  1522. const char *a, *v;
  1523. if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
  1524. to = xs_list_append(to, a);
  1525. /* add this author to the tag list as a mention */
  1526. if (!xs_is_null(a)) {
  1527. xs *l = xs_split(a, "/");
  1528. xs *actor_o = NULL;
  1529. if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
  1530. const char *uname = xs_dict_get(actor_o, "preferredUsername");
  1531. if (!xs_is_null(uname) && *uname) {
  1532. xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
  1533. xs *t = xs_dict_new();
  1534. t = xs_dict_append(t, "type", "Mention");
  1535. t = xs_dict_append(t, "href", a);
  1536. t = xs_dict_append(t, "name", handle);
  1537. tag = xs_list_append(tag, t);
  1538. }
  1539. }
  1540. }
  1541. /* get the context, if there is one */
  1542. if ((v = xs_dict_get(p_msg, "context")))
  1543. ctxt = xs_dup(v);
  1544. /* propagate the conversation field, if there is one */
  1545. if ((v = xs_dict_get(p_msg, "conversation")))
  1546. msg = xs_dict_append(msg, "conversation", v);
  1547. /* if this message is public or unlisted, ours will also be */
  1548. const int orig_scope = get_msg_visibility(p_msg);
  1549. if (!priv && orig_scope == SCOPE_PUBLIC && xs_list_in(to, public_address) == -1)
  1550. to = xs_list_append(to, public_address);
  1551. else
  1552. if (!priv && orig_scope == SCOPE_UNLISTED && xs_list_in(cc, public_address) == -1)
  1553. cc = xs_list_append(cc, public_address);
  1554. }
  1555. irt = xs_dup(in_reply_to);
  1556. }
  1557. else
  1558. irt = xs_val_new(XSTYPE_NULL);
  1559. /* extract the mentions and hashtags and convert the content */
  1560. fc1 = process_tags(snac, fc2, &tag);
  1561. /* create the attachment list, if there are any */
  1562. if (!xs_is_null(attach)) {
  1563. xs_list_foreach(attach, v) {
  1564. const char *url = xs_list_get(v, 0);
  1565. const char *alt = xs_list_get(v, 1);
  1566. const char *mime = xs_mime_by_ext(url);
  1567. int add = 1;
  1568. /* check if it's already here */
  1569. const xs_dict *ad;
  1570. xs_list_foreach(atls, ad) {
  1571. if (strcmp(xs_dict_get_def(ad, "url", ""), url) == 0) {
  1572. add = 0;
  1573. break;
  1574. }
  1575. }
  1576. if (add) {
  1577. xs *d = xs_dict_new();
  1578. d = xs_dict_append(d, "mediaType", mime);
  1579. d = xs_dict_append(d, "url", url);
  1580. d = xs_dict_append(d, "name", alt);
  1581. d = xs_dict_append(d, "type",
  1582. xs_startswith(mime, "image/") ? "Image" : "Document");
  1583. atls = xs_list_append(atls, d);
  1584. }
  1585. }
  1586. }
  1587. if (ctxt == NULL)
  1588. ctxt = xs_fmt("%s#ctxt", id);
  1589. /* add all mentions to the appropriate field */
  1590. p = tag;
  1591. while (xs_list_iter(&p, &v)) {
  1592. if (xs_type(v) == XSTYPE_DICT) {
  1593. const char *t;
  1594. if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
  1595. if (!xs_is_null(t = xs_dict_get(v, "href"))) {
  1596. if (scope == SCOPE_MENTIONED) {
  1597. /* for DMs, mentions go to 'to' */
  1598. to = xs_list_append(to, t);
  1599. } else {
  1600. /* for other visibility levels, mentions go to 'cc' */
  1601. cc = xs_list_append(cc, t);
  1602. }
  1603. }
  1604. }
  1605. }
  1606. }
  1607. if (scope == SCOPE_UNLISTED) {
  1608. /* Mastodon's "quiet public": remove from to, add public address to cc */
  1609. int idx;
  1610. if ((idx = xs_list_in(to, public_address)) != -1)
  1611. to = xs_list_del(to, idx);
  1612. if (xs_list_in(cc, public_address) == -1)
  1613. cc = xs_list_append(cc, public_address);
  1614. }
  1615. else
  1616. if (scope == SCOPE_FOLLOWERS) {
  1617. /* followers-only: add followers collection to to */
  1618. xs *followers_url = xs_fmt("%s/followers", snac->actor);
  1619. if (xs_list_in(to, followers_url) == -1)
  1620. to = xs_list_append(to, followers_url);
  1621. }
  1622. else
  1623. if (scope == SCOPE_PUBLIC) {
  1624. /* public: ensure public address is in to and not in cc */
  1625. int idx;
  1626. if ((idx = xs_list_in(cc, public_address)) != -1)
  1627. cc = xs_list_del(cc, idx);
  1628. if (xs_list_in(to, public_address) == -1)
  1629. to = xs_list_append(to, public_address);
  1630. }
  1631. /* delete all cc recipients that also are in the to */
  1632. p = to;
  1633. while (xs_list_iter(&p, &v)) {
  1634. int i;
  1635. if ((i = xs_list_in(cc, v)) != -1)
  1636. cc = xs_list_del(cc, i);
  1637. }
  1638. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  1639. msg = xs_dict_append(msg, "summary", "");
  1640. msg = xs_dict_append(msg, "content", fc1);
  1641. msg = xs_dict_append(msg, "context", ctxt);
  1642. msg = xs_dict_append(msg, "url", id);
  1643. msg = xs_dict_append(msg, "to", to);
  1644. msg = xs_dict_append(msg, "cc", cc);
  1645. msg = xs_dict_append(msg, "inReplyTo", irt);
  1646. msg = xs_dict_append(msg, "tag", tag);
  1647. msg = xs_dict_append(msg, "sourceContent", content);
  1648. if (xs_list_len(atls))
  1649. msg = xs_dict_append(msg, "attachment", atls);
  1650. /* set language content map */
  1651. if (xs_type(lang_str) == XSTYPE_STRING) {
  1652. /* split at the first _ */
  1653. xs *l0 = xs_split(lang_str, "_");
  1654. const char *lang = xs_list_get(l0, 0);
  1655. if (xs_type(lang) == XSTYPE_STRING && strlen(lang) == 2) {
  1656. /* a valid ISO language id */
  1657. xs *cmap = xs_dict_new();
  1658. cmap = xs_dict_set(cmap, lang, xs_dict_get(msg, "content"));
  1659. msg = xs_dict_set(msg, "contentMap", cmap);
  1660. }
  1661. }
  1662. if (!priv) {
  1663. /* create the replies object */
  1664. xs *replies = xs_dict_new();
  1665. xs *r_id = xs_replace(id, "/p/", "/r/");
  1666. xs *h_id = xs_fmt("%s", r_id);
  1667. xs *rp = msg_replies(snac, id, 0);
  1668. replies = xs_dict_set(replies, "id", h_id);
  1669. replies = xs_dict_set(replies, "type", "Collection");
  1670. replies = xs_dict_set(replies, "first", rp);
  1671. msg = xs_dict_set(msg, "replies", replies);
  1672. }
  1673. return msg;
  1674. }
  1675. xs_dict *msg_ping(snac *user, const char *rcpt)
  1676. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1677. {
  1678. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  1679. msg = xs_dict_append(msg, "to", rcpt);
  1680. return msg;
  1681. }
  1682. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  1683. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1684. {
  1685. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  1686. msg = xs_dict_append(msg, "to", rcpt);
  1687. return msg;
  1688. }
  1689. xs_dict *msg_move(snac *user, const char *new_account)
  1690. /* creates a Move message (to move the user to new_account) */
  1691. {
  1692. xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
  1693. msg = xs_dict_append(msg, "target", new_account);
  1694. return msg;
  1695. }
  1696. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  1697. const xs_list *opts, int multiple, int end_secs)
  1698. /* creates a Question message */
  1699. {
  1700. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0, NULL, NULL);
  1701. int max = 8;
  1702. xs_set seen;
  1703. msg = xs_dict_set(msg, "type", "Question");
  1704. /* make it non-editable */
  1705. msg = xs_dict_del(msg, "sourceContent");
  1706. xs *o = xs_list_new();
  1707. xs_list *p = (xs_list *)opts;
  1708. const xs_str *v;
  1709. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  1710. xs_set_init(&seen);
  1711. while (max && xs_list_iter(&p, &v)) {
  1712. if (*v) {
  1713. xs *v2 = xs_dup(v);
  1714. xs *d = xs_dict_new();
  1715. if (strlen(v2) > 60) {
  1716. v2[60] = '\0';
  1717. v2 = xs_str_cat(v2, "...");
  1718. }
  1719. if (xs_set_add(&seen, v2) == 1) {
  1720. d = xs_dict_append(d, "type", "Note");
  1721. d = xs_dict_append(d, "name", v2);
  1722. d = xs_dict_append(d, "replies", replies);
  1723. o = xs_list_append(o, d);
  1724. max--;
  1725. }
  1726. }
  1727. }
  1728. xs_set_free(&seen);
  1729. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  1730. /* set the end time */
  1731. time_t t = time(NULL) + end_secs;
  1732. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  1733. msg = xs_dict_append(msg, "endTime", et);
  1734. return msg;
  1735. }
  1736. int get_msg_visibility(const xs_dict *msg)
  1737. /* determine visibility from message based on CC, TO and /followers mark */
  1738. {
  1739. const xs_val *to = xs_dict_get(msg, "to");
  1740. const xs_val *cc = xs_dict_get(msg, "cc");
  1741. /* check if it's unlisted (public in cc but not in to) */
  1742. int pub_in_to = 0;
  1743. int pub_in_cc = 0;
  1744. if ((xs_type(to) == XSTYPE_STRING && strcmp(to, public_address) == 0) ||
  1745. (xs_type(to) == XSTYPE_LIST && xs_list_in(to, public_address) != -1))
  1746. pub_in_to = 1;
  1747. if ((xs_type(cc) == XSTYPE_STRING && strcmp(cc, public_address) == 0) ||
  1748. (xs_type(cc) == XSTYPE_LIST && xs_list_in(cc, public_address) != -1))
  1749. pub_in_cc = 1;
  1750. if (!pub_in_to && pub_in_cc) {
  1751. return SCOPE_UNLISTED;
  1752. }
  1753. if (pub_in_to && !pub_in_cc) {
  1754. return SCOPE_PUBLIC;
  1755. }
  1756. xs *followers_url = xs_fmt("%s/followers", xs_dict_get(msg, "attributedTo"));
  1757. if ((xs_type(to) == XSTYPE_STRING && strcmp(to, followers_url) == 0) ||
  1758. (xs_type(to) == XSTYPE_LIST && xs_list_in(to, followers_url) != -1))
  1759. return SCOPE_FOLLOWERS;
  1760. else
  1761. return SCOPE_MENTIONED;
  1762. /* should be unreachable, someone violated the protocol. */
  1763. /* better treat it as followers-only to make sure we don't leak information.*/
  1764. return SCOPE_FOLLOWERS;
  1765. }
  1766. int update_question(snac *user, const char *id)
  1767. /* updates the poll counts */
  1768. {
  1769. xs *msg = NULL;
  1770. xs *rcnt = xs_dict_new();
  1771. xs *lopts = xs_list_new();
  1772. const xs_list *opts;
  1773. xs_list *p;
  1774. const xs_val *v;
  1775. /* get the object */
  1776. if (!valid_status(object_get(id, &msg)))
  1777. return -1;
  1778. /* closed? do nothing more */
  1779. if (xs_dict_get(msg, "closed"))
  1780. return -2;
  1781. /* get the options */
  1782. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  1783. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  1784. return -3;
  1785. /* fill the initial count */
  1786. int c = 0;
  1787. while (xs_list_next(opts, &v, &c)) {
  1788. const char *name = xs_dict_get(v, "name");
  1789. if (name) {
  1790. lopts = xs_list_append(lopts, name);
  1791. rcnt = xs_dict_set(rcnt, name, xs_stock(0));
  1792. }
  1793. }
  1794. xs_set s;
  1795. xs_set_init(&s);
  1796. /* iterate now the children (the votes) */
  1797. xs *chld = object_children(id);
  1798. p = chld;
  1799. while (xs_list_iter(&p, &v)) {
  1800. xs *obj = NULL;
  1801. if (!valid_status(object_get_by_md5(v, &obj)))
  1802. continue;
  1803. const char *name = xs_dict_get(obj, "name");
  1804. const char *atto = get_atto(obj);
  1805. if (name && atto) {
  1806. /* get the current count */
  1807. const xs_number *cnt = xs_dict_get(rcnt, name);
  1808. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1809. /* if it exists, increment */
  1810. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  1811. rcnt = xs_dict_set(rcnt, name, ucnt);
  1812. xs_set_add(&s, atto);
  1813. }
  1814. }
  1815. }
  1816. xs *rcpts = xs_set_result(&s);
  1817. /* create a new list of options with their new counts */
  1818. xs *nopts = xs_list_new();
  1819. p = lopts;
  1820. while (xs_list_iter(&p, &v)) {
  1821. const xs_number *cnt = xs_dict_get(rcnt, v);
  1822. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1823. xs *d1 = xs_dict_new();
  1824. xs *d2 = xs_dict_new();
  1825. d2 = xs_dict_append(d2, "type", "Collection");
  1826. d2 = xs_dict_append(d2, "totalItems", cnt);
  1827. d1 = xs_dict_append(d1, "type", "Note");
  1828. d1 = xs_dict_append(d1, "name", v);
  1829. d1 = xs_dict_append(d1, "replies", d2);
  1830. nopts = xs_list_append(nopts, d1);
  1831. }
  1832. }
  1833. /* update the list */
  1834. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  1835. /* due date? */
  1836. int closed = 0;
  1837. const char *end_time = xs_dict_get(msg, "endTime");
  1838. if (!xs_is_null(end_time)) {
  1839. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  1840. /* is now greater than the endTime? */
  1841. if (strcmp(now, end_time) >= 0) {
  1842. xs *et = xs_dup(end_time);
  1843. msg = xs_dict_set(msg, "closed", et);
  1844. closed = 1;
  1845. }
  1846. }
  1847. /* update the count of voters */
  1848. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  1849. msg = xs_dict_set(msg, "votersCount", vcnt);
  1850. msg = xs_dict_set(msg, "cc", rcpts);
  1851. /* store */
  1852. object_add_ow(id, msg);
  1853. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  1854. timeline_touch(user);
  1855. /* send an update message to all voters */
  1856. xs *u_msg = msg_update(user, msg);
  1857. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  1858. enqueue_message(user, u_msg);
  1859. if (closed) {
  1860. xs *c_msg = msg_update(user, msg);
  1861. notify(user, "Update", "Question", user->actor, c_msg);
  1862. }
  1863. return 0;
  1864. }
  1865. /** queues **/
  1866. int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
  1867. /* processes an ActivityPub message from the input queue */
  1868. /* return values: -1, fatal error; 0, transient error, retry;
  1869. 1, processed and done; 2, propagate to users (only when no user is set) */
  1870. {
  1871. const char *actor = xs_dict_get(msg, "actor");
  1872. const char *type = xs_dict_get(msg, "type");
  1873. xs *actor_o = NULL;
  1874. int a_status;
  1875. int do_notify = 0;
  1876. if (xs_is_null(actor) || *actor == '\0') {
  1877. srv_debug(0, xs_fmt("malformed message (bad actor)"));
  1878. return -1;
  1879. }
  1880. /* this instance is alive */
  1881. instance_failure(actor, 2);
  1882. /* question votes may not have a type */
  1883. if (xs_is_null(type))
  1884. type = "Note";
  1885. /* reject uninteresting messages right now */
  1886. if (xs_match(type, "Add|View|Reject|Read|Remove")) {
  1887. srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
  1888. /* archive the ignored activity */
  1889. xs *ntid = tid(0);
  1890. xs *fn = xs_fmt("%s/ignored/%s.json", srv_basedir, ntid);
  1891. FILE *f;
  1892. if ((f = fopen(fn, "w")) != NULL) {
  1893. xs_json_dump(msg, 4, f);
  1894. fclose(f);
  1895. }
  1896. return -1;
  1897. }
  1898. const char *object, *utype;
  1899. object = xs_dict_get(msg, "object");
  1900. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1901. utype = xs_dict_get(object, "type");
  1902. else
  1903. utype = "(null)";
  1904. /* special case for Delete messages */
  1905. if (strcmp(type, "Delete") == 0) {
  1906. /* if the actor is not here, do not even try */
  1907. if (!object_here(actor)) {
  1908. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
  1909. return -1;
  1910. }
  1911. /* discard crap */
  1912. if (xs_is_null(object)) {
  1913. srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
  1914. return -1;
  1915. }
  1916. /* also discard if the object to be deleted is not here */
  1917. const char *obj_id = object;
  1918. if (xs_type(obj_id) == XSTYPE_DICT)
  1919. obj_id = xs_dict_get(obj_id, "id");
  1920. if (xs_is_null(obj_id) || !object_here(obj_id)) {
  1921. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
  1922. return -1;
  1923. }
  1924. }
  1925. /* bring the actor */
  1926. a_status = actor_request(snac, actor, &actor_o);
  1927. /* do not retry permanent failures */
  1928. if (a_status == HTTP_STATUS_NOT_FOUND
  1929. || a_status == HTTP_STATUS_GONE
  1930. || a_status < 0) {
  1931. srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1932. return -1;
  1933. }
  1934. if (!valid_status(a_status)) {
  1935. /* do not retry 'Delete' messages */
  1936. if (strcmp(type, "Delete") == 0) {
  1937. srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
  1938. return -1;
  1939. }
  1940. /* other actor download errors */
  1941. /* the actor may require a signed request; propagate if no user is set */
  1942. if (snac == NULL)
  1943. return 2;
  1944. /* may need a retry */
  1945. srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1946. return 0;
  1947. }
  1948. /* check the signature */
  1949. xs *sig_err = NULL;
  1950. if (!check_signature(req, &sig_err)) {
  1951. srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
  1952. srv_archive_error("check_signature", sig_err, req, msg);
  1953. return -1;
  1954. }
  1955. /* if no user is set, no further checks can be done; propagate */
  1956. if (snac == NULL)
  1957. return 2;
  1958. /* reject messages that are not for this user */
  1959. if (!is_msg_for_me(snac, msg)) {
  1960. snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1961. return 1;
  1962. }
  1963. /* if it's a DM from someone we don't follow, reject the message */
  1964. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1965. if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
  1966. !following_check(snac, actor)) {
  1967. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1968. return 1;
  1969. }
  1970. }
  1971. /* check the minimum acceptable account age */
  1972. int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
  1973. if (min_account_age > 0) {
  1974. const char *actor_date = xs_dict_get(actor_o, "published");
  1975. if (!xs_is_null(actor_date)) {
  1976. time_t actor_t = xs_parse_iso_date(actor_date, 0);
  1977. if (actor_t < 950000000) {
  1978. snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
  1979. actor, actor_date));
  1980. return 1;
  1981. }
  1982. if (actor_t > 0) {
  1983. int td = (int)(time(NULL) - actor_t);
  1984. snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
  1985. if (td < min_account_age) {
  1986. snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
  1987. actor, td));
  1988. return 1;
  1989. }
  1990. }
  1991. }
  1992. else
  1993. snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
  1994. }
  1995. if (strcmp(type, "Follow") == 0) { /** **/
  1996. const char *id = xs_dict_get(msg, "id");
  1997. if (xs_is_null(id)) {
  1998. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1999. }
  2000. else
  2001. if (!follower_check(snac, actor)) {
  2002. /* ensure the actor object is here */
  2003. if (!object_here(actor)) {
  2004. xs *actor_obj = NULL;
  2005. actor_request(snac, actor, &actor_obj);
  2006. object_add(actor, actor_obj);
  2007. }
  2008. if (xs_is_true(xs_dict_get(snac->config, "approve_followers"))) {
  2009. pending_add(snac, actor, msg);
  2010. snac_log(snac, xs_fmt("new pending follower approval %s", actor));
  2011. }
  2012. else {
  2013. /* automatic following */
  2014. xs *f_msg = xs_dup(msg);
  2015. xs *reply = msg_accept(snac, f_msg, actor);
  2016. post_message(snac, actor, reply);
  2017. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  2018. /* add a date if it doesn't include one (Mastodon) */
  2019. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2020. f_msg = xs_dict_set(f_msg, "published", date);
  2021. }
  2022. timeline_add(snac, id, f_msg);
  2023. follower_add(snac, actor);
  2024. snac_log(snac, xs_fmt("new follower %s", actor));
  2025. }
  2026. do_notify = 1;
  2027. }
  2028. else
  2029. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  2030. }
  2031. else
  2032. if (strcmp(type, "Undo") == 0) { /** **/
  2033. const char *id = xs_dict_get(object, "object");
  2034. if (xs_type(object) != XSTYPE_DICT) {
  2035. snac_debug(snac, 1, xs_fmt("undo: overriding utype %s | %s | %s",
  2036. utype, id, actor));
  2037. utype = "Follow";
  2038. }
  2039. if (strcmp(utype, "Follow") == 0) { /** **/
  2040. if (!id) {
  2041. snac_log(snac, xs_fmt("no id (msg.object.object) when "
  2042. "handling follow: %s", actor));
  2043. return 1;
  2044. }
  2045. if (strcmp(id, snac->actor) != 0)
  2046. snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
  2047. else
  2048. if (valid_status(follower_del(snac, actor))) {
  2049. snac_log(snac, xs_fmt("no longer following us %s", actor));
  2050. do_notify = 1;
  2051. }
  2052. else
  2053. if (pending_check(snac, actor)) {
  2054. pending_del(snac, actor);
  2055. snac_log(snac, xs_fmt("cancelled pending follow from %s", actor));
  2056. }
  2057. else
  2058. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  2059. }
  2060. else
  2061. if (strcmp(utype, "Like") == 0 || strcmp(utype, "EmojiReact") == 0) { /** **/
  2062. int status = object_unadmire(id, actor, 1);
  2063. snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
  2064. }
  2065. else
  2066. if (strcmp(utype, "Announce") == 0) { /** **/
  2067. int status = HTTP_STATUS_OK;
  2068. /* commented out: if a followed user boosts something that
  2069. is requested and then unboosts, the post remains here,
  2070. but with no apparent reason, and that is confusing */
  2071. //status = object_unadmire(id, actor, 0);
  2072. snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
  2073. }
  2074. else
  2075. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  2076. }
  2077. else
  2078. if (strcmp(type, "Create") == 0) { /** **/
  2079. if (is_muted(snac, actor)) {
  2080. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  2081. return 1;
  2082. }
  2083. if (xs_match(utype, "Note|Article")) { /** **/
  2084. const char *id = xs_dict_get(object, "id");
  2085. const char *in_reply_to = get_in_reply_to(object);
  2086. const char *atto = get_atto(object);
  2087. xs *wrk = NULL;
  2088. if (xs_is_null(id))
  2089. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2090. else
  2091. if (xs_is_null(atto))
  2092. snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
  2093. else
  2094. if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
  2095. snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
  2096. }
  2097. else {
  2098. if (content_match("filter_reject.txt", object)) {
  2099. snac_log(snac, xs_fmt("rejected by content %s", id));
  2100. return 1;
  2101. }
  2102. if (strcmp(actor, atto) != 0)
  2103. snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
  2104. timeline_request(snac, &in_reply_to, &wrk, 0);
  2105. const char *quoted_id = xs_or(xs_dict_get(object, "quoteUri"), xs_dict_get(object, "quoteUrl"));
  2106. if (xs_is_string(quoted_id) && xs_match(quoted_id, "https://*|http://*")) { /** **/
  2107. xs *quoted_post = NULL;
  2108. int status;
  2109. if (valid_status(status = activitypub_request(snac, quoted_id, &quoted_post))) {
  2110. /* got quoted post */
  2111. object_add(quoted_id, quoted_post);
  2112. }
  2113. snac_debug(snac, 1, xs_fmt("retrieving quoted post %s %d", quoted_id, status));
  2114. }
  2115. if (timeline_add(snac, id, object)) {
  2116. snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
  2117. do_notify = 1;
  2118. }
  2119. /* if it has a "name" field, it may be a vote for a question */
  2120. const char *name = xs_dict_get(object, "name");
  2121. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  2122. update_question(snac, in_reply_to);
  2123. }
  2124. }
  2125. else
  2126. if (strcmp(utype, "Question") == 0) { /** **/
  2127. const char *id = xs_dict_get(object, "id");
  2128. if (xs_is_null(id))
  2129. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2130. else
  2131. if (timeline_add(snac, id, object))
  2132. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  2133. }
  2134. else
  2135. if (xs_match(utype, "Audio|Video|Event")) { /** **/
  2136. const char *id = xs_dict_get(object, "id");
  2137. if (xs_is_null(id))
  2138. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2139. else
  2140. if (timeline_add(snac, id, object))
  2141. snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
  2142. }
  2143. else
  2144. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  2145. }
  2146. else
  2147. if (strcmp(type, "Accept") == 0) { /** **/
  2148. if (strcmp(utype, "(null)") == 0) {
  2149. const char *obj_id = xs_dict_get(msg, "object");
  2150. /* if the accepted object id is a string that may
  2151. be created by us, it's a follow */
  2152. if (xs_type(obj_id) == XSTYPE_STRING &&
  2153. xs_startswith(obj_id, srv_baseurl) &&
  2154. xs_endswith(obj_id, "/Follow"))
  2155. utype = "Follow";
  2156. }
  2157. if (strcmp(utype, "Follow") == 0) { /** **/
  2158. if (following_check(snac, actor)) {
  2159. following_add(snac, actor, msg);
  2160. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  2161. /* request a bit of this fellow's outbox */
  2162. enqueue_collect_outbox(snac, actor);
  2163. }
  2164. else
  2165. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  2166. }
  2167. else
  2168. if (strcmp(utype, "Create") == 0) {
  2169. /* some implementations send Create confirmations, go figure */
  2170. snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
  2171. }
  2172. else {
  2173. srv_archive_error("accept", "ignored Accept", req, msg);
  2174. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  2175. }
  2176. }
  2177. else
  2178. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
  2179. if (xs_type(object) == XSTYPE_DICT)
  2180. object = xs_dict_get(object, "id");
  2181. if (xs_is_null(object))
  2182. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2183. else
  2184. if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
  2185. snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
  2186. else
  2187. snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
  2188. do_notify = 1;
  2189. }
  2190. else
  2191. if (strcmp(type, "Announce") == 0) { /** **/
  2192. if (xs_type(object) == XSTYPE_DICT)
  2193. object = xs_dict_get(object, "id");
  2194. if (xs_is_null(object))
  2195. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2196. else
  2197. if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
  2198. snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
  2199. else
  2200. if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
  2201. snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
  2202. else {
  2203. xs *a_msg = NULL;
  2204. xs *wrk = NULL;
  2205. timeline_request(snac, &object, &wrk, 0);
  2206. if (valid_status(object_get(object, &a_msg))) {
  2207. const char *who = get_atto(a_msg);
  2208. /* got the admired object: instance is [back] online */
  2209. instance_failure(object, 2);
  2210. if (who && !is_muted(snac, who)) {
  2211. /* bring the actor */
  2212. xs *who_o = NULL;
  2213. if (valid_status(actor_request(snac, who, &who_o))) {
  2214. /* don't account as such announces by our own relay */
  2215. xs *this_relay = xs_fmt("%s/relay", srv_baseurl);
  2216. if (strcmp(actor, this_relay) != 0) {
  2217. if (valid_status(timeline_admire(snac, object, actor, 0)))
  2218. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  2219. else
  2220. snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
  2221. actor, object));
  2222. }
  2223. /* distribute the post with the actor as 'proxy' */
  2224. list_distribute(snac, actor, a_msg);
  2225. /* distribute the post to users following these hashtags */
  2226. followed_hashtag_distribute(a_msg);
  2227. do_notify = 1;
  2228. }
  2229. else
  2230. snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
  2231. }
  2232. else
  2233. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  2234. }
  2235. else
  2236. snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
  2237. }
  2238. }
  2239. else
  2240. if (strcmp(type, "Update") == 0) { /** **/
  2241. if (xs_match(utype, "Person|Service|Application")) { /** **/
  2242. actor_add(actor, xs_dict_get(msg, "object"));
  2243. timeline_touch(snac);
  2244. snac_log(snac, xs_fmt("updated actor %s", actor));
  2245. }
  2246. else
  2247. if (xs_match(utype, "Note|Page|Article|Video|Audio|Event")) { /** **/
  2248. const char *id = xs_dict_get(object, "id");
  2249. if (xs_is_null(id))
  2250. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2251. else
  2252. if (object_here(id)) {
  2253. if (xs_startswith(id, srv_baseurl) && !xs_startswith(id, actor))
  2254. snac_log(snac, xs_fmt("ignored incorrect 'Update' %s %s", actor, id));
  2255. else {
  2256. object_add_ow(id, object);
  2257. timeline_touch(snac);
  2258. snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
  2259. }
  2260. }
  2261. else
  2262. snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
  2263. }
  2264. else
  2265. if (strcmp(utype, "Question") == 0) { /** **/
  2266. const char *id = xs_dict_get(object, "id");
  2267. const char *closed = xs_dict_get(object, "closed");
  2268. if (xs_is_null(id))
  2269. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2270. else {
  2271. object_add_ow(id, object);
  2272. timeline_touch(snac);
  2273. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  2274. if (closed != NULL)
  2275. do_notify = 1;
  2276. }
  2277. }
  2278. else {
  2279. srv_archive_error("unsupported_update", "unsupported_update", req, msg);
  2280. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  2281. }
  2282. }
  2283. else
  2284. if (strcmp(type, "Delete") == 0) { /** **/
  2285. if (xs_type(object) == XSTYPE_DICT)
  2286. object = xs_dict_get(object, "id");
  2287. if (xs_is_null(object))
  2288. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  2289. else
  2290. if (object_here(object)) {
  2291. if (xs_startswith(object, srv_baseurl) && !xs_startswith(object, actor))
  2292. snac_log(snac, xs_fmt("ignored incorrect 'Delete' %s %s", actor, object));
  2293. else {
  2294. timeline_del(snac, object);
  2295. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  2296. }
  2297. }
  2298. else
  2299. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  2300. }
  2301. else
  2302. if (strcmp(type, "Pong") == 0) { /** **/
  2303. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  2304. }
  2305. else
  2306. if (strcmp(type, "Ping") == 0) { /** **/
  2307. const char *id = xs_dict_get(msg, "id");
  2308. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  2309. if (!xs_is_null(id)) {
  2310. xs *rsp = msg_pong(snac, actor, id);
  2311. enqueue_output_by_actor(snac, rsp, actor, 0);
  2312. }
  2313. }
  2314. else
  2315. if (strcmp(type, "Block") == 0) { /** **/
  2316. snac_debug(snac, 1, xs_fmt("'Block' received from %s", actor));
  2317. /* should we MUTE the actor back? */
  2318. /* mute(snac, actor); */
  2319. if (!xs_is_true(xs_dict_get(srv_config, "disable_block_notifications")))
  2320. do_notify = 1;
  2321. }
  2322. else
  2323. if (strcmp(type, "Move") == 0) { /** **/
  2324. do_notify = 1;
  2325. const char *old_account = xs_dict_get(msg, "object");
  2326. const char *new_account = xs_dict_get(msg, "target");
  2327. if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
  2328. if (following_check(snac, old_account)) {
  2329. xs *n_actor = NULL;
  2330. if (valid_status(object_get(new_account, &n_actor))) {
  2331. const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
  2332. if (xs_type(aka) == XSTYPE_LIST) {
  2333. if (xs_list_in(aka, old_account) != -1) {
  2334. /* all conditions met! */
  2335. /* follow new account */
  2336. xs *f_msg = msg_follow(snac, new_account);
  2337. if (f_msg != NULL) {
  2338. const char *new_actor = xs_dict_get(f_msg, "object");
  2339. following_add(snac, new_actor, f_msg);
  2340. enqueue_output_by_actor(snac, f_msg, new_actor, 0);
  2341. snac_log(snac, xs_fmt("'Move': following %s", new_account));
  2342. }
  2343. /* unfollow old account */
  2344. xs *of_msg = NULL;
  2345. if (valid_status(following_get(snac, old_account, &of_msg))) {
  2346. xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
  2347. following_del(snac, old_account);
  2348. enqueue_output_by_actor(snac, uf_msg, old_account, 0);
  2349. snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
  2350. }
  2351. }
  2352. else
  2353. snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
  2354. old_account, new_account));
  2355. }
  2356. else
  2357. snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
  2358. }
  2359. else {
  2360. snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
  2361. /* may be a server hiccup, retry later */
  2362. return 0;
  2363. }
  2364. }
  2365. else
  2366. snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
  2367. }
  2368. else {
  2369. snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
  2370. srv_archive_error("move", "move", req, msg);
  2371. }
  2372. }
  2373. else {
  2374. srv_archive_error("unsupported_type", "unsupported_type", req, msg);
  2375. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  2376. }
  2377. if (do_notify) {
  2378. notify(snac, type, utype, actor, msg);
  2379. timeline_touch(snac);
  2380. }
  2381. return 1;
  2382. }
  2383. int send_email(const xs_dict *mailinfo)
  2384. /* invoke curl */
  2385. {
  2386. const char *url = xs_dict_get(srv_config, "smtp_url");
  2387. if (!xs_is_string(url) || *url == '\0') {
  2388. /* revert back to old sendmail pipe behaviour */
  2389. const char *msg = xs_dict_get(mailinfo, "body");
  2390. FILE *f;
  2391. int status;
  2392. int fds[2];
  2393. pid_t pid;
  2394. if (pipe(fds) == -1) return -1;
  2395. pid = vfork();
  2396. if (pid == -1) return -1;
  2397. else if (pid == 0) {
  2398. dup2(fds[0], 0);
  2399. close(fds[0]);
  2400. close(fds[1]);
  2401. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  2402. _exit(1);
  2403. }
  2404. close(fds[0]);
  2405. if ((f = fdopen(fds[1], "w")) == NULL) {
  2406. close(fds[1]);
  2407. return -1;
  2408. }
  2409. fprintf(f, "%s\n", msg);
  2410. fclose(f);
  2411. if (waitpid(pid, &status, 0) == -1) return -1;
  2412. return status;
  2413. }
  2414. const char
  2415. *user = xs_dict_get(srv_config, "smtp_username"),
  2416. *pass = xs_dict_get(srv_config, "smtp_password"),
  2417. *from = xs_dict_get(mailinfo, "from"),
  2418. *to = xs_dict_get(mailinfo, "to"),
  2419. *body = xs_dict_get(mailinfo, "body");
  2420. if (url == NULL || *url == '\0')
  2421. url = "smtp://localhost";
  2422. int smtp_port = parse_port(url, NULL);
  2423. return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587);
  2424. }
  2425. void process_user_queue_item(snac *user, xs_dict *q_item)
  2426. /* processes an item from the user queue */
  2427. {
  2428. const char *type;
  2429. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  2430. if ((type = xs_dict_get(q_item, "type")) == NULL)
  2431. type = "output";
  2432. if (strcmp(type, "message") == 0) {
  2433. const xs_dict *msg = xs_dict_get(q_item, "message");
  2434. xs *rcpts = recipient_list(user, msg, 1);
  2435. xs_set inboxes;
  2436. const xs_str *actor;
  2437. xs_set_init(&inboxes);
  2438. /* add this shared inbox first */
  2439. xs *this_shared_inbox = xs_fmt("%s/shared-inbox", srv_baseurl);
  2440. xs_set_add(&inboxes, this_shared_inbox);
  2441. enqueue_output(user, msg, this_shared_inbox, 0, 0);
  2442. /* iterate the recipients */
  2443. xs_list_foreach(rcpts, actor) {
  2444. /* local users were served by this_shared_inbox */
  2445. if (!xs_startswith(actor, srv_baseurl)) {
  2446. xs *inbox = get_actor_inbox(actor, 1);
  2447. if (inbox != NULL) {
  2448. /* add to the set and, if it's not there, send message */
  2449. if (xs_set_add(&inboxes, inbox) == 1)
  2450. enqueue_output(user, msg, inbox, 0, 0);
  2451. }
  2452. else
  2453. snac_log(user, xs_fmt("cannot find inbox for %s", actor));
  2454. }
  2455. }
  2456. /* if it's a public note or question, send to the collected inboxes */
  2457. if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
  2458. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  2459. xs *shibx = inbox_list();
  2460. const xs_str *inbox;
  2461. xs_list_foreach(shibx, inbox) {
  2462. if (xs_set_add(&inboxes, inbox) == 1)
  2463. enqueue_output(user, msg, inbox, 0, 0);
  2464. }
  2465. }
  2466. }
  2467. xs_set_free(&inboxes);
  2468. /* relay this note */
  2469. if (is_msg_public(msg) && strcmp(user->uid, "relay") != 0) { /* avoid loops */
  2470. snac relay;
  2471. if (user_open(&relay, "relay")) {
  2472. /* a 'relay' user exists */
  2473. const char *type = xs_dict_get(msg, "type");
  2474. if (xs_is_string(type) && strcmp(type, "Create") == 0) {
  2475. const xs_val *object = xs_dict_get(msg, "object");
  2476. if (xs_is_dict(object)) {
  2477. object = xs_dict_get(object, "id");
  2478. snac_debug(&relay, 1, xs_fmt("relaying message %s", object));
  2479. xs *boost = msg_admiration(&relay, object, "Announce");
  2480. enqueue_message(&relay, boost);
  2481. }
  2482. }
  2483. user_free(&relay);
  2484. }
  2485. }
  2486. }
  2487. else
  2488. if (strcmp(type, "input") == 0) {
  2489. /* process the message */
  2490. const xs_dict *msg = xs_dict_get(q_item, "message");
  2491. const xs_dict *req = xs_dict_get(q_item, "req");
  2492. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2493. if (xs_is_null(msg))
  2494. return;
  2495. if (!process_input_message(user, msg, req)) {
  2496. if (retries > queue_retry_max)
  2497. snac_log(user, xs_fmt("input giving up"));
  2498. else {
  2499. /* reenqueue */
  2500. enqueue_input(user, msg, req, retries + 1);
  2501. snac_log(user, xs_fmt("input requeue #%d", retries + 1));
  2502. }
  2503. }
  2504. }
  2505. else
  2506. if (strcmp(type, "close_question") == 0) {
  2507. /* the time for this question has ended */
  2508. const char *id = xs_dict_get(q_item, "message");
  2509. if (!xs_is_null(id))
  2510. update_question(user, id);
  2511. }
  2512. else
  2513. if (strcmp(type, "object_request") == 0) {
  2514. const char *id = xs_dict_get(q_item, "message");
  2515. if (!xs_is_null(id)) {
  2516. int status;
  2517. xs *data = NULL;
  2518. status = activitypub_request(user, id, &data);
  2519. if (valid_status(status))
  2520. object_add_ow(id, data);
  2521. snac_debug(user, 1, xs_fmt("object_request %s %d", id, status));
  2522. }
  2523. }
  2524. else
  2525. if (strcmp(type, "verify_links") == 0) {
  2526. verify_links(user);
  2527. }
  2528. else
  2529. if (strcmp(type, "actor_refresh") == 0) {
  2530. const char *actor = xs_dict_get(q_item, "actor");
  2531. double mtime = object_mtime(actor);
  2532. if (actor_failure(actor, 0) == -1) {
  2533. /* actor is broken beyond repair */
  2534. snac_debug(user, 1, xs_fmt("actor_refresh skipped broken actor %s", actor));
  2535. }
  2536. else
  2537. /* only refresh if it was refreshed more than an hour ago */
  2538. if (mtime + 3600.0 < (double) time(NULL)) {
  2539. xs *actor_o = NULL;
  2540. int status;
  2541. if (valid_status((status = activitypub_request(user, actor, &actor_o)))) {
  2542. /* refresh collection counts with throttling to prevent resource exhaustion */
  2543. actor_o = actor_get_collections(user, actor_o, 1);
  2544. actor_add(actor, actor_o);
  2545. }
  2546. else {
  2547. if (status == HTTP_STATUS_GONE) {
  2548. actor_failure(actor, 1);
  2549. snac_log(user, xs_fmt("actor_refresh marking actor %s as broken %d", actor, status));
  2550. }
  2551. else {
  2552. actor_failure(actor, 2);
  2553. object_touch(actor);
  2554. }
  2555. }
  2556. snac_log(user, xs_fmt("actor_refresh %s %d", actor, status));
  2557. }
  2558. }
  2559. else
  2560. if (strcmp(type, "notify_webhook") == 0) {
  2561. const char *webhook = xs_dict_get(user->config, "notify_webhook");
  2562. if (xs_is_string(webhook) && xs_match(webhook, "https://*|http://*")) { /** **/
  2563. const xs_dict *msg = xs_dict_get(q_item, "message");
  2564. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2565. xs *hdrs = xs_dict_new();
  2566. hdrs = xs_dict_set(hdrs, "content-type", "application/json");
  2567. hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
  2568. xs *body = xs_json_dumps(msg, 4);
  2569. int status;
  2570. xs *rsp = xs_http_request("POST", webhook, hdrs, body, strlen(body), &status, NULL, NULL, 0);
  2571. snac_debug(user, 0, xs_fmt("webhook post %s %d", webhook, status));
  2572. if (!valid_status(status)) {
  2573. retries++;
  2574. if (retries > queue_retry_max)
  2575. snac_debug(user, 0, xs_fmt("webhook post giving up %s", webhook));
  2576. else {
  2577. snac_debug(user, 0, xs_fmt("webhook post requeue %s %d", webhook, retries));
  2578. enqueue_notify_webhook(user, msg, retries);
  2579. }
  2580. }
  2581. }
  2582. }
  2583. else
  2584. if (strcmp(type, "collect_replies") == 0) {
  2585. const char *post = xs_dict_get(q_item, "message");
  2586. collect_replies(user, post);
  2587. }
  2588. else
  2589. if (strcmp(type, "collect_outbox") == 0) {
  2590. const char *actor_id = xs_dict_get(q_item, "message");
  2591. collect_outbox(user, actor_id);
  2592. }
  2593. else
  2594. snac_log(user, xs_fmt("unexpected user q_item type '%s'", type));
  2595. }
  2596. int process_user_queue(snac *snac)
  2597. /* processes a user's queue */
  2598. {
  2599. int cnt = 0;
  2600. xs *list = user_queue(snac);
  2601. xs_list *p = list;
  2602. const xs_str *fn;
  2603. while (xs_list_iter(&p, &fn)) {
  2604. xs *q_item = dequeue(fn);
  2605. if (q_item == NULL)
  2606. continue;
  2607. process_user_queue_item(snac, q_item);
  2608. cnt++;
  2609. }
  2610. scheduled_process(snac);
  2611. return cnt;
  2612. }
  2613. xs_str *str_status(int status)
  2614. {
  2615. return xs_fmt("%d %s", status, status < 0 ? xs_curl_strerr(status) : xs_http_status_text(status));
  2616. }
  2617. void process_queue_item(xs_dict *q_item)
  2618. /* processes an item from the global queue */
  2619. {
  2620. const char *type = xs_dict_get(q_item, "type");
  2621. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  2622. if (strcmp(type, "output") == 0) {
  2623. int status;
  2624. const xs_str *inbox = xs_dict_get(q_item, "inbox");
  2625. const xs_str *keyid = xs_dict_get(q_item, "keyid");
  2626. const xs_str *seckey = xs_dict_get(q_item, "seckey");
  2627. const xs_dict *msg = xs_dict_get(q_item, "message");
  2628. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2629. int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
  2630. xs *payload = NULL;
  2631. int p_size = 0;
  2632. int timeout = 0;
  2633. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  2634. srv_log(xs_fmt("output message error: missing fields"));
  2635. return;
  2636. }
  2637. if (is_instance_blocked(inbox)) {
  2638. srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
  2639. return;
  2640. }
  2641. if (instance_failure(inbox, 0)) {
  2642. srv_debug(1, xs_fmt("output message error: too many failures for instance %s", inbox));
  2643. return;
  2644. }
  2645. /* deliver (if previous error status was a timeout, try now longer) */
  2646. if (p_status == 599)
  2647. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
  2648. else
  2649. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
  2650. if (timeout == 0)
  2651. timeout = 6;
  2652. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
  2653. /* register or clear a value for this instance */
  2654. instance_failure(inbox, valid_status(status) ? 2 : 1);
  2655. if (payload) {
  2656. if (p_size > 64) {
  2657. /* trim the message */
  2658. payload[64] = '\0';
  2659. payload = xs_str_cat(payload, "...");
  2660. }
  2661. /* strip ugly control characters */
  2662. payload = xs_replace_i(payload, "\n", "");
  2663. payload = xs_replace_i(payload, "\r", "");
  2664. if (*payload)
  2665. payload = xs_str_wrap_i(" [", payload, "]");
  2666. }
  2667. else
  2668. payload = xs_str_new(NULL);
  2669. xs *s_status = str_status(status);
  2670. srv_log(xs_fmt("output message: sent to inbox %s (%s)%s", inbox, s_status, payload));
  2671. if (!valid_status(status)) {
  2672. retries++;
  2673. /* if it's not the first time it fails with a timeout,
  2674. penalize the server by skipping one retry */
  2675. if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
  2676. retries++;
  2677. /* error sending; requeue? */
  2678. if (status == HTTP_STATUS_BAD_REQUEST
  2679. || status == HTTP_STATUS_NOT_FOUND
  2680. || status == HTTP_STATUS_METHOD_NOT_ALLOWED
  2681. || status == HTTP_STATUS_GONE
  2682. || status == HTTP_STATUS_UNPROCESSABLE_CONTENT
  2683. || status < 0)
  2684. /* explicit error: discard */
  2685. srv_log(xs_fmt("output message: error %s (%s)", inbox, s_status));
  2686. else
  2687. if (retries > queue_retry_max)
  2688. srv_log(xs_fmt("output message: giving up %s (%s)", inbox, s_status));
  2689. else {
  2690. /* requeue */
  2691. enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
  2692. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  2693. }
  2694. }
  2695. }
  2696. else
  2697. if (strcmp(type, "email") == 0) {
  2698. /* send this email */
  2699. const xs_dict *msg = xs_dict_get(q_item, "message");
  2700. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2701. if (!send_email(msg))
  2702. srv_debug(1, xs_fmt("email message sent"));
  2703. else {
  2704. retries++;
  2705. if (retries > queue_retry_max)
  2706. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  2707. else {
  2708. /* requeue */
  2709. srv_log(xs_fmt(
  2710. "email requeue #%d (errno: %d)", retries, errno));
  2711. enqueue_email(msg, retries);
  2712. }
  2713. }
  2714. }
  2715. else
  2716. if (strcmp(type, "telegram") == 0) {
  2717. /* send this via telegram */
  2718. const char *bot = xs_dict_get(q_item, "bot");
  2719. const char *msg = xs_dict_get(q_item, "message");
  2720. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  2721. int status = 0;
  2722. /* chat_id must start with a - */
  2723. if (!xs_startswith(chat_id, "-"))
  2724. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  2725. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  2726. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  2727. xs *headers = xs_dict_new();
  2728. headers = xs_dict_append(headers, "content-type", "application/json");
  2729. xs *rsp = xs_http_request("POST", url, headers,
  2730. body, strlen(body), &status, NULL, NULL, 0);
  2731. rsp = xs_free(rsp);
  2732. srv_debug(0, xs_fmt("telegram post %d", status));
  2733. }
  2734. else
  2735. if (strcmp(type, "ntfy") == 0) {
  2736. /* send this via ntfy */
  2737. const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
  2738. const char *msg = xs_dict_get(q_item, "message");
  2739. const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
  2740. int status = 0;
  2741. xs *url = xs_fmt("%s", ntfy_server);
  2742. xs *body = xs_fmt("%s", msg);
  2743. xs *headers = xs_dict_new();
  2744. headers = xs_dict_append(headers, "content-type", "text/plain");
  2745. // Append the Authorization header only if ntfy_token is not NULL
  2746. if (ntfy_token != NULL) {
  2747. headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
  2748. }
  2749. xs *rsp = xs_http_request("POST", url, headers,
  2750. body, strlen(body), &status, NULL, NULL, 0);
  2751. rsp = xs_free(rsp);
  2752. srv_debug(0, xs_fmt("ntfy post %d", status));
  2753. }
  2754. else
  2755. if (strcmp(type, "purge") == 0) {
  2756. srv_log(xs_dup("purge start"));
  2757. purge_all();
  2758. srv_log(xs_dup("purge end"));
  2759. }
  2760. else
  2761. if (strcmp(type, "input") == 0) {
  2762. const xs_dict *msg = xs_dict_get(q_item, "message");
  2763. const xs_dict *req = xs_dict_get(q_item, "req");
  2764. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2765. /* do some instance-level checks */
  2766. int r = process_input_message(NULL, msg, req);
  2767. if (r == 0) {
  2768. /* transient error? retry */
  2769. if (retries > queue_retry_max)
  2770. srv_log(xs_fmt("shared input giving up"));
  2771. else {
  2772. /* reenqueue */
  2773. enqueue_shared_input(msg, req, retries + 1);
  2774. srv_log(xs_fmt("shared input requeue #%d", retries + 1));
  2775. }
  2776. }
  2777. else
  2778. if (r == 2) {
  2779. /* redistribute the input message to all users */
  2780. const char *ntid = xs_dict_get(q_item, "ntid");
  2781. xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
  2782. FILE *f;
  2783. if ((f = fopen(tmpfn, "w")) != NULL) {
  2784. xs_json_dump(q_item, 4, f);
  2785. fclose(f);
  2786. }
  2787. xs *users = user_list();
  2788. xs_list *p = users;
  2789. const char *v;
  2790. int cnt = 0;
  2791. while (xs_list_iter(&p, &v)) {
  2792. snac user;
  2793. if (user_open(&user, v)) {
  2794. int rsn = is_msg_for_me(&user, msg);
  2795. if (rsn) {
  2796. xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
  2797. snac_debug(&user, 1,
  2798. xs_fmt("enqueue_input (from shared inbox) %s [%d]", xs_dict_get(msg, "id"), rsn));
  2799. if (link(tmpfn, fn) < 0)
  2800. srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
  2801. cnt++;
  2802. }
  2803. user_free(&user);
  2804. }
  2805. }
  2806. unlink(tmpfn);
  2807. if (cnt == 0) {
  2808. srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
  2809. }
  2810. }
  2811. }
  2812. else
  2813. if (strcmp(type, "webmention") == 0) {
  2814. const xs_dict *msg = xs_dict_get(q_item, "message");
  2815. const char *source = xs_dict_get(msg, "id");
  2816. const char *content = xs_dict_get(msg, "content");
  2817. if (xs_is_string(source) && xs_is_string(content)) {
  2818. xs *links = xs_regex_select(content, "\"https?:/" "/[^\"]+");
  2819. const char *link;
  2820. xs_list_foreach(links, link) {
  2821. xs *target = xs_strip_chars_i(xs_dup(link), "\"");
  2822. int r = xs_webmention_send(source, target, USER_AGENT);
  2823. srv_debug(1, xs_fmt("webmention source=%s target=%s %d", source, target, r));
  2824. }
  2825. }
  2826. }
  2827. else
  2828. if (strcmp(type, "rss_hashtag_poll") == 0) {
  2829. rss_poll_hashtags();
  2830. }
  2831. else
  2832. if (strcmp(type, "fsck") == 0) {
  2833. srv_log(xs_fmt("started deferred data integrity check"));
  2834. data_fsck();
  2835. srv_log(xs_fmt("finished deferred data integrity check"));
  2836. }
  2837. else
  2838. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  2839. }
  2840. int process_queue(void)
  2841. /* processes the global queue */
  2842. {
  2843. int cnt = 0;
  2844. xs *list = queue();
  2845. xs_list *p = list;
  2846. const xs_str *fn;
  2847. while (xs_list_iter(&p, &fn)) {
  2848. xs *q_item = dequeue(fn);
  2849. if (q_item != NULL) {
  2850. job_post(q_item, 0);
  2851. cnt++;
  2852. }
  2853. }
  2854. return cnt;
  2855. }
  2856. /** account migration **/
  2857. int migrate_account(snac *user)
  2858. /* migrates this account to a new one (stored in the 'alias' user field) */
  2859. {
  2860. const char *new_account = xs_dict_get(user->config, "alias");
  2861. if (xs_type(new_account) != XSTYPE_STRING) {
  2862. snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
  2863. return 1;
  2864. }
  2865. xs *new_actor = NULL;
  2866. int status;
  2867. if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
  2868. snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
  2869. return 1;
  2870. }
  2871. actor_add(new_account, new_actor);
  2872. const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
  2873. if (xs_type(loaka) != XSTYPE_LIST) {
  2874. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
  2875. return 1;
  2876. }
  2877. if (xs_list_in(loaka, user->actor) == -1) {
  2878. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
  2879. return 1;
  2880. }
  2881. xs *move = msg_move(user, new_account);
  2882. xs *fwers = follower_list(user);
  2883. const char *actor;
  2884. xs_list_foreach(fwers, actor) {
  2885. /* get the actor inbox, excluding the shared one */
  2886. xs *inbox = get_actor_inbox(actor, 0);
  2887. if (xs_is_null(inbox))
  2888. snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
  2889. else
  2890. enqueue_output(user, move, inbox, 0, 0);
  2891. }
  2892. return 0;
  2893. }
  2894. /** HTTP handlers */
  2895. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  2896. char **body, int *b_size, char **ctype)
  2897. {
  2898. int status = HTTP_STATUS_OK;
  2899. const char *accept = xs_dict_get(req, "accept");
  2900. snac snac;
  2901. xs *msg = NULL;
  2902. if (accept == NULL)
  2903. return 0;
  2904. if (xs_str_in(accept, "application/activity+json") == -1 &&
  2905. xs_str_in(accept, "application/ld+json") == -1)
  2906. return 0;
  2907. xs *l = xs_split_n(q_path, "/", 2);
  2908. const char *uid;
  2909. const char *p_path;
  2910. uid = xs_list_get(l, 1);
  2911. if (!user_open(&snac, uid)) {
  2912. /* invalid user */
  2913. status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
  2914. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s %d", uid, status));
  2915. return status;
  2916. }
  2917. p_path = xs_list_get(l, 2);
  2918. const xs_dict *q_vars = xs_dict_get(req, "q_vars");
  2919. *ctype = "application/activity+json";
  2920. int show_contact_metrics = xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"));
  2921. if (p_path == NULL) {
  2922. /* if there was no component after the user, it's an actor request */
  2923. msg = msg_actor(&snac);
  2924. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  2925. const char *ua = xs_dict_get(req, "user-agent");
  2926. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  2927. }
  2928. else
  2929. if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
  2930. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2931. xs *list = xs_list_new();
  2932. const char *v;
  2933. int cnt = xs_number_get(xs_dict_get_def(srv_config, "max_public_entries", "20"));
  2934. /* get the public outbox or the pinned list */
  2935. xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, cnt, NULL) : pinned_list(&snac);
  2936. xs_list_foreach(elems, v) {
  2937. xs *i = NULL;
  2938. if (valid_status(object_get_by_md5(v, &i))) {
  2939. const char *type = xs_dict_get(i, "type");
  2940. const char *id = xs_dict_get(i, "id");
  2941. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  2942. if (is_msg_public(i)) {
  2943. xs *c_msg = msg_create(&snac, i);
  2944. list = xs_list_append(list, c_msg);
  2945. }
  2946. }
  2947. }
  2948. }
  2949. /* replace the 'orderedItems' with the latest posts */
  2950. msg = msg_collection(&snac, id, xs_list_len(list));
  2951. msg = xs_dict_set(msg, "orderedItems", list);
  2952. }
  2953. else
  2954. if (strcmp(p_path, "followers") == 0) {
  2955. int total = 0;
  2956. if (show_contact_metrics) {
  2957. total = follower_list_len(&snac);
  2958. }
  2959. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2960. msg = msg_collection(&snac, id, total);
  2961. }
  2962. else
  2963. if (strcmp(p_path, "following") == 0) {
  2964. int total = 0;
  2965. if (show_contact_metrics) {
  2966. total = following_list_len(&snac);
  2967. }
  2968. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2969. msg = msg_collection(&snac, id, total);
  2970. }
  2971. else
  2972. if (xs_startswith(p_path, "p/")) {
  2973. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2974. status = object_get(id, &msg);
  2975. /* don't return non-public objects */
  2976. if (valid_status(status) && !is_msg_public(msg))
  2977. status = HTTP_STATUS_NOT_FOUND;
  2978. }
  2979. else
  2980. if (xs_startswith(p_path, "r/")) {
  2981. /* replies to a post */
  2982. xs *s = xs_dup(p_path);
  2983. s[0] = 'p';
  2984. xs *id = xs_fmt("%s/%s", snac.actor, s);
  2985. xs *obj = NULL;
  2986. status = object_get(id, &obj);
  2987. /* don't return non-public objects */
  2988. if (!valid_status(status))
  2989. status = HTTP_STATUS_NOT_FOUND;
  2990. else
  2991. if (!is_msg_public(obj))
  2992. status = HTTP_STATUS_NOT_FOUND;
  2993. else
  2994. if (xs_dict_get(q_vars, "page"))
  2995. msg = msg_replies(&snac, id, 1);
  2996. else {
  2997. const xs_dict *replies = xs_dict_get(obj, "replies");
  2998. if (xs_is_dict(replies)) {
  2999. msg = xs_dup(replies);
  3000. msg = xs_dict_set(msg, "@context", "https:/""/www.w3.org/ns/activitystreams");
  3001. }
  3002. }
  3003. }
  3004. else
  3005. status = HTTP_STATUS_NOT_FOUND;
  3006. if (status == HTTP_STATUS_OK && msg != NULL) {
  3007. *body = xs_json_dumps(msg, 4);
  3008. *b_size = strlen(*body);
  3009. }
  3010. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  3011. user_free(&snac);
  3012. return status;
  3013. }
  3014. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  3015. char *payload, int p_size,
  3016. char **body, int *b_size, char **ctype)
  3017. /* processes an input message */
  3018. {
  3019. (void)b_size;
  3020. int status = HTTP_STATUS_ACCEPTED;
  3021. const char *i_ctype = xs_dict_get(req, "content-type");
  3022. snac snac;
  3023. const char *v;
  3024. if (i_ctype == NULL) {
  3025. *body = xs_str_new("no content-type");
  3026. *ctype = "text/plain";
  3027. return HTTP_STATUS_BAD_REQUEST;
  3028. }
  3029. if (xs_is_null(payload)) {
  3030. *body = xs_str_new("no payload");
  3031. *ctype = "text/plain";
  3032. return HTTP_STATUS_BAD_REQUEST;
  3033. }
  3034. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  3035. xs_str_in(i_ctype, "application/ld+json") == -1)
  3036. return 0;
  3037. /* decode the message */
  3038. xs *msg = xs_json_loads(payload);
  3039. const char *id = xs_dict_get(msg, "id");
  3040. if (msg == NULL) {
  3041. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  3042. srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
  3043. *body = xs_str_new("JSON error");
  3044. *ctype = "text/plain";
  3045. return HTTP_STATUS_BAD_REQUEST;
  3046. }
  3047. if (id && is_instance_blocked(id)) {
  3048. srv_debug(1, xs_fmt("full instance block for %s", id));
  3049. *body = xs_str_new("blocked");
  3050. *ctype = "text/plain";
  3051. return HTTP_STATUS_FORBIDDEN;
  3052. }
  3053. /* get the user and path */
  3054. xs *l = xs_split_n(q_path, "/", 2);
  3055. if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
  3056. enqueue_shared_input(msg, req, 0);
  3057. return HTTP_STATUS_ACCEPTED;
  3058. }
  3059. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  3060. /* strange q_path */
  3061. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  3062. return HTTP_STATUS_NOT_FOUND;
  3063. }
  3064. const char *uid = xs_list_get(l, 1);
  3065. if (!user_open(&snac, uid)) {
  3066. /* invalid user */
  3067. status = grave(uid, 0) ? HTTP_STATUS_GONE : HTTP_STATUS_NOT_FOUND;
  3068. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s %d", uid, status));
  3069. return status;
  3070. }
  3071. /* if it has a digest, check it now, because
  3072. later the payload won't be exactly the same */
  3073. if ((v = xs_dict_get(req, "digest")) != NULL) {
  3074. xs *s1 = xs_sha256_base64(payload, p_size);
  3075. xs *s2 = xs_fmt("SHA-256=%s", s1);
  3076. if (strcmp(s2, v) != 0) {
  3077. srv_log(xs_fmt("digest check FAILED"));
  3078. *body = xs_str_new("bad digest");
  3079. *ctype = "text/plain";
  3080. status = HTTP_STATUS_BAD_REQUEST;
  3081. }
  3082. }
  3083. /* if the message is from a muted actor, reject it right now */
  3084. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  3085. if (is_muted(&snac, v)) {
  3086. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  3087. *body = xs_str_new("rejected");
  3088. *ctype = "text/plain";
  3089. status = HTTP_STATUS_FORBIDDEN;
  3090. }
  3091. }
  3092. if (valid_status(status)) {
  3093. enqueue_input(&snac, msg, req, 0);
  3094. *ctype = "application/activity+json";
  3095. }
  3096. user_free(&snac);
  3097. return status;
  3098. }