activitypub.c 43 KB

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