activitypub.c 36 KB

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