1
0

activitypub.c 121 KB

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