activitypub.c 28 KB

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