activitypub.c 44 KB

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