activitypub.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. if (data != NULL) {
  53. *data = payload;
  54. payload = NULL;
  55. }
  56. }
  57. return status;
  58. }
  59. int timeline_request(snac *snac, char *id, char *referrer)
  60. /* ensures that an entry and its ancestors are in the timeline */
  61. {
  62. int status = 0;
  63. if (!xs_is_null(id)) {
  64. /* is the admired object already there? */
  65. if (!timeline_here(snac, id)) {
  66. xs *object = NULL;
  67. /* no; download it */
  68. status = activitypub_request(snac, id, &object);
  69. if (valid_status(status)) {
  70. char *actor = xs_dict_get(object, "actor");
  71. /* request (and drop) the actor for this entry */
  72. actor_request(snac, actor, NULL);
  73. /* does it have an ancestor? */
  74. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  75. /* recurse! */
  76. timeline_request(snac, in_reply_to, referrer);
  77. /* finally store */
  78. timeline_add(snac, id, object, in_reply_to, referrer);
  79. }
  80. }
  81. }
  82. return status;
  83. }
  84. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  85. /* sends a message to an Inbox */
  86. {
  87. int status;
  88. d_char *response;
  89. xs *j_msg = xs_json_dumps_pp(msg, 4);
  90. response = http_signed_request(snac, "POST", inbox,
  91. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  92. free(response);
  93. return status;
  94. }
  95. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  96. /* sends a message to an actor */
  97. {
  98. int status;
  99. xs *data = NULL;
  100. /* resolve the actor first */
  101. status = actor_request(snac, actor, &data);
  102. if (valid_status(status)) {
  103. char *inbox = xs_dict_get(data, "inbox");
  104. if (inbox != NULL)
  105. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  106. else
  107. status = 400;
  108. }
  109. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  110. return status;
  111. }
  112. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  113. /* returns the list of recipients for a message */
  114. {
  115. d_char *list = xs_list_new();
  116. char *to = xs_dict_get(msg, "to");
  117. char *cc = xs_dict_get(msg, "cc");
  118. int n;
  119. char *lists[] = { to, cc, NULL };
  120. for (n = 0; lists[n]; n++) {
  121. char *l = lists[n];
  122. char *v;
  123. xs *tl = NULL;
  124. /* if it's a string, create a list with only one element */
  125. if (xs_type(l) == XSTYPE_STRING) {
  126. tl = xs_list_new();
  127. tl = xs_list_append(tl, l);
  128. l = tl;
  129. }
  130. while (xs_list_iter(&l, &v)) {
  131. if (expand_public && strcmp(v, public_address) == 0) {
  132. /* iterate the followers and add them */
  133. xs *fwers = follower_list(snac);
  134. char *fw;
  135. char *p = fwers;
  136. while (xs_list_iter(&p, &fw)) {
  137. char *actor = xs_dict_get(fw, "actor");
  138. if (xs_list_in(list, actor) == -1)
  139. list = xs_list_append(list, actor);
  140. }
  141. }
  142. else
  143. if (xs_list_in(list, v) == -1)
  144. list = xs_list_append(list, v);
  145. }
  146. }
  147. return list;
  148. }
  149. int is_msg_public(snac *snac, char *msg)
  150. /* checks if a message is public */
  151. {
  152. int ret = 0;
  153. xs *rcpts = recipient_list(snac, msg, 0);
  154. char *p, *v;
  155. p = rcpts;
  156. while (!ret && xs_list_iter(&p, &v)) {
  157. if (strcmp(v, public_address) == 0)
  158. ret = 1;
  159. }
  160. return ret;
  161. }
  162. void process_tags(const char *content, d_char **n_content, d_char **tag)
  163. /* parses mentions and tags from content */
  164. {
  165. d_char *nc = xs_str_new(NULL);
  166. d_char *tl = xs_list_new();
  167. xs *split;
  168. char *p, *v;
  169. int n = 0;
  170. p = split = xs_regex_split(content, "(@[A-Za-z0-9_]+@[A-Za-z0-9\\.-]+|#[^ ,\\.:;]+)");
  171. while (xs_list_iter(&p, &v)) {
  172. if ((n & 0x1)) {
  173. if (*v == '@') {
  174. /* query the webfinger about this fellow */
  175. xs *actor = NULL;
  176. xs *uid = NULL;
  177. int status;
  178. status = webfinger_request(v + 1, &actor, &uid);
  179. if (valid_status(status)) {
  180. xs *d = xs_dict_new();
  181. xs *n = xs_fmt("@%s", uid);
  182. xs *l = xs_fmt("<a href=\"%s\">%s</a>", actor, n);
  183. d = xs_dict_append(d, "type", "Mention");
  184. d = xs_dict_append(d, "href", actor);
  185. d = xs_dict_append(d, "name", n);
  186. tl = xs_list_append(tl, d);
  187. /* add the code */
  188. nc = xs_str_cat(nc, l);
  189. }
  190. else
  191. /* store as is */
  192. nc = xs_str_cat(nc, v);
  193. }
  194. else
  195. if (*v == '#') {
  196. /* hashtag */
  197. /* store as is by now */
  198. nc = xs_str_cat(nc, v);
  199. }
  200. }
  201. else
  202. nc = xs_str_cat(nc, v);
  203. n++;
  204. }
  205. *n_content = nc;
  206. *tag = tl;
  207. }
  208. /** messages **/
  209. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  210. /* creates a base ActivityPub message */
  211. {
  212. xs *did = NULL;
  213. xs *published = NULL;
  214. /* generated values */
  215. if (date && strcmp(date, "@now") == 0)
  216. date = published = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  217. if (id != NULL) {
  218. if (strcmp(id, "@dummy") == 0) {
  219. xs *ntid = tid(0);
  220. id = did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  221. }
  222. else
  223. if (strcmp(id, "@object") == 0) {
  224. if (object != NULL)
  225. id = did = xs_fmt("%s/%s", xs_dict_get(object, "id"), type);
  226. else
  227. id = NULL;
  228. }
  229. }
  230. d_char *msg = xs_dict_new();
  231. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  232. msg = xs_dict_append(msg, "type", type);
  233. if (id != NULL)
  234. msg = xs_dict_append(msg, "id", id);
  235. if (actor != NULL)
  236. msg = xs_dict_append(msg, "actor", actor);
  237. if (date != NULL)
  238. msg = xs_dict_append(msg, "published", date);
  239. if (object != NULL)
  240. msg = xs_dict_append(msg, "object", object);
  241. return msg;
  242. }
  243. d_char *msg_collection(snac *snac, char *id)
  244. /* creates an empty OrderedCollection message */
  245. {
  246. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  247. xs *ol = xs_list_new();
  248. xs *nz = xs_number_new(0);
  249. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  250. msg = xs_dict_append(msg, "orderedItems", ol);
  251. msg = xs_dict_append(msg, "totalItems", nz);
  252. return msg;
  253. }
  254. d_char *msg_accept(snac *snac, char *object, char *to)
  255. /* creates an Accept message (as a response to a Follow) */
  256. {
  257. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  258. msg = xs_dict_append(msg, "to", to);
  259. return msg;
  260. }
  261. d_char *msg_update(snac *snac, char *object)
  262. /* creates an Update message */
  263. {
  264. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  265. msg = xs_dict_append(msg, "to", public_address);
  266. return msg;
  267. }
  268. d_char *msg_admiration(snac *snac, char *object, char *type)
  269. /* creates a Like or Announce message */
  270. {
  271. xs *a_msg = NULL;
  272. d_char *msg = NULL;
  273. /* call the object */
  274. timeline_request(snac, object, snac->actor);
  275. if ((a_msg = timeline_find(snac, object)) != NULL) {
  276. xs *rcpts = xs_list_new();
  277. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  278. rcpts = xs_list_append(rcpts, public_address);
  279. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  280. msg = xs_dict_append(msg, "to", rcpts);
  281. }
  282. else
  283. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  284. return msg;
  285. }
  286. d_char *msg_actor(snac *snac)
  287. /* create a Person message for this actor */
  288. {
  289. xs *ctxt = xs_list_new();
  290. xs *icon = xs_dict_new();
  291. xs *keys = xs_dict_new();
  292. xs *avtr = NULL;
  293. xs *kid = NULL;
  294. xs *f_bio = NULL;
  295. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  296. char *p;
  297. int n;
  298. /* change the @context (is this really necessary?) */
  299. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  300. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  301. msg = xs_dict_set(msg, "@context", ctxt);
  302. msg = xs_dict_set(msg, "url", snac->actor);
  303. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  304. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  305. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  306. not_really_markdown(xs_dict_get(snac->config, "bio"), &f_bio);
  307. msg = xs_dict_set(msg, "summary", f_bio);
  308. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  309. for (n = 0; folders[n]; n++) {
  310. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  311. msg = xs_dict_set(msg, folders[n], f);
  312. }
  313. p = xs_dict_get(snac->config, "avatar");
  314. if (*p == '\0')
  315. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  316. else
  317. avtr = xs_dup(p);
  318. icon = xs_dict_append(icon, "type", "Image");
  319. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  320. icon = xs_dict_append(icon, "url", avtr);
  321. msg = xs_dict_set(msg, "icon", icon);
  322. kid = xs_fmt("%s#main-key", snac->actor);
  323. keys = xs_dict_append(keys, "id", kid);
  324. keys = xs_dict_append(keys, "owner", snac->actor);
  325. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  326. msg = xs_dict_set(msg, "publicKey", keys);
  327. return msg;
  328. }
  329. d_char *msg_create(snac *snac, char *object)
  330. /* creates a 'Create' message */
  331. {
  332. d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  333. msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
  334. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  335. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  336. return msg;
  337. }
  338. d_char *msg_undo(snac *snac, char *object)
  339. /* creates an 'Undo' message */
  340. {
  341. d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  342. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  343. return msg;
  344. }
  345. d_char *msg_delete(snac *snac, char *id)
  346. /* creates a 'Delete' + 'Tombstone' for a local entry */
  347. {
  348. xs *tomb = xs_dict_new();
  349. d_char *msg = NULL;
  350. /* sculpt the tombstone */
  351. tomb = xs_dict_append(tomb, "type", "Tombstone");
  352. tomb = xs_dict_append(tomb, "id", id);
  353. /* now create the Delete */
  354. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  355. msg = xs_dict_append(msg, "to", public_address);
  356. return msg;
  357. }
  358. d_char *msg_follow(snac *snac, char *actor)
  359. /* creates a 'Follow' message */
  360. {
  361. d_char *actor_o = NULL;
  362. d_char *msg = NULL;
  363. int status;
  364. /* request the actor */
  365. status = actor_request(snac, actor, &actor_o);
  366. if (valid_status(status)) {
  367. /* check if the actor is an alias */
  368. char *r_actor = xs_dict_get(actor_o, "id");
  369. if (r_actor && strcmp(actor, r_actor) != 0) {
  370. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  371. actor = r_actor;
  372. }
  373. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, actor);
  374. }
  375. else
  376. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  377. return msg;
  378. }
  379. d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to)
  380. /* creates a 'Note' message */
  381. {
  382. xs *ntid = tid(0);
  383. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  384. xs *ctxt = NULL;
  385. xs *fc2 = NULL;
  386. xs *fc1 = NULL;
  387. xs *to = NULL;
  388. xs *cc = xs_list_new();
  389. xs *irt = NULL;
  390. xs *tag = NULL;
  391. d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  392. char *p, *v;
  393. if (rcpts == NULL)
  394. to = xs_list_new();
  395. else
  396. to = xs_dup(rcpts);
  397. /* format the content */
  398. not_really_markdown(content, &fc2);
  399. /* extract the tags */
  400. process_tags(fc2, &fc1, &tag);
  401. if (in_reply_to != NULL) {
  402. xs *p_msg = NULL;
  403. /* demand this thing */
  404. timeline_request(snac, in_reply_to, NULL);
  405. if ((p_msg = timeline_find(snac, in_reply_to)) != NULL) {
  406. /* add this author as recipient */
  407. char *v;
  408. if ((v = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, v) == -1)
  409. to = xs_list_append(to, v);
  410. if ((v = xs_dict_get(p_msg, "context")))
  411. ctxt = xs_dup(v);
  412. /* if this message is public, ours will also be */
  413. if (is_msg_public(snac, p_msg) &&
  414. xs_list_in(to, (char *)public_address) == -1)
  415. to = xs_list_append(to, public_address);
  416. }
  417. irt = xs_dup(in_reply_to);
  418. }
  419. else
  420. irt = xs_val_new(XSTYPE_NULL);
  421. if (tag == NULL)
  422. tag = xs_list_new();
  423. if (ctxt == NULL)
  424. ctxt = xs_fmt("%s#ctxt", id);
  425. /* add all mentions to the cc */
  426. p = tag;
  427. while (xs_list_iter(&p, &v)) {
  428. if (xs_type(v) == XSTYPE_DICT) {
  429. char *t;
  430. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  431. if ((t = xs_dict_get(v, "href")) != NULL)
  432. cc = xs_list_append(cc, t);
  433. }
  434. }
  435. }
  436. /* no recipients? must be for everybody */
  437. if (xs_list_len(to) == 0)
  438. to = xs_list_append(to, public_address);
  439. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  440. msg = xs_dict_append(msg, "summary", "");
  441. msg = xs_dict_append(msg, "content", fc1);
  442. msg = xs_dict_append(msg, "context", ctxt);
  443. msg = xs_dict_append(msg, "url", id);
  444. msg = xs_dict_append(msg, "to", to);
  445. msg = xs_dict_append(msg, "cc", cc);
  446. msg = xs_dict_append(msg, "inReplyTo", irt);
  447. msg = xs_dict_append(msg, "tag", tag);
  448. return msg;
  449. }
  450. /** queues **/
  451. int process_message(snac *snac, char *msg, char *req)
  452. /* processes an ActivityPub message from the input queue */
  453. {
  454. /* actor and type exist, were checked previously */
  455. char *actor = xs_dict_get(msg, "actor");
  456. char *type = xs_dict_get(msg, "type");
  457. xs *actor_o = NULL;
  458. int a_status;
  459. char *object, *utype;
  460. object = xs_dict_get(msg, "object");
  461. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  462. utype = xs_dict_get(object, "type");
  463. else
  464. utype = "(null)";
  465. /* bring the actor */
  466. a_status = actor_request(snac, actor, &actor_o);
  467. /* if the actor does not explicitly exist, discard */
  468. if (a_status == 404 || a_status == 410) {
  469. snac_debug(snac, 1,
  470. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  471. return 1;
  472. }
  473. if (!valid_status(a_status)) {
  474. /* other actor download errors may need a retry */
  475. snac_debug(snac, 1,
  476. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  477. return 0;
  478. }
  479. /* check the signature */
  480. if (!check_signature(snac, req)) {
  481. snac_log(snac, xs_fmt("bad signature"));
  482. return 1;
  483. }
  484. if (strcmp(type, "Follow") == 0) {
  485. xs *f_msg = xs_dup(msg);
  486. xs *reply = msg_accept(snac, f_msg, actor);
  487. post(snac, reply);
  488. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  489. /* add a date if it doesn't include one (Mastodon) */
  490. xs *date = xs_utc_time("%Y-%m-%dT%H:%M:%SZ");
  491. f_msg = xs_dict_set(f_msg, "published", date);
  492. }
  493. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg, NULL, NULL);
  494. follower_add(snac, actor, f_msg);
  495. snac_log(snac, xs_fmt("New follower %s", actor));
  496. }
  497. else
  498. if (strcmp(type, "Undo") == 0) {
  499. if (strcmp(utype, "Follow") == 0) {
  500. if (valid_status(follower_del(snac, actor)))
  501. snac_log(snac, xs_fmt("no longer following us %s", actor));
  502. else
  503. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  504. }
  505. else
  506. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  507. }
  508. else
  509. if (strcmp(type, "Create") == 0) {
  510. if (strcmp(utype, "Note") == 0) {
  511. if (is_muted(snac, actor))
  512. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  513. else {
  514. char *id = xs_dict_get(object, "id");
  515. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  516. timeline_request(snac, in_reply_to, NULL);
  517. if (timeline_add(snac, id, object, in_reply_to, NULL))
  518. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  519. }
  520. }
  521. else
  522. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  523. }
  524. else
  525. if (strcmp(type, "Accept") == 0) {
  526. if (strcmp(utype, "Follow") == 0) {
  527. if (following_check(snac, actor)) {
  528. following_add(snac, actor, msg);
  529. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  530. }
  531. else
  532. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  533. }
  534. else
  535. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  536. }
  537. else
  538. if (strcmp(type, "Like") == 0) {
  539. if (xs_type(object) == XSTYPE_DICT)
  540. object = xs_dict_get(object, "id");
  541. timeline_admire(snac, object, actor, 1);
  542. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  543. }
  544. else
  545. if (strcmp(type, "Announce") == 0) {
  546. xs *a_msg = NULL;
  547. if (xs_type(object) == XSTYPE_DICT)
  548. object = xs_dict_get(object, "id");
  549. timeline_request(snac, object, actor);
  550. if ((a_msg = timeline_find(snac, object)) != NULL) {
  551. char *who = xs_dict_get(a_msg, "attributedTo");
  552. if (who && !is_muted(snac, who)) {
  553. /* bring the actor */
  554. xs *who_o = NULL;
  555. if (valid_status(actor_request(snac, who, &who_o))) {
  556. timeline_admire(snac, object, actor, 0);
  557. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  558. }
  559. else
  560. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  561. }
  562. else
  563. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  564. }
  565. else
  566. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  567. }
  568. else
  569. if (strcmp(type, "Update") == 0) {
  570. if (strcmp(utype, "Person") == 0) {
  571. actor_add(snac, actor, xs_dict_get(msg, "object"));
  572. snac_log(snac, xs_fmt("updated actor %s", actor));
  573. }
  574. else
  575. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  576. }
  577. else
  578. if (strcmp(type, "Delete") == 0) {
  579. if (xs_type(object) == XSTYPE_DICT)
  580. object = xs_dict_get(object, "id");
  581. timeline_del(snac, object);
  582. snac_log(snac, xs_fmt("received delete request for %s", object));
  583. }
  584. else
  585. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  586. return 1;
  587. }
  588. void process_queue(snac *snac)
  589. /* processes the queue */
  590. {
  591. xs *list;
  592. char *p, *fn;
  593. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  594. list = queue(snac);
  595. p = list;
  596. while (xs_list_iter(&p, &fn)) {
  597. xs *q_item = dequeue(snac, fn);
  598. char *type;
  599. if (q_item == NULL) {
  600. snac_log(snac, xs_fmt("process_queue q_item error"));
  601. continue;
  602. }
  603. if ((type = xs_dict_get(q_item, "type")) == NULL)
  604. type = "output";
  605. if (strcmp(type, "output") == 0) {
  606. int status;
  607. char *actor = xs_dict_get(q_item, "actor");
  608. char *msg = xs_dict_get(q_item, "object");
  609. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  610. xs *payload = NULL;
  611. int p_size = 0;
  612. /* deliver */
  613. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  614. if (!valid_status(status)) {
  615. /* error sending; reenqueue? */
  616. if (retries > queue_retry_max)
  617. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  618. else {
  619. /* reenqueue */
  620. enqueue_output(snac, msg, actor, retries + 1);
  621. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  622. }
  623. }
  624. }
  625. else
  626. if (strcmp(type, "input") == 0) {
  627. /* process the message */
  628. char *msg = xs_dict_get(q_item, "object");
  629. char *req = xs_dict_get(q_item, "req");
  630. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  631. if (!process_message(snac, msg, req)) {
  632. if (retries > queue_retry_max)
  633. snac_log(snac, xs_fmt("process_queue input giving up"));
  634. else {
  635. /* reenqueue */
  636. enqueue_input(snac, msg, req, retries + 1);
  637. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  638. }
  639. }
  640. }
  641. }
  642. }
  643. void post(snac *snac, char *msg)
  644. /* enqueues a message to all its recipients */
  645. {
  646. xs *rcpts = recipient_list(snac, msg, 1);
  647. char *p, *v;
  648. p = rcpts;
  649. while (xs_list_iter(&p, &v)) {
  650. enqueue_output(snac, msg, v, 0);
  651. }
  652. }
  653. /** HTTP handlers */
  654. int activitypub_get_handler(d_char *req, char *q_path,
  655. char **body, int *b_size, char **ctype)
  656. {
  657. int status = 200;
  658. char *accept = xs_dict_get(req, "accept");
  659. snac snac;
  660. xs *msg = NULL;
  661. if (accept == NULL)
  662. return 400;
  663. if (xs_str_in(accept, "application/activity+json") == -1 &&
  664. xs_str_in(accept, "application/ld+json") == -1)
  665. return 0;
  666. xs *l = xs_split_n(q_path, "/", 2);
  667. char *uid, *p_path;
  668. uid = xs_list_get(l, 1);
  669. if (!user_open(&snac, uid)) {
  670. /* invalid user */
  671. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  672. return 404;
  673. }
  674. p_path = xs_list_get(l, 2);
  675. *ctype = "application/activity+json";
  676. if (p_path == NULL) {
  677. /* if there was no component after the user, it's an actor request */
  678. msg = msg_actor(&snac);
  679. *ctype = "application/ld+json";
  680. }
  681. else
  682. if (strcmp(p_path, "outbox") == 0) {
  683. xs *id = xs_fmt("%s/outbox", snac.actor);
  684. xs *elems = local_list(&snac, 20);
  685. xs *list = xs_list_new();
  686. msg = msg_collection(&snac, id);
  687. char *p, *v;
  688. p = elems;
  689. while (xs_list_iter(&p, &v)) {
  690. xs *i = timeline_get(&snac, v);
  691. char *type = xs_dict_get(i, "type");
  692. char *id = xs_dict_get(i, "id");
  693. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  694. i = xs_dict_del(i, "_snac");
  695. list = xs_list_append(list, i);
  696. }
  697. }
  698. /* replace the 'orderedItems' with the latest posts */
  699. msg = xs_dict_set(msg, "orderedItems", list);
  700. msg = xs_dict_set(msg, "totalItems", xs_number_new(xs_list_len(list)));
  701. }
  702. else
  703. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  704. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  705. msg = msg_collection(&snac, id);
  706. }
  707. else
  708. if (xs_startswith(p_path, "p/")) {
  709. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  710. if ((msg = timeline_find(&snac, id)) != NULL)
  711. msg = xs_dict_del(msg, "_snac");
  712. else
  713. status = 404;
  714. }
  715. else
  716. status = 404;
  717. if (status == 200 && msg != NULL) {
  718. *body = xs_json_dumps_pp(msg, 4);
  719. *b_size = strlen(*body);
  720. }
  721. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  722. user_free(&snac);
  723. return status;
  724. }
  725. int activitypub_post_handler(d_char *req, char *q_path,
  726. d_char *payload, int p_size,
  727. char **body, int *b_size, char **ctype)
  728. /* processes an input message */
  729. {
  730. int status = 202; /* accepted */
  731. char *i_ctype = xs_dict_get(req, "content-type");
  732. snac snac;
  733. char *v;
  734. if (i_ctype == NULL)
  735. return 400;
  736. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  737. xs_str_in(i_ctype, "application/ld+json") == -1)
  738. return 0;
  739. /* decode the message */
  740. xs *msg = xs_json_loads(payload);
  741. if (msg == NULL) {
  742. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  743. status = 400;
  744. }
  745. /* get the user and path */
  746. xs *l = xs_split_n(q_path, "/", 2);
  747. char *uid;
  748. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  749. /* strange q_path */
  750. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  751. return 404;
  752. }
  753. uid = xs_list_get(l, 1);
  754. if (!user_open(&snac, uid)) {
  755. /* invalid user */
  756. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  757. return 404;
  758. }
  759. /* if it has a digest, check it now, because
  760. later the payload won't be exactly the same */
  761. if ((v = xs_dict_get(req, "digest")) != NULL) {
  762. xs *s1 = xs_sha256_base64(payload, p_size);
  763. xs *s2 = xs_fmt("SHA-256=%s", s1);
  764. if (strcmp(s2, v) != 0) {
  765. srv_log(xs_fmt("digest check FAILED"));
  766. status = 400;
  767. }
  768. }
  769. if (valid_status(status)) {
  770. enqueue_input(&snac, msg, req, 0);
  771. *ctype = "application/activity+json";
  772. }
  773. user_free(&snac);
  774. return status;
  775. }