activitypub.c 63 KB

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