activitypub.c 22 KB

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