activitypub.c 26 KB

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