activitypub.c 36 KB

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