activitypub.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  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. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  372. /* notifies the user of relevant events */
  373. {
  374. const char *id = xs_dict_get(msg, "id");
  375. if (strcmp(type, "Create") == 0) {
  376. /* only notify of notes specifically for us */
  377. xs *rcpts = recipient_list(snac, msg, 0);
  378. if (xs_list_in(rcpts, snac->actor) == -1)
  379. return;
  380. /* discard votes */
  381. const xs_dict *note = xs_dict_get(msg, "object");
  382. if (note && !xs_is_null(xs_dict_get(note, "name")))
  383. return;
  384. }
  385. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  386. return;
  387. /* get the object id */
  388. const char *objid = xs_dict_get(msg, "object");
  389. if (xs_type(objid) == XSTYPE_DICT)
  390. objid = xs_dict_get(objid, "id");
  391. if (strcmp(type, "Like") == 0 || strcmp(type, "Announce") == 0) {
  392. /* if it's not an admiration about something by us, done */
  393. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  394. return;
  395. }
  396. /* updated poll? */
  397. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  398. /* if it's not closed, discard */
  399. if (xs_is_null(xs_dict_get(msg, "closed")))
  400. return;
  401. /* if it's not ours and we didn't vote, discard */
  402. if (!xs_startswith(id, snac->actor) && !was_question_voted(snac, id))
  403. return;
  404. }
  405. /* user will love to know about this! */
  406. /* prepare message body */
  407. xs *body = xs_fmt("User : @%s@%s\n",
  408. xs_dict_get(snac->config, "uid"),
  409. xs_dict_get(srv_config, "host")
  410. );
  411. if (strcmp(utype, "(null)") != 0) {
  412. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  413. body = xs_str_cat(body, s1);
  414. }
  415. else {
  416. xs *s1 = xs_fmt("Type : %s\n", type);
  417. body = xs_str_cat(body, s1);
  418. }
  419. {
  420. xs *s1 = xs_fmt("Actor : %s\n", actor);
  421. body = xs_str_cat(body, s1);
  422. }
  423. if (objid != NULL) {
  424. xs *s1 = xs_fmt("Object: %s\n", objid);
  425. body = xs_str_cat(body, s1);
  426. }
  427. /* email */
  428. const char *email = "[disabled by admin]";
  429. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  430. email = xs_dict_get(snac->config_o, "email");
  431. if (xs_is_null(email)) {
  432. email = xs_dict_get(snac->config, "email");
  433. if (xs_is_null(email))
  434. email = "[empty]";
  435. }
  436. }
  437. if (*email != '\0' && *email != '[') {
  438. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  439. xs *subject = xs_fmt("snac notify for @%s@%s",
  440. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  441. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  442. xs *header = xs_fmt(
  443. "From: %s\n"
  444. "To: %s\n"
  445. "Subject: %s\n"
  446. "\n",
  447. from, email, subject);
  448. xs *email_body = xs_fmt("%s%s", header, body);
  449. enqueue_email(email_body, 0);
  450. }
  451. /* telegram */
  452. char *bot = xs_dict_get(snac->config, "telegram_bot");
  453. char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  454. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  455. enqueue_telegram(body, bot, chat_id);
  456. /* finally, store it in the notification folder */
  457. if (strcmp(type, "Follow") == 0)
  458. objid = id;
  459. notify_add(snac, type, utype, actor, objid);
  460. }
  461. /** messages **/
  462. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  463. const char *actor, const char *date, const char *object)
  464. /* creates a base ActivityPub message */
  465. {
  466. xs *did = NULL;
  467. xs *published = NULL;
  468. xs *ntid = tid(0);
  469. /* generated values */
  470. if (date && strcmp(date, "@now") == 0) {
  471. published = xs_str_utctime(0, ISO_DATE_SPEC);
  472. date = published;
  473. }
  474. if (id != NULL) {
  475. if (strcmp(id, "@dummy") == 0) {
  476. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  477. id = did;
  478. }
  479. else
  480. if (strcmp(id, "@object") == 0) {
  481. if (object != NULL) {
  482. did = xs_fmt("%s/%s_%s", xs_dict_get(object, "id"), type, ntid);
  483. id = did;
  484. }
  485. else
  486. id = NULL;
  487. }
  488. }
  489. xs_dict *msg = xs_dict_new();
  490. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  491. msg = xs_dict_append(msg, "type", type);
  492. if (id != NULL)
  493. msg = xs_dict_append(msg, "id", id);
  494. if (actor != NULL)
  495. msg = xs_dict_append(msg, "actor", actor);
  496. if (date != NULL)
  497. msg = xs_dict_append(msg, "published", date);
  498. if (object != NULL)
  499. msg = xs_dict_append(msg, "object", object);
  500. return msg;
  501. }
  502. d_char *msg_collection(snac *snac, char *id)
  503. /* creates an empty OrderedCollection message */
  504. {
  505. d_char *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  506. xs *ol = xs_list_new();
  507. xs *nz = xs_number_new(0);
  508. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  509. msg = xs_dict_append(msg, "orderedItems", ol);
  510. msg = xs_dict_append(msg, "totalItems", nz);
  511. return msg;
  512. }
  513. d_char *msg_accept(snac *snac, char *object, char *to)
  514. /* creates an Accept message (as a response to a Follow) */
  515. {
  516. d_char *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  517. msg = xs_dict_append(msg, "to", to);
  518. return msg;
  519. }
  520. xs_dict *msg_update(snac *snac, xs_dict *object)
  521. /* creates an Update message */
  522. {
  523. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  524. char *type = xs_dict_get(object, "type");
  525. if (strcmp(type, "Note") == 0) {
  526. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  527. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  528. }
  529. else
  530. if (strcmp(type, "Person") == 0) {
  531. msg = xs_dict_append(msg, "to", public_address);
  532. /* also spam the people being followed, so that
  533. they have the newest information about who we are */
  534. xs *cc = following_list(snac);
  535. msg = xs_dict_append(msg, "cc", cc);
  536. }
  537. else
  538. msg = xs_dict_append(msg, "to", public_address);
  539. return msg;
  540. }
  541. d_char *msg_admiration(snac *snac, char *object, char *type)
  542. /* creates a Like or Announce message */
  543. {
  544. xs *a_msg = NULL;
  545. d_char *msg = NULL;
  546. xs *wrk = NULL;
  547. /* call the object */
  548. timeline_request(snac, &object, &wrk);
  549. if (valid_status(object_get(object, &a_msg))) {
  550. xs *rcpts = xs_list_new();
  551. msg = msg_base(snac, type, "@dummy", snac->actor, "@now", object);
  552. if (is_msg_public(snac, a_msg))
  553. rcpts = xs_list_append(rcpts, public_address);
  554. rcpts = xs_list_append(rcpts, xs_dict_get(a_msg, "attributedTo"));
  555. msg = xs_dict_append(msg, "to", rcpts);
  556. }
  557. else
  558. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  559. return msg;
  560. }
  561. d_char *msg_actor(snac *snac)
  562. /* create a Person message for this actor */
  563. {
  564. xs *ctxt = xs_list_new();
  565. xs *icon = xs_dict_new();
  566. xs *keys = xs_dict_new();
  567. xs *avtr = NULL;
  568. xs *kid = NULL;
  569. xs *f_bio = NULL;
  570. d_char *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  571. char *p;
  572. int n;
  573. /* change the @context (is this really necessary?) */
  574. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  575. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  576. msg = xs_dict_set(msg, "@context", ctxt);
  577. msg = xs_dict_set(msg, "url", snac->actor);
  578. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  579. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  580. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  581. f_bio = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL);
  582. msg = xs_dict_set(msg, "summary", f_bio);
  583. char *folders[] = { "inbox", "outbox", "followers", "following", NULL };
  584. for (n = 0; folders[n]; n++) {
  585. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  586. msg = xs_dict_set(msg, folders[n], f);
  587. }
  588. p = xs_dict_get(snac->config, "avatar");
  589. if (*p == '\0')
  590. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  591. else
  592. avtr = xs_dup(p);
  593. icon = xs_dict_append(icon, "type", "Image");
  594. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  595. icon = xs_dict_append(icon, "url", avtr);
  596. msg = xs_dict_set(msg, "icon", icon);
  597. kid = xs_fmt("%s#main-key", snac->actor);
  598. keys = xs_dict_append(keys, "id", kid);
  599. keys = xs_dict_append(keys, "owner", snac->actor);
  600. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  601. msg = xs_dict_set(msg, "publicKey", keys);
  602. return msg;
  603. }
  604. xs_dict *msg_create(snac *snac, const xs_dict *object)
  605. /* creates a 'Create' message */
  606. {
  607. xs_dict *msg = msg_base(snac, "Create", "@object", snac->actor, "@now", object);
  608. xs_val *v;
  609. if ((v = xs_dict_get(object, "attributedTo")))
  610. msg = xs_dict_append(msg, "attributedTo", v);
  611. if ((v = xs_dict_get(object, "cc")))
  612. msg = xs_dict_append(msg, "cc", v);
  613. if ((v = xs_dict_get(object, "to")))
  614. msg = xs_dict_append(msg, "to", v);
  615. else
  616. msg = xs_dict_append(msg, "to", public_address);
  617. return msg;
  618. }
  619. d_char *msg_undo(snac *snac, char *object)
  620. /* creates an 'Undo' message */
  621. {
  622. d_char *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  623. msg = xs_dict_append(msg, "to", xs_dict_get(object, "object"));
  624. return msg;
  625. }
  626. d_char *msg_delete(snac *snac, char *id)
  627. /* creates a 'Delete' + 'Tombstone' for a local entry */
  628. {
  629. xs *tomb = xs_dict_new();
  630. d_char *msg = NULL;
  631. /* sculpt the tombstone */
  632. tomb = xs_dict_append(tomb, "type", "Tombstone");
  633. tomb = xs_dict_append(tomb, "id", id);
  634. /* now create the Delete */
  635. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  636. msg = xs_dict_append(msg, "to", public_address);
  637. return msg;
  638. }
  639. xs_dict *msg_follow(snac *snac, const char *q)
  640. /* creates a 'Follow' message */
  641. {
  642. xs *actor_o = NULL;
  643. xs *actor = NULL;
  644. d_char *msg = NULL;
  645. int status;
  646. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  647. if (xs_startswith(url_or_uid, "https:/"))
  648. actor = xs_dup(url_or_uid);
  649. else
  650. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  651. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  652. return NULL;
  653. }
  654. /* request the actor */
  655. status = actor_request(snac, actor, &actor_o);
  656. if (valid_status(status)) {
  657. /* check if the actor is an alias */
  658. char *r_actor = xs_dict_get(actor_o, "id");
  659. if (r_actor && strcmp(actor, r_actor) != 0) {
  660. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  661. }
  662. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  663. }
  664. else
  665. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  666. return msg;
  667. }
  668. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  669. xs_str *in_reply_to, xs_list *attach, int priv)
  670. /* creates a 'Note' message */
  671. {
  672. xs *ntid = tid(0);
  673. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  674. xs *ctxt = NULL;
  675. xs *fc2 = NULL;
  676. xs *fc1 = NULL;
  677. xs *to = NULL;
  678. xs *cc = xs_list_new();
  679. xs *irt = NULL;
  680. xs *tag = xs_list_new();
  681. xs *atls = xs_list_new();
  682. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  683. xs_list *p;
  684. xs_val *v;
  685. if (rcpts == NULL)
  686. to = xs_list_new();
  687. else {
  688. if (xs_type(rcpts) == XSTYPE_STRING) {
  689. to = xs_list_new();
  690. to = xs_list_append(to, rcpts);
  691. }
  692. else
  693. to = xs_dup(rcpts);
  694. }
  695. /* format the content */
  696. fc2 = not_really_markdown(content, &atls);
  697. if (in_reply_to != NULL && *in_reply_to) {
  698. xs *p_msg = NULL;
  699. xs *wrk = NULL;
  700. /* demand this thing */
  701. timeline_request(snac, &in_reply_to, &wrk);
  702. if (valid_status(object_get(in_reply_to, &p_msg))) {
  703. /* add this author as recipient */
  704. char *a, *v;
  705. if ((a = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, a) == -1)
  706. to = xs_list_append(to, a);
  707. /* add this author to the tag list as a mention */
  708. xs *t_href = NULL;
  709. xs *t_name = NULL;
  710. if (!xs_is_null(a) && valid_status(webfinger_request(a, &t_href, &t_name))) {
  711. xs *t = xs_dict_new();
  712. t = xs_dict_append(t, "type", "Mention");
  713. t = xs_dict_append(t, "href", t_href);
  714. t = xs_dict_append(t, "name", t_name);
  715. tag = xs_list_append(tag, t);
  716. }
  717. /* get the context, if there is one */
  718. if ((v = xs_dict_get(p_msg, "context")))
  719. ctxt = xs_dup(v);
  720. /* if this message is public, ours will also be */
  721. if (!priv && is_msg_public(snac, p_msg) && xs_list_in(to, public_address) == -1)
  722. to = xs_list_append(to, public_address);
  723. }
  724. irt = xs_dup(in_reply_to);
  725. }
  726. else
  727. irt = xs_val_new(XSTYPE_NULL);
  728. /* extract the mentions and hashtags and convert the content */
  729. process_tags(snac, fc2, &fc1, &tag);
  730. /* create the attachment list, if there are any */
  731. if (!xs_is_null(attach)) {
  732. while (xs_list_iter(&attach, &v)) {
  733. xs *d = xs_dict_new();
  734. char *url = xs_list_get(v, 0);
  735. char *alt = xs_list_get(v, 1);
  736. char *mime = xs_mime_by_ext(url);
  737. d = xs_dict_append(d, "mediaType", mime);
  738. d = xs_dict_append(d, "url", url);
  739. d = xs_dict_append(d, "name", alt);
  740. d = xs_dict_append(d, "type",
  741. xs_startswith(mime, "image/") ? "Image" : "Document");
  742. atls = xs_list_append(atls, d);
  743. }
  744. }
  745. if (ctxt == NULL)
  746. ctxt = xs_fmt("%s#ctxt", id);
  747. /* add all mentions to the cc */
  748. p = tag;
  749. while (xs_list_iter(&p, &v)) {
  750. if (xs_type(v) == XSTYPE_DICT) {
  751. char *t;
  752. if ((t = xs_dict_get(v, "type")) != NULL && strcmp(t, "Mention") == 0) {
  753. if ((t = xs_dict_get(v, "href")) != NULL)
  754. cc = xs_list_append(cc, t);
  755. }
  756. }
  757. }
  758. /* no recipients? must be for everybody */
  759. if (!priv && xs_list_len(to) == 0)
  760. to = xs_list_append(to, public_address);
  761. /* delete all cc recipients that also are in the to */
  762. p = to;
  763. while (xs_list_iter(&p, &v)) {
  764. int i;
  765. if ((i = xs_list_in(cc, v)) != -1)
  766. cc = xs_list_del(cc, i);
  767. }
  768. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  769. msg = xs_dict_append(msg, "summary", "");
  770. msg = xs_dict_append(msg, "content", fc1);
  771. msg = xs_dict_append(msg, "context", ctxt);
  772. msg = xs_dict_append(msg, "url", id);
  773. msg = xs_dict_append(msg, "to", to);
  774. msg = xs_dict_append(msg, "cc", cc);
  775. msg = xs_dict_append(msg, "inReplyTo", irt);
  776. msg = xs_dict_append(msg, "tag", tag);
  777. msg = xs_dict_append(msg, "sourceContent", content);
  778. if (xs_list_len(atls))
  779. msg = xs_dict_append(msg, "attachment", atls);
  780. return msg;
  781. }
  782. xs_dict *msg_ping(snac *user, const char *rcpt)
  783. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  784. {
  785. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  786. msg = xs_dict_append(msg, "to", rcpt);
  787. return msg;
  788. }
  789. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  790. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  791. {
  792. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  793. msg = xs_dict_append(msg, "to", rcpt);
  794. return msg;
  795. }
  796. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  797. const xs_list *opts, int multiple, int end_secs)
  798. /* creates a Question message */
  799. {
  800. xs *ntid = tid(0);
  801. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  802. int max = 8;
  803. xs_set seen;
  804. msg = xs_dict_set(msg, "type", "Question");
  805. /* make it non-editable */
  806. msg = xs_dict_del(msg, "sourceContent");
  807. xs *o = xs_list_new();
  808. xs_list *p = (xs_list *)opts;
  809. xs_str *v;
  810. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  811. xs_set_init(&seen);
  812. while (max && xs_list_iter(&p, &v)) {
  813. if (*v) {
  814. xs *v2 = xs_dup(v);
  815. xs *d = xs_dict_new();
  816. if (strlen(v2) > 60) {
  817. v2[60] = '\0';
  818. v2 = xs_str_cat(v2, "...");
  819. }
  820. if (xs_set_add(&seen, v2) == 1) {
  821. d = xs_dict_append(d, "name", v2);
  822. d = xs_dict_append(d, "replies", replies);
  823. o = xs_list_append(o, d);
  824. max--;
  825. }
  826. }
  827. }
  828. xs_set_free(&seen);
  829. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  830. /* set the end time */
  831. time_t t = time(NULL) + end_secs;
  832. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  833. msg = xs_dict_append(msg, "endTime", et);
  834. return msg;
  835. }
  836. int update_question(snac *user, const char *id)
  837. /* updates the poll counts */
  838. {
  839. xs *msg = NULL;
  840. xs *rcnt = xs_dict_new();
  841. xs *z = xs_number_new(0);
  842. xs *rcpts = xs_list_new();
  843. xs *lopts = xs_list_new();
  844. xs_list *opts;
  845. xs_list *p;
  846. xs_val *v;
  847. /* get the object */
  848. if (!valid_status(object_get(id, &msg)))
  849. return -1;
  850. /* closed? do nothing more */
  851. if (xs_dict_get(msg, "closed"))
  852. return -2;
  853. /* get the options */
  854. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  855. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  856. return -3;
  857. /* fill the initial count */
  858. p = opts;
  859. while (xs_list_iter(&p, &v)) {
  860. const char *name = xs_dict_get(v, "name");
  861. if (name) {
  862. lopts = xs_list_append(lopts, name);
  863. rcnt = xs_dict_set(rcnt, name, z);
  864. }
  865. }
  866. /* iterate now the children (the votes) */
  867. xs *chld = object_children(id);
  868. p = chld;
  869. while (xs_list_iter(&p, &v)) {
  870. xs *obj = NULL;
  871. if (!valid_status(object_get_by_md5(v, &obj)))
  872. continue;
  873. const char *name = xs_dict_get(obj, "name");
  874. const char *atto = xs_dict_get(obj, "attributedTo");
  875. if (name && atto) {
  876. /* get the current count */
  877. const xs_number *cnt = xs_dict_get(rcnt, name);
  878. if (xs_type(cnt) == XSTYPE_NUMBER) {
  879. /* if it exists, increment */
  880. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  881. rcnt = xs_dict_set(rcnt, name, ucnt);
  882. rcpts = xs_list_append(rcpts, atto);
  883. }
  884. }
  885. }
  886. /* create a new list of options with their new counts */
  887. xs *nopts = xs_list_new();
  888. p = lopts;
  889. while (xs_list_iter(&p, &v)) {
  890. const xs_number *cnt = xs_dict_get(rcnt, v);
  891. if (xs_type(cnt) == XSTYPE_NUMBER) {
  892. xs *d1 = xs_dict_new();
  893. xs *d2 = xs_dict_new();
  894. d2 = xs_dict_append(d2, "type", "Collection");
  895. d2 = xs_dict_append(d2, "totalItems", cnt);
  896. d1 = xs_dict_append(d1, "type", "Note");
  897. d1 = xs_dict_append(d1, "name", v);
  898. d1 = xs_dict_append(d1, "replies", d2);
  899. nopts = xs_list_append(nopts, d1);
  900. }
  901. }
  902. /* update the list */
  903. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  904. /* due date? */
  905. const char *end_time = xs_dict_get(msg, "endTime");
  906. if (!xs_is_null(end_time)) {
  907. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  908. /* it's now greater than the endTime? */
  909. if (strcmp(now, end_time) >= 0) {
  910. xs *et = xs_dup(end_time);
  911. msg = xs_dict_set(msg, "closed", et);
  912. notify(user, "Update", "Question", user->actor, msg);
  913. }
  914. }
  915. /* update the count of voters */
  916. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  917. msg = xs_dict_set(msg, "votersCount", vcnt);
  918. /* store */
  919. object_add_ow(id, msg);
  920. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  921. timeline_touch(user);
  922. /* send an update message to all voters */
  923. xs *u_msg = msg_update(user, msg);
  924. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  925. enqueue_message(user, u_msg);
  926. return 0;
  927. }
  928. /** queues **/
  929. int process_input_message(snac *snac, xs_dict *msg, xs_dict *req)
  930. /* processes an ActivityPub message from the input queue */
  931. {
  932. /* actor and type exist, were checked previously */
  933. char *actor = xs_dict_get(msg, "actor");
  934. char *type = xs_dict_get(msg, "type");
  935. xs *actor_o = NULL;
  936. int a_status;
  937. int do_notify = 0;
  938. /* question votes may not have a type */
  939. if (xs_is_null(type))
  940. type = "Note";
  941. if (xs_is_null(actor)) {
  942. snac_debug(snac, 0, xs_fmt("malformed message"));
  943. return 1;
  944. }
  945. char *object, *utype;
  946. object = xs_dict_get(msg, "object");
  947. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  948. utype = xs_dict_get(object, "type");
  949. else
  950. utype = "(null)";
  951. /* reject messages that are not for this user */
  952. if (!is_msg_for_me(snac, msg)) {
  953. snac_debug(snac, 0, xs_fmt("message from %s of type '%s' not for us", actor, type));
  954. return 1;
  955. }
  956. /* if it's a DM from someone we don't follow, reject the message */
  957. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  958. if (strcmp(utype, "Note") == 0 && !is_msg_public(snac, msg) &&
  959. !following_check(snac, actor)) {
  960. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  961. return 1;
  962. }
  963. }
  964. /* bring the actor */
  965. a_status = actor_request(snac, actor, &actor_o);
  966. /* if the actor does not explicitly exist, discard */
  967. if (a_status == 404 || a_status == 410) {
  968. snac_debug(snac, 1,
  969. xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  970. return 1;
  971. }
  972. if (!valid_status(a_status)) {
  973. /* other actor download errors may need a retry */
  974. snac_debug(snac, 1,
  975. xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  976. return 0;
  977. }
  978. /* check the signature */
  979. xs *sig_err = NULL;
  980. if (!check_signature(snac, req, &sig_err)) {
  981. snac_log(snac, xs_fmt("bad signature %s (%s)", actor, sig_err));
  982. srv_archive_error("check_signature", sig_err, req, msg);
  983. return 1;
  984. }
  985. if (strcmp(type, "Follow") == 0) { /** **/
  986. if (!follower_check(snac, actor)) {
  987. xs *f_msg = xs_dup(msg);
  988. xs *reply = msg_accept(snac, f_msg, actor);
  989. enqueue_message(snac, reply);
  990. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  991. /* add a date if it doesn't include one (Mastodon) */
  992. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  993. f_msg = xs_dict_set(f_msg, "published", date);
  994. }
  995. timeline_add(snac, xs_dict_get(f_msg, "id"), f_msg);
  996. follower_add(snac, actor);
  997. snac_log(snac, xs_fmt("new follower %s", actor));
  998. do_notify = 1;
  999. }
  1000. else
  1001. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1002. }
  1003. else
  1004. if (strcmp(type, "Undo") == 0) { /** **/
  1005. if (strcmp(utype, "Follow") == 0) { /** **/
  1006. if (valid_status(follower_del(snac, actor))) {
  1007. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1008. do_notify = 1;
  1009. }
  1010. else
  1011. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1012. }
  1013. else
  1014. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1015. }
  1016. else
  1017. if (strcmp(type, "Create") == 0) { /** **/
  1018. if (is_muted(snac, actor))
  1019. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1020. if (strcmp(utype, "Note") == 0) { /** **/
  1021. char *id = xs_dict_get(object, "id");
  1022. char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1023. xs *wrk = NULL;
  1024. timeline_request(snac, &in_reply_to, &wrk);
  1025. if (timeline_add(snac, id, object)) {
  1026. snac_log(snac, xs_fmt("new 'Note' %s %s", actor, id));
  1027. do_notify = 1;
  1028. }
  1029. /* if it has a "name" field, it may be a vote for a question */
  1030. const char *name = xs_dict_get(object, "name");
  1031. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1032. update_question(snac, in_reply_to);
  1033. }
  1034. else
  1035. if (strcmp(utype, "Question") == 0) { /** **/
  1036. char *id = xs_dict_get(object, "id");
  1037. if (timeline_add(snac, id, object))
  1038. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1039. }
  1040. else
  1041. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1042. }
  1043. else
  1044. if (strcmp(type, "Accept") == 0) { /** **/
  1045. if (strcmp(utype, "Follow") == 0) { /** **/
  1046. if (following_check(snac, actor)) {
  1047. following_add(snac, actor, msg);
  1048. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1049. }
  1050. else
  1051. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1052. }
  1053. else
  1054. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1055. }
  1056. else
  1057. if (strcmp(type, "Like") == 0) { /** **/
  1058. if (xs_type(object) == XSTYPE_DICT)
  1059. object = xs_dict_get(object, "id");
  1060. timeline_admire(snac, object, actor, 1);
  1061. snac_log(snac, xs_fmt("new 'Like' %s %s", actor, object));
  1062. do_notify = 1;
  1063. }
  1064. else
  1065. if (strcmp(type, "Announce") == 0) { /** **/
  1066. xs *a_msg = NULL;
  1067. xs *wrk = NULL;
  1068. if (xs_type(object) == XSTYPE_DICT)
  1069. object = xs_dict_get(object, "id");
  1070. timeline_request(snac, &object, &wrk);
  1071. if (valid_status(object_get(object, &a_msg))) {
  1072. char *who = xs_dict_get(a_msg, "attributedTo");
  1073. if (who && !is_muted(snac, who)) {
  1074. /* bring the actor */
  1075. xs *who_o = NULL;
  1076. if (valid_status(actor_request(snac, who, &who_o))) {
  1077. timeline_admire(snac, object, actor, 0);
  1078. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1079. do_notify = 1;
  1080. }
  1081. else
  1082. snac_log(snac, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1083. }
  1084. else
  1085. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1086. }
  1087. else
  1088. snac_log(snac, xs_fmt("error requesting 'Announce' object %s", object));
  1089. }
  1090. else
  1091. if (strcmp(type, "Update") == 0) { /** **/
  1092. if (strcmp(utype, "Person") == 0) {
  1093. actor_add(actor, xs_dict_get(msg, "object"));
  1094. timeline_touch(snac);
  1095. snac_log(snac, xs_fmt("updated actor %s", actor));
  1096. }
  1097. else
  1098. if (strcmp(utype, "Note") == 0) { /** **/
  1099. const char *id = xs_dict_get(object, "id");
  1100. object_add_ow(id, object);
  1101. timeline_touch(snac);
  1102. snac_log(snac, xs_fmt("updated post %s", id));
  1103. }
  1104. else
  1105. if (strcmp(utype, "Question") == 0) { /** **/
  1106. const char *id = xs_dict_get(object, "id");
  1107. const char *closed = xs_dict_get(object, "closed");
  1108. object_add_ow(id, object);
  1109. timeline_touch(snac);
  1110. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1111. if (closed != NULL)
  1112. do_notify = 1;
  1113. }
  1114. else
  1115. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1116. }
  1117. else
  1118. if (strcmp(type, "Delete") == 0) { /** **/
  1119. if (xs_type(object) == XSTYPE_DICT)
  1120. object = xs_dict_get(object, "id");
  1121. if (valid_status(timeline_del(snac, object)))
  1122. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1123. else
  1124. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1125. }
  1126. else
  1127. if (strcmp(type, "Pong") == 0) { /** **/
  1128. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1129. }
  1130. else
  1131. if (strcmp(type, "Ping") == 0) { /** **/
  1132. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1133. xs *rsp = msg_pong(snac, actor, xs_dict_get(msg, "id"));
  1134. enqueue_output_by_actor(snac, rsp, actor, 0);
  1135. }
  1136. else
  1137. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1138. if (do_notify) {
  1139. notify(snac, type, utype, actor, msg);
  1140. timeline_touch(snac);
  1141. }
  1142. return 1;
  1143. }
  1144. int send_email(char *msg)
  1145. /* invoke sendmail with email headers and body in msg */
  1146. {
  1147. FILE *f;
  1148. int status;
  1149. int fds[2];
  1150. pid_t pid;
  1151. if (pipe(fds) == -1) return -1;
  1152. pid = vfork();
  1153. if (pid == -1) return -1;
  1154. else if (pid == 0) {
  1155. dup2(fds[0], 0);
  1156. close(fds[0]);
  1157. close(fds[1]);
  1158. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1159. _exit(1);
  1160. }
  1161. close(fds[0]);
  1162. if ((f = fdopen(fds[1], "w")) == NULL) {
  1163. close(fds[1]);
  1164. return -1;
  1165. }
  1166. fprintf(f, "%s\n", msg);
  1167. fclose(f);
  1168. if (waitpid(pid, &status, 0) == -1) return -1;
  1169. return status;
  1170. }
  1171. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1172. /* processes an item from the user queue */
  1173. {
  1174. char *type;
  1175. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1176. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1177. type = "output";
  1178. if (strcmp(type, "message") == 0) {
  1179. xs_dict *msg = xs_dict_get(q_item, "message");
  1180. xs *rcpts = recipient_list(snac, msg, 1);
  1181. xs_set inboxes;
  1182. xs_list *p;
  1183. xs_str *actor;
  1184. xs_set_init(&inboxes);
  1185. /* iterate the recipients */
  1186. p = rcpts;
  1187. while (xs_list_iter(&p, &actor)) {
  1188. xs *inbox = get_actor_inbox(snac, actor);
  1189. if (inbox != NULL) {
  1190. /* add to the set and, if it's not there, send message */
  1191. if (xs_set_add(&inboxes, inbox) == 1)
  1192. enqueue_output(snac, msg, inbox, 0);
  1193. }
  1194. else
  1195. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1196. }
  1197. /* if it's public, send to the collected inboxes */
  1198. if (is_msg_public(snac, msg)) {
  1199. xs *shibx = inbox_list();
  1200. xs_str *inbox;
  1201. p = shibx;
  1202. while (xs_list_iter(&p, &inbox)) {
  1203. if (xs_set_add(&inboxes, inbox) == 1)
  1204. enqueue_output(snac, msg, inbox, 0);
  1205. }
  1206. }
  1207. xs_set_free(&inboxes);
  1208. }
  1209. else
  1210. if (strcmp(type, "input") == 0) {
  1211. /* process the message */
  1212. xs_dict *msg = xs_dict_get(q_item, "message");
  1213. xs_dict *req = xs_dict_get(q_item, "req");
  1214. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1215. if (xs_is_null(msg))
  1216. return;
  1217. if (!process_input_message(snac, msg, req)) {
  1218. if (retries > queue_retry_max)
  1219. snac_log(snac, xs_fmt("input giving up"));
  1220. else {
  1221. /* reenqueue */
  1222. enqueue_input(snac, msg, req, retries + 1);
  1223. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1224. }
  1225. }
  1226. }
  1227. else
  1228. if (strcmp(type, "close_question") == 0) {
  1229. /* the time for this question has ended */
  1230. const char *id = xs_dict_get(q_item, "message");
  1231. if (!xs_is_null(id))
  1232. update_question(snac, id);
  1233. }
  1234. else
  1235. snac_log(snac, xs_fmt("unexpected q_item type '%s'", type));
  1236. }
  1237. int process_user_queue(snac *snac)
  1238. /* processes a user's queue */
  1239. {
  1240. int cnt = 0;
  1241. xs *list = user_queue(snac);
  1242. xs_list *p = list;
  1243. xs_str *fn;
  1244. while (xs_list_iter(&p, &fn)) {
  1245. xs *q_item = dequeue(fn);
  1246. if (q_item == NULL) {
  1247. snac_log(snac, xs_fmt("process_user_queue q_item error"));
  1248. continue;
  1249. }
  1250. process_user_queue_item(snac, q_item);
  1251. cnt++;
  1252. }
  1253. return cnt;
  1254. }
  1255. void process_queue_item(xs_dict *q_item)
  1256. /* processes an item from the global queue */
  1257. {
  1258. char *type = xs_dict_get(q_item, "type");
  1259. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1260. if (strcmp(type, "output") == 0) {
  1261. int status;
  1262. xs_str *inbox = xs_dict_get(q_item, "inbox");
  1263. xs_str *keyid = xs_dict_get(q_item, "keyid");
  1264. xs_str *seckey = xs_dict_get(q_item, "seckey");
  1265. xs_dict *msg = xs_dict_get(q_item, "message");
  1266. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1267. xs *payload = NULL;
  1268. int p_size = 0;
  1269. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1270. srv_log(xs_fmt("output message error: missing fields"));
  1271. return;
  1272. }
  1273. /* deliver */
  1274. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, retries == 0 ? 3 : 8);
  1275. if (payload) {
  1276. if (p_size > 64) {
  1277. /* trim the message */
  1278. payload[64] = '\0';
  1279. payload = xs_str_cat(payload, "...");
  1280. }
  1281. /* strip ugly control characters */
  1282. payload = xs_replace_i(payload, "\n", "");
  1283. payload = xs_replace_i(payload, "\r", "");
  1284. if (*payload)
  1285. payload = xs_str_wrap_i(" [", payload, "]");
  1286. }
  1287. else
  1288. payload = xs_str_new(NULL);
  1289. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1290. if (!valid_status(status)) {
  1291. retries++;
  1292. /* error sending; requeue? */
  1293. if (status == 404 || status == 410)
  1294. /* explicit error: discard */
  1295. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1296. else
  1297. if (retries > queue_retry_max)
  1298. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1299. else {
  1300. /* requeue */
  1301. enqueue_output_raw(keyid, seckey, msg, inbox, retries);
  1302. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1303. }
  1304. }
  1305. }
  1306. else
  1307. if (strcmp(type, "email") == 0) {
  1308. /* send this email */
  1309. xs_str *msg = xs_dict_get(q_item, "message");
  1310. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1311. if (!send_email(msg))
  1312. srv_debug(1, xs_fmt("email message sent"));
  1313. else {
  1314. retries++;
  1315. if (retries > queue_retry_max)
  1316. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  1317. else {
  1318. /* requeue */
  1319. srv_log(xs_fmt(
  1320. "email requeue #%d (errno: %d)", retries, errno));
  1321. enqueue_email(msg, retries);
  1322. }
  1323. }
  1324. }
  1325. else
  1326. if (strcmp(type, "telegram") == 0) {
  1327. /* send this via telegram */
  1328. char *bot = xs_dict_get(q_item, "bot");
  1329. char *msg = xs_dict_get(q_item, "message");
  1330. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  1331. int status = 0;
  1332. /* chat_id must start with a - */
  1333. if (!xs_startswith(chat_id, "-"))
  1334. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  1335. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  1336. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  1337. xs *headers = xs_dict_new();
  1338. headers = xs_dict_append(headers, "content-type", "application/json");
  1339. xs *rsp = xs_http_request("POST", url, headers,
  1340. body, strlen(body), &status, NULL, NULL, 0);
  1341. rsp = xs_free(rsp);
  1342. srv_debug(0, xs_fmt("telegram post %d", status));
  1343. }
  1344. else
  1345. if (strcmp(type, "purge") == 0) {
  1346. srv_log(xs_dup("purge start"));
  1347. purge_all();
  1348. srv_log(xs_dup("purge end"));
  1349. }
  1350. else
  1351. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  1352. }
  1353. int process_queue(void)
  1354. /* processes the global queue */
  1355. {
  1356. int cnt = 0;
  1357. xs *list = queue();
  1358. xs_list *p = list;
  1359. xs_str *fn;
  1360. while (xs_list_iter(&p, &fn)) {
  1361. xs *q_item = dequeue(fn);
  1362. if (q_item != NULL) {
  1363. job_post(q_item, 0);
  1364. cnt++;
  1365. }
  1366. }
  1367. return cnt;
  1368. }
  1369. /** HTTP handlers */
  1370. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  1371. char **body, int *b_size, char **ctype)
  1372. {
  1373. int status = 200;
  1374. char *accept = xs_dict_get(req, "accept");
  1375. snac snac;
  1376. xs *msg = NULL;
  1377. if (accept == NULL)
  1378. return 0;
  1379. if (xs_str_in(accept, "application/activity+json") == -1 &&
  1380. xs_str_in(accept, "application/ld+json") == -1)
  1381. return 0;
  1382. xs *l = xs_split_n(q_path, "/", 2);
  1383. char *uid, *p_path;
  1384. uid = xs_list_get(l, 1);
  1385. if (!user_open(&snac, uid)) {
  1386. /* invalid user */
  1387. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  1388. return 404;
  1389. }
  1390. p_path = xs_list_get(l, 2);
  1391. *ctype = "application/activity+json";
  1392. if (p_path == NULL) {
  1393. /* if there was no component after the user, it's an actor request */
  1394. msg = msg_actor(&snac);
  1395. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  1396. char *ua = xs_dict_get(req, "user-agent");
  1397. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  1398. }
  1399. else
  1400. if (strcmp(p_path, "outbox") == 0) {
  1401. xs *id = xs_fmt("%s/outbox", snac.actor);
  1402. xs *elems = timeline_simple_list(&snac, "public", 0, 20);
  1403. xs *list = xs_list_new();
  1404. msg = msg_collection(&snac, id);
  1405. char *p, *v;
  1406. p = elems;
  1407. while (xs_list_iter(&p, &v)) {
  1408. xs *i = NULL;
  1409. if (valid_status(object_get_by_md5(v, &i))) {
  1410. char *type = xs_dict_get(i, "type");
  1411. char *id = xs_dict_get(i, "id");
  1412. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  1413. i = xs_dict_del(i, "_snac");
  1414. list = xs_list_append(list, i);
  1415. }
  1416. }
  1417. }
  1418. /* replace the 'orderedItems' with the latest posts */
  1419. xs *items = xs_number_new(xs_list_len(list));
  1420. msg = xs_dict_set(msg, "orderedItems", list);
  1421. msg = xs_dict_set(msg, "totalItems", items);
  1422. }
  1423. else
  1424. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  1425. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1426. msg = msg_collection(&snac, id);
  1427. }
  1428. else
  1429. if (xs_startswith(p_path, "p/")) {
  1430. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  1431. status = object_get(id, &msg);
  1432. }
  1433. else
  1434. status = 404;
  1435. if (status == 200 && msg != NULL) {
  1436. *body = xs_json_dumps_pp(msg, 4);
  1437. *b_size = strlen(*body);
  1438. }
  1439. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  1440. user_free(&snac);
  1441. return status;
  1442. }
  1443. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  1444. char *payload, int p_size,
  1445. char **body, int *b_size, char **ctype)
  1446. /* processes an input message */
  1447. {
  1448. (void)b_size;
  1449. int status = 202; /* accepted */
  1450. char *i_ctype = xs_dict_get(req, "content-type");
  1451. snac snac;
  1452. char *v;
  1453. if (i_ctype == NULL) {
  1454. *body = xs_str_new("no content-type");
  1455. *ctype = "text/plain";
  1456. return 400;
  1457. }
  1458. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  1459. xs_str_in(i_ctype, "application/ld+json") == -1)
  1460. return 0;
  1461. /* decode the message */
  1462. xs *msg = xs_json_loads(payload);
  1463. if (msg == NULL) {
  1464. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  1465. *body = xs_str_new("JSON error");
  1466. *ctype = "text/plain";
  1467. status = 400;
  1468. }
  1469. /* get the user and path */
  1470. xs *l = xs_split_n(q_path, "/", 2);
  1471. char *uid;
  1472. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  1473. /* strange q_path */
  1474. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  1475. return 404;
  1476. }
  1477. uid = xs_list_get(l, 1);
  1478. if (!user_open(&snac, uid)) {
  1479. /* invalid user */
  1480. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  1481. return 404;
  1482. }
  1483. /* if it has a digest, check it now, because
  1484. later the payload won't be exactly the same */
  1485. if ((v = xs_dict_get(req, "digest")) != NULL) {
  1486. xs *s1 = xs_sha256_base64(payload, p_size);
  1487. xs *s2 = xs_fmt("SHA-256=%s", s1);
  1488. if (strcmp(s2, v) != 0) {
  1489. srv_log(xs_fmt("digest check FAILED"));
  1490. *body = xs_str_new("bad digest");
  1491. *ctype = "text/plain";
  1492. status = 400;
  1493. }
  1494. }
  1495. /* if the message is from a muted actor, reject it right now */
  1496. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  1497. if (is_muted(&snac, v)) {
  1498. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  1499. *body = xs_str_new("rejected");
  1500. *ctype = "text/plain";
  1501. status = 403;
  1502. }
  1503. }
  1504. if (valid_status(status)) {
  1505. enqueue_input(&snac, msg, req, 0);
  1506. *ctype = "application/activity+json";
  1507. }
  1508. user_free(&snac);
  1509. return status;
  1510. }