activitypub.c 34 KB

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