activitypub.c 55 KB

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