activitypub.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. timeline_del(snac, object);
  606. snac_log(snac, xs_fmt("received delete request for %s", object));
  607. }
  608. else
  609. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  610. return 1;
  611. }
  612. void process_queue(snac *snac)
  613. /* processes the queue */
  614. {
  615. xs *list;
  616. char *p, *fn;
  617. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  618. list = queue(snac);
  619. p = list;
  620. while (xs_list_iter(&p, &fn)) {
  621. xs *q_item = dequeue(snac, fn);
  622. char *type;
  623. if (q_item == NULL) {
  624. snac_log(snac, xs_fmt("process_queue q_item error"));
  625. continue;
  626. }
  627. if ((type = xs_dict_get(q_item, "type")) == NULL)
  628. type = "output";
  629. if (strcmp(type, "output") == 0) {
  630. int status;
  631. char *actor = xs_dict_get(q_item, "actor");
  632. char *msg = xs_dict_get(q_item, "object");
  633. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  634. xs *payload = NULL;
  635. int p_size = 0;
  636. /* deliver */
  637. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  638. if (!valid_status(status)) {
  639. /* error sending; reenqueue? */
  640. if (retries > queue_retry_max)
  641. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  642. else {
  643. /* reenqueue */
  644. enqueue_output(snac, msg, actor, retries + 1);
  645. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  646. }
  647. }
  648. }
  649. else
  650. if (strcmp(type, "input") == 0) {
  651. /* process the message */
  652. char *msg = xs_dict_get(q_item, "object");
  653. char *req = xs_dict_get(q_item, "req");
  654. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  655. if (!process_message(snac, msg, req)) {
  656. if (retries > queue_retry_max)
  657. snac_log(snac, xs_fmt("process_queue input giving up"));
  658. else {
  659. /* reenqueue */
  660. enqueue_input(snac, msg, req, retries + 1);
  661. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  662. }
  663. }
  664. }
  665. }
  666. }
  667. void post(snac *snac, char *msg)
  668. /* enqueues a message to all its recipients */
  669. {
  670. xs *rcpts = recipient_list(snac, msg, 1);
  671. char *p, *v;
  672. p = rcpts;
  673. while (xs_list_iter(&p, &v)) {
  674. enqueue_output(snac, msg, v, 0);
  675. }
  676. }
  677. /** HTTP handlers */
  678. int activitypub_get_handler(d_char *req, char *q_path,
  679. char **body, int *b_size, char **ctype)
  680. {
  681. int status = 200;
  682. char *accept = xs_dict_get(req, "accept");
  683. snac snac;
  684. xs *msg = NULL;
  685. if (accept == NULL)
  686. return 400;
  687. if (xs_str_in(accept, "application/activity+json") == -1 &&
  688. xs_str_in(accept, "application/ld+json") == -1)
  689. return 0;
  690. xs *l = xs_split_n(q_path, "/", 2);
  691. char *uid, *p_path;
  692. uid = xs_list_get(l, 1);
  693. if (!user_open(&snac, uid)) {
  694. /* invalid user */
  695. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  696. return 404;
  697. }
  698. p_path = xs_list_get(l, 2);
  699. *ctype = "application/activity+json";
  700. if (p_path == NULL) {
  701. /* if there was no component after the user, it's an actor request */
  702. msg = msg_actor(&snac);
  703. *ctype = "application/ld+json";
  704. }
  705. else
  706. if (strcmp(p_path, "outbox") == 0) {
  707. xs *id = xs_fmt("%s/outbox", snac.actor);
  708. xs *elems = local_list(&snac, 20);
  709. xs *list = xs_list_new();
  710. msg = msg_collection(&snac, id);
  711. char *p, *v;
  712. p = elems;
  713. while (xs_list_iter(&p, &v)) {
  714. xs *i = timeline_get(&snac, v);
  715. char *type = xs_dict_get(i, "type");
  716. char *id = xs_dict_get(i, "id");
  717. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  718. i = xs_dict_del(i, "_snac");
  719. list = xs_list_append(list, i);
  720. }
  721. }
  722. /* replace the 'orderedItems' with the latest posts */
  723. msg = xs_dict_set(msg, "orderedItems", list);
  724. msg = xs_dict_set(msg, "totalItems", xs_number_new(xs_list_len(list)));
  725. }
  726. else
  727. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  728. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  729. msg = msg_collection(&snac, id);
  730. }
  731. else
  732. if (xs_startswith(p_path, "p/")) {
  733. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  734. if ((msg = timeline_find(&snac, id)) != NULL)
  735. msg = xs_dict_del(msg, "_snac");
  736. else
  737. status = 404;
  738. }
  739. else
  740. status = 404;
  741. if (status == 200 && msg != NULL) {
  742. *body = xs_json_dumps_pp(msg, 4);
  743. *b_size = strlen(*body);
  744. }
  745. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  746. user_free(&snac);
  747. return status;
  748. }
  749. int activitypub_post_handler(d_char *req, char *q_path,
  750. d_char *payload, int p_size,
  751. char **body, int *b_size, char **ctype)
  752. /* processes an input message */
  753. {
  754. int status = 202; /* accepted */
  755. char *i_ctype = xs_dict_get(req, "content-type");
  756. snac snac;
  757. char *v;
  758. if (i_ctype == NULL)
  759. return 400;
  760. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  761. xs_str_in(i_ctype, "application/ld+json") == -1)
  762. return 0;
  763. /* decode the message */
  764. xs *msg = xs_json_loads(payload);
  765. if (msg == NULL) {
  766. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  767. status = 400;
  768. }
  769. /* get the user and path */
  770. xs *l = xs_split_n(q_path, "/", 2);
  771. char *uid;
  772. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  773. /* strange q_path */
  774. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  775. return 404;
  776. }
  777. uid = xs_list_get(l, 1);
  778. if (!user_open(&snac, uid)) {
  779. /* invalid user */
  780. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  781. return 404;
  782. }
  783. /* if it has a digest, check it now, because
  784. later the payload won't be exactly the same */
  785. if ((v = xs_dict_get(req, "digest")) != NULL) {
  786. xs *s1 = xs_sha256_base64(payload, p_size);
  787. xs *s2 = xs_fmt("SHA-256=%s", s1);
  788. if (strcmp(s2, v) != 0) {
  789. srv_log(xs_fmt("digest check FAILED"));
  790. status = 400;
  791. }
  792. }
  793. if (valid_status(status)) {
  794. enqueue_input(&snac, msg, req, 0);
  795. *ctype = "application/activity+json";
  796. }
  797. user_free(&snac);
  798. return status;
  799. }