activitypub.c 100 KB

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