activitypub.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  58. /* sends a message to an Inbox */
  59. {
  60. int status;
  61. d_char *response;
  62. xs *j_msg = xs_json_dumps_pp(msg, 4);
  63. response = http_signed_request(snac, "POST", inbox,
  64. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  65. free(response);
  66. return status;
  67. }
  68. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  69. /* sends a message to an actor */
  70. {
  71. int status;
  72. xs *data = NULL;
  73. /* resolve the actor first */
  74. status = actor_request(snac, actor, &data);
  75. if (valid_status(status)) {
  76. char *inbox = xs_dict_get(data, "inbox");
  77. if (inbox != NULL)
  78. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  79. else
  80. status = 400;
  81. }
  82. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  83. return status;
  84. }
  85. void process_queue(snac *snac)
  86. /* processes the queue */
  87. {
  88. xs *list;
  89. char *p, *fn;
  90. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  91. list = queue(snac);
  92. p = list;
  93. while (xs_list_iter(&p, &fn)) {
  94. xs *q_item = dequeue(snac, fn);
  95. char *type;
  96. if ((type = xs_dict_get(q_item, "type")) == NULL)
  97. type = "output";
  98. if (strcmp(type, "output") == 0) {
  99. int status;
  100. char *actor = xs_dict_get(q_item, "actor");
  101. char *msg = xs_dict_get(q_item, "object");
  102. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  103. /* deliver */
  104. status = send_to_actor(snac, actor, msg, NULL, 0);
  105. if (!valid_status(status)) {
  106. /* error sending; reenqueue? */
  107. if (retries > queue_retry_max)
  108. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  109. else {
  110. /* reenqueue */
  111. enqueue_output(snac, actor, msg, retries + 1);
  112. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  113. }
  114. }
  115. }
  116. }
  117. }
  118. int activitypub_get_handler(d_char *req, char *q_path,
  119. char **body, int *b_size, char **ctype)
  120. {
  121. int status = 200;
  122. char *headers = xs_dict_get(req, "headers");
  123. char *accept = xs_dict_get(headers, "accept");
  124. snac snac;
  125. xs *msg = xs_dict_new();
  126. if (xs_str_in(accept, "application/activity+json") == -1 &&
  127. xs_str_in(accept, "application/ld+json") == -1)
  128. return 0;
  129. xs *l = xs_split_n(q_path, "/", 2);
  130. char *uid, *p_path;
  131. uid = xs_list_get(l, 1);
  132. if (!user_open(&snac, uid)) {
  133. /* invalid user */
  134. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  135. return 404;
  136. }
  137. p_path = xs_list_get(l, 2);
  138. if (p_path == NULL) {
  139. /* if there was no component after the user, it's an actor request */
  140. }
  141. else
  142. if (strcmp(p_path, "outbox") == 0) {
  143. }
  144. else
  145. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  146. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  147. xs *ol = xs_list_new();
  148. xs *nz = xs_number_new(0);
  149. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  150. msg = xs_dict_append(msg, "attributedTo", snac.actor);
  151. msg = xs_dict_append(msg, "id", id);
  152. msg = xs_dict_append(msg, "orderedItems", ol);
  153. msg = xs_dict_append(msg, "totalItems", nz);
  154. msg = xs_dict_append(msg, "type", "OrderedCollection");
  155. }
  156. else
  157. if (xs_startswith(p_path, "p/")) {
  158. }
  159. else
  160. status = 404;
  161. if (status == 200) {
  162. *body = xs_json_dumps_pp(msg, 4);
  163. *b_size = strlen(*body);
  164. }
  165. user_free(&snac);
  166. return status;
  167. }
  168. int activitypub_post_handler(d_char *req, char *q_path,
  169. d_char *payload, int p_size,
  170. char **body, int *b_size, char **ctype)
  171. /* processes an input message */
  172. {
  173. int status = 200;
  174. char *headers = xs_dict_get(req, "headers");
  175. char *i_ctype = xs_dict_get(headers, "content-type");
  176. snac snac;
  177. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  178. xs_str_in(i_ctype, "application/ld+json") == -1)
  179. return 0;
  180. xs *l = xs_split_n(q_path, "/", 2);
  181. char *uid;
  182. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  183. /* strange q_path */
  184. srv_log(xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  185. return 404;
  186. }
  187. uid = xs_list_get(l, 1);
  188. if (!user_open(&snac, uid)) {
  189. /* invalid user */
  190. srv_log(xs_fmt("activitypub_post_handler bad user %s", uid));
  191. return 404;
  192. }
  193. user_free(&snac);
  194. return status;
  195. }