activitypub.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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)
  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);
  69. /* finally store */
  70. timeline_add(snac, id, object, in_reply_to);
  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_SOD)
  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);
  212. if (timeline_add(snac, id, msg, in_reply_to))
  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_STRING) {
  227. timeline_admire(snac, object, actor, 1);
  228. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  229. }
  230. else
  231. snac_debug(snac, 2, xs_fmt("xs_type for 'Like' object not string"));
  232. }
  233. else
  234. if (strcmp(type, "Announce") == 0) {
  235. if (xs_type(object) == XSTYPE_STRING) {
  236. timeline_request(snac, object);
  237. timeline_admire(snac, object, actor, 0);
  238. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  239. }
  240. else
  241. snac_debug(snac, 2, xs_fmt("xs_type for 'Announce' object not string"));
  242. }
  243. /*
  244. else
  245. if (strcmp(type, "Update") == 0) {
  246. }
  247. else
  248. if (strcmp(type, "Delete") == 0) {
  249. }
  250. */
  251. else
  252. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  253. }
  254. void process_queue(snac *snac)
  255. /* processes the queue */
  256. {
  257. xs *list;
  258. char *p, *fn;
  259. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  260. list = queue(snac);
  261. p = list;
  262. while (xs_list_iter(&p, &fn)) {
  263. xs *q_item = dequeue(snac, fn);
  264. char *type;
  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. int activitypub_get_handler(d_char *req, char *q_path,
  295. char **body, int *b_size, char **ctype)
  296. {
  297. int status = 200;
  298. char *accept = xs_dict_get(req, "accept");
  299. snac snac;
  300. xs *msg = NULL;
  301. if (accept == NULL)
  302. return 400;
  303. if (xs_str_in(accept, "application/activity+json") == -1 &&
  304. xs_str_in(accept, "application/ld+json") == -1)
  305. return 0;
  306. xs *l = xs_split_n(q_path, "/", 2);
  307. char *uid, *p_path;
  308. uid = xs_list_get(l, 1);
  309. if (!user_open(&snac, uid)) {
  310. /* invalid user */
  311. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  312. return 404;
  313. }
  314. p_path = xs_list_get(l, 2);
  315. if (p_path == NULL) {
  316. /* if there was no component after the user, it's an actor request */
  317. msg = msg_actor(&snac);
  318. }
  319. else
  320. if (strcmp(p_path, "outbox") == 0) {
  321. xs *id = xs_fmt("%s/outbox", snac.actor);
  322. msg = msg_collection(&snac, id);
  323. /* replace the 'orderedItems' with the latest posts */
  324. /* ... */
  325. }
  326. else
  327. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  328. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  329. msg = msg_collection(&snac, id);
  330. }
  331. else
  332. if (xs_startswith(p_path, "p/")) {
  333. }
  334. else
  335. status = 404;
  336. if (status == 200 && msg != NULL) {
  337. *body = xs_json_dumps_pp(msg, 4);
  338. *b_size = strlen(*body);
  339. }
  340. user_free(&snac);
  341. return status;
  342. }
  343. int activitypub_post_handler(d_char *req, char *q_path,
  344. d_char *payload, int p_size,
  345. char **body, int *b_size, char **ctype)
  346. /* processes an input message */
  347. {
  348. int status = 202; /* accepted */
  349. char *i_ctype = xs_dict_get(req, "content-type");
  350. snac snac;
  351. if (i_ctype == NULL)
  352. return 400;
  353. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  354. xs_str_in(i_ctype, "application/ld+json") == -1)
  355. return 0;
  356. /* decode the message */
  357. xs *msg = xs_json_loads(payload);
  358. if (msg == NULL) {
  359. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  360. status = 400;
  361. }
  362. /* get the user and path */
  363. xs *l = xs_split_n(q_path, "/", 2);
  364. char *uid;
  365. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  366. /* strange q_path */
  367. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  368. return 404;
  369. }
  370. uid = xs_list_get(l, 1);
  371. if (!user_open(&snac, uid)) {
  372. /* invalid user */
  373. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  374. return 404;
  375. }
  376. enqueue_input(&snac, msg, req);
  377. user_free(&snac);
  378. if (valid_status(status))
  379. *ctype = "application/activity+json";
  380. return status;
  381. }