activitypub.c 101 KB

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