activitypub.c 118 KB

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