activitypub.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 "snac.h"
  8. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  9. int activitypub_request(snac *snac, char *url, d_char **data)
  10. /* request an object */
  11. {
  12. int status;
  13. xs *response = NULL;
  14. xs *payload = NULL;
  15. int p_size;
  16. char *ctype;
  17. /* check if it's an url for this same site */
  18. /* ... */
  19. /* get from the net */
  20. response = http_signed_request(snac, "GET", url,
  21. NULL, NULL, 0, &status, &payload, &p_size);
  22. if (valid_status(status)) {
  23. if (dbglevel >= 3) {
  24. xs *j = xs_json_dumps_pp(response, 4);
  25. fprintf(stderr, "%s\n", j);
  26. }
  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. *data = xs_json_loads(payload);
  31. else
  32. status = 500;
  33. }
  34. if (!valid_status(status))
  35. *data = NULL;
  36. return status;
  37. }
  38. int actor_request(snac *snac, char *actor, d_char **data)
  39. /* request an actor */
  40. {
  41. int status, status2;
  42. xs *payload = NULL;
  43. /* get from disk first */
  44. status = actor_get(snac, actor, data);
  45. if (status == 200)
  46. return status;
  47. /* actor data non-existent or stale: get from the net */
  48. status2 = activitypub_request(snac, actor, &payload);
  49. if (valid_status(status2)) {
  50. /* renew data */
  51. status = actor_add(snac, actor, payload);
  52. *data = payload;
  53. payload = NULL;
  54. }
  55. return status;
  56. }
  57. void timeline_request(snac *snac, char *id)
  58. /* ensures that an entry and its ancestors are in the timeline */
  59. {
  60. if (!xs_is_null(id)) {
  61. /* is the admired object already there? */
  62. if (!timeline_here(snac, id)) {
  63. int status;
  64. xs *object = NULL;
  65. /* no; download it */
  66. status = activitypub_request(snac, id, &object);
  67. if (valid_status(status)) {
  68. /* does it have an ancestor? */
  69. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  70. /* recurse! */
  71. timeline_request(snac, in_reply_to);
  72. /* finally store */
  73. timeline_add(snac, id, object, in_reply_to);
  74. }
  75. }
  76. }
  77. }
  78. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  79. /* sends a message to an Inbox */
  80. {
  81. int status;
  82. d_char *response;
  83. xs *j_msg = xs_json_dumps_pp(msg, 4);
  84. response = http_signed_request(snac, "POST", inbox,
  85. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  86. free(response);
  87. return status;
  88. }
  89. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  90. /* sends a message to an actor */
  91. {
  92. int status;
  93. xs *data = NULL;
  94. /* resolve the actor first */
  95. status = actor_request(snac, actor, &data);
  96. if (valid_status(status)) {
  97. char *inbox = xs_dict_get(data, "inbox");
  98. if (inbox != NULL)
  99. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  100. else
  101. status = 400;
  102. }
  103. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  104. return status;
  105. }
  106. /** messages **/
  107. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date)
  108. /* creates a base ActivityPub message */
  109. {
  110. d_char *msg = xs_dict_new();
  111. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  112. msg = xs_dict_append(msg, "type", type);
  113. if (id != NULL)
  114. msg = xs_dict_append(msg, "id", id);
  115. if (actor != NULL)
  116. msg = xs_dict_append(msg, "actor", actor);
  117. if (date != NULL) {
  118. xs *published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  119. msg = xs_dict_append(msg, "published", published);
  120. }
  121. return msg;
  122. }
  123. d_char *msg_collection(snac *snac, char *id)
  124. /* creates an empty OrderedCollection message */
  125. {
  126. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL);
  127. xs *ol = xs_list_new();
  128. xs *nz = xs_number_new(0);
  129. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  130. msg = xs_dict_append(msg, "orderedItems", ol);
  131. msg = xs_dict_append(msg, "totalItems", nz);
  132. return msg;
  133. }
  134. d_char *msg_update(snac *snac, char *object)
  135. /* creates an Update message */
  136. {
  137. xs *id = xs_fmt("%s/Update", xs_dict_get(object, "id"));
  138. d_char *msg = msg_base(snac, "Update", id, snac->actor, "");
  139. msg = xs_dict_append(msg, "to", public_address);
  140. msg = xs_dict_append(msg, "object", object);
  141. return msg;
  142. }
  143. /** queues **/
  144. void process_message(snac *snac, char *msg, char *req)
  145. /* processes an ActivityPub message from the input queue */
  146. {
  147. /* actor and type exist, were checked previously */
  148. char *actor = xs_dict_get(msg, "actor");
  149. char *type = xs_dict_get(msg, "type");
  150. char *object, *utype;
  151. object = xs_dict_get(msg, "object");
  152. if (object != NULL && xs_type(object) == XSTYPE_SOD)
  153. utype = xs_dict_get(object, "type");
  154. else
  155. utype = "(null)";
  156. /* check the signature */
  157. /* ... */
  158. /*
  159. if (strcmp(type, "Follow") == 0) {
  160. }
  161. else
  162. if (strcmp(type, "Undo") == 0) {
  163. }
  164. else
  165. */
  166. if (strcmp(type, "Create") == 0) {
  167. if (strcmp(utype, "Note") == 0) {
  168. if (is_muted(snac, actor))
  169. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  170. else {
  171. char *id = xs_dict_get(object, "id");
  172. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  173. timeline_request(snac, in_reply_to);
  174. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  175. timeline_add(snac, id, msg, in_reply_to);
  176. }
  177. }
  178. else
  179. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  180. }
  181. else
  182. /*
  183. if (strcmp(type, "Accept") == 0) {
  184. }
  185. else
  186. */
  187. if (strcmp(type, "Like") == 0) {
  188. if (xs_type(object) == XSTYPE_STRING)
  189. timeline_admire(snac, object, actor, 1);
  190. else
  191. snac_debug(snac, 2, xs_fmt("xs_type for 'Like' object not string"));
  192. }
  193. else
  194. if (strcmp(type, "Announce") == 0) {
  195. if (xs_type(object) == XSTYPE_STRING) {
  196. timeline_request(snac, object);
  197. timeline_admire(snac, object, actor, 0);
  198. }
  199. else
  200. snac_debug(snac, 2, xs_fmt("xs_type for 'Announce' object not string"));
  201. }
  202. /*
  203. else
  204. if (strcmp(type, "Update") == 0) {
  205. }
  206. else
  207. if (strcmp(type, "Delete") == 0) {
  208. }
  209. */
  210. else
  211. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  212. }
  213. void process_queue(snac *snac)
  214. /* processes the queue */
  215. {
  216. xs *list;
  217. char *p, *fn;
  218. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  219. list = queue(snac);
  220. p = list;
  221. while (xs_list_iter(&p, &fn)) {
  222. xs *q_item = dequeue(snac, fn);
  223. char *type;
  224. if ((type = xs_dict_get(q_item, "type")) == NULL)
  225. type = "output";
  226. if (strcmp(type, "output") == 0) {
  227. int status;
  228. char *actor = xs_dict_get(q_item, "actor");
  229. char *msg = xs_dict_get(q_item, "object");
  230. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  231. /* deliver */
  232. status = send_to_actor(snac, actor, msg, NULL, 0);
  233. if (!valid_status(status)) {
  234. /* error sending; reenqueue? */
  235. if (retries > queue_retry_max)
  236. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  237. else {
  238. /* reenqueue */
  239. enqueue_output(snac, msg, actor, retries + 1);
  240. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  241. }
  242. }
  243. }
  244. else
  245. if (strcmp(type, "input") == 0) {
  246. /* process the message */
  247. char *msg = xs_dict_get(q_item, "object");
  248. char *req = xs_dict_get(q_item, "req");
  249. process_message(snac, msg, req);
  250. }
  251. }
  252. }
  253. int activitypub_get_handler(d_char *req, char *q_path,
  254. char **body, int *b_size, char **ctype)
  255. {
  256. int status = 200;
  257. char *accept = xs_dict_get(req, "accept");
  258. snac snac;
  259. xs *msg = NULL;
  260. if (accept == NULL)
  261. return 400;
  262. if (xs_str_in(accept, "application/activity+json") == -1 &&
  263. xs_str_in(accept, "application/ld+json") == -1)
  264. return 0;
  265. xs *l = xs_split_n(q_path, "/", 2);
  266. char *uid, *p_path;
  267. uid = xs_list_get(l, 1);
  268. if (!user_open(&snac, uid)) {
  269. /* invalid user */
  270. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  271. return 404;
  272. }
  273. p_path = xs_list_get(l, 2);
  274. if (p_path == NULL) {
  275. /* if there was no component after the user, it's an actor request */
  276. }
  277. else
  278. if (strcmp(p_path, "outbox") == 0) {
  279. xs *id = xs_fmt("%s/outbox", snac.actor);
  280. msg = msg_collection(&snac, id);
  281. /* replace the 'orderedItems' with the latest posts */
  282. /* ... */
  283. }
  284. else
  285. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  286. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  287. msg = msg_collection(&snac, id);
  288. }
  289. else
  290. if (xs_startswith(p_path, "p/")) {
  291. }
  292. else
  293. status = 404;
  294. if (status == 200 && msg != NULL) {
  295. *body = xs_json_dumps_pp(msg, 4);
  296. *b_size = strlen(*body);
  297. }
  298. user_free(&snac);
  299. return status;
  300. }
  301. int activitypub_post_handler(d_char *req, char *q_path,
  302. d_char *payload, int p_size,
  303. char **body, int *b_size, char **ctype)
  304. /* processes an input message */
  305. {
  306. int status = 202; /* accepted */
  307. char *i_ctype = xs_dict_get(req, "content-type");
  308. snac snac;
  309. if (i_ctype == NULL)
  310. return 400;
  311. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  312. xs_str_in(i_ctype, "application/ld+json") == -1)
  313. return 0;
  314. /* decode the message */
  315. xs *msg = xs_json_loads(payload);
  316. if (msg == NULL) {
  317. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  318. status = 400;
  319. }
  320. /* get the user and path */
  321. xs *l = xs_split_n(q_path, "/", 2);
  322. char *uid;
  323. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  324. /* strange q_path */
  325. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  326. return 404;
  327. }
  328. uid = xs_list_get(l, 1);
  329. if (!user_open(&snac, uid)) {
  330. /* invalid user */
  331. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  332. return 404;
  333. }
  334. enqueue_input(&snac, msg, req);
  335. user_free(&snac);
  336. if (valid_status(status))
  337. *ctype = "application/activity+json";
  338. return status;
  339. }