activitypub.c 33 KB

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