activitypub.c 24 KB

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