activitypub.c 36 KB

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