activitypub.c 34 KB

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