activitypub.c 93 KB

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