activitypub.c 92 KB

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