activitypub.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161
  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 *type = xs_dict_get(object, "type");
  72. if (!xs_is_null(type) && strcmp(type, "Note") == 0) {
  73. char *actor = xs_dict_get(object, "attributedTo");
  74. /* request (and drop) the actor for this entry */
  75. if (!xs_is_null(actor))
  76. actor_request(snac, actor, NULL);
  77. /* does it have an ancestor? */
  78. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  79. /* recurse! */
  80. timeline_request(snac, in_reply_to, referrer);
  81. /* finally store */
  82. timeline_add(snac, id, object, in_reply_to, referrer);
  83. }
  84. }
  85. }
  86. }
  87. return status;
  88. }
  89. int send_to_inbox(snac *snac, char *inbox, char *msg, d_char **payload, int *p_size)
  90. /* sends a message to an Inbox */
  91. {
  92. int status;
  93. d_char *response;
  94. xs *j_msg = xs_json_dumps_pp(msg, 4);
  95. response = http_signed_request(snac, "POST", inbox,
  96. NULL, j_msg, strlen(j_msg), &status, payload, p_size);
  97. xs_free(response);
  98. return status;
  99. }
  100. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size)
  101. /* sends a message to an actor */
  102. {
  103. int status;
  104. xs *data = NULL;
  105. /* resolve the actor first */
  106. status = actor_request(snac, actor, &data);
  107. if (valid_status(status)) {
  108. char *inbox = xs_dict_get(data, "inbox");
  109. if (inbox != NULL)
  110. status = send_to_inbox(snac, inbox, msg, payload, p_size);
  111. else
  112. status = 400;
  113. }
  114. snac_log(snac, xs_fmt("send_to_actor %s %d", actor, status));
  115. return status;
  116. }
  117. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  118. /* returns the list of recipients for a message */
  119. {
  120. d_char *list = xs_list_new();
  121. char *to = xs_dict_get(msg, "to");
  122. char *cc = xs_dict_get(msg, "cc");
  123. int n;
  124. char *lists[] = { to, cc, NULL };
  125. for (n = 0; lists[n]; n++) {
  126. char *l = lists[n];
  127. char *v;
  128. xs *tl = NULL;
  129. /* if it's a string, create a list with only one element */
  130. if (xs_type(l) == XSTYPE_STRING) {
  131. tl = xs_list_new();
  132. tl = xs_list_append(tl, l);
  133. l = tl;
  134. }
  135. while (xs_list_iter(&l, &v)) {
  136. if (expand_public && strcmp(v, public_address) == 0) {
  137. /* iterate the followers and add them */
  138. xs *fwers = follower_list(snac);
  139. char *fw;
  140. char *p = fwers;
  141. while (xs_list_iter(&p, &fw)) {
  142. char *actor = xs_dict_get(fw, "actor");
  143. if (xs_list_in(list, actor) == -1)
  144. list = xs_list_append(list, actor);
  145. }
  146. }
  147. else
  148. if (xs_list_in(list, v) == -1)
  149. list = xs_list_append(list, v);
  150. }
  151. }
  152. return list;
  153. }
  154. int is_msg_public(snac *snac, char *msg)
  155. /* checks if a message is public */
  156. {
  157. int ret = 0;
  158. xs *rcpts = recipient_list(snac, msg, 0);
  159. char *p, *v;
  160. p = rcpts;
  161. while (!ret && xs_list_iter(&p, &v)) {
  162. if (strcmp(v, public_address) == 0)
  163. ret = 1;
  164. }
  165. return ret;
  166. }
  167. void process_tags(const char *content, d_char **n_content, d_char **tag)
  168. /* parses mentions and tags from content */
  169. {
  170. d_char *nc = xs_str_new(NULL);
  171. d_char *tl = xs_list_new();
  172. xs *split;
  173. char *p, *v;
  174. int n = 0;
  175. p = split = xs_regex_split(content, "(@[A-Za-z0-9_]+@[A-Za-z0-9\\.-]+|#[^ ,\\.:;]+)");
  176. while (xs_list_iter(&p, &v)) {
  177. if ((n & 0x1)) {
  178. if (*v == '@') {
  179. /* query the webfinger about this fellow */
  180. xs *actor = NULL;
  181. xs *uid = NULL;
  182. int status;
  183. status = webfinger_request(v + 1, &actor, &uid);
  184. if (valid_status(status)) {
  185. xs *d = xs_dict_new();
  186. xs *n = xs_fmt("@%s", uid);
  187. xs *l = xs_fmt("<a href=\"%s\">%s</a>", actor, n);
  188. d = xs_dict_append(d, "type", "Mention");
  189. d = xs_dict_append(d, "href", actor);
  190. d = xs_dict_append(d, "name", n);
  191. tl = xs_list_append(tl, d);
  192. /* add the code */
  193. nc = xs_str_cat(nc, l);
  194. }
  195. else
  196. /* store as is */
  197. nc = xs_str_cat(nc, v);
  198. }
  199. else
  200. if (*v == '#') {
  201. /* hashtag */
  202. /* store as is by now */
  203. nc = xs_str_cat(nc, v);
  204. }
  205. }
  206. else
  207. nc = xs_str_cat(nc, v);
  208. n++;
  209. }
  210. *n_content = nc;
  211. *tag = tl;
  212. }
  213. /** messages **/
  214. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  215. /* creates a base ActivityPub message */
  216. {
  217. xs *did = NULL;
  218. xs *published = NULL;
  219. /* generated values */
  220. if (date && strcmp(date, "@now") == 0)
  221. date = published = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  222. if (id != NULL) {
  223. if (strcmp(id, "@dummy") == 0) {
  224. xs *ntid = tid(0);
  225. id = did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  226. }
  227. else
  228. if (strcmp(id, "@object") == 0) {
  229. if (object != NULL)
  230. id = did = xs_fmt("%s/%s", xs_dict_get(object, "id"), type);
  231. else
  232. id = NULL;
  233. }
  234. }
  235. d_char *msg = xs_dict_new();
  236. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  237. msg = xs_dict_append(msg, "type", type);
  238. if (id != NULL)
  239. msg = xs_dict_append(msg, "id", id);
  240. if (actor != NULL)
  241. msg = xs_dict_append(msg, "actor", actor);
  242. if (date != NULL)
  243. msg = xs_dict_append(msg, "published", date);
  244. if (object != NULL)
  245. msg = xs_dict_append(msg, "object", object);
  246. return msg;
  247. }
  248. d_char *msg_collection(snac *snac, char *id)
  249. /* creates an empty OrderedCollection message */
  250. {
  251. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  252. xs *ol = xs_list_new();
  253. xs *nz = xs_number_new(0);
  254. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  255. msg = xs_dict_append(msg, "orderedItems", ol);
  256. msg = xs_dict_append(msg, "totalItems", nz);
  257. return msg;
  258. }
  259. d_char *msg_accept(snac *snac, char *object, char *to)
  260. /* creates an Accept message (as a response to a Follow) */
  261. {
  262. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  263. msg = xs_dict_append(msg, "to", to);
  264. return msg;
  265. }
  266. d_char *msg_update(snac *snac, char *object)
  267. /* creates an Update message */
  268. {
  269. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  270. msg = xs_dict_append(msg, "to", public_address);
  271. return msg;
  272. }
  273. d_char *msg_admiration(snac *snac, char *object, char *type)
  274. /* creates a Like or Announce message */
  275. {
  276. xs *a_msg = NULL;
  277. d_char *msg = NULL;
  278. /* call the object */
  279. timeline_request(snac, object, snac->actor);
  280. if ((a_msg = timeline_find(snac, object)) != NULL) {
  281. xs *rcpts = xs_list_new();
  282. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  283. rcpts = xs_list_append(rcpts, public_address);
  284. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  285. msg = xs_dict_append(msg, "to", rcpts);
  286. }
  287. else
  288. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  289. return msg;
  290. }
  291. d_char *msg_actor(snac *snac)
  292. /* create a Person message for this actor */
  293. {
  294. xs *ctxt = xs_list_new();
  295. xs *icon = xs_dict_new();
  296. xs *keys = xs_dict_new();
  297. xs *avtr = NULL;
  298. xs *kid = NULL;
  299. xs *f_bio = NULL;
  300. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  301. char *p;
  302. int n;
  303. /* change the @context (is this really necessary?) */
  304. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  305. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  306. msg = xs_dict_set(msg, "@context", ctxt);
  307. msg = xs_dict_set(msg, "url", snac->actor);
  308. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  309. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  310. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  311. not_really_markdown(xs_dict_get(snac->config, "bio"), &f_bio);
  312. msg = xs_dict_set(msg, "summary", f_bio);
  313. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  314. for (n = 0; folders[n]; n++) {
  315. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  316. msg = xs_dict_set(msg, folders[n], f);
  317. }
  318. p = xs_dict_get(snac->config, "avatar");
  319. if (*p == '\0')
  320. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  321. else
  322. avtr = xs_dup(p);
  323. icon = xs_dict_append(icon, "type", "Image");
  324. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  325. icon = xs_dict_append(icon, "url", avtr);
  326. msg = xs_dict_set(msg, "icon", icon);
  327. kid = xs_fmt("%s#main-key", snac->actor);
  328. keys = xs_dict_append(keys, "id", kid);
  329. keys = xs_dict_append(keys, "owner", snac->actor);
  330. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  331. msg = xs_dict_set(msg, "publicKey", keys);
  332. return msg;
  333. }
  334. d_char *msg_create(snac *snac, char *object)
  335. /* creates a 'Create' message */
  336. {
  337. d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  338. msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
  339. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  340. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  341. return msg;
  342. }
  343. d_char *msg_undo(snac *snac, char *object)
  344. /* creates an 'Undo' message */
  345. {
  346. d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  347. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  348. return msg;
  349. }
  350. d_char *msg_delete(snac *snac, char *id)
  351. /* creates a 'Delete' + 'Tombstone' for a local entry */
  352. {
  353. xs *tomb = xs_dict_new();
  354. d_char *msg = NULL;
  355. /* sculpt the tombstone */
  356. tomb = xs_dict_append(tomb, "type", "Tombstone");
  357. tomb = xs_dict_append(tomb, "id", id);
  358. /* now create the Delete */
  359. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  360. msg = xs_dict_append(msg, "to", public_address);
  361. return msg;
  362. }
  363. d_char *msg_follow(snac *snac, char *url_or_uid)
  364. /* creates a 'Follow' message */
  365. {
  366. xs *actor_o = NULL;
  367. xs *actor = NULL;
  368. d_char *msg = NULL;
  369. int status;
  370. if (xs_startswith(url_or_uid, "https:/"))
  371. actor = xs_dup(url_or_uid);
  372. else
  373. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL))) {
  374. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  375. return NULL;
  376. }
  377. /* request the actor */
  378. status = actor_request(snac, actor, &actor_o);
  379. if (valid_status(status)) {
  380. /* check if the actor is an alias */
  381. char *r_actor = xs_dict_get(actor_o, "id");
  382. if (r_actor && strcmp(actor, r_actor) != 0) {
  383. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  384. actor = r_actor;
  385. }
  386. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, actor);
  387. }
  388. else
  389. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  390. return msg;
  391. }
  392. d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to, char *attach)
  393. /* creates a 'Note' message */
  394. {
  395. xs *ntid = tid(0);
  396. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  397. xs *ctxt = NULL;
  398. xs *fc2 = NULL;
  399. xs *fc1 = NULL;
  400. xs *to = NULL;
  401. xs *cc = xs_list_new();
  402. xs *irt = NULL;
  403. xs *tag = NULL;
  404. xs *atls = NULL;
  405. d_char *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  406. char *p, *v;
  407. if (rcpts == NULL)
  408. to = xs_list_new();
  409. else {
  410. if (xs_type(rcpts) == XSTYPE_STRING) {
  411. to = xs_list_new();
  412. to = xs_list_append(to, rcpts);
  413. }
  414. else
  415. to = xs_dup(rcpts);
  416. }
  417. /* format the content */
  418. not_really_markdown(content, &fc2);
  419. /* extract the tags */
  420. process_tags(fc2, &fc1, &tag);
  421. if (in_reply_to != NULL) {
  422. xs *p_msg = NULL;
  423. /* demand this thing */
  424. timeline_request(snac, in_reply_to, NULL);
  425. if ((p_msg = timeline_find(snac, in_reply_to)) != NULL) {
  426. /* add this author as recipient */
  427. char *v;
  428. if ((v = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, v) == -1)
  429. to = xs_list_append(to, v);
  430. if ((v = xs_dict_get(p_msg, "context")))
  431. ctxt = xs_dup(v);
  432. /* if this message is public, ours will also be */
  433. if (is_msg_public(snac, p_msg) &&
  434. xs_list_in(to, public_address) == -1)
  435. to = xs_list_append(to, public_address);
  436. }
  437. irt = xs_dup(in_reply_to);
  438. }
  439. else
  440. irt = xs_val_new(XSTYPE_NULL);
  441. /* create the attachment list, if there are any */
  442. if (!xs_is_null(attach) && *attach != '\0') {
  443. xs *lsof1 = NULL;
  444. if (xs_type(attach) == XSTYPE_STRING) {
  445. lsof1 = xs_list_append(xs_list_new(), attach);
  446. attach = lsof1;
  447. }
  448. atls = xs_list_new();
  449. while (xs_list_iter(&attach, &v)) {
  450. xs *d = xs_dict_new();
  451. char *mime = xs_mime_by_ext(v);
  452. d = xs_dict_append(d, "mediaType", mime);
  453. d = xs_dict_append(d, "url", v);
  454. d = xs_dict_append(d, "name", "");
  455. d = xs_dict_append(d, "type",
  456. xs_startswith(mime, "image/") ? "Image" : "Document");
  457. atls = xs_list_append(atls, d);
  458. }
  459. }
  460. if (tag == NULL)
  461. tag = xs_list_new();
  462. if (ctxt == NULL)
  463. ctxt = xs_fmt("%s#ctxt", id);
  464. /* add all mentions to the cc */
  465. p = tag;
  466. while (xs_list_iter(&p, &v)) {
  467. if (xs_type(v) == XSTYPE_DICT) {
  468. char *t;
  469. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  470. if ((t = xs_dict_get(v, "href")) != NULL)
  471. cc = xs_list_append(cc, t);
  472. }
  473. }
  474. }
  475. /* no recipients? must be for everybody */
  476. if (xs_list_len(to) == 0)
  477. to = xs_list_append(to, public_address);
  478. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  479. msg = xs_dict_append(msg, "summary", "");
  480. msg = xs_dict_append(msg, "content", fc1);
  481. msg = xs_dict_append(msg, "context", ctxt);
  482. msg = xs_dict_append(msg, "url", id);
  483. msg = xs_dict_append(msg, "to", to);
  484. msg = xs_dict_append(msg, "cc", cc);
  485. msg = xs_dict_append(msg, "inReplyTo", irt);
  486. msg = xs_dict_append(msg, "tag", tag);
  487. if (atls != NULL)
  488. msg = xs_dict_append(msg, "attachment", atls);
  489. return msg;
  490. }
  491. void notify(snac *snac, char *type, char *utype, char *actor, char *msg)
  492. /* notifies the user of relevant events */
  493. {
  494. char *email = xs_dict_get(snac->config, "email");
  495. char *object = NULL;
  496. /* no email address? done */
  497. if (xs_is_null(email) || *email == '\0')
  498. return;
  499. if (strcmp(type, "Create") == 0) {
  500. /* only notify of notes specifically for us */
  501. char *rcpts = recipient_list(snac, msg, 0);
  502. if (xs_list_in(rcpts, snac->actor) == -1)
  503. return;
  504. }
  505. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  506. return;
  507. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  508. object = xs_dict_get(msg, "object");
  509. if (xs_is_null(object))
  510. return;
  511. else {
  512. if (xs_type(object) == XSTYPE_DICT)
  513. object = xs_dict_get(object, "id");
  514. /* if it's not an admiration about something by us, done */
  515. if (xs_is_null(object) || !xs_startswith(object, snac->actor))
  516. return;
  517. }
  518. }
  519. snac_debug(snac, 1, xs_fmt("notify(%s, %s, %s)", type, utype, actor));
  520. /* prepare message */
  521. xs *subject = xs_fmt("snac notify for @%s@%s",
  522. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  523. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  524. xs *header = xs_fmt(
  525. "From: %s\n"
  526. "To: %s\n"
  527. "Subject: %s\n"
  528. "\n",
  529. from, email, subject);
  530. xs *body = xs_str_new(header);
  531. if (strcmp(utype, "(null)") != 0) {
  532. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  533. body = xs_str_cat(body, s1);
  534. }
  535. else {
  536. xs *s1 = xs_fmt("Type : %s\n", type);
  537. body = xs_str_cat(body, s1);
  538. }
  539. {
  540. xs *s1 = xs_fmt("Actor : %s\n", actor);
  541. body = xs_str_cat(body, s1);
  542. }
  543. if (object != NULL) {
  544. xs *s1 = xs_fmt("Object: %s\n", object);
  545. body = xs_str_cat(body, s1);
  546. }
  547. enqueue_email(snac, body, 0);
  548. }
  549. /** queues **/
  550. int process_message(snac *snac, char *msg, char *req)
  551. /* processes an ActivityPub message from the input queue */
  552. {
  553. /* actor and type exist, were checked previously */
  554. char *actor = xs_dict_get(msg, "actor");
  555. char *type = xs_dict_get(msg, "type");
  556. xs *actor_o = NULL;
  557. int a_status;
  558. int do_notify = 0;
  559. char *object, *utype;
  560. object = xs_dict_get(msg, "object");
  561. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  562. utype = xs_dict_get(object, "type");
  563. else
  564. utype = "(null)";
  565. /* bring the actor */
  566. a_status = actor_request(snac, actor, &actor_o);
  567. /* if the actor does not explicitly exist, discard */
  568. if (a_status == 404 || a_status == 410) {
  569. snac_debug(snac, 1,
  570. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  571. return 1;
  572. }
  573. if (!valid_status(a_status)) {
  574. /* other actor download errors may need a retry */
  575. snac_debug(snac, 1,
  576. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  577. return 0;
  578. }
  579. /* check the signature */
  580. if (!check_signature(snac, req)) {
  581. snac_log(snac, xs_fmt("bad signature"));
  582. return 1;
  583. }
  584. if (strcmp(type, "Follow") == 0) {
  585. xs *f_msg = xs_dup(msg);
  586. xs *reply = msg_accept(snac, f_msg, actor);
  587. post(snac, reply);
  588. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  589. /* add a date if it doesn't include one (Mastodon) */
  590. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  591. f_msg = xs_dict_set(f_msg, "published", date);
  592. }
  593. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg, NULL, NULL);
  594. follower_add(snac, actor, f_msg);
  595. snac_log(snac, xs_fmt("New follower %s", actor));
  596. do_notify = 1;
  597. }
  598. else
  599. if (strcmp(type, "Undo") == 0) {
  600. if (strcmp(utype, "Follow") == 0) {
  601. if (valid_status(follower_del(snac, actor))) {
  602. snac_log(snac, xs_fmt("no longer following us %s", actor));
  603. do_notify = 1;
  604. }
  605. else
  606. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  607. }
  608. else
  609. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  610. }
  611. else
  612. if (strcmp(type, "Create") == 0) {
  613. if (strcmp(utype, "Note") == 0) {
  614. if (is_muted(snac, actor))
  615. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  616. else {
  617. char *id = xs_dict_get(object, "id");
  618. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  619. timeline_request(snac, in_reply_to, NULL);
  620. if (timeline_add(snac, id, object, in_reply_to, NULL)) {
  621. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  622. do_notify = 1;
  623. }
  624. }
  625. }
  626. else
  627. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  628. }
  629. else
  630. if (strcmp(type, "Accept") == 0) {
  631. if (strcmp(utype, "Follow") == 0) {
  632. if (following_check(snac, actor)) {
  633. following_add(snac, actor, msg);
  634. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  635. }
  636. else
  637. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  638. }
  639. else
  640. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  641. }
  642. else
  643. if (strcmp(type, "Like") == 0) {
  644. if (xs_type(object) == XSTYPE_DICT)
  645. object = xs_dict_get(object, "id");
  646. timeline_admire(snac, object, actor, 1);
  647. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  648. do_notify = 1;
  649. }
  650. else
  651. if (strcmp(type, "Announce") == 0) {
  652. xs *a_msg = NULL;
  653. if (xs_type(object) == XSTYPE_DICT)
  654. object = xs_dict_get(object, "id");
  655. timeline_request(snac, object, actor);
  656. if ((a_msg = timeline_find(snac, object)) != NULL) {
  657. char *who = xs_dict_get(a_msg, "attributedTo");
  658. if (who && !is_muted(snac, who)) {
  659. /* bring the actor */
  660. xs *who_o = NULL;
  661. if (valid_status(actor_request(snac, who, &who_o))) {
  662. timeline_admire(snac, object, actor, 0);
  663. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  664. do_notify = 1;
  665. }
  666. else
  667. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  668. }
  669. else
  670. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  671. }
  672. else
  673. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  674. }
  675. else
  676. if (strcmp(type, "Update") == 0) {
  677. if (strcmp(utype, "Person") == 0) {
  678. actor_add(snac, actor, xs_dict_get(msg, "object"));
  679. snac_log(snac, xs_fmt("updated actor %s", actor));
  680. }
  681. else
  682. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  683. }
  684. else
  685. if (strcmp(type, "Delete") == 0) {
  686. if (xs_type(object) == XSTYPE_DICT)
  687. object = xs_dict_get(object, "id");
  688. if (valid_status(timeline_del(snac, object)))
  689. snac_log(snac, xs_fmt("New 'Delete' %s %s", actor, object));
  690. else
  691. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  692. }
  693. else
  694. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  695. if (do_notify)
  696. notify(snac, type, utype, actor, msg);
  697. return 1;
  698. }
  699. void process_queue(snac *snac)
  700. /* processes the queue */
  701. {
  702. xs *list;
  703. char *p, *fn;
  704. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  705. list = queue(snac);
  706. p = list;
  707. while (xs_list_iter(&p, &fn)) {
  708. xs *q_item = dequeue(snac, fn);
  709. char *type;
  710. if (q_item == NULL) {
  711. snac_log(snac, xs_fmt("process_queue q_item error"));
  712. continue;
  713. }
  714. if ((type = xs_dict_get(q_item, "type")) == NULL)
  715. type = "output";
  716. if (strcmp(type, "output") == 0) {
  717. int status;
  718. char *actor = xs_dict_get(q_item, "actor");
  719. char *msg = xs_dict_get(q_item, "object");
  720. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  721. xs *payload = NULL;
  722. int p_size = 0;
  723. /* deliver */
  724. status = send_to_actor(snac, actor, msg, &payload, &p_size);
  725. if (!valid_status(status)) {
  726. /* error sending; reenqueue? */
  727. if (retries > queue_retry_max)
  728. snac_log(snac, xs_fmt("process_queue giving up %s %d", actor, status));
  729. else {
  730. /* reenqueue */
  731. enqueue_output(snac, msg, actor, retries + 1);
  732. snac_log(snac, xs_fmt("process_queue requeue %s %d", actor, retries + 1));
  733. }
  734. }
  735. }
  736. else
  737. if (strcmp(type, "input") == 0) {
  738. /* process the message */
  739. char *msg = xs_dict_get(q_item, "object");
  740. char *req = xs_dict_get(q_item, "req");
  741. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  742. if (!process_message(snac, msg, req)) {
  743. if (retries > queue_retry_max)
  744. snac_log(snac, xs_fmt("process_queue input giving up"));
  745. else {
  746. /* reenqueue */
  747. enqueue_input(snac, msg, req, retries + 1);
  748. snac_log(snac, xs_fmt("process_queue input requeue %d", retries + 1));
  749. }
  750. }
  751. }
  752. else
  753. if (strcmp(type, "email") == 0) {
  754. /* send this email */
  755. char *msg = xs_dict_get(q_item, "message");
  756. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  757. FILE *f;
  758. int ok = 0;
  759. if ((f = popen("/usr/sbin/sendmail -t", "w")) != NULL) {
  760. fprintf(f, "%s\n", msg);
  761. if (fclose(f) != EOF)
  762. ok = 1;
  763. }
  764. if (ok)
  765. snac_debug(snac, 1, xs_fmt("email message sent"));
  766. else {
  767. if (retries > queue_retry_max)
  768. snac_log(snac, xs_fmt("process_queue email giving up (errno: %d)", errno));
  769. else {
  770. /* requeue */
  771. snac_log(snac, xs_fmt(
  772. "process_queue email requeue %d (errno: %d)", retries + 1, errno));
  773. enqueue_email(snac, msg, retries + 1);
  774. }
  775. }
  776. }
  777. }
  778. }
  779. void post(snac *snac, char *msg)
  780. /* enqueues a message to all its recipients */
  781. {
  782. xs *rcpts = recipient_list(snac, msg, 1);
  783. char *p, *v;
  784. p = rcpts;
  785. while (xs_list_iter(&p, &v)) {
  786. enqueue_output(snac, msg, v, 0);
  787. }
  788. }
  789. /** HTTP handlers */
  790. int activitypub_get_handler(d_char *req, char *q_path,
  791. char **body, int *b_size, char **ctype)
  792. {
  793. int status = 200;
  794. char *accept = xs_dict_get(req, "accept");
  795. snac snac;
  796. xs *msg = NULL;
  797. if (accept == NULL)
  798. return 0;
  799. if (xs_str_in(accept, "application/activity+json") == -1 &&
  800. xs_str_in(accept, "application/ld+json") == -1)
  801. return 0;
  802. xs *l = xs_split_n(q_path, "/", 2);
  803. char *uid, *p_path;
  804. uid = xs_list_get(l, 1);
  805. if (!user_open(&snac, uid)) {
  806. /* invalid user */
  807. srv_log(xs_fmt("activitypub_get_handler bad user %s", uid));
  808. return 404;
  809. }
  810. p_path = xs_list_get(l, 2);
  811. *ctype = "application/activity+json";
  812. if (p_path == NULL) {
  813. /* if there was no component after the user, it's an actor request */
  814. msg = msg_actor(&snac);
  815. *ctype = "application/ld+json";
  816. }
  817. else
  818. if (strcmp(p_path, "outbox") == 0) {
  819. xs *id = xs_fmt("%s/outbox", snac.actor);
  820. xs *elems = local_list(&snac, 20);
  821. xs *list = xs_list_new();
  822. msg = msg_collection(&snac, id);
  823. char *p, *v;
  824. p = elems;
  825. while (xs_list_iter(&p, &v)) {
  826. xs *i = timeline_get(&snac, v);
  827. char *type = xs_dict_get(i, "type");
  828. char *id = xs_dict_get(i, "id");
  829. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  830. i = xs_dict_del(i, "_snac");
  831. list = xs_list_append(list, i);
  832. }
  833. }
  834. /* replace the 'orderedItems' with the latest posts */
  835. xs *items = xs_number_new(xs_list_len(list));
  836. msg = xs_dict_set(msg, "orderedItems", list);
  837. msg = xs_dict_set(msg, "totalItems", items);
  838. }
  839. else
  840. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  841. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  842. msg = msg_collection(&snac, id);
  843. }
  844. else
  845. if (xs_startswith(p_path, "p/")) {
  846. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  847. if ((msg = timeline_find(&snac, id)) != NULL)
  848. msg = xs_dict_del(msg, "_snac");
  849. else
  850. status = 404;
  851. }
  852. else
  853. status = 404;
  854. if (status == 200 && msg != NULL) {
  855. *body = xs_json_dumps_pp(msg, 4);
  856. *b_size = strlen(*body);
  857. }
  858. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  859. user_free(&snac);
  860. return status;
  861. }
  862. int activitypub_post_handler(d_char *req, char *q_path,
  863. d_char *payload, int p_size,
  864. char **body, int *b_size, char **ctype)
  865. /* processes an input message */
  866. {
  867. int status = 202; /* accepted */
  868. char *i_ctype = xs_dict_get(req, "content-type");
  869. snac snac;
  870. char *v;
  871. if (i_ctype == NULL)
  872. return 400;
  873. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  874. xs_str_in(i_ctype, "application/ld+json") == -1)
  875. return 0;
  876. /* decode the message */
  877. xs *msg = xs_json_loads(payload);
  878. if (msg == NULL) {
  879. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  880. status = 400;
  881. }
  882. /* get the user and path */
  883. xs *l = xs_split_n(q_path, "/", 2);
  884. char *uid;
  885. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  886. /* strange q_path */
  887. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  888. return 404;
  889. }
  890. uid = xs_list_get(l, 1);
  891. if (!user_open(&snac, uid)) {
  892. /* invalid user */
  893. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  894. return 404;
  895. }
  896. /* if it has a digest, check it now, because
  897. later the payload won't be exactly the same */
  898. if ((v = xs_dict_get(req, "digest")) != NULL) {
  899. xs *s1 = xs_sha256_base64(payload, p_size);
  900. xs *s2 = xs_fmt("SHA-256=%s", s1);
  901. if (strcmp(s2, v) != 0) {
  902. srv_log(xs_fmt("digest check FAILED"));
  903. status = 400;
  904. }
  905. }
  906. if (valid_status(status)) {
  907. enqueue_input(&snac, msg, req, 0);
  908. *ctype = "application/activity+json";
  909. }
  910. user_free(&snac);
  911. return status;
  912. }