activitypub.c 54 KB

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