activitypub.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "xs_curl.h"
  7. #include "xs_mime.h"
  8. #include "xs_openssl.h"
  9. #include "xs_regex.h"
  10. #include "xs_time.h"
  11. #include "xs_set.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 *default_avatar_base64(void)
  29. /* returns the default avatar in base64 */
  30. {
  31. return susie;
  32. }
  33. int activitypub_request(snac *snac, char *url, d_char **data)
  34. /* request an object */
  35. {
  36. int status;
  37. xs *response = NULL;
  38. xs *payload = NULL;
  39. int p_size;
  40. char *ctype;
  41. /* check if it's an url for this same site */
  42. /* ... */
  43. /* get from the net */
  44. response = http_signed_request(snac, "GET", url,
  45. NULL, NULL, 0, &status, &payload, &p_size, 0);
  46. if (valid_status(status)) {
  47. /* ensure it's ActivityPub data */
  48. ctype = xs_dict_get(response, "content-type");
  49. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  50. xs_str_in(ctype, "application/ld+json") != -1)
  51. *data = xs_json_loads(payload);
  52. else
  53. status = 500;
  54. }
  55. if (!valid_status(status))
  56. *data = NULL;
  57. return status;
  58. }
  59. int actor_request(snac *snac, char *actor, d_char **data)
  60. /* request an actor */
  61. {
  62. int status, status2;
  63. xs *payload = NULL;
  64. /* get from disk first */
  65. status = actor_get(snac, actor, data);
  66. if (status == 200)
  67. return status;
  68. /* actor data non-existent or stale: get from the net */
  69. status2 = activitypub_request(snac, actor, &payload);
  70. if (valid_status(status2)) {
  71. /* renew data */
  72. status = actor_add(snac, actor, payload);
  73. if (data != NULL) {
  74. *data = payload;
  75. payload = NULL;
  76. }
  77. }
  78. return status;
  79. }
  80. int timeline_request(snac *snac, char **id, d_char **wrk)
  81. /* ensures that an entry and its ancestors are in the timeline */
  82. {
  83. int status = 0;
  84. if (!xs_is_null(*id)) {
  85. /* is the admired object already there? */
  86. if (!object_here(*id)) {
  87. xs *object = NULL;
  88. /* no; download it */
  89. status = activitypub_request(snac, *id, &object);
  90. if (valid_status(status)) {
  91. char *type = xs_dict_get(object, "type");
  92. /* get the id again from the object, as it may be different */
  93. char *nid = xs_dict_get(object, "id");
  94. if (wrk && strcmp(nid, *id) != 0) {
  95. snac_debug(snac, 1,
  96. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  97. *wrk = xs_dup(nid);
  98. *id = *wrk;
  99. }
  100. if (!xs_is_null(type) && strcmp(type, "Note") == 0) {
  101. char *actor = xs_dict_get(object, "attributedTo");
  102. /* request (and drop) the actor for this entry */
  103. if (!xs_is_null(actor))
  104. actor_request(snac, actor, NULL);
  105. /* does it have an ancestor? */
  106. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  107. /* recurse! */
  108. timeline_request(snac, &in_reply_to, NULL);
  109. /* finally store */
  110. timeline_add(snac, *id, object);
  111. }
  112. }
  113. }
  114. }
  115. return status;
  116. }
  117. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size, int timeout)
  118. /* sends a message to an Inbox */
  119. {
  120. int status;
  121. d_char *response;
  122. xs *j_msg = xs_json_dumps_pp(msg, 4);
  123. response = http_signed_request(snac, "POST", inbox,
  124. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  125. xs_free(response);
  126. return status;
  127. }
  128. d_char *get_actor_inbox(snac *snac, char *actor)
  129. /* gets an actor's inbox */
  130. {
  131. xs *data = NULL;
  132. char *v = NULL;
  133. if (valid_status(actor_request(snac, actor, &data))) {
  134. /* try first endpoints/sharedInbox */
  135. if ((v = xs_dict_get(data, "endpoints")))
  136. v = xs_dict_get(v, "sharedInbox");
  137. /* try then the regular inbox */
  138. if (xs_is_null(v))
  139. v = xs_dict_get(data, "inbox");
  140. }
  141. return xs_is_null(v) ? NULL : xs_dup(v);
  142. }
  143. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout)
  144. /* sends a message to an actor */
  145. {
  146. int status = 400;
  147. xs *inbox = get_actor_inbox(snac, actor);
  148. if (!xs_is_null(inbox))
  149. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  150. return status;
  151. }
  152. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  153. /* returns the list of recipients for a message */
  154. {
  155. char *to = xs_dict_get(msg, "to");
  156. char *cc = xs_dict_get(msg, "cc");
  157. xs_set rcpts;
  158. int n;
  159. xs_set_init(&rcpts);
  160. char *lists[] = { to, cc, NULL };
  161. for (n = 0; lists[n]; n++) {
  162. char *l = lists[n];
  163. char *v;
  164. xs *tl = NULL;
  165. /* if it's a string, create a list with only one element */
  166. if (xs_type(l) == XSTYPE_STRING) {
  167. tl = xs_list_new();
  168. tl = xs_list_append(tl, l);
  169. l = tl;
  170. }
  171. while (xs_list_iter(&l, &v)) {
  172. if (expand_public && strcmp(v, public_address) == 0) {
  173. /* iterate the followers and add them */
  174. xs *fwers = follower_list(snac);
  175. char *actor;
  176. char *p = fwers;
  177. while (xs_list_iter(&p, &actor))
  178. xs_set_add(&rcpts, actor);
  179. }
  180. else
  181. xs_set_add(&rcpts, v);
  182. }
  183. }
  184. return xs_set_result(&rcpts);
  185. }
  186. d_char *inbox_list(snac *snac, char *msg)
  187. /* returns the list of inboxes that are recipients of this message */
  188. {
  189. xs *rcpts = recipient_list(snac, msg, 1);
  190. xs_set inboxes;
  191. char *p, *v;
  192. xs_set_init(&inboxes);
  193. p = rcpts;
  194. while (xs_list_iter(&p, &v)) {
  195. xs *inbox;
  196. if ((inbox = get_actor_inbox(snac, v)) != NULL) {
  197. /* add the inbox if it's not already there */
  198. xs_set_add(&inboxes, inbox);
  199. }
  200. }
  201. return xs_set_result(&inboxes);
  202. }
  203. int is_msg_public(snac *snac, char *msg)
  204. /* checks if a message is public */
  205. {
  206. int ret = 0;
  207. xs *rcpts = recipient_list(snac, msg, 0);
  208. char *p, *v;
  209. p = rcpts;
  210. while (!ret && xs_list_iter(&p, &v)) {
  211. if (strcmp(v, public_address) == 0)
  212. ret = 1;
  213. }
  214. return ret;
  215. }
  216. void process_tags(snac *snac, const char *content, d_char **n_content, d_char **tag)
  217. /* parses mentions and tags from content */
  218. {
  219. d_char *nc = xs_str_new(NULL);
  220. d_char *tl = xs_list_new();
  221. xs *split;
  222. char *p, *v;
  223. int n = 0;
  224. split = xs_regex_split(content, "(@[A-Za-z0-9_]+@[A-Za-z0-9\\.-]+|&#[0-9]+;|#[^ ,\\.:;<]+)");
  225. p = split;
  226. while (xs_list_iter(&p, &v)) {
  227. if ((n & 0x1)) {
  228. if (*v == '@') {
  229. /* query the webfinger about this fellow */
  230. xs *v2 = xs_strip_chars_i(xs_dup(v), "@.");
  231. xs *actor = NULL;
  232. xs *uid = NULL;
  233. int status;
  234. status = webfinger_request(v2, &actor, &uid);
  235. if (valid_status(status)) {
  236. xs *d = xs_dict_new();
  237. xs *n = xs_fmt("@%s", uid);
  238. xs *l = xs_fmt("<a href=\"%s\" class=\"u-url mention\">%s</a>", actor, n);
  239. d = xs_dict_append(d, "type", "Mention");
  240. d = xs_dict_append(d, "href", actor);
  241. d = xs_dict_append(d, "name", n);
  242. tl = xs_list_append(tl, d);
  243. /* add the code */
  244. nc = xs_str_cat(nc, l);
  245. }
  246. else
  247. /* store as is */
  248. nc = xs_str_cat(nc, v);
  249. }
  250. else
  251. if (*v == '#') {
  252. /* hashtag */
  253. xs *d = xs_dict_new();
  254. xs *n = xs_tolower_i(xs_dup(v));
  255. xs *h = xs_fmt("%s%s", snac->actor, n);
  256. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  257. d = xs_dict_append(d, "type", "Hashtag");
  258. d = xs_dict_append(d, "href", h);
  259. d = xs_dict_append(d, "name", n);
  260. tl = xs_list_append(tl, d);
  261. /* add the code */
  262. nc = xs_str_cat(nc, l);
  263. }
  264. else
  265. if (*v == '&') {
  266. /* HTML Unicode entity, probably part of an emoji */
  267. /* write as is */
  268. nc = xs_str_cat(nc, v);
  269. }
  270. }
  271. else
  272. nc = xs_str_cat(nc, v);
  273. n++;
  274. }
  275. *n_content = nc;
  276. *tag = tl;
  277. }
  278. /** messages **/
  279. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  280. /* creates a base ActivityPub message */
  281. {
  282. xs *did = NULL;
  283. xs *published = NULL;
  284. xs *ntid = tid(0);
  285. /* generated values */
  286. if (date && strcmp(date, "@now") == 0) {
  287. published = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  288. date = published;
  289. }
  290. if (id != NULL) {
  291. if (strcmp(id, "@dummy") == 0) {
  292. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  293. id = did;
  294. }
  295. else
  296. if (strcmp(id, "@object") == 0) {
  297. if (object != NULL) {
  298. did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
  299. id = did;
  300. }
  301. else
  302. id = NULL;
  303. }
  304. }
  305. d_char *msg = xs_dict_new();
  306. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  307. msg = xs_dict_append(msg, "type", type);
  308. if (id != NULL)
  309. msg = xs_dict_append(msg, "id", id);
  310. if (actor != NULL)
  311. msg = xs_dict_append(msg, "actor", actor);
  312. if (date != NULL)
  313. msg = xs_dict_append(msg, "published", date);
  314. if (object != NULL)
  315. msg = xs_dict_append(msg, "object", object);
  316. return msg;
  317. }
  318. d_char *msg_collection(snac *snac, char *id)
  319. /* creates an empty OrderedCollection message */
  320. {
  321. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  322. xs *ol = xs_list_new();
  323. xs *nz = xs_number_new(0);
  324. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  325. msg = xs_dict_append(msg, "orderedItems", ol);
  326. msg = xs_dict_append(msg, "totalItems", nz);
  327. return msg;
  328. }
  329. d_char *msg_accept(snac *snac, char *object, char *to)
  330. /* creates an Accept message (as a response to a Follow) */
  331. {
  332. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  333. msg = xs_dict_append(msg, "to", to);
  334. return msg;
  335. }
  336. d_char *msg_update(snac *snac, char *object)
  337. /* creates an Update message */
  338. {
  339. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  340. msg = xs_dict_append(msg, "to", public_address);
  341. return msg;
  342. }
  343. d_char *msg_admiration(snac *snac, char *object, char *type)
  344. /* creates a Like or Announce message */
  345. {
  346. xs *a_msg = NULL;
  347. d_char *msg = NULL;
  348. xs *wrk = NULL;
  349. /* call the object */
  350. timeline_request(snac, &object, &wrk);
  351. if (valid_status(object_get(object, &a_msg, NULL))) {
  352. xs *rcpts = xs_list_new();
  353. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  354. rcpts = xs_list_append(rcpts, public_address);
  355. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  356. msg = xs_dict_append(msg, "to", rcpts);
  357. }
  358. else
  359. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  360. return msg;
  361. }
  362. d_char *msg_actor(snac *snac)
  363. /* create a Person message for this actor */
  364. {
  365. xs *ctxt = xs_list_new();
  366. xs *icon = xs_dict_new();
  367. xs *keys = xs_dict_new();
  368. xs *avtr = NULL;
  369. xs *kid = NULL;
  370. xs *f_bio = NULL;
  371. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  372. char *p;
  373. int n;
  374. /* change the @context (is this really necessary?) */
  375. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  376. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  377. msg = xs_dict_set(msg, "@context", ctxt);
  378. msg = xs_dict_set(msg, "url", snac->actor);
  379. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  380. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  381. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  382. f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"));
  383. msg = xs_dict_set(msg, "summary", f_bio);
  384. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  385. for (n = 0; folders[n]; n++) {
  386. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  387. msg = xs_dict_set(msg, folders[n], f);
  388. }
  389. p = xs_dict_get(snac->config, "avatar");
  390. if (*p == '\0')
  391. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  392. else
  393. avtr = xs_dup(p);
  394. icon = xs_dict_append(icon, "type", "Image");
  395. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  396. icon = xs_dict_append(icon, "url", avtr);
  397. msg = xs_dict_set(msg, "icon", icon);
  398. kid = xs_fmt("%s#main-key", snac->actor);
  399. keys = xs_dict_append(keys, "id", kid);
  400. keys = xs_dict_append(keys, "owner", snac->actor);
  401. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  402. msg = xs_dict_set(msg, "publicKey", keys);
  403. return msg;
  404. }
  405. d_char *msg_create(snac *snac, char *object)
  406. /* creates a 'Create' message */
  407. {
  408. d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  409. msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
  410. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  411. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  412. return msg;
  413. }
  414. d_char *msg_undo(snac *snac, char *object)
  415. /* creates an 'Undo' message */
  416. {
  417. d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  418. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  419. return msg;
  420. }
  421. d_char *msg_delete(snac *snac, char *id)
  422. /* creates a 'Delete' + 'Tombstone' for a local entry */
  423. {
  424. xs *tomb = xs_dict_new();
  425. d_char *msg = NULL;
  426. /* sculpt the tombstone */
  427. tomb = xs_dict_append(tomb, "type", "Tombstone");
  428. tomb = xs_dict_append(tomb, "id", id);
  429. /* now create the Delete */
  430. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  431. msg = xs_dict_append(msg, "to", public_address);
  432. return msg;
  433. }
  434. d_char *msg_follow(snac *snac, char *url_or_uid)
  435. /* creates a 'Follow' message */
  436. {
  437. xs *actor_o = NULL;
  438. xs *actor = NULL;
  439. d_char *msg = NULL;
  440. int status;
  441. if (xs_startswith(url_or_uid, "https:/"))
  442. actor = xs_dup(url_or_uid);
  443. else
  444. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL))) {
  445. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  446. return NULL;
  447. }
  448. /* request the actor */
  449. status = actor_request(snac, actor, &actor_o);
  450. if (valid_status(status)) {
  451. /* check if the actor is an alias */
  452. char *r_actor = xs_dict_get(actor_o, "id");
  453. if (r_actor && strcmp(actor, r_actor) != 0) {
  454. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  455. }
  456. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  457. }
  458. else
  459. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  460. return msg;
  461. }
  462. d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char *attach)
  463. /* creates a 'Note' message */
  464. {
  465. xs *ntid = tid(0);
  466. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  467. xs *ctxt = NULL;
  468. xs *fc2 = NULL;
  469. xs *fc1 = NULL;
  470. xs *to = NULL;
  471. xs *cc = xs_list_new();
  472. xs *irt = NULL;
  473. xs *tag = NULL;
  474. xs *atls = NULL;
  475. d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  476. char *p, *v;
  477. if (rcpts == NULL)
  478. to = xs_list_new();
  479. else {
  480. if (xs_type(rcpts) == XSTYPE_STRING) {
  481. to = xs_list_new();
  482. to = xs_list_append(to, rcpts);
  483. }
  484. else
  485. to = xs_dup(rcpts);
  486. }
  487. /* format the content */
  488. fc2 = not_really_markdown(content);
  489. /* extract the tags */
  490. process_tags(snac, fc2, &fc1, &tag);
  491. if (tag == NULL)
  492. tag = xs_list_new();
  493. if (in_reply_to != NULL && *in_reply_to) {
  494. xs *p_msg = NULL;
  495. xs *wrk = NULL;
  496. /* demand this thing */
  497. timeline_request(snac, &in_reply_to, &wrk);
  498. if (valid_status(object_get(in_reply_to, &p_msg, NULL))) {
  499. /* add this author as recipient */
  500. char *a, *v;
  501. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  502. to = xs_list_append(to, a);
  503. /* add this author to the tag list as a mention */
  504. xs *t_href = NULL;
  505. xs *t_name = NULL;
  506. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  507. xs *t = xs_dict_new();
  508. t = xs_dict_append(t, "type", "Mention");
  509. t = xs_dict_append(t, "href", t_href);
  510. t = xs_dict_append(t, "name", t_name);
  511. tag = xs_list_append(tag, t);
  512. }
  513. /* get the context, if there is one */
  514. if ((v = xs_dict_get(p_msg, "context")))
  515. ctxt = xs_dup(v);
  516. /* if this message is public, ours will also be */
  517. if (is_msg_public(snac, p_msg) &&
  518. xs_list_in(to, public_address) == -1)
  519. to = xs_list_append(to, public_address);
  520. }
  521. irt = xs_dup(in_reply_to);
  522. }
  523. else
  524. irt = xs_val_new(XSTYPE_NULL);
  525. /* create the attachment list, if there are any */
  526. if (!xs_is_null(attach) && *attach != '\0') {
  527. xs *lsof1 = NULL;
  528. if (xs_type(attach) == XSTYPE_STRING) {
  529. lsof1 = xs_list_append(xs_list_new(), attach);
  530. attach = lsof1;
  531. }
  532. atls = xs_list_new();
  533. while (xs_list_iter(&attach, &v)) {
  534. xs *d = xs_dict_new();
  535. char *mime = xs_mime_by_ext(v);
  536. d = xs_dict_append(d, "mediaType", mime);
  537. d = xs_dict_append(d, "url", v);
  538. d = xs_dict_append(d, "name", "");
  539. d = xs_dict_append(d, "type",
  540. xs_startswith(mime, "image/") ? "Image" : "Document");
  541. atls = xs_list_append(atls, d);
  542. }
  543. }
  544. if (ctxt == NULL)
  545. ctxt = xs_fmt("%s#ctxt", id);
  546. /* add all mentions to the cc */
  547. p = tag;
  548. while (xs_list_iter(&p, &v)) {
  549. if (xs_type(v) == XSTYPE_DICT) {
  550. char *t;
  551. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  552. if ((t = xs_dict_get(v, "href")) != NULL)
  553. cc = xs_list_append(cc, t);
  554. }
  555. }
  556. }
  557. /* no recipients? must be for everybody */
  558. if (xs_list_len(to) == 0)
  559. to = xs_list_append(to, public_address);
  560. /* delete all cc recipients that also are in the to */
  561. p = to;
  562. while (xs_list_iter(&p, &v)) {
  563. int i;
  564. if ((i = xs_list_in(cc, v)) != -1)
  565. cc = xs_list_del(cc, i);
  566. }
  567. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  568. msg = xs_dict_append(msg, "summary", "");
  569. msg = xs_dict_append(msg, "content", fc1);
  570. msg = xs_dict_append(msg, "context", ctxt);
  571. msg = xs_dict_append(msg, "url", id);
  572. msg = xs_dict_append(msg, "to", to);
  573. msg = xs_dict_append(msg, "cc", cc);
  574. msg = xs_dict_append(msg, "inReplyTo", irt);
  575. msg = xs_dict_append(msg, "tag", tag);
  576. msg = xs_dict_append(msg, "sourceContent", content);
  577. if (atls != NULL)
  578. msg = xs_dict_append(msg, "attachment", atls);
  579. return msg;
  580. }
  581. void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
  582. /* notifies the user of relevant events */
  583. {
  584. char *email = xs_dict_get(snac->config, "email");
  585. char *object = NULL;
  586. /* no email address? done */
  587. if (xs_is_null(email) || *email == '\0')
  588. return;
  589. if (strcmp(type, "Create") == 0) {
  590. /* only notify of notes specifically for us */
  591. xs *rcpts = recipient_list(snac, msg, 0);
  592. if (xs_list_in(rcpts, snac->actor) == -1)
  593. return;
  594. }
  595. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  596. return;
  597. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  598. object = xs_dict_get(msg, "object");
  599. if (xs_is_null(object))
  600. return;
  601. else {
  602. if (xs_type(object) == XSTYPE_DICT)
  603. object = xs_dict_get(object, "id");
  604. /* if it's not an admiration about something by us, done */
  605. if (xs_is_null(object) || !xs_startswith(object, snac->actor))
  606. return;
  607. }
  608. }
  609. snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor));
  610. /* prepare message */
  611. xs *subject = xs_fmt("snac notify for @%s@%s",
  612. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  613. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  614. xs *header = xs_fmt(
  615. "From: %s\n"
  616. "To: %s\n"
  617. "Subject: %s\n"
  618. "\n",
  619. from, email, subject);
  620. xs *body = xs_str_new(header);
  621. if (strcmp(utype, "(null)") != 0) {
  622. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  623. body = xs_str_cat(body, s1);
  624. }
  625. else {
  626. xs *s1 = xs_fmt("Type : %s\n", type);
  627. body = xs_str_cat(body, s1);
  628. }
  629. {
  630. xs *s1 = xs_fmt("Actor : %s\n", actor);
  631. body = xs_str_cat(body, s1);
  632. }
  633. if (object != NULL) {
  634. xs *s1 = xs_fmt("Object: %s\n", object);
  635. body = xs_str_cat(body, s1);
  636. }
  637. enqueue_email(snac, body, 0);
  638. }
  639. /** queues **/
  640. int process_message(snac *snac, char *msg, char *req)
  641. /* processes an ActivityPub message from the input queue */
  642. {
  643. /* actor and type exist, were checked previously */
  644. char *actor = xs_dict_get(msg, "actor");
  645. char *type = xs_dict_get(msg, "type");
  646. xs *actor_o = NULL;
  647. int a_status;
  648. int do_notify = 0;
  649. char *object, *utype;
  650. object = xs_dict_get(msg, "object");
  651. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  652. utype = xs_dict_get(object, "type");
  653. else
  654. utype = "(null)";
  655. /* bring the actor */
  656. a_status = actor_request(snac, actor, &actor_o);
  657. /* if the actor does not explicitly exist, discard */
  658. if (a_status == 404 || a_status == 410) {
  659. snac_debug(snac, 1,
  660. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  661. return 1;
  662. }
  663. if (!valid_status(a_status)) {
  664. /* other actor download errors may need a retry */
  665. snac_debug(snac, 1,
  666. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  667. return 0;
  668. }
  669. /* check the signature */
  670. if (!check_signature(snac, req)) {
  671. snac_log(snac, xs_fmt("bad signature %s", actor));
  672. return 1;
  673. }
  674. if (strcmp(type, "Follow") == 0) {
  675. if (!follower_check(snac, actor)) {
  676. xs *f_msg = xs_dup(msg);
  677. xs *reply = msg_accept(snac, f_msg, actor);
  678. enqueue_message(snac, reply);
  679. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  680. /* add a date if it doesn't include one (Mastodon) */
  681. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  682. f_msg = xs_dict_set(f_msg, "published", date);
  683. }
  684. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  685. follower_add(snac, actor);
  686. snac_log(snac, xs_fmt("new follower %s", actor));
  687. do_notify = 1;
  688. }
  689. else
  690. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  691. }
  692. else
  693. if (strcmp(type, "Undo") == 0) {
  694. if (strcmp(utype, "Follow") == 0) {
  695. if (valid_status(follower_del(snac, actor))) {
  696. snac_log(snac, xs_fmt("no longer following us %s", actor));
  697. do_notify = 1;
  698. }
  699. else
  700. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  701. }
  702. else
  703. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  704. }
  705. else
  706. if (strcmp(type, "Create") == 0) {
  707. if (strcmp(utype, "Note") == 0) {
  708. if (is_muted(snac, actor))
  709. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  710. else {
  711. char *id = xs_dict_get(object, "id");
  712. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  713. xs *wrk = NULL;
  714. timeline_request(snac, &in_reply_to, &wrk);
  715. if (timeline_add(snac, id, object)) {
  716. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  717. do_notify = 1;
  718. }
  719. }
  720. }
  721. else
  722. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  723. }
  724. else
  725. if (strcmp(type, "Accept") == 0) {
  726. if (strcmp(utype, "Follow") == 0) {
  727. if (following_check(snac, actor)) {
  728. following_add(snac, actor, msg);
  729. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  730. }
  731. else
  732. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  733. }
  734. else
  735. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  736. }
  737. else
  738. if (strcmp(type, "Like") == 0) {
  739. if (xs_type(object) == XSTYPE_DICT)
  740. object = xs_dict_get(object, "id");
  741. timeline_admire(snac, object, actor, 1);
  742. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  743. do_notify = 1;
  744. }
  745. else
  746. if (strcmp(type, "Announce") == 0) {
  747. xs *a_msg = NULL;
  748. xs *wrk = NULL;
  749. if (xs_type(object) == XSTYPE_DICT)
  750. object = xs_dict_get(object, "id");
  751. timeline_request(snac, &object, &wrk);
  752. if (valid_status(object_get(object, &a_msg, NULL))) {
  753. char *who = xs_dict_get(a_msg, "attributedTo");
  754. if (who && !is_muted(snac, who)) {
  755. /* bring the actor */
  756. xs *who_o = NULL;
  757. if (valid_status(actor_request(snac, who, &who_o))) {
  758. timeline_admire(snac, object, actor, 0);
  759. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  760. do_notify = 1;
  761. }
  762. else
  763. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  764. }
  765. else
  766. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  767. }
  768. else
  769. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  770. }
  771. else
  772. if (strcmp(type, "Update") == 0) {
  773. if (strcmp(utype, "Person") == 0) {
  774. actor_add(snac, actor, xs_dict_get(msg, "object"));
  775. snac_log(snac, xs_fmt("updated actor %s", actor));
  776. }
  777. else
  778. if (strcmp(utype, "Note") == 0) {
  779. char *id = xs_dict_get(object, "id");
  780. object_add_ow(id, object);
  781. snac_log(snac, xs_fmt("updated post %s", id));
  782. }
  783. else
  784. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  785. }
  786. else
  787. if (strcmp(type, "Delete") == 0) {
  788. if (xs_type(object) == XSTYPE_DICT)
  789. object = xs_dict_get(object, "id");
  790. if (valid_status(timeline_del(snac, object)))
  791. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  792. else
  793. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  794. }
  795. else
  796. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  797. if (do_notify)
  798. notify(snac, type, utype, actor, msg);
  799. return 1;
  800. }
  801. int send_email(char *msg)
  802. /* invoke sendmail with email headers and body in msg */
  803. {
  804. FILE *f;
  805. int status;
  806. int fds[2];
  807. pid_t pid;
  808. if (pipe(fds) == -1) return -1;
  809. pid = vfork();
  810. if (pid == -1) return -1;
  811. else if (pid == 0) {
  812. dup2(fds[0], 0);
  813. close(fds[0]);
  814. close(fds[1]);
  815. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  816. _exit(1);
  817. }
  818. close(fds[0]);
  819. if ((f = fdopen(fds[1], "w")) == NULL) {
  820. close(fds[1]);
  821. return -1;
  822. }
  823. fprintf(f, "%s\n", msg);
  824. fclose(f);
  825. if (waitpid(pid, &status, 0) == -1) return -1;
  826. return status;
  827. }
  828. void process_queue(snac *snac)
  829. /* processes the queue */
  830. {
  831. xs *list;
  832. char *p, *fn;
  833. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  834. list = queue(snac);
  835. p = list;
  836. while (xs_list_iter(&p, &fn)) {
  837. xs *q_item = dequeue(snac, fn);
  838. char *type;
  839. if (q_item == NULL) {
  840. snac_log(snac, xs_fmt("process_queue q_item error"));
  841. continue;
  842. }
  843. if ((type = xs_dict_get(q_item, "type")) == NULL)
  844. type = "output";
  845. if (strcmp(type, "message") == 0) {
  846. char *msg = xs_dict_get(q_item, "message");
  847. xs *inboxes = inbox_list(snac, msg);
  848. char *p, *v;
  849. p = inboxes;
  850. while (xs_list_iter(&p, &v)) {
  851. enqueue_output(snac, msg, v, 0);
  852. }
  853. }
  854. else
  855. if (strcmp(type, "output") == 0) {
  856. int status;
  857. char *inbox = xs_dict_get(q_item, "inbox");
  858. char *msg = xs_dict_get(q_item, "object");
  859. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  860. xs *payload = NULL;
  861. int p_size = 0;
  862. if (xs_is_null(inbox) || xs_is_null(msg))
  863. continue;
  864. /* deliver */
  865. status = send_to_inbox(snac, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  866. snac_log(snac, xs_fmt("process_queue sent to inbox %s %d", inbox, status));
  867. if (!valid_status(status)) {
  868. /* error sending; requeue? */
  869. if (status == 404 || status == 410)
  870. /* explicit error: discard */
  871. snac_log(snac, xs_fmt("process_queue error %s %d", inbox, status));
  872. else
  873. if (retries > queue_retry_max)
  874. snac_log(snac, xs_fmt("process_queue giving up %s %d", inbox, status));
  875. else {
  876. /* requeue */
  877. enqueue_output(snac, msg, inbox, retries + 1);
  878. snac_log(snac, xs_fmt("process_queue requeue %s #%d", inbox, retries + 1));
  879. }
  880. }
  881. }
  882. else
  883. if (strcmp(type, "input") == 0) {
  884. /* process the message */
  885. char *msg = xs_dict_get(q_item, "object");
  886. char *req = xs_dict_get(q_item, "req");
  887. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  888. if (!process_message(snac, msg, req)) {
  889. if (retries > queue_retry_max)
  890. snac_log(snac, xs_fmt("process_queue input giving up"));
  891. else {
  892. /* reenqueue */
  893. enqueue_input(snac, msg, req, retries + 1);
  894. snac_log(snac, xs_fmt("process_queue input requeue #%d", retries + 1));
  895. }
  896. }
  897. }
  898. else
  899. if (strcmp(type, "email") == 0) {
  900. /* send this email */
  901. char *msg = xs_dict_get(q_item, "message");
  902. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  903. if (!send_email(msg))
  904. snac_debug(snac, 1, xs_fmt("email message sent"));
  905. else {
  906. if (retries > queue_retry_max)
  907. snac_log(snac, xs_fmt("process_queue email giving up (errno: %d)", errno));
  908. else {
  909. /* requeue */
  910. snac_log(snac, xs_fmt(
  911. "process_queue email requeue #%d (errno: %d)", retries + 1, errno));
  912. enqueue_email(snac, msg, retries + 1);
  913. }
  914. }
  915. }
  916. }
  917. }
  918. void post(snac *snac, char *msg)
  919. /* enqueues a message to all its recipients */
  920. {
  921. xs *inboxes = inbox_list(snac, msg);
  922. char *p, *v;
  923. p = inboxes;
  924. while (xs_list_iter(&p, &v)) {
  925. enqueue_output(snac, msg, v, 0);
  926. }
  927. }
  928. /** HTTP handlers */
  929. int activitypub_get_handler(d_char *req, char *q_path,
  930. char **body, int *b_size, char **ctype)
  931. {
  932. int status = 200;
  933. char *accept = xs_dict_get(req, "accept");
  934. snac snac;
  935. xs *msg = NULL;
  936. if (accept == NULL)
  937. return 0;
  938. if (xs_str_in(accept, "application/activity+json") == -1 &&
  939. xs_str_in(accept, "application/ld+json") == -1)
  940. return 0;
  941. xs *l = xs_split_n(q_path, "/", 2);
  942. char *uid, *p_path;
  943. uid = xs_list_get(l, 1);
  944. if (!user_open(&snac, uid)) {
  945. /* invalid user */
  946. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  947. return 404;
  948. }
  949. p_path = xs_list_get(l, 2);
  950. *ctype = "application/activity+json";
  951. if (p_path == NULL) {
  952. /* if there was no component after the user, it's an actor request */
  953. msg = msg_actor(&snac);
  954. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  955. snac_debug(&snac, 1, xs_fmt("serving actor"));
  956. }
  957. else
  958. if (strcmp(p_path, "outbox") == 0) {
  959. xs *id = xs_fmt("%s/outbox", snac.actor);
  960. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  961. xs *list = xs_list_new();
  962. msg = msg_collection(&snac, id);
  963. char *p, *v;
  964. p = elems;
  965. while (xs_list_iter(&p, &v)) {
  966. xs *i = NULL;
  967. if (valid_status(object_get_by_md5(v, &i, NULL))) {
  968. char *type = xs_dict_get(i, "type");
  969. char *id = xs_dict_get(i, "id");
  970. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  971. i = xs_dict_del(i, "_snac");
  972. list = xs_list_append(list, i);
  973. }
  974. }
  975. }
  976. /* replace the 'orderedItems' with the latest posts */
  977. xs *items = xs_number_new(xs_list_len(list));
  978. msg = xs_dict_set(msg, "orderedItems", list);
  979. msg = xs_dict_set(msg, "totalItems", items);
  980. }
  981. else
  982. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  983. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  984. msg = msg_collection(&snac, id);
  985. }
  986. else
  987. if (xs_startswith(p_path, "p/")) {
  988. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  989. status = object_get(id, &msg, NULL);
  990. }
  991. else
  992. status = 404;
  993. if (status == 200 && msg != NULL) {
  994. *body = xs_json_dumps_pp(msg, 4);
  995. *b_size = strlen(*body);
  996. }
  997. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  998. user_free(&snac);
  999. return status;
  1000. }
  1001. int activitypub_post_handler(d_char *req, char *q_path,
  1002. d_char *payload, int p_size,
  1003. char **body, int *b_size, char **ctype)
  1004. /* processes an input message */
  1005. {
  1006. int status = 202; /* accepted */
  1007. char *i_ctype = xs_dict_get(req, "content-type");
  1008. snac snac;
  1009. char *v;
  1010. if (i_ctype == NULL)
  1011. return 400;
  1012. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1013. xs_str_in(i_ctype, "application/ld+json") == -1)
  1014. return 0;
  1015. /* decode the message */
  1016. xs *msg = xs_json_loads(payload);
  1017. if (msg == NULL) {
  1018. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1019. status = 400;
  1020. }
  1021. /* get the user and path */
  1022. xs *l = xs_split_n(q_path, "/", 2);
  1023. char *uid;
  1024. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1025. /* strange q_path */
  1026. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1027. return 404;
  1028. }
  1029. uid = xs_list_get(l, 1);
  1030. if (!user_open(&snac, uid)) {
  1031. /* invalid user */
  1032. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1033. return 404;
  1034. }
  1035. /* if it has a digest, check it now, because
  1036. later the payload won't be exactly the same */
  1037. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1038. xs *s1 = xs_sha256_base64(payload, p_size);
  1039. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1040. if (strcmp(s2, v) != 0) {
  1041. srv_log(xs_fmt("digest check FAILED"));
  1042. status = 400;
  1043. }
  1044. }
  1045. if (valid_status(status)) {
  1046. enqueue_input(&snac, msg, req, 0);
  1047. *ctype = "application/activity+json";
  1048. }
  1049. user_free(&snac);
  1050. return status;
  1051. }