activitypub.c 91 KB

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