activitypub.c 13 KB

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