activitypub.c 33 KB

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