activitypub.c 28 KB

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