activitypub.c 58 KB

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