activitypub.c 42 KB

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