activitypub.c 37 KB

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