activitypub.c 34 KB

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