activitypub.c 89 KB

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