activitypub.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. /** messages **/
  86. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date)
  87. /* creates a base ActivityPub message */
  88. {
  89. d_char *msg = xs_dict_new();
  90. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  91. msg = xs_dict_append(msg, "type", type);
  92. if (id != NULL)
  93. msg = xs_dict_append(msg, "id", id);
  94. if (actor != NULL)
  95. msg = xs_dict_append(msg, "actor", actor);
  96. if (date != NULL) {
  97. xs *published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  98. msg = xs_dict_append(msg, "published", published);
  99. }
  100. return msg;
  101. }
  102. d_char *msg_collection(snac *snac, char *id)
  103. /* creates an empty OrderedCollection message */
  104. {
  105. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL);
  106. xs *ol = xs_list_new();
  107. xs *nz = xs_number_new(0);
  108. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  109. msg = xs_dict_append(msg, "orderedItems", ol);
  110. msg = xs_dict_append(msg, "totalItems", nz);
  111. return msg;
  112. }
  113. d_char *msg_update(snac *snac, char *object)
  114. /* creates an Update message */
  115. {
  116. xs *id = xs_fmt("%s/Update", xs_dict_get(object, "id"));
  117. d_char *msg = msg_base(snac, "Update", id, snac->actor, "");
  118. msg = xs_dict_append(msg, "to", public_address);
  119. msg = xs_dict_append(msg, "object", object);
  120. return msg;
  121. }
  122. /** queues **/
  123. void process_message(snac *snac, char *msg, char *req)
  124. /* processes an ActivityPub message from the input queue */
  125. {
  126. /* actor and type exist, were checked previously */
  127. char *actor = xs_dict_get(msg, "actor");
  128. char *type = xs_dict_get(msg, "type");
  129. char *object, *utype;
  130. object = xs_dict_get(msg, "object");
  131. if (object != NULL && xs_type(object) == XSTYPE_SOD)
  132. utype = xs_dict_get(object, "type");
  133. else
  134. utype = "(null)";
  135. /* check the signature */
  136. /* ... */
  137. /*
  138. if (strcmp(type, "Follow") == 0) {
  139. }
  140. else
  141. if (strcmp(type, "Undo") == 0) {
  142. }
  143. else
  144. */
  145. if (strcmp(type, "Create") == 0) {
  146. if (strcmp(utype, "Note") == 0) {
  147. if (is_muted(snac, actor))
  148. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  149. else {
  150. char *id = xs_dict_get(object, "id");
  151. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  152. if (xs_is_null(in_reply_to)) {
  153. /* recursively download ancestors */
  154. /* ... */
  155. }
  156. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  157. timeline_add(snac, id, msg, in_reply_to);
  158. }
  159. }
  160. else
  161. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  162. }
  163. else
  164. /*
  165. if (strcmp(type, "Accept") == 0) {
  166. }
  167. else
  168. */
  169. if (strcmp(type, "Like") == 0) {
  170. if (xs_type(object) == XSTYPE_STRING)
  171. timeline_admire(snac, object, actor, 1);
  172. else
  173. snac_debug(snac, 2, xs_fmt("xs_type for 'Like' object not string"));
  174. }
  175. else
  176. /*
  177. || strcmp(type, "Announce") == 0) {
  178. }
  179. else
  180. if (strcmp(type, "Update") == 0) {
  181. }
  182. else
  183. if (strcmp(type, "Delete") == 0) {
  184. }
  185. else
  186. */
  187. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  188. }
  189. void process_queue(snac *snac)
  190. /* processes the queue */
  191. {
  192. xs *list;
  193. char *p, *fn;
  194. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  195. list = queue(snac);
  196. p = list;
  197. while (xs_list_iter(&p, &fn)) {
  198. xs *q_item = dequeue(snac, fn);
  199. char *type;
  200. if ((type = xs_dict_get(q_item, "type")) == NULL)
  201. type = "output";
  202. if (strcmp(type, "output") == 0) {
  203. int status;
  204. char *actor = xs_dict_get(q_item, "actor");
  205. char *msg = xs_dict_get(q_item, "object");
  206. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  207. /* deliver */
  208. status = send_to_actor(snac, actor, msg, NULL, 0);
  209. if (!valid_status(status)) {
  210. /* error sending; reenqueue? */
  211. if (retries > queue_retry_max)
  212. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  213. else {
  214. /* reenqueue */
  215. enqueue_output(snac, msg, actor, retries + 1);
  216. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  217. }
  218. }
  219. }
  220. else
  221. if (strcmp(type, "input") == 0) {
  222. /* process the message */
  223. char *msg = xs_dict_get(q_item, "object");
  224. char *req = xs_dict_get(q_item, "req");
  225. process_message(snac, msg, req);
  226. }
  227. }
  228. }
  229. int activitypub_get_handler(d_char *req, char *q_path,
  230. char **body, int *b_size, char **ctype)
  231. {
  232. int status = 200;
  233. char *accept = xs_dict_get(req, "accept");
  234. snac snac;
  235. xs *msg = NULL;
  236. if (accept == NULL)
  237. return 400;
  238. if (xs_str_in(accept, "application/activity+json") == -1 &&
  239. xs_str_in(accept, "application/ld+json") == -1)
  240. return 0;
  241. xs *l = xs_split_n(q_path, "/", 2);
  242. char *uid, *p_path;
  243. uid = xs_list_get(l, 1);
  244. if (!user_open(&snac, uid)) {
  245. /* invalid user */
  246. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  247. return 404;
  248. }
  249. p_path = xs_list_get(l, 2);
  250. if (p_path == NULL) {
  251. /* if there was no component after the user, it's an actor request */
  252. }
  253. else
  254. if (strcmp(p_path, "outbox") == 0) {
  255. xs *id = xs_fmt("%s/outbox", snac.actor);
  256. msg = msg_collection(&snac, id);
  257. /* replace the 'orderedItems' with the latest posts */
  258. /* ... */
  259. }
  260. else
  261. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  262. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  263. msg = msg_collection(&snac, id);
  264. }
  265. else
  266. if (xs_startswith(p_path, "p/")) {
  267. }
  268. else
  269. status = 404;
  270. if (status == 200 && msg != NULL) {
  271. *body = xs_json_dumps_pp(msg, 4);
  272. *b_size = strlen(*body);
  273. }
  274. user_free(&snac);
  275. return status;
  276. }
  277. int activitypub_post_handler(d_char *req, char *q_path,
  278. d_char *payload, int p_size,
  279. char **body, int *b_size, char **ctype)
  280. /* processes an input message */
  281. {
  282. int status = 202; /* accepted */
  283. char *i_ctype = xs_dict_get(req, "content-type");
  284. snac snac;
  285. if (i_ctype == NULL)
  286. return 400;
  287. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  288. xs_str_in(i_ctype, "application/ld+json") == -1)
  289. return 0;
  290. /* decode the message */
  291. xs *msg = xs_json_loads(payload);
  292. if (msg == NULL) {
  293. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  294. status = 400;
  295. }
  296. /* get the user and path */
  297. xs *l = xs_split_n(q_path, "/", 2);
  298. char *uid;
  299. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  300. /* strange q_path */
  301. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  302. return 404;
  303. }
  304. uid = xs_list_get(l, 1);
  305. if (!user_open(&snac, uid)) {
  306. /* invalid user */
  307. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  308. return 404;
  309. }
  310. enqueue_input(&snac, msg, req);
  311. user_free(&snac);
  312. if (valid_status(status))
  313. *ctype = "application/activity+json";
  314. return status;
  315. }