activitypub.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 "snac.h"
  9. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  10. int activitypub_request(snac *snac, char *url, d_char **data)
  11. /* request an object */
  12. {
  13. int status;
  14. xs *response = NULL;
  15. xs *payload = NULL;
  16. int p_size;
  17. char *ctype;
  18. /* check if it's an url for this same site */
  19. /* ... */
  20. /* get from the net */
  21. response = http_signed_request(snac, "GET", url,
  22. NULL, NULL, 0, &status, &payload, &p_size);
  23. if (valid_status(status)) {
  24. /* ensure it's ActivityPub data */
  25. ctype = xs_dict_get(response, "content-type");
  26. if (xs_str_in(ctype, "application/activity+json") != -1)
  27. *data = xs_json_loads(payload);
  28. else
  29. status = 500;
  30. }
  31. if (!valid_status(status))
  32. *data = NULL;
  33. return status;
  34. }
  35. int actor_request(snac *snac, char *actor, d_char **data)
  36. /* request an actor */
  37. {
  38. int status, status2;
  39. xs *payload = NULL;
  40. /* get from disk first */
  41. status = actor_get(snac, actor, data);
  42. if (status == 200)
  43. return status;
  44. /* actor data non-existent or stale: get from the net */
  45. status2 = activitypub_request(snac, actor, &payload);
  46. if (valid_status(status2)) {
  47. /* renew data */
  48. status = actor_add(snac, actor, payload);
  49. *data = payload;
  50. payload = NULL;
  51. }
  52. return status;
  53. }
  54. void timeline_request(snac *snac, char *id, char *referrer)
  55. /* ensures that an entry and its ancestors are in the timeline */
  56. {
  57. if (!xs_is_null(id)) {
  58. /* is the admired object already there? */
  59. if (!timeline_here(snac, id)) {
  60. int status;
  61. xs *object = NULL;
  62. /* no; download it */
  63. status = activitypub_request(snac, id, &object);
  64. if (valid_status(status)) {
  65. /* does it have an ancestor? */
  66. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  67. /* recurse! */
  68. timeline_request(snac, in_reply_to, referrer);
  69. /* finally store */
  70. timeline_add(snac, id, object, in_reply_to, referrer);
  71. }
  72. }
  73. }
  74. }
  75. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  76. /* sends a message to an Inbox */
  77. {
  78. int status;
  79. d_char *response;
  80. xs *j_msg = xs_json_dumps_pp(msg, 4);
  81. response = http_signed_request(snac, "POST", inbox,
  82. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  83. free(response);
  84. return status;
  85. }
  86. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  87. /* sends a message to an actor */
  88. {
  89. int status;
  90. xs *data = NULL;
  91. /* resolve the actor first */
  92. status = actor_request(snac, actor, &data);
  93. if (valid_status(status)) {
  94. char *inbox = xs_dict_get(data, "inbox");
  95. if (inbox != NULL)
  96. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  97. else
  98. status = 400;
  99. }
  100. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  101. return status;
  102. }
  103. /** messages **/
  104. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date)
  105. /* creates a base ActivityPub message */
  106. {
  107. d_char *msg = xs_dict_new();
  108. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  109. msg = xs_dict_append(msg, "type", type);
  110. if (id != NULL)
  111. msg = xs_dict_append(msg, "id", id);
  112. if (actor != NULL)
  113. msg = xs_dict_append(msg, "actor", actor);
  114. if (date != NULL) {
  115. xs *published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  116. msg = xs_dict_append(msg, "published", published);
  117. }
  118. return msg;
  119. }
  120. d_char *msg_collection(snac *snac, char *id)
  121. /* creates an empty OrderedCollection message */
  122. {
  123. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL);
  124. xs *ol = xs_list_new();
  125. xs *nz = xs_number_new(0);
  126. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  127. msg = xs_dict_append(msg, "orderedItems", ol);
  128. msg = xs_dict_append(msg, "totalItems", nz);
  129. return msg;
  130. }
  131. d_char *msg_update(snac *snac, char *object)
  132. /* creates an Update message */
  133. {
  134. xs *id = xs_fmt("%s/Update", xs_dict_get(object, "id"));
  135. d_char *msg = msg_base(snac, "Update", id, snac->actor, "");
  136. msg = xs_dict_append(msg, "to", public_address);
  137. msg = xs_dict_append(msg, "object", object);
  138. return msg;
  139. }
  140. d_char *msg_admiration(snac *snac, char *object, char *type)
  141. /* creates a Like or Announce message */
  142. {
  143. xs *a_msg = NULL;
  144. d_char *msg = NULL;
  145. /* call the object */
  146. timeline_request(snac, object, snac->actor);
  147. if ((a_msg = timeline_find(snac, object)) != NULL) {
  148. xs *ntid = tid(0);
  149. xs *id = xs_fmt("%s/d/%d/%s", snac->actor, ntid, type);
  150. xs *rcpts = xs_list_new();
  151. msg = msg_base(snac, type, id, snac->actor, "");
  152. rcpts = xs_list_append(rcpts, public_address);
  153. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  154. msg = xs_dict_append(msg, "to", rcpts);
  155. msg = xs_dict_append(msg, "object", object);
  156. }
  157. else
  158. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  159. return msg;
  160. }
  161. d_char *msg_actor(snac *snac)
  162. /* create a Person message for this actor */
  163. {
  164. xs *ctxt = xs_list_new();
  165. xs *icon = xs_dict_new();
  166. xs *keys = xs_dict_new();
  167. xs *avtr = NULL;
  168. xs *kid = NULL;
  169. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL);
  170. char *p;
  171. int n;
  172. /* change the @context (is this really necessary?) */
  173. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  174. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  175. msg = xs_dict_set(msg, "@context", ctxt);
  176. msg = xs_dict_set(msg, "url", snac->actor);
  177. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  178. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  179. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  180. msg = xs_dict_set(msg, "summary", xs_dict_get(snac->config, "bio"));
  181. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  182. for (n = 0; folders[n]; n++) {
  183. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  184. msg = xs_dict_set(msg, folders[n], f);
  185. }
  186. p = xs_dict_get(snac->config, "avatar");
  187. if (*p == '\0')
  188. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  189. else
  190. avtr = xs_dup(p);
  191. icon = xs_dict_append(icon, "type", "Image");
  192. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  193. icon = xs_dict_append(icon, "url", avtr);
  194. msg = xs_dict_set(msg, "icon", icon);
  195. kid = xs_fmt("%s#main-key", snac->actor);
  196. keys = xs_dict_append(keys, "id", kid);
  197. keys = xs_dict_append(keys, "owner", snac->actor);
  198. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  199. msg = xs_dict_set(msg, "publicKey", keys);
  200. return msg;
  201. }
  202. /** queues **/
  203. void process_message(snac *snac, char *msg, char *req)
  204. /* processes an ActivityPub message from the input queue */
  205. {
  206. /* actor and type exist, were checked previously */
  207. char *actor = xs_dict_get(msg, "actor");
  208. char *type = xs_dict_get(msg, "type");
  209. char *object, *utype;
  210. object = xs_dict_get(msg, "object");
  211. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  212. utype = xs_dict_get(object, "type");
  213. else
  214. utype = "(null)";
  215. /* check the signature */
  216. /* ... */
  217. /*
  218. if (strcmp(type, "Follow") == 0) {
  219. }
  220. else
  221. if (strcmp(type, "Undo") == 0) {
  222. }
  223. else
  224. */
  225. if (strcmp(type, "Create") == 0) {
  226. if (strcmp(utype, "Note") == 0) {
  227. if (is_muted(snac, actor))
  228. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  229. else {
  230. char *id = xs_dict_get(object, "id");
  231. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  232. timeline_request(snac, in_reply_to, NULL);
  233. if (timeline_add(snac, id, msg, in_reply_to, NULL))
  234. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  235. }
  236. }
  237. else
  238. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  239. }
  240. else
  241. /*
  242. if (strcmp(type, "Accept") == 0) {
  243. }
  244. else
  245. */
  246. if (strcmp(type, "Like") == 0) {
  247. if (xs_type(object) == XSTYPE_DICT)
  248. object = xs_dict_get(object, "id");
  249. timeline_admire(snac, object, actor, 1);
  250. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  251. }
  252. else
  253. if (strcmp(type, "Announce") == 0) {
  254. if (xs_type(object) == XSTYPE_DICT)
  255. object = xs_dict_get(object, "id");
  256. timeline_request(snac, object, actor);
  257. timeline_admire(snac, object, actor, 0);
  258. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  259. }
  260. /*
  261. else
  262. if (strcmp(type, "Update") == 0) {
  263. }
  264. else
  265. if (strcmp(type, "Delete") == 0) {
  266. }
  267. */
  268. else
  269. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  270. }
  271. void process_queue(snac *snac)
  272. /* processes the queue */
  273. {
  274. xs *list;
  275. char *p, *fn;
  276. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  277. list = queue(snac);
  278. p = list;
  279. while (xs_list_iter(&p, &fn)) {
  280. xs *q_item = dequeue(snac, fn);
  281. char *type;
  282. if (q_item == NULL) {
  283. snac_log(snac, xs_fmt("process_queue q_item error"));
  284. continue;
  285. }
  286. if ((type = xs_dict_get(q_item, "type")) == NULL)
  287. type = "output";
  288. if (strcmp(type, "output") == 0) {
  289. int status;
  290. char *actor = xs_dict_get(q_item, "actor");
  291. char *msg = xs_dict_get(q_item, "object");
  292. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  293. xs *payload = NULL;
  294. int p_size = 0;
  295. /* deliver */
  296. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  297. if (!valid_status(status)) {
  298. /* error sending; reenqueue? */
  299. if (retries > queue_retry_max)
  300. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  301. else {
  302. /* reenqueue */
  303. enqueue_output(snac, msg, actor, retries + 1);
  304. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  305. }
  306. }
  307. }
  308. else
  309. if (strcmp(type, "input") == 0) {
  310. /* process the message */
  311. char *msg = xs_dict_get(q_item, "object");
  312. char *req = xs_dict_get(q_item, "req");
  313. process_message(snac, msg, req);
  314. }
  315. }
  316. }
  317. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  318. /* returns the list of recipients for a message */
  319. {
  320. d_char *list = xs_list_new();
  321. char *to = xs_dict_get(msg, "to");
  322. char *cc = xs_dict_get(msg, "cc");
  323. int n;
  324. char *lists[] = { to, cc, NULL };
  325. for (n = 0; lists[n]; n++) {
  326. char *l = lists[n];
  327. char *v;
  328. while (xs_list_iter(&l, &v)) {
  329. if (expand_public && strcmp(v, public_address) == 0) {
  330. /* iterate the followers and add them */
  331. xs *fwers = follower_list(snac);
  332. char *fw;
  333. char *p = fwers;
  334. while (xs_list_iter(&p, &fw)) {
  335. char *actor = xs_dict_get(fw, "actor");
  336. if (xs_list_in(list, actor) == -1)
  337. list = xs_list_append(list, actor);
  338. }
  339. }
  340. else
  341. if (xs_list_in(list, v) == -1)
  342. list = xs_list_append(list, v);
  343. }
  344. }
  345. return list;
  346. }
  347. int is_msg_public(snac *snac, char *msg)
  348. /* checks if a message is public */
  349. {
  350. int ret = 0;
  351. xs *rcpts = recipient_list(snac, msg, 0);
  352. char *p, *v;
  353. p = rcpts;
  354. while (!ret && xs_list_iter(&p, &v)) {
  355. if (strcmp(v, public_address) == 0)
  356. ret = 1;
  357. }
  358. return ret;
  359. }
  360. void post(snac *snac, char *msg)
  361. /* enqueues a message to all its recipients */
  362. {
  363. xs *rcpts = recipient_list(snac, msg, 1);
  364. char *p, *v;
  365. p = rcpts;
  366. while (xs_list_iter(&p, &v)) {
  367. enqueue_output(snac, msg, v, 0);
  368. }
  369. }
  370. /** HTTP handlers */
  371. int activitypub_get_handler(d_char *req, char *q_path,
  372. char **body, int *b_size, char **ctype)
  373. {
  374. int status = 200;
  375. char *accept = xs_dict_get(req, "accept");
  376. snac snac;
  377. xs *msg = NULL;
  378. if (accept == NULL)
  379. return 400;
  380. if (xs_str_in(accept, "application/activity+json") == -1 &&
  381. xs_str_in(accept, "application/ld+json") == -1)
  382. return 0;
  383. xs *l = xs_split_n(q_path, "/", 2);
  384. char *uid, *p_path;
  385. uid = xs_list_get(l, 1);
  386. if (!user_open(&snac, uid)) {
  387. /* invalid user */
  388. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  389. return 404;
  390. }
  391. p_path = xs_list_get(l, 2);
  392. *ctype = "application/activity+json";
  393. if (p_path == NULL) {
  394. /* if there was no component after the user, it's an actor request */
  395. msg = msg_actor(&snac);
  396. *ctype = "application/ld+json";
  397. }
  398. else
  399. if (strcmp(p_path, "outbox") == 0) {
  400. xs *id = xs_fmt("%s/outbox", snac.actor);
  401. msg = msg_collection(&snac, id);
  402. /* replace the 'orderedItems' with the latest posts */
  403. /* ... */
  404. }
  405. else
  406. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  407. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  408. msg = msg_collection(&snac, id);
  409. }
  410. else
  411. if (xs_startswith(p_path, "p/")) {
  412. }
  413. else
  414. status = 404;
  415. if (status == 200 && msg != NULL) {
  416. *body = xs_json_dumps_pp(msg, 4);
  417. *b_size = strlen(*body);
  418. }
  419. user_free(&snac);
  420. return status;
  421. }
  422. int activitypub_post_handler(d_char *req, char *q_path,
  423. d_char *payload, int p_size,
  424. char **body, int *b_size, char **ctype)
  425. /* processes an input message */
  426. {
  427. int status = 202; /* accepted */
  428. char *i_ctype = xs_dict_get(req, "content-type");
  429. snac snac;
  430. if (i_ctype == NULL)
  431. return 400;
  432. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  433. xs_str_in(i_ctype, "application/ld+json") == -1)
  434. return 0;
  435. /* decode the message */
  436. xs *msg = xs_json_loads(payload);
  437. if (msg == NULL) {
  438. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  439. status = 400;
  440. }
  441. /* get the user and path */
  442. xs *l = xs_split_n(q_path, "/", 2);
  443. char *uid;
  444. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  445. /* strange q_path */
  446. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  447. return 404;
  448. }
  449. uid = xs_list_get(l, 1);
  450. if (!user_open(&snac, uid)) {
  451. /* invalid user */
  452. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  453. return 404;
  454. }
  455. enqueue_input(&snac, msg, req);
  456. user_free(&snac);
  457. if (valid_status(status))
  458. *ctype = "application/activity+json";
  459. return status;
  460. }