activitypub.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_actor(snac *snac)
  141. /* create a Person message for this actor */
  142. {
  143. xs *ctxt = xs_list_new();
  144. xs *icon = xs_dict_new();
  145. xs *keys = xs_dict_new();
  146. xs *avtr = NULL;
  147. xs *kid = NULL;
  148. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL);
  149. char *p;
  150. int n;
  151. /* change the @context (is this really necessary?) */
  152. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  153. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  154. msg = xs_dict_set(msg, "@context", ctxt);
  155. msg = xs_dict_set(msg, "url", snac->actor);
  156. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  157. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  158. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  159. msg = xs_dict_set(msg, "summary", xs_dict_get(snac->config, "bio"));
  160. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  161. for (n = 0; folders[n]; n++) {
  162. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  163. msg = xs_dict_set(msg, folders[n], f);
  164. }
  165. p = xs_dict_get(snac->config, "avatar");
  166. if (*p == '\0')
  167. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  168. else
  169. avtr = xs_dup(p);
  170. icon = xs_dict_append(icon, "type", "Image");
  171. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  172. icon = xs_dict_append(icon, "url", avtr);
  173. msg = xs_dict_set(msg, "icon", icon);
  174. kid = xs_fmt("%s#main-key", snac->actor);
  175. keys = xs_dict_append(keys, "id", kid);
  176. keys = xs_dict_append(keys, "owner", snac->actor);
  177. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  178. msg = xs_dict_set(msg, "publicKey", keys);
  179. return msg;
  180. }
  181. /** queues **/
  182. void process_message(snac *snac, char *msg, char *req)
  183. /* processes an ActivityPub message from the input queue */
  184. {
  185. /* actor and type exist, were checked previously */
  186. char *actor = xs_dict_get(msg, "actor");
  187. char *type = xs_dict_get(msg, "type");
  188. char *object, *utype;
  189. object = xs_dict_get(msg, "object");
  190. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  191. utype = xs_dict_get(object, "type");
  192. else
  193. utype = "(null)";
  194. /* check the signature */
  195. /* ... */
  196. /*
  197. if (strcmp(type, "Follow") == 0) {
  198. }
  199. else
  200. if (strcmp(type, "Undo") == 0) {
  201. }
  202. else
  203. */
  204. if (strcmp(type, "Create") == 0) {
  205. if (strcmp(utype, "Note") == 0) {
  206. if (is_muted(snac, actor))
  207. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  208. else {
  209. char *id = xs_dict_get(object, "id");
  210. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  211. timeline_request(snac, in_reply_to, NULL);
  212. if (timeline_add(snac, id, msg, in_reply_to, NULL))
  213. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  214. }
  215. }
  216. else
  217. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  218. }
  219. else
  220. /*
  221. if (strcmp(type, "Accept") == 0) {
  222. }
  223. else
  224. */
  225. if (strcmp(type, "Like") == 0) {
  226. if (xs_type(object) == XSTYPE_DICT)
  227. object = xs_dict_get(object, "id");
  228. timeline_admire(snac, object, actor, 1);
  229. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  230. }
  231. else
  232. if (strcmp(type, "Announce") == 0) {
  233. if (xs_type(object) == XSTYPE_DICT)
  234. object = xs_dict_get(object, "id");
  235. timeline_request(snac, object, actor);
  236. timeline_admire(snac, object, actor, 0);
  237. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  238. }
  239. /*
  240. else
  241. if (strcmp(type, "Update") == 0) {
  242. }
  243. else
  244. if (strcmp(type, "Delete") == 0) {
  245. }
  246. */
  247. else
  248. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  249. }
  250. void process_queue(snac *snac)
  251. /* processes the queue */
  252. {
  253. xs *list;
  254. char *p, *fn;
  255. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  256. list = queue(snac);
  257. p = list;
  258. while (xs_list_iter(&p, &fn)) {
  259. xs *q_item = dequeue(snac, fn);
  260. char *type;
  261. if (q_item == NULL) {
  262. snac_log(snac, xs_fmt("process_queue q_item error"));
  263. continue;
  264. }
  265. if ((type = xs_dict_get(q_item, "type")) == NULL)
  266. type = "output";
  267. if (strcmp(type, "output") == 0) {
  268. int status;
  269. char *actor = xs_dict_get(q_item, "actor");
  270. char *msg = xs_dict_get(q_item, "object");
  271. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  272. /* deliver */
  273. status = send_to_actor(snac, actor, msg, NULL, 0);
  274. if (!valid_status(status)) {
  275. /* error sending; reenqueue? */
  276. if (retries > queue_retry_max)
  277. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  278. else {
  279. /* reenqueue */
  280. enqueue_output(snac, msg, actor, retries + 1);
  281. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  282. }
  283. }
  284. }
  285. else
  286. if (strcmp(type, "input") == 0) {
  287. /* process the message */
  288. char *msg = xs_dict_get(q_item, "object");
  289. char *req = xs_dict_get(q_item, "req");
  290. process_message(snac, msg, req);
  291. }
  292. }
  293. }
  294. d_char *recipent_list(snac *snac, char *msg, int expand_public)
  295. /* returns the list of recipients for a message */
  296. {
  297. d_char *list = xs_list_new();
  298. char *to = xs_dict_get(msg, "to");
  299. char *cc = xs_dict_get(msg, "cc");
  300. int n;
  301. char *lists[] = { to, cc, NULL };
  302. for (n = 0; lists[n]; n++) {
  303. char *l = lists[n];
  304. char *v;
  305. while (xs_list_iter(&l, &v)) {
  306. if (expand_public && strcmp(v, public_address) == 0) {
  307. /* iterate the followers and add them */
  308. xs *fwers = follower_list(snac);
  309. char *fw;
  310. char *p = fwers;
  311. while (xs_list_iter(&p, &fw)) {
  312. if (!xs_list_in(list, fw))
  313. list = xs_list_append(list, fw);
  314. }
  315. }
  316. else
  317. if (!xs_list_in(list, v))
  318. list = xs_list_append(list, v);
  319. }
  320. }
  321. return list;
  322. }
  323. /** HTTP handlers */
  324. int activitypub_get_handler(d_char *req, char *q_path,
  325. char **body, int *b_size, char **ctype)
  326. {
  327. int status = 200;
  328. char *accept = xs_dict_get(req, "accept");
  329. snac snac;
  330. xs *msg = NULL;
  331. if (accept == NULL)
  332. return 400;
  333. if (xs_str_in(accept, "application/activity+json") == -1 &&
  334. xs_str_in(accept, "application/ld+json") == -1)
  335. return 0;
  336. xs *l = xs_split_n(q_path, "/", 2);
  337. char *uid, *p_path;
  338. uid = xs_list_get(l, 1);
  339. if (!user_open(&snac, uid)) {
  340. /* invalid user */
  341. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  342. return 404;
  343. }
  344. p_path = xs_list_get(l, 2);
  345. if (p_path == NULL) {
  346. /* if there was no component after the user, it's an actor request */
  347. msg = msg_actor(&snac);
  348. }
  349. else
  350. if (strcmp(p_path, "outbox") == 0) {
  351. xs *id = xs_fmt("%s/outbox", snac.actor);
  352. msg = msg_collection(&snac, id);
  353. /* replace the 'orderedItems' with the latest posts */
  354. /* ... */
  355. }
  356. else
  357. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  358. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  359. msg = msg_collection(&snac, id);
  360. }
  361. else
  362. if (xs_startswith(p_path, "p/")) {
  363. }
  364. else
  365. status = 404;
  366. if (status == 200 && msg != NULL) {
  367. *body = xs_json_dumps_pp(msg, 4);
  368. *b_size = strlen(*body);
  369. *ctype = "application/activity+json";
  370. }
  371. user_free(&snac);
  372. return status;
  373. }
  374. int activitypub_post_handler(d_char *req, char *q_path,
  375. d_char *payload, int p_size,
  376. char **body, int *b_size, char **ctype)
  377. /* processes an input message */
  378. {
  379. int status = 202; /* accepted */
  380. char *i_ctype = xs_dict_get(req, "content-type");
  381. snac snac;
  382. if (i_ctype == NULL)
  383. return 400;
  384. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  385. xs_str_in(i_ctype, "application/ld+json") == -1)
  386. return 0;
  387. /* decode the message */
  388. xs *msg = xs_json_loads(payload);
  389. if (msg == NULL) {
  390. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  391. status = 400;
  392. }
  393. /* get the user and path */
  394. xs *l = xs_split_n(q_path, "/", 2);
  395. char *uid;
  396. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  397. /* strange q_path */
  398. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  399. return 404;
  400. }
  401. uid = xs_list_get(l, 1);
  402. if (!user_open(&snac, uid)) {
  403. /* invalid user */
  404. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  405. return 404;
  406. }
  407. enqueue_input(&snac, msg, req);
  408. user_free(&snac);
  409. if (valid_status(status))
  410. *ctype = "application/activity+json";
  411. return status;
  412. }