activitypub.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "xs_curl.h"
  7. #include "xs_mime.h"
  8. #include "xs_openssl.h"
  9. #include "xs_regex.h"
  10. #include "xs_time.h"
  11. #include "xs_set.h"
  12. #include "snac.h"
  13. #include <sys/wait.h>
  14. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  15. /* susie.png */
  16. const char *susie =
  17. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  18. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  19. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  20. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  21. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  22. const char *susie_cool =
  23. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  24. "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
  25. "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
  26. "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
  27. "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
  28. const char *default_avatar_base64(void)
  29. /* returns the default avatar in base64 */
  30. {
  31. time_t t = time(NULL);
  32. struct tm tm;
  33. gmtime_r(&t, &tm);
  34. return tm.tm_wday == 0 || tm.tm_wday == 6 ? susie_cool : susie;
  35. }
  36. int activitypub_request(snac *snac, char *url, d_char **data)
  37. /* request an object */
  38. {
  39. int status;
  40. xs *response = NULL;
  41. xs *payload = NULL;
  42. int p_size;
  43. char *ctype;
  44. /* get from the net */
  45. response = http_signed_request(snac, "GET", url,
  46. NULL, NULL, 0, &status, &payload, &p_size, 0);
  47. if (status == 0 || (status >= 500 && status <= 599)) {
  48. /* I found an instance running Misskey that returned
  49. 500 on signed messages but returned the object
  50. perfectly without signing (?), so why not try */
  51. xs_free(response);
  52. xs *hdrs = xs_dict_new();
  53. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  54. response = xs_http_request("GET", url, hdrs,
  55. NULL, 0, &status, &payload, &p_size, 0);
  56. }
  57. if (valid_status(status)) {
  58. /* ensure it's ActivityPub data */
  59. ctype = xs_dict_get(response, "content-type");
  60. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  61. xs_str_in(ctype, "application/ld+json") != -1)
  62. *data = xs_json_loads(payload);
  63. else
  64. status = 500;
  65. }
  66. if (!valid_status(status))
  67. *data = NULL;
  68. return status;
  69. }
  70. int actor_request(snac *snac, char *actor, d_char **data)
  71. /* request an actor */
  72. {
  73. int status, status2;
  74. xs *payload = NULL;
  75. /* get from disk first */
  76. status = actor_get(snac, actor, data);
  77. if (status == 200)
  78. return status;
  79. /* actor data non-existent or stale: get from the net */
  80. status2 = activitypub_request(snac, actor, &payload);
  81. if (valid_status(status2)) {
  82. /* renew data */
  83. status = actor_add(snac, actor, payload);
  84. if (data != NULL) {
  85. *data = payload;
  86. payload = NULL;
  87. }
  88. }
  89. return status;
  90. }
  91. int timeline_request(snac *snac, char **id, d_char **wrk)
  92. /* ensures that an entry and its ancestors are in the timeline */
  93. {
  94. int status = 0;
  95. if (!xs_is_null(*id)) {
  96. /* is the admired object already there? */
  97. if (!object_here(*id)) {
  98. xs *object = NULL;
  99. /* no; download it */
  100. status = activitypub_request(snac, *id, &object);
  101. if (valid_status(status)) {
  102. char *type = xs_dict_get(object, "type");
  103. /* get the id again from the object, as it may be different */
  104. char *nid = xs_dict_get(object, "id");
  105. if (wrk && strcmp(nid, *id) != 0) {
  106. snac_debug(snac, 1,
  107. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  108. *wrk = xs_dup(nid);
  109. *id = *wrk;
  110. }
  111. if (!xs_is_null(type) && strcmp(type, "Note") == 0) {
  112. char *actor = xs_dict_get(object, "attributedTo");
  113. /* request (and drop) the actor for this entry */
  114. if (!xs_is_null(actor))
  115. actor_request(snac, actor, NULL);
  116. /* does it have an ancestor? */
  117. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  118. /* recurse! */
  119. timeline_request(snac, &in_reply_to, NULL);
  120. /* finally store */
  121. timeline_add(snac, *id, object);
  122. }
  123. }
  124. }
  125. }
  126. return status;
  127. }
  128. int send_to_inbox_raw(const char *keyid, const char *seckey,
  129. const xs_str *inbox, const xs_dict *msg,
  130. xs_val **payload, int *p_size, int timeout)
  131. /* sends a message to an Inbox */
  132. {
  133. int status;
  134. xs_dict *response;
  135. xs *j_msg = xs_json_dumps_pp((xs_dict *)msg, 4);
  136. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  137. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  138. xs_free(response);
  139. return status;
  140. }
  141. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  142. xs_val **payload, int *p_size, int timeout)
  143. /* sends a message to an Inbox */
  144. {
  145. char *seckey = xs_dict_get(snac->key, "secret");
  146. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  147. }
  148. d_char *get_actor_inbox(snac *snac, char *actor)
  149. /* gets an actor's inbox */
  150. {
  151. xs *data = NULL;
  152. char *v = NULL;
  153. if (valid_status(actor_request(snac, actor, &data))) {
  154. /* try first endpoints/sharedInbox */
  155. if ((v = xs_dict_get(data, "endpoints")))
  156. v = xs_dict_get(v, "sharedInbox");
  157. /* try then the regular inbox */
  158. if (xs_is_null(v))
  159. v = xs_dict_get(data, "inbox");
  160. }
  161. return xs_is_null(v) ? NULL : xs_dup(v);
  162. }
  163. int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_size, int timeout)
  164. /* sends a message to an actor */
  165. {
  166. int status = 400;
  167. xs *inbox = get_actor_inbox(snac, actor);
  168. if (!xs_is_null(inbox))
  169. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  170. return status;
  171. }
  172. d_char *recipient_list(snac *snac, char *msg, int expand_public)
  173. /* returns the list of recipients for a message */
  174. {
  175. char *to = xs_dict_get(msg, "to");
  176. char *cc = xs_dict_get(msg, "cc");
  177. xs_set rcpts;
  178. int n;
  179. xs_set_init(&rcpts);
  180. char *lists[] = { to, cc, NULL };
  181. for (n = 0; lists[n]; n++) {
  182. char *l = lists[n];
  183. char *v;
  184. xs *tl = NULL;
  185. /* if it's a string, create a list with only one element */
  186. if (xs_type(l) == XSTYPE_STRING) {
  187. tl = xs_list_new();
  188. tl = xs_list_append(tl, l);
  189. l = tl;
  190. }
  191. while (xs_list_iter(&l, &v)) {
  192. if (expand_public && strcmp(v, public_address) == 0) {
  193. /* iterate the followers and add them */
  194. xs *fwers = follower_list(snac);
  195. char *actor;
  196. char *p = fwers;
  197. while (xs_list_iter(&p, &actor))
  198. xs_set_add(&rcpts, actor);
  199. }
  200. else
  201. xs_set_add(&rcpts, v);
  202. }
  203. }
  204. return xs_set_result(&rcpts);
  205. }
  206. d_char *inbox_list(snac *snac, char *msg)
  207. /* returns the list of inboxes that are recipients of this message */
  208. {
  209. xs *rcpts = recipient_list(snac, msg, 1);
  210. xs_set inboxes;
  211. char *p, *v;
  212. xs_set_init(&inboxes);
  213. p = rcpts;
  214. while (xs_list_iter(&p, &v)) {
  215. xs *inbox;
  216. if ((inbox = get_actor_inbox(snac, v)) != NULL) {
  217. /* add the inbox if it's not already there */
  218. xs_set_add(&inboxes, inbox);
  219. }
  220. else
  221. snac_log(snac, xs_fmt("cannot find inbox for %s", v));
  222. }
  223. return xs_set_result(&inboxes);
  224. }
  225. int is_msg_public(snac *snac, char *msg)
  226. /* checks if a message is public */
  227. {
  228. int ret = 0;
  229. xs *rcpts = recipient_list(snac, msg, 0);
  230. char *p, *v;
  231. p = rcpts;
  232. while (!ret && xs_list_iter(&p, &v)) {
  233. if (strcmp(v, public_address) == 0)
  234. ret = 1;
  235. }
  236. return ret;
  237. }
  238. void process_tags(snac *snac, const char *content, d_char **n_content, d_char **tag)
  239. /* parses mentions and tags from content */
  240. {
  241. d_char *nc = xs_str_new(NULL);
  242. d_char *tl = xs_list_new();
  243. xs *split;
  244. char *p, *v;
  245. int n = 0;
  246. split = xs_regex_split(content, "(@[A-Za-z0-9_]+@[A-Za-z0-9\\.-]+|&#[0-9]+;|#[^ ,\\.:;<]+)");
  247. p = split;
  248. while (xs_list_iter(&p, &v)) {
  249. if ((n & 0x1)) {
  250. if (*v == '@') {
  251. /* query the webfinger about this fellow */
  252. xs *v2 = xs_strip_chars_i(xs_dup(v), "@.");
  253. xs *actor = NULL;
  254. xs *uid = NULL;
  255. int status;
  256. status = webfinger_request(v2, &actor, &uid);
  257. if (valid_status(status)) {
  258. xs *d = xs_dict_new();
  259. xs *n = xs_fmt("@%s", uid);
  260. xs *l = xs_fmt("<a href=\"%s\" class=\"u-url mention\">%s</a>", actor, n);
  261. d = xs_dict_append(d, "type", "Mention");
  262. d = xs_dict_append(d, "href", actor);
  263. d = xs_dict_append(d, "name", n);
  264. tl = xs_list_append(tl, d);
  265. /* add the code */
  266. nc = xs_str_cat(nc, l);
  267. }
  268. else
  269. /* store as is */
  270. nc = xs_str_cat(nc, v);
  271. }
  272. else
  273. if (*v == '#') {
  274. /* hashtag */
  275. xs *d = xs_dict_new();
  276. xs *n = xs_tolower_i(xs_dup(v));
  277. xs *h = xs_fmt("%s%s", snac->actor, n);
  278. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  279. d = xs_dict_append(d, "type", "Hashtag");
  280. d = xs_dict_append(d, "href", h);
  281. d = xs_dict_append(d, "name", n);
  282. tl = xs_list_append(tl, d);
  283. /* add the code */
  284. nc = xs_str_cat(nc, l);
  285. }
  286. else
  287. if (*v == '&') {
  288. /* HTML Unicode entity, probably part of an emoji */
  289. /* write as is */
  290. nc = xs_str_cat(nc, v);
  291. }
  292. }
  293. else
  294. nc = xs_str_cat(nc, v);
  295. n++;
  296. }
  297. *n_content = nc;
  298. *tag = tl;
  299. }
  300. /** messages **/
  301. d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object)
  302. /* creates a base ActivityPub message */
  303. {
  304. xs *did = NULL;
  305. xs *published = NULL;
  306. xs *ntid = tid(0);
  307. /* generated values */
  308. if (date && strcmp(date, "@now") == 0) {
  309. published = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  310. date = published;
  311. }
  312. if (id != NULL) {
  313. if (strcmp(id, "@dummy") == 0) {
  314. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  315. id = did;
  316. }
  317. else
  318. if (strcmp(id, "@object") == 0) {
  319. if (object != NULL) {
  320. did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
  321. id = did;
  322. }
  323. else
  324. id = NULL;
  325. }
  326. }
  327. d_char *msg = xs_dict_new();
  328. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  329. msg = xs_dict_append(msg, "type", type);
  330. if (id != NULL)
  331. msg = xs_dict_append(msg, "id", id);
  332. if (actor != NULL)
  333. msg = xs_dict_append(msg, "actor", actor);
  334. if (date != NULL)
  335. msg = xs_dict_append(msg, "published", date);
  336. if (object != NULL)
  337. msg = xs_dict_append(msg, "object", object);
  338. return msg;
  339. }
  340. d_char *msg_collection(snac *snac, char *id)
  341. /* creates an empty OrderedCollection message */
  342. {
  343. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  344. xs *ol = xs_list_new();
  345. xs *nz = xs_number_new(0);
  346. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  347. msg = xs_dict_append(msg, "orderedItems", ol);
  348. msg = xs_dict_append(msg, "totalItems", nz);
  349. return msg;
  350. }
  351. d_char *msg_accept(snac *snac, char *object, char *to)
  352. /* creates an Accept message (as a response to a Follow) */
  353. {
  354. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  355. msg = xs_dict_append(msg, "to", to);
  356. return msg;
  357. }
  358. d_char *msg_update(snac *snac, char *object)
  359. /* creates an Update message */
  360. {
  361. d_char *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  362. msg = xs_dict_append(msg, "to", public_address);
  363. return msg;
  364. }
  365. d_char *msg_admiration(snac *snac, char *object, char *type)
  366. /* creates a Like or Announce message */
  367. {
  368. xs *a_msg = NULL;
  369. d_char *msg = NULL;
  370. xs *wrk = NULL;
  371. /* call the object */
  372. timeline_request(snac, &object, &wrk);
  373. if (valid_status(object_get(object, &a_msg))) {
  374. xs *rcpts = xs_list_new();
  375. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  376. rcpts = xs_list_append(rcpts, public_address);
  377. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  378. msg = xs_dict_append(msg, "to", rcpts);
  379. }
  380. else
  381. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  382. return msg;
  383. }
  384. d_char *msg_actor(snac *snac)
  385. /* create a Person message for this actor */
  386. {
  387. xs *ctxt = xs_list_new();
  388. xs *icon = xs_dict_new();
  389. xs *keys = xs_dict_new();
  390. xs *avtr = NULL;
  391. xs *kid = NULL;
  392. xs *f_bio = NULL;
  393. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  394. char *p;
  395. int n;
  396. /* change the @context (is this really necessary?) */
  397. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  398. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  399. msg = xs_dict_set(msg, "@context", ctxt);
  400. msg = xs_dict_set(msg, "url", snac->actor);
  401. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  402. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  403. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  404. f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"));
  405. msg = xs_dict_set(msg, "summary", f_bio);
  406. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  407. for (n = 0; folders[n]; n++) {
  408. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  409. msg = xs_dict_set(msg, folders[n], f);
  410. }
  411. p = xs_dict_get(snac->config, "avatar");
  412. if (*p == '\0')
  413. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  414. else
  415. avtr = xs_dup(p);
  416. icon = xs_dict_append(icon, "type", "Image");
  417. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  418. icon = xs_dict_append(icon, "url", avtr);
  419. msg = xs_dict_set(msg, "icon", icon);
  420. kid = xs_fmt("%s#main-key", snac->actor);
  421. keys = xs_dict_append(keys, "id", kid);
  422. keys = xs_dict_append(keys, "owner", snac->actor);
  423. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  424. msg = xs_dict_set(msg, "publicKey", keys);
  425. return msg;
  426. }
  427. d_char *msg_create(snac *snac, char *object)
  428. /* creates a 'Create' message */
  429. {
  430. d_char *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  431. msg = xs_dict_append(msg, "attributedTo", xs_dict_get(object, "attributedTo"));
  432. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  433. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  434. return msg;
  435. }
  436. d_char *msg_undo(snac *snac, char *object)
  437. /* creates an 'Undo' message */
  438. {
  439. d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  440. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  441. return msg;
  442. }
  443. d_char *msg_delete(snac *snac, char *id)
  444. /* creates a 'Delete' + 'Tombstone' for a local entry */
  445. {
  446. xs *tomb = xs_dict_new();
  447. d_char *msg = NULL;
  448. /* sculpt the tombstone */
  449. tomb = xs_dict_append(tomb, "type", "Tombstone");
  450. tomb = xs_dict_append(tomb, "id", id);
  451. /* now create the Delete */
  452. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  453. msg = xs_dict_append(msg, "to", public_address);
  454. return msg;
  455. }
  456. d_char *msg_follow(snac *snac, char *url_or_uid)
  457. /* creates a 'Follow' message */
  458. {
  459. xs *actor_o = NULL;
  460. xs *actor = NULL;
  461. d_char *msg = NULL;
  462. int status;
  463. if (xs_startswith(url_or_uid, "https:/"))
  464. actor = xs_dup(url_or_uid);
  465. else
  466. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL))) {
  467. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  468. return NULL;
  469. }
  470. /* request the actor */
  471. status = actor_request(snac, actor, &actor_o);
  472. if (valid_status(status)) {
  473. /* check if the actor is an alias */
  474. char *r_actor = xs_dict_get(actor_o, "id");
  475. if (r_actor && strcmp(actor, r_actor) != 0) {
  476. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  477. }
  478. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  479. }
  480. else
  481. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  482. return msg;
  483. }
  484. xs_dict *msg_note(snac *snac, xs_str *content, xs_val *rcpts,
  485. xs_str *in_reply_to, xs_list *attach, int priv)
  486. /* creates a 'Note' message */
  487. {
  488. xs *ntid = tid(0);
  489. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  490. xs *ctxt = NULL;
  491. xs *fc2 = NULL;
  492. xs *fc1 = NULL;
  493. xs *to = NULL;
  494. xs *cc = xs_list_new();
  495. xs *irt = NULL;
  496. xs *tag = NULL;
  497. xs *atls = NULL;
  498. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  499. xs_list *p;
  500. xs_val *v;
  501. if (rcpts == NULL)
  502. to = xs_list_new();
  503. else {
  504. if (xs_type(rcpts) == XSTYPE_STRING) {
  505. to = xs_list_new();
  506. to = xs_list_append(to, rcpts);
  507. }
  508. else
  509. to = xs_dup(rcpts);
  510. }
  511. /* format the content */
  512. fc2 = not_really_markdown(content);
  513. /* extract the tags */
  514. process_tags(snac, fc2, &fc1, &tag);
  515. if (tag == NULL)
  516. tag = xs_list_new();
  517. if (in_reply_to != NULL && *in_reply_to) {
  518. xs *p_msg = NULL;
  519. xs *wrk = NULL;
  520. /* demand this thing */
  521. timeline_request(snac, &in_reply_to, &wrk);
  522. if (valid_status(object_get(in_reply_to, &p_msg))) {
  523. /* add this author as recipient */
  524. char *a, *v;
  525. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  526. to = xs_list_append(to, a);
  527. /* add this author to the tag list as a mention */
  528. xs *t_href = NULL;
  529. xs *t_name = NULL;
  530. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  531. xs *t = xs_dict_new();
  532. t = xs_dict_append(t, "type", "Mention");
  533. t = xs_dict_append(t, "href", t_href);
  534. t = xs_dict_append(t, "name", t_name);
  535. tag = xs_list_append(tag, t);
  536. }
  537. /* get the context, if there is one */
  538. if ((v = xs_dict_get(p_msg, "context")))
  539. ctxt = xs_dup(v);
  540. /* if this message is public, ours will also be */
  541. if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1)
  542. to = xs_list_append(to, public_address);
  543. }
  544. irt = xs_dup(in_reply_to);
  545. }
  546. else
  547. irt = xs_val_new(XSTYPE_NULL);
  548. /* create the attachment list, if there are any */
  549. if (!xs_is_null(attach)) {
  550. atls = xs_list_new();
  551. while (xs_list_iter(&attach, &v)) {
  552. xs *d = xs_dict_new();
  553. char *url = xs_list_get(v, 0);
  554. char *alt = xs_list_get(v, 1);
  555. char *mime = xs_mime_by_ext(url);
  556. d = xs_dict_append(d, "mediaType", mime);
  557. d = xs_dict_append(d, "url", url);
  558. d = xs_dict_append(d, "name", alt);
  559. d = xs_dict_append(d, "type",
  560. xs_startswith(mime, "image/") ? "Image" : "Document");
  561. atls = xs_list_append(atls, d);
  562. }
  563. }
  564. if (ctxt == NULL)
  565. ctxt = xs_fmt("%s#ctxt", id);
  566. /* add all mentions to the cc */
  567. p = tag;
  568. while (xs_list_iter(&p, &v)) {
  569. if (xs_type(v) == XSTYPE_DICT) {
  570. char *t;
  571. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  572. if ((t = xs_dict_get(v, "href")) != NULL)
  573. cc = xs_list_append(cc, t);
  574. }
  575. }
  576. }
  577. /* no recipients? must be for everybody */
  578. if (!priv && xs_list_len(to) == 0)
  579. to = xs_list_append(to, public_address);
  580. /* delete all cc recipients that also are in the to */
  581. p = to;
  582. while (xs_list_iter(&p, &v)) {
  583. int i;
  584. if ((i = xs_list_in(cc, v)) != -1)
  585. cc = xs_list_del(cc, i);
  586. }
  587. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  588. msg = xs_dict_append(msg, "summary", "");
  589. msg = xs_dict_append(msg, "content", fc1);
  590. msg = xs_dict_append(msg, "context", ctxt);
  591. msg = xs_dict_append(msg, "url", id);
  592. msg = xs_dict_append(msg, "to", to);
  593. msg = xs_dict_append(msg, "cc", cc);
  594. msg = xs_dict_append(msg, "inReplyTo", irt);
  595. msg = xs_dict_append(msg, "tag", tag);
  596. msg = xs_dict_append(msg, "sourceContent", content);
  597. if (atls != NULL)
  598. msg = xs_dict_append(msg, "attachment", atls);
  599. return msg;
  600. }
  601. void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg)
  602. /* notifies the user of relevant events */
  603. {
  604. xs_val *object = NULL;
  605. if (strcmp(type, "Create") == 0) {
  606. /* only notify of notes specifically for us */
  607. xs *rcpts = recipient_list(snac, msg, 0);
  608. if (xs_list_in(rcpts, snac->actor) == -1)
  609. return;
  610. }
  611. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  612. return;
  613. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  614. object = xs_dict_get(msg, "object");
  615. if (xs_is_null(object))
  616. return;
  617. else {
  618. if (xs_type(object) == XSTYPE_DICT)
  619. object = xs_dict_get(object, "id");
  620. /* if it's not an admiration about something by us, done */
  621. if (xs_is_null(object) || !xs_startswith(object, snac->actor))
  622. return;
  623. }
  624. }
  625. /* prepare message body */
  626. xs *body = xs_fmt("User : @%s@%s\n",
  627. xs_dict_get(snac->config, "uid"),
  628. xs_dict_get(srv_config, "host")
  629. );
  630. if (strcmp(utype, "(null)") != 0) {
  631. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  632. body = xs_str_cat(body, s1);
  633. }
  634. else {
  635. xs *s1 = xs_fmt("Type : %s\n", type);
  636. body = xs_str_cat(body, s1);
  637. }
  638. {
  639. xs *s1 = xs_fmt("Actor : %s\n", actor);
  640. body = xs_str_cat(body, s1);
  641. }
  642. if (object != NULL) {
  643. xs *s1 = xs_fmt("Object: %s\n", object);
  644. body = xs_str_cat(body, s1);
  645. }
  646. /* email */
  647. const char *email = "[disabled by admin]";
  648. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  649. email = xs_dict_get(snac->config_o, "email");
  650. if (xs_is_null(email)) {
  651. email = xs_dict_get(snac->config, "email");
  652. if (xs_is_null(email))
  653. email = "[empty]";
  654. }
  655. }
  656. if (*email != '\0' && *email != '[') {
  657. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  658. xs *subject = xs_fmt("snac notify for @%s@%s",
  659. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  660. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  661. xs *header = xs_fmt(
  662. "From: %s\n"
  663. "To: %s\n"
  664. "Subject: %s\n"
  665. "\n",
  666. from, email, subject);
  667. xs *email_body = xs_fmt("%s%s", header, body);
  668. enqueue_email(email_body, 0);
  669. }
  670. /* telegram */
  671. char *bot = xs_dict_get(snac->config, "telegram_bot");
  672. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  673. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  674. enqueue_telegram(body, bot, chat_id);
  675. }
  676. /** queues **/
  677. int process_input_message(snac *snac, char *msg, char *req)
  678. /* processes an ActivityPub message from the input queue */
  679. {
  680. /* actor and type exist, were checked previously */
  681. char *actor = xs_dict_get(msg, "actor");
  682. char *type = xs_dict_get(msg, "type");
  683. xs *actor_o = NULL;
  684. int a_status;
  685. int do_notify = 0;
  686. char *object, *utype;
  687. object = xs_dict_get(msg, "object");
  688. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  689. utype = xs_dict_get(object, "type");
  690. else
  691. utype = "(null)";
  692. /* bring the actor */
  693. a_status = actor_request(snac, actor, &actor_o);
  694. /* if the actor does not explicitly exist, discard */
  695. if (a_status == 404 || a_status == 410) {
  696. snac_debug(snac, 1,
  697. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  698. return 1;
  699. }
  700. if (!valid_status(a_status)) {
  701. /* other actor download errors may need a retry */
  702. snac_debug(snac, 1,
  703. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  704. return 0;
  705. }
  706. /* check the signature */
  707. if (!check_signature(snac, req)) {
  708. snac_log(snac, xs_fmt("bad signature %s", actor));
  709. return 1;
  710. }
  711. if (strcmp(type, "Follow") == 0) {
  712. if (!follower_check(snac, actor)) {
  713. xs *f_msg = xs_dup(msg);
  714. xs *reply = msg_accept(snac, f_msg, actor);
  715. enqueue_message(snac, reply);
  716. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  717. /* add a date if it doesn't include one (Mastodon) */
  718. xs *date = xs_str_utctime(0, "%Y-%m-%dT%H:%M:%SZ");
  719. f_msg = xs_dict_set(f_msg, "published", date);
  720. }
  721. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  722. follower_add(snac, actor);
  723. snac_log(snac, xs_fmt("new follower %s", actor));
  724. do_notify = 1;
  725. }
  726. else
  727. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  728. }
  729. else
  730. if (strcmp(type, "Undo") == 0) {
  731. if (strcmp(utype, "Follow") == 0) {
  732. if (valid_status(follower_del(snac, actor))) {
  733. snac_log(snac, xs_fmt("no longer following us %s", actor));
  734. do_notify = 1;
  735. }
  736. else
  737. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  738. }
  739. else
  740. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  741. }
  742. else
  743. if (strcmp(type, "Create") == 0) {
  744. if (strcmp(utype, "Note") == 0) {
  745. if (is_muted(snac, actor))
  746. snac_log(snac, xs_fmt("ignored 'Note' from muted actor %s", actor));
  747. else {
  748. char *id = xs_dict_get(object, "id");
  749. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  750. xs *wrk = NULL;
  751. timeline_request(snac, &in_reply_to, &wrk);
  752. if (timeline_add(snac, id, object)) {
  753. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  754. do_notify = 1;
  755. }
  756. }
  757. }
  758. else
  759. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  760. }
  761. else
  762. if (strcmp(type, "Accept") == 0) {
  763. if (strcmp(utype, "Follow") == 0) {
  764. if (following_check(snac, actor)) {
  765. following_add(snac, actor, msg);
  766. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  767. }
  768. else
  769. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  770. }
  771. else
  772. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  773. }
  774. else
  775. if (strcmp(type, "Like") == 0) {
  776. if (xs_type(object) == XSTYPE_DICT)
  777. object = xs_dict_get(object, "id");
  778. timeline_admire(snac, object, actor, 1);
  779. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  780. do_notify = 1;
  781. }
  782. else
  783. if (strcmp(type, "Announce") == 0) {
  784. xs *a_msg = NULL;
  785. xs *wrk = NULL;
  786. if (xs_type(object) == XSTYPE_DICT)
  787. object = xs_dict_get(object, "id");
  788. timeline_request(snac, &object, &wrk);
  789. if (valid_status(object_get(object, &a_msg))) {
  790. char *who = xs_dict_get(a_msg, "attributedTo");
  791. if (who && !is_muted(snac, who)) {
  792. /* bring the actor */
  793. xs *who_o = NULL;
  794. if (valid_status(actor_request(snac, who, &who_o))) {
  795. timeline_admire(snac, object, actor, 0);
  796. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  797. do_notify = 1;
  798. }
  799. else
  800. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  801. }
  802. else
  803. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  804. }
  805. else
  806. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  807. }
  808. else
  809. if (strcmp(type, "Update") == 0) {
  810. if (strcmp(utype, "Person") == 0) {
  811. actor_add(snac, actor, xs_dict_get(msg, "object"));
  812. snac_log(snac, xs_fmt("updated actor %s", actor));
  813. }
  814. else
  815. if (strcmp(utype, "Note") == 0) {
  816. char *id = xs_dict_get(object, "id");
  817. object_add_ow(id, object);
  818. snac_log(snac, xs_fmt("updated post %s", id));
  819. }
  820. else
  821. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  822. }
  823. else
  824. if (strcmp(type, "Delete") == 0) {
  825. if (xs_type(object) == XSTYPE_DICT)
  826. object = xs_dict_get(object, "id");
  827. if (valid_status(timeline_del(snac, object)))
  828. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  829. else
  830. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  831. }
  832. else
  833. snac_debug(snac, 1, xs_fmt("process_message type '%s' ignored", type));
  834. if (do_notify)
  835. notify(snac, type, utype, actor, msg);
  836. return 1;
  837. }
  838. int send_email(char *msg)
  839. /* invoke sendmail with email headers and body in msg */
  840. {
  841. FILE *f;
  842. int status;
  843. int fds[2];
  844. pid_t pid;
  845. if (pipe(fds) == -1) return -1;
  846. pid = vfork();
  847. if (pid == -1) return -1;
  848. else if (pid == 0) {
  849. dup2(fds[0], 0);
  850. close(fds[0]);
  851. close(fds[1]);
  852. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  853. _exit(1);
  854. }
  855. close(fds[0]);
  856. if ((f = fdopen(fds[1], "w")) == NULL) {
  857. close(fds[1]);
  858. return -1;
  859. }
  860. fprintf(f, "%s\n", msg);
  861. fclose(f);
  862. if (waitpid(pid, &status, 0) == -1) return -1;
  863. return status;
  864. }
  865. void process_user_queue_item(snac *snac, xs_dict *q_item)
  866. /* processes an item from the user queue */
  867. {
  868. char *type;
  869. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  870. if ((type = xs_dict_get(q_item, "type")) == NULL)
  871. type = "output";
  872. if (strcmp(type, "message") == 0) {
  873. xs_dict *msg = xs_dict_get(q_item, "message");
  874. xs *rcpts = recipient_list(snac, msg, 1);
  875. xs_set inboxes;
  876. xs_list *p;
  877. xs_str *actor;
  878. xs_set_init(&inboxes);
  879. p = rcpts;
  880. while (xs_list_iter(&p, &actor)) {
  881. xs *inbox = get_actor_inbox(snac, actor);
  882. if (inbox != NULL) {
  883. /* add to the set and, if it's not there, send message */
  884. if (xs_set_add(&inboxes, inbox) == 1)
  885. enqueue_output(snac, msg, inbox, 0);
  886. }
  887. else
  888. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  889. }
  890. xs_set_free(&inboxes);
  891. }
  892. else
  893. if (strcmp(type, "input") == 0) {
  894. /* process the message */
  895. xs_dict *msg = xs_dict_get(q_item, "message");
  896. xs_dict *req = xs_dict_get(q_item, "req");
  897. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  898. if (xs_is_null(msg))
  899. return;
  900. if (!process_input_message(snac, msg, req)) {
  901. if (retries > queue_retry_max)
  902. snac_log(snac, xs_fmt("input giving up"));
  903. else {
  904. /* reenqueue */
  905. enqueue_input(snac, msg, req, retries + 1);
  906. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  907. }
  908. }
  909. }
  910. else
  911. snac_log(snac, xs_fmt("unexpected q_item type '%s'", type));
  912. }
  913. int process_user_queue(snac *snac)
  914. /* processes a user's queue */
  915. {
  916. int cnt = 0;
  917. xs *list = user_queue(snac);
  918. xs_list *p = list;
  919. xs_str *fn;
  920. while (xs_list_iter(&p, &fn)) {
  921. xs *q_item = dequeue(fn);
  922. if (q_item == NULL) {
  923. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  924. continue;
  925. }
  926. process_user_queue_item(snac, q_item);
  927. cnt++;
  928. }
  929. return cnt;
  930. }
  931. void process_queue_item(xs_dict *q_item)
  932. /* processes an item from the global queue */
  933. {
  934. char *type = xs_dict_get(q_item, "type");
  935. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  936. if (strcmp(type, "output") == 0) {
  937. int status;
  938. xs_str *inbox = xs_dict_get(q_item, "inbox");
  939. xs_str *keyid = xs_dict_get(q_item, "keyid");
  940. xs_str *seckey = xs_dict_get(q_item, "seckey");
  941. xs_dict *msg = xs_dict_get(q_item, "message");
  942. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  943. xs *payload = NULL;
  944. int p_size = 0;
  945. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  946. srv_log(xs_fmt("output message error: missing fields"));
  947. return;
  948. }
  949. /* deliver */
  950. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  951. if (payload) {
  952. if (p_size > 24) {
  953. /* trim the message */
  954. payload[24] = '\0';
  955. payload = xs_str_cat(payload, "...");
  956. }
  957. /* strip ugly control characters */
  958. payload = xs_replace_i(payload, "\n", "");
  959. payload = xs_replace_i(payload, "\r", "");
  960. if (*payload)
  961. payload = xs_str_wrap_i(" [", payload, "]");
  962. }
  963. else
  964. payload = xs_str_new(NULL);
  965. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  966. if (!valid_status(status)) {
  967. retries++;
  968. /* error sending; requeue? */
  969. if (status == 404 || status == 410)
  970. /* explicit error: discard */
  971. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  972. else
  973. if (retries > queue_retry_max)
  974. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  975. else {
  976. /* requeue */
  977. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  978. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  979. }
  980. }
  981. }
  982. else
  983. if (strcmp(type, "email") == 0) {
  984. /* send this email */
  985. xs_str *msg = xs_dict_get(q_item, "message");
  986. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  987. if (!send_email(msg))
  988. srv_debug(1, xs_fmt("email message sent"));
  989. else {
  990. retries++;
  991. if (retries > queue_retry_max)
  992. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  993. else {
  994. /* requeue */
  995. srv_log(xs_fmt(
  996. "email requeue #%d (errno: %d)", retries, errno));
  997. enqueue_email(msg, retries);
  998. }
  999. }
  1000. }
  1001. else
  1002. if (strcmp(type, "telegram") == 0) {
  1003. /* send this via telegram */
  1004. char *bot = xs_dict_get(q_item, "bot");
  1005. char *msg = xs_dict_get(q_item, "message");
  1006. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1007. int status = 0;
  1008. /* chat_id must start with a - */
  1009. if (!xs_startswith(chat_id, "-"))
  1010. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1011. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1012. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1013. xs *headers = xs_dict_new();
  1014. headers = xs_dict_append(headers, "content-type", "application/json");
  1015. xs *rsp = xs_http_request("POST", url, headers,
  1016. body, strlen(body), &status, NULL, NULL, 0);
  1017. srv_debug(0, xs_fmt("telegram post %d", status));
  1018. }
  1019. else
  1020. if (strcmp(type, "purge") == 0) {
  1021. srv_log(xs_dup("purge start"));
  1022. purge_all();
  1023. srv_log(xs_dup("purge end"));
  1024. }
  1025. else
  1026. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1027. }
  1028. int process_queue(void)
  1029. /* processes the global queue */
  1030. {
  1031. int cnt = 0;
  1032. xs *list = queue();
  1033. xs_list *p = list;
  1034. xs_str *fn;
  1035. while (xs_list_iter(&p, &fn)) {
  1036. xs *q_item = dequeue(fn);
  1037. if (q_item != NULL) {
  1038. job_post(q_item);
  1039. cnt++;
  1040. }
  1041. }
  1042. return cnt;
  1043. }
  1044. /** HTTP handlers */
  1045. int activitypub_get_handler(d_char *req, char *q_path,
  1046. char **body, int *b_size, char **ctype)
  1047. {
  1048. int status = 200;
  1049. char *accept = xs_dict_get(req, "accept");
  1050. snac snac;
  1051. xs *msg = NULL;
  1052. if (accept == NULL)
  1053. return 0;
  1054. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1055. xs_str_in(accept, "application/ld+json") == -1)
  1056. return 0;
  1057. xs *l = xs_split_n(q_path, "/", 2);
  1058. char *uid, *p_path;
  1059. uid = xs_list_get(l, 1);
  1060. if (!user_open(&snac, uid)) {
  1061. /* invalid user */
  1062. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1063. return 404;
  1064. }
  1065. p_path = xs_list_get(l, 2);
  1066. *ctype = "application/activity+json";
  1067. if (p_path == NULL) {
  1068. /* if there was no component after the user, it's an actor request */
  1069. msg = msg_actor(&snac);
  1070. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1071. snac_debug(&snac, 1, xs_fmt("serving actor"));
  1072. }
  1073. else
  1074. if (strcmp(p_path, "outbox") == 0) {
  1075. xs *id = xs_fmt("%s/outbox", snac.actor);
  1076. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1077. xs *list = xs_list_new();
  1078. msg = msg_collection(&snac, id);
  1079. char *p, *v;
  1080. p = elems;
  1081. while (xs_list_iter(&p, &v)) {
  1082. xs *i = NULL;
  1083. if (valid_status(object_get_by_md5(v, &i))) {
  1084. char *type = xs_dict_get(i, "type");
  1085. char *id = xs_dict_get(i, "id");
  1086. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1087. i = xs_dict_del(i, "_snac");
  1088. list = xs_list_append(list, i);
  1089. }
  1090. }
  1091. }
  1092. /* replace the 'orderedItems' with the latest posts */
  1093. xs *items = xs_number_new(xs_list_len(list));
  1094. msg = xs_dict_set(msg, "orderedItems", list);
  1095. msg = xs_dict_set(msg, "totalItems", items);
  1096. }
  1097. else
  1098. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1099. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1100. msg = msg_collection(&snac, id);
  1101. }
  1102. else
  1103. if (xs_startswith(p_path, "p/")) {
  1104. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1105. status = object_get(id, &msg);
  1106. }
  1107. else
  1108. status = 404;
  1109. if (status == 200 && msg != NULL) {
  1110. *body = xs_json_dumps_pp(msg, 4);
  1111. *b_size = strlen(*body);
  1112. }
  1113. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1114. user_free(&snac);
  1115. return status;
  1116. }
  1117. int activitypub_post_handler(d_char *req, char *q_path,
  1118. d_char *payload, int p_size,
  1119. char **body, int *b_size, char **ctype)
  1120. /* processes an input message */
  1121. {
  1122. int status = 202; /* accepted */
  1123. char *i_ctype = xs_dict_get(req, "content-type");
  1124. snac snac;
  1125. char *v;
  1126. if (i_ctype == NULL) {
  1127. *body = xs_str_new("no content-type");
  1128. *ctype = "text/plain";
  1129. return 400;
  1130. }
  1131. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1132. xs_str_in(i_ctype, "application/ld+json") == -1)
  1133. return 0;
  1134. /* decode the message */
  1135. xs *msg = xs_json_loads(payload);
  1136. if (msg == NULL) {
  1137. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1138. *body = xs_str_new("JSON error");
  1139. *ctype = "text/plain";
  1140. status = 400;
  1141. }
  1142. /* get the user and path */
  1143. xs *l = xs_split_n(q_path, "/", 2);
  1144. char *uid;
  1145. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1146. /* strange q_path */
  1147. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1148. return 404;
  1149. }
  1150. uid = xs_list_get(l, 1);
  1151. if (!user_open(&snac, uid)) {
  1152. /* invalid user */
  1153. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1154. return 404;
  1155. }
  1156. /* if it has a digest, check it now, because
  1157. later the payload won't be exactly the same */
  1158. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1159. xs *s1 = xs_sha256_base64(payload, p_size);
  1160. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1161. if (strcmp(s2, v) != 0) {
  1162. srv_log(xs_fmt("digest check FAILED"));
  1163. *body = xs_str_new("bad digest");
  1164. *ctype = "text/plain";
  1165. status = 400;
  1166. }
  1167. }
  1168. if (valid_status(status)) {
  1169. enqueue_input(&snac, msg, req, 0);
  1170. *ctype = "application/activity+json";
  1171. }
  1172. user_free(&snac);
  1173. return status;
  1174. }