activitypub.c 43 KB

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