activitypub.c 43 KB

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