activitypub.c 37 KB

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