activitypub.c 36 KB

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