activitypub.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. char *object = NULL;
  479. /* no email address? done */
  480. if (xs_is_null(email) || *email == '\0')
  481. return;
  482. if (strcmp(type, "Create") == 0) {
  483. /* only notify of notes specifically for us */
  484. char *rcpts = recipient_list(snac, msg, 0);
  485. if (xs_list_in(rcpts, snac->actor) == -1)
  486. return;
  487. }
  488. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  489. return;
  490. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  491. object = xs_dict_get(msg, "object");
  492. if (xs_is_null(object))
  493. return;
  494. else {
  495. if (xs_type(object) == XSTYPE_DICT)
  496. object = xs_dict_get(object, "id");
  497. /* if it's not an admiration about something by us, done */
  498. if (xs_is_null(object) || !xs_startswith(object, snac->actor))
  499. return;
  500. }
  501. }
  502. snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor));
  503. /* prepare message */
  504. xs *subject = xs_fmt("snac notify for @%s@%s",
  505. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  506. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  507. xs *header = xs_fmt(
  508. "From: %s\n"
  509. "To: %s\n"
  510. "Subject: %s\n"
  511. "\n",
  512. from, email, subject);
  513. xs *body = xs_str_new(header);
  514. if (strcmp(utype, "(null)") != 0) {
  515. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  516. body = xs_str_cat(body, s1);
  517. }
  518. else {
  519. xs *s1 = xs_fmt("Type : %s\n", type);
  520. body = xs_str_cat(body, s1);
  521. }
  522. {
  523. xs *s1 = xs_fmt("Actor : %s\n", actor);
  524. body = xs_str_cat(body, s1);
  525. }
  526. if (object != NULL) {
  527. xs *s1 = xs_fmt("Object: %s\n", object);
  528. body = xs_str_cat(body, s1);
  529. }
  530. enqueue_email(snac, body, 0);
  531. }
  532. /** queues **/
  533. int process_message(snac *snac, char *msg, char *req)
  534. /* processes an ActivityPub message from the input queue */
  535. {
  536. /* actor and type exist, were checked previously */
  537. char *actor = xs_dict_get(msg, "actor");
  538. char *type = xs_dict_get(msg, "type");
  539. xs *actor_o = NULL;
  540. int a_status;
  541. int do_notify = 0;
  542. char *object, *utype;
  543. object = xs_dict_get(msg, "object");
  544. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  545. utype = xs_dict_get(object, "type");
  546. else
  547. utype = "(null)";
  548. /* bring the actor */
  549. a_status = actor_request(snac, actor, &actor_o);
  550. /* if the actor does not explicitly exist, discard */
  551. if (a_status == 404 || a_status == 410) {
  552. snac_debug(snac, 1,
  553. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  554. return 1;
  555. }
  556. if (!valid_status(a_status)) {
  557. /* other actor download errors may need a retry */
  558. snac_debug(snac, 1,
  559. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  560. return 0;
  561. }
  562. /* check the signature */
  563. if (!check_signature(snac, req)) {
  564. snac_log(snac, xs_fmt("bad signature"));
  565. return 1;
  566. }
  567. if (strcmp(type, "Follow") == 0) {
  568. xs *f_msg = xs_dup(msg);
  569. xs *reply = msg_accept(snac, f_msg, actor);
  570. post(snac, reply);
  571. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  572. /* add a date if it doesn't include one (Mastodon) */
  573. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  574. f_msg = xs_dict_set(f_msg, "published", date);
  575. }
  576. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg, NULL, NULL);
  577. follower_add(snac, actor, f_msg);
  578. snac_log(snac, xs_fmt("New follower %s", actor));
  579. do_notify = 1;
  580. }
  581. else
  582. if (strcmp(type, "Undo") == 0) {
  583. if (strcmp(utype, "Follow") == 0) {
  584. if (valid_status(follower_del(snac, actor))) {
  585. snac_log(snac, xs_fmt("no longer following us %s", actor));
  586. do_notify = 1;
  587. }
  588. else
  589. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  590. }
  591. else
  592. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  593. }
  594. else
  595. if (strcmp(type, "Create") == 0) {
  596. if (strcmp(utype, "Note") == 0) {
  597. if (is_muted(snac, actor))
  598. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  599. else {
  600. char *id = xs_dict_get(object, "id");
  601. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  602. timeline_request(snac, in_reply_to, NULL);
  603. if (timeline_add(snac, id, object, in_reply_to, NULL)) {
  604. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  605. do_notify = 1;
  606. }
  607. }
  608. }
  609. else
  610. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  611. }
  612. else
  613. if (strcmp(type, "Accept") == 0) {
  614. if (strcmp(utype, "Follow") == 0) {
  615. if (following_check(snac, actor)) {
  616. following_add(snac, actor, msg);
  617. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  618. }
  619. else
  620. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  621. }
  622. else
  623. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  624. }
  625. else
  626. if (strcmp(type, "Like") == 0) {
  627. if (xs_type(object) == XSTYPE_DICT)
  628. object = xs_dict_get(object, "id");
  629. timeline_admire(snac, object, actor, 1);
  630. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  631. do_notify = 1;
  632. }
  633. else
  634. if (strcmp(type, "Announce") == 0) {
  635. xs *a_msg = NULL;
  636. if (xs_type(object) == XSTYPE_DICT)
  637. object = xs_dict_get(object, "id");
  638. timeline_request(snac, object, actor);
  639. if ((a_msg = timeline_find(snac, object)) != NULL) {
  640. char *who = xs_dict_get(a_msg, "attributedTo");
  641. if (who && !is_muted(snac, who)) {
  642. /* bring the actor */
  643. xs *who_o = NULL;
  644. if (valid_status(actor_request(snac, who, &who_o))) {
  645. timeline_admire(snac, object, actor, 0);
  646. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  647. do_notify = 1;
  648. }
  649. else
  650. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  651. }
  652. else
  653. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  654. }
  655. else
  656. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  657. }
  658. else
  659. if (strcmp(type, "Update") == 0) {
  660. if (strcmp(utype, "Person") == 0) {
  661. actor_add(snac, actor, xs_dict_get(msg, "object"));
  662. snac_log(snac, xs_fmt("updated actor %s", actor));
  663. }
  664. else
  665. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  666. }
  667. else
  668. if (strcmp(type, "Delete") == 0) {
  669. if (xs_type(object) == XSTYPE_DICT)
  670. object = xs_dict_get(object, "id");
  671. if (valid_status(timeline_del(snac, object)))
  672. snac_log(snac, xs_fmt("New 'Delete' %s %s", actor, object));
  673. else
  674. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  675. }
  676. else
  677. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  678. if (do_notify)
  679. notify(snac, type, utype, actor, msg);
  680. return 1;
  681. }
  682. void process_queue(snac *snac)
  683. /* processes the queue */
  684. {
  685. xs *list;
  686. char *p, *fn;
  687. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  688. list = queue(snac);
  689. p = list;
  690. while (xs_list_iter(&p, &fn)) {
  691. xs *q_item = dequeue(snac, fn);
  692. char *type;
  693. if (q_item == NULL) {
  694. snac_log(snac, xs_fmt("process_queue q_item error"));
  695. continue;
  696. }
  697. if ((type = xs_dict_get(q_item, "type")) == NULL)
  698. type = "output";
  699. if (strcmp(type, "output") == 0) {
  700. int status;
  701. char *actor = xs_dict_get(q_item, "actor");
  702. char *msg = xs_dict_get(q_item, "object");
  703. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  704. xs *payload = NULL;
  705. int p_size = 0;
  706. /* deliver */
  707. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  708. if (!valid_status(status)) {
  709. /* error sending; reenqueue? */
  710. if (retries > queue_retry_max)
  711. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  712. else {
  713. /* reenqueue */
  714. enqueue_output(snac, msg, actor, retries + 1);
  715. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  716. }
  717. }
  718. }
  719. else
  720. if (strcmp(type, "input") == 0) {
  721. /* process the message */
  722. char *msg = xs_dict_get(q_item, "object");
  723. char *req = xs_dict_get(q_item, "req");
  724. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  725. if (!process_message(snac, msg, req)) {
  726. if (retries > queue_retry_max)
  727. snac_log(snac, xs_fmt("process_queue input giving up"));
  728. else {
  729. /* reenqueue */
  730. enqueue_input(snac, msg, req, retries + 1);
  731. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  732. }
  733. }
  734. }
  735. else
  736. if (strcmp(type, "email") == 0) {
  737. /* send this email */
  738. char *msg = xs_dict_get(q_item, "message");
  739. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  740. FILE *f;
  741. int ok = 0;
  742. if ((f = popen("/usr/sbin/sendmail -t", "w")) != NULL) {
  743. fprintf(f, "%s\n", msg);
  744. if (fclose(f) != EOF)
  745. ok = 1;
  746. }
  747. if (ok)
  748. snac_debug(snac, 1, xs_fmt("email message sent"));
  749. else {
  750. if (retries > queue_retry_max)
  751. snac_log(snac, xs_fmt("process_queue email giving up (errno: %d)", errno));
  752. else {
  753. /* requeue */
  754. snac_log(snac, xs_fmt(
  755. "process_queue email requeue %d (errno: %d)", retries + 1, errno));
  756. enqueue_email(snac, msg, retries + 1);
  757. }
  758. }
  759. }
  760. }
  761. }
  762. void post(snac *snac, char *msg)
  763. /* enqueues a message to all its recipients */
  764. {
  765. xs *rcpts = recipient_list(snac, msg, 1);
  766. char *p, *v;
  767. p = rcpts;
  768. while (xs_list_iter(&p, &v)) {
  769. enqueue_output(snac, msg, v, 0);
  770. }
  771. }
  772. /** HTTP handlers */
  773. int activitypub_get_handler(d_char *req, char *q_path,
  774. char **body, int *b_size, char **ctype)
  775. {
  776. int status = 200;
  777. char *accept = xs_dict_get(req, "accept");
  778. snac snac;
  779. xs *msg = NULL;
  780. if (accept == NULL)
  781. return 0;
  782. if (xs_str_in(accept, "application/activity+json") == -1 &&
  783. xs_str_in(accept, "application/ld+json") == -1)
  784. return 0;
  785. xs *l = xs_split_n(q_path, "/", 2);
  786. char *uid, *p_path;
  787. uid = xs_list_get(l, 1);
  788. if (!user_open(&snac, uid)) {
  789. /* invalid user */
  790. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  791. return 404;
  792. }
  793. p_path = xs_list_get(l, 2);
  794. *ctype = "application/activity+json";
  795. if (p_path == NULL) {
  796. /* if there was no component after the user, it's an actor request */
  797. msg = msg_actor(&snac);
  798. *ctype = "application/ld+json";
  799. }
  800. else
  801. if (strcmp(p_path, "outbox") == 0) {
  802. xs *id = xs_fmt("%s/outbox", snac.actor);
  803. xs *elems = local_list(&snac, 20);
  804. xs *list = xs_list_new();
  805. msg = msg_collection(&snac, id);
  806. char *p, *v;
  807. p = elems;
  808. while (xs_list_iter(&p, &v)) {
  809. xs *i = timeline_get(&snac, v);
  810. char *type = xs_dict_get(i, "type");
  811. char *id = xs_dict_get(i, "id");
  812. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  813. i = xs_dict_del(i, "_snac");
  814. list = xs_list_append(list, i);
  815. }
  816. }
  817. /* replace the 'orderedItems' with the latest posts */
  818. msg = xs_dict_set(msg, "orderedItems", list);
  819. msg = xs_dict_set(msg, "totalItems", xs_number_new(xs_list_len(list)));
  820. }
  821. else
  822. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  823. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  824. msg = msg_collection(&snac, id);
  825. }
  826. else
  827. if (xs_startswith(p_path, "p/")) {
  828. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  829. if ((msg = timeline_find(&snac, id)) != NULL)
  830. msg = xs_dict_del(msg, "_snac");
  831. else
  832. status = 404;
  833. }
  834. else
  835. status = 404;
  836. if (status == 200 && msg != NULL) {
  837. *body = xs_json_dumps_pp(msg, 4);
  838. *b_size = strlen(*body);
  839. }
  840. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  841. user_free(&snac);
  842. return status;
  843. }
  844. int activitypub_post_handler(d_char *req, char *q_path,
  845. d_char *payload, int p_size,
  846. char **body, int *b_size, char **ctype)
  847. /* processes an input message */
  848. {
  849. int status = 202; /* accepted */
  850. char *i_ctype = xs_dict_get(req, "content-type");
  851. snac snac;
  852. char *v;
  853. if (i_ctype == NULL)
  854. return 400;
  855. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  856. xs_str_in(i_ctype, "application/ld+json") == -1)
  857. return 0;
  858. /* decode the message */
  859. xs *msg = xs_json_loads(payload);
  860. if (msg == NULL) {
  861. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  862. status = 400;
  863. }
  864. /* get the user and path */
  865. xs *l = xs_split_n(q_path, "/", 2);
  866. char *uid;
  867. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  868. /* strange q_path */
  869. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  870. return 404;
  871. }
  872. uid = xs_list_get(l, 1);
  873. if (!user_open(&snac, uid)) {
  874. /* invalid user */
  875. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  876. return 404;
  877. }
  878. /* if it has a digest, check it now, because
  879. later the payload won't be exactly the same */
  880. if ((v = xs_dict_get(req, "digest")) != NULL) {
  881. xs *s1 = xs_sha256_base64(payload, p_size);
  882. xs *s2 = xs_fmt("SHA-256=%s", s1);
  883. if (strcmp(s2, v) != 0) {
  884. srv_log(xs_fmt("digest check FAILED"));
  885. status = 400;
  886. }
  887. }
  888. if (valid_status(status)) {
  889. enqueue_input(&snac, msg, req, 0);
  890. *ctype = "application/activity+json";
  891. }
  892. user_free(&snac);
  893. return status;
  894. }