activitypub.c 94 KB

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