activitypub.c 32 KB

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