activitypub.c 31 KB

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