activitypub.c 34 KB

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