activitypub.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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, xs_list *attach,
  707. const xs_list *opts, int multiple, int end_secs)
  708. /* creates a Question message */
  709. {
  710. xs *ntid = tid(0);
  711. xs *id = xs_fmt("%s/q/%s", user->actor, ntid);
  712. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  713. msg = xs_dict_set(msg, "type", "Question");
  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 *rcpts = xs_list_new();
  738. xs *lopts = xs_list_new();
  739. xs_list *opts;
  740. xs_list *p;
  741. xs_val *v;
  742. /* get the object */
  743. if (!valid_status(object_get(id, &msg)))
  744. return -1;
  745. /* closed? do nothing more */
  746. if (xs_dict_get(msg, "closed"))
  747. return -2;
  748. /* get the options */
  749. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  750. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  751. return -3;
  752. /* fill the initial count */
  753. p = opts;
  754. while (xs_list_iter(&p, &v)) {
  755. const char *name = xs_dict_get(v, "name");
  756. if (name) {
  757. lopts = xs_list_append(lopts, name);
  758. rcnt = xs_dict_set(rcnt, name, z);
  759. }
  760. }
  761. /* iterate now the children (the votes) */
  762. xs *chld = object_children(id);
  763. p = chld;
  764. while (xs_list_iter(&p, &v)) {
  765. xs *obj = NULL;
  766. if (!valid_status(object_get_by_md5(v, &obj)))
  767. continue;
  768. const char *name = xs_dict_get(obj, "name");
  769. const char *atto = xs_dict_get(obj, "attributedTo");
  770. if (name && atto) {
  771. /* get the current count */
  772. const xs_number *cnt = xs_dict_get(rcnt, name);
  773. if (xs_type(cnt) == XSTYPE_NUMBER) {
  774. /* if it exists, increment */
  775. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  776. rcnt = xs_dict_set(rcnt, name, ucnt);
  777. rcpts = xs_list_append(rcpts, atto);
  778. }
  779. }
  780. }
  781. /* create a new list of options with their new counts */
  782. xs *nopts = xs_list_new();
  783. p = lopts;
  784. while (xs_list_iter(&p, &v)) {
  785. const xs_number *cnt = xs_dict_get(rcnt, v);
  786. if (xs_type(cnt) == XSTYPE_NUMBER) {
  787. xs *d1 = xs_dict_new();
  788. xs *d2 = xs_dict_new();
  789. d2 = xs_dict_append(d2, "type", "Collection");
  790. d2 = xs_dict_append(d2, "totalItems", cnt);
  791. d1 = xs_dict_append(d1, "type", "Note");
  792. d1 = xs_dict_append(d1, "name", v);
  793. d1 = xs_dict_append(d1, "replies", d2);
  794. nopts = xs_list_append(nopts, d1);
  795. }
  796. }
  797. /* update the list */
  798. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  799. /* due date? */
  800. const char *end_time = xs_dict_get(msg, "endTime");
  801. if (!xs_is_null(end_time)) {
  802. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  803. /* it's now greater than the endTime? */
  804. if (strcmp(now, end_time) > 0) {
  805. xs *et = xs_dup(end_time);
  806. msg = xs_dict_set(msg, "closed", et);
  807. }
  808. }
  809. /* update the count of voters */
  810. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  811. msg = xs_dict_set(msg, "votersCount", vcnt);
  812. /* store */
  813. object_add_ow(id, msg);
  814. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  815. timeline_touch(user);
  816. /* send an update message to all voters */
  817. xs *u_msg = msg_update(user, msg);
  818. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  819. enqueue_message(user, u_msg);
  820. return 0;
  821. }
  822. void notify(snac *snac, xs_str *type, xs_str *utype, xs_str *actor, xs_dict *msg)
  823. /* notifies the user of relevant events */
  824. {
  825. if (strcmp(type, "Create") == 0) {
  826. /* only notify of notes specifically for us */
  827. xs *rcpts = recipient_list(snac, msg, 0);
  828. if (xs_list_in(rcpts, snac->actor) == -1)
  829. return;
  830. }
  831. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  832. return;
  833. /* get the object id */
  834. const char *objid = xs_dict_get(msg, "object");
  835. if (xs_type(objid) == XSTYPE_DICT)
  836. objid = xs_dict_get(objid, "id");
  837. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  838. /* if it's not an admiration about something by us, done */
  839. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  840. return;
  841. }
  842. /* user will love to know about this! */
  843. /* prepare message body */
  844. xs *body = xs_fmt("User : @%s@%s\n",
  845. xs_dict_get(snac->config, "uid"),
  846. xs_dict_get(srv_config, "host")
  847. );
  848. if (strcmp(utype, "(null)") != 0) {
  849. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  850. body = xs_str_cat(body, s1);
  851. }
  852. else {
  853. xs *s1 = xs_fmt("Type : %s\n", type);
  854. body = xs_str_cat(body, s1);
  855. }
  856. {
  857. xs *s1 = xs_fmt("Actor : %s\n", actor);
  858. body = xs_str_cat(body, s1);
  859. }
  860. if (objid != NULL) {
  861. xs *s1 = xs_fmt("Object: %s\n", objid);
  862. body = xs_str_cat(body, s1);
  863. }
  864. /* email */
  865. const char *email = "[disabled by admin]";
  866. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  867. email = xs_dict_get(snac->config_o, "email");
  868. if (xs_is_null(email)) {
  869. email = xs_dict_get(snac->config, "email");
  870. if (xs_is_null(email))
  871. email = "[empty]";
  872. }
  873. }
  874. if (*email != '\0' && *email != '[') {
  875. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  876. xs *subject = xs_fmt("snac notify for @%s@%s",
  877. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  878. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  879. xs *header = xs_fmt(
  880. "From: %s\n"
  881. "To: %s\n"
  882. "Subject: %s\n"
  883. "\n",
  884. from, email, subject);
  885. xs *email_body = xs_fmt("%s%s", header, body);
  886. enqueue_email(email_body, 0);
  887. }
  888. /* telegram */
  889. char *bot = xs_dict_get(snac->config, "telegram_bot");
  890. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  891. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  892. enqueue_telegram(body, bot, chat_id);
  893. /* finally, store it in the notification folder */
  894. if (strcmp(type, "Follow") == 0)
  895. objid = xs_dict_get(msg, "id");
  896. notify_add(snac, type, utype, actor, objid);
  897. }
  898. /** queues **/
  899. int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
  900. /* processes an ActivityPub message from the input queue */
  901. {
  902. /* actor and type exist, were checked previously */
  903. char *actor = xs_dict_get(msg, "actor");
  904. char *type = xs_dict_get(msg, "type");
  905. xs *actor_o = NULL;
  906. int a_status;
  907. int do_notify = 0;
  908. /* question votes may not have a type */
  909. if (xs_is_null(type))
  910. type = "Note";
  911. if (xs_is_null(actor)) {
  912. snac_debug(snac, 0, xs_fmt("malformed message"));
  913. return 1;
  914. }
  915. char *object, *utype;
  916. object = xs_dict_get(msg, "object");
  917. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  918. utype = xs_dict_get(object, "type");
  919. else
  920. utype = "(null)";
  921. /* reject messages that are not for this user */
  922. if (!is_msg_for_me(snac, msg)) {
  923. snac_debug(snac, 0, xs_fmt("message from %s of type '%s' not for us", actor, type));
  924. return 1;
  925. }
  926. /* if it's a DM from someone we don't follow, reject the message */
  927. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  928. if (strcmp(utype, "Note") == 0 && !is_msg_public(snac, msg) &&
  929. !following_check(snac, actor)) {
  930. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  931. return 1;
  932. }
  933. }
  934. /* bring the actor */
  935. a_status = actor_request(snac, actor, &actor_o);
  936. /* if the actor does not explicitly exist, discard */
  937. if (a_status == 404 || a_status == 410) {
  938. snac_debug(snac, 1,
  939. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  940. return 1;
  941. }
  942. if (!valid_status(a_status)) {
  943. /* other actor download errors may need a retry */
  944. snac_debug(snac, 1,
  945. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  946. return 0;
  947. }
  948. /* check the signature */
  949. xs *sig_err = NULL;
  950. if (!check_signature(snac, req, &sig_err)) {
  951. snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
  952. srv_archive_error("check_signature", sig_err, req, msg);
  953. return 1;
  954. }
  955. if (strcmp(type, "Follow") == 0) {
  956. if (!follower_check(snac, actor)) {
  957. xs *f_msg = xs_dup(msg);
  958. xs *reply = msg_accept(snac, f_msg, actor);
  959. enqueue_message(snac, reply);
  960. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  961. /* add a date if it doesn't include one (Mastodon) */
  962. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  963. f_msg = xs_dict_set(f_msg, "published", date);
  964. }
  965. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  966. follower_add(snac, actor);
  967. snac_log(snac, xs_fmt("new follower %s", actor));
  968. do_notify = 1;
  969. }
  970. else
  971. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  972. }
  973. else
  974. if (strcmp(type, "Undo") == 0) {
  975. if (strcmp(utype, "Follow") == 0) {
  976. if (valid_status(follower_del(snac, actor))) {
  977. snac_log(snac, xs_fmt("no longer following us %s", actor));
  978. do_notify = 1;
  979. }
  980. else
  981. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  982. }
  983. else
  984. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  985. }
  986. else
  987. if (strcmp(type, "Create") == 0) {
  988. if (is_muted(snac, actor))
  989. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  990. if (strcmp(utype, "Note") == 0) {
  991. char *id = xs_dict_get(object, "id");
  992. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  993. xs *wrk = NULL;
  994. timeline_request(snac, &in_reply_to, &wrk);
  995. if (timeline_add(snac, id, object)) {
  996. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  997. do_notify = 1;
  998. }
  999. /* if it has a "name" field, it may be a vote for a question */
  1000. const char *name = xs_dict_get(object, "name");
  1001. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1002. update_question(snac, in_reply_to);
  1003. }
  1004. else
  1005. if (strcmp(utype, "Question") == 0) {
  1006. char *id = xs_dict_get(object, "id");
  1007. if (timeline_add(snac, id, object))
  1008. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1009. }
  1010. else
  1011. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1012. }
  1013. else
  1014. if (strcmp(type, "Accept") == 0) {
  1015. if (strcmp(utype, "Follow") == 0) {
  1016. if (following_check(snac, actor)) {
  1017. following_add(snac, actor, msg);
  1018. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1019. }
  1020. else
  1021. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1022. }
  1023. else
  1024. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1025. }
  1026. else
  1027. if (strcmp(type, "Like") == 0) {
  1028. if (xs_type(object) == XSTYPE_DICT)
  1029. object = xs_dict_get(object, "id");
  1030. timeline_admire(snac, object, actor, 1);
  1031. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1032. do_notify = 1;
  1033. }
  1034. else
  1035. if (strcmp(type, "Announce") == 0) {
  1036. xs *a_msg = NULL;
  1037. xs *wrk = NULL;
  1038. if (xs_type(object) == XSTYPE_DICT)
  1039. object = xs_dict_get(object, "id");
  1040. timeline_request(snac, &object, &wrk);
  1041. if (valid_status(object_get(object, &a_msg))) {
  1042. char *who = xs_dict_get(a_msg, "attributedTo");
  1043. if (who && !is_muted(snac, who)) {
  1044. /* bring the actor */
  1045. xs *who_o = NULL;
  1046. if (valid_status(actor_request(snac, who, &who_o))) {
  1047. timeline_admire(snac, object, actor, 0);
  1048. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1049. do_notify = 1;
  1050. }
  1051. else
  1052. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1053. }
  1054. else
  1055. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1056. }
  1057. else
  1058. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  1059. }
  1060. else
  1061. if (strcmp(type, "Update") == 0) {
  1062. if (strcmp(utype, "Person") == 0) {
  1063. actor_add(actor, xs_dict_get(msg, "object"));
  1064. timeline_touch(snac);
  1065. snac_log(snac, xs_fmt("updated actor %s", actor));
  1066. }
  1067. else
  1068. if (strcmp(utype, "Note") == 0) {
  1069. char *id = xs_dict_get(object, "id");
  1070. object_add_ow(id, object);
  1071. timeline_touch(snac);
  1072. snac_log(snac, xs_fmt("updated post %s", id));
  1073. }
  1074. else
  1075. if (strcmp(utype, "Question") == 0) {
  1076. char *id = xs_dict_get(object, "id");
  1077. object_add_ow(id, object);
  1078. timeline_touch(snac);
  1079. snac_log(snac, xs_fmt("updated poll %s", id));
  1080. }
  1081. else
  1082. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1083. }
  1084. else
  1085. if (strcmp(type, "Delete") == 0) {
  1086. if (xs_type(object) == XSTYPE_DICT)
  1087. object = xs_dict_get(object, "id");
  1088. if (valid_status(timeline_del(snac, object)))
  1089. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1090. else
  1091. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1092. }
  1093. else
  1094. if (strcmp(type, "Pong") == 0) {
  1095. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1096. }
  1097. else
  1098. if (strcmp(type, "Ping") == 0) {
  1099. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1100. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1101. enqueue_output_by_actor(snac, rsp, actor, 0);
  1102. }
  1103. else
  1104. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1105. if (do_notify) {
  1106. notify(snac, type, utype, actor, msg);
  1107. timeline_touch(snac);
  1108. }
  1109. return 1;
  1110. }
  1111. int send_email(char *msg)
  1112. /* invoke sendmail with email headers and body in msg */
  1113. {
  1114. FILE *f;
  1115. int status;
  1116. int fds[2];
  1117. pid_t pid;
  1118. if (pipe(fds) == -1) return -1;
  1119. pid = vfork();
  1120. if (pid == -1) return -1;
  1121. else if (pid == 0) {
  1122. dup2(fds[0], 0);
  1123. close(fds[0]);
  1124. close(fds[1]);
  1125. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1126. _exit(1);
  1127. }
  1128. close(fds[0]);
  1129. if ((f = fdopen(fds[1], "w")) == NULL) {
  1130. close(fds[1]);
  1131. return -1;
  1132. }
  1133. fprintf(f, "%s\n", msg);
  1134. fclose(f);
  1135. if (waitpid(pid, &status, 0) == -1) return -1;
  1136. return status;
  1137. }
  1138. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1139. /* processes an item from the user queue */
  1140. {
  1141. char *type;
  1142. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1143. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1144. type = "output";
  1145. if (strcmp(type, "message") == 0) {
  1146. xs_dict *msg = xs_dict_get(q_item, "message");
  1147. xs *rcpts = recipient_list(snac, msg, 1);
  1148. xs_set inboxes;
  1149. xs_list *p;
  1150. xs_str *actor;
  1151. xs_set_init(&inboxes);
  1152. /* iterate the recipients */
  1153. p = rcpts;
  1154. while (xs_list_iter(&p, &actor)) {
  1155. xs *inbox = get_actor_inbox(snac, actor);
  1156. if (inbox != NULL) {
  1157. /* add to the set and, if it's not there, send message */
  1158. if (xs_set_add(&inboxes, inbox) == 1)
  1159. enqueue_output(snac, msg, inbox, 0);
  1160. }
  1161. else
  1162. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1163. }
  1164. /* if it's public, send to the collected inboxes */
  1165. if (is_msg_public(snac, msg)) {
  1166. xs *shibx = inbox_list();
  1167. xs_str *inbox;
  1168. p = shibx;
  1169. while (xs_list_iter(&p, &inbox)) {
  1170. if (xs_set_add(&inboxes, inbox) == 1)
  1171. enqueue_output(snac, msg, inbox, 0);
  1172. }
  1173. }
  1174. xs_set_free(&inboxes);
  1175. }
  1176. else
  1177. if (strcmp(type, "input") == 0) {
  1178. /* process the message */
  1179. xs_dict *msg = xs_dict_get(q_item, "message");
  1180. xs_dict *req = xs_dict_get(q_item, "req");
  1181. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1182. if (xs_is_null(msg))
  1183. return;
  1184. if (!process_input_message(snac, msg, req)) {
  1185. if (retries > queue_retry_max)
  1186. snac_log(snac, xs_fmt("input giving up"));
  1187. else {
  1188. /* reenqueue */
  1189. enqueue_input(snac, msg, req, retries + 1);
  1190. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1191. }
  1192. }
  1193. }
  1194. else
  1195. if (strcmp(type, "close_question") == 0) {
  1196. /* the time for this question has ended */
  1197. const char *id = xs_dict_get(q_item, "message");
  1198. if (!xs_is_null(id))
  1199. update_question(snac, id);
  1200. }
  1201. else
  1202. snac_log(snac, xs_fmt("unexpected q_item type '%s'", type));
  1203. }
  1204. int process_user_queue(snac *snac)
  1205. /* processes a user's queue */
  1206. {
  1207. int cnt = 0;
  1208. xs *list = user_queue(snac);
  1209. xs_list *p = list;
  1210. xs_str *fn;
  1211. while (xs_list_iter(&p, &fn)) {
  1212. xs *q_item = dequeue(fn);
  1213. if (q_item == NULL) {
  1214. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  1215. continue;
  1216. }
  1217. process_user_queue_item(snac, q_item);
  1218. cnt++;
  1219. }
  1220. return cnt;
  1221. }
  1222. void process_queue_item(xs_dict *q_item)
  1223. /* processes an item from the global queue */
  1224. {
  1225. char *type = xs_dict_get(q_item, "type");
  1226. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1227. if (strcmp(type, "output") == 0) {
  1228. int status;
  1229. xs_str *inbox = xs_dict_get(q_item, "inbox");
  1230. xs_str *keyid = xs_dict_get(q_item, "keyid");
  1231. xs_str *seckey = xs_dict_get(q_item, "seckey");
  1232. xs_dict *msg = xs_dict_get(q_item, "message");
  1233. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1234. xs *payload = NULL;
  1235. int p_size = 0;
  1236. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1237. srv_log(xs_fmt("output message error: missing fields"));
  1238. return;
  1239. }
  1240. /* deliver */
  1241. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  1242. if (payload) {
  1243. if (p_size > 64) {
  1244. /* trim the message */
  1245. payload[64] = '\0';
  1246. payload = xs_str_cat(payload, "...");
  1247. }
  1248. /* strip ugly control characters */
  1249. payload = xs_replace_i(payload, "\n", "");
  1250. payload = xs_replace_i(payload, "\r", "");
  1251. if (*payload)
  1252. payload = xs_str_wrap_i(" [", payload, "]");
  1253. }
  1254. else
  1255. payload = xs_str_new(NULL);
  1256. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1257. if (!valid_status(status)) {
  1258. retries++;
  1259. /* error sending; requeue? */
  1260. if (status == 404 || status == 410)
  1261. /* explicit error: discard */
  1262. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1263. else
  1264. if (retries > queue_retry_max)
  1265. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1266. else {
  1267. /* requeue */
  1268. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  1269. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1270. }
  1271. }
  1272. }
  1273. else
  1274. if (strcmp(type, "email") == 0) {
  1275. /* send this email */
  1276. xs_str *msg = xs_dict_get(q_item, "message");
  1277. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1278. if (!send_email(msg))
  1279. srv_debug(1, xs_fmt("email message sent"));
  1280. else {
  1281. retries++;
  1282. if (retries > queue_retry_max)
  1283. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1284. else {
  1285. /* requeue */
  1286. srv_log(xs_fmt(
  1287. "email requeue #%d (errno: %d)", retries, errno));
  1288. enqueue_email(msg, retries);
  1289. }
  1290. }
  1291. }
  1292. else
  1293. if (strcmp(type, "telegram") == 0) {
  1294. /* send this via telegram */
  1295. char *bot = xs_dict_get(q_item, "bot");
  1296. char *msg = xs_dict_get(q_item, "message");
  1297. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1298. int status = 0;
  1299. /* chat_id must start with a - */
  1300. if (!xs_startswith(chat_id, "-"))
  1301. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1302. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1303. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1304. xs *headers = xs_dict_new();
  1305. headers = xs_dict_append(headers, "content-type", "application/json");
  1306. xs *rsp = xs_http_request("POST", url, headers,
  1307. body, strlen(body), &status, NULL, NULL, 0);
  1308. rsp = xs_free(rsp);
  1309. srv_debug(0, xs_fmt("telegram post %d", status));
  1310. }
  1311. else
  1312. if (strcmp(type, "purge") == 0) {
  1313. srv_log(xs_dup("purge start"));
  1314. purge_all();
  1315. srv_log(xs_dup("purge end"));
  1316. }
  1317. else
  1318. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1319. }
  1320. int process_queue(void)
  1321. /* processes the global queue */
  1322. {
  1323. int cnt = 0;
  1324. xs *list = queue();
  1325. xs_list *p = list;
  1326. xs_str *fn;
  1327. while (xs_list_iter(&p, &fn)) {
  1328. xs *q_item = dequeue(fn);
  1329. if (q_item != NULL) {
  1330. job_post(q_item, 0);
  1331. cnt++;
  1332. }
  1333. }
  1334. return cnt;
  1335. }
  1336. /** HTTP handlers */
  1337. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  1338. char **body, int *b_size, char **ctype)
  1339. {
  1340. int status = 200;
  1341. char *accept = xs_dict_get(req, "accept");
  1342. snac snac;
  1343. xs *msg = NULL;
  1344. if (accept == NULL)
  1345. return 0;
  1346. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1347. xs_str_in(accept, "application/ld+json") == -1)
  1348. return 0;
  1349. xs *l = xs_split_n(q_path, "/", 2);
  1350. char *uid, *p_path;
  1351. uid = xs_list_get(l, 1);
  1352. if (!user_open(&snac, uid)) {
  1353. /* invalid user */
  1354. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1355. return 404;
  1356. }
  1357. p_path = xs_list_get(l, 2);
  1358. *ctype = "application/activity+json";
  1359. if (p_path == NULL) {
  1360. /* if there was no component after the user, it's an actor request */
  1361. msg = msg_actor(&snac);
  1362. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1363. char *ua = xs_dict_get(req, "user-agent");
  1364. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  1365. }
  1366. else
  1367. if (strcmp(p_path, "outbox") == 0) {
  1368. xs *id = xs_fmt("%s/outbox", snac.actor);
  1369. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1370. xs *list = xs_list_new();
  1371. msg = msg_collection(&snac, id);
  1372. char *p, *v;
  1373. p = elems;
  1374. while (xs_list_iter(&p, &v)) {
  1375. xs *i = NULL;
  1376. if (valid_status(object_get_by_md5(v, &i))) {
  1377. char *type = xs_dict_get(i, "type");
  1378. char *id = xs_dict_get(i, "id");
  1379. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1380. i = xs_dict_del(i, "_snac");
  1381. list = xs_list_append(list, i);
  1382. }
  1383. }
  1384. }
  1385. /* replace the 'orderedItems' with the latest posts */
  1386. xs *items = xs_number_new(xs_list_len(list));
  1387. msg = xs_dict_set(msg, "orderedItems", list);
  1388. msg = xs_dict_set(msg, "totalItems", items);
  1389. }
  1390. else
  1391. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1392. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1393. msg = msg_collection(&snac, id);
  1394. }
  1395. else
  1396. if (xs_startswith(p_path, "p/")) {
  1397. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1398. status = object_get(id, &msg);
  1399. }
  1400. else
  1401. status = 404;
  1402. if (status == 200 && msg != NULL) {
  1403. *body = xs_json_dumps_pp(msg, 4);
  1404. *b_size = strlen(*body);
  1405. }
  1406. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1407. user_free(&snac);
  1408. return status;
  1409. }
  1410. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  1411. char *payload, int p_size,
  1412. char **body, int *b_size, char **ctype)
  1413. /* processes an input message */
  1414. {
  1415. (void)b_size;
  1416. int status = 202; /* accepted */
  1417. char *i_ctype = xs_dict_get(req, "content-type");
  1418. snac snac;
  1419. char *v;
  1420. if (i_ctype == NULL) {
  1421. *body = xs_str_new("no content-type");
  1422. *ctype = "text/plain";
  1423. return 400;
  1424. }
  1425. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1426. xs_str_in(i_ctype, "application/ld+json") == -1)
  1427. return 0;
  1428. /* decode the message */
  1429. xs *msg = xs_json_loads(payload);
  1430. if (msg == NULL) {
  1431. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1432. *body = xs_str_new("JSON error");
  1433. *ctype = "text/plain";
  1434. status = 400;
  1435. }
  1436. /* get the user and path */
  1437. xs *l = xs_split_n(q_path, "/", 2);
  1438. char *uid;
  1439. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1440. /* strange q_path */
  1441. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1442. return 404;
  1443. }
  1444. uid = xs_list_get(l, 1);
  1445. if (!user_open(&snac, uid)) {
  1446. /* invalid user */
  1447. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1448. return 404;
  1449. }
  1450. /* if it has a digest, check it now, because
  1451. later the payload won't be exactly the same */
  1452. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1453. xs *s1 = xs_sha256_base64(payload, p_size);
  1454. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1455. if (strcmp(s2, v) != 0) {
  1456. srv_log(xs_fmt("digest check FAILED"));
  1457. *body = xs_str_new("bad digest");
  1458. *ctype = "text/plain";
  1459. status = 400;
  1460. }
  1461. }
  1462. /* if the message is from a muted actor, reject it right now */
  1463. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  1464. if (is_muted(&snac, v)) {
  1465. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  1466. *body = xs_str_new("rejected");
  1467. *ctype = "text/plain";
  1468. status = 403;
  1469. }
  1470. }
  1471. if (valid_status(status)) {
  1472. enqueue_input(&snac, msg, req, 0);
  1473. *ctype = "application/activity+json";
  1474. }
  1475. user_free(&snac);
  1476. return status;
  1477. }