activitypub.c 93 KB

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