activitypub.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  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 "xs_openssl.h"
  9. #include "snac.h"
  10. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  11. int activitypub_request(snac *snac, char *url, d_char **data)
  12. /* request an object */
  13. {
  14. int status;
  15. xs *response = NULL;
  16. xs *payload = NULL;
  17. int p_size;
  18. char *ctype;
  19. /* check if it's an url for this same site */
  20. /* ... */
  21. /* get from the net */
  22. response = http_signed_request(snac, "GET", url,
  23. NULL, NULL, 0, &status, &payload, &p_size);
  24. if (valid_status(status)) {
  25. /* ensure it's ActivityPub data */
  26. ctype = xs_dict_get(response, "content-type");
  27. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  28. xs_str_in(ctype, "application/ld+json") != -1)
  29. *data = xs_json_loads(payload);
  30. else
  31. status = 500;
  32. }
  33. if (!valid_status(status))
  34. *data = NULL;
  35. return status;
  36. }
  37. int actor_request(snac *snac, char *actor, d_char **data)
  38. /* request an actor */
  39. {
  40. int status, status2;
  41. xs *payload = NULL;
  42. /* get from disk first */
  43. status = actor_get(snac, actor, data);
  44. if (status == 200)
  45. return status;
  46. /* actor data non-existent or stale: get from the net */
  47. status2 = activitypub_request(snac, actor, &payload);
  48. if (valid_status(status2)) {
  49. /* renew data */
  50. status = actor_add(snac, actor, payload);
  51. *data = payload;
  52. payload = NULL;
  53. }
  54. return status;
  55. }
  56. int timeline_request(snac *snac, char *id, char *referrer)
  57. /* ensures that an entry and its ancestors are in the timeline */
  58. {
  59. int status = 0;
  60. if (!xs_is_null(id)) {
  61. /* is the admired object already there? */
  62. if (!timeline_here(snac, id)) {
  63. xs *object = NULL;
  64. /* no; download it */
  65. status = activitypub_request(snac, id, &object);
  66. if (valid_status(status)) {
  67. /* does it have an ancestor? */
  68. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  69. /* recurse! */
  70. timeline_request(snac, in_reply_to, referrer);
  71. /* finally store */
  72. timeline_add(snac, id, object, in_reply_to, referrer);
  73. }
  74. }
  75. }
  76. return status;
  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. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  107. /* returns the list of recipients for a message */
  108. {
  109. d_char *list = xs_list_new();
  110. char *to = xs_dict_get(msg, "to");
  111. char *cc = xs_dict_get(msg, "cc");
  112. int n;
  113. char *lists[] = { to, cc, NULL };
  114. for (n = 0; lists[n]; n++) {
  115. char *l = lists[n];
  116. char *v;
  117. if (xs_type(l) == XSTYPE_STRING) {
  118. if (xs_list_in(list, l) == -1)
  119. list = xs_list_append(list, l);
  120. }
  121. else
  122. while (xs_list_iter(&l, &v)) {
  123. if (expand_public && strcmp(v, public_address) == 0) {
  124. /* iterate the followers and add them */
  125. xs *fwers = follower_list(snac);
  126. char *fw;
  127. char *p = fwers;
  128. while (xs_list_iter(&p, &fw)) {
  129. char *actor = xs_dict_get(fw, "actor");
  130. if (xs_list_in(list, actor) == -1)
  131. list = xs_list_append(list, actor);
  132. }
  133. }
  134. else
  135. if (xs_list_in(list, v) == -1)
  136. list = xs_list_append(list, v);
  137. }
  138. }
  139. return list;
  140. }
  141. int is_msg_public(snac *snac, char *msg)
  142. /* checks if a message is public */
  143. {
  144. int ret = 0;
  145. xs *rcpts = recipient_list(snac, msg, 0);
  146. char *p, *v;
  147. p = rcpts;
  148. while (!ret && xs_list_iter(&p, &v)) {
  149. if (strcmp(v, public_address) == 0)
  150. ret = 1;
  151. }
  152. return ret;
  153. }
  154. /** messages **/
  155. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  156. /* creates a base ActivityPub message */
  157. {
  158. xs *did = NULL;
  159. xs *published = NULL;
  160. /* generated values */
  161. if (date && strcmp(date, "@now") == 0)
  162. date = published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  163. if (id != NULL) {
  164. if (strcmp(id, "@dummy") == 0) {
  165. xs *ntid = tid(0);
  166. id = did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  167. }
  168. else
  169. if (strcmp(id, "@object") == 0) {
  170. if (object != NULL)
  171. id = did = xs_fmt("%s/%s", xs_dict_get(object, "id"), type);
  172. else
  173. id = NULL;
  174. }
  175. }
  176. d_char *msg = xs_dict_new();
  177. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  178. msg = xs_dict_append(msg, "type", type);
  179. if (id != NULL)
  180. msg = xs_dict_append(msg, "id", id);
  181. if (actor != NULL)
  182. msg = xs_dict_append(msg, "actor", actor);
  183. if (date != NULL)
  184. msg = xs_dict_append(msg, "published", date);
  185. if (object != NULL)
  186. msg = xs_dict_append(msg, "object", object);
  187. return msg;
  188. }
  189. d_char *msg_collection(snac *snac, char *id)
  190. /* creates an empty OrderedCollection message */
  191. {
  192. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  193. xs *ol = xs_list_new();
  194. xs *nz = xs_number_new(0);
  195. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  196. msg = xs_dict_append(msg, "orderedItems", ol);
  197. msg = xs_dict_append(msg, "totalItems", nz);
  198. return msg;
  199. }
  200. d_char *msg_accept(snac *snac, char *object, char *to)
  201. /* creates an Accept message (as a response to a Follow) */
  202. {
  203. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  204. msg = xs_dict_append(msg, "to", to);
  205. return msg;
  206. }
  207. d_char *msg_update(snac *snac, char *object)
  208. /* creates an Update message */
  209. {
  210. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  211. msg = xs_dict_append(msg, "to", public_address);
  212. return msg;
  213. }
  214. d_char *msg_admiration(snac *snac, char *object, char *type)
  215. /* creates a Like or Announce message */
  216. {
  217. xs *a_msg = NULL;
  218. d_char *msg = NULL;
  219. /* call the object */
  220. timeline_request(snac, object, snac->actor);
  221. if ((a_msg = timeline_find(snac, object)) != NULL) {
  222. xs *rcpts = xs_list_new();
  223. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  224. rcpts = xs_list_append(rcpts, public_address);
  225. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  226. msg = xs_dict_append(msg, "to", rcpts);
  227. }
  228. else
  229. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  230. return msg;
  231. }
  232. d_char *msg_actor(snac *snac)
  233. /* create a Person message for this actor */
  234. {
  235. xs *ctxt = xs_list_new();
  236. xs *icon = xs_dict_new();
  237. xs *keys = xs_dict_new();
  238. xs *avtr = NULL;
  239. xs *kid = NULL;
  240. xs *f_bio = NULL;
  241. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  242. char *p;
  243. int n;
  244. /* change the @context (is this really necessary?) */
  245. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  246. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  247. msg = xs_dict_set(msg, "@context", ctxt);
  248. msg = xs_dict_set(msg, "url", snac->actor);
  249. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  250. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  251. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  252. not_really_markdown(xs_dict_get(snac->config, "bio"), &f_bio);
  253. msg = xs_dict_set(msg, "summary", f_bio);
  254. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  255. for (n = 0; folders[n]; n++) {
  256. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  257. msg = xs_dict_set(msg, folders[n], f);
  258. }
  259. p = xs_dict_get(snac->config, "avatar");
  260. if (*p == '\0')
  261. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  262. else
  263. avtr = xs_dup(p);
  264. icon = xs_dict_append(icon, "type", "Image");
  265. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  266. icon = xs_dict_append(icon, "url", avtr);
  267. msg = xs_dict_set(msg, "icon", icon);
  268. kid = xs_fmt("%s#main-key", snac->actor);
  269. keys = xs_dict_append(keys, "id", kid);
  270. keys = xs_dict_append(keys, "owner", snac->actor);
  271. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  272. msg = xs_dict_set(msg, "publicKey", keys);
  273. return msg;
  274. }
  275. d_char *msg_create(snac *snac, char *object)
  276. /* creates a 'Create' message */
  277. {
  278. d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  279. msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
  280. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  281. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  282. return msg;
  283. }
  284. d_char *msg_follow(snac *snac, char *actor)
  285. /* creates a 'Follow' message */
  286. {
  287. d_char *actor_o = NULL;
  288. d_char *msg = NULL;
  289. int status;
  290. /* request the actor */
  291. status = actor_request(snac, actor, &actor_o);
  292. if (valid_status(status)) {
  293. /* check if the actor is an alias */
  294. char *r_actor = xs_dict_get(actor_o, "id");
  295. if (r_actor && strcmp(actor, r_actor) != 0) {
  296. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  297. actor = r_actor;
  298. }
  299. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, actor);
  300. }
  301. else
  302. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  303. return msg;
  304. }
  305. d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to)
  306. /* creates a 'Note' message */
  307. {
  308. xs *ntid = tid(0);
  309. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  310. xs *ctxt = NULL;
  311. xs *fc1 = NULL;
  312. xs *to = NULL;
  313. xs *cc = xs_list_new();
  314. xs *irt = NULL;
  315. xs *tag = NULL;
  316. d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  317. char *p, *v;
  318. if (rcpts == NULL)
  319. to = xs_list_new();
  320. else
  321. to = xs_dup(rcpts);
  322. /* format the content */
  323. not_really_markdown(content, &fc1);
  324. if (in_reply_to != NULL) {
  325. xs *p_msg = NULL;
  326. /* demand this thing */
  327. timeline_request(snac, in_reply_to, NULL);
  328. if ((p_msg = timeline_find(snac, in_reply_to)) != NULL) {
  329. /* add this author as recipient */
  330. char *v;
  331. if ((v = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, v) == -1)
  332. to = xs_list_append(to, v);
  333. if ((v = xs_dict_get(p_msg, "context")))
  334. ctxt = xs_dup(v);
  335. /* if this message is public, ours will also be */
  336. if (is_msg_public(snac, p_msg) &&
  337. xs_list_in(to, (char *)public_address) == -1)
  338. to = xs_list_append(to, public_address);
  339. }
  340. irt = xs_dup(in_reply_to);
  341. }
  342. else
  343. irt = xs_val_new(XSTYPE_NULL);
  344. if (tag == NULL)
  345. tag = xs_list_new();
  346. if (ctxt == NULL)
  347. ctxt = xs_fmt("%s#ctxt", id);
  348. /* add all mentions to the cc */
  349. p = tag;
  350. while (xs_list_iter(&p, &v)) {
  351. if (xs_type(v) == XSTYPE_DICT) {
  352. char *t;
  353. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  354. if ((t = xs_dict_get(v, "href")) != NULL)
  355. cc = xs_list_append(cc, t);
  356. }
  357. }
  358. }
  359. /* no recipients? must be for everybody */
  360. if (xs_list_len(to) == 0)
  361. to = xs_list_append(to, public_address);
  362. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  363. msg = xs_dict_append(msg, "summary", "");
  364. msg = xs_dict_append(msg, "content", fc1);
  365. msg = xs_dict_append(msg, "context", ctxt);
  366. msg = xs_dict_append(msg, "url", id);
  367. msg = xs_dict_append(msg, "to", to);
  368. msg = xs_dict_append(msg, "cc", cc);
  369. msg = xs_dict_append(msg, "inReplyTo", irt);
  370. msg = xs_dict_append(msg, "tag", tag);
  371. return msg;
  372. }
  373. /** queues **/
  374. void process_message(snac *snac, char *msg, char *req)
  375. /* processes an ActivityPub message from the input queue */
  376. {
  377. /* actor and type exist, were checked previously */
  378. char *actor = xs_dict_get(msg, "actor");
  379. char *type = xs_dict_get(msg, "type");
  380. char *object, *utype;
  381. object = xs_dict_get(msg, "object");
  382. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  383. utype = xs_dict_get(object, "type");
  384. else
  385. utype = "(null)";
  386. /* check the signature */
  387. /* ... */
  388. if (strcmp(type, "Follow") == 0) {
  389. xs *reply = msg_accept(snac, msg, actor);
  390. post(snac, reply);
  391. timeline_add(snac, xs_dict_get(msg, "id"), msg, NULL, NULL);
  392. follower_add(snac, actor, msg);
  393. snac_log(snac, xs_fmt("New follower %s", actor));
  394. }
  395. else
  396. /*
  397. if (strcmp(type, "Undo") == 0) {
  398. }
  399. else
  400. */
  401. if (strcmp(type, "Create") == 0) {
  402. if (strcmp(utype, "Note") == 0) {
  403. if (is_muted(snac, actor))
  404. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  405. else {
  406. char *id = xs_dict_get(object, "id");
  407. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  408. timeline_request(snac, in_reply_to, NULL);
  409. if (timeline_add(snac, id, object, in_reply_to, NULL))
  410. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  411. }
  412. }
  413. else
  414. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  415. }
  416. else
  417. if (strcmp(type, "Accept") == 0) {
  418. if (strcmp(utype, "Follow") == 0) {
  419. if (following_check(snac, actor)) {
  420. following_add(snac, actor, msg);
  421. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  422. }
  423. else
  424. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  425. }
  426. else
  427. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  428. }
  429. else
  430. if (strcmp(type, "Like") == 0) {
  431. if (xs_type(object) == XSTYPE_DICT)
  432. object = xs_dict_get(object, "id");
  433. timeline_admire(snac, object, actor, 1);
  434. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  435. }
  436. else
  437. if (strcmp(type, "Announce") == 0) {
  438. if (xs_type(object) == XSTYPE_DICT)
  439. object = xs_dict_get(object, "id");
  440. timeline_request(snac, object, actor);
  441. timeline_admire(snac, object, actor, 0);
  442. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  443. }
  444. /*
  445. else
  446. if (strcmp(type, "Update") == 0) {
  447. }
  448. else
  449. if (strcmp(type, "Delete") == 0) {
  450. }
  451. */
  452. else
  453. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  454. }
  455. void process_queue(snac *snac)
  456. /* processes the queue */
  457. {
  458. xs *list;
  459. char *p, *fn;
  460. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  461. list = queue(snac);
  462. p = list;
  463. while (xs_list_iter(&p, &fn)) {
  464. xs *q_item = dequeue(snac, fn);
  465. char *type;
  466. if (q_item == NULL) {
  467. snac_log(snac, xs_fmt("process_queue q_item error"));
  468. continue;
  469. }
  470. if ((type = xs_dict_get(q_item, "type")) == NULL)
  471. type = "output";
  472. if (strcmp(type, "output") == 0) {
  473. int status;
  474. char *actor = xs_dict_get(q_item, "actor");
  475. char *msg = xs_dict_get(q_item, "object");
  476. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  477. xs *payload = NULL;
  478. int p_size = 0;
  479. /* deliver */
  480. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  481. if (!valid_status(status)) {
  482. /* error sending; reenqueue? */
  483. if (retries > queue_retry_max)
  484. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  485. else {
  486. /* reenqueue */
  487. enqueue_output(snac, msg, actor, retries + 1);
  488. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  489. }
  490. }
  491. }
  492. else
  493. if (strcmp(type, "input") == 0) {
  494. /* process the message */
  495. char *msg = xs_dict_get(q_item, "object");
  496. char *req = xs_dict_get(q_item, "req");
  497. process_message(snac, msg, req);
  498. }
  499. }
  500. }
  501. void post(snac *snac, char *msg)
  502. /* enqueues a message to all its recipients */
  503. {
  504. xs *rcpts = recipient_list(snac, msg, 1);
  505. char *p, *v;
  506. p = rcpts;
  507. while (xs_list_iter(&p, &v)) {
  508. enqueue_output(snac, msg, v, 0);
  509. }
  510. }
  511. /** HTTP handlers */
  512. int activitypub_get_handler(d_char *req, char *q_path,
  513. char **body, int *b_size, char **ctype)
  514. {
  515. int status = 200;
  516. char *accept = xs_dict_get(req, "accept");
  517. snac snac;
  518. xs *msg = NULL;
  519. if (accept == NULL)
  520. return 400;
  521. if (xs_str_in(accept, "application/activity+json") == -1 &&
  522. xs_str_in(accept, "application/ld+json") == -1)
  523. return 0;
  524. xs *l = xs_split_n(q_path, "/", 2);
  525. char *uid, *p_path;
  526. uid = xs_list_get(l, 1);
  527. if (!user_open(&snac, uid)) {
  528. /* invalid user */
  529. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  530. return 404;
  531. }
  532. p_path = xs_list_get(l, 2);
  533. *ctype = "application/activity+json";
  534. if (p_path == NULL) {
  535. /* if there was no component after the user, it's an actor request */
  536. msg = msg_actor(&snac);
  537. *ctype = "application/ld+json";
  538. }
  539. else
  540. if (strcmp(p_path, "outbox") == 0) {
  541. xs *id = xs_fmt("%s/outbox", snac.actor);
  542. msg = msg_collection(&snac, id);
  543. /* replace the 'orderedItems' with the latest posts */
  544. /* ... */
  545. }
  546. else
  547. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  548. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  549. msg = msg_collection(&snac, id);
  550. }
  551. else
  552. if (xs_startswith(p_path, "p/")) {
  553. }
  554. else
  555. status = 404;
  556. if (status == 200 && msg != NULL) {
  557. *body = xs_json_dumps_pp(msg, 4);
  558. *b_size = strlen(*body);
  559. }
  560. user_free(&snac);
  561. return status;
  562. }
  563. int activitypub_post_handler(d_char *req, char *q_path,
  564. d_char *payload, int p_size,
  565. char **body, int *b_size, char **ctype)
  566. /* processes an input message */
  567. {
  568. int status = 202; /* accepted */
  569. char *i_ctype = xs_dict_get(req, "content-type");
  570. snac snac;
  571. char *v;
  572. if (i_ctype == NULL)
  573. return 400;
  574. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  575. xs_str_in(i_ctype, "application/ld+json") == -1)
  576. return 0;
  577. /* decode the message */
  578. xs *msg = xs_json_loads(payload);
  579. if (msg == NULL) {
  580. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  581. status = 400;
  582. }
  583. /* get the user and path */
  584. xs *l = xs_split_n(q_path, "/", 2);
  585. char *uid;
  586. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  587. /* strange q_path */
  588. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  589. return 404;
  590. }
  591. uid = xs_list_get(l, 1);
  592. if (!user_open(&snac, uid)) {
  593. /* invalid user */
  594. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  595. return 404;
  596. }
  597. /* if it has a digest, check it now, because
  598. later the payload won't be exactly the same */
  599. if ((v = xs_dict_get(req, "digest")) != NULL) {
  600. xs *s1 = xs_sha256_base64(payload, p_size);
  601. xs *s2 = xs_fmt("SHA-256=%s", s1);
  602. if (strcmp(s2, v) == 0)
  603. srv_log(xs_fmt("digest check OK"));
  604. else
  605. srv_log(xs_fmt("digest check FAILED"));
  606. }
  607. enqueue_input(&snac, msg, req);
  608. user_free(&snac);
  609. if (valid_status(status))
  610. *ctype = "application/activity+json";
  611. return status;
  612. }