activitypub.c 41 KB

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