activitypub.c 42 KB

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