activitypub.c 41 KB

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