activitypub.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 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 "xs_match.h"
  12. #include "snac.h"
  13. #include <sys/wait.h>
  14. const char *public_address = "https:/" "/www.w3.org/ns/activitystreams#Public";
  15. /* susie.png */
  16. const char *susie =
  17. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  18. "CEkxzAAAAUUlEQVQoz43R0QkAMQwCUDdw/y3dwE"
  19. "vsvzlL4X1IoQkAisKmwfAFT3RgJHbQezpSRoXEq"
  20. "eqCL9BJBf7h3QbOCCxV5EVWMEMwG7K1/WODtlvx"
  21. "AYTtEsDU9F34AAAAAElFTkSuQmCC";
  22. const char *susie_cool =
  23. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  24. "CEkxzAAAAV0lEQVQoz43RwQ3AMAwCQDZg/y3ZgN"
  25. "qo3+JaedwDOUQBQFHYaTB8wTM6sGl2cMPu+DFzn"
  26. "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
  27. "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
  28. const char *susie_muertos =
  29. "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
  30. "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
  31. "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
  32. "xXksrZYgZwJrIox+Z8vJAfe2lCxG6AK7eKkWcEb"
  33. "QHbF617xAQatAAD7jJHUAAAAAElFTkSuQmCC";
  34. const char *default_avatar_base64(void)
  35. /* returns the default avatar in base64 */
  36. {
  37. time_t t = time(NULL);
  38. struct tm tm;
  39. const char *p = susie;
  40. gmtime_r(&t, &tm);
  41. if (tm.tm_mon == 10 && tm.tm_mday == 2)
  42. p = susie_muertos;
  43. else
  44. if (tm.tm_wday == 0 || tm.tm_wday == 6)
  45. p = susie_cool;
  46. return p;
  47. }
  48. int activitypub_request(snac *user, const char *url, xs_dict **data)
  49. /* request an object */
  50. {
  51. int status = 0;
  52. xs *response = NULL;
  53. xs *payload = NULL;
  54. int p_size;
  55. const char *ctype;
  56. *data = NULL;
  57. if (user != NULL) {
  58. /* get from the net */
  59. response = http_signed_request(user, "GET", url,
  60. NULL, NULL, 0, &status, &payload, &p_size, 0);
  61. }
  62. if (status == 0 || (status >= 500 && status <= 599)) {
  63. /* I found an instance running Misskey that returned
  64. 500 on signed messages but returned the object
  65. perfectly without signing (?), so why not try */
  66. xs_free(response);
  67. xs *hdrs = xs_dict_new();
  68. hdrs = xs_dict_append(hdrs, "accept", "application/activity+json");
  69. hdrs = xs_dict_append(hdrs, "user-agent", USER_AGENT);
  70. response = xs_http_request("GET", url, hdrs,
  71. NULL, 0, &status, &payload, &p_size, 0);
  72. }
  73. if (valid_status(status)) {
  74. /* ensure it's ActivityPub data */
  75. ctype = xs_dict_get(response, "content-type");
  76. if (xs_is_null(ctype))
  77. status = HTTP_STATUS_BAD_REQUEST;
  78. else
  79. if (xs_str_in(ctype, "application/activity+json") != -1 ||
  80. xs_str_in(ctype, "application/ld+json") != -1) {
  81. /* if there is no payload, fail */
  82. if (xs_is_null(payload))
  83. status = HTTP_STATUS_BAD_REQUEST;
  84. else
  85. *data = xs_json_loads(payload);
  86. }
  87. else
  88. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  89. }
  90. return status;
  91. }
  92. int actor_request(snac *user, const char *actor, xs_dict **data)
  93. /* request an actor */
  94. {
  95. int status;
  96. xs *payload = NULL;
  97. if (data)
  98. *data = NULL;
  99. /* get from disk first */
  100. status = actor_get_refresh(user, actor, data);
  101. if (!valid_status(status)) {
  102. /* actor data non-existent: get from the net */
  103. status = activitypub_request(user, actor, &payload);
  104. if (valid_status(status)) {
  105. /* renew data */
  106. status = actor_add(actor, payload);
  107. if (data != NULL) {
  108. *data = payload;
  109. payload = NULL;
  110. }
  111. }
  112. else
  113. srv_debug(1, xs_fmt("actor_request error %s %d", actor, status));
  114. }
  115. /* collect the (presumed) shared inbox in this actor */
  116. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  117. if (valid_status(status) && data && *data)
  118. inbox_add_by_actor(*data);
  119. }
  120. return status;
  121. }
  122. const char *get_atto(const xs_dict *msg)
  123. /* gets the attributedTo field (an actor) */
  124. {
  125. const xs_val *actor = xs_dict_get(msg, "attributedTo");
  126. /* if the actor is a list of objects (like on Peertube videos), pick the Person */
  127. if (xs_type(actor) == XSTYPE_LIST) {
  128. const xs_list *p = actor;
  129. int c = 0;
  130. const xs_dict *v;
  131. actor = NULL;
  132. while (actor == NULL && xs_list_next(p, &v, &c)) {
  133. if (xs_type(v) == XSTYPE_DICT) {
  134. const char *type = xs_dict_get(v, "type");
  135. if (xs_type(type) == XSTYPE_STRING && strcmp(type, "Person") == 0) {
  136. actor = xs_dict_get(v, "id");
  137. if (xs_type(actor) != XSTYPE_STRING)
  138. actor = NULL;
  139. }
  140. }
  141. }
  142. }
  143. return actor;
  144. }
  145. xs_list *get_attachments(const xs_dict *msg)
  146. /* unify the garbage fire that are the attachments */
  147. {
  148. xs_list *l = xs_list_new();
  149. const xs_list *p;
  150. /* try first the attachments list */
  151. if (!xs_is_null(p = xs_dict_get(msg, "attachment"))) {
  152. xs *attach = NULL;
  153. const xs_val *v;
  154. /* ensure it's a list */
  155. if (xs_type(p) == XSTYPE_DICT) {
  156. attach = xs_list_new();
  157. attach = xs_list_append(attach, p);
  158. }
  159. else
  160. attach = xs_dup(p);
  161. if (xs_type(attach) == XSTYPE_LIST) {
  162. /* does the message have an image? */
  163. const xs_dict *d = xs_dict_get(msg, "image");
  164. if (xs_type(d) == XSTYPE_DICT) {
  165. /* add it to the attachment list */
  166. attach = xs_list_append(attach, d);
  167. }
  168. }
  169. /* now iterate the list */
  170. int c = 0;
  171. while (xs_list_next(attach, &v, &c)) {
  172. const char *type = xs_dict_get(v, "mediaType");
  173. if (xs_is_null(type))
  174. type = xs_dict_get(v, "type");
  175. if (xs_is_null(type))
  176. continue;
  177. const char *href = xs_dict_get(v, "url");
  178. if (xs_is_null(href))
  179. href = xs_dict_get(v, "href");
  180. if (xs_is_null(href))
  181. continue;
  182. /* infer MIME type from non-specific attachments */
  183. if (xs_list_len(attach) < 2 && xs_match(type, "Link|Document")) {
  184. char *mt = (char *)xs_mime_by_ext(href);
  185. if (xs_match(mt, "image/*|audio/*|video/*")) /* */
  186. type = mt;
  187. }
  188. const char *name = xs_dict_get(v, "name");
  189. if (xs_is_null(name))
  190. name = xs_dict_get(msg, "name");
  191. if (xs_is_null(name))
  192. name = "";
  193. xs *d = xs_dict_new();
  194. d = xs_dict_append(d, "type", type);
  195. d = xs_dict_append(d, "href", href);
  196. d = xs_dict_append(d, "name", name);
  197. l = xs_list_append(l, d);
  198. }
  199. }
  200. /** urls (attachments from Peertube) **/
  201. p = xs_dict_get(msg, "url");
  202. if (xs_type(p) == XSTYPE_LIST) {
  203. const char *href = NULL;
  204. const char *type = NULL;
  205. int c = 0;
  206. const xs_val *v;
  207. while (href == NULL && xs_list_next(p, &v, &c)) {
  208. if (xs_type(v) == XSTYPE_DICT) {
  209. const char *mtype = xs_dict_get(v, "type");
  210. if (xs_type(mtype) == XSTYPE_STRING && strcmp(mtype, "Link") == 0) {
  211. mtype = xs_dict_get(v, "mediaType");
  212. const xs_list *tag = xs_dict_get(v, "tag");
  213. if (xs_type(mtype) == XSTYPE_STRING &&
  214. strcmp(mtype, "application/x-mpegURL") == 0 &&
  215. xs_type(tag) == XSTYPE_LIST) {
  216. /* now iterate the tag list, looking for a video URL */
  217. const xs_dict *d;
  218. int c = 0;
  219. while (href == NULL && xs_list_next(tag, &d, &c)) {
  220. if (xs_type(d) == XSTYPE_DICT) {
  221. if (xs_type(mtype = xs_dict_get(d, "mediaType")) == XSTYPE_STRING &&
  222. xs_startswith(mtype, "video/")) {
  223. const char *h = xs_dict_get(d, "href");
  224. /* this is probably it */
  225. if (xs_type(h) == XSTYPE_STRING) {
  226. href = h;
  227. type = mtype;
  228. }
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. if (href && type) {
  237. xs *d = xs_dict_new();
  238. d = xs_dict_append(d, "href", href);
  239. d = xs_dict_append(d, "type", type);
  240. d = xs_dict_append(d, "name", "---");
  241. l = xs_list_append(l, d);
  242. }
  243. }
  244. return l;
  245. }
  246. int timeline_request(snac *snac, const char **id, xs_str **wrk, int level)
  247. /* ensures that an entry and its ancestors are in the timeline */
  248. {
  249. int status = 0;
  250. if (level < MAX_CONVERSATION_LEVELS && !xs_is_null(*id)) {
  251. xs *msg = NULL;
  252. /* from a blocked instance? discard and break */
  253. if (is_instance_blocked(*id)) {
  254. snac_debug(snac, 1, xs_fmt("timeline_request blocked instance %s", *id));
  255. return status;
  256. }
  257. /* is the object already there? */
  258. if (!valid_status(object_get(*id, &msg))) {
  259. /* no; download it */
  260. status = activitypub_request(snac, *id, &msg);
  261. if (valid_status(status)) {
  262. const xs_dict *object = msg;
  263. const char *type = xs_dict_get(object, "type");
  264. /* get the id again from the object, as it may be different */
  265. const char *nid = xs_dict_get(object, "id");
  266. if (xs_type(nid) != XSTYPE_STRING)
  267. return 0;
  268. if (wrk && strcmp(nid, *id) != 0) {
  269. snac_debug(snac, 1,
  270. xs_fmt("timeline_request canonical id for %s is %s", *id, nid));
  271. *wrk = xs_dup(nid);
  272. *id = *wrk;
  273. }
  274. if (xs_is_null(type))
  275. type = "(null)";
  276. srv_debug(2, xs_fmt("timeline_request type %s '%s'", nid, type));
  277. if (strcmp(type, "Create") == 0) {
  278. /* some software like lemmy nest Announce + Create + Note */
  279. if (!xs_is_null(object = xs_dict_get(object, "object"))) {
  280. type = xs_dict_get(object, "type");
  281. nid = xs_dict_get(object, "id");
  282. }
  283. else
  284. type = "(null)";
  285. }
  286. if (xs_match(type, POSTLIKE_OBJECT_TYPE)) {
  287. if (content_match("filter_reject.txt", object))
  288. snac_log(snac, xs_fmt("timeline_request rejected by content %s", nid));
  289. else {
  290. const char *actor = get_atto(object);
  291. if (!xs_is_null(actor)) {
  292. /* request (and drop) the actor for this entry */
  293. if (!valid_status(actor_request(snac, actor, NULL))) {
  294. /* failed? retry later */
  295. enqueue_actor_refresh(snac, actor, 60);
  296. }
  297. /* does it have an ancestor? */
  298. const char *in_reply_to = xs_dict_get(object, "inReplyTo");
  299. /* store */
  300. timeline_add(snac, nid, object);
  301. /* redistribute to lists for this user */
  302. list_distribute(snac, actor, object);
  303. /* recurse! */
  304. timeline_request(snac, &in_reply_to, NULL, level + 1);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. return status;
  312. }
  313. int send_to_inbox_raw(const char *keyid, const char *seckey,
  314. const xs_str *inbox, const xs_dict *msg,
  315. xs_val **payload, int *p_size, int timeout)
  316. /* sends a message to an Inbox */
  317. {
  318. int status;
  319. xs_dict *response;
  320. xs *j_msg = xs_json_dumps((xs_dict *)msg, 4);
  321. response = http_signed_request_raw(keyid, seckey, "POST", inbox,
  322. NULL, j_msg, strlen(j_msg), &status, payload, p_size, timeout);
  323. xs_free(response);
  324. return status;
  325. }
  326. int send_to_inbox(snac *snac, const xs_str *inbox, const xs_dict *msg,
  327. xs_val **payload, int *p_size, int timeout)
  328. /* sends a message to an Inbox */
  329. {
  330. const char *seckey = xs_dict_get(snac->key, "secret");
  331. return send_to_inbox_raw(snac->actor, seckey, inbox, msg, payload, p_size, timeout);
  332. }
  333. xs_str *get_actor_inbox(const char *actor, int shared)
  334. /* gets an actor's inbox */
  335. {
  336. xs *data = NULL;
  337. const char *v = NULL;
  338. if (valid_status(actor_request(NULL, actor, &data))) {
  339. /* try first endpoints/sharedInbox */
  340. if (shared && (v = xs_dict_get(data, "endpoints")))
  341. v = xs_dict_get(v, "sharedInbox");
  342. /* try then the regular inbox */
  343. if (xs_is_null(v))
  344. v = xs_dict_get(data, "inbox");
  345. }
  346. return xs_is_null(v) ? NULL : xs_dup(v);
  347. }
  348. int send_to_actor(snac *snac, const char *actor, const xs_dict *msg,
  349. xs_val **payload, int *p_size, int timeout)
  350. /* sends a message to an actor */
  351. {
  352. int status = HTTP_STATUS_BAD_REQUEST;
  353. xs *inbox = get_actor_inbox(actor, 1);
  354. if (!xs_is_null(inbox))
  355. status = send_to_inbox(snac, inbox, msg, payload, p_size, timeout);
  356. return status;
  357. }
  358. void post_message(snac *snac, const char *actor, const xs_dict *msg)
  359. /* posts a message immediately (bypassing the output queues) */
  360. {
  361. xs *payload = NULL;
  362. int p_size;
  363. int status = send_to_actor(snac, actor, msg, &payload, &p_size, 3);
  364. srv_log(xs_fmt("post_message to actor %s %d", actor, status));
  365. if (!valid_status(status))
  366. /* cannot send right now, enqueue */
  367. enqueue_message(snac, msg);
  368. }
  369. xs_list *recipient_list(snac *snac, const xs_dict *msg, int expand_public)
  370. /* returns the list of recipients for a message */
  371. {
  372. const xs_val *to = xs_dict_get(msg, "to");
  373. const xs_val *cc = xs_dict_get(msg, "cc");
  374. xs_set rcpts;
  375. int n;
  376. xs_set_init(&rcpts);
  377. const xs_list *lists[] = { to, cc, NULL };
  378. for (n = 0; lists[n]; n++) {
  379. xs_list *l = (xs_list *)lists[n];
  380. const char *v;
  381. xs *tl = NULL;
  382. /* if it's a string, create a list with only one element */
  383. if (xs_type(l) == XSTYPE_STRING) {
  384. tl = xs_list_new();
  385. tl = xs_list_append(tl, l);
  386. l = tl;
  387. }
  388. while (xs_list_iter(&l, &v)) {
  389. if (expand_public && strcmp(v, public_address) == 0) {
  390. /* iterate the followers and add them */
  391. xs *fwers = follower_list(snac);
  392. const char *actor;
  393. char *p = fwers;
  394. while (xs_list_iter(&p, &actor))
  395. xs_set_add(&rcpts, actor);
  396. }
  397. else
  398. xs_set_add(&rcpts, v);
  399. }
  400. }
  401. return xs_set_result(&rcpts);
  402. }
  403. int is_msg_public(const xs_dict *msg)
  404. /* checks if a message is public */
  405. {
  406. const char *to = xs_dict_get(msg, "to");
  407. const char *cc = xs_dict_get(msg, "cc");
  408. int n;
  409. const char *lists[] = { to, cc, NULL };
  410. for (n = 0; lists[n]; n++) {
  411. const xs_val *l = lists[n];
  412. if (xs_type(l) == XSTYPE_STRING) {
  413. if (strcmp(l, public_address) == 0)
  414. return 1;
  415. }
  416. else
  417. if (xs_type(l) == XSTYPE_LIST) {
  418. if (xs_list_in(l, public_address) != -1)
  419. return 1;
  420. }
  421. }
  422. return 0;
  423. }
  424. int is_msg_from_private_user(const xs_dict *msg)
  425. /* checks if a message is from a local, private user */
  426. {
  427. int ret = 0;
  428. /* is this message from a local user? */
  429. if (xs_startswith(xs_dict_get(msg, "id"), srv_baseurl)) {
  430. const char *atto = get_atto(msg);
  431. xs *l = xs_split(atto, "/");
  432. const char *uid = xs_list_get(l, -1);
  433. snac user;
  434. if (uid && user_open(&user, uid)) {
  435. if (xs_type(xs_dict_get(user.config, "private")) == XSTYPE_TRUE)
  436. ret = 1;
  437. user_free(&user);
  438. }
  439. }
  440. return ret;
  441. }
  442. int is_msg_for_me(snac *snac, const xs_dict *c_msg)
  443. /* checks if this message is for me */
  444. {
  445. const char *type = xs_dict_get(c_msg, "type");
  446. const char *actor = xs_dict_get(c_msg, "actor");
  447. if (strcmp(actor, snac->actor) == 0) {
  448. /* message by myself? (most probably via the shared-inbox) reject */
  449. snac_debug(snac, 1, xs_fmt("ignoring message by myself"));
  450. return 0;
  451. }
  452. if (xs_match(type, "Like|Announce|EmojiReact")) {
  453. const char *object = xs_dict_get(c_msg, "object");
  454. if (xs_type(object) == XSTYPE_DICT)
  455. object = xs_dict_get(object, "id");
  456. /* bad object id? reject */
  457. if (xs_type(object) != XSTYPE_STRING)
  458. return 0;
  459. /* if it's about one of our posts, accept it */
  460. if (xs_startswith(object, snac->actor))
  461. return 2;
  462. /* if it's by someone we don't follow, reject */
  463. return following_check(snac, actor);
  464. }
  465. /* if it's an Undo, it must be from someone related to us */
  466. if (xs_match(type, "Undo")) {
  467. return follower_check(snac, actor) || following_check(snac, actor);
  468. }
  469. /* if it's an Accept + Follow, it must be for a Follow we created */
  470. if (xs_match(type, "Accept")) {
  471. return following_check(snac, actor);
  472. }
  473. /* if it's a Follow, it must be explicitly for us */
  474. if (xs_match(type, "Follow")) {
  475. const char *object = xs_dict_get(c_msg, "object");
  476. return !xs_is_null(object) && strcmp(snac->actor, object) == 0;
  477. }
  478. /* only accept Ping directed to us */
  479. if (xs_match(type, "Ping")) {
  480. const char *dest = xs_dict_get(c_msg, "to");
  481. return !xs_is_null(dest) && strcmp(snac->actor, dest) == 0;
  482. }
  483. /* if it's not a Create or Update, allow as is */
  484. if (!xs_match(type, "Create|Update")) {
  485. return 1;
  486. }
  487. int pub_msg = is_msg_public(c_msg);
  488. /* if this message is public and we follow the actor of this post, allow */
  489. if (pub_msg && following_check(snac, actor))
  490. return 1;
  491. const xs_dict *msg = xs_dict_get(c_msg, "object");
  492. xs *rcpts = recipient_list(snac, msg, 0);
  493. xs_list *p = rcpts;
  494. const xs_str *v;
  495. xs *actor_followers = NULL;
  496. if (!pub_msg) {
  497. /* not a public message; get the actor and its followers list */
  498. xs *actor_obj = NULL;
  499. if (valid_status(object_get(actor, &actor_obj))) {
  500. const xs_val *fw = xs_dict_get(actor_obj, "followers");
  501. if (fw)
  502. actor_followers = xs_dup(fw);
  503. }
  504. }
  505. while(xs_list_iter(&p, &v)) {
  506. /* explicitly for me? accept */
  507. if (strcmp(v, snac->actor) == 0)
  508. return 2;
  509. if (pub_msg) {
  510. /* a public message for someone we follow? (probably cc'ed) accept */
  511. if (following_check(snac, v))
  512. return 5;
  513. }
  514. else
  515. if (actor_followers && strcmp(v, actor_followers) == 0) {
  516. /* if this message is for this actor's followers, are we one of them? */
  517. if (following_check(snac, actor))
  518. return 6;
  519. }
  520. }
  521. /* accept if it's by someone we follow */
  522. const char *atto = get_atto(msg);
  523. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  524. return 3;
  525. /* is this message a reply to another? */
  526. const char *irt = xs_dict_get(msg, "inReplyTo");
  527. if (!xs_is_null(irt)) {
  528. xs *r_msg = NULL;
  529. /* try to get the replied message */
  530. if (valid_status(object_get(irt, &r_msg))) {
  531. atto = get_atto(r_msg);
  532. /* accept if the replied message is from someone we follow */
  533. if (pub_msg && !xs_is_null(atto) && following_check(snac, atto))
  534. return 4;
  535. }
  536. }
  537. return 0;
  538. }
  539. xs_str *process_tags(snac *snac, const char *content, xs_list **tag)
  540. /* parses mentions and tags from content */
  541. {
  542. xs_str *nc = xs_str_new(NULL);
  543. xs_list *tl = *tag;
  544. xs *split;
  545. xs_list *p;
  546. const xs_val *v;
  547. int n = 0;
  548. /* create a default server for incomplete mentions */
  549. xs *def_srv = NULL;
  550. if (xs_list_len(tl)) {
  551. /* if there are any mentions, get the server from
  552. the first one, which is the inReplyTo author */
  553. p = tl;
  554. while (xs_list_iter(&p, &v)) {
  555. const char *type = xs_dict_get(v, "type");
  556. const char *name = xs_dict_get(v, "name");
  557. if (type && name && strcmp(type, "Mention") == 0) {
  558. xs *l = xs_split(name, "@");
  559. def_srv = xs_dup(xs_list_get(l, -1));
  560. break;
  561. }
  562. }
  563. }
  564. if (xs_is_null(def_srv))
  565. /* use this same server */
  566. def_srv = xs_dup(xs_dict_get(srv_config, "host"));
  567. split = xs_regex_split(content, "(@[A-Za-z0-9_]+(@[A-Za-z0-9\\.-]+)?|&#[0-9]+;|#[^[:punct:][:space:]]+)");
  568. p = split;
  569. while (xs_list_iter(&p, &v)) {
  570. if ((n & 0x1)) {
  571. if (*v == '@') {
  572. xs *link = NULL;
  573. xs *wuid = NULL;
  574. if (strchr(v + 1, '@') == NULL) {
  575. /* only one @? it's a dumb Mastodon-like mention
  576. without server; add the default one */
  577. wuid = xs_fmt("%s@%s", v, def_srv);
  578. snac_debug(snac, 2, xs_fmt("mention without server '%s' '%s'", v, wuid));
  579. }
  580. else
  581. wuid = xs_dup(v);
  582. /* query the webfinger about this fellow */
  583. xs *actor = NULL;
  584. xs *uid = NULL;
  585. int status;
  586. status = webfinger_request(wuid, &actor, &uid);
  587. if (valid_status(status) && actor && uid) {
  588. xs *d = xs_dict_new();
  589. xs *n = xs_fmt("@%s", uid);
  590. d = xs_dict_append(d, "type", "Mention");
  591. d = xs_dict_append(d, "href", actor);
  592. d = xs_dict_append(d, "name", n);
  593. tl = xs_list_append(tl, d);
  594. link = xs_fmt("<a href=\"%s\" class=\"u-url h-card mention\">%s</a>", actor, n);
  595. }
  596. if (!xs_is_null(link))
  597. nc = xs_str_cat(nc, link);
  598. else
  599. nc = xs_str_cat(nc, v);
  600. }
  601. else
  602. if (*v == '#') {
  603. /* hashtag */
  604. xs *d = xs_dict_new();
  605. xs *n = xs_tolower_i(xs_dup(v));
  606. xs *h = xs_fmt("%s?t=%s", srv_baseurl, n + 1);
  607. xs *l = xs_fmt("<a href=\"%s\" class=\"mention hashtag\" rel=\"tag\">%s</a>", h, v);
  608. d = xs_dict_append(d, "type", "Hashtag");
  609. d = xs_dict_append(d, "href", h);
  610. d = xs_dict_append(d, "name", n);
  611. tl = xs_list_append(tl, d);
  612. /* add the code */
  613. nc = xs_str_cat(nc, l);
  614. }
  615. else
  616. if (*v == '&') {
  617. /* HTML Unicode entity, probably part of an emoji */
  618. /* write as is */
  619. nc = xs_str_cat(nc, v);
  620. }
  621. }
  622. else
  623. nc = xs_str_cat(nc, v);
  624. n++;
  625. }
  626. *tag = tl;
  627. return nc;
  628. }
  629. void notify(snac *snac, const char *type, const char *utype, const char *actor, const xs_dict *msg)
  630. /* notifies the user of relevant events */
  631. {
  632. /* skip our own notifications */
  633. if (strcmp(snac->actor, actor) == 0)
  634. return;
  635. const char *id = xs_dict_get(msg, "id");
  636. if (strcmp(type, "Create") == 0) {
  637. /* only notify of notes specifically for us */
  638. xs *rcpts = recipient_list(snac, msg, 0);
  639. if (xs_list_in(rcpts, snac->actor) == -1)
  640. return;
  641. /* discard votes */
  642. const xs_dict *note = xs_dict_get(msg, "object");
  643. if (note && !xs_is_null(xs_dict_get(note, "name")))
  644. return;
  645. }
  646. if (strcmp(type, "Undo") == 0 && strcmp(utype, "Follow") != 0)
  647. return;
  648. /* get the object id */
  649. const char *objid = xs_dict_get(msg, "object");
  650. if (xs_type(objid) == XSTYPE_DICT)
  651. objid = xs_dict_get(objid, "id");
  652. if (xs_match(type, "Like|Announce|EmojiReact")) {
  653. /* if it's not an admiration about something by us, done */
  654. if (xs_is_null(objid) || !xs_startswith(objid, snac->actor))
  655. return;
  656. }
  657. /* updated poll? */
  658. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0) {
  659. const xs_dict *poll;
  660. const char *poll_id;
  661. if ((poll = xs_dict_get(msg, "object")) == NULL)
  662. return;
  663. /* if it's not closed, discard */
  664. if (xs_is_null(xs_dict_get(poll, "closed")))
  665. return;
  666. if ((poll_id = xs_dict_get(poll, "id")) == NULL)
  667. return;
  668. /* if it's not ours and we didn't vote, discard */
  669. if (!xs_startswith(poll_id, snac->actor) && !was_question_voted(snac, poll_id))
  670. return;
  671. }
  672. /* user will love to know about this! */
  673. /* prepare message body */
  674. xs *body = xs_fmt("User : @%s@%s\n",
  675. xs_dict_get(snac->config, "uid"),
  676. xs_dict_get(srv_config, "host")
  677. );
  678. if (strcmp(utype, "(null)") != 0) {
  679. xs *s1 = xs_fmt("Type : %s + %s\n", type, utype);
  680. body = xs_str_cat(body, s1);
  681. }
  682. else {
  683. xs *s1 = xs_fmt("Type : %s\n", type);
  684. body = xs_str_cat(body, s1);
  685. }
  686. {
  687. xs *s1 = xs_fmt("Actor : %s\n", actor);
  688. body = xs_str_cat(body, s1);
  689. }
  690. if (objid != NULL) {
  691. xs *s1 = xs_fmt("Object: %s\n", objid);
  692. body = xs_str_cat(body, s1);
  693. }
  694. /* email */
  695. const char *email = "[disabled by admin]";
  696. if (xs_type(xs_dict_get(srv_config, "disable_email_notifications")) != XSTYPE_TRUE) {
  697. email = xs_dict_get(snac->config_o, "email");
  698. if (xs_is_null(email)) {
  699. email = xs_dict_get(snac->config, "email");
  700. if (xs_is_null(email))
  701. email = "[empty]";
  702. }
  703. }
  704. if (*email != '\0' && *email != '[') {
  705. snac_debug(snac, 1, xs_fmt("email notify %s %s %s", type, utype, actor));
  706. xs *subject = xs_fmt("snac notify for @%s@%s",
  707. xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
  708. xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host"));
  709. xs *header = xs_fmt(
  710. "From: %s\n"
  711. "To: %s\n"
  712. "Subject: %s\n"
  713. "\n",
  714. from, email, subject);
  715. xs *email_body = xs_fmt("%s%s", header, body);
  716. enqueue_email(email_body, 0);
  717. }
  718. /* telegram */
  719. const char *bot = xs_dict_get(snac->config, "telegram_bot");
  720. const char *chat_id = xs_dict_get(snac->config, "telegram_chat_id");
  721. if (!xs_is_null(bot) && !xs_is_null(chat_id) && *bot && *chat_id)
  722. enqueue_telegram(body, bot, chat_id);
  723. /* ntfy */
  724. const char *ntfy_server = xs_dict_get(snac->config, "ntfy_server");
  725. const char *ntfy_token = xs_dict_get(snac->config, "ntfy_token");
  726. if (!xs_is_null(ntfy_server) && *ntfy_server)
  727. enqueue_ntfy(body, ntfy_server, ntfy_token);
  728. /* auto boost */
  729. if (xs_match(type, "Create") && xs_is_true(xs_dict_get(snac->config, "auto_boost"))) {
  730. xs *msg = msg_admiration(snac, objid, "Announce");
  731. enqueue_message(snac, msg);
  732. snac_debug(snac, 1, xs_fmt("auto boosted %s", objid));
  733. }
  734. /* finally, store it in the notification folder */
  735. if (strcmp(type, "Follow") == 0)
  736. objid = id;
  737. else
  738. if (strcmp(utype, "Follow") == 0)
  739. objid = actor;
  740. notify_add(snac, type, utype, actor, objid != NULL ? objid : id, msg);
  741. }
  742. /** messages **/
  743. xs_dict *msg_base(snac *snac, const char *type, const char *id,
  744. const char *actor, const char *date, const char *object)
  745. /* creates a base ActivityPub message */
  746. {
  747. xs *did = NULL;
  748. xs *published = NULL;
  749. xs *ntid = tid(0);
  750. const char *obj_id;
  751. if (xs_type(object) == XSTYPE_DICT)
  752. obj_id = xs_dict_get(object, "id");
  753. else
  754. obj_id = object;
  755. /* generated values */
  756. if (date && strcmp(date, "@now") == 0) {
  757. published = xs_str_utctime(0, ISO_DATE_SPEC);
  758. date = published;
  759. }
  760. if (id != NULL) {
  761. if (strcmp(id, "@dummy") == 0) {
  762. did = xs_fmt("%s/d/%s/%s", snac->actor, ntid, type);
  763. id = did;
  764. }
  765. else
  766. if (strcmp(id, "@object") == 0) {
  767. if (obj_id != NULL) {
  768. did = xs_fmt("%s/%s_%s", obj_id, type, ntid);
  769. id = did;
  770. }
  771. else
  772. id = NULL;
  773. }
  774. else
  775. if (strcmp(id, "@wrapper") == 0) {
  776. /* like @object, but always generate the same id */
  777. if (object != NULL) {
  778. date = xs_dict_get(object, "published");
  779. did = xs_fmt("%s/%s", obj_id, type);
  780. id = did;
  781. }
  782. else
  783. id = NULL;
  784. }
  785. }
  786. xs_dict *msg = xs_dict_new();
  787. msg = xs_dict_append(msg, "@context", "https:/" "/www.w3.org/ns/activitystreams");
  788. msg = xs_dict_append(msg, "type", type);
  789. if (id != NULL)
  790. msg = xs_dict_append(msg, "id", id);
  791. if (actor != NULL)
  792. msg = xs_dict_append(msg, "actor", actor);
  793. if (date != NULL)
  794. msg = xs_dict_append(msg, "published", date);
  795. if (object != NULL)
  796. msg = xs_dict_append(msg, "object", object);
  797. return msg;
  798. }
  799. xs_dict *msg_collection(snac *snac, const char *id)
  800. /* creates an empty OrderedCollection message */
  801. {
  802. xs_dict *msg = msg_base(snac, "OrderedCollection", id, NULL, NULL, NULL);
  803. xs *ol = xs_list_new();
  804. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  805. msg = xs_dict_append(msg, "orderedItems", ol);
  806. msg = xs_dict_append(msg, "totalItems", xs_stock(0));
  807. return msg;
  808. }
  809. xs_dict *msg_accept(snac *snac, const xs_val *object, const char *to)
  810. /* creates an Accept message (as a response to a Follow) */
  811. {
  812. xs_dict *msg = msg_base(snac, "Accept", "@dummy", snac->actor, NULL, object);
  813. msg = xs_dict_append(msg, "to", to);
  814. return msg;
  815. }
  816. xs_dict *msg_update(snac *snac, const xs_dict *object)
  817. /* creates an Update message */
  818. {
  819. xs_dict *msg = msg_base(snac, "Update", "@object", snac->actor, "@now", object);
  820. const char *type = xs_dict_get(object, "type");
  821. if (strcmp(type, "Note") == 0) {
  822. msg = xs_dict_append(msg, "to", xs_dict_get(object, "to"));
  823. msg = xs_dict_append(msg, "cc", xs_dict_get(object, "cc"));
  824. }
  825. else
  826. if (strcmp(type, "Person") == 0) {
  827. msg = xs_dict_append(msg, "to", public_address);
  828. /* also spam the people being followed, so that
  829. they have the newest information about who we are */
  830. xs *cc = following_list(snac);
  831. msg = xs_dict_append(msg, "cc", cc);
  832. }
  833. else
  834. msg = xs_dict_append(msg, "to", public_address);
  835. return msg;
  836. }
  837. xs_dict *msg_admiration(snac *snac, const char *object, const char *type)
  838. /* creates a Like or Announce message */
  839. {
  840. xs *a_msg = NULL;
  841. xs_dict *msg = NULL;
  842. xs *wrk = NULL;
  843. /* call the object */
  844. timeline_request(snac, &object, &wrk, 0);
  845. if (valid_status(object_get(object, &a_msg))) {
  846. xs *rcpts = xs_list_new();
  847. xs *o_md5 = xs_md5_hex(object, strlen(object));
  848. xs *id = xs_fmt("%s/%s/%s", snac->actor, *type == 'L' ? "l" : "a", o_md5);
  849. msg = msg_base(snac, type, id, snac->actor, "@now", object);
  850. if (is_msg_public(a_msg))
  851. rcpts = xs_list_append(rcpts, public_address);
  852. rcpts = xs_list_append(rcpts, get_atto(a_msg));
  853. msg = xs_dict_append(msg, "to", rcpts);
  854. }
  855. else
  856. snac_log(snac, xs_fmt("msg_admiration cannot retrieve object %s", object));
  857. return msg;
  858. }
  859. xs_dict *msg_repulsion(snac *user, const char *id, const char *type)
  860. /* creates an Undo + admiration message */
  861. {
  862. xs *a_msg = NULL;
  863. xs_dict *msg = NULL;
  864. if (valid_status(object_get(id, &a_msg))) {
  865. /* create a clone of the original admiration message */
  866. xs *object = msg_admiration(user, id, type);
  867. /* delete the published date */
  868. object = xs_dict_del(object, "published");
  869. /* create an undo message for this object */
  870. msg = msg_undo(user, object);
  871. /* copy the 'to' field */
  872. msg = xs_dict_set(msg, "to", xs_dict_get(object, "to"));
  873. }
  874. /* now we despise this */
  875. object_unadmire(id, user->actor, *type == 'L' ? 1 : 0);
  876. return msg;
  877. }
  878. xs_dict *msg_actor(snac *snac)
  879. /* create a Person message for this actor */
  880. {
  881. xs *ctxt = xs_list_new();
  882. xs *icon = xs_dict_new();
  883. xs *keys = xs_dict_new();
  884. xs *tags = xs_list_new();
  885. xs *avtr = NULL;
  886. xs *kid = NULL;
  887. xs *f_bio = NULL;
  888. xs_dict *msg = msg_base(snac, "Person", snac->actor, NULL, NULL, NULL);
  889. const char *p;
  890. int n;
  891. /* change the @context (is this really necessary?) */
  892. ctxt = xs_list_append(ctxt, "https:/" "/www.w3.org/ns/activitystreams");
  893. ctxt = xs_list_append(ctxt, "https:/" "/w3id.org/security/v1");
  894. msg = xs_dict_set(msg, "@context", ctxt);
  895. msg = xs_dict_set(msg, "url", snac->actor);
  896. msg = xs_dict_set(msg, "name", xs_dict_get(snac->config, "name"));
  897. msg = xs_dict_set(msg, "preferredUsername", snac->uid);
  898. msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
  899. xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
  900. f_bio = process_tags(snac, f_bio_2, &tags);
  901. msg = xs_dict_set(msg, "summary", f_bio);
  902. msg = xs_dict_set(msg, "tag", tags);
  903. char *folders[] = { "inbox", "outbox", "followers", "following", "featured", NULL };
  904. for (n = 0; folders[n]; n++) {
  905. xs *f = xs_fmt("%s/%s", snac->actor, folders[n]);
  906. msg = xs_dict_set(msg, folders[n], f);
  907. }
  908. p = xs_dict_get(snac->config, "avatar");
  909. if (*p == '\0')
  910. avtr = xs_fmt("%s/susie.png", srv_baseurl);
  911. else
  912. avtr = xs_dup(p);
  913. icon = xs_dict_append(icon, "type", "Image");
  914. icon = xs_dict_append(icon, "mediaType", xs_mime_by_ext(avtr));
  915. icon = xs_dict_append(icon, "url", avtr);
  916. msg = xs_dict_set(msg, "icon", icon);
  917. kid = xs_fmt("%s#main-key", snac->actor);
  918. keys = xs_dict_append(keys, "id", kid);
  919. keys = xs_dict_append(keys, "owner", snac->actor);
  920. keys = xs_dict_append(keys, "publicKeyPem", xs_dict_get(snac->key, "public"));
  921. msg = xs_dict_set(msg, "publicKey", keys);
  922. /* if the "bot" config field is set to true, change type to "Service" */
  923. if (xs_type(xs_dict_get(snac->config, "bot")) == XSTYPE_TRUE)
  924. msg = xs_dict_set(msg, "type", "Service");
  925. /* add the header image, if there is one defined */
  926. const char *header = xs_dict_get(snac->config, "header");
  927. if (!xs_is_null(header)) {
  928. xs *d = xs_dict_new();
  929. d = xs_dict_append(d, "type", "Image");
  930. d = xs_dict_append(d, "mediaType", xs_mime_by_ext(header));
  931. d = xs_dict_append(d, "url", header);
  932. msg = xs_dict_set(msg, "image", d);
  933. }
  934. /* add the metadata as attachments of PropertyValue */
  935. const xs_dict *metadata = xs_dict_get(snac->config, "metadata");
  936. if (xs_type(metadata) == XSTYPE_DICT) {
  937. xs *attach = xs_list_new();
  938. const xs_str *k;
  939. const xs_str *v;
  940. int c = 0;
  941. while (xs_dict_next(metadata, &k, &v, &c)) {
  942. xs *d = xs_dict_new();
  943. xs *k2 = encode_html(k);
  944. xs *v2 = NULL;
  945. if (xs_startswith(v, "https:/") || xs_startswith(v, "http:/")) {
  946. xs *t = encode_html(v);
  947. v2 = xs_fmt("<a href=\"%s\" rel=\"me\">%s</a>", t, t);
  948. }
  949. else
  950. v2 = encode_html(v);
  951. d = xs_dict_append(d, "type", "PropertyValue");
  952. d = xs_dict_append(d, "name", k2);
  953. d = xs_dict_append(d, "value", v2);
  954. attach = xs_list_append(attach, d);
  955. }
  956. msg = xs_dict_set(msg, "attachment", attach);
  957. }
  958. /* use shared inboxes? */
  959. if (xs_type(xs_dict_get(srv_config, "shared_inboxes")) == XSTYPE_TRUE) {
  960. xs *d = xs_dict_new();
  961. xs *si = xs_fmt("%s/shared-inbox", srv_baseurl);
  962. d = xs_dict_append(d, "sharedInbox", si);
  963. msg = xs_dict_set(msg, "endpoints", d);
  964. }
  965. /* does this user have an aka? */
  966. const char *aka = xs_dict_get(snac->config, "alias");
  967. if (xs_type(aka) == XSTYPE_STRING && *aka) {
  968. xs *loaka = xs_list_append(xs_list_new(), aka);
  969. msg = xs_dict_set(msg, "alsoKnownAs", loaka);
  970. }
  971. return msg;
  972. }
  973. xs_dict *msg_create(snac *snac, const xs_dict *object)
  974. /* creates a 'Create' message */
  975. {
  976. xs_dict *msg = msg_base(snac, "Create", "@wrapper", snac->actor, NULL, object);
  977. const xs_val *v;
  978. if ((v = get_atto(object)))
  979. msg = xs_dict_append(msg, "attributedTo", v);
  980. if ((v = xs_dict_get(object, "cc")))
  981. msg = xs_dict_append(msg, "cc", v);
  982. if ((v = xs_dict_get(object, "to")))
  983. msg = xs_dict_append(msg, "to", v);
  984. else
  985. msg = xs_dict_append(msg, "to", public_address);
  986. return msg;
  987. }
  988. xs_dict *msg_undo(snac *snac, const xs_val *object)
  989. /* creates an 'Undo' message */
  990. {
  991. xs_dict *msg = msg_base(snac, "Undo", "@object", snac->actor, "@now", object);
  992. const char *to;
  993. if (xs_type(object) == XSTYPE_DICT && (to = xs_dict_get(object, "object")))
  994. msg = xs_dict_append(msg, "to", to);
  995. return msg;
  996. }
  997. xs_dict *msg_delete(snac *snac, const char *id)
  998. /* creates a 'Delete' + 'Tombstone' for a local entry */
  999. {
  1000. xs *tomb = xs_dict_new();
  1001. xs_dict *msg = NULL;
  1002. /* sculpt the tombstone */
  1003. tomb = xs_dict_append(tomb, "type", "Tombstone");
  1004. tomb = xs_dict_append(tomb, "id", id);
  1005. /* now create the Delete */
  1006. msg = msg_base(snac, "Delete", "@object", snac->actor, "@now", tomb);
  1007. msg = xs_dict_append(msg, "to", public_address);
  1008. return msg;
  1009. }
  1010. xs_dict *msg_follow(snac *snac, const char *q)
  1011. /* creates a 'Follow' message */
  1012. {
  1013. xs *actor_o = NULL;
  1014. xs *actor = NULL;
  1015. xs_dict *msg = NULL;
  1016. int status;
  1017. xs *url_or_uid = xs_strip_i(xs_str_new(q));
  1018. if (xs_startswith(url_or_uid, "https:/") || xs_startswith(url_or_uid, "http:/"))
  1019. actor = xs_dup(url_or_uid);
  1020. else
  1021. if (!valid_status(webfinger_request(url_or_uid, &actor, NULL)) || actor == NULL) {
  1022. snac_log(snac, xs_fmt("cannot resolve user %s to follow", url_or_uid));
  1023. return NULL;
  1024. }
  1025. /* request the actor */
  1026. status = actor_request(snac, actor, &actor_o);
  1027. if (valid_status(status)) {
  1028. /* check if the actor is an alias */
  1029. const char *r_actor = xs_dict_get(actor_o, "id");
  1030. if (r_actor && strcmp(actor, r_actor) != 0) {
  1031. snac_log(snac, xs_fmt("actor to follow is an alias %s -> %s", actor, r_actor));
  1032. }
  1033. msg = msg_base(snac, "Follow", "@dummy", snac->actor, NULL, r_actor);
  1034. }
  1035. else
  1036. snac_log(snac, xs_fmt("cannot get actor to follow %s %d", actor, status));
  1037. return msg;
  1038. }
  1039. xs_dict *msg_note(snac *snac, const xs_str *content, const xs_val *rcpts,
  1040. const xs_str *in_reply_to, const xs_list *attach, int priv)
  1041. /* creates a 'Note' message */
  1042. {
  1043. xs *ntid = tid(0);
  1044. xs *id = xs_fmt("%s/p/%s", snac->actor, ntid);
  1045. xs *ctxt = NULL;
  1046. xs *fc2 = NULL;
  1047. xs *fc1 = NULL;
  1048. xs *to = NULL;
  1049. xs *cc = xs_list_new();
  1050. xs *irt = NULL;
  1051. xs *tag = xs_list_new();
  1052. xs *atls = xs_list_new();
  1053. xs_dict *msg = msg_base(snac, "Note", id, NULL, "@now", NULL);
  1054. xs_list *p;
  1055. const xs_val *v;
  1056. if (rcpts == NULL)
  1057. to = xs_list_new();
  1058. else {
  1059. if (xs_type(rcpts) == XSTYPE_STRING) {
  1060. to = xs_list_new();
  1061. to = xs_list_append(to, rcpts);
  1062. }
  1063. else
  1064. to = xs_dup(rcpts);
  1065. }
  1066. /* format the content */
  1067. fc2 = not_really_markdown(content, &atls, &tag);
  1068. if (in_reply_to != NULL && *in_reply_to) {
  1069. xs *p_msg = NULL;
  1070. xs *wrk = NULL;
  1071. /* demand this thing */
  1072. timeline_request(snac, &in_reply_to, &wrk, 0);
  1073. if (valid_status(object_get(in_reply_to, &p_msg))) {
  1074. /* add this author as recipient */
  1075. const char *a, *v;
  1076. if ((a = get_atto(p_msg)) && xs_list_in(to, a) == -1)
  1077. to = xs_list_append(to, a);
  1078. /* add this author to the tag list as a mention */
  1079. if (!xs_is_null(a)) {
  1080. xs *l = xs_split(a, "/");
  1081. xs *actor_o = NULL;
  1082. if (xs_list_len(l) > 3 && valid_status(object_get(a, &actor_o))) {
  1083. const char *uname = xs_dict_get(actor_o, "preferredUsername");
  1084. if (!xs_is_null(uname) && *uname) {
  1085. xs *handle = xs_fmt("@%s@%s", uname, xs_list_get(l, 2));
  1086. xs *t = xs_dict_new();
  1087. t = xs_dict_append(t, "type", "Mention");
  1088. t = xs_dict_append(t, "href", a);
  1089. t = xs_dict_append(t, "name", handle);
  1090. tag = xs_list_append(tag, t);
  1091. }
  1092. }
  1093. }
  1094. /* get the context, if there is one */
  1095. if ((v = xs_dict_get(p_msg, "context")))
  1096. ctxt = xs_dup(v);
  1097. /* propagate the conversation field, if there is one */
  1098. if ((v = xs_dict_get(p_msg, "conversation")))
  1099. msg = xs_dict_append(msg, "conversation", v);
  1100. /* if this message is public, ours will also be */
  1101. if (!priv && is_msg_public(p_msg) && xs_list_in(to, public_address) == -1)
  1102. to = xs_list_append(to, public_address);
  1103. }
  1104. irt = xs_dup(in_reply_to);
  1105. }
  1106. else
  1107. irt = xs_val_new(XSTYPE_NULL);
  1108. /* extract the mentions and hashtags and convert the content */
  1109. fc1 = process_tags(snac, fc2, &tag);
  1110. /* create the attachment list, if there are any */
  1111. if (!xs_is_null(attach)) {
  1112. int c = 0;
  1113. while (xs_list_next(attach, &v, &c)) {
  1114. xs *d = xs_dict_new();
  1115. const char *url = xs_list_get(v, 0);
  1116. const char *alt = xs_list_get(v, 1);
  1117. const char *mime = xs_mime_by_ext(url);
  1118. d = xs_dict_append(d, "mediaType", mime);
  1119. d = xs_dict_append(d, "url", url);
  1120. d = xs_dict_append(d, "name", alt);
  1121. d = xs_dict_append(d, "type",
  1122. xs_startswith(mime, "image/") ? "Image" : "Document");
  1123. atls = xs_list_append(atls, d);
  1124. }
  1125. }
  1126. if (ctxt == NULL)
  1127. ctxt = xs_fmt("%s#ctxt", id);
  1128. /* add all mentions to the cc */
  1129. p = tag;
  1130. while (xs_list_iter(&p, &v)) {
  1131. if (xs_type(v) == XSTYPE_DICT) {
  1132. const char *t;
  1133. if (!xs_is_null(t = xs_dict_get(v, "type")) && strcmp(t, "Mention") == 0) {
  1134. if (!xs_is_null(t = xs_dict_get(v, "href")))
  1135. cc = xs_list_append(cc, t);
  1136. }
  1137. }
  1138. }
  1139. /* no recipients? must be for everybody */
  1140. if (!priv && xs_list_len(to) == 0)
  1141. to = xs_list_append(to, public_address);
  1142. /* delete all cc recipients that also are in the to */
  1143. p = to;
  1144. while (xs_list_iter(&p, &v)) {
  1145. int i;
  1146. if ((i = xs_list_in(cc, v)) != -1)
  1147. cc = xs_list_del(cc, i);
  1148. }
  1149. msg = xs_dict_append(msg, "attributedTo", snac->actor);
  1150. msg = xs_dict_append(msg, "summary", "");
  1151. msg = xs_dict_append(msg, "content", fc1);
  1152. msg = xs_dict_append(msg, "context", ctxt);
  1153. msg = xs_dict_append(msg, "url", id);
  1154. msg = xs_dict_append(msg, "to", to);
  1155. msg = xs_dict_append(msg, "cc", cc);
  1156. msg = xs_dict_append(msg, "inReplyTo", irt);
  1157. msg = xs_dict_append(msg, "tag", tag);
  1158. msg = xs_dict_append(msg, "sourceContent", content);
  1159. if (xs_list_len(atls))
  1160. msg = xs_dict_append(msg, "attachment", atls);
  1161. return msg;
  1162. }
  1163. xs_dict *msg_ping(snac *user, const char *rcpt)
  1164. /* creates a Ping message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1165. {
  1166. xs_dict *msg = msg_base(user, "Ping", "@dummy", user->actor, NULL, NULL);
  1167. msg = xs_dict_append(msg, "to", rcpt);
  1168. return msg;
  1169. }
  1170. xs_dict *msg_pong(snac *user, const char *rcpt, const char *object)
  1171. /* creates a Pong message (https://humungus.tedunangst.com/r/honk/v/tip/f/docs/ping.txt) */
  1172. {
  1173. xs_dict *msg = msg_base(user, "Pong", "@dummy", user->actor, NULL, object);
  1174. msg = xs_dict_append(msg, "to", rcpt);
  1175. return msg;
  1176. }
  1177. xs_dict *msg_move(snac *user, const char *new_account)
  1178. /* creates a Move message (to move the user to new_account) */
  1179. {
  1180. xs_dict *msg = msg_base(user, "Move", "@dummy", user->actor, NULL, user->actor);
  1181. msg = xs_dict_append(msg, "target", new_account);
  1182. return msg;
  1183. }
  1184. xs_dict *msg_question(snac *user, const char *content, xs_list *attach,
  1185. const xs_list *opts, int multiple, int end_secs)
  1186. /* creates a Question message */
  1187. {
  1188. xs_dict *msg = msg_note(user, content, NULL, NULL, attach, 0);
  1189. int max = 8;
  1190. xs_set seen;
  1191. msg = xs_dict_set(msg, "type", "Question");
  1192. /* make it non-editable */
  1193. msg = xs_dict_del(msg, "sourceContent");
  1194. xs *o = xs_list_new();
  1195. xs_list *p = (xs_list *)opts;
  1196. const xs_str *v;
  1197. xs *replies = xs_json_loads("{\"type\":\"Collection\",\"totalItems\":0}");
  1198. xs_set_init(&seen);
  1199. while (max && xs_list_iter(&p, &v)) {
  1200. if (*v) {
  1201. xs *v2 = xs_dup(v);
  1202. xs *d = xs_dict_new();
  1203. if (strlen(v2) > 60) {
  1204. v2[60] = '\0';
  1205. v2 = xs_str_cat(v2, "...");
  1206. }
  1207. if (xs_set_add(&seen, v2) == 1) {
  1208. d = xs_dict_append(d, "type", "Note");
  1209. d = xs_dict_append(d, "name", v2);
  1210. d = xs_dict_append(d, "replies", replies);
  1211. o = xs_list_append(o, d);
  1212. max--;
  1213. }
  1214. }
  1215. }
  1216. xs_set_free(&seen);
  1217. msg = xs_dict_append(msg, multiple ? "anyOf" : "oneOf", o);
  1218. /* set the end time */
  1219. time_t t = time(NULL) + end_secs;
  1220. xs *et = xs_str_utctime(t, ISO_DATE_SPEC);
  1221. msg = xs_dict_append(msg, "endTime", et);
  1222. return msg;
  1223. }
  1224. int update_question(snac *user, const char *id)
  1225. /* updates the poll counts */
  1226. {
  1227. xs *msg = NULL;
  1228. xs *rcnt = xs_dict_new();
  1229. xs *lopts = xs_list_new();
  1230. const xs_list *opts;
  1231. xs_list *p;
  1232. const xs_val *v;
  1233. /* get the object */
  1234. if (!valid_status(object_get(id, &msg)))
  1235. return -1;
  1236. /* closed? do nothing more */
  1237. if (xs_dict_get(msg, "closed"))
  1238. return -2;
  1239. /* get the options */
  1240. if ((opts = xs_dict_get(msg, "oneOf")) == NULL &&
  1241. (opts = xs_dict_get(msg, "anyOf")) == NULL)
  1242. return -3;
  1243. /* fill the initial count */
  1244. int c = 0;
  1245. while (xs_list_next(opts, &v, &c)) {
  1246. const char *name = xs_dict_get(v, "name");
  1247. if (name) {
  1248. lopts = xs_list_append(lopts, name);
  1249. rcnt = xs_dict_set(rcnt, name, xs_stock(0));
  1250. }
  1251. }
  1252. xs_set s;
  1253. xs_set_init(&s);
  1254. /* iterate now the children (the votes) */
  1255. xs *chld = object_children(id);
  1256. p = chld;
  1257. while (xs_list_iter(&p, &v)) {
  1258. xs *obj = NULL;
  1259. if (!valid_status(object_get_by_md5(v, &obj)))
  1260. continue;
  1261. const char *name = xs_dict_get(obj, "name");
  1262. const char *atto = get_atto(obj);
  1263. if (name && atto) {
  1264. /* get the current count */
  1265. const xs_number *cnt = xs_dict_get(rcnt, name);
  1266. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1267. /* if it exists, increment */
  1268. xs *ucnt = xs_number_new(xs_number_get(cnt) + 1);
  1269. rcnt = xs_dict_set(rcnt, name, ucnt);
  1270. xs_set_add(&s, atto);
  1271. }
  1272. }
  1273. }
  1274. xs *rcpts = xs_set_result(&s);
  1275. /* create a new list of options with their new counts */
  1276. xs *nopts = xs_list_new();
  1277. p = lopts;
  1278. while (xs_list_iter(&p, &v)) {
  1279. const xs_number *cnt = xs_dict_get(rcnt, v);
  1280. if (xs_type(cnt) == XSTYPE_NUMBER) {
  1281. xs *d1 = xs_dict_new();
  1282. xs *d2 = xs_dict_new();
  1283. d2 = xs_dict_append(d2, "type", "Collection");
  1284. d2 = xs_dict_append(d2, "totalItems", cnt);
  1285. d1 = xs_dict_append(d1, "type", "Note");
  1286. d1 = xs_dict_append(d1, "name", v);
  1287. d1 = xs_dict_append(d1, "replies", d2);
  1288. nopts = xs_list_append(nopts, d1);
  1289. }
  1290. }
  1291. /* update the list */
  1292. msg = xs_dict_set(msg, xs_dict_get(msg, "oneOf") != NULL ? "oneOf" : "anyOf", nopts);
  1293. /* due date? */
  1294. int closed = 0;
  1295. const char *end_time = xs_dict_get(msg, "endTime");
  1296. if (!xs_is_null(end_time)) {
  1297. xs *now = xs_str_utctime(0, ISO_DATE_SPEC);
  1298. /* is now greater than the endTime? */
  1299. if (strcmp(now, end_time) >= 0) {
  1300. xs *et = xs_dup(end_time);
  1301. msg = xs_dict_set(msg, "closed", et);
  1302. closed = 1;
  1303. }
  1304. }
  1305. /* update the count of voters */
  1306. xs *vcnt = xs_number_new(xs_list_len(rcpts));
  1307. msg = xs_dict_set(msg, "votersCount", vcnt);
  1308. msg = xs_dict_set(msg, "cc", rcpts);
  1309. /* store */
  1310. object_add_ow(id, msg);
  1311. snac_debug(user, 1, xs_fmt("recounted poll %s", id));
  1312. timeline_touch(user);
  1313. /* send an update message to all voters */
  1314. xs *u_msg = msg_update(user, msg);
  1315. u_msg = xs_dict_set(u_msg, "cc", rcpts);
  1316. enqueue_message(user, u_msg);
  1317. if (closed) {
  1318. xs *c_msg = msg_update(user, msg);
  1319. notify(user, "Update", "Question", user->actor, c_msg);
  1320. }
  1321. return 0;
  1322. }
  1323. /** queues **/
  1324. int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
  1325. /* processes an ActivityPub message from the input queue */
  1326. /* return values: -1, fatal error; 0, transient error, retry;
  1327. 1, processed and done; 2, propagate to users (only when no user is set) */
  1328. {
  1329. const char *actor = xs_dict_get(msg, "actor");
  1330. const char *type = xs_dict_get(msg, "type");
  1331. xs *actor_o = NULL;
  1332. int a_status;
  1333. int do_notify = 0;
  1334. if (xs_is_null(actor) || *actor == '\0') {
  1335. srv_debug(0, xs_fmt("malformed message (bad actor)"));
  1336. return -1;
  1337. }
  1338. /* question votes may not have a type */
  1339. if (xs_is_null(type))
  1340. type = "Note";
  1341. /* reject uninteresting messages right now */
  1342. if (xs_match(type, "Add|View|Reject|Read|Remove")) {
  1343. srv_debug(0, xs_fmt("Ignored message of type '%s'", type));
  1344. return -1;
  1345. }
  1346. const char *object, *utype;
  1347. object = xs_dict_get(msg, "object");
  1348. if (object != NULL && xs_type(object) == XSTYPE_DICT)
  1349. utype = xs_dict_get(object, "type");
  1350. else
  1351. utype = "(null)";
  1352. /* special case for Delete messages */
  1353. if (strcmp(type, "Delete") == 0) {
  1354. /* if the actor is not here, do not even try */
  1355. if (!object_here(actor)) {
  1356. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown actor '%s'", actor));
  1357. return -1;
  1358. }
  1359. /* discard crap */
  1360. if (xs_is_null(object)) {
  1361. srv_log(xs_fmt("dropped 'Delete' message with invalid object from actor '%s'", actor));
  1362. return -1;
  1363. }
  1364. /* also discard if the object to be deleted is not here */
  1365. const char *obj_id = object;
  1366. if (xs_type(obj_id) == XSTYPE_DICT)
  1367. obj_id = xs_dict_get(obj_id, "id");
  1368. if (xs_is_null(obj_id) || !object_here(obj_id)) {
  1369. srv_debug(1, xs_fmt("dropped 'Delete' message from unknown object '%s'", obj_id));
  1370. return -1;
  1371. }
  1372. }
  1373. /* bring the actor */
  1374. a_status = actor_request(snac, actor, &actor_o);
  1375. /* do not retry permanent failures */
  1376. if (a_status == HTTP_STATUS_NOT_FOUND
  1377. || a_status == HTTP_STATUS_GONE
  1378. || a_status < 0) {
  1379. srv_debug(1, xs_fmt("dropping message due to actor error %s %d", actor, a_status));
  1380. return -1;
  1381. }
  1382. if (!valid_status(a_status)) {
  1383. /* do not retry 'Delete' messages */
  1384. if (strcmp(type, "Delete") == 0) {
  1385. srv_debug(1, xs_fmt("dropping 'Delete' message due to actor error %s %d", actor, a_status));
  1386. return -1;
  1387. }
  1388. /* other actor download errors */
  1389. /* the actor may require a signed request; propagate if no user is set */
  1390. if (snac == NULL)
  1391. return 2;
  1392. /* may need a retry */
  1393. srv_debug(0, xs_fmt("error requesting actor %s %d -- retry later", actor, a_status));
  1394. return 0;
  1395. }
  1396. /* check the signature */
  1397. xs *sig_err = NULL;
  1398. if (!check_signature(req, &sig_err)) {
  1399. srv_log(xs_fmt("bad signature %s (%s)", actor, sig_err));
  1400. srv_archive_error("check_signature", sig_err, req, msg);
  1401. return -1;
  1402. }
  1403. /* if no user is set, no further checks can be done; propagate */
  1404. if (snac == NULL)
  1405. return 2;
  1406. /* reject messages that are not for this user */
  1407. if (!is_msg_for_me(snac, msg)) {
  1408. snac_debug(snac, 1, xs_fmt("message from %s of type '%s' not for us", actor, type));
  1409. return 1;
  1410. }
  1411. /* if it's a DM from someone we don't follow, reject the message */
  1412. if (xs_type(xs_dict_get(snac->config, "drop_dm_from_unknown")) == XSTYPE_TRUE) {
  1413. if (strcmp(utype, "Note") == 0 && !is_msg_public(msg) &&
  1414. !following_check(snac, actor)) {
  1415. snac_log(snac, xs_fmt("DM rejected from unknown actor %s", actor));
  1416. return 1;
  1417. }
  1418. }
  1419. /* check the minimum acceptable account age */
  1420. int min_account_age = xs_number_get(xs_dict_get(srv_config, "min_account_age"));
  1421. if (min_account_age > 0) {
  1422. const char *actor_date = xs_dict_get(actor_o, "published");
  1423. if (!xs_is_null(actor_date)) {
  1424. time_t actor_t = xs_parse_iso_date(actor_date, 0);
  1425. if (actor_t < 950000000) {
  1426. snac_log(snac, xs_fmt("rejected activity from %s (suspicious date, %s)",
  1427. actor, actor_date));
  1428. return 1;
  1429. }
  1430. if (actor_t > 0) {
  1431. int td = (int)(time(NULL) - actor_t);
  1432. snac_debug(snac, 2, xs_fmt("actor %s age: %d seconds", actor, td));
  1433. if (td < min_account_age) {
  1434. snac_log(snac, xs_fmt("rejected activity from %s (too new, %d seconds)",
  1435. actor, td));
  1436. return 1;
  1437. }
  1438. }
  1439. }
  1440. else
  1441. snac_debug(snac, 1, xs_fmt("warning: empty or null creation date for %s", actor));
  1442. }
  1443. if (strcmp(type, "Follow") == 0) { /** **/
  1444. const char *id = xs_dict_get(msg, "id");
  1445. if (xs_is_null(id)) {
  1446. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1447. }
  1448. else
  1449. if (!follower_check(snac, actor)) {
  1450. /* ensure the actor object is here */
  1451. if (!object_here(actor)) {
  1452. xs *actor_obj = NULL;
  1453. actor_request(snac, actor, &actor_obj);
  1454. object_add(actor, actor_obj);
  1455. }
  1456. xs *f_msg = xs_dup(msg);
  1457. xs *reply = msg_accept(snac, f_msg, actor);
  1458. post_message(snac, actor, reply);
  1459. if (xs_is_null(xs_dict_get(f_msg, "published"))) {
  1460. /* add a date if it doesn't include one (Mastodon) */
  1461. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1462. f_msg = xs_dict_set(f_msg, "published", date);
  1463. }
  1464. timeline_add(snac, id, f_msg);
  1465. follower_add(snac, actor);
  1466. snac_log(snac, xs_fmt("new follower %s", actor));
  1467. do_notify = 1;
  1468. }
  1469. else
  1470. snac_log(snac, xs_fmt("repeated 'Follow' from %s", actor));
  1471. }
  1472. else
  1473. if (strcmp(type, "Undo") == 0) { /** **/
  1474. const char *id = xs_dict_get(object, "object");
  1475. if (xs_type(object) != XSTYPE_DICT)
  1476. utype = "Follow";
  1477. if (strcmp(utype, "Follow") == 0) { /** **/
  1478. if (id && strcmp(id, snac->actor) != 0)
  1479. snac_debug(snac, 1, xs_fmt("Undo + Follow from %s not for us (%s)", actor, id));
  1480. else {
  1481. if (valid_status(follower_del(snac, actor))) {
  1482. snac_log(snac, xs_fmt("no longer following us %s", actor));
  1483. do_notify = 1;
  1484. }
  1485. else
  1486. snac_log(snac, xs_fmt("error deleting follower %s", actor));
  1487. }
  1488. }
  1489. else
  1490. if (strcmp(utype, "Like") == 0 || strcmp(utype, "EmojiReact") == 0) { /** **/
  1491. int status = object_unadmire(id, actor, 1);
  1492. snac_log(snac, xs_fmt("Undo '%s' for %s %d", utype, id, status));
  1493. }
  1494. else
  1495. if (strcmp(utype, "Announce") == 0) { /** **/
  1496. int status = HTTP_STATUS_OK;
  1497. /* commented out: if a followed user boosts something that
  1498. is requested and then unboosts, the post remains here,
  1499. but with no apparent reason, and that is confusing */
  1500. //status = object_unadmire(id, actor, 0);
  1501. snac_log(snac, xs_fmt("Unboost for %s %d", id, status));
  1502. }
  1503. else
  1504. snac_debug(snac, 1, xs_fmt("ignored 'Undo' for object type '%s'", utype));
  1505. }
  1506. else
  1507. if (strcmp(type, "Create") == 0) { /** **/
  1508. if (is_muted(snac, actor)) {
  1509. snac_log(snac, xs_fmt("ignored 'Create' + '%s' from muted actor %s", utype, actor));
  1510. return 1;
  1511. }
  1512. if (xs_match(utype, "Note|Article")) { /** **/
  1513. const char *id = xs_dict_get(object, "id");
  1514. const char *in_reply_to = xs_dict_get(object, "inReplyTo");
  1515. const char *atto = get_atto(object);
  1516. xs *wrk = NULL;
  1517. if (xs_is_null(id))
  1518. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1519. else
  1520. if (xs_is_null(atto))
  1521. snac_log(snac, xs_fmt("malformed message: no 'attributedTo' field"));
  1522. else
  1523. if (!xs_is_null(in_reply_to) && is_hidden(snac, in_reply_to)) {
  1524. snac_debug(snac, 0, xs_fmt("dropped reply %s to hidden post %s", id, in_reply_to));
  1525. }
  1526. else {
  1527. if (content_match("filter_reject.txt", object)) {
  1528. snac_log(snac, xs_fmt("rejected by content %s", id));
  1529. return 1;
  1530. }
  1531. if (strcmp(actor, atto) != 0)
  1532. snac_log(snac, xs_fmt("SUSPICIOUS: actor != atto (%s != %s)", actor, atto));
  1533. timeline_request(snac, &in_reply_to, &wrk, 0);
  1534. if (timeline_add(snac, id, object)) {
  1535. snac_log(snac, xs_fmt("new '%s' %s %s", utype, actor, id));
  1536. do_notify = 1;
  1537. }
  1538. /* if it has a "name" field, it may be a vote for a question */
  1539. const char *name = xs_dict_get(object, "name");
  1540. if (!xs_is_null(name) && *name && !xs_is_null(in_reply_to) && *in_reply_to)
  1541. update_question(snac, in_reply_to);
  1542. }
  1543. }
  1544. else
  1545. if (strcmp(utype, "Question") == 0) { /** **/
  1546. const char *id = xs_dict_get(object, "id");
  1547. if (xs_is_null(id))
  1548. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1549. else
  1550. if (timeline_add(snac, id, object))
  1551. snac_log(snac, xs_fmt("new 'Question' %s %s", actor, id));
  1552. }
  1553. else
  1554. if (strcmp(utype, "Video") == 0) { /** **/
  1555. const char *id = xs_dict_get(object, "id");
  1556. if (xs_is_null(id))
  1557. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1558. else
  1559. if (timeline_add(snac, id, object))
  1560. snac_log(snac, xs_fmt("new 'Video' %s %s", actor, id));
  1561. }
  1562. else
  1563. snac_debug(snac, 1, xs_fmt("ignored 'Create' for object type '%s'", utype));
  1564. }
  1565. else
  1566. if (strcmp(type, "Accept") == 0) { /** **/
  1567. if (strcmp(utype, "(null)") == 0) {
  1568. const char *obj_id = xs_dict_get(msg, "object");
  1569. /* if the accepted object id is a string that may
  1570. be created by us, it's a follow */
  1571. if (xs_type(obj_id) == XSTYPE_STRING &&
  1572. xs_startswith(obj_id, srv_baseurl) &&
  1573. xs_endswith(obj_id, "/Follow"))
  1574. utype = "Follow";
  1575. }
  1576. if (strcmp(utype, "Follow") == 0) { /** **/
  1577. if (following_check(snac, actor)) {
  1578. following_add(snac, actor, msg);
  1579. snac_log(snac, xs_fmt("confirmed follow from %s", actor));
  1580. }
  1581. else
  1582. snac_log(snac, xs_fmt("spurious follow accept from %s", actor));
  1583. }
  1584. else
  1585. if (strcmp(utype, "Create") == 0) {
  1586. /* some implementations send Create confirmations, go figure */
  1587. snac_debug(snac, 1, xs_dup("ignored 'Accept' + 'Create'"));
  1588. }
  1589. else {
  1590. srv_archive_error("accept", "ignored Accept", req, msg);
  1591. snac_debug(snac, 1, xs_fmt("ignored 'Accept' for object type '%s'", utype));
  1592. }
  1593. }
  1594. else
  1595. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0) { /** **/
  1596. if (xs_type(object) == XSTYPE_DICT)
  1597. object = xs_dict_get(object, "id");
  1598. if (xs_is_null(object))
  1599. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1600. else
  1601. if (timeline_admire(snac, object, actor, 1) == HTTP_STATUS_CREATED)
  1602. snac_log(snac, xs_fmt("new '%s' %s %s", type, actor, object));
  1603. else
  1604. snac_log(snac, xs_fmt("repeated '%s' from %s to %s", type, actor, object));
  1605. do_notify = 1;
  1606. }
  1607. else
  1608. if (strcmp(type, "Announce") == 0) { /** **/
  1609. if (xs_type(object) == XSTYPE_DICT)
  1610. object = xs_dict_get(object, "id");
  1611. if (xs_is_null(object))
  1612. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1613. else
  1614. if (is_muted(snac, actor) && !xs_startswith(object, snac->actor))
  1615. snac_log(snac, xs_fmt("dropped 'Announce' from muted actor %s", actor));
  1616. else
  1617. if (is_limited(snac, actor) && !xs_startswith(object, snac->actor))
  1618. snac_log(snac, xs_fmt("dropped 'Announce' from limited actor %s", actor));
  1619. else {
  1620. xs *a_msg = NULL;
  1621. xs *wrk = NULL;
  1622. timeline_request(snac, &object, &wrk, 0);
  1623. if (valid_status(object_get(object, &a_msg))) {
  1624. const char *who = get_atto(a_msg);
  1625. if (who && !is_muted(snac, who)) {
  1626. /* bring the actor */
  1627. xs *who_o = NULL;
  1628. if (valid_status(actor_request(snac, who, &who_o))) {
  1629. if (timeline_admire(snac, object, actor, 0) == HTTP_STATUS_CREATED)
  1630. snac_log(snac, xs_fmt("new 'Announce' %s %s", actor, object));
  1631. else
  1632. snac_log(snac, xs_fmt("repeated 'Announce' from %s to %s",
  1633. actor, object));
  1634. /* distribute the post with the actor as 'proxy' */
  1635. list_distribute(snac, actor, a_msg);
  1636. do_notify = 1;
  1637. }
  1638. else
  1639. snac_debug(snac, 1, xs_fmt("dropped 'Announce' on actor request error %s", who));
  1640. }
  1641. else
  1642. snac_log(snac, xs_fmt("ignored 'Announce' about muted actor %s", who));
  1643. }
  1644. else
  1645. snac_debug(snac, 2, xs_fmt("error requesting 'Announce' object %s", object));
  1646. }
  1647. }
  1648. else
  1649. if (strcmp(type, "Update") == 0) { /** **/
  1650. if (xs_match(utype, "Person|Service")) { /** **/
  1651. actor_add(actor, xs_dict_get(msg, "object"));
  1652. timeline_touch(snac);
  1653. snac_log(snac, xs_fmt("updated actor %s", actor));
  1654. }
  1655. else
  1656. if (xs_match(utype, "Note|Page|Article|Video")) { /** **/
  1657. const char *id = xs_dict_get(object, "id");
  1658. if (xs_is_null(id))
  1659. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1660. else
  1661. if (object_here(id)) {
  1662. object_add_ow(id, object);
  1663. timeline_touch(snac);
  1664. snac_log(snac, xs_fmt("updated '%s' %s", utype, id));
  1665. }
  1666. else
  1667. snac_log(snac, xs_fmt("dropped update for unknown '%s' %s", utype, id));
  1668. }
  1669. else
  1670. if (strcmp(utype, "Question") == 0) { /** **/
  1671. const char *id = xs_dict_get(object, "id");
  1672. const char *closed = xs_dict_get(object, "closed");
  1673. if (xs_is_null(id))
  1674. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1675. else {
  1676. object_add_ow(id, object);
  1677. timeline_touch(snac);
  1678. snac_log(snac, xs_fmt("%s poll %s", closed == NULL ? "updated" : "closed", id));
  1679. if (closed != NULL)
  1680. do_notify = 1;
  1681. }
  1682. }
  1683. else {
  1684. srv_archive_error("unsupported_update", "unsupported_update", req, msg);
  1685. snac_log(snac, xs_fmt("ignored 'Update' for object type '%s'", utype));
  1686. }
  1687. }
  1688. else
  1689. if (strcmp(type, "Delete") == 0) { /** **/
  1690. if (xs_type(object) == XSTYPE_DICT)
  1691. object = xs_dict_get(object, "id");
  1692. if (xs_is_null(object))
  1693. snac_log(snac, xs_fmt("malformed message: no 'id' field"));
  1694. else
  1695. if (object_here(object)) {
  1696. timeline_del(snac, object);
  1697. snac_debug(snac, 1, xs_fmt("new 'Delete' %s %s", actor, object));
  1698. }
  1699. else
  1700. snac_debug(snac, 1, xs_fmt("ignored 'Delete' for unknown object %s", object));
  1701. }
  1702. else
  1703. if (strcmp(type, "Pong") == 0) { /** **/
  1704. snac_log(snac, xs_fmt("'Pong' received from %s", actor));
  1705. }
  1706. else
  1707. if (strcmp(type, "Ping") == 0) { /** **/
  1708. const char *id = xs_dict_get(msg, "id");
  1709. snac_log(snac, xs_fmt("'Ping' requested from %s", actor));
  1710. if (!xs_is_null(id)) {
  1711. xs *rsp = msg_pong(snac, actor, id);
  1712. enqueue_output_by_actor(snac, rsp, actor, 0);
  1713. }
  1714. }
  1715. else
  1716. if (strcmp(type, "Block") == 0) { /** **/
  1717. snac_log(snac, xs_fmt("'Block' received from %s", actor));
  1718. /* should we MUTE the actor back? */
  1719. /* mute(snac, actor); */
  1720. do_notify = 1;
  1721. }
  1722. else
  1723. if (strcmp(type, "Move") == 0) { /** **/
  1724. do_notify = 1;
  1725. const char *old_account = xs_dict_get(msg, "object");
  1726. const char *new_account = xs_dict_get(msg, "target");
  1727. if (!xs_is_null(old_account) && !xs_is_null(new_account)) {
  1728. if (following_check(snac, old_account)) {
  1729. xs *n_actor = NULL;
  1730. if (valid_status(object_get(new_account, &n_actor))) {
  1731. const xs_list *aka = xs_dict_get(n_actor, "alsoKnownAs");
  1732. if (xs_type(aka) == XSTYPE_LIST) {
  1733. if (xs_list_in(aka, old_account) != -1) {
  1734. /* all conditions met! */
  1735. /* follow new account */
  1736. xs *f_msg = msg_follow(snac, new_account);
  1737. if (f_msg != NULL) {
  1738. const char *new_actor = xs_dict_get(f_msg, "object");
  1739. following_add(snac, new_actor, f_msg);
  1740. enqueue_output_by_actor(snac, f_msg, new_actor, 0);
  1741. snac_log(snac, xs_fmt("'Move': following %s", new_account));
  1742. }
  1743. /* unfollow old account */
  1744. xs *of_msg = NULL;
  1745. if (valid_status(following_get(snac, old_account, &of_msg))) {
  1746. xs *uf_msg = msg_undo(snac, xs_dict_get(of_msg, "object"));
  1747. following_del(snac, old_account);
  1748. enqueue_output_by_actor(snac, uf_msg, old_account, 0);
  1749. snac_log(snac, xs_fmt("'Move': unfollowing %s", old_account));
  1750. }
  1751. }
  1752. else
  1753. snac_log(snac, xs_fmt("'Move' error: old actor %s not found in %s 'alsoKnownAs'",
  1754. old_account, new_account));
  1755. }
  1756. else
  1757. snac_log(snac, xs_fmt("'Move' error: cannot get %s 'alsoKnownAs'", new_account));
  1758. }
  1759. else {
  1760. snac_log(snac, xs_fmt("'Move' error: cannot get new actor %s", new_account));
  1761. /* may be a server hiccup, retry later */
  1762. return 0;
  1763. }
  1764. }
  1765. else
  1766. snac_log(snac, xs_fmt("'Move' error: actor %s is not being followed", old_account));
  1767. }
  1768. else {
  1769. snac_log(snac, xs_fmt("'Move' error: malformed message from %s", actor));
  1770. srv_archive_error("move", "move", req, msg);
  1771. }
  1772. }
  1773. else {
  1774. srv_archive_error("unsupported_type", "unsupported_type", req, msg);
  1775. snac_debug(snac, 1, xs_fmt("process_input_message type '%s' ignored", type));
  1776. }
  1777. if (do_notify) {
  1778. notify(snac, type, utype, actor, msg);
  1779. timeline_touch(snac);
  1780. }
  1781. return 1;
  1782. }
  1783. int send_email(const char *msg)
  1784. /* invoke sendmail with email headers and body in msg */
  1785. {
  1786. FILE *f;
  1787. int status;
  1788. int fds[2];
  1789. pid_t pid;
  1790. if (pipe(fds) == -1) return -1;
  1791. pid = vfork();
  1792. if (pid == -1) return -1;
  1793. else if (pid == 0) {
  1794. dup2(fds[0], 0);
  1795. close(fds[0]);
  1796. close(fds[1]);
  1797. execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
  1798. _exit(1);
  1799. }
  1800. close(fds[0]);
  1801. if ((f = fdopen(fds[1], "w")) == NULL) {
  1802. close(fds[1]);
  1803. return -1;
  1804. }
  1805. fprintf(f, "%s\n", msg);
  1806. fclose(f);
  1807. if (waitpid(pid, &status, 0) == -1) return -1;
  1808. return status;
  1809. }
  1810. void process_user_queue_item(snac *snac, xs_dict *q_item)
  1811. /* processes an item from the user queue */
  1812. {
  1813. const char *type;
  1814. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1815. if ((type = xs_dict_get(q_item, "type")) == NULL)
  1816. type = "output";
  1817. if (strcmp(type, "message") == 0) {
  1818. const xs_dict *msg = xs_dict_get(q_item, "message");
  1819. xs *rcpts = recipient_list(snac, msg, 1);
  1820. xs_set inboxes;
  1821. const xs_str *actor;
  1822. int c;
  1823. xs_set_init(&inboxes);
  1824. /* iterate the recipients */
  1825. c = 0;
  1826. while (xs_list_next(rcpts, &actor, &c)) {
  1827. xs *inbox = get_actor_inbox(actor, 1);
  1828. if (inbox != NULL) {
  1829. /* add to the set and, if it's not there, send message */
  1830. if (xs_set_add(&inboxes, inbox) == 1)
  1831. enqueue_output(snac, msg, inbox, 0, 0);
  1832. }
  1833. else
  1834. snac_log(snac, xs_fmt("cannot find inbox for %s", actor));
  1835. }
  1836. /* if it's a public note or question, send to the collected inboxes */
  1837. if (xs_match(xs_dict_get_def(msg, "type", ""), "Create|Update") && is_msg_public(msg)) {
  1838. if (xs_type(xs_dict_get(srv_config, "disable_inbox_collection")) != XSTYPE_TRUE) {
  1839. xs *shibx = inbox_list();
  1840. const xs_str *inbox;
  1841. c = 0;
  1842. while (xs_list_next(shibx, &inbox, &c)) {
  1843. if (xs_set_add(&inboxes, inbox) == 1)
  1844. enqueue_output(snac, msg, inbox, 0, 0);
  1845. }
  1846. }
  1847. }
  1848. xs_set_free(&inboxes);
  1849. }
  1850. else
  1851. if (strcmp(type, "input") == 0) {
  1852. /* process the message */
  1853. const xs_dict *msg = xs_dict_get(q_item, "message");
  1854. const xs_dict *req = xs_dict_get(q_item, "req");
  1855. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1856. if (xs_is_null(msg))
  1857. return;
  1858. if (!process_input_message(snac, msg, req)) {
  1859. if (retries > queue_retry_max)
  1860. snac_log(snac, xs_fmt("input giving up"));
  1861. else {
  1862. /* reenqueue */
  1863. enqueue_input(snac, msg, req, retries + 1);
  1864. snac_log(snac, xs_fmt("input requeue #%d", retries + 1));
  1865. }
  1866. }
  1867. }
  1868. else
  1869. if (strcmp(type, "close_question") == 0) {
  1870. /* the time for this question has ended */
  1871. const char *id = xs_dict_get(q_item, "message");
  1872. if (!xs_is_null(id))
  1873. update_question(snac, id);
  1874. }
  1875. else
  1876. if (strcmp(type, "object_request") == 0) {
  1877. const char *id = xs_dict_get(q_item, "message");
  1878. if (!xs_is_null(id)) {
  1879. int status;
  1880. xs *data = NULL;
  1881. status = activitypub_request(snac, id, &data);
  1882. if (valid_status(status))
  1883. object_add_ow(id, data);
  1884. snac_debug(snac, 1, xs_fmt("object_request %s %d", id, status));
  1885. }
  1886. }
  1887. else
  1888. if (strcmp(type, "verify_links") == 0) {
  1889. verify_links(snac);
  1890. }
  1891. else
  1892. if (strcmp(type, "actor_refresh") == 0) {
  1893. const char *actor = xs_dict_get(q_item, "actor");
  1894. double mtime = object_mtime(actor);
  1895. /* only refresh if it was refreshed more than an hour ago */
  1896. if (mtime + 3600.0 < (double) time(NULL)) {
  1897. xs *actor_o = NULL;
  1898. int status;
  1899. if (valid_status((status = activitypub_request(snac, actor, &actor_o))))
  1900. actor_add(actor, actor_o);
  1901. else
  1902. object_touch(actor);
  1903. snac_log(snac, xs_fmt("actor_refresh %s %d", actor, status));
  1904. }
  1905. }
  1906. else
  1907. snac_log(snac, xs_fmt("unexpected user q_item type '%s'", type));
  1908. }
  1909. int process_user_queue(snac *snac)
  1910. /* processes a user's queue */
  1911. {
  1912. int cnt = 0;
  1913. xs *list = user_queue(snac);
  1914. xs_list *p = list;
  1915. const xs_str *fn;
  1916. while (xs_list_iter(&p, &fn)) {
  1917. xs *q_item = dequeue(fn);
  1918. if (q_item == NULL)
  1919. continue;
  1920. process_user_queue_item(snac, q_item);
  1921. cnt++;
  1922. }
  1923. return cnt;
  1924. }
  1925. void process_queue_item(xs_dict *q_item)
  1926. /* processes an item from the global queue */
  1927. {
  1928. const char *type = xs_dict_get(q_item, "type");
  1929. int queue_retry_max = xs_number_get(xs_dict_get(srv_config, "queue_retry_max"));
  1930. if (strcmp(type, "output") == 0) {
  1931. int status;
  1932. const xs_str *inbox = xs_dict_get(q_item, "inbox");
  1933. const xs_str *keyid = xs_dict_get(q_item, "keyid");
  1934. const xs_str *seckey = xs_dict_get(q_item, "seckey");
  1935. const xs_dict *msg = xs_dict_get(q_item, "message");
  1936. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  1937. int p_status = xs_number_get(xs_dict_get(q_item, "p_status"));
  1938. xs *payload = NULL;
  1939. int p_size = 0;
  1940. int timeout = 0;
  1941. if (xs_is_null(inbox) || xs_is_null(msg) || xs_is_null(keyid) || xs_is_null(seckey)) {
  1942. srv_log(xs_fmt("output message error: missing fields"));
  1943. return;
  1944. }
  1945. if (is_instance_blocked(inbox)) {
  1946. srv_debug(0, xs_fmt("discarded output message to blocked instance %s", inbox));
  1947. return;
  1948. }
  1949. /* deliver (if previous error status was a timeout, try now longer) */
  1950. if (p_status == 599)
  1951. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout_2", "8"));
  1952. else
  1953. timeout = xs_number_get(xs_dict_get_def(srv_config, "queue_timeout", "6"));
  1954. if (timeout == 0)
  1955. timeout = 6;
  1956. status = send_to_inbox_raw(keyid, seckey, inbox, msg, &payload, &p_size, timeout);
  1957. if (payload) {
  1958. if (p_size > 64) {
  1959. /* trim the message */
  1960. payload[64] = '\0';
  1961. payload = xs_str_cat(payload, "...");
  1962. }
  1963. /* strip ugly control characters */
  1964. payload = xs_replace_i(payload, "\n", "");
  1965. payload = xs_replace_i(payload, "\r", "");
  1966. if (*payload)
  1967. payload = xs_str_wrap_i(" [", payload, "]");
  1968. }
  1969. else
  1970. payload = xs_str_new(NULL);
  1971. srv_log(xs_fmt("output message: sent to inbox %s %d%s", inbox, status, payload));
  1972. if (!valid_status(status)) {
  1973. retries++;
  1974. /* if it's not the first time it fails with a timeout,
  1975. penalize the server by skipping one retry */
  1976. if (p_status == status && status == HTTP_STATUS_CLIENT_CLOSED_REQUEST)
  1977. retries++;
  1978. /* error sending; requeue? */
  1979. if (status == HTTP_STATUS_BAD_REQUEST
  1980. || status == HTTP_STATUS_NOT_FOUND
  1981. || status == HTTP_STATUS_METHOD_NOT_ALLOWED
  1982. || status == HTTP_STATUS_GONE
  1983. || status == HTTP_STATUS_UNPROCESSABLE_CONTENT
  1984. || status < 0)
  1985. /* explicit error: discard */
  1986. srv_log(xs_fmt("output message: fatal error %s %d", inbox, status));
  1987. else
  1988. if (retries > queue_retry_max)
  1989. srv_log(xs_fmt("output message: giving up %s %d", inbox, status));
  1990. else {
  1991. /* requeue */
  1992. enqueue_output_raw(keyid, seckey, msg, inbox, retries, status);
  1993. srv_log(xs_fmt("output message: requeue %s #%d", inbox, retries));
  1994. }
  1995. }
  1996. }
  1997. else
  1998. if (strcmp(type, "email") == 0) {
  1999. /* send this email */
  2000. const xs_str *msg = xs_dict_get(q_item, "message");
  2001. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2002. if (!send_email(msg))
  2003. srv_debug(1, xs_fmt("email message sent"));
  2004. else {
  2005. retries++;
  2006. if (retries > queue_retry_max)
  2007. srv_log(xs_fmt("email giving up (errno: %d)", errno));
  2008. else {
  2009. /* requeue */
  2010. srv_log(xs_fmt(
  2011. "email requeue #%d (errno: %d)", retries, errno));
  2012. enqueue_email(msg, retries);
  2013. }
  2014. }
  2015. }
  2016. else
  2017. if (strcmp(type, "telegram") == 0) {
  2018. /* send this via telegram */
  2019. const char *bot = xs_dict_get(q_item, "bot");
  2020. const char *msg = xs_dict_get(q_item, "message");
  2021. xs *chat_id = xs_dup(xs_dict_get(q_item, "chat_id"));
  2022. int status = 0;
  2023. /* chat_id must start with a - */
  2024. if (!xs_startswith(chat_id, "-"))
  2025. chat_id = xs_str_wrap_i("-", chat_id, NULL);
  2026. xs *url = xs_fmt("https:/" "/api.telegram.org/bot%s/sendMessage", bot);
  2027. xs *body = xs_fmt("{\"chat_id\":%s,\"text\":\"%s\"}", chat_id, msg);
  2028. xs *headers = xs_dict_new();
  2029. headers = xs_dict_append(headers, "content-type", "application/json");
  2030. xs *rsp = xs_http_request("POST", url, headers,
  2031. body, strlen(body), &status, NULL, NULL, 0);
  2032. rsp = xs_free(rsp);
  2033. srv_debug(0, xs_fmt("telegram post %d", status));
  2034. }
  2035. else
  2036. if (strcmp(type, "ntfy") == 0) {
  2037. /* send this via ntfy */
  2038. const char *ntfy_server = xs_dict_get(q_item, "ntfy_server");
  2039. const char *msg = xs_dict_get(q_item, "message");
  2040. const char *ntfy_token = xs_dict_get(q_item, "ntfy_token");
  2041. int status = 0;
  2042. xs *url = xs_fmt("%s", ntfy_server);
  2043. xs *body = xs_fmt("%s", msg);
  2044. xs *headers = xs_dict_new();
  2045. headers = xs_dict_append(headers, "content-type", "text/plain");
  2046. // Append the Authorization header only if ntfy_token is not NULL
  2047. if (ntfy_token != NULL) {
  2048. headers = xs_dict_append(headers, "Authorization", xs_fmt("Bearer %s", ntfy_token));
  2049. }
  2050. xs *rsp = xs_http_request("POST", url, headers,
  2051. body, strlen(body), &status, NULL, NULL, 0);
  2052. rsp = xs_free(rsp);
  2053. srv_debug(0, xs_fmt("ntfy post %d", status));
  2054. }
  2055. else
  2056. if (strcmp(type, "purge") == 0) {
  2057. srv_log(xs_dup("purge start"));
  2058. purge_all();
  2059. srv_log(xs_dup("purge end"));
  2060. }
  2061. else
  2062. if (strcmp(type, "input") == 0) {
  2063. const xs_dict *msg = xs_dict_get(q_item, "message");
  2064. const xs_dict *req = xs_dict_get(q_item, "req");
  2065. int retries = xs_number_get(xs_dict_get(q_item, "retries"));
  2066. /* do some instance-level checks */
  2067. int r = process_input_message(NULL, msg, req);
  2068. if (r == 0) {
  2069. /* transient error? retry */
  2070. if (retries > queue_retry_max)
  2071. srv_log(xs_fmt("shared input giving up"));
  2072. else {
  2073. /* reenqueue */
  2074. enqueue_shared_input(msg, req, retries + 1);
  2075. srv_log(xs_fmt("shared input requeue #%d", retries + 1));
  2076. }
  2077. }
  2078. else
  2079. if (r == 2) {
  2080. /* redistribute the input message to all users */
  2081. const char *ntid = xs_dict_get(q_item, "ntid");
  2082. xs *tmpfn = xs_fmt("%s/tmp/%s.json", srv_basedir, ntid);
  2083. FILE *f;
  2084. if ((f = fopen(tmpfn, "w")) != NULL) {
  2085. xs_json_dump(q_item, 4, f);
  2086. fclose(f);
  2087. }
  2088. xs *users = user_list();
  2089. xs_list *p = users;
  2090. const char *v;
  2091. int cnt = 0;
  2092. while (xs_list_iter(&p, &v)) {
  2093. snac user;
  2094. if (user_open(&user, v)) {
  2095. if (is_msg_for_me(&user, msg)) {
  2096. xs *fn = xs_fmt("%s/queue/%s.json", user.basedir, ntid);
  2097. snac_debug(&user, 1,
  2098. xs_fmt("enqueue_input (from shared inbox) %s", xs_dict_get(msg, "id")));
  2099. if (link(tmpfn, fn) < 0)
  2100. srv_log(xs_fmt("link(%s, %s) error", tmpfn, fn));
  2101. cnt++;
  2102. }
  2103. user_free(&user);
  2104. }
  2105. }
  2106. unlink(tmpfn);
  2107. if (cnt == 0) {
  2108. srv_debug(1, xs_fmt("no valid recipients for %s", xs_dict_get(msg, "id")));
  2109. }
  2110. }
  2111. }
  2112. else
  2113. srv_log(xs_fmt("unexpected q_item type '%s'", type));
  2114. }
  2115. int process_queue(void)
  2116. /* processes the global queue */
  2117. {
  2118. int cnt = 0;
  2119. xs *list = queue();
  2120. xs_list *p = list;
  2121. const xs_str *fn;
  2122. while (xs_list_iter(&p, &fn)) {
  2123. xs *q_item = dequeue(fn);
  2124. if (q_item != NULL) {
  2125. job_post(q_item, 0);
  2126. cnt++;
  2127. }
  2128. }
  2129. return cnt;
  2130. }
  2131. /** account migration **/
  2132. int migrate_account(snac *user)
  2133. /* migrates this account to a new one (stored in the 'alias' user field) */
  2134. {
  2135. const char *new_account = xs_dict_get(user->config, "alias");
  2136. if (xs_type(new_account) != XSTYPE_STRING) {
  2137. snac_log(user, xs_fmt("Cannot migrate: alias (destination account) not set"));
  2138. return 1;
  2139. }
  2140. xs *new_actor = NULL;
  2141. int status;
  2142. if (!valid_status(status = activitypub_request(user, new_account, &new_actor))) {
  2143. snac_log(user, xs_fmt("Cannot migrate: error requesting actor %s %d", new_account, status));
  2144. return 1;
  2145. }
  2146. actor_add(new_account, new_actor);
  2147. const char *loaka = xs_dict_get(new_actor, "alsoKnownAs");
  2148. if (xs_type(loaka) != XSTYPE_LIST) {
  2149. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have any aliases"));
  2150. return 1;
  2151. }
  2152. if (xs_list_in(loaka, user->actor) == -1) {
  2153. snac_log(user, xs_fmt("Cannot migrate: destination account doesn't have this one as an alias"));
  2154. return 1;
  2155. }
  2156. xs *move = msg_move(user, new_account);
  2157. xs *fwers = follower_list(user);
  2158. const char *actor;
  2159. xs_list_foreach(fwers, actor) {
  2160. /* get the actor inbox, excluding the shared one */
  2161. xs *inbox = get_actor_inbox(actor, 0);
  2162. if (xs_is_null(inbox))
  2163. snac_log(user, xs_fmt("migrate_account: cannot get inbox for actor %s", actor));
  2164. else
  2165. enqueue_output(user, move, inbox, 0, 0);
  2166. }
  2167. return 0;
  2168. }
  2169. /** HTTP handlers */
  2170. int activitypub_get_handler(const xs_dict *req, const char *q_path,
  2171. char **body, int *b_size, char **ctype)
  2172. {
  2173. int status = HTTP_STATUS_OK;
  2174. const char *accept = xs_dict_get(req, "accept");
  2175. snac snac;
  2176. xs *msg = NULL;
  2177. if (accept == NULL)
  2178. return 0;
  2179. if (xs_str_in(accept, "application/activity+json") == -1 &&
  2180. xs_str_in(accept, "application/ld+json") == -1)
  2181. return 0;
  2182. xs *l = xs_split_n(q_path, "/", 2);
  2183. const char *uid;
  2184. const char *p_path;
  2185. uid = xs_list_get(l, 1);
  2186. if (!user_open(&snac, uid)) {
  2187. /* invalid user */
  2188. srv_debug(1, xs_fmt("activitypub_get_handler bad user %s", uid));
  2189. return HTTP_STATUS_NOT_FOUND;
  2190. }
  2191. p_path = xs_list_get(l, 2);
  2192. *ctype = "application/activity+json";
  2193. if (p_path == NULL) {
  2194. /* if there was no component after the user, it's an actor request */
  2195. msg = msg_actor(&snac);
  2196. *ctype = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"";
  2197. const char *ua = xs_dict_get(req, "user-agent");
  2198. snac_debug(&snac, 0, xs_fmt("serving actor [%s]", ua ? ua : "No UA"));
  2199. }
  2200. else
  2201. if (strcmp(p_path, "outbox") == 0 || strcmp(p_path, "featured") == 0) {
  2202. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2203. xs *list = xs_list_new();
  2204. msg = msg_collection(&snac, id);
  2205. const char *v;
  2206. int tc = 0;
  2207. /* get the public outbox or the pinned list */
  2208. xs *elems = *p_path == 'o' ? timeline_simple_list(&snac, "public", 0, 20) : pinned_list(&snac);
  2209. while (xs_list_next(elems, &v, &tc)) {
  2210. xs *i = NULL;
  2211. if (valid_status(object_get_by_md5(v, &i))) {
  2212. const char *type = xs_dict_get(i, "type");
  2213. const char *id = xs_dict_get(i, "id");
  2214. if (type && id && strcmp(type, "Note") == 0 && xs_startswith(id, snac.actor)) {
  2215. xs *c_msg = msg_create(&snac, i);
  2216. list = xs_list_append(list, c_msg);
  2217. }
  2218. }
  2219. }
  2220. /* replace the 'orderedItems' with the latest posts */
  2221. xs *items = xs_number_new(xs_list_len(list));
  2222. msg = xs_dict_set(msg, "orderedItems", list);
  2223. msg = xs_dict_set(msg, "totalItems", items);
  2224. }
  2225. else
  2226. if (strcmp(p_path, "followers") == 0 || strcmp(p_path, "following") == 0) {
  2227. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2228. msg = msg_collection(&snac, id);
  2229. }
  2230. else
  2231. if (xs_startswith(p_path, "p/")) {
  2232. xs *id = xs_fmt("%s/%s", snac.actor, p_path);
  2233. status = object_get(id, &msg);
  2234. /* don't return non-public objects */
  2235. if (valid_status(status) && !is_msg_public(msg))
  2236. status = HTTP_STATUS_NOT_FOUND;
  2237. }
  2238. else
  2239. status = HTTP_STATUS_NOT_FOUND;
  2240. if (status == HTTP_STATUS_OK && msg != NULL) {
  2241. *body = xs_json_dumps(msg, 4);
  2242. *b_size = strlen(*body);
  2243. }
  2244. snac_debug(&snac, 1, xs_fmt("activitypub_get_handler serving %s %d", q_path, status));
  2245. user_free(&snac);
  2246. return status;
  2247. }
  2248. int activitypub_post_handler(const xs_dict *req, const char *q_path,
  2249. char *payload, int p_size,
  2250. char **body, int *b_size, char **ctype)
  2251. /* processes an input message */
  2252. {
  2253. (void)b_size;
  2254. int status = HTTP_STATUS_ACCEPTED;
  2255. const char *i_ctype = xs_dict_get(req, "content-type");
  2256. snac snac;
  2257. const char *v;
  2258. if (i_ctype == NULL) {
  2259. *body = xs_str_new("no content-type");
  2260. *ctype = "text/plain";
  2261. return HTTP_STATUS_BAD_REQUEST;
  2262. }
  2263. if (xs_is_null(payload)) {
  2264. *body = xs_str_new("no payload");
  2265. *ctype = "text/plain";
  2266. return HTTP_STATUS_BAD_REQUEST;
  2267. }
  2268. if (xs_str_in(i_ctype, "application/activity+json") == -1 &&
  2269. xs_str_in(i_ctype, "application/ld+json") == -1)
  2270. return 0;
  2271. /* decode the message */
  2272. xs *msg = xs_json_loads(payload);
  2273. const char *id = xs_dict_get(msg, "id");
  2274. if (msg == NULL) {
  2275. srv_log(xs_fmt("activitypub_post_handler JSON error %s", q_path));
  2276. srv_archive_error("activitypub_post_handler", "JSON error", req, payload);
  2277. *body = xs_str_new("JSON error");
  2278. *ctype = "text/plain";
  2279. return HTTP_STATUS_BAD_REQUEST;
  2280. }
  2281. if (id && is_instance_blocked(id)) {
  2282. srv_debug(1, xs_fmt("full instance block for %s", id));
  2283. *body = xs_str_new("blocked");
  2284. *ctype = "text/plain";
  2285. return HTTP_STATUS_FORBIDDEN;
  2286. }
  2287. /* get the user and path */
  2288. xs *l = xs_split_n(q_path, "/", 2);
  2289. if (xs_list_len(l) == 2 && strcmp(xs_list_get(l, 1), "shared-inbox") == 0) {
  2290. enqueue_shared_input(msg, req, 0);
  2291. return HTTP_STATUS_ACCEPTED;
  2292. }
  2293. if (xs_list_len(l) != 3 || strcmp(xs_list_get(l, 2), "inbox") != 0) {
  2294. /* strange q_path */
  2295. srv_debug(1, xs_fmt("activitypub_post_handler unsupported path %s", q_path));
  2296. return HTTP_STATUS_NOT_FOUND;
  2297. }
  2298. const char *uid = xs_list_get(l, 1);
  2299. if (!user_open(&snac, uid)) {
  2300. /* invalid user */
  2301. srv_debug(1, xs_fmt("activitypub_post_handler bad user %s", uid));
  2302. return HTTP_STATUS_NOT_FOUND;
  2303. }
  2304. /* if it has a digest, check it now, because
  2305. later the payload won't be exactly the same */
  2306. if ((v = xs_dict_get(req, "digest")) != NULL) {
  2307. xs *s1 = xs_sha256_base64(payload, p_size);
  2308. xs *s2 = xs_fmt("SHA-256=%s", s1);
  2309. if (strcmp(s2, v) != 0) {
  2310. srv_log(xs_fmt("digest check FAILED"));
  2311. *body = xs_str_new("bad digest");
  2312. *ctype = "text/plain";
  2313. status = HTTP_STATUS_BAD_REQUEST;
  2314. }
  2315. }
  2316. /* if the message is from a muted actor, reject it right now */
  2317. if (!xs_is_null(v = xs_dict_get(msg, "actor")) && *v) {
  2318. if (is_muted(&snac, v)) {
  2319. snac_log(&snac, xs_fmt("rejected message from MUTEd actor %s", v));
  2320. *body = xs_str_new("rejected");
  2321. *ctype = "text/plain";
  2322. status = HTTP_STATUS_FORBIDDEN;
  2323. }
  2324. }
  2325. if (valid_status(status)) {
  2326. enqueue_input(&snac, msg, req, 0);
  2327. *ctype = "application/activity+json";
  2328. }
  2329. user_free(&snac);
  2330. return status;
  2331. }