activitypub.c 105 KB

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