activitypub.c 36 KB

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