activitypub.c 32 KB

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