activitypub.c 37 KB

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