activitypub.c 36 KB

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