activitypub.c 110 KB

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