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