activitypub.c 27 KB

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