activitypub.c 42 KB

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