activitypub.c 55 KB

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