activitypub.c 46 KB

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