activitypub.c 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785
  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)
  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 ((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);
  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")) {
  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]+;|#[^ ,\\.:;<]+)");
  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 (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  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);
  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, NULL);
  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", 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. return msg;
  964. }
  965. xs_dict *msg_create(snac *snac, const xs_dict *object)
  966. /* creates a 'Create' message */
  967. {
  968. xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
  969. const xs_val *v;
  970. if ((v = get_atto(object)))
  971. msg = xs_dict_append(msg, "attributedTo", v);
  972. if ((v = xs_dict_get(object, "cc")))
  973. msg = xs_dict_append(msg, "cc", v);
  974. if ((v = xs_dict_get(object, "to")))
  975. msg = xs_dict_append(msg, "to", v);
  976. else
  977. msg = xs_dict_append(msg, "to", public_address);
  978. return msg;
  979. }
  980. xs_dict *msg_undo(snac *snac, const xs_val *object)
  981. /* creates an 'Undo' message */
  982. {
  983. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  984. const char *to;
  985. if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
  986. msg = xs_dict_append(msg, "to", to);
  987. return msg;
  988. }
  989. xs_dict *msg_delete(snac *snac, const char *id)
  990. /* creates a 'Delete' + 'Tombstone' for a local entry */
  991. {
  992. xs *tomb = xs_dict_new();
  993. xs_dict *msg = NULL;
  994. /* sculpt the tombstone */
  995. tomb = xs_dict_append(tomb, "type", "Tombstone");
  996. tomb = xs_dict_append(tomb, "id", id);
  997. /* now create the Delete */
  998. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  999. msg = xs_dict_append(msg, "to", public_address);
  1000. return msg;
  1001. }
  1002. xs_dict *msg_follow(snac *snac, const char *q)
  1003. /* creates a 'Follow' message */
  1004. {
  1005. xs *actor_o = NULL;
  1006. xs *actor = NULL;
  1007. xs_dict *msg = NULL;
  1008. int status;
  1009. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  1010. if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
  1011. actor = xs_dup(url_or_uid);
  1012. else
  1013. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  1014. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  1015. return NULL;
  1016. }
  1017. /* request the actor */
  1018. status = actor_request(snac, actor, &actor_o);
  1019. if (valid_status(status)) {
  1020. /* check if the actor is an alias */
  1021. const char *r_actor = xs_dict_get(actor_o, "id");
  1022. if (r_actor && strcmp(actor, r_actor) != 0) {
  1023. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  1024. }
  1025. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  1026. }
  1027. else
  1028. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  1029. return msg;
  1030. }
  1031. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  1032. const xs_str *in_reply_to, const xs_list *attach, int priv)
  1033. /* creates a 'Note' message */
  1034. {
  1035. xs *ntid = tid(0);
  1036. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  1037. xs *ctxt = NULL;
  1038. xs *fc2 = NULL;
  1039. xs *fc1 = NULL;
  1040. xs *to = NULL;
  1041. xs *cc = xs_list_new();
  1042. xs *irt = NULL;
  1043. xs *tag = xs_list_new();
  1044. xs *atls = xs_list_new();
  1045. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  1046. xs_list *p;
  1047. const xs_val *v;
  1048. if (rcpts == NULL)
  1049. to = xs_list_new();
  1050. else {
  1051. if (xs_type(rcpts) == XSTYPE_STRING) {
  1052. to = xs_list_new();
  1053. to = xs_list_append(to, rcpts);
  1054. }
  1055. else
  1056. to = xs_dup(rcpts);
  1057. }
  1058. /* format the content */
  1059. fc2 = not_really_markdown(content, &atls, &tag);
  1060. if (in_reply_to != NULL && *in_reply_to) {
  1061. xs *p_msg = NULL;
  1062. xs *wrk = NULL;
  1063. /* demand this thing */
  1064. timeline_request(snac, &in_reply_to, &wrk, 0);
  1065. if (valid_status(object_get(in_reply_to, &p_msg))) {
  1066. /* add this author as recipient */
  1067. const char *a, *v;
  1068. if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
  1069. to = xs_list_append(to, a);
  1070. /* add this author to the tag list as a mention */
  1071. if (!xs_is_null(a)) {
  1072. xs *l = xs_split(a, "/");
  1073. xs *actor_o = NULL;
  1074. if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
  1075. const char *uname = xs_dict_get(actor_o, "preferredUsername");
  1076. if (!xs_is_null(uname) && *uname) {
  1077. xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
  1078. xs *t = xs_dict_new();
  1079. t = xs_dict_append(t, "type", "Mention");
  1080. t = xs_dict_append(t, "href", a);
  1081. t = xs_dict_append(t, "name", handle);
  1082. tag = xs_list_append(tag, t);
  1083. }
  1084. }
  1085. }
  1086. /* get the context, if there is one */
  1087. if ((v = xs_dict_get(p_msg, "context")))
  1088. ctxt = xs_dup(v);
  1089. /* propagate the conversation field, if there is one */
  1090. if ((v = xs_dict_get(p_msg, "conversation")))
  1091. msg = xs_dict_append(msg, "conversation", v);
  1092. /* if this message is public, ours will also be */
  1093. if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
  1094. to = xs_list_append(to, public_address);
  1095. }
  1096. irt = xs_dup(in_reply_to);
  1097. }
  1098. else
  1099. irt = xs_val_new(XSTYPE_NULL);
  1100. /* extract the mentions and hashtags and convert the content */
  1101. fc1 = process_tags(snac, fc2, &tag);
  1102. /* create the attachment list, if there are any */
  1103. if (!xs_is_null(attach)) {
  1104. int c = 0;
  1105. while (xs_list_next(attach, &v, &c)) {
  1106. xs *d = xs_dict_new();
  1107. const char *url = xs_list_get(v, 0);
  1108. const char *alt = xs_list_get(v, 1);
  1109. const char *mime = xs_mime_by_ext(url);
  1110. d = xs_dict_append(d, "mediaType", mime);
  1111. d = xs_dict_append(d, "url", url);
  1112. d = xs_dict_append(d, "name", alt);
  1113. d = xs_dict_append(d, "type",
  1114. xs_startswith(mime, "image/") ? "Image" : "Document");
  1115. atls = xs_list_append(atls, d);
  1116. }
  1117. }
  1118. if (ctxt == NULL)
  1119. ctxt = xs_fmt("%s#ctxt", id);
  1120. /* add all mentions to the cc */
  1121. p = tag;
  1122. while (xs_list_iter(&p, &v)) {
  1123. if (xs_type(v) == XSTYPE_DICT) {
  1124. const char *t;
  1125. if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
  1126. if (!xs_is_null(t = xs_dict_get(v, "href")))
  1127. cc = xs_list_append(cc, t);
  1128. }
  1129. }
  1130. }
  1131. /* no recipients? must be for everybody */
  1132. if (!priv && xs_list_len(to) == 0)
  1133. to = xs_list_append(to, public_address);
  1134. /* delete all cc recipients that also are in the to */
  1135. p = to;
  1136. while (xs_list_iter(&p, &v)) {
  1137. int i;
  1138. if ((i = xs_list_in(cc, v)) != -1)
  1139. cc = xs_list_del(cc, i);
  1140. }
  1141. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  1142. msg = xs_dict_append(msg, "summary", "");
  1143. msg = xs_dict_append(msg, "content", fc1);
  1144. msg = xs_dict_append(msg, "context", ctxt);
  1145. msg = xs_dict_append(msg, "url", id);
  1146. msg = xs_dict_append(msg, "to", to);
  1147. msg = xs_dict_append(msg, "cc", cc);
  1148. msg = xs_dict_append(msg, "inReplyTo", irt);
  1149. msg = xs_dict_append(msg, "tag", tag);
  1150. msg = xs_dict_append(msg, "sourceContent", content);
  1151. if (xs_list_len(atls))
  1152. msg = xs_dict_append(msg, "attachment", atls);
  1153. return msg;
  1154. }
  1155. xs_dict *msg_ping(snac *user, const char *rcpt)
  1156. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1157. {
  1158. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  1159. msg = xs_dict_append(msg, "to", rcpt);
  1160. return msg;
  1161. }
  1162. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  1163. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1164. {
  1165. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  1166. msg = xs_dict_append(msg, "to", rcpt);
  1167. return msg;
  1168. }
  1169. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  1170. const xs_list *opts, int multiple, int end_secs)
  1171. /* creates a Question message */
  1172. {
  1173. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  1174. int max = 8;
  1175. xs_set seen;
  1176. msg = xs_dict_set(msg, "type", "Question");
  1177. /* make it non-editable */
  1178. msg = xs_dict_del(msg, "sourceContent");
  1179. xs *o = xs_list_new();
  1180. xs_list *p = (xs_list *)opts;
  1181. const xs_str *v;
  1182. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  1183. xs_set_init(&seen);
  1184. while (max && xs_list_iter(&p, &v)) {
  1185. if (*v) {
  1186. xs *v2 = xs_dup(v);
  1187. xs *d = xs_dict_new();
  1188. if (strlen(v2) > 60) {
  1189. v2[60] = '\0';
  1190. v2 = xs_str_cat(v2, "...");
  1191. }
  1192. if (xs_set_add(&seen, v2) == 1) {
  1193. d = xs_dict_append(d, "type", "Note");
  1194. d = xs_dict_append(d, "name", v2);
  1195. d = xs_dict_append(d, "replies", replies);
  1196. o = xs_list_append(o, d);
  1197. max--;
  1198. }
  1199. }
  1200. }
  1201. xs_set_free(&seen);
  1202. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  1203. /* set the end time */
  1204. time_t t = time(NULL) + end_secs;
  1205. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  1206. msg = xs_dict_append(msg, "endTime", et);
  1207. return msg;
  1208. }
  1209. int update_question(snac *user, const char *id)
  1210. /* updates the poll counts */
  1211. {
  1212. xs *msg = NULL;
  1213. xs *rcnt = xs_dict_new();
  1214. xs *lopts = xs_list_new();
  1215. const xs_list *opts;
  1216. xs_list *p;
  1217. const xs_val *v;
  1218. /* get the object */
  1219. if (!valid_status(object_get(id, &msg)))
  1220. return -1;
  1221. /* closed? do nothing more */
  1222. if (xs_dict_get(msg, "closed"))
  1223. return -2;
  1224. /* get the options */
  1225. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  1226. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  1227. return -3;
  1228. /* fill the initial count */
  1229. int c = 0;
  1230. while (xs_list_next(opts, &v, &c)) {
  1231. const char *name = xs_dict_get(v, "name");
  1232. if (name) {
  1233. lopts = xs_list_append(lopts, name);
  1234. rcnt = xs_dict_set(rcnt, name, xs_stock(0));
  1235. }
  1236. }
  1237. xs_set s;
  1238. xs_set_init(&s);
  1239. /* iterate now the children (the votes) */
  1240. xs *chld = object_children(id);
  1241. p = chld;
  1242. while (xs_list_iter(&p, &v)) {
  1243. xs *obj = NULL;
  1244. if (!valid_status(object_get_by_md5(v, &obj)))
  1245. continue;
  1246. const char *name = xs_dict_get(obj, "name");
  1247. const char *atto = get_atto(obj);
  1248. if (name && atto) {
  1249. /* get the current count */
  1250. const xs_number *cnt = xs_dict_get(rcnt, name);
  1251. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1252. /* if it exists, increment */
  1253. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  1254. rcnt = xs_dict_set(rcnt, name, ucnt);
  1255. xs_set_add(&s, atto);
  1256. }
  1257. }
  1258. }
  1259. xs *rcpts = xs_set_result(&s);
  1260. /* create a new list of options with their new counts */
  1261. xs *nopts = xs_list_new();
  1262. p = lopts;
  1263. while (xs_list_iter(&p, &v)) {
  1264. const xs_number *cnt = xs_dict_get(rcnt, v);
  1265. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1266. xs *d1 = xs_dict_new();
  1267. xs *d2 = xs_dict_new();
  1268. d2 = xs_dict_append(d2, "type", "Collection");
  1269. d2 = xs_dict_append(d2, "totalItems", cnt);
  1270. d1 = xs_dict_append(d1, "type", "Note");
  1271. d1 = xs_dict_append(d1, "name", v);
  1272. d1 = xs_dict_append(d1, "replies", d2);
  1273. nopts = xs_list_append(nopts, d1);
  1274. }
  1275. }
  1276. /* update the list */
  1277. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  1278. /* due date? */
  1279. int closed = 0;
  1280. const char *end_time = xs_dict_get(msg, "endTime");
  1281. if (!xs_is_null(end_time)) {
  1282. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  1283. /* is now greater than the endTime? */
  1284. if (strcmp(now, end_time) >= 0) {
  1285. xs *et = xs_dup(end_time);
  1286. msg = xs_dict_set(msg, "closed", et);
  1287. closed = 1;
  1288. }
  1289. }
  1290. /* update the count of voters */
  1291. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  1292. msg = xs_dict_set(msg, "votersCount", vcnt);
  1293. msg = xs_dict_set(msg, "cc", rcpts);
  1294. /* store */
  1295. object_add_ow(id, msg);
  1296. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  1297. timeline_touch(user);
  1298. /* send an update message to all voters */
  1299. xs *u_msg = msg_update(user, msg);
  1300. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  1301. enqueue_message(user, u_msg);
  1302. if (closed) {
  1303. xs *c_msg = msg_update(user, msg);
  1304. notify(user, "Update", "Question", user->actor, c_msg);
  1305. }
  1306. return 0;
  1307. }
  1308. /** queues **/
  1309. int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
  1310. /* processes an ActivityPub message from the input queue */
  1311. /* return values: -1, fatal error; 0, transient error, retry;
  1312. 1, processed and done; 2, propagate to users (only when no user is set) */
  1313. {
  1314. const char *actor = xs_dict_get(msg, "actor");
  1315. const char *type = xs_dict_get(msg, "type");
  1316. xs *actor_o = NULL;
  1317. int a_status;
  1318. int do_notify = 0;
  1319. if (xs_is_null(actor) || *actor == '\0') {
  1320. srv_debug(0, xs_fmt("malformed message (bad actor)"));
  1321. return -1;
  1322. }
  1323. /* question votes may not have a type */
  1324. if (xs_is_null(type))
  1325. type = "Note";
  1326. /* reject uninteresting messages right now */
  1327. if (xs_match(type, "Add|View")) {
  1328. srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
  1329. return -1;
  1330. }
  1331. const char *object, *utype;
  1332. object = xs_dict_get(msg, "object");
  1333. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1334. utype = xs_dict_get(object, "type");
  1335. else
  1336. utype = "(null)";
  1337. /* special case for Delete messages */
  1338. if (strcmp(type, "Delete") == 0) {
  1339. /* if the actor is not here, do not even try */
  1340. if (!object_here(actor)) {
  1341. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
  1342. return -1;
  1343. }
  1344. /* discard crap */
  1345. if (xs_is_null(object)) {
  1346. srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
  1347. return -1;
  1348. }
  1349. /* also discard if the object to be deleted is not here */
  1350. const char *obj_id = object;
  1351. if (xs_type(obj_id) == XSTYPE_DICT)
  1352. obj_id = xs_dict_get(obj_id, "id");
  1353. if (!object_here(obj_id)) {
  1354. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
  1355. return -1;
  1356. }
  1357. }
  1358. /* bring the actor */
  1359. a_status = actor_request(snac, actor, &actor_o);
  1360. /* do not retry permanent failures */
  1361. if (a_status == HTTP_STATUS_NOT_FOUND
  1362. || a_status == HTTP_STATUS_GONE
  1363. || a_status < 0) {
  1364. srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1365. return -1;
  1366. }
  1367. if (!valid_status(a_status)) {
  1368. /* do not retry 'Delete' messages */
  1369. if (strcmp(type, "Delete") == 0) {
  1370. srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
  1371. return -1;
  1372. }
  1373. /* other actor download errors */
  1374. /* the actor may require a signed request; propagate if no user is set */
  1375. if (snac == NULL)
  1376. return 2;
  1377. /* may need a retry */
  1378. srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1379. return 0;
  1380. }
  1381. /* check the signature */
  1382. xs *sig_err = NULL;
  1383. if (!check_signature(req, &sig_err)) {
  1384. srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
  1385. srv_archive_error("check_signature", sig_err, req, msg);
  1386. return -1;
  1387. }
  1388. /* if no user is set, no further checks can be done; propagate */
  1389. if (snac == NULL)
  1390. return 2;
  1391. /* reject messages that are not for this user */
  1392. if (!is_msg_for_me(snac, msg)) {
  1393. snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1394. return 1;
  1395. }
  1396. /* if it's a DM from someone we don't follow, reject the message */
  1397. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1398. if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
  1399. !following_check(snac, actor)) {
  1400. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1401. return 1;
  1402. }
  1403. }
  1404. /* check the minimum acceptable account age */
  1405. int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
  1406. if (min_account_age > 0) {
  1407. const char *actor_date = xs_dict_get(actor_o, "published");
  1408. if (!xs_is_null(actor_date)) {
  1409. time_t actor_t = xs_parse_iso_date(actor_date, 0);
  1410. if (actor_t < 950000000) {
  1411. snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
  1412. actor, actor_date));
  1413. return 1;
  1414. }
  1415. if (actor_t > 0) {
  1416. int td = (int)(time(NULL) - actor_t);
  1417. snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
  1418. if (td < min_account_age) {
  1419. snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
  1420. actor, td));
  1421. return 1;
  1422. }
  1423. }
  1424. }
  1425. else
  1426. snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
  1427. }
  1428. if (strcmp(type, "Follow") == 0) { /** **/
  1429. if (!follower_check(snac, actor)) {
  1430. /* ensure the actor object is here */
  1431. if (!object_here(actor)) {
  1432. xs *actor_obj = NULL;
  1433. actor_request(snac, actor, &actor_obj);
  1434. object_add(actor, actor_obj);
  1435. }
  1436. xs *f_msg = xs_dup(msg);
  1437. xs *reply = msg_accept(snac, f_msg, actor);
  1438. post_message(snac, actor, reply);
  1439. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1440. /* add a date if it doesn't include one (Mastodon) */
  1441. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1442. f_msg = xs_dict_set(f_msg, "published", date);
  1443. }
  1444. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  1445. follower_add(snac, actor);
  1446. snac_log(snac, xs_fmt("new follower %s", actor));
  1447. do_notify = 1;
  1448. }
  1449. else
  1450. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1451. }
  1452. else
  1453. if (strcmp(type, "Undo") == 0) { /** **/
  1454. const char *id = xs_dict_get(object, "object");
  1455. if (xs_type(object) != XSTYPE_DICT)
  1456. utype = "Follow";
  1457. if (strcmp(utype, "Follow") == 0) { /** **/
  1458. if (id && strcmp(id, snac->actor) != 0)
  1459. snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
  1460. else {
  1461. if (valid_status(follower_del(snac, actor))) {
  1462. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1463. do_notify = 1;
  1464. }
  1465. else
  1466. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1467. }
  1468. }
  1469. else
  1470. if (strcmp(utype, "Like") == 0) { /** **/
  1471. int status = object_unadmire(id, actor, 1);
  1472. snac_log(snac, xs_fmt("Unlike for %s %d", id, status));
  1473. }
  1474. else
  1475. if (strcmp(utype, "Announce") == 0) { /** **/
  1476. int status = HTTP_STATUS_OK;
  1477. /* commented out: if a followed user boosts something that
  1478. is requested and then unboosts, the post remains here,
  1479. but with no apparent reason, and that is confusing */
  1480. //status = object_unadmire(id, actor, 0);
  1481. snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
  1482. }
  1483. else
  1484. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1485. }
  1486. else
  1487. if (strcmp(type, "Create") == 0) { /** **/
  1488. if (is_muted(snac, actor)) {
  1489. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1490. return 1;
  1491. }
  1492. if (xs_match(utype, "Note|Article")) { /** **/
  1493. const char *id = xs_dict_get(object, "id");
  1494. const char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1495. const char *atto = get_atto(object);
  1496. xs *wrk = NULL;
  1497. if (xs_is_null(id))
  1498. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1499. else
  1500. if (xs_is_null(atto))
  1501. snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
  1502. else
  1503. if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
  1504. snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
  1505. }
  1506. else {
  1507. if (content_match("filter_reject.txt", object)) {
  1508. snac_log(snac, xs_fmt("rejected by content %s", id));
  1509. return 1;
  1510. }
  1511. if (strcmp(actor, atto) != 0)
  1512. snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
  1513. timeline_request(snac, &in_reply_to, &wrk, 0);
  1514. if (timeline_add(snac, id, object)) {
  1515. snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
  1516. do_notify = 1;
  1517. }
  1518. /* if it has a "name" field, it may be a vote for a question */
  1519. const char *name = xs_dict_get(object, "name");
  1520. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1521. update_question(snac, in_reply_to);
  1522. }
  1523. }
  1524. else
  1525. if (strcmp(utype, "Question") == 0) { /** **/
  1526. const char *id = xs_dict_get(object, "id");
  1527. if (timeline_add(snac, id, object))
  1528. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1529. }
  1530. else
  1531. if (strcmp(utype, "Video") == 0) { /** **/
  1532. const char *id = xs_dict_get(object, "id");
  1533. if (timeline_add(snac, id, object))
  1534. snac_log(snac, xs_fmt("new 'Video' %s %s", actor, id));
  1535. }
  1536. else
  1537. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1538. }
  1539. else
  1540. if (strcmp(type, "Accept") == 0) { /** **/
  1541. if (strcmp(utype, "(null)") == 0) {
  1542. const char *obj_id = xs_dict_get(msg, "object");
  1543. /* if the accepted object id is a string that may
  1544. be created by us, it's a follow */
  1545. if (xs_type(obj_id) == XSTYPE_STRING &&
  1546. xs_startswith(obj_id, srv_baseurl) &&
  1547. xs_endswith(obj_id, "/Follow"))
  1548. utype = "Follow";
  1549. }
  1550. if (strcmp(utype, "Follow") == 0) { /** **/
  1551. if (following_check(snac, actor)) {
  1552. following_add(snac, actor, msg);
  1553. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1554. }
  1555. else
  1556. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1557. }
  1558. else
  1559. if (strcmp(utype, "Create") == 0) {
  1560. /* some implementations send Create confirmations, go figure */
  1561. snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
  1562. }
  1563. else {
  1564. srv_archive_error("accept", "ignored Accept", req, msg);
  1565. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1566. }
  1567. }
  1568. else
  1569. if (strcmp(type, "Like") == 0) { /** **/
  1570. if (xs_type(object) == XSTYPE_DICT)
  1571. object = xs_dict_get(object, "id");
  1572. if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
  1573. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1574. else
  1575. snac_log(snac, xs_fmt("repeated 'Like' from %s to %s", actor, object));
  1576. do_notify = 1;
  1577. }
  1578. else
  1579. if (strcmp(type, "Announce") == 0) { /** **/
  1580. if (xs_type(object) == XSTYPE_DICT)
  1581. object = xs_dict_get(object, "id");
  1582. if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
  1583. snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
  1584. else
  1585. if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
  1586. snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
  1587. else {
  1588. xs *a_msg = NULL;
  1589. xs *wrk = NULL;
  1590. timeline_request(snac, &object, &wrk, 0);
  1591. if (valid_status(object_get(object, &a_msg))) {
  1592. const char *who = get_atto(a_msg);
  1593. if (who && !is_muted(snac, who)) {
  1594. /* bring the actor */
  1595. xs *who_o = NULL;
  1596. if (valid_status(actor_request(snac, who, &who_o))) {
  1597. if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
  1598. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1599. else
  1600. snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
  1601. actor, object));
  1602. /* distribute the post with the actor as 'proxy' */
  1603. list_distribute(snac, actor, a_msg);
  1604. do_notify = 1;
  1605. }
  1606. else
  1607. snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1608. }
  1609. else
  1610. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1611. }
  1612. else
  1613. snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
  1614. }
  1615. }
  1616. else
  1617. if (strcmp(type, "Update") == 0) { /** **/
  1618. if (xs_match(utype, "Person|Service")) { /** **/
  1619. actor_add(actor, xs_dict_get(msg, "object"));
  1620. timeline_touch(snac);
  1621. snac_log(snac, xs_fmt("updated actor %s", actor));
  1622. }
  1623. else
  1624. if (xs_match(utype, "Note|Page|Article|Video")) { /** **/
  1625. const char *id = xs_dict_get(object, "id");
  1626. if (object_here(id)) {
  1627. object_add_ow(id, object);
  1628. timeline_touch(snac);
  1629. snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
  1630. }
  1631. else
  1632. snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
  1633. }
  1634. else
  1635. if (strcmp(utype, "Question") == 0) { /** **/
  1636. const char *id = xs_dict_get(object, "id");
  1637. const char *closed = xs_dict_get(object, "closed");
  1638. object_add_ow(id, object);
  1639. timeline_touch(snac);
  1640. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1641. if (closed != NULL)
  1642. do_notify = 1;
  1643. }
  1644. else {
  1645. srv_archive_error("unsupported_update", "unsupported_update", req, msg);
  1646. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1647. }
  1648. }
  1649. else
  1650. if (strcmp(type, "Delete") == 0) { /** **/
  1651. if (xs_type(object) == XSTYPE_DICT)
  1652. object = xs_dict_get(object, "id");
  1653. if (object_here(object)) {
  1654. timeline_del(snac, object);
  1655. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1656. }
  1657. else
  1658. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1659. }
  1660. else
  1661. if (strcmp(type, "Pong") == 0) { /** **/
  1662. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1663. }
  1664. else
  1665. if (strcmp(type, "Ping") == 0) { /** **/
  1666. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1667. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1668. enqueue_output_by_actor(snac, rsp, actor, 0);
  1669. }
  1670. else {
  1671. srv_archive_error("unsupported_type", "unsupported_type", req, msg);
  1672. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1673. }
  1674. if (do_notify) {
  1675. notify(snac, type, utype, actor, msg);
  1676. timeline_touch(snac);
  1677. }
  1678. return 1;
  1679. }
  1680. int send_email(const char *msg)
  1681. /* invoke sendmail with email headers and body in msg */
  1682. {
  1683. FILE *f;
  1684. int status;
  1685. int fds[2];
  1686. pid_t pid;
  1687. if (pipe(fds) == -1) return -1;
  1688. pid = vfork();
  1689. if (pid == -1) return -1;
  1690. else if (pid == 0) {
  1691. dup2(fds[0], 0);
  1692. close(fds[0]);
  1693. close(fds[1]);
  1694. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1695. _exit(1);
  1696. }
  1697. close(fds[0]);
  1698. if ((f = fdopen(fds[1], "w")) == NULL) {
  1699. close(fds[1]);
  1700. return -1;
  1701. }
  1702. fprintf(f, "%s\n", msg);
  1703. fclose(f);
  1704. if (waitpid(pid, &status, 0) == -1) return -1;
  1705. return status;
  1706. }
  1707. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1708. /* processes an item from the user queue */
  1709. {
  1710. const char *type;
  1711. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1712. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1713. type = "output";
  1714. if (strcmp(type, "message") == 0) {
  1715. const xs_dict *msg = xs_dict_get(q_item, "message");
  1716. xs *rcpts = recipient_list(snac, msg, 1);
  1717. xs_set inboxes;
  1718. const xs_str *actor;
  1719. int c;
  1720. xs_set_init(&inboxes);
  1721. /* iterate the recipients */
  1722. c = 0;
  1723. while (xs_list_next(rcpts, &actor, &c)) {
  1724. xs *inbox = get_actor_inbox(actor);
  1725. if (inbox != NULL) {
  1726. /* add to the set and, if it's not there, send message */
  1727. if (xs_set_add(&inboxes, inbox) == 1)
  1728. enqueue_output(snac, msg, inbox, 0, 0);
  1729. }
  1730. else
  1731. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1732. }
  1733. /* if it's a public note or question, send to the collected inboxes */
  1734. if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
  1735. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  1736. xs *shibx = inbox_list();
  1737. const xs_str *inbox;
  1738. c = 0;
  1739. while (xs_list_next(shibx, &inbox, &c)) {
  1740. if (xs_set_add(&inboxes, inbox) == 1)
  1741. enqueue_output(snac, msg, inbox, 0, 0);
  1742. }
  1743. }
  1744. }
  1745. xs_set_free(&inboxes);
  1746. }
  1747. else
  1748. if (strcmp(type, "input") == 0) {
  1749. /* process the message */
  1750. const xs_dict *msg = xs_dict_get(q_item, "message");
  1751. const xs_dict *req = xs_dict_get(q_item, "req");
  1752. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1753. if (xs_is_null(msg))
  1754. return;
  1755. if (!process_input_message(snac, msg, req)) {
  1756. srv_archive_error("input", "process_input_message", req, msg);
  1757. if (retries > queue_retry_max)
  1758. snac_log(snac, xs_fmt("input giving up"));
  1759. else {
  1760. /* reenqueue */
  1761. enqueue_input(snac, msg, req, retries + 1);
  1762. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1763. }
  1764. }
  1765. }
  1766. else
  1767. if (strcmp(type, "close_question") == 0) {
  1768. /* the time for this question has ended */
  1769. const char *id = xs_dict_get(q_item, "message");
  1770. if (!xs_is_null(id))
  1771. update_question(snac, id);
  1772. }
  1773. else
  1774. if (strcmp(type, "object_request") == 0) {
  1775. const char *id = xs_dict_get(q_item, "message");
  1776. if (!xs_is_null(id)) {
  1777. int status;
  1778. xs *data = NULL;
  1779. status = activitypub_request(snac, id, &data);
  1780. if (valid_status(status))
  1781. object_add_ow(id, data);
  1782. snac_debug(snac, 1, xs_fmt("object_request %s %d", id, status));
  1783. }
  1784. }
  1785. else
  1786. if (strcmp(type, "verify_links") == 0) {
  1787. verify_links(snac);
  1788. }
  1789. else
  1790. if (strcmp(type, "actor_refresh") == 0) {
  1791. const char *actor = xs_dict_get(q_item, "actor");
  1792. double mtime = object_mtime(actor);
  1793. /* only refresh if it was refreshed more than an hour ago */
  1794. if (mtime + 3600.0 < (double) time(NULL)) {
  1795. xs *actor_o = NULL;
  1796. int status;
  1797. if (valid_status((status = activitypub_request(snac, actor, &actor_o))))
  1798. actor_add(actor, actor_o);
  1799. else
  1800. object_touch(actor);
  1801. snac_log(snac, xs_fmt("actor_refresh %s %d", actor, status));
  1802. }
  1803. }
  1804. else
  1805. snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
  1806. }
  1807. int process_user_queue(snac *snac)
  1808. /* processes a user's queue */
  1809. {
  1810. int cnt = 0;
  1811. xs *list = user_queue(snac);
  1812. xs_list *p = list;
  1813. const xs_str *fn;
  1814. while (xs_list_iter(&p, &fn)) {
  1815. xs *q_item = dequeue(fn);
  1816. if (q_item == NULL)
  1817. continue;
  1818. process_user_queue_item(snac, q_item);
  1819. cnt++;
  1820. }
  1821. return cnt;
  1822. }
  1823. void process_queue_item(xs_dict *q_item)
  1824. /* processes an item from the global queue */
  1825. {
  1826. const char *type = xs_dict_get(q_item, "type");
  1827. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1828. if (strcmp(type, "output") == 0) {
  1829. int status;
  1830. const xs_str *inbox = xs_dict_get(q_item, "inbox");
  1831. const xs_str *keyid = xs_dict_get(q_item, "keyid");
  1832. const xs_str *seckey = xs_dict_get(q_item, "seckey");
  1833. const xs_dict *msg = xs_dict_get(q_item, "message");
  1834. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1835. int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
  1836. xs *payload = NULL;
  1837. int p_size = 0;
  1838. int timeout = 0;
  1839. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1840. srv_log(xs_fmt("output message error: missing fields"));
  1841. return;
  1842. }
  1843. if (is_instance_blocked(inbox)) {
  1844. srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
  1845. return;
  1846. }
  1847. /* deliver (if previous error status was a timeout, try now longer) */
  1848. if (p_status == 599)
  1849. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
  1850. else
  1851. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
  1852. if (timeout == 0)
  1853. timeout = 6;
  1854. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
  1855. if (payload) {
  1856. if (p_size > 64) {
  1857. /* trim the message */
  1858. payload[64] = '\0';
  1859. payload = xs_str_cat(payload, "...");
  1860. }
  1861. /* strip ugly control characters */
  1862. payload = xs_replace_i(payload, "\n", "");
  1863. payload = xs_replace_i(payload, "\r", "");
  1864. if (*payload)
  1865. payload = xs_str_wrap_i(" [", payload, "]");
  1866. }
  1867. else
  1868. payload = xs_str_new(NULL);
  1869. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1870. if (!valid_status(status)) {
  1871. retries++;
  1872. /* if it's not the first time it fails with a timeout,
  1873. penalize the server by skipping one retry */
  1874. if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
  1875. retries++;
  1876. /* error sending; requeue? */
  1877. if (status == HTTP_STATUS_BAD_REQUEST
  1878. || status == HTTP_STATUS_NOT_FOUND
  1879. || status == HTTP_STATUS_METHOD_NOT_ALLOWED
  1880. || status == HTTP_STATUS_GONE
  1881. || status < 0)
  1882. /* explicit error: discard */
  1883. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1884. else
  1885. if (retries > queue_retry_max)
  1886. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1887. else {
  1888. /* requeue */
  1889. enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
  1890. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1891. }
  1892. }
  1893. }
  1894. else
  1895. if (strcmp(type, "email") == 0) {
  1896. /* send this email */
  1897. const xs_str *msg = xs_dict_get(q_item, "message");
  1898. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1899. if (!send_email(msg))
  1900. srv_debug(1, xs_fmt("email message sent"));
  1901. else {
  1902. retries++;
  1903. if (retries > queue_retry_max)
  1904. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1905. else {
  1906. /* requeue */
  1907. srv_log(xs_fmt(
  1908. "email requeue #%d (errno: %d)", retries, errno));
  1909. enqueue_email(msg, retries);
  1910. }
  1911. }
  1912. }
  1913. else
  1914. if (strcmp(type, "telegram") == 0) {
  1915. /* send this via telegram */
  1916. const char *bot = xs_dict_get(q_item, "bot");
  1917. const char *msg = xs_dict_get(q_item, "message");
  1918. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1919. int status = 0;
  1920. /* chat_id must start with a - */
  1921. if (!xs_startswith(chat_id, "-"))
  1922. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1923. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1924. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1925. xs *headers = xs_dict_new();
  1926. headers = xs_dict_append(headers, "content-type", "application/json");
  1927. xs *rsp = xs_http_request("POST", url, headers,
  1928. body, strlen(body), &status, NULL, NULL, 0);
  1929. rsp = xs_free(rsp);
  1930. srv_debug(0, xs_fmt("telegram post %d", status));
  1931. }
  1932. else
  1933. if (strcmp(type, "ntfy") == 0) {
  1934. /* send this via ntfy */
  1935. const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
  1936. const char *msg = xs_dict_get(q_item, "message");
  1937. const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
  1938. int status = 0;
  1939. xs *url = xs_fmt("%s", ntfy_server);
  1940. xs *body = xs_fmt("%s", msg);
  1941. xs *headers = xs_dict_new();
  1942. headers = xs_dict_append(headers, "content-type", "text/plain");
  1943. // Append the Authorization header only if ntfy_token is not NULL
  1944. if (ntfy_token != NULL) {
  1945. headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
  1946. }
  1947. xs *rsp = xs_http_request("POST", url, headers,
  1948. body, strlen(body), &status, NULL, NULL, 0);
  1949. rsp = xs_free(rsp);
  1950. srv_debug(0, xs_fmt("ntfy post %d", status));
  1951. }
  1952. else
  1953. if (strcmp(type, "purge") == 0) {
  1954. srv_log(xs_dup("purge start"));
  1955. purge_all();
  1956. srv_log(xs_dup("purge end"));
  1957. }
  1958. else
  1959. if (strcmp(type, "input") == 0) {
  1960. const xs_dict *msg = xs_dict_get(q_item, "message");
  1961. const xs_dict *req = xs_dict_get(q_item, "req");
  1962. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1963. /* do some instance-level checks */
  1964. int r = process_input_message(NULL, msg, req);
  1965. if (r == 0) {
  1966. /* transient error? retry */
  1967. if (retries > queue_retry_max)
  1968. srv_log(xs_fmt("shared input giving up"));
  1969. else {
  1970. /* reenqueue */
  1971. enqueue_shared_input(msg, req, retries + 1);
  1972. srv_log(xs_fmt("shared input requeue #%d", retries + 1));
  1973. }
  1974. }
  1975. else
  1976. if (r == 2) {
  1977. /* redistribute the input message to all users */
  1978. const char *ntid = xs_dict_get(q_item, "ntid");
  1979. xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
  1980. FILE *f;
  1981. if ((f = fopen(tmpfn, "w")) != NULL) {
  1982. xs_json_dump(q_item, 4, f);
  1983. fclose(f);
  1984. }
  1985. xs *users = user_list();
  1986. xs_list *p = users;
  1987. const char *v;
  1988. int cnt = 0;
  1989. while (xs_list_iter(&p, &v)) {
  1990. snac user;
  1991. if (user_open(&user, v)) {
  1992. if (is_msg_for_me(&user, msg)) {
  1993. xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
  1994. snac_debug(&user, 1,
  1995. xs_fmt("enqueue_input (from shared inbox) %s", xs_dict_get(msg, "id")));
  1996. if (link(tmpfn, fn) < 0)
  1997. srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
  1998. cnt++;
  1999. }
  2000. user_free(&user);
  2001. }
  2002. }
  2003. unlink(tmpfn);
  2004. if (cnt == 0) {
  2005. srv_archive_qitem("no_valid_recipients", q_item);
  2006. srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
  2007. }
  2008. }
  2009. }
  2010. else
  2011. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  2012. }
  2013. int process_queue(void)
  2014. /* processes the global queue */
  2015. {
  2016. int cnt = 0;
  2017. xs *list = queue();
  2018. xs_list *p = list;
  2019. const xs_str *fn;
  2020. while (xs_list_iter(&p, &fn)) {
  2021. xs *q_item = dequeue(fn);
  2022. if (q_item != NULL) {
  2023. job_post(q_item, 0);
  2024. cnt++;
  2025. }
  2026. }
  2027. return cnt;
  2028. }
  2029. /** HTTP handlers */
  2030. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  2031. char **body, int *b_size, char **ctype)
  2032. {
  2033. int status = HTTP_STATUS_OK;
  2034. const char *accept = xs_dict_get(req, "accept");
  2035. snac snac;
  2036. xs *msg = NULL;
  2037. if (accept == NULL)
  2038. return 0;
  2039. if (xs_str_in(accept, "application/activity+json") == -1 &&
  2040. xs_str_in(accept, "application/ld+json") == -1)
  2041. return 0;
  2042. xs *l = xs_split_n(q_path, "/", 2);
  2043. const char *uid;
  2044. const char *p_path;
  2045. uid = xs_list_get(l, 1);
  2046. if (!user_open(&snac, uid)) {
  2047. /* invalid user */
  2048. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  2049. return HTTP_STATUS_NOT_FOUND;
  2050. }
  2051. p_path = xs_list_get(l, 2);
  2052. *ctype = "application/activity+json";
  2053. if (p_path == NULL) {
  2054. /* if there was no component after the user, it's an actor request */
  2055. msg = msg_actor(&snac);
  2056. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  2057. const char *ua = xs_dict_get(req, "user-agent");
  2058. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  2059. }
  2060. else
  2061. if (strcmp(p_path, "outbox") == 0) {
  2062. xs *id = xs_fmt("%s/outbox", snac.actor);
  2063. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  2064. xs *list = xs_list_new();
  2065. msg = msg_collection(&snac, id);
  2066. char *p;
  2067. const char *v;
  2068. p = elems;
  2069. while (xs_list_iter(&p, &v)) {
  2070. xs *i = NULL;
  2071. if (valid_status(object_get_by_md5(v, &i))) {
  2072. const char *type = xs_dict_get(i, "type");
  2073. const char *id = xs_dict_get(i, "id");
  2074. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  2075. xs *c_msg = msg_create(&snac, i);
  2076. list = xs_list_append(list, c_msg);
  2077. }
  2078. }
  2079. }
  2080. /* replace the 'orderedItems' with the latest posts */
  2081. xs *items = xs_number_new(xs_list_len(list));
  2082. msg = xs_dict_set(msg, "orderedItems", list);
  2083. msg = xs_dict_set(msg, "totalItems", items);
  2084. }
  2085. else
  2086. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  2087. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2088. msg = msg_collection(&snac, id);
  2089. }
  2090. else
  2091. if (xs_startswith(p_path, "p/")) {
  2092. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2093. status = object_get(id, &msg);
  2094. /* don't return non-public objects */
  2095. if (valid_status(status) && !is_msg_public(msg))
  2096. status = HTTP_STATUS_NOT_FOUND;
  2097. }
  2098. else
  2099. status = HTTP_STATUS_NOT_FOUND;
  2100. if (status == HTTP_STATUS_OK && msg != NULL) {
  2101. *body = xs_json_dumps(msg, 4);
  2102. *b_size = strlen(*body);
  2103. }
  2104. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  2105. user_free(&snac);
  2106. return status;
  2107. }
  2108. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  2109. char *payload, int p_size,
  2110. char **body, int *b_size, char **ctype)
  2111. /* processes an input message */
  2112. {
  2113. (void)b_size;
  2114. int status = HTTP_STATUS_ACCEPTED;
  2115. const char *i_ctype = xs_dict_get(req, "content-type");
  2116. snac snac;
  2117. const char *v;
  2118. if (i_ctype == NULL) {
  2119. *body = xs_str_new("no content-type");
  2120. *ctype = "text/plain";
  2121. return HTTP_STATUS_BAD_REQUEST;
  2122. }
  2123. if (xs_is_null(payload)) {
  2124. *body = xs_str_new("no payload");
  2125. *ctype = "text/plain";
  2126. return HTTP_STATUS_BAD_REQUEST;
  2127. }
  2128. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  2129. xs_str_in(i_ctype, "application/ld+json") == -1)
  2130. return 0;
  2131. /* decode the message */
  2132. xs *msg = xs_json_loads(payload);
  2133. const char *id = xs_dict_get(msg, "id");
  2134. if (msg == NULL) {
  2135. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  2136. srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
  2137. *body = xs_str_new("JSON error");
  2138. *ctype = "text/plain";
  2139. return HTTP_STATUS_BAD_REQUEST;
  2140. }
  2141. if (id && is_instance_blocked(id)) {
  2142. srv_debug(1, xs_fmt("full instance block for %s", id));
  2143. *body = xs_str_new("blocked");
  2144. *ctype = "text/plain";
  2145. return HTTP_STATUS_FORBIDDEN;
  2146. }
  2147. /* get the user and path */
  2148. xs *l = xs_split_n(q_path, "/", 2);
  2149. if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
  2150. enqueue_shared_input(msg, req, 0);
  2151. return HTTP_STATUS_ACCEPTED;
  2152. }
  2153. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  2154. /* strange q_path */
  2155. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  2156. return HTTP_STATUS_NOT_FOUND;
  2157. }
  2158. const char *uid = xs_list_get(l, 1);
  2159. if (!user_open(&snac, uid)) {
  2160. /* invalid user */
  2161. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  2162. return HTTP_STATUS_NOT_FOUND;
  2163. }
  2164. /* if it has a digest, check it now, because
  2165. later the payload won't be exactly the same */
  2166. if ((v = xs_dict_get(req, "digest")) != NULL) {
  2167. xs *s1 = xs_sha256_base64(payload, p_size);
  2168. xs *s2 = xs_fmt("SHA-256=%s", s1);
  2169. if (strcmp(s2, v) != 0) {
  2170. srv_log(xs_fmt("digest check FAILED"));
  2171. *body = xs_str_new("bad digest");
  2172. *ctype = "text/plain";
  2173. status = HTTP_STATUS_BAD_REQUEST;
  2174. }
  2175. }
  2176. /* if the message is from a muted actor, reject it right now */
  2177. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  2178. if (is_muted(&snac, v)) {
  2179. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  2180. *body = xs_str_new("rejected");
  2181. *ctype = "text/plain";
  2182. status = HTTP_STATUS_FORBIDDEN;
  2183. }
  2184. }
  2185. if (valid_status(status)) {
  2186. enqueue_input(&snac, msg, req, 0);
  2187. *ctype = "application/activity+json";
  2188. }
  2189. user_free(&snac);
  2190. return status;
  2191. }