activitypub.c 34 KB

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