1
0

activitypub.c 45 KB

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