activitypub.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  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. int 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. xs *actor_o = NULL;
  431. int a_status;
  432. char *object, *utype;
  433. object = xs_dict_get(msg, "object");
  434. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  435. utype = xs_dict_get(object, "type");
  436. else
  437. utype = "(null)";
  438. /* bring the actor */
  439. a_status = actor_request(snac, actor, &actor_o);
  440. /* if the actor does not explicitly exist, discard */
  441. if (a_status == 404 || a_status == 410) {
  442. snac_debug(snac, 1,
  443. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  444. return 1;
  445. }
  446. if (!valid_status(a_status)) {
  447. /* other actor download errors may need a retry */
  448. snac_debug(snac, 1,
  449. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  450. return 0;
  451. }
  452. /* check the signature */
  453. if (!check_signature(snac, req)) {
  454. snac_log(snac, xs_fmt("bad signature"));
  455. return 1;
  456. }
  457. if (strcmp(type, "Follow") == 0) {
  458. xs *reply = msg_accept(snac, msg, actor);
  459. post(snac, reply);
  460. timeline_add(snac, xs_dict_get(msg, "id"), msg, NULL, NULL);
  461. follower_add(snac, actor, msg);
  462. snac_log(snac, xs_fmt("New follower %s", actor));
  463. }
  464. else
  465. if (strcmp(type, "Undo") == 0) {
  466. if (strcmp(utype, "Follow") == 0) {
  467. if (valid_status(follower_del(snac, actor)))
  468. snac_log(snac, xs_fmt("no longer following us %s", actor));
  469. else
  470. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  471. }
  472. else
  473. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  474. }
  475. else
  476. if (strcmp(type, "Create") == 0) {
  477. if (strcmp(utype, "Note") == 0) {
  478. if (is_muted(snac, actor))
  479. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  480. else {
  481. char *id = xs_dict_get(object, "id");
  482. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  483. timeline_request(snac, in_reply_to, NULL);
  484. if (timeline_add(snac, id, object, in_reply_to, NULL))
  485. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  486. }
  487. }
  488. else
  489. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  490. }
  491. else
  492. if (strcmp(type, "Accept") == 0) {
  493. if (strcmp(utype, "Follow") == 0) {
  494. if (following_check(snac, actor)) {
  495. following_add(snac, actor, msg);
  496. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  497. }
  498. else
  499. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  500. }
  501. else
  502. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  503. }
  504. else
  505. if (strcmp(type, "Like") == 0) {
  506. if (xs_type(object) == XSTYPE_DICT)
  507. object = xs_dict_get(object, "id");
  508. timeline_admire(snac, object, actor, 1);
  509. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  510. }
  511. else
  512. if (strcmp(type, "Announce") == 0) {
  513. xs *a_msg = NULL;
  514. if (xs_type(object) == XSTYPE_DICT)
  515. object = xs_dict_get(object, "id");
  516. timeline_request(snac, object, actor);
  517. if ((a_msg = timeline_find(snac, object)) != NULL) {
  518. char *who = xs_dict_get(a_msg, "attributedTo");
  519. if (who && !is_muted(snac, who)) {
  520. /* bring the actor */
  521. xs *who_o = NULL;
  522. if (valid_status(actor_request(snac, who, &who_o))) {
  523. timeline_admire(snac, object, actor, 0);
  524. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  525. }
  526. else
  527. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  528. }
  529. else
  530. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  531. }
  532. else
  533. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  534. }
  535. else
  536. if (strcmp(type, "Update") == 0) {
  537. if (strcmp(utype, "Person") == 0) {
  538. actor_add(snac, actor, xs_dict_get(msg, "object"));
  539. snac_log(snac, xs_fmt("updated actor %s", actor));
  540. }
  541. else
  542. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  543. }
  544. /*
  545. else
  546. if (strcmp(type, "Delete") == 0) {
  547. }
  548. */
  549. else
  550. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  551. return 1;
  552. }
  553. void process_queue(snac *snac)
  554. /* processes the queue */
  555. {
  556. xs *list;
  557. char *p, *fn;
  558. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  559. list = queue(snac);
  560. p = list;
  561. while (xs_list_iter(&p, &fn)) {
  562. xs *q_item = dequeue(snac, fn);
  563. char *type;
  564. if (q_item == NULL) {
  565. snac_log(snac, xs_fmt("process_queue q_item error"));
  566. continue;
  567. }
  568. if ((type = xs_dict_get(q_item, "type")) == NULL)
  569. type = "output";
  570. if (strcmp(type, "output") == 0) {
  571. int status;
  572. char *actor = xs_dict_get(q_item, "actor");
  573. char *msg = xs_dict_get(q_item, "object");
  574. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  575. xs *payload = NULL;
  576. int p_size = 0;
  577. /* deliver */
  578. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  579. if (!valid_status(status)) {
  580. /* error sending; reenqueue? */
  581. if (retries > queue_retry_max)
  582. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  583. else {
  584. /* reenqueue */
  585. enqueue_output(snac, msg, actor, retries + 1);
  586. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  587. }
  588. }
  589. }
  590. else
  591. if (strcmp(type, "input") == 0) {
  592. /* process the message */
  593. char *msg = xs_dict_get(q_item, "object");
  594. char *req = xs_dict_get(q_item, "req");
  595. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  596. if (!process_message(snac, msg, req)) {
  597. if (retries > queue_retry_max)
  598. snac_log(snac, xs_fmt("process_queue input giving up"));
  599. else {
  600. /* reenqueue */
  601. enqueue_input(snac, msg, req, retries + 1);
  602. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  603. }
  604. }
  605. }
  606. }
  607. }
  608. void post(snac *snac, char *msg)
  609. /* enqueues a message to all its recipients */
  610. {
  611. xs *rcpts = recipient_list(snac, msg, 1);
  612. char *p, *v;
  613. p = rcpts;
  614. while (xs_list_iter(&p, &v)) {
  615. enqueue_output(snac, msg, v, 0);
  616. }
  617. }
  618. /** HTTP handlers */
  619. int activitypub_get_handler(d_char *req, char *q_path,
  620. char **body, int *b_size, char **ctype)
  621. {
  622. int status = 200;
  623. char *accept = xs_dict_get(req, "accept");
  624. snac snac;
  625. xs *msg = NULL;
  626. if (accept == NULL)
  627. return 400;
  628. if (xs_str_in(accept, "application/activity+json") == -1 &&
  629. xs_str_in(accept, "application/ld+json") == -1)
  630. return 0;
  631. xs *l = xs_split_n(q_path, "/", 2);
  632. char *uid, *p_path;
  633. uid = xs_list_get(l, 1);
  634. if (!user_open(&snac, uid)) {
  635. /* invalid user */
  636. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  637. return 404;
  638. }
  639. p_path = xs_list_get(l, 2);
  640. *ctype = "application/activity+json";
  641. if (p_path == NULL) {
  642. /* if there was no component after the user, it's an actor request */
  643. msg = msg_actor(&snac);
  644. *ctype = "application/ld+json";
  645. }
  646. else
  647. if (strcmp(p_path, "outbox") == 0) {
  648. xs *id = xs_fmt("%s/outbox", snac.actor);
  649. xs *elems = local_list(&snac, 20);
  650. xs *list = xs_list_new();
  651. msg = msg_collection(&snac, id);
  652. char *p, *v;
  653. p = elems;
  654. while (xs_list_iter(&p, &v)) {
  655. xs *i = timeline_get(&snac, v);
  656. char *type = xs_dict_get(i, "type");
  657. char *id = xs_dict_get(i, "id");
  658. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  659. i = xs_dict_del(i, "_snac");
  660. list = xs_list_append(list, i);
  661. }
  662. }
  663. /* replace the 'orderedItems' with the latest posts */
  664. msg = xs_dict_set(msg, "orderedItems", list);
  665. msg = xs_dict_set(msg, "totalItems", xs_number_new(xs_list_len(list)));
  666. }
  667. else
  668. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  669. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  670. msg = msg_collection(&snac, id);
  671. }
  672. else
  673. if (xs_startswith(p_path, "p/")) {
  674. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  675. if ((msg = timeline_find(&snac, id)) != NULL)
  676. msg = xs_dict_del(msg, "_snac");
  677. else
  678. status = 404;
  679. }
  680. else
  681. status = 404;
  682. if (status == 200 && msg != NULL) {
  683. *body = xs_json_dumps_pp(msg, 4);
  684. *b_size = strlen(*body);
  685. }
  686. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  687. user_free(&snac);
  688. return status;
  689. }
  690. int activitypub_post_handler(d_char *req, char *q_path,
  691. d_char *payload, int p_size,
  692. char **body, int *b_size, char **ctype)
  693. /* processes an input message */
  694. {
  695. int status = 202; /* accepted */
  696. char *i_ctype = xs_dict_get(req, "content-type");
  697. snac snac;
  698. char *v;
  699. if (i_ctype == NULL)
  700. return 400;
  701. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  702. xs_str_in(i_ctype, "application/ld+json") == -1)
  703. return 0;
  704. /* decode the message */
  705. xs *msg = xs_json_loads(payload);
  706. if (msg == NULL) {
  707. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  708. status = 400;
  709. }
  710. /* get the user and path */
  711. xs *l = xs_split_n(q_path, "/", 2);
  712. char *uid;
  713. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  714. /* strange q_path */
  715. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  716. return 404;
  717. }
  718. uid = xs_list_get(l, 1);
  719. if (!user_open(&snac, uid)) {
  720. /* invalid user */
  721. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  722. return 404;
  723. }
  724. /* if it has a digest, check it now, because
  725. later the payload won't be exactly the same */
  726. if ((v = xs_dict_get(req, "digest")) != NULL) {
  727. xs *s1 = xs_sha256_base64(payload, p_size);
  728. xs *s2 = xs_fmt("SHA-256=%s", s1);
  729. if (strcmp(s2, v) != 0) {
  730. srv_log(xs_fmt("digest check FAILED"));
  731. status = 400;
  732. }
  733. }
  734. if (valid_status(status)) {
  735. enqueue_input(&snac, msg, req, 0);
  736. *ctype = "application/activity+json";
  737. }
  738. user_free(&snac);
  739. return status;
  740. }