activitypub.c 48 KB

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