activitypub.c 61 KB

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