activitypub.c 102 KB

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