activitypub.c 39 KB

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