activitypub.c 35 KB

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