activitypub.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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, char *attach)
  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. xs *atls = NULL;
  394. d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  395. char *p, *v;
  396. if (rcpts == NULL)
  397. to = xs_list_new();
  398. else
  399. to = xs_dup(rcpts);
  400. /* format the content */
  401. not_really_markdown(content, &fc2);
  402. /* extract the tags */
  403. process_tags(fc2, &fc1, &tag);
  404. if (in_reply_to != NULL) {
  405. xs *p_msg = NULL;
  406. /* demand this thing */
  407. timeline_request(snac, in_reply_to, NULL);
  408. if ((p_msg = timeline_find(snac, in_reply_to)) != NULL) {
  409. /* add this author as recipient */
  410. char *v;
  411. if ((v = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, v) == -1)
  412. to = xs_list_append(to, v);
  413. if ((v = xs_dict_get(p_msg, "context")))
  414. ctxt = xs_dup(v);
  415. /* if this message is public, ours will also be */
  416. if (is_msg_public(snac, p_msg) &&
  417. xs_list_in(to, (char *)public_address) == -1)
  418. to = xs_list_append(to, public_address);
  419. }
  420. irt = xs_dup(in_reply_to);
  421. }
  422. else
  423. irt = xs_val_new(XSTYPE_NULL);
  424. /* create the attachment list, if there are any */
  425. if (!xs_is_null(attach) && *attach != '\0') {
  426. xs *lsof1 = NULL;
  427. if (xs_type(attach) == XSTYPE_STRING) {
  428. lsof1 = xs_list_append(xs_list_new(), attach);
  429. attach = lsof1;
  430. }
  431. atls = xs_list_new();
  432. while (xs_list_iter(&attach, &v)) {
  433. xs *d = xs_dict_new();
  434. char *mime = xs_mime_by_ext(v);
  435. d = xs_dict_append(d, "mediaType", mime);
  436. d = xs_dict_append(d, "url", v);
  437. d = xs_dict_append(d, "name", "");
  438. d = xs_dict_append(d, "type",
  439. xs_startswith(mime, "image/") ? "Image" : "Document");
  440. atls = xs_list_append(atls, d);
  441. }
  442. }
  443. if (tag == NULL)
  444. tag = xs_list_new();
  445. if (ctxt == NULL)
  446. ctxt = xs_fmt("%s#ctxt", id);
  447. /* add all mentions to the cc */
  448. p = tag;
  449. while (xs_list_iter(&p, &v)) {
  450. if (xs_type(v) == XSTYPE_DICT) {
  451. char *t;
  452. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  453. if ((t = xs_dict_get(v, "href")) != NULL)
  454. cc = xs_list_append(cc, t);
  455. }
  456. }
  457. }
  458. /* no recipients? must be for everybody */
  459. if (xs_list_len(to) == 0)
  460. to = xs_list_append(to, public_address);
  461. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  462. msg = xs_dict_append(msg, "summary", "");
  463. msg = xs_dict_append(msg, "content", fc1);
  464. msg = xs_dict_append(msg, "context", ctxt);
  465. msg = xs_dict_append(msg, "url", id);
  466. msg = xs_dict_append(msg, "to", to);
  467. msg = xs_dict_append(msg, "cc", cc);
  468. msg = xs_dict_append(msg, "inReplyTo", irt);
  469. msg = xs_dict_append(msg, "tag", tag);
  470. if (atls != NULL)
  471. msg = xs_dict_append(msg, "attachment", atls);
  472. return msg;
  473. }
  474. /** queues **/
  475. int process_message(snac *snac, char *msg, char *req)
  476. /* processes an ActivityPub message from the input queue */
  477. {
  478. /* actor and type exist, were checked previously */
  479. char *actor = xs_dict_get(msg, "actor");
  480. char *type = xs_dict_get(msg, "type");
  481. xs *actor_o = NULL;
  482. int a_status;
  483. char *object, *utype;
  484. object = xs_dict_get(msg, "object");
  485. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  486. utype = xs_dict_get(object, "type");
  487. else
  488. utype = "(null)";
  489. /* bring the actor */
  490. a_status = actor_request(snac, actor, &actor_o);
  491. /* if the actor does not explicitly exist, discard */
  492. if (a_status == 404 || a_status == 410) {
  493. snac_debug(snac, 1,
  494. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  495. return 1;
  496. }
  497. if (!valid_status(a_status)) {
  498. /* other actor download errors may need a retry */
  499. snac_debug(snac, 1,
  500. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  501. return 0;
  502. }
  503. /* check the signature */
  504. if (!check_signature(snac, req)) {
  505. snac_log(snac, xs_fmt("bad signature"));
  506. return 1;
  507. }
  508. if (strcmp(type, "Follow") == 0) {
  509. xs *f_msg = xs_dup(msg);
  510. xs *reply = msg_accept(snac, f_msg, actor);
  511. post(snac, reply);
  512. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  513. /* add a date if it doesn't include one (Mastodon) */
  514. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  515. f_msg = xs_dict_set(f_msg, "published", date);
  516. }
  517. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg, NULL, NULL);
  518. follower_add(snac, actor, f_msg);
  519. snac_log(snac, xs_fmt("New follower %s", actor));
  520. }
  521. else
  522. if (strcmp(type, "Undo") == 0) {
  523. if (strcmp(utype, "Follow") == 0) {
  524. if (valid_status(follower_del(snac, actor)))
  525. snac_log(snac, xs_fmt("no longer following us %s", actor));
  526. else
  527. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  528. }
  529. else
  530. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  531. }
  532. else
  533. if (strcmp(type, "Create") == 0) {
  534. if (strcmp(utype, "Note") == 0) {
  535. if (is_muted(snac, actor))
  536. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  537. else {
  538. char *id = xs_dict_get(object, "id");
  539. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  540. timeline_request(snac, in_reply_to, NULL);
  541. if (timeline_add(snac, id, object, in_reply_to, NULL))
  542. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  543. }
  544. }
  545. else
  546. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  547. }
  548. else
  549. if (strcmp(type, "Accept") == 0) {
  550. if (strcmp(utype, "Follow") == 0) {
  551. if (following_check(snac, actor)) {
  552. following_add(snac, actor, msg);
  553. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  554. }
  555. else
  556. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  557. }
  558. else
  559. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  560. }
  561. else
  562. if (strcmp(type, "Like") == 0) {
  563. if (xs_type(object) == XSTYPE_DICT)
  564. object = xs_dict_get(object, "id");
  565. timeline_admire(snac, object, actor, 1);
  566. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  567. }
  568. else
  569. if (strcmp(type, "Announce") == 0) {
  570. xs *a_msg = NULL;
  571. if (xs_type(object) == XSTYPE_DICT)
  572. object = xs_dict_get(object, "id");
  573. timeline_request(snac, object, actor);
  574. if ((a_msg = timeline_find(snac, object)) != NULL) {
  575. char *who = xs_dict_get(a_msg, "attributedTo");
  576. if (who && !is_muted(snac, who)) {
  577. /* bring the actor */
  578. xs *who_o = NULL;
  579. if (valid_status(actor_request(snac, who, &who_o))) {
  580. timeline_admire(snac, object, actor, 0);
  581. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  582. }
  583. else
  584. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  585. }
  586. else
  587. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  588. }
  589. else
  590. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  591. }
  592. else
  593. if (strcmp(type, "Update") == 0) {
  594. if (strcmp(utype, "Person") == 0) {
  595. actor_add(snac, actor, xs_dict_get(msg, "object"));
  596. snac_log(snac, xs_fmt("updated actor %s", actor));
  597. }
  598. else
  599. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  600. }
  601. else
  602. if (strcmp(type, "Delete") == 0) {
  603. if (xs_type(object) == XSTYPE_DICT)
  604. object = xs_dict_get(object, "id");
  605. if (valid_status(timeline_del(snac, object)))
  606. snac_log(snac, xs_fmt("New 'Delete' %s %s", actor, object));
  607. else
  608. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  609. }
  610. else
  611. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  612. return 1;
  613. }
  614. void process_queue(snac *snac)
  615. /* processes the queue */
  616. {
  617. xs *list;
  618. char *p, *fn;
  619. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  620. list = queue(snac);
  621. p = list;
  622. while (xs_list_iter(&p, &fn)) {
  623. xs *q_item = dequeue(snac, fn);
  624. char *type;
  625. if (q_item == NULL) {
  626. snac_log(snac, xs_fmt("process_queue q_item error"));
  627. continue;
  628. }
  629. if ((type = xs_dict_get(q_item, "type")) == NULL)
  630. type = "output";
  631. if (strcmp(type, "output") == 0) {
  632. int status;
  633. char *actor = xs_dict_get(q_item, "actor");
  634. char *msg = xs_dict_get(q_item, "object");
  635. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  636. xs *payload = NULL;
  637. int p_size = 0;
  638. /* deliver */
  639. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  640. if (!valid_status(status)) {
  641. /* error sending; reenqueue? */
  642. if (retries > queue_retry_max)
  643. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  644. else {
  645. /* reenqueue */
  646. enqueue_output(snac, msg, actor, retries + 1);
  647. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  648. }
  649. }
  650. }
  651. else
  652. if (strcmp(type, "input") == 0) {
  653. /* process the message */
  654. char *msg = xs_dict_get(q_item, "object");
  655. char *req = xs_dict_get(q_item, "req");
  656. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  657. if (!process_message(snac, msg, req)) {
  658. if (retries > queue_retry_max)
  659. snac_log(snac, xs_fmt("process_queue input giving up"));
  660. else {
  661. /* reenqueue */
  662. enqueue_input(snac, msg, req, retries + 1);
  663. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  664. }
  665. }
  666. }
  667. }
  668. }
  669. void post(snac *snac, char *msg)
  670. /* enqueues a message to all its recipients */
  671. {
  672. xs *rcpts = recipient_list(snac, msg, 1);
  673. char *p, *v;
  674. p = rcpts;
  675. while (xs_list_iter(&p, &v)) {
  676. enqueue_output(snac, msg, v, 0);
  677. }
  678. }
  679. /** HTTP handlers */
  680. int activitypub_get_handler(d_char *req, char *q_path,
  681. char **body, int *b_size, char **ctype)
  682. {
  683. int status = 200;
  684. char *accept = xs_dict_get(req, "accept");
  685. snac snac;
  686. xs *msg = NULL;
  687. if (accept == NULL)
  688. return 0;
  689. if (xs_str_in(accept, "application/activity+json") == -1 &&
  690. xs_str_in(accept, "application/ld+json") == -1)
  691. return 0;
  692. xs *l = xs_split_n(q_path, "/", 2);
  693. char *uid, *p_path;
  694. uid = xs_list_get(l, 1);
  695. if (!user_open(&snac, uid)) {
  696. /* invalid user */
  697. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  698. return 404;
  699. }
  700. p_path = xs_list_get(l, 2);
  701. *ctype = "application/activity+json";
  702. if (p_path == NULL) {
  703. /* if there was no component after the user, it's an actor request */
  704. msg = msg_actor(&snac);
  705. *ctype = "application/ld+json";
  706. }
  707. else
  708. if (strcmp(p_path, "outbox") == 0) {
  709. xs *id = xs_fmt("%s/outbox", snac.actor);
  710. xs *elems = local_list(&snac, 20);
  711. xs *list = xs_list_new();
  712. msg = msg_collection(&snac, id);
  713. char *p, *v;
  714. p = elems;
  715. while (xs_list_iter(&p, &v)) {
  716. xs *i = timeline_get(&snac, v);
  717. char *type = xs_dict_get(i, "type");
  718. char *id = xs_dict_get(i, "id");
  719. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  720. i = xs_dict_del(i, "_snac");
  721. list = xs_list_append(list, i);
  722. }
  723. }
  724. /* replace the 'orderedItems' with the latest posts */
  725. msg = xs_dict_set(msg, "orderedItems", list);
  726. msg = xs_dict_set(msg, "totalItems", xs_number_new(xs_list_len(list)));
  727. }
  728. else
  729. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  730. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  731. msg = msg_collection(&snac, id);
  732. }
  733. else
  734. if (xs_startswith(p_path, "p/")) {
  735. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  736. if ((msg = timeline_find(&snac, id)) != NULL)
  737. msg = xs_dict_del(msg, "_snac");
  738. else
  739. status = 404;
  740. }
  741. else
  742. status = 404;
  743. if (status == 200 && msg != NULL) {
  744. *body = xs_json_dumps_pp(msg, 4);
  745. *b_size = strlen(*body);
  746. }
  747. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  748. user_free(&snac);
  749. return status;
  750. }
  751. int activitypub_post_handler(d_char *req, char *q_path,
  752. d_char *payload, int p_size,
  753. char **body, int *b_size, char **ctype)
  754. /* processes an input message */
  755. {
  756. int status = 202; /* accepted */
  757. char *i_ctype = xs_dict_get(req, "content-type");
  758. snac snac;
  759. char *v;
  760. if (i_ctype == NULL)
  761. return 400;
  762. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  763. xs_str_in(i_ctype, "application/ld+json") == -1)
  764. return 0;
  765. /* decode the message */
  766. xs *msg = xs_json_loads(payload);
  767. if (msg == NULL) {
  768. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  769. status = 400;
  770. }
  771. /* get the user and path */
  772. xs *l = xs_split_n(q_path, "/", 2);
  773. char *uid;
  774. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  775. /* strange q_path */
  776. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  777. return 404;
  778. }
  779. uid = xs_list_get(l, 1);
  780. if (!user_open(&snac, uid)) {
  781. /* invalid user */
  782. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  783. return 404;
  784. }
  785. /* if it has a digest, check it now, because
  786. later the payload won't be exactly the same */
  787. if ((v = xs_dict_get(req, "digest")) != NULL) {
  788. xs *s1 = xs_sha256_base64(payload, p_size);
  789. xs *s2 = xs_fmt("SHA-256=%s", s1);
  790. if (strcmp(s2, v) != 0) {
  791. srv_log(xs_fmt("digest check FAILED"));
  792. status = 400;
  793. }
  794. }
  795. if (valid_status(status)) {
  796. enqueue_input(&snac, msg, req, 0);
  797. *ctype = "application/activity+json";
  798. }
  799. user_free(&snac);
  800. return status;
  801. }