activitypub.c 94 KB

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